summaryrefslogtreecommitdiff
path: root/minie
diff options
context:
space:
mode:
authorAndreas Baumann <mail@andreasbaumann.cc>2018-09-06 21:12:33 +0200
committerAndreas Baumann <mail@andreasbaumann.cc>2018-09-06 21:12:33 +0200
commit6b354f74920750e3bb36748b439d09a85d8f3364 (patch)
tree88a5bbf818e87be562b2237459481344fad202d6 /minie
parent050fc2ae7fc8b182825fe209081bae304f1cf9c5 (diff)
downloadcompilertests-6b354f74920750e3bb36748b439d09a85d8f3364.tar.gz
compilertests-6b354f74920750e3bb36748b439d09a85d8f3364.tar.bz2
added expressions
Diffstat (limited to 'minie')
-rw-r--r--minie/e2c.c78
-rw-r--r--minie/test4.e2
2 files changed, 67 insertions, 13 deletions
diff --git a/minie/e2c.c b/minie/e2c.c
index c155164..8c33b0d 100644
--- a/minie/e2c.c
+++ b/minie/e2c.c
@@ -29,6 +29,12 @@ typedef enum {
S_semicolon,
S_colon,
S_assign,
+ S_plus,
+ S_minus,
+ S_star,
+ S_slash,
+ S_lparen,
+ S_rparen,
S_eof
} Symbol;
@@ -43,6 +49,12 @@ char *symname[S_eof+1] = {
";",
":",
":=",
+ "+",
+ "-",
+ "*",
+ "/",
+ "(",
+ ")",
"eof"
};
@@ -249,10 +261,28 @@ static Symbol getSym( )
case ';':
look = getChar( );
return S_semicolon;
+ case '+':
+ look = getChar( );
+ return S_plus;
+ case '-':
+ look = getChar( );
+ return S_minus;
+ case '*':
+ look = getChar( );
+ return S_star;
+ case '/':
+ look = getChar( );
+ return S_slash;
+ case '(':
+ look = getChar( );
+ return S_lparen;
+ case ')':
+ look = getChar( );
+ return S_rparen;
case EOF:
return S_eof;
default:
- Abort( "Illegal character" );
+ Abort( "Illegal character '%c'", (char)look );
}
return S_char;
}
@@ -262,13 +292,7 @@ static void Expect( Symbol expect )
if( sym == expect ) {
sym = getSym( );
} else {
- char s[MAX_ERRMSG_LEN+1];
- s[0] = '\0';
- strncat( s, "Expected symbol '", MAX_ERRMSG_LEN );
- strncat( s, symname[expect], MAX_ERRMSG_LEN );
- strncat( s, "'", MAX_ERRMSG_LEN );
- s[MAX_ERRMSG_LEN-1] = '\0';
- Abort( s );
+ Abort( "Expected symbol '%s'", symname[expect] );
}
}
@@ -320,19 +344,49 @@ static void variableName( void )
}
}
-static void expression( void )
+static void expression( void );
+
+static void factor( void )
{
if( sym == S_number ) {
- emit( "%d;", num );
+ emit( "%d", num );
sym = getSym( );
} else if( sym == S_ident ) {
- emit( "%s;", ident );
+ emit( "%s", ident );
sym = getSym( );
+ } else if( sym == S_lparen ) {
+ emit( "(" );
+ sym = getSym( );
+ expression( );
+ Expect( S_rparen );
+ emit( ")" );
} else {
Abort( "Expected expression" );
}
}
+static void term( void )
+{
+ factor( );
+ while( sym == S_star || sym == S_slash ) {
+ if( sym == S_star ) emit( "*" );
+ if( sym == S_slash ) emit( "/" );
+ sym = getSym( );
+ factor( );
+ }
+}
+
+static void expression( void )
+{
+ term( );
+ while( sym == S_plus || sym == S_minus ) {
+ if( sym == S_plus ) emit( "+" );
+ if( sym == S_minus ) emit( "-" );
+ sym = getSym( );
+ term( );
+ }
+}
+
static void statement( void )
{
char v[MAX_IDENT_LEN+1];
@@ -345,7 +399,7 @@ static void statement( void )
Expect( S_assign );
emit( "%s = ", v );
expression( );
- emitLn( "" );
+ emitLn( ";" );
}
static void statementBlock( void )
diff --git a/minie/test4.e b/minie/test4.e
index 18ba33b..23c6278 100644
--- a/minie/test4.e
+++ b/minie/test4.e
@@ -6,7 +6,7 @@ var
z : integer;
begin
- x := 7;
+ x := 7+4*(3-1/2);
y := 22;
z := x;
end