corth-lang / Corth

A self-hosted stack based language like Forth
MIT License
7 stars 1 forks source link

Enums #47

Open HuseyinSimsek7904 opened 6 months ago

HuseyinSimsek7904 commented 6 months ago

Take a look at this piece of code:

macro KEYWORD-UNKNOWN      0  endmacro
macro KEYWORD-MACRO        1  endmacro
macro KEYWORD-ENDMACRO     2  endmacro
macro KEYWORD-PROC         3  endmacro
macro KEYWORD-RETURNS      4  endmacro
macro KEYWORD-IN           5  endmacro
macro KEYWORD-END          6  endmacro
macro KEYWORD-RETURN       7  endmacro
macro KEYWORD-MEMORY       8  endmacro
macro KEYWORD-AND          9  endmacro
macro KEYWORD-LET          10 endmacro
macro KEYWORD-PEEK         11 endmacro
macro KEYWORD-IF           12 endmacro
macro KEYWORD-ELSE         13 endmacro
macro KEYWORD-WHILE        14 endmacro
macro KEYWORD-DO           15 endmacro
macro KEYWORD-BREAK        16 endmacro
macro KEYWORD-INCLUDE      17 endmacro
macro KEYWORD-CAST         18 endmacro
macro KEYWORD-SIZEOF       19 endmacro
macro KEYWORD-NAMESPACE    20 endmacro
macro KEYWORD-ENDNAMESPACE 21 endmacro

This code could be rewritten using enums (and namespaces). For example:

namespace KEYWORD enum 
  MACRO
  ENDMACRO
  PROC
  RETURNS
  IN
  ...
end endnamespace

In this example, namespace keyword is used to prepend every name with KEYWORD: and enum is used to repeatedly and easily define macros that expend to a single integer that is different every time.

This new code is much easier to write and read. Adding the enum keyword will improve the readability of the code.

HuseyinSimsek7904 commented 5 months ago

A similar keyword to the const keyword from Porth programming language may also be implemented.

Utkub24 commented 3 months ago

Would proper sum types be a good fit for Corth instead of C-style enumerations? Take a look at Rust's enums.