Implement enumeration type

This commit is contained in:
2025-04-04 22:48:12 +02:00
parent 50970f3289
commit 18c4e79012
14 changed files with 612 additions and 416 deletions

View File

@ -3,67 +3,69 @@ const
SEEK_CUR* = 1;
SEEK_END* = 2;
TOKEN_IDENTIFIER* = 1;
TOKEN_IF* = 2;
TOKEN_THEN* = 3;
TOKEN_ELSE* = 4;
TOKEN_ELSIF* = 5;
TOKEN_WHILE* = 6;
TOKEN_DO* = 7;
TOKEN_PROC* = 8;
TOKEN_BEGIN* = 9;
TOKEN_END* = 10;
TOKEN_EXTERN* = 11;
TOKEN_CONST* = 12;
TOKEN_VAR* = 13;
TOKEN_ARRAY* = 14;
TOKEN_OF* = 15;
TOKEN_TYPE* = 16;
TOKEN_RECORD* = 17;
TOKEN_UNION* = 18;
TOKEN_POINTER* = 19;
TOKEN_TO* = 20;
TOKEN_BOOLEAN* = 21;
TOKEN_NIL* = 22;
TOKEN_AND* = 23;
TOKEN_OR* = 24;
TOKEN_NOT* = 25;
TOKEN_RETURN* = 26;
TOKEN_CAST* = 27;
TOKEN_SHIFT_LEFT* = 28;
TOKEN_SHIFT_RIGHT* = 29;
TOKEN_LEFT_PAREN* = 30;
TOKEN_RIGHT_PAREN* = 31;
TOKEN_LEFT_SQUARE* = 32;
TOKEN_RIGHT_SQUARE* = 33;
TOKEN_GREATER_EQUAL* = 34;
TOKEN_LESS_EQUAL* = 35;
TOKEN_GREATER_THAN* = 36;
TOKEN_LESS_THAN* = 37;
TOKEN_NOT_EQUAL* = 38;
TOKEN_EQUAL* = 39;
TOKEN_SEMICOLON* = 40;
TOKEN_DOT* = 41;
TOKEN_COMMA* = 42;
TOKEN_PLUS* = 43;
TOKEN_MINUS* = 44;
TOKEN_MULTIPLICATION* = 45;
TOKEN_DIVISION* = 46;
TOKEN_REMAINDER* = 47;
TOKEN_ASSIGNMENT* = 48;
TOKEN_COLON* = 49;
TOKEN_HAT* = 50;
TOKEN_AT* = 51;
TOKEN_COMMENT* = 52;
TOKEN_INTEGER* = 53;
TOKEN_WORD* = 54;
TOKEN_CHARACTER* = 55;
TOKEN_STRING* = 56;
TOKEN_DEFER* = 57;
TOKEN_EXCLAMATION* = 58;
TOKEN_ARROW = 59;
type
TokenKind* = (
unknown,
identifier,
_if,
_then,
_else,
_elsif,
_while,
_do,
_proc,
_begin,
_end,
_extern,
_const,
_var,
array,
_of,
_type,
_record,
_union,
pointer,
to,
boolean,
_nil,
and,
_or,
not,
_return,
_cast,
shift_left,
shift_right,
left_paren,
right_paren,
left_square,
right_square,
greater_equal,
less_equal,
greater_than,
less_than,
not_equal,
equal,
semicolon,
dot,
comma,
plus,
minus,
multiplication,
division,
remainder,
assignment,
colon,
hat,
at,
comment,
integer,
word,
character,
string,
_defer,
exclamation,
arrow
);
Position* = record
line: Word;
column: Word
@ -93,7 +95,7 @@ type
head: proc(^Byte) -> Char
end;
Token* = record
kind: Int;
kind: TokenKind;
value: union
int_value: Int;
string: String;
@ -208,12 +210,12 @@ end
proc is_alnum(c: Char) -> Bool;
begin
return is_digit(c) | is_alpha(c)
return is_digit(c) or is_alpha(c)
end
proc is_space(c: Char) -> Bool;
begin
return c = ' ' | c = '\n' | c = '\t'
return c = ' ' or c = '\n' or c = '\t'
end
proc substring(string: String, start: Word, count: Word) -> String;
@ -413,7 +415,7 @@ end
proc is_ident(char: Char) -> Bool;
begin
return is_alnum(char) | char = '_'
return is_alnum(char) or char = '_'
end
proc lex_identifier(source_code: ^SourceCode, token_content: ^StringBuffer);
@ -516,137 +518,137 @@ begin
while i < tokens_size do
current_token := tokens + i;
if current_token^.kind = TOKEN_IF then
if current_token^.kind = TokenKind._if then
write_s("IF")
elsif current_token^.kind = TOKEN_THEN then
elsif current_token^.kind = TokenKind._then then
write_s("THEN")
elsif current_token^.kind = TOKEN_ELSE then
elsif current_token^.kind = TokenKind._else then
write_s("ELSE")
elsif current_token^.kind = TOKEN_ELSIF then
elsif current_token^.kind = TokenKind._elsif then
write_s("ELSIF")
elsif current_token^.kind = TOKEN_WHILE then
elsif current_token^.kind = TokenKind._while then
write_s("WHILE")
elsif current_token^.kind = TOKEN_DO then
elsif current_token^.kind = TokenKind._do then
write_s("DO")
elsif current_token^.kind = TOKEN_PROC then
elsif current_token^.kind = TokenKind._proc then
write_s("PROC")
elsif current_token^.kind = TOKEN_BEGIN then
elsif current_token^.kind = TokenKind._begin then
write_s("BEGIN")
elsif current_token^.kind = TOKEN_END then
elsif current_token^.kind = TokenKind._end then
write_s("END")
elsif current_token^.kind = TOKEN_EXTERN then
elsif current_token^.kind = TokenKind._extern then
write_s("EXTERN")
elsif current_token^.kind = TOKEN_CONST then
elsif current_token^.kind = TokenKind._const then
write_s("CONST")
elsif current_token^.kind = TOKEN_VAR then
elsif current_token^.kind = TokenKind._var then
write_s("VAR")
elsif current_token^.kind = TOKEN_ARRAY then
elsif current_token^.kind = TokenKind.array then
write_s("ARRAY")
elsif current_token^.kind = TOKEN_OF then
elsif current_token^.kind = TokenKind._of then
write_s("OF")
elsif current_token^.kind = TOKEN_TYPE then
elsif current_token^.kind = TokenKind._type then
write_s("TYPE")
elsif current_token^.kind = TOKEN_RECORD then
elsif current_token^.kind = TokenKind._record then
write_s("RECORD")
elsif current_token^.kind = TOKEN_UNION then
elsif current_token^.kind = TokenKind._union then
write_s("UNION")
elsif current_token^.kind = TOKEN_POINTER then
elsif current_token^.kind = TokenKind.pointer then
write_s("POINTER")
elsif current_token^.kind = TOKEN_TO then
elsif current_token^.kind = TokenKind.to then
write_s("TO")
elsif current_token^.kind = TOKEN_BOOLEAN then
elsif current_token^.kind = TokenKind.boolean then
write_s("BOOLEAN<");
write_b(current_token^.value.boolean_value);
write_c('>')
elsif current_token^.kind = TOKEN_NIL then
elsif current_token^.kind = TokenKind._nil then
write_s("NIL")
elsif current_token^.kind = TOKEN_AND then
elsif current_token^.kind = TokenKind.and then
write_s("AND")
elsif current_token^.kind = TOKEN_OR then
elsif current_token^.kind = TokenKind._or then
write_s("OR")
elsif current_token^.kind = TOKEN_NOT then
elsif current_token^.kind = TokenKind.not then
write_s("NOT")
elsif current_token^.kind = TOKEN_RETURN then
elsif current_token^.kind = TokenKind._return then
write_s("RETURN")
elsif current_token^.kind = TOKEN_CAST then
elsif current_token^.kind = TokenKind._cast then
write_s("CAST")
elsif current_token^.kind = TOKEN_SHIFT_LEFT then
elsif current_token^.kind = TokenKind.shift_left then
write_s("<<")
elsif current_token^.kind = TOKEN_SHIFT_RIGHT then
elsif current_token^.kind = TokenKind.shift_right then
write_s(">>")
elsif current_token^.kind = TOKEN_IDENTIFIER then
elsif current_token^.kind = TokenKind.identifier then
write_c('<');
write_s(current_token^.value.string);
write_c('>')
elsif current_token^.kind = TOKEN_LEFT_PAREN then
elsif current_token^.kind = TokenKind.left_paren then
write_s("(")
elsif current_token^.kind = TOKEN_RIGHT_PAREN then
elsif current_token^.kind = TokenKind.right_paren then
write_s(")")
elsif current_token^.kind = TOKEN_LEFT_SQUARE then
elsif current_token^.kind = TokenKind.left_square then
write_s("[")
elsif current_token^.kind = TOKEN_RIGHT_SQUARE then
elsif current_token^.kind = TokenKind.right_square then
write_s("]")
elsif current_token^.kind = TOKEN_GREATER_EQUAL then
elsif current_token^.kind = TokenKind.greater_equal then
write_s(">=")
elsif current_token^.kind = TOKEN_LESS_EQUAL then
elsif current_token^.kind = TokenKind.less_equal then
write_s("<=")
elsif current_token^.kind = TOKEN_GREATER_THAN then
elsif current_token^.kind = TokenKind.greater_than then
write_s(">")
elsif current_token^.kind = TOKEN_LESS_THAN then
elsif current_token^.kind = TokenKind.less_than then
write_s("<")
elsif current_token^.kind = TOKEN_EQUAL then
elsif current_token^.kind = TokenKind.equal then
write_s("=")
elsif current_token^.kind = TOKEN_NOT_EQUAL then
elsif current_token^.kind = TokenKind.not_equal then
write_s("<>")
elsif current_token^.kind = TOKEN_SEMICOLON then
elsif current_token^.kind = TokenKind.semicolon then
write_c(';')
elsif current_token^.kind = TOKEN_DOT then
elsif current_token^.kind = TokenKind.dot then
write_c('.')
elsif current_token^.kind = TOKEN_COMMA then
elsif current_token^.kind = TokenKind.comma then
write_c(',')
elsif current_token^.kind = TOKEN_PLUS then
elsif current_token^.kind = TokenKind.plus then
write_c('+')
elsif current_token^.kind = TOKEN_MINUS then
elsif current_token^.kind = TokenKind.minus then
write_c('-')
elsif current_token^.kind = TOKEN_MULTIPLICATION then
elsif current_token^.kind = TokenKind.multiplication then
write_c('*')
elsif current_token^.kind = TOKEN_DIVISION then
elsif current_token^.kind = TokenKind.division then
write_c('/')
elsif current_token^.kind = TOKEN_REMAINDER then
elsif current_token^.kind = TokenKind.remainder then
write_c('%')
elsif current_token^.kind = TOKEN_ASSIGNMENT then
elsif current_token^.kind = TokenKind.assignment then
write_s(":=")
elsif current_token^.kind = TOKEN_COLON then
elsif current_token^.kind = TokenKind.colon then
write_c(':')
elsif current_token^.kind = TOKEN_HAT then
elsif current_token^.kind = TokenKind.hat then
write_c('^')
elsif current_token^.kind = TOKEN_AT then
elsif current_token^.kind = TokenKind.at then
write_c('@')
elsif current_token^.kind = TOKEN_COMMENT then
elsif current_token^.kind = TokenKind.comment then
write_s("(* COMMENT *)")
elsif current_token^.kind = TOKEN_INTEGER then
elsif current_token^.kind = TokenKind.integer then
write_c('<');
write_i(current_token^.value.int_value);
write_c('>')
elsif current_token^.kind = TOKEN_WORD then
elsif current_token^.kind = TokenKind.word then
write_c('<');
write_i(current_token^.value.int_value);
write_s("u>")
elsif current_token^.kind = TOKEN_CHARACTER then
elsif current_token^.kind = TokenKind.character then
write_c('<');
write_i(cast(current_token^.value.char_value: Int));
write_s("c>")
elsif current_token^.kind = TOKEN_STRING then
elsif current_token^.kind = TokenKind.string then
write_s("\"...\"")
elsif current_token^.kind = TOKEN_DEFER then
elsif current_token^.kind = TokenKind._defer then
write_s("DEFER")
elsif current_token^.kind = TOKEN_EXCLAMATION then
elsif current_token^.kind = TokenKind.exclamation then
write_c('!')
elsif current_token^.kind = TOKEN_ARROW then
elsif current_token^.kind = TokenKind.arrow then
write_s("->")
else
write_s("UNKNOWN<");
write_i(current_token^.kind);
write_i(cast(current_token^.kind: Int));
write_c('>')
end;
write_c(' ');
@ -661,65 +663,65 @@ var
current_token: Token;
begin
if "if" = token_content then
current_token.kind := TOKEN_IF
current_token.kind := TokenKind._if
elsif "then" = token_content then
current_token.kind := TOKEN_THEN
current_token.kind := TokenKind._then
elsif "else" = token_content then
current_token.kind := TOKEN_ELSE
current_token.kind := TokenKind._else
elsif "elsif" = token_content then
current_token.kind := TOKEN_ELSIF
current_token.kind := TokenKind._elsif
elsif "while" = token_content then
current_token.kind := TOKEN_WHILE
current_token.kind := TokenKind._while
elsif "do" = token_content then
current_token.kind := TOKEN_DO
current_token.kind := TokenKind._do
elsif "proc" = token_content then
current_token.kind := TOKEN_PROC
current_token.kind := TokenKind._proc
elsif "begin" = token_content then
current_token.kind := TOKEN_BEGIN
current_token.kind := TokenKind._begin
elsif "end" = token_content then
current_token.kind := TOKEN_END
current_token.kind := TokenKind._end
elsif "extern" = token_content then
current_token.kind := TOKEN_EXTERN
current_token.kind := TokenKind._extern
elsif "const" = token_content then
current_token.kind := TOKEN_CONST
current_token.kind := TokenKind._const
elsif "var" = token_content then
current_token.kind := TOKEN_VAR
current_token.kind := TokenKind._var
elsif "array" = token_content then
current_token.kind := TOKEN_ARRAY
current_token.kind := TokenKind.array
elsif "of" = token_content then
current_token.kind := TOKEN_OF
current_token.kind := TokenKind._of
elsif "type" = token_content then
current_token.kind := TOKEN_TYPE
current_token.kind := TokenKind._type
elsif "record" = token_content then
current_token.kind := TOKEN_RECORD
current_token.kind := TokenKind._record
elsif "union" = token_content then
current_token.kind := TOKEN_UNION
current_token.kind := TokenKind._union
elsif "pointer" = token_content then
current_token.kind := TOKEN_POINTER
current_token.kind := TokenKind.pointer
elsif "to" = token_content then
current_token.kind := TOKEN_TO
current_token.kind := TokenKind.to
elsif "true" = token_content then
current_token.kind := TOKEN_BOOLEAN;
current_token.kind := TokenKind.boolean;
current_token.value.boolean_value := true
elsif "false" = token_content then
current_token.kind := TOKEN_BOOLEAN;
current_token.kind := TokenKind.boolean;
current_token.value.boolean_value := false
elsif "nil" = token_content then
current_token.kind := TOKEN_NIL
current_token.kind := TokenKind._nil
elsif "and" = token_content then
current_token.kind := TOKEN_AND
current_token.kind := TokenKind.and
elsif "or" = token_content then
current_token.kind := TOKEN_OR
current_token.kind := TokenKind._or
elsif "not" = token_content then
current_token.kind := TOKEN_NOT
current_token.kind := TokenKind.not
elsif "return" = token_content then
current_token.kind := TOKEN_RETURN
current_token.kind := TokenKind._return
elsif "cast" = token_content then
current_token.kind := TOKEN_CAST
current_token.kind := TokenKind._cast
elsif "defer" = token_content then
current_token.kind := TOKEN_DEFER
current_token.kind := TokenKind._defer
else
current_token.kind := TOKEN_IDENTIFIER;
current_token.kind := TokenKind.identifier;
current_token.value.string := string_dup(token_content)
end;
@ -743,154 +745,154 @@ begin
current_token := tokens + tokens_size^;
first_char := source_code_head(source_code);
if is_alpha(first_char) | first_char = '_' then
if is_alpha(first_char) or first_char = '_' then
lex_identifier(@source_code, @token_buffer);
current_token^ := categorize_identifier(string_buffer_clear(@token_buffer))
elsif is_digit(first_char) then
lex_number(@source_code, @current_token^.value.int_value);
if source_code_expect(@source_code, 'u') then
current_token^.kind := TOKEN_WORD;
current_token^.kind := TokenKind.word;
source_code_advance(@source_code)
else
current_token^.kind := TOKEN_INTEGER
current_token^.kind := TokenKind.integer
end
elsif first_char = '(' then
source_code_advance(@source_code);
if source_code_empty(@source_code) then
current_token^.kind := TOKEN_LEFT_PAREN
current_token^.kind := TokenKind.left_paren
elsif source_code_head(source_code) = '*' then
source_code_advance(@source_code);
if lex_comment(@source_code, @token_buffer) then
current_token^.value.string := string_dup(string_buffer_clear(@token_buffer));
current_token^.kind := TOKEN_COMMENT
current_token^.kind := TokenKind.comment
else
current_token^.kind := 0
current_token^.kind := TokenKind.unknown
end
else
current_token^.kind := TOKEN_LEFT_PAREN
current_token^.kind := TokenKind.left_paren
end
elsif first_char = ')' then
current_token^.kind := TOKEN_RIGHT_PAREN;
current_token^.kind := TokenKind.right_paren;
source_code_advance(@source_code)
elsif first_char = '\'' then
source_code_advance(@source_code);
if lex_character(@source_code, @current_token^.value.char_value) & source_code_expect(@source_code, '\'') then
current_token^.kind := TOKEN_CHARACTER;
current_token^.kind := TokenKind.character;
source_code_advance(@source_code)
else
current_token^.kind := 0
current_token^.kind := TokenKind.unknown
end
elsif first_char = '"' then
source_code_advance(@source_code);
if lex_string(@source_code, @token_buffer) then
current_token^.kind := TOKEN_STRING;
current_token^.kind := TokenKind.string;
current_token^.value.string := string_dup(string_buffer_clear(@token_buffer))
else
current_token^.kind := 0
current_token^.kind := TokenKind.unknown
end
elsif first_char = '[' then
current_token^.kind := TOKEN_LEFT_SQUARE;
current_token^.kind := TokenKind.left_square;
source_code_advance(@source_code)
elsif first_char = ']' then
current_token^.kind := TOKEN_RIGHT_SQUARE;
current_token^.kind := TokenKind.right_square;
source_code_advance(@source_code)
elsif first_char = '>' then
source_code_advance(@source_code);
if source_code_empty(@source_code) then
current_token^.kind := TOKEN_GREATER_THAN
current_token^.kind := TokenKind.greater_than
elsif source_code_head(source_code) = '=' then
current_token^.kind := TOKEN_GREATER_EQUAL;
current_token^.kind := TokenKind.greater_equal;
source_code_advance(@source_code)
elsif source_code_head(source_code) = '>' then
current_token^.kind := TOKEN_SHIFT_RIGHT;
current_token^.kind := TokenKind.shift_right;
source_code_advance(@source_code)
else
current_token^.kind := TOKEN_GREATER_THAN
current_token^.kind := TokenKind.greater_than
end
elsif first_char = '<' then
source_code_advance(@source_code);
if source_code_empty(@source_code) then
current_token^.kind := TOKEN_LESS_THAN
current_token^.kind := TokenKind.less_than
elsif source_code_head(source_code) = '=' then
current_token^.kind := TOKEN_LESS_EQUAL;
current_token^.kind := TokenKind.less_equal;
source_code_advance(@source_code)
elsif source_code_head(source_code) = '<' then
current_token^.kind := TOKEN_SHIFT_LEFT;
current_token^.kind := TokenKind.shift_left;
source_code_advance(@source_code)
elsif source_code_head(source_code) = '>' then
current_token^.kind := TOKEN_NOT_EQUAL;
current_token^.kind := TokenKind.not_equal;
source_code_advance(@source_code)
else
current_token^.kind := TOKEN_LESS_THAN
current_token^.kind := TokenKind.less_than
end
elsif first_char = '=' then
current_token^.kind := TOKEN_EQUAL;
current_token^.kind := TokenKind.equal;
source_code_advance(@source_code)
elsif first_char = ';' then
current_token^.kind := TOKEN_SEMICOLON;
current_token^.kind := TokenKind.semicolon;
source_code_advance(@source_code)
elsif first_char = '.' then
current_token^.kind := TOKEN_DOT;
current_token^.kind := TokenKind.dot;
source_code_advance(@source_code)
elsif first_char = ',' then
current_token^.kind := TOKEN_COMMA;
current_token^.kind := TokenKind.comma;
source_code_advance(@source_code)
elsif first_char = '+' then
current_token^.kind := TOKEN_PLUS;
current_token^.kind := TokenKind.plus;
source_code_advance(@source_code)
elsif first_char = '-' then
source_code_advance(@source_code);
if source_code_empty(@source_code) then
current_token^.kind := TOKEN_MINUS
current_token^.kind := TokenKind.minus
elsif source_code_head(source_code) = '>' then
current_token^.kind := TOKEN_ARROW;
current_token^.kind := TokenKind.arrow;
source_code_advance(@source_code)
else
current_token^.kind := TOKEN_MINUS
current_token^.kind := TokenKind.minus
end
elsif first_char = '*' then
current_token^.kind := TOKEN_MULTIPLICATION;
current_token^.kind := TokenKind.multiplication;
source_code_advance(@source_code)
elsif first_char = '/' then
current_token^.kind := TOKEN_DIVISION;
current_token^.kind := TokenKind.division;
source_code_advance(@source_code)
elsif first_char = '%' then
current_token^.kind := TOKEN_REMAINDER;
current_token^.kind := TokenKind.remainder;
source_code_advance(@source_code)
elsif first_char = ':' then
source_code_advance(@source_code);
if source_code_empty(@source_code) then
current_token^.kind := TOKEN_COLON
current_token^.kind := TokenKind.colon
elsif source_code_head(source_code) = '=' then
current_token^.kind := TOKEN_ASSIGNMENT;
current_token^.kind := TokenKind.assignment;
source_code_advance(@source_code)
else
current_token^.kind := TOKEN_COLON
current_token^.kind := TokenKind.colon
end
elsif first_char = '^' then
current_token^.kind := TOKEN_HAT;
current_token^.kind := TokenKind.hat;
source_code_advance(@source_code)
elsif first_char = '@' then
current_token^.kind := TOKEN_AT;
current_token^.kind := TokenKind.at;
source_code_advance(@source_code)
elsif first_char = '!' then
current_token^.kind := TOKEN_EXCLAMATION;
current_token^.kind := TokenKind.exclamation;
source_code_advance(@source_code)
else
current_token^.kind := 0;
current_token^.kind := TokenKind.unknown;
source_code_advance(@source_code)
end;
if current_token^.kind <> 0 then
if current_token^.kind <> TokenKind.unknown then
tokens_size^ := tokens_size^ + 1u;
skip_spaces(@source_code)
else