Implement defer

This commit is contained in:
2025-02-07 22:12:59 +01:00
parent 077de53c74
commit d39d2368a0
12 changed files with 335 additions and 218 deletions

View File

@ -1,60 +1,61 @@
const
SEEK_SET = 0; SEEK_CUR = 1; SEEK_END = 2;
SEEK_SET* = 0; 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_AS = 28; TOKEN_SIZEOF = 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_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_AS* = 28; TOKEN_SIZEOF* = 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;
type
Position = record
Position* = record
line: Word;
column: Word
end,
Location = record
Location* = record
first: Position;
last: Position
end,
TokenValue = union
TokenValue* = union
int_value: Int;
string_value: pointer to Char;
boolean_value: Bool;
char_value: Char
end,
Token = record
Token* = record
kind: Int;
value: TokenValue;
location: Location
end,
FILE = record
FILE* = record
dummy: Int
end,
CommandLine = record
CommandLine* = record
input: pointer to Char;
tokenize: Bool
tokenize: Bool;
syntax_tree: Bool
end,
Literal = record
Literal* = record
value: Int
end,
ConstantDefinition = record
ConstantDefinition* = record
name: pointer to Char;
body: pointer to Literal
end,
ConstantPart = record
ConstantPart* = record
elements: pointer to pointer to ConstantDefinition;
count: Word
end,
Program = record
Program* = record
constants: ConstantPart
end;
@ -457,6 +458,8 @@ begin
write_s("c>")
elsif current_token^.kind = TOKEN_STRING then
write_s("\"...\"")
elsif current_token^.kind = TOKEN_DEFER then
write_s("DEFER")
else
write_s("UNKNOWN<");
write_i(current_token^.kind);
@ -533,6 +536,8 @@ begin
current_token.kind := TOKEN_AS
elsif strncmp("sizeof", input_pointer, token_length) = 0 then
current_token.kind := TOKEN_SIZEOF
elsif strncmp("defer", input_pointer, token_length) = 0 then
current_token.kind := TOKEN_DEFER
else
current_token.kind := TOKEN_IDENTIFIER;
current_token.value.string_value := cast(calloc(token_length + 1, 1) as pointer to Char);
@ -756,7 +761,7 @@ begin
end
end;
proc parse_command_line(argc: Int, argv: pointer to pointer to Char): pointer to CommandLine;
proc parse_command_line*(argc: Int, argv: pointer to pointer to Char): pointer to CommandLine;
var
parameter: pointer to pointer to Char,
i: Int,
@ -765,6 +770,7 @@ begin
i := 1;
result := cast(malloc(sizeof(CommandLine)) as pointer to CommandLine);
result^.tokenize := false;
result^.syntax_tree := false;
result^.input := nil;
while i < argc do
@ -772,6 +778,8 @@ begin
if strcmp(parameter^, "--tokenize") = 0 then
result^.tokenize := true
elsif strcmp(parameter^, "--syntax-tree") = 0 then
result^.syntax_tree := true
elsif parameter^^ <> '-' then
result^.input := parameter^
else
@ -802,10 +810,10 @@ var
command_line: pointer to CommandLine;
begin
command_line := parse_command_line(argc, argv);
if cast(command_line as Word) = 0u then
if command_line = nil then
return 2
end;
input := read_source(command_line^.input);
if input = nil then
perror(command_line^.input);
@ -816,8 +824,9 @@ begin
if command_line^.tokenize then
print_tokens(tokens, tokens_size)
end;
parse_program(@tokens, @tokens_size);
if command_line^.syntax_tree then
parse_program(@tokens, @tokens_size)
end;
return 0
end;