summaryrefslogtreecommitdiff
path: root/minie
diff options
context:
space:
mode:
authorAndreas Baumann <mail@andreasbaumann.cc>2018-12-27 20:53:57 +0100
committerAndreas Baumann <mail@andreasbaumann.cc>2018-12-27 20:53:57 +0100
commit97df3ff8620c5939ddf93ddb0eecf18af47db9d1 (patch)
tree3eda156a5e2fef2355c3c57f229c48356063dcb6 /minie
parentd30cb3af63fcc30b93bc85441dac772021906cc2 (diff)
downloadcompilertests-97df3ff8620c5939ddf93ddb0eecf18af47db9d1.tar.gz
compilertests-97df3ff8620c5939ddf93ddb0eecf18af47db9d1.tar.bz2
added compare operators
Diffstat (limited to 'minie')
-rw-r--r--minie/e2c.c98
-rw-r--r--minie/test4.e3
2 files changed, 79 insertions, 22 deletions
diff --git a/minie/e2c.c b/minie/e2c.c
index 8bd7105..e8368de 100644
--- a/minie/e2c.c
+++ b/minie/e2c.c
@@ -56,6 +56,12 @@ typedef enum {
S_rparen,
S_lbracket,
S_rbracket,
+ S_less,
+ S_less_or_equals,
+ S_equals,
+ S_more_or_equals,
+ S_more,
+ S_not_equals,
S_eof
} Symbol;
@@ -90,6 +96,12 @@ char *symname[S_eof+1] = {
")",
"[",
"]",
+ "<",
+ "<=",
+ "=",
+ ">=",
+ ">",
+ "<>",
"eof"
};
@@ -432,6 +444,26 @@ next:
case ']':
look = getChar( );
return S_rbracket;
+ case '<':
+ look = getChar( );
+ if( look == '=' ) {
+ look = getChar( );
+ return S_less_or_equals;
+ } else if( look == '>' ) {
+ look = getChar( );
+ return S_not_equals;
+ }
+ return S_less;
+ case '=':
+ look = getChar( );
+ return S_equals;
+ case '>':
+ look = getChar( );
+ if( look == '=' ) {
+ look = getChar( );
+ return S_more_or_equals;
+ }
+ return S_more;
case EOF:
return S_eof;
default:
@@ -669,6 +701,7 @@ static void variableName( void )
}
static void expression( void );
+static void simpleExpression( void );
static void parameterList( void );
static void factor( void )
@@ -723,7 +756,7 @@ static void term( void )
}
}
-static void expression( void )
+static void simpleExpression( void )
{
term( );
while( sym == S_plus || sym == S_minus ) {
@@ -734,6 +767,44 @@ static void expression( void )
}
}
+static int isRelationalOperator( Symbol sym )
+{
+ if( sym == S_less || sym == S_less_or_equals ||
+ sym == S_equals || sym == S_more ||
+ sym == S_more_or_equals || sym == S_not_equals ) {
+ return 1;
+ } else {
+ return 0;
+ }
+}
+
+static void relationalOperator( void )
+{
+ if( sym == S_less ) {
+ emit( " < " );
+ } else if( sym == S_less_or_equals ) {
+ emit( " <= " );
+ } else if( sym == S_equals ) {
+ emit( " == " );
+ } else if( sym == S_more_or_equals ) {
+ emit( " >= " );
+ } else if( sym == S_more ) {
+ emit( " > " );
+ } else if( sym == S_not_equals ) {
+ emit( " != " );
+ }
+}
+
+static void expression( void )
+{
+ simpleExpression( );
+ if( isRelationalOperator( sym ) ) {
+ relationalOperator( );
+ sym = getSym( );
+ simpleExpression( );
+ }
+}
+
static void assignment( void )
{
Type type;
@@ -753,27 +824,12 @@ static void assignment( void )
emitLn( ";" );
}
-static void condition( )
-{
- identifier( );
- /* TODO: temporary, assume we have a boolean variable */
- if( strcmp( ident, "false" ) == 0 ) {
- sym = getSym( );
- emit( "0" );
- } else if( strcmp( ident, "true" ) == 0 ) {
- sym = getSym( );
- emit( "1" );
- } else {
- Abort( "Too complex condition, expressions not implemented yet" );
- }
-}
-
static void statementSequence( void );
static void doIf( void )
{
emit( "if( " );
- condition( );
+ expression( );
emitLn( ") {" );
Expect( S_do );
statementSequence( );
@@ -799,10 +855,10 @@ void static doFor( void )
emit( " %s = ", loopVar );
sym = getSym( );
Expect( S_assign );
- expression( );
+ simpleExpression( );
emit( "; %s <= ", loopVar );
Expect( S_to );
- expression( );
+ simpleExpression( );
/* TODO: add "by" constExpression if needed */
emitLn( "; %s++ ) {", loopVar );
Expect( S_do );
@@ -863,14 +919,14 @@ static void parameterList( void )
}
/* TODO: no VAR parameters, strictly pass-by-value */
- expression( );
+ simpleExpression( );
n = 1;
while( sym == S_comma ) {
n++;
emit( ", " );
sym = getSym( );
/* TODO: no VAR parameters, strictly pass-by-value */
- expression( );
+ simpleExpression( );
}
/* epilogue of 1-parameter function or procedure */
diff --git a/minie/test4.e b/minie/test4.e
index 24281c0..ef8ff87 100644
--- a/minie/test4.e
+++ b/minie/test4.e
@@ -16,7 +16,8 @@ begin
z := x;
c := 45;
x := c;
- if false do
+ b := c < y;
+ if b do
if true do
res := 1;
else