summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--boot/stage13.elna43
-rw-r--r--boot/stage14.elna571
2 files changed, 252 insertions, 362 deletions
diff --git a/boot/stage13.elna b/boot/stage13.elna
index 925a1cd..f4fa817 100644
--- a/boot/stage13.elna
+++ b/boot/stage13.elna
@@ -6,6 +6,7 @@
(* - Multiline comments. *)
(* - elsif conditions. *)
+(* - Optional "begin" if the procedure body is a single return statement. *)
const
symbol_builtin_name_int := "Int";
@@ -71,7 +72,7 @@ begin
if _load_byte(string) <> '"' then
counter := counter + 1;
- goto .string_length_loop;
+ goto .string_length_loop
end;
return counter
@@ -100,9 +101,9 @@ begin
contents := contents + 1;
if current_byte <> '\\' then
- compiler_strings_length := compiler_strings_length + 1;
+ compiler_strings_length := compiler_strings_length + 1
end;
- goto .add_string_loop;
+ goto .add_string_loop
end;
return result
@@ -145,10 +146,10 @@ begin
local_buffer := @result + 11;
if number >= 0 then
- is_negative := 0;
+ is_negative := 0
else
number = -number;
- is_negative := 1;
+ is_negative := 1
end;
.print_i_digit10;
@@ -159,11 +160,11 @@ begin
local_buffer := local_buffer + -1;
if number <> 0 then
- goto .print_i_digit10;
+ goto .print_i_digit10
end;
if is_negative = 1 then
_store_byte('-', local_buffer);
- local_buffer := local_buffer + -1;
+ local_buffer := local_buffer + -1
end;
result := @result + 11;
result := result + -local_buffer;
@@ -210,7 +211,7 @@ begin
_write_c(next_byte);
(* Advance the input string by one byte. *)
- _write_z(string + 1);
+ _write_z(string + 1)
end;
end;
@@ -314,8 +315,8 @@ begin
count := count + -1;
if result = 0 then
- goto .memcmp_loop;
- end;
+ goto .memcmp_loop
+ end
end;
return result
@@ -341,7 +342,7 @@ begin
destination := destination + 1;
source := source + 1;
count := count + -1;
- goto .memcpy_loop;
+ goto .memcpy_loop
end;
return destination
@@ -383,7 +384,7 @@ begin
character := _load_byte(source_code_position);
if character = '\\' then
_write_c('\\');
- source_code_position := source_code_position + 1;
+ source_code_position := source_code_position + 1
end;
_write_s(source_code_position, 1);
_write_s("'\n", 2);
@@ -981,9 +982,14 @@ end;
proc _skip_spaces();
var
current_byte: Word;
+ lhs: Word;
+ rhs: Word;
begin
current_byte := _load_byte(source_code_position);
- if current_byte = '\t' then
+ lhs := current_byte = '\t';
+ rhs := current_byte = ' ';
+
+ if lhs or rhs then
source_code_position := source_code_position + 1;
_skip_spaces();
end;
@@ -1194,9 +1200,14 @@ begin
_read_procedure_temporaries();
(* Skip semicolon, "begin" and newline. *)
- source_code_position := source_code_position + 6;
-
- _compile_procedure_body();
+ _lexer_read_token(@token_kind);
+ if token_kind = _lexer_token_kind_begin() then
+ _lexer_skip_token();
+ _compile_procedure_body();
+ end;
+ if token_kind = _lexer_token_kind_return() then
+ _compile_return_statement();
+ end;
(* Write the epilogue. *)
_write_z("\tlw ra, 124(sp)\n\tlw s0, 120(sp)\n\taddi sp, sp, 128\n\tret\n\0");
diff --git a/boot/stage14.elna b/boot/stage14.elna
index fd3d2d3..e3091dc 100644
--- a/boot/stage14.elna
+++ b/boot/stage14.elna
@@ -18,7 +18,6 @@ const
PRIMITIVE_TYPE = 1
Primitive types have only type size. *)
-
symbol_builtin_type_int := S(1, 4);
symbol_builtin_type_word := S(1, 4);
symbol_builtin_type_pointer := S(1, 4);
@@ -32,7 +31,6 @@ const
INFO_TEMPORARY = 3
Type info has the type it belongs to. *)
-
symbol_type_info_int := S(1, @symbol_builtin_type_int);
symbol_type_info_word := S(1, @symbol_builtin_type_word);
symbol_type_info_pointer := S(1, @symbol_builtin_type_pointer);
@@ -74,7 +72,7 @@ begin
if _load_byte(string) <> '"' then
counter := counter + 1;
- goto .string_length_loop;
+ goto .string_length_loop
end;
return counter
@@ -105,9 +103,9 @@ begin
contents := contents + 1;
if current_byte <> '\\' then
- compiler_strings_length := compiler_strings_length + 1;
+ compiler_strings_length := compiler_strings_length + 1
end;
- goto .add_string_loop;
+ goto .add_string_loop
end;
return result
@@ -123,7 +121,6 @@ end;
* Returns the amount of bytes written in a0.
*)
proc _read_file(buffer: Word, size: Word);
-begin
return _syscall(0, buffer, size, 0, 0, 0, 63)
end;
@@ -136,7 +133,7 @@ end;
*)
proc _write_s(buffer: Word, size: Word);
begin
- _syscall(1, buffer, size, 0, 0, 0, 64);
+ _syscall(1, buffer, size, 0, 0, 0, 64)
end;
(**
@@ -158,10 +155,10 @@ begin
local_buffer := @result + 11;
if number >= 0 then
- is_negative := 0;
+ is_negative := 0
else
number = -number;
- is_negative := 1;
+ is_negative := 1
end;
.print_i_digit10;
@@ -172,10 +169,11 @@ begin
local_buffer := local_buffer + -1;
if number <> 0 then
- goto .print_i_digit10;
- elsif is_negative = 1 then
+ goto .print_i_digit10
+ end;
+ if is_negative = 1 then
_store_byte('-', local_buffer);
- local_buffer := local_buffer + -1;
+ local_buffer := local_buffer + -1
end;
result := @result + 11;
result := result + -local_buffer;
@@ -196,7 +194,7 @@ var
length: Word;
begin
length := _print_i(number, @local_buffer);
- _write_s(@local_buffer, length);
+ _write_s(@local_buffer, length)
end;
(**
@@ -207,7 +205,7 @@ end;
*)
proc _write_c(character: Word);
begin
- _write_s(@character, 1);
+ _write_s(@character, 1)
end;
(**
@@ -228,11 +226,13 @@ begin
_write_c(next_byte);
(* Advance the input string by one byte. *)
- _write_z(string + 1);
- end;
+ _write_z(string + 1)
+ end
end;
-(* Detects if a0 is an uppercase character. Sets a0 to 1 if so, otherwise to 0. *)
+(**
+ * Detects if a0 is an uppercase character. Sets a0 to 1 if so, otherwise to 0.
+ *)
proc _is_upper(character: Word);
var
lhs: Word;
@@ -245,7 +245,9 @@ begin
end;
-(* Detects if a0 is an lowercase character. Sets a0 to 1 if so, otherwise to 0. *)
+(**
+ * Detects if a0 is an lowercase character. Sets a0 to 1 if so, otherwise to 0.
+ *)
proc _is_lower(character: Word);
var
lhs: Word;
@@ -281,8 +283,7 @@ begin
end;
(**
- * Detects whether the passed character is a digit
- * (a value between 0 and 9).
+ * Detects whether the passed character is a digit (a value between 0 and 9).
*
* Parameters:
* character - Exemined value.
@@ -338,8 +339,8 @@ begin
count := count + -1;
if result = 0 then
- goto .memcmp_loop;
- end;
+ goto .memcmp_loop
+ end
end;
return result
@@ -367,7 +368,7 @@ begin
destination := destination + 1;
source := source + 1;
count := count + -1;
- goto .memcpy_loop;
+ goto .memcpy_loop
end;
return destination
@@ -398,7 +399,7 @@ begin
_write_token(integer_token);
_lexer_skip_token();
- _write_c('\n');
+ _write_c('\n')
end;
proc _compile_character_literal();
@@ -411,24 +412,24 @@ begin
character := _load_byte(source_code_position);
if character = '\\' then
_write_c('\\');
- source_code_position := source_code_position + 1;
+ source_code_position := source_code_position + 1
end;
_write_s(source_code_position, 1);
_write_s("'\n", 2);
- source_code_position := source_code_position + 2;
+ source_code_position := source_code_position + 2
end;
proc _compile_variable_expression();
begin
_compile_designator();
- _write_z("\tlw t0, (t0)\n\0");
+ _write_z("\tlw t0, (t0)\n\0")
end;
proc _compile_address_expression();
begin
(* Skip the "@" sign. *)
source_code_position := source_code_position + 1;
- _compile_designator();
+ _compile_designator()
end;
proc _compile_negate_expression();
@@ -437,7 +438,7 @@ begin
source_code_position := source_code_position + 1;
_compile_term();
- _write_z("\tneg t0, t0\n\0");
+ _write_z("\tneg t0, t0\n\0")
end;
proc _compile_not_expression();
@@ -446,7 +447,7 @@ begin
source_code_position := source_code_position + 1;
_compile_term();
- _write_z("\tnot t0, t0\n\0");
+ _write_z("\tnot t0, t0\n\0")
end;
proc _compile_string_literal();
@@ -465,7 +466,7 @@ begin
_write_i(offset);
_write_c('\n');
- _write_z("\tadd t0, t0, t1\n\0");
+ _write_z("\tadd t0, t0, t1\n\0")
end;
proc _compile_term();
@@ -475,23 +476,23 @@ begin
current_character := _load_byte(source_code_position);
if current_character = '\'' then
- _compile_character_literal();
+ _compile_character_literal()
elsif current_character = '@' then
- _compile_address_expression();
+ _compile_address_expression()
elsif current_character = '-' then
- _compile_negate_expression();
+ _compile_negate_expression()
elsif current_character = '~' then
- _compile_not_expression();
+ _compile_not_expression()
elsif current_character = '"' then
- _compile_string_literal();
+ _compile_string_literal()
elsif current_character = '_' then
_compile_call();
- _write_z("\nmv t0, a0\n\0");
+ _write_z("\nmv t0, a0\n\0")
elsif _is_digit(current_character) = 1 then
- _compile_integer_literal();
+ _compile_integer_literal()
elsif _is_lower(current_character) = 1 then
- _compile_variable_expression();
- end;
+ _compile_variable_expression()
+ end
end;
proc _compile_binary_rhs();
@@ -501,7 +502,7 @@ begin
_compile_term();
(* Load the left expression from the stack; *)
- _write_z("\tlw t1, 64(sp)\n\0");
+ _write_z("\tlw t1, 64(sp)\n\0")
end;
proc _compile_expression();
@@ -512,7 +513,7 @@ begin
current_character := _load_byte(source_code_position);
if current_character <> ' ' then
- goto .compile_expression_end;
+ goto .compile_expression_end
end;
(* It is a binary expression. *)
@@ -528,49 +529,49 @@ begin
_compile_binary_rhs();
(* Execute the operation. *)
- _write_z("add t0, t0, t1\n\0");
+ _write_z("add t0, t0, t1\n\0")
elsif current_character = '*' then
source_code_position := source_code_position + 1;
_compile_binary_rhs();
(* Execute the operation. *)
- _write_z("\tmul t0, t0, t1\n\0");
+ _write_z("\tmul t0, t0, t1\n\0")
elsif current_character = '&' then
source_code_position := source_code_position + 1;
_compile_binary_rhs();
(* Execute the operation. *)
- _write_z("\tand t0, t0, t1\n\0");
+ _write_z("\tand t0, t0, t1\n\0")
elsif current_character = 'o' then
source_code_position := source_code_position + 2;
_compile_binary_rhs();
(* Execute the operation. *)
- _write_z("or t0, t0, t1\n\0");
+ _write_z("or t0, t0, t1\n\0")
elsif current_character = 'x' then
source_code_position := source_code_position + 3;
_compile_binary_rhs();
(* Execute the operation. *)
- _write_z("xor t0, t0, t1\n\0");
+ _write_z("xor t0, t0, t1\n\0")
elsif current_character = '=' then
source_code_position := source_code_position + 1;
_compile_binary_rhs();
(* Execute the operation. *)
- _write_z("xor t0, t0, t1\nseqz t0, t0\n\0");
+ _write_z("xor t0, t0, t1\nseqz t0, t0\n\0")
elsif current_character = '%' then
source_code_position := source_code_position + 1;
_compile_binary_rhs();
(* Execute the operation. *)
- _write_z("rem t0, t1, t0\n\0");
+ _write_z("rem t0, t1, t0\n\0")
elsif current_character = '/' then
source_code_position := source_code_position + 1;
_compile_binary_rhs();
(* Execute the operation. *)
- _write_z("div t0, t1, t0\n\0");
+ _write_z("div t0, t1, t0\n\0")
elsif current_character = '<' then
source_code_position := source_code_position + 1;
current_character := _load_byte(source_code_position);
@@ -580,19 +581,19 @@ begin
_compile_binary_rhs();
(* Execute the operation. *)
- _write_z("\txor t0, t0, t1\nsnez t0, t0\n\0");
+ _write_z("\txor t0, t0, t1\nsnez t0, t0\n\0")
elsif current_character = '=' then
source_code_position := source_code_position + 1;
_compile_binary_rhs();
(* Execute the operation. *)
- _write_z("\tslt t0, t0, t1\nxori t0, t0, 1\n\0");
+ _write_z("\tslt t0, t0, t1\nxori t0, t0, 1\n\0")
else
_compile_binary_rhs();
(* Execute the operation. *)
- _write_z("slt t0, t1, t0\n\0");
- end;
+ _write_z("slt t0, t1, t0\n\0")
+ end
elsif current_character = '>' then
source_code_position := source_code_position + 1;
current_character := _load_byte(source_code_position);
@@ -601,13 +602,13 @@ begin
_compile_binary_rhs();
(* Execute the operation. *)
- _write_z("\tslt t0, t1, t0\nxori t0, t0, 1\n\0");
+ _write_z("\tslt t0, t1, t0\nxori t0, t0, 1\n\0")
else
_compile_binary_rhs();
(* Execute the operation. *)
- _write_z("\tslt t0, t0, t1\n\0");
- end;
+ _write_z("\tslt t0, t0, t1\n\0")
+ end
end;
.compile_expression_end;
@@ -633,7 +634,7 @@ begin
source_code_position := source_code_position + 1;
if _load_byte(source_code_position) = ')' then
- goto .compile_call_finalize;
+ goto .compile_call_finalize
end;
.compile_call_loop;
_compile_expression();
@@ -651,7 +652,7 @@ begin
argument_count := argument_count + 1;
if _load_byte(source_code_position) <> ',' then
- goto .compile_call_finalize;
+ goto .compile_call_finalize
end;
source_code_position := source_code_position + 2;
goto .compile_call_loop;
@@ -673,7 +674,7 @@ begin
_write_z("(sp)\n\0");
- goto .compile_call_finalize;
+ goto .compile_call_finalize
end;
.compile_call_end;
@@ -681,7 +682,7 @@ begin
_write_s(name, name_length);
(* Skip the right paren. *)
- source_code_position := source_code_position + 1;
+ source_code_position := source_code_position + 1
end;
proc _compile_goto();
@@ -698,7 +699,7 @@ begin
_write_z("\tj .\0");
_write_token(next_token);
- _lexer_skip_token();
+ _lexer_skip_token()
end;
proc _compile_local_designator(symbol: Word);
@@ -709,7 +710,7 @@ begin
variable_offset := _parameter_info_get_offset(symbol);
_write_i(variable_offset);
_write_c('\n');
- _lexer_skip_token();
+ _lexer_skip_token()
end;
proc _compile_global_designator();
@@ -723,7 +724,7 @@ begin
_write_token(name);
_lexer_skip_token();
- _write_c('\n');
+ _write_c('\n')
end;
proc _compile_designator();
@@ -741,12 +742,10 @@ begin
lookup_result := _symbol_table_lookup(@symbol_table_local, name, name_token);
if lookup_result <> 0 then
- _compile_local_designator(lookup_result);
- goto .compile_designator_end;
- end;
- _compile_global_designator();
-
- .compile_designator_end;
+ _compile_local_designator(lookup_result)
+ else
+ _compile_global_designator()
+ end
end;
proc _compile_assignment();
@@ -762,7 +761,7 @@ begin
(* Compile the assignment. *)
_compile_expression();
- _write_z("\tlw t1, 60(sp)\nsw t0, (t1)\n\0");
+ _write_z("\tlw t1, 60(sp)\nsw t0, (t1)\n\0")
end;
proc _compile_return_statement();
@@ -775,7 +774,7 @@ begin
source_code_position := source_code_position + 1;
_compile_expression();
- _write_z("\tmv a0, t0\n\0");
+ _write_z("\tmv a0, t0\n\0")
end;
(**
@@ -787,18 +786,14 @@ end;
proc _write_label(counter: Word);
begin
_write_z(".L\0");
- _write_i(counter);
+ _write_i(counter)
end;
-proc _compile_if();
+proc _compile_condition(after_end_label: Word);
var
- after_end_label: Word;
condition_label: Word;
token_kind: Word;
begin
- (* Skip "if ". *)
- _lexer_read_token(@token_kind);
- _lexer_skip_token();
source_code_position := source_code_position + 1;
(* Compile condition. *)
@@ -807,9 +802,6 @@ begin
_lexer_read_token(@token_kind);
_lexer_skip_token();
- after_end_label := label_counter;
- label_counter := label_counter + 1;
-
(* condition_label is the label in front of the next elsif condition or end. *)
condition_label := label_counter;
label_counter := label_counter + 1;
@@ -825,47 +817,39 @@ begin
_write_c('\n');
_write_label(condition_label);
- _write_z(":\n\0");
+ _write_z(":\n\0")
+end;
+
+proc _compile_if();
+var
+ after_end_label: Word;
+ condition_label: Word;
+ token_kind: Word;
+begin
+ (* Skip "if ". *)
+ _lexer_read_token(@token_kind);
+ _lexer_skip_token();
+
+ after_end_label := label_counter;
+ label_counter := label_counter + 1;
+ _compile_condition(after_end_label);
.compile_if_loop;
_lexer_read_token(@token_kind);
if token_kind = _lexer_token_kind_else() then
_lexer_skip_token();
- _compile_procedure_body();
+ _compile_procedure_body()
elsif token_kind = _lexer_token_kind_elsif() then
_lexer_skip_token();
- source_code_position := source_code_position + 1;
-
- (* Compile condition. *)
- _compile_expression();
- (* Skip " then" with newline. *)
- _lexer_read_token(@token_kind);
- _lexer_skip_token();
+ _compile_condition(after_end_label);
- (* condition_label is the label in front of the next elsif condition or end. *)
- condition_label := label_counter;
- label_counter := label_counter + 1;
-
- _write_z("\tbeqz t0, \0");
- _write_label(condition_label);
- _write_c('\n');
-
- _compile_procedure_body();
-
- _write_z("\tj \0");
- _write_label(after_end_label);
- _write_c('\n');
-
- _write_label(condition_label);
- _write_z(":\n\0");
-
- goto .compile_if_loop;
+ goto .compile_if_loop
end;
_lexer_skip_token();
_write_label(after_end_label);
- _write_z(":\n\0");
+ _write_z(":\n\0")
end;
proc _compile_label_declaration();
@@ -883,7 +867,7 @@ begin
_write_c('.');
_write_s(name, label_token);
_write_z(":\n\0");
- _lexer_skip_token();
+ _lexer_skip_token()
end;
proc _compile_statement();
@@ -894,13 +878,13 @@ begin
_lexer_read_token(@token_kind);
if token_kind = _lexer_token_kind_goto() then
- _compile_goto();
+ _compile_goto()
elsif token_kind = _lexer_token_kind_if() then
- _compile_if();
+ _compile_if()
elsif token_kind = _lexer_token_kind_return() then
- _compile_return_statement();
+ _compile_return_statement()
elsif token_kind = _lexer_token_kind_dot() then
- _compile_label_declaration();
+ _compile_label_declaration()
elsif token_kind = _lexer_token_kind_identifier() then
current_byte := _lexer_global_start();
current_byte := _load_word(current_byte);
@@ -908,12 +892,12 @@ begin
(* This is a call if the statement starts with an underscore. *)
if current_byte = '_' then
- _compile_call();
+ _compile_call()
else
- _compile_assignment();
- end;
+ _compile_assignment()
+ end
end;
- _write_c('\n');
+ _write_c('\n')
end;
proc _compile_procedure_body();
@@ -926,10 +910,9 @@ begin
if token_kind = _lexer_token_kind_semicolon() then
_lexer_skip_token();
- _compile_procedure_body();
- else
- _skip_empty_lines();
+ _compile_procedure_body()
end;
+ _skip_empty_lines()
end;
(**
@@ -943,18 +926,23 @@ proc _write_register(register_character: Word, register_number: Word);
begin
_write_c(register_character);
register_number := register_number + '0';
- _write_c(register_number);
+ _write_c(register_number)
end;
proc _skip_spaces();
var
current_byte: Word;
+ lhs: Word;
+ rhs: Word;
begin
current_byte := _load_byte(source_code_position);
- if current_byte = '\t' then
+ lhs := current_byte = '\t';
+ rhs := current_byte = ' ';
+
+ if lhs or rhs then
source_code_position := source_code_position + 1;
- _skip_spaces();
- end;
+ _skip_spaces()
+ end
end;
proc _read_type_expression();
@@ -963,12 +951,11 @@ var
token_kind: Word;
begin
type_name := _lexer_read_token(@token_kind);
- _lexer_skip_token();
+ _lexer_skip_token()
end;
(**
* Parameters:
- *
* parameter_index - Parameter index.
*)
proc _parameter_info_create(parameter_index: Word);
@@ -1001,7 +988,6 @@ end;
(**
* Parameters:
- *
* temporary_index - Parameter index.
*)
proc _temporary_info_create(temporary_index: Word);
@@ -1033,7 +1019,6 @@ end;
(**
* Parameters:
- *
* parameter_index - Parameter index.
*)
proc _read_procedure_parameter(parameter_index: Word);
@@ -1063,7 +1048,7 @@ begin
info := _parameter_info_get_offset(info);
_write_i(info);
- _write_z("(sp)\n\0");
+ _write_z("(sp)\n\0")
end;
proc _read_procedure_parameters();
@@ -1081,11 +1066,11 @@ begin
if _load_byte(source_code_position) = ',' then
source_code_position := source_code_position + 2;
- goto .compile_procedure_prologue_skip;
- end;
+ goto .compile_procedure_prologue_skip
+ end
end;
(* Skip close paren. *)
- source_code_position := source_code_position + 1;
+ source_code_position := source_code_position + 1
end;
(**
@@ -1113,25 +1098,27 @@ begin
_symbol_table_enter(@symbol_table_local, name_position, name_length, info);
(* Skip semicolon and newline after the variable declaration *)
- source_code_position := source_code_position + 2;
+ source_code_position := source_code_position + 2
end;
proc _read_procedure_temporaries();
var
temporary_counter: Word;
begin
- if _memcmp(source_code_position, "var", 3) = 0 then
- source_code_position := source_code_position + 4;
- temporary_counter := 0;
+ if _memcmp(source_code_position, "var", 3) <> 0 then
+ goto .read_local_variables_end
+ end;
+ source_code_position := source_code_position + 4;
+ temporary_counter := 0;
- .read_local_variables_loop;
- if _memcmp(source_code_position, "begin", 5) <> 0 then
- _read_procedure_temporary(temporary_counter);
+ .read_local_variables_loop;
+ if _memcmp(source_code_position, "begin", 5) <> 0 then
+ _read_procedure_temporary(temporary_counter);
- temporary_counter := temporary_counter + 1;
- goto .read_local_variables_loop;
- end;
+ temporary_counter := temporary_counter + 1;
+ goto .read_local_variables_loop
end;
+ .read_local_variables_end
end;
proc _compile_procedure();
@@ -1166,15 +1153,20 @@ begin
_read_procedure_temporaries();
(* Skip semicolon, "begin" and newline. *)
- source_code_position := source_code_position + 6;
-
- _compile_procedure_body();
+ _lexer_read_token(@token_kind);
+ if token_kind = _lexer_token_kind_begin() then
+ _lexer_skip_token();
+ _compile_procedure_body()
+ end;
+ if token_kind = _lexer_token_kind_return() then
+ _compile_return_statement()
+ end;
(* Write the epilogue. *)
_write_z("\tlw ra, 124(sp)\n\tlw s0, 120(sp)\n\taddi sp, sp, 128\n\tret\n\0");
(* Skip the "end" keyword, semicolon and newline. *)
- source_code_position := source_code_position + 5;
+ source_code_position := source_code_position + 5
end;
(**
@@ -1186,7 +1178,7 @@ var
begin
_lexer_read_token(@token_kind);
_lexer_skip_token();
- source_code_position := source_code_position + 1;
+ source_code_position := source_code_position + 1
end;
(**
@@ -1205,19 +1197,19 @@ begin
if current_byte = '\n' then
source_code_position := current_position + 1;
- _skip_empty_lines();
+ _skip_empty_lines()
elsif current_byte = '\t' then
current_position := current_position + 1;
- goto .skip_empty_lines_loop;
+ goto .skip_empty_lines_loop
elsif current_byte = '(' then
current_byte := _load_byte(current_position + 1);
if current_byte = '*' then
source_code_position := current_position;
_skip_comment();
- _skip_empty_lines();
+ goto .skip_empty_lines_rerun
end;
- end;
+ end
end;
proc _compile_global_initializer();
@@ -1239,15 +1231,15 @@ begin
source_code_position := source_code_position + length;
source_code_position := source_code_position + 2;
- goto .compile_global_initializer_end;
+ goto .compile_global_initializer_end
elsif current_byte = 'S' then
(* Skip "S(". *)
source_code_position := source_code_position + 2;
if _load_byte(source_code_position) = ')' then
- goto .compile_global_initializer_closing;
+ goto .compile_global_initializer_closing
end;
- goto .compile_global_initializer_loop;
+ goto .compile_global_initializer_loop
elsif current_byte = '@' then
(* Skip @. *)
source_code_position := source_code_position + 1;
@@ -1256,15 +1248,16 @@ begin
_write_token(current_byte);
_lexer_skip_token();
- goto .compile_global_initializer_end;
+ goto .compile_global_initializer_end
elsif _is_digit(current_byte) = 1 then
_write_z("\n\t.word \0");
current_byte := _lexer_read_token(@token_kind);
_write_token(current_byte);
source_code_position := source_code_position + 1;
- goto .compile_global_initializer_end;
+ goto .compile_global_initializer_end
end;
+
.compile_global_initializer_loop;
_compile_global_initializer();
@@ -1272,16 +1265,14 @@ begin
(* Skip comma and whitespace after it. *)
source_code_position := source_code_position + 2;
- goto .compile_global_initializer_loop;
+ goto .compile_global_initializer_loop
end;
.compile_global_initializer_closing;
(* Skip ")" *)
source_code_position := source_code_position + 1;
- goto .compile_global_initializer_end;
-
- .compile_global_initializer_end;
+ .compile_global_initializer_end
end;
proc _compile_constant_declaration();
@@ -1304,7 +1295,7 @@ begin
_compile_global_initializer();
(* Skip semicolon and newline. *)
source_code_position := source_code_position + 2;
- _write_c('\n');
+ _write_c('\n')
end;
proc _compile_const_part();
@@ -1315,7 +1306,7 @@ begin
_lexer_read_token(@token_kind);
if token_kind <> _lexer_token_kind_const() then
- goto .compile_const_part_end;
+ goto .compile_const_part_end
end;
(* Skip "const" with the newline after it. *)
_lexer_skip_token();
@@ -1329,10 +1320,10 @@ begin
if _load_byte(source_code_position) = '\t' then
source_code_position := source_code_position + 1;
_compile_constant_declaration();
- goto .compile_const_part_loop;
+ goto .compile_const_part_loop
end;
- .compile_const_part_end;
+ .compile_const_part_end
end;
proc _compile_variable_declaration();
@@ -1357,17 +1348,17 @@ begin
if _load_byte(source_code_position) <> ' ' then
(* Else we assume this is a zeroed 81920 bytes big array. *)
- _write_z(" .zero 81920\0");
+ _write_z(" .zero 81920\0")
else
(* Skip the assignment sign with surrounding whitespaces. *)
source_code_position := source_code_position + 4;
- _compile_global_initializer();
+ _compile_global_initializer()
end;
(* Skip semicolon and newline. *)
_lexer_read_token(@token_kind);
_lexer_skip_token();
- _write_c('\n');
+ _write_c('\n')
end;
proc _compile_var_part();
@@ -1377,7 +1368,7 @@ begin
_lexer_read_token(@token_kind);
if token_kind <> _lexer_token_kind_var() then
- goto .compile_var_part_end;
+ goto .compile_var_part_end
end;
(* Skip "var" and newline. *)
_lexer_skip_token();
@@ -1389,13 +1380,15 @@ begin
if token_kind = _lexer_token_kind_identifier() then
_compile_variable_declaration();
- goto .compile_var_part_loop;
+ goto .compile_var_part_loop
end;
- .compile_var_part_end;
+ .compile_var_part_end
end;
-(* Process the source code and print the generated code. *)
+(**
+ * Process the source code and print the generated code.
+ *)
proc _compile_module();
begin
_compile_const_part();
@@ -1416,10 +1409,10 @@ begin
(* 5 is "proc " length. Space is needed to distinguish from "procedure". *)
if _memcmp(source_code_position, "proc ", 5) = 0 then
_compile_procedure();
- goto .compile_module_loop;
- end;
+ goto .compile_module_loop
+ end
end;
- .compile_module_end;
+ .compile_module_end
end;
proc _compile();
@@ -1443,18 +1436,21 @@ begin
compiler_strings_copy := compiler_strings_copy + 1;
_write_c(current_byte);
- goto .compile_loop;
+ goto .compile_loop
end;
_write_c('"');
- _write_c('\n');
+ _write_c('\n')
end;
(**
- * Terminates the program.
+ * Terminates the program. a0 contains the return code.
+ *
+ * Parameters:
+ * a0 - Status code.
*)
proc _exit();
begin
- _syscall(0, 0, 0, 0, 0, 0, 93);
+ _syscall(0, 0, 0, 0, 0, 0, 93)
end;
(**
@@ -1484,7 +1480,7 @@ begin
.symbol_table_lookup_loop;
if symbol_table_length = 0 then
- goto .symbol_table_lookup_end;
+ goto .symbol_table_lookup_end
end;
(* Symbol name pointer and length. *)
@@ -1493,11 +1489,11 @@ begin
(* If lengths don't match, exit and return nil. *)
if name_length <> current_length then
- goto .symbol_table_lookup_repeat;
+ goto .symbol_table_lookup_repeat
end;
(* If names don't match, exit and return nil. *)
if _memcmp(symbol_name, current_name, name_length) <> 0 then
- goto .symbol_table_lookup_repeat;
+ goto .symbol_table_lookup_repeat
end;
(* Otherwise, the symbol is found. *)
result := _load_word(symbol_table + 8);
@@ -1542,7 +1538,7 @@ begin
(* Increment the symbol table length. *)
table_length := table_length + 1;
- _store_word(table_length, symbol_table);
+ _store_word(table_length, symbol_table)
end;
proc _symbol_table_build();
@@ -1555,7 +1551,7 @@ begin
_symbol_table_enter(@symbol_table_global, symbol_builtin_name_word, 4, @symbol_type_info_word);
_symbol_table_enter(@symbol_table_global, symbol_builtin_name_pointer, 7, @symbol_type_info_pointer);
_symbol_table_enter(@symbol_table_global, symbol_builtin_name_char, 4, @symbol_type_info_char);
- _symbol_table_enter(@symbol_table_global, symbol_builtin_name_bool, 4, @symbol_type_info_bool);
+ _symbol_table_enter(@symbol_table_global, symbol_builtin_name_bool, 4, @symbol_type_info_bool)
end;
@@ -1564,247 +1560,199 @@ end;
* characters of the same group a handled equivalently.
*
* Transition = record
- * action: TransitionAction;
- * next_state: TransitionState
+ * action: TransitionAction;
+ * next_state: TransitionState
* end;
*)
proc _lexer_class_invalid();
-begin
return 1
end;
proc _lexer_class_digit();
-begin
return 2
end;
proc _lexer_class_alpha();
-begin
return 3
end;
proc _lexer_class_space();
-begin
return 4
end;
proc _lexer_class_colon();
-begin
return 5
end;
proc _lexer_class_equals();
-begin
return 6
end;
proc _lexer_class_left_paren();
-begin
return 7
end;
proc _lexer_class_right_paren();
-begin
return 8
end;
proc _lexer_class_asterisk();
-begin
return 9
end;
proc _lexer_class_underscore();
-begin
return 10
end;
proc _lexer_class_single();
-begin
return 11
end;
proc _lexer_class_hex();
-begin
return 12
end;
proc _lexer_class_zero();
-begin
return 13
end;
proc _lexer_class_x();
-begin
return 14
end;
proc _lexer_class_eof();
-begin
return 15
end;
proc _lexer_class_dot();
-begin
return 16
end;
proc _lexer_class_minus();
-begin
return 17
end;
proc _lexer_class_single_quote();
-begin
return 18
end;
proc _lexer_class_double_quote();
-begin
return 19
end;
proc _lexer_class_greater();
-begin
return 20
end;
proc _lexer_class_less();
-begin
return 21
end;
proc _lexer_class_other();
-begin
return 22
end;
proc _lexer_state_start();
-begin
return 1
end;
proc _lexer_state_colon();
-begin
return 2
end;
proc _lexer_state_identifier();
-begin
return 3
end;
proc _lexer_state_decimal();
-begin
return 4
end;
proc _lexer_state_greater();
-begin
return 5
end;
proc _lexer_state_minus();
-begin
return 6
end;
proc _lexer_state_left_paren();
-begin
return 7
end;
proc _lexer_state_less();
-begin
return 8
end;
proc _lexer_state_dot();
-begin
return 9
end;
proc _lexer_state_comment();
-begin
return 10
end;
proc _lexer_state_closing_comment();
-begin
return 11
end;
proc _lexer_state_character();
-begin
return 12
end;
proc _lexer_state_string();
-begin
return 13
end;
proc _lexer_state_leading_zero();
-begin
return 14
end;
proc _lexer_state_decimal_suffix();
-begin
return 15
end;
proc _lexer_state_end();
-begin
return 16
end;
proc _lexer_action_none();
-begin
return 1
end;
proc _lexer_action_accumulate();
-begin
return 2
end;
proc _lexer_action_skip();
-begin
return 3
end;
proc _lexer_action_single();
-begin
return 4
end;
proc _lexer_action_eof();
-begin
return 5
end;
proc _lexer_action_finalize();
-begin
return 6
end;
proc _lexer_action_composite();
-begin
return 7
end;
proc _lexer_action_key_id();
-begin
return 8
end;
proc _lexer_action_integer();
-begin
return 9
end;
proc _lexer_action_delimited();
-begin
return 10
end;
@@ -1824,7 +1772,7 @@ begin
target := target * 4;
target := array + target;
- _store_word(data, target);
+ _store_word(data, target)
end;
proc _get_at(array: Word, index: Word);
@@ -1838,7 +1786,9 @@ begin
return _load_word(target)
end;
-(* Initializes the array with character classes. *)
+(**
+ * Initializes the array with character classes.
+ *)
proc _lexer_classifications();
var
code: Word;
@@ -1980,8 +1930,8 @@ begin
code := code + 1;
if code < 257 then
- goto .create_classification_loop;
- end;
+ goto .create_classification_loop
+ end
end;
proc _lexer_get_transition(current_state: Word, character_class: Word);
@@ -2018,12 +1968,12 @@ begin
transition := _lexer_get_transition(current_state, character_class);
_lexer_transition_set_action(transition, action);
- _lexer_transition_set_state(transition, next_state);
+ _lexer_transition_set_state(transition, next_state)
end;
+(* Sets same action and state transition for all character classes in one transition row. *)
+
(**
- * Sets same action and state transition for all character classes in one transition row.
- *
* Parameters:
* current_state - Current state (Transition state enumeration).
* default_action - Default action (Callback).
@@ -2052,10 +2002,9 @@ begin
_lexer_set_transition(current_state, _lexer_class_double_quote(), default_action, next_state);
_lexer_set_transition(current_state, _lexer_class_greater(), default_action, next_state);
_lexer_set_transition(current_state, _lexer_class_less(), default_action, next_state);
- _lexer_set_transition(current_state, _lexer_class_other(), default_action, next_state);
+ _lexer_set_transition(current_state, _lexer_class_other(), default_action, next_state)
end;
-
(**
* The transition table describes transitions from one state to another, given
* a symbol (character class).
@@ -2172,7 +2121,7 @@ begin
_lexer_set_transition(_lexer_state_decimal_suffix(), _lexer_class_alpha(), _lexer_action_none(), _lexer_state_end());
_lexer_set_transition(_lexer_state_decimal_suffix(), _lexer_class_hex(), _lexer_action_none(), _lexer_state_end());
_lexer_set_transition(_lexer_state_decimal_suffix(), _lexer_class_zero(), _lexer_action_none(), _lexer_state_end());
- _lexer_set_transition(_lexer_state_decimal_suffix(), _lexer_class_x(), _lexer_action_none(), _lexer_state_end());
+ _lexer_set_transition(_lexer_state_decimal_suffix(), _lexer_class_x(), _lexer_action_none(), _lexer_state_end())
end;
(**
@@ -2181,7 +2130,6 @@ end;
* 1024 = 256 * 4
*)
proc _lexer_get_transition_table();
-begin
return @classification + 1024
end;
@@ -2191,7 +2139,6 @@ end;
* and 22 columns (character classes), so 2816 = 8 * 16 * 22.
*)
proc _lexer_global_state();
-begin
return _lexer_get_transition_table() + 2816
end;
@@ -2199,7 +2146,6 @@ end;
* Gets pointer to the token start.
*)
proc _lexer_global_start();
-begin
return _lexer_global_state() + 4
end;
@@ -2207,28 +2153,25 @@ end;
* Gets pointer to the token end.
*)
proc _lexer_global_end();
-begin
return _lexer_global_start() + 4
end;
proc _lexer_transition_get_action(transition: Word);
-begin
return _load_word(transition)
end;
proc _lexer_transition_set_action(transition: Word, action: Word);
begin
- _store_word(action, transition);
+ _store_word(action, transition)
end;
proc _lexer_transition_get_state(transition: Word);
-begin
return _load_word(transition + 4)
end;
proc _lexer_transition_set_state(transition: Word, state: Word);
begin
- _store_word(state, transition + 4);
+ _store_word(state, transition + 4)
end;
(**
@@ -2247,7 +2190,7 @@ begin
_store_word(source_code_position, current);
current := _lexer_global_end();
- _store_word(source_code_position, current);
+ _store_word(source_code_position, current)
end;
(**
@@ -2256,7 +2199,7 @@ end;
proc _lexer_initialize();
begin
_lexer_classifications();
- _lexer_transitions();
+ _lexer_transitions()
end;
proc _lexer_next_transition();
@@ -2278,322 +2221,258 @@ begin
end;
proc _lexer_token_kind_identifier();
-begin
return 1
end;
proc _lexer_token_kind_const();
-begin
return 2
end;
proc _lexer_token_kind_var();
-begin
return 3
end;
proc _lexer_token_kind_proc();
-begin
return 4
end;
proc _lexer_token_kind_type();
-begin
return 5
end;
proc _lexer_token_kind_begin();
-begin
return 6
end;
proc _lexer_token_kind_end();
-begin
return 7
end;
proc _lexer_token_kind_if();
-begin
return 8
end;
proc _lexer_token_kind_then();
-begin
return 9
end;
proc _lexer_token_kind_else();
-begin
return 10
end;
proc _lexer_token_kind_elsif();
-begin
return 11
end;
proc _lexer_token_kind_while();
-begin
return 12
end;
proc _lexer_token_kind_do();
-begin
return 13
end;
proc _lexer_token_kind_extern();
-begin
return 14
end;
proc _lexer_token_kind_record();
-begin
return 15
end;
proc _lexer_token_kind_union();
-begin
return 16
end;
proc _lexer_token_kind_true();
-begin
return 17
end;
proc _lexer_token_kind_false();
-begin
return 18
end;
proc _lexer_token_kind_nil();
-begin
return 19
end;
proc _lexer_token_kind_and();
-begin
return 20
end;
proc _lexer_token_kind_or();
-begin
return 21
end;
proc _lexer_token_kind_xor();
-begin
return 22
end;
proc _lexer_token_kind_pipe();
-begin
return 23
end;
proc _lexer_token_kind_not();
-begin
return 24
end;
proc _lexer_token_kind_return();
-begin
return 24
end;
proc _lexer_token_kind_module();
-begin
return 25
end;
proc _lexer_token_kind_program();
-begin
return 26
end;
proc _lexer_token_kind_import();
-begin
return 27
end;
proc _lexer_token_kind_cast();
-begin
return 28
end;
proc _lexer_token_kind_defer();
-begin
return 29
end;
proc _lexer_token_kind_case();
-begin
return 30
end;
proc _lexer_token_kind_of();
-begin
return 31
end;
proc _lexer_token_kind_trait();
-begin
return 32
end;
proc _lexer_token_kind_left_paren();
-begin
return 33
end;
proc _lexer_token_kind_right_paren();
-begin
return 34
end;
proc _lexer_token_kind_left_square();
-begin
return 35
end;
proc _lexer_token_kind_right_square();
-begin
return 36
end;
proc _lexer_token_kind_shift_left();
-begin
return 37
end;
proc _lexer_token_kind_shift_right();
-begin
return 38
end;
proc _lexer_token_kind_greater_equal();
-begin
return 39
end;
proc _lexer_token_kind_less_equal();
-begin
return 40
end;
proc _lexer_token_kind_greater_than();
-begin
return 41
end;
proc _lexer_token_kind_less_than();
-begin
return 42
end;
proc _lexer_token_kind_not_equal();
-begin
return 43
end;
proc _lexer_token_kind_equals();
-begin
return 44
end;
proc _lexer_token_kind_semicolon();
-begin
return 45
end;
proc _lexer_token_kind_dot();
-begin
return 46
end;
proc _lexer_token_kind_comma();
-begin
return 47
end;
proc _lexer_token_kind_plus();
-begin
return 48
end;
proc _lexer_token_kind_arrow();
-begin
return 49
end;
proc _lexer_token_kind_minus();
-begin
return 50
end;
proc _lexer_token_kind_multiplication();
-begin
return 51
end;
proc _lexer_token_kind_division();
-begin
return 52
end;
proc _lexer_token_kind_remainder();
-begin
return 53
end;
proc _lexer_token_kind_assignment();
-begin
return 54
end;
proc _lexer_token_kind_colon();
-begin
return 55
end;
proc _lexer_token_kind_hat();
-begin
return 56
end;
proc _lexer_token_kind_at();
-begin
return 57
end;
proc _lexer_token_kind_exclamation();
-begin
return 58
end;
proc _lexer_token_kind_string();
-begin
return 59
end;
proc _lexer_token_kind_character();
-begin
return 60
end;
proc _lexer_token_kind_integer();
-begin
return 61
end;
proc _lexer_token_kind_word();
-begin
return 62
end;
proc _lexer_token_kind_goto();
-begin
return 63
end;
@@ -2604,7 +2483,7 @@ begin
result := 0;
if lhs_length = rhs_length then
- result := _memcmp(lhs_pointer, rhs_pointer, lhs_length) = 0;
+ result := _memcmp(lhs_pointer, rhs_pointer, lhs_length) = 0
end;
return result
end;
@@ -2618,31 +2497,31 @@ begin
token_length := position_end + -position_start;
if _lexer_compare_keyword(position_start, token_length, "const", 5) = 1 then
- result := _lexer_token_kind_const();
+ result := _lexer_token_kind_const()
elsif _lexer_compare_keyword(position_start, token_length, "var", 3) = 1 then
- result := _lexer_token_kind_var();
+ result := _lexer_token_kind_var()
elsif _lexer_compare_keyword(position_start, token_length, "proc", 4) = 1 then
- result := _lexer_token_kind_proc();
+ result := _lexer_token_kind_proc()
elsif _lexer_compare_keyword(position_start, token_length, "type", 4) = 1 then
- result := _lexer_token_kind_type();
+ result := _lexer_token_kind_type()
elsif _lexer_compare_keyword(position_start, token_length, "begin", 5) = 1 then
- result := _lexer_token_kind_begin();
+ result := _lexer_token_kind_begin()
elsif _lexer_compare_keyword(position_start, token_length, "end", 3) = 1 then
- result := _lexer_token_kind_end();
+ result := _lexer_token_kind_end()
elsif _lexer_compare_keyword(position_start, token_length, "return", 6) = 1 then
- result := _lexer_token_kind_return();
+ result := _lexer_token_kind_return()
elsif _lexer_compare_keyword(position_start, token_length, "goto", 4) = 1 then
- result := _lexer_token_kind_goto();
+ result := _lexer_token_kind_goto()
elsif _lexer_compare_keyword(position_start, token_length, "if", 2) = 1 then
- result := _lexer_token_kind_if();
+ result := _lexer_token_kind_if()
elsif _lexer_compare_keyword(position_start, token_length, "while", 5) = 1 then
- result := _lexer_token_kind_while();
+ result := _lexer_token_kind_while()
elsif _lexer_compare_keyword(position_start, token_length, "then", 4) = 1 then
- result := _lexer_token_kind_then();
+ result := _lexer_token_kind_then()
elsif _lexer_compare_keyword(position_start, token_length, "else", 4) = 1 then
- result := _lexer_token_kind_else();
+ result := _lexer_token_kind_else()
elsif _lexer_compare_keyword(position_start, token_length, "elsif", 5) = 1 then
- result := _lexer_token_kind_elsif();
+ result := _lexer_token_kind_elsif()
end;
return result
end;
@@ -2656,9 +2535,9 @@ begin
character := _load_byte(start_position);
if character = ':' then
- result := _lexer_token_kind_colon();
+ result := _lexer_token_kind_colon()
elsif character = '.' then
- result := _lexer_token_kind_dot();
+ result := _lexer_token_kind_dot()
end;
return result
end;
@@ -2672,7 +2551,7 @@ begin
character := _load_byte(start_position);
if character = ';' then
- result := _lexer_token_kind_semicolon();
+ result := _lexer_token_kind_semicolon()
end;
return result
end;
@@ -2692,23 +2571,23 @@ begin
if action_to_perform = _lexer_action_none() then
elsif action_to_perform = _lexer_action_accumulate() then
- _store_word(position_end + 1, pointer_end);
+ _store_word(position_end + 1, pointer_end)
elsif action_to_perform = _lexer_action_skip() then
_store_word(position_start + 1, pointer_start);
- _store_word(position_end + 1, pointer_end);
+ _store_word(position_end + 1, pointer_end)
elsif action_to_perform = _lexer_action_single() then
_store_word(position_end + 1, pointer_end);
intermediate := _lexer_classify_single(position_start);
- _store_word(intermediate, kind);
+ _store_word(intermediate, kind)
elsif action_to_perform = _lexer_action_eof() then
elsif action_to_perform = _lexer_action_finalize() then
intermediate := _lexer_classify_finalize(position_start);
- _store_word(intermediate, kind);
+ _store_word(intermediate, kind)
elsif action_to_perform = _lexer_action_composite() then
elsif action_to_perform = _lexer_action_key_id() then
intermediate := _lexer_classify_keyword(position_start, position_end);
- _store_word(intermediate, kind);
+ _store_word(intermediate, kind)
elsif action_to_perform = _lexer_action_integer() then
elsif action_to_perform = _lexer_action_delimited() then
end;
@@ -2736,8 +2615,8 @@ end;
proc _lexer_advance_token(kind: Word);
begin
if _lexer_execute_transition(kind) <> _lexer_state_end() then
- _lexer_advance_token(kind);
- end;
+ _lexer_advance_token(kind)
+ end
end;
(**
@@ -2764,10 +2643,10 @@ var
new_position: Word;
begin
new_position := _lexer_global_end();
- source_code_position := _load_word(new_position);
+ source_code_position := _load_word(new_position)
end;
-(**
+(*
* Entry point.
*)
proc _start();
@@ -2786,9 +2665,9 @@ begin
last_read := _read_file(offset, 81920);
if last_read > 0 then
offset := offset + last_read;
- goto .start_read;
+ goto .start_read
end;
_compile();
- _exit(0);
+ _exit(0)
end;