summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndreas Baumann <mail@andreasbaumann.cc>2020-06-07 20:07:44 +0200
committerAndreas Baumann <mail@andreasbaumann.cc>2020-06-07 20:07:44 +0200
commit4be5f1a89e1368c9a4c98eeb2c05f5b3f06714a5 (patch)
tree1afec2c9440a47eef2c499699a1498b3afd97c74
parent9f8d1f2820ebd707004b3e404f125d4a6abf2281 (diff)
downloadcompilertests-4be5f1a89e1368c9a4c98eeb2c05f5b3f06714a5.tar.gz
compilertests-4be5f1a89e1368c9a4c98eeb2c05f5b3f06714a5.tar.bz2
work on array dereference
-rw-r--r--ecomp-c/ec.c33
-rw-r--r--ecomp-c/minie.ebnf4
-rw-r--r--ecomp-c/test1.e4
-rw-r--r--ecomp-c/tests/type_check_assignment.easm1
-rw-r--r--ecomp-c/tests/variable_assign_from_type.easm1
-rw-r--r--ecomp-c/tests/variable_not_initialized.easm1
6 files changed, 40 insertions, 4 deletions
diff --git a/ecomp-c/ec.c b/ecomp-c/ec.c
index 001fb28..42d6203 100644
--- a/ecomp-c/ec.c
+++ b/ecomp-c/ec.c
@@ -51,6 +51,8 @@ typedef enum {
S_star,
S_lparen,
S_rparen,
+ S_lbracket,
+ S_rbracket,
S_comma,
S_less,
S_less_or_equals,
@@ -90,6 +92,8 @@ static char *symname[S_eof+1] = {
"*",
"(",
")",
+ "[",
+ "]",
",",
"<",
"<=",
@@ -541,6 +545,14 @@ static S_Symbol getSym( void )
look = getChar( );
s = S_rparen;
break;
+ case '[':
+ look = getChar( );
+ s = S_lbracket;
+ break;
+ case ']':
+ look = getChar( );
+ s = S_rbracket;
+ break;
case ',':
look = getChar( );
s = S_comma;
@@ -1233,9 +1245,27 @@ static void parseAssignment( Scope *scope )
if( symbol->class == SYMBOL_CLASS_CONSTANT ) {
Abort( "'%s' is a constant and can not be changed", ident );
}
+
symbol->initialized = 1;
- Expect( S_assign );
+ if( sym == S_assign ) {
+ Expect( S_assign );
+ Emit( "; LET %s <- ", symbol->name );
+ } else if( sym == S_lbracket ) {
+ Expect( S_lbracket );
+ if( symbol->type->class != SYMBOL_CLASS_ARRAY_TYPE ) {
+ Abort( "array index not allowed on '%s' (not an array)", symbol->name );
+ }
+ Emit( "; LET %s[", symbol->name );
+ node = parseExpression( );
+ generate_expression_comment( node );
+ Emit( "] <- " );
+ Emit( "\n" );
+ emit_expression_code( node, scope );
+ Emit( "pop ebx\n" );
+ Expect( S_rbracket );
+ Expect( S_assign );
+ }
node = parseExpression( );
@@ -1244,7 +1274,6 @@ static void parseAssignment( Scope *scope )
node->actual_type->name, symbol->name, symbol->type->name );
}
- Emit( "; LET %s <- ", symbol->name );
generate_expression_comment( node );
Emit( "\n" );
emit_expression_code( node, scope );
diff --git a/ecomp-c/minie.ebnf b/ecomp-c/minie.ebnf
index c4d9513..5576e9d 100644
--- a/ecomp-c/minie.ebnf
+++ b/ecomp-c/minie.ebnf
@@ -7,12 +7,12 @@ Number = Digit { Digit } .
Character = "'" Digit | Letter | Special | "'" .
String = """" { Character } """" .
-Factor = Number | Character | String | Identifier | "(" Expression ")" | "not" Factor .
+Factor = Number | Character | String | Identifier [ "[" Expression "]" ] | "(" Expression ")" | "not" Factor .
Term = Factor { ( "*" | "/" | "mod" | "and" ) Factor } .
SimpleExpression = Term { ( "+" | "-" | "or" ) Term } .
RelationalOperator = "=" | "<>" | "<" | ">" | "<=" | ">=" .
Expression = SimpleExpression [ RelationalOperator SimpleExpression ] .
-Assignment = Identifier ":=" Expression .
+Assignment = Identifier [ "[" Expression "]" ] ":=" Expression .
Statement = Assignment | IfStatement | WhileStatement .
IfStatement = "if" Expression "do" StatementSequence "else" StatementSequence "end" .
WhileStatement = "while" Expression "do" StatementSequence "end" .
diff --git a/ecomp-c/test1.e b/ecomp-c/test1.e
index 49d7adb..83090ec 100644
--- a/ecomp-c/test1.e
+++ b/ecomp-c/test1.e
@@ -29,6 +29,7 @@ var
j : character := C;
s : array 10 of character := S;
s2 : array 10 of character := "hello";
+ a1 : array 10 of integer;
begin
a := 1;
@@ -59,4 +60,7 @@ begin
end;
j := 'B';
+ a1[0] := 'c';
+ s[0] := 'H';
+ j := s2[1];
end
diff --git a/ecomp-c/tests/type_check_assignment.easm b/ecomp-c/tests/type_check_assignment.easm
index 03888c0..55cc387 100644
--- a/ecomp-c/tests/type_check_assignment.easm
+++ b/ecomp-c/tests/type_check_assignment.easm
@@ -8,3 +8,4 @@ mov eax, 1
push eax
pop eax
mov [i], eax
+; LET b <- \ No newline at end of file
diff --git a/ecomp-c/tests/variable_assign_from_type.easm b/ecomp-c/tests/variable_assign_from_type.easm
index c89a9c2..6a1629f 100644
--- a/ecomp-c/tests/variable_assign_from_type.easm
+++ b/ecomp-c/tests/variable_assign_from_type.easm
@@ -3,3 +3,4 @@ use32
org $1000000
; CONST N -> integer, 20
; DECL a -> integer, 0
+; LET a <- \ No newline at end of file
diff --git a/ecomp-c/tests/variable_not_initialized.easm b/ecomp-c/tests/variable_not_initialized.easm
index 92f06c6..23665ea 100644
--- a/ecomp-c/tests/variable_not_initialized.easm
+++ b/ecomp-c/tests/variable_not_initialized.easm
@@ -3,3 +3,4 @@ use32
org $1000000
; DECL a -> integer, 0
; DECL b -> integer, 0
+; LET a <- \ No newline at end of file