From 156506e8fa483e273097c3a7d060de37849b94bc Mon Sep 17 00:00:00 2001 From: Eugen Wissner Date: Tue, 21 Jan 2025 20:18:27 +0100 Subject: [PATCH] Force the execution of expression statements --- example.elna | 109 +++++++++++++++++++++----------------------- gcc/elna-generic.cc | 1 + source/parser.yy | 2 +- 3 files changed, 54 insertions(+), 58 deletions(-) diff --git a/example.elna b/example.elna index 3b728bc..080c6cd 100644 --- a/example.elna +++ b/example.elna @@ -1,26 +1,17 @@ type T = array 5 of Int, - R = record - x: Int; - y: Int + TokenValue = union + intValue: Int; + stringValue: String + end, + Token = record + kind: Int; + value: TokenValue end, FILE = record - x: Int - end, - U = union - a: Int; - b: Int + dummy: Int end; -proc test_string(); -var s: String; -begin - s := "Test string."; - - writei(""); - writei(s) -end; - proc test_array(); var a: T, x: Int; begin @@ -38,26 +29,15 @@ begin end; proc test_record(); -var r: R; +var r: Token; begin writei(""); writei("Test record:"); - r.x := 4; - r.y := 8; + r.kind := 4; + r.value.intValue := 8; - writei(r.y) -end; - -proc test_union(); -var u: U; -begin - writei(""); - writei("Test union:"); - - u.a := 9; - - writei(u.b) + writei(r.value.intValue) end; proc test_primitive(); @@ -72,17 +52,6 @@ begin writei(z) end; -proc test_const(); -const t = 5; -var x: Int; -begin - x := t; - - writei(""); - writei("Test const:"); - writei(x) -end; - proc test_if(); var x: Bool, y: Bool; begin @@ -153,40 +122,66 @@ proc fopen(pathname: String, mode: String): pointer to FILE; extern; proc fclose(stream: pointer to FILE): Int; extern; proc fseek(stream: pointer to FILE, off: Int, whence: Int): Int; extern; proc ftell(stream: pointer to FILE): Int; extern; +proc fread(ptr: pointer to Char, size: Int, nmemb: Int, stream: pointer to FILE): Int; extern; +proc write(fd: Int, buf: pointer to Char, count: Int): Int; extern; +proc malloc(size: Int): pointer to Char; extern; +proc free(ptr: pointer to Char); extern; +proc memset(ptr: pointer to Char, c: Int, n: Int): pointer to Char; extern; -proc compile(); +proc read_source(filename: String): pointer to Char; const + -- Bug: The procedure doesn't see global constants. SEEK_SET = 0, SEEK_CUR = 1, SEEK_END = 2; var input_file: pointer to FILE, - source_size: Int, ret: Int; + source_size: Int, + input: pointer to Char; begin - input_file := fopen("example.elna", "rb"); + input_file := fopen(filename, "rb"); - // Bug: Functions as statements aren't called. - ret := fseek(input_file, 0, SEEK_END); + fseek(input_file, 0, SEEK_END); source_size := ftell(input_file); - ret := fseek(input_file, 0, SEEK_SET); + fseek(input_file, 0, SEEK_SET); - writei(""); - writei("File size: "); - writei(input_file); - writei(source_size); + input := malloc(source_size + 1); + memset(input, 0, source_size + 1); + fread(input, source_size, 1, input_file); - ret := fclose(input_file) + fclose(input_file); + + return input +end; + +-- Bug: "while not is_eof(…) do" is a syntax error. +proc is_not_eof(buffer: pointer to Int): Bool; +begin + return buffer^ /= 0 +end; + +proc compile(); +var + input: pointer to Char, + input_pointer: pointer to Char; +begin + input := read_source("example.elna"); + + input_pointer := input; + while is_not_eof(input_pointer) do + write(0, input_pointer, 1); + input_pointer := input_pointer + 1 + end; + + free(input) end; begin test_primitive(); - test_string(); test_array(); test_record(); - test_const(); test_if(); test_not(); test_param(8, 7); test_const_char(); - test_union(); writei(test_return_int()); test_add_pointer(); diff --git a/gcc/elna-generic.cc b/gcc/elna-generic.cc index 0c38694..e4339ea 100644 --- a/gcc/elna-generic.cc +++ b/gcc/elna-generic.cc @@ -852,6 +852,7 @@ namespace gcc void generic_visitor::visit(source::expression_statement *statement) { statement->body().accept(this); + append_to_statement_list(this->current_expression, &this->current_statements); } void generic_visitor::visit(source::return_statement *statement) diff --git a/source/parser.yy b/source/parser.yy index af6b388..7ebf4a5 100644 --- a/source/parser.yy +++ b/source/parser.yy @@ -311,7 +311,7 @@ designator_expression: } | designator_expression DOT IDENTIFIER { - $$ = new elna::source::field_access_expression(elna::source::make_position(@1), $1, $3); + $$ = new elna::source::field_access_expression(elna::source::make_position(@2), $1, $3); } | designator_expression HAT {