diff options
Diffstat (limited to 'source/main.elna')
| -rw-r--r-- | source/main.elna | 56 |
1 files changed, 33 insertions, 23 deletions
diff --git a/source/main.elna b/source/main.elna index bdcaf34..23fdc97 100644 --- a/source/main.elna +++ b/source/main.elna @@ -20,9 +20,9 @@ type position: ElnaLocation; input: Pointer; - empty: proc(stream: Pointer): Bool; + empty: proc(stream: Pointer) -> Bool; advance: proc(stream: Pointer); - head: proc(stream: Pointer): Word8 + head: proc(stream: Pointer) -> Word8 end Tokenizer* = record length: Word; @@ -32,10 +32,10 @@ type (* Standard procedures. *) -proc reallocarray(ptr: Pointer; n: Word; size: Word): Pointer +proc reallocarray(ptr: Pointer; n: Word; size: Word) -> Pointer return realloc(ptr, n * size) -proc string_dup(origin: []const Word8): []const Word8 +proc string_dup(origin: []const Word8) -> []const Word8 var copy: ^Word8 begin @@ -43,7 +43,7 @@ begin strncpy(copy, origin.ptr, origin.length) return copy[1 to origin.length] -proc string_buffer_new(): StringBuffer +proc string_buffer_new() -> StringBuffer var result: StringBuffer begin @@ -67,7 +67,7 @@ begin buffer^.size := buffer^.size - count return -proc string_buffer_clear(buffer: ^StringBuffer): []const Word8 +proc string_buffer_clear(buffer: ^StringBuffer) -> []const Word8 var result: []const Word8 begin @@ -79,7 +79,7 @@ return result Source code stream procedures. *) -proc read_source(filename: ^Word8): ^SourceFile +proc read_source(filename: ^Word8) -> ^SourceFile var result: ^SourceFile file_handle: ^FILE @@ -94,7 +94,7 @@ begin end return result -proc source_file_empty(source_input: Pointer): Bool +proc source_file_empty(source_input: Pointer) -> Bool var source_file: ^SourceFile begin @@ -106,7 +106,7 @@ begin end return source_file^.size = 0u -proc source_file_head(source_input: Pointer): Word8 +proc source_file_head(source_input: Pointer) -> Word8 var source_file: ^SourceFile begin @@ -122,10 +122,10 @@ begin source_file^.index := source_file^.index + 1u return -proc source_code_empty(source_code: ^SourceCode): Bool +proc source_code_empty(source_code: ^SourceCode) -> Bool return source_code^.empty(source_code^.input) -proc source_code_head(source_code: SourceCode): Word8 +proc source_code_head(source_code: SourceCode) -> Word8 return source_code.head(source_code.input) proc source_code_advance(source_code: ^SourceCode) @@ -140,14 +140,14 @@ begin source_code^.position.column := 0u return -proc source_code_expect(source_code: ^SourceCode; expected: Word8): Bool +proc source_code_expect(source_code: ^SourceCode; expected: Word8) -> Bool return ~source_code_empty(source_code) & source_code_head(source_code^) = expected (* Token procedures. *) -proc lexer_escape(escape: Word8; result: ^Word8): Bool +proc lexer_escape(escape: Word8; result: ^Word8) -> Bool var successful: Bool begin @@ -200,7 +200,7 @@ begin return (* Checker whether the character is allowed in an identificator. *) -proc lexer_is_ident(char: Word8): Bool +proc lexer_is_ident(char: Word8) -> Bool return isalnum(cast(char: Int)) <> 0 or char = '_' proc lexer_identifier(source_code: ^SourceCode; token_content: ^StringBuffer) @@ -213,7 +213,7 @@ begin end return -proc lexer_comment(source_code: ^SourceCode; token_content: ^StringBuffer): Bool +proc lexer_comment(source_code: ^SourceCode; token_content: ^StringBuffer) -> Bool var trailing: Word begin @@ -234,7 +234,7 @@ begin end return trailing = 2u -proc lexer_character(source_code: ^SourceCode; token_content: ^Word8): Bool +proc lexer_character(source_code: ^SourceCode; token_content: ^Word8) -> Bool var successful: Bool begin @@ -255,7 +255,7 @@ begin end return successful -proc lexer_string(source_code: ^SourceCode; token_content: ^StringBuffer): Bool +proc lexer_string(source_code: ^SourceCode; token_content: ^StringBuffer) -> Bool var token_end, constructed_string: ^Word8 token_length: Word @@ -289,7 +289,7 @@ begin return (* Categorize an identifier. *) -proc lexer_categorize(token_content: []const Word8): ^ElnaLexerToken +proc lexer_categorize(token_content: []const Word8) -> ^ElnaLexerToken var current_token: ^ElnaLexerToken begin @@ -385,7 +385,7 @@ begin return (* Read the next token from the input. *) -proc lexer_next(source_code: SourceCode; token_buffer: ^StringBuffer): ^ElnaLexerToken +proc lexer_next(source_code: SourceCode; token_buffer: ^StringBuffer) -> ^ElnaLexerToken var current_token: ^ElnaLexerToken := nil first_char: Word8 @@ -523,7 +523,15 @@ begin elsif first_char = '-' then source_code_advance(@source_code); current_token := malloc(#size(ElnaLexerToken)); - current_token^.kind := ElnaLexerKind.minus + + if source_code_empty(@source_code) then + current_token^.kind := ElnaLexerKind.minus + elsif source_code_head(source_code) = '>' then + current_token^.kind := ElnaLexerKind.arrow; + source_code_advance(@source_code) + else + current_token^.kind := ElnaLexerKind.minus + end elsif first_char = '*' then current_token := malloc(#size(ElnaLexerToken)); current_token^.kind := ElnaLexerKind.multiplication; @@ -576,7 +584,7 @@ begin return current_token (* Split the source text into tokens. *) -proc lexer_text(source_code: SourceCode): Tokenizer +proc lexer_text(source_code: SourceCode) -> Tokenizer var current_token: ^ElnaLexerToken token_buffer: StringBuffer @@ -739,6 +747,8 @@ begin write_s("DEFER") | ElnaLexerKind.exclamation: write_c('!') + | ElnaLexerKind.arrow: + write_s("->") | ElnaLexerKind._import: write_s("IMPORT") else @@ -757,7 +767,7 @@ return Compilation entry. *) -proc compile_in_stages(command_line: ^CommandLine; source_code: SourceCode): Int +proc compile_in_stages(command_line: ^CommandLine; source_code: SourceCode) -> Int var return_code: Int := 0 lexer: Tokenizer @@ -770,7 +780,7 @@ begin end return return_code -proc process(argc: Int; argv: ^^Word8): Int +proc process(argc: Int; argv: ^^Word8) -> Int var tokens: ^ElnaLexerToken tokens_size: Word |
