Support while … else

This commit is contained in:
2025-04-13 18:40:49 +02:00
parent 8ec407515a
commit 1cd44508c3
6 changed files with 145 additions and 135 deletions

View File

@ -1,7 +1,7 @@
const
SEEK_SET* := 0;
SEEK_CUR* := 1;
SEEK_END* := 2;
SEEK_SET* := 0
SEEK_CUR* := 1
SEEK_END* := 2
type
TokenKind* = (
@ -65,27 +65,27 @@ type
_defer,
exclamation,
arrow
);
)
Position* = record
line: Word;
column: Word
end;
end
Location* = record
first: Position;
last: Position
end;
end
SourceFile* = record
buffer: [1024]Char;
handle: ^FILE;
size: Word;
index: Word
end;
FILE* = record end;
end
FILE* = record end
StringBuffer* = record
data: ^Byte;
size: Word;
capacity: Word
end;
end
SourceCode = record
position: Position;
@ -93,7 +93,7 @@ type
empty: proc(^Byte) -> Bool;
advance: proc(^Byte);
head: proc(^Byte) -> Char
end;
end
Token* = record
kind: TokenKind;
value: union
@ -103,12 +103,12 @@ type
char_value: Char
end;
location: Location
end;
end
CommandLine* = record
input: ^Char;
tokenize: Bool;
syntax_tree: Bool
end;
end
(*
External procedures.
@ -171,9 +171,9 @@ end
proc write_i(value: Int);
var
digit: Int;
n: Word;
buffer: [10]Char;
digit: Int
n: Word
buffer: [10]Char
begin
n := 10u;
@ -230,7 +230,7 @@ end
proc string_dup(origin: String) -> String;
var
copy: ^Char;
copy: ^Char
begin
copy := cast(malloc(origin.length): ^Char);
strncpy(copy, origin.ptr, origin.length);
@ -240,7 +240,7 @@ end
proc string_buffer_new() -> StringBuffer;
var
result: StringBuffer;
result: StringBuffer
begin
result.capacity := 64u;
result.data := malloc(result.capacity);
@ -266,7 +266,7 @@ end
proc string_buffer_clear(buffer: ^StringBuffer) -> String;
var
result: String;
result: String
begin
result := String(cast(buffer^.data: ^Char), buffer^.size);
buffer^.size := 0u;
@ -284,8 +284,8 @@ end
proc read_source(filename: ^Char) -> ^SourceFile;
var
result: ^SourceFile;
file_handle: ^FILE;
result: ^SourceFile
file_handle: ^FILE
begin
file_handle := fopen(filename, "rb\0".ptr);
@ -300,7 +300,7 @@ end
proc escape_char(escape: Char, result: ^Char) -> Bool;
var
successful: Bool;
successful: Bool
begin
if escape = 'n' then
result^ := '\n';
@ -346,7 +346,7 @@ end
proc source_file_empty(source_input: ^Byte) -> Bool;
var
source_file: ^SourceFile;
source_file: ^SourceFile
begin
source_file := cast(source_input: ^SourceFile);
@ -360,7 +360,7 @@ end
proc source_file_head(source_input: ^Byte) -> Char;
var
source_file: ^SourceFile;
source_file: ^SourceFile
begin
source_file := cast(source_input: ^SourceFile);
@ -369,7 +369,7 @@ end
proc source_file_advance(source_input: ^Byte);
var
source_file: ^SourceFile;
source_file: ^SourceFile
begin
source_file := cast(source_input: ^SourceFile);
@ -405,7 +405,7 @@ end
proc skip_spaces(source_code: ^SourceCode);
var
current: Char;
current: Char
begin
while ~source_code_empty(source_code), loop do
current := source_code_head(source_code^);
@ -426,7 +426,7 @@ end
proc lex_identifier(source_code: ^SourceCode, token_content: ^StringBuffer);
var
content_length: Word;
content_length: Word
begin
while ~source_code_empty(source_code) & is_ident(source_code_head(source_code^)) do
string_buffer_push(token_content, source_code_head(source_code^));
@ -436,7 +436,7 @@ end
proc lex_comment(source_code: ^SourceCode, token_content: ^StringBuffer) -> Bool;
var
trailing: Word;
trailing: Word
begin
trailing := 0u;
@ -459,7 +459,7 @@ end
proc lex_character(source_code: ^SourceCode, token_content: ^Char) -> Bool;
var
successful: Bool;
successful: Bool
begin
successful := ~source_code_empty(source_code);
@ -481,10 +481,10 @@ end
proc lex_string(source_code: ^SourceCode, token_content: ^StringBuffer) -> Bool;
var
token_end, constructed_string: ^Char;
token_length: Word;
is_valid: Bool;
next_char: Char;
token_end, constructed_string: ^Char
token_length: Word
is_valid: Bool
next_char: Char
begin
is_valid := true;
@ -517,8 +517,8 @@ end
proc print_tokens(tokens: ^Token, tokens_size: Word);
var
current_token: ^Token;
i: Word;
current_token: ^Token
i: Word
begin
i := 0u;
while i < tokens_size do
@ -667,7 +667,7 @@ end
proc categorize_identifier(token_content: String) -> Token;
var
current_token: Token;
current_token: Token
begin
if "if" = token_content then
current_token.kind := TokenKind._if
@ -737,9 +737,9 @@ end
proc tokenize(source_code: SourceCode, tokens_size: ^Word) -> ^Token;
var
tokens, current_token: ^Token;
first_char: Char;
token_buffer: StringBuffer;
tokens, current_token: ^Token
first_char: Char
token_buffer: StringBuffer
begin
tokens_size^ := 0u;
tokens := nil;
@ -914,9 +914,9 @@ end
proc parse_command_line*(argc: Int, argv: ^^Char) -> ^CommandLine;
var
parameter: ^^Char;
i: Int;
result: ^CommandLine;
parameter: ^^Char
i: Int
result: ^CommandLine
begin
i := 1;
result := cast(malloc(#size(CommandLine)): ^CommandLine);
@ -955,11 +955,11 @@ end
proc process(argc: Int, argv: ^^Char) -> Int;
var
tokens: ^Token;
tokens_size: Word;
source_code: SourceCode;
command_line: ^CommandLine;
return_code: Int;
tokens: ^Token
tokens_size: Word
source_code: SourceCode
command_line: ^CommandLine
return_code: Int
begin
return_code := 0;