summaryrefslogtreecommitdiff
path: root/minie
diff options
context:
space:
mode:
authorAndreas Baumann <mail@andreasbaumann.cc>2018-09-10 07:02:34 +0200
committerAndreas Baumann <mail@andreasbaumann.cc>2018-09-10 07:02:34 +0200
commitff303401e2abbe4b3edb9161efdc231c14097489 (patch)
tree2e605fe08bd2ffe6b2ac92fb757b61ece6396133 /minie
parent87da5f12b7c055471bce6eb5e9b19607f09500ec (diff)
downloadcompilertests-ff303401e2abbe4b3edb9161efdc231c14097489.tar.gz
compilertests-ff303401e2abbe4b3edb9161efdc231c14097489.tar.bz2
some work on bootstrapping the compiler
Diffstat (limited to 'minie')
-rw-r--r--minie/DESIGN3
-rw-r--r--minie/e2c.c58
-rw-r--r--minie/ec.e20
-rw-r--r--minie/test4.e11
4 files changed, 82 insertions, 10 deletions
diff --git a/minie/DESIGN b/minie/DESIGN
index 2bba202..5a6d15d 100644
--- a/minie/DESIGN
+++ b/minie/DESIGN
@@ -52,3 +52,6 @@ the generated code. As this is a throw-away piece of code, it doesn't
matter so much how many features of O we use to implement it.
Step 2: Write compiler in N, with a backend for O''
+
+Do all the steps in language N as nicely as possible. Introduce new
+elements to N (and thus to the N to O translator as needed).
diff --git a/minie/e2c.c b/minie/e2c.c
index fae65de..bcd3210 100644
--- a/minie/e2c.c
+++ b/minie/e2c.c
@@ -25,10 +25,12 @@ typedef enum {
S_module,
S_begin,
S_end,
+ S_import,
S_var,
S_if,
S_do,
S_else,
+ S_comma,
S_semicolon,
S_colon,
S_assign,
@@ -48,10 +50,12 @@ char *symname[S_eof+1] = {
"module",
"begin",
"end",
+ "import",
"var",
"if",
"do",
"else",
+ ",",
";",
":",
":=",
@@ -211,6 +215,8 @@ static Symbol getSym( )
identifier( );
if( strcmp( ident, "if" ) == 0 ) {
return S_if;
+ } else if( strcmp( ident, "import" ) == 0 ) {
+ return S_import;
}
return S_ident;
case 'k':
@@ -279,6 +285,9 @@ static Symbol getSym( )
case ';':
look = getChar( );
return S_semicolon;
+ case ',':
+ look = getChar( );
+ return S_comma;
case '+':
look = getChar( );
return S_plus;
@@ -366,6 +375,12 @@ static void expression( void );
static void factor( void )
{
+ if( sym == S_plus ) {
+ sym = getSym( );
+ } else if( sym == S_minus ) {
+ sym = getSym( );
+ emit( "-" );
+ }
if( sym == S_number ) {
emit( "%d", num );
sym = getSym( );
@@ -403,12 +418,6 @@ static void term( void )
static void expression( void )
{
- if( sym == S_plus ) {
- sym = getSym( );
- } else if( sym == S_minus ) {
- sym = getSym( );
- emit( "-" );
- }
term( );
while( sym == S_plus || sym == S_minus ) {
if( sym == S_plus ) emit( "+" );
@@ -435,21 +444,34 @@ static void assignment( void )
static void condition( )
{
identifier( );
- sym = getSym( );
+ /* 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( );
+ emitLn( ") {" );
Expect( S_do );
statementSequence( );
if( sym == S_else ) {
+ emitLn( " } else {" );
sym = getSym( );
statementSequence( );
}
Expect( S_end );
+ emitLn( "}" );
}
static void statement( void )
@@ -529,6 +551,25 @@ static void declarationBlock( void )
}
}
+static void handleImport( void )
+{
+ fprintf( stderr, "Import module '%s'\n", ident );
+}
+
+static void importBlock( void )
+{
+ Expect( S_import );
+ identifier( );
+ handleImport( );
+ sym = getSym( );
+ while( sym == S_comma ) {
+ identifier( );
+ handleImport( );
+ sym = getSym( );
+ }
+ Expect( S_semicolon );
+}
+
static void module( void )
{
Expect( S_module );
@@ -538,6 +579,9 @@ static void module( void )
}
Expect( S_ident );
Expect( S_semicolon );
+ if( sym == S_import ) {
+ importBlock( );
+ }
if( sym == S_var ) {
declarationBlock( );
}
diff --git a/minie/ec.e b/minie/ec.e
new file mode 100644
index 0000000..de60f26
--- /dev/null
+++ b/minie/ec.e
@@ -0,0 +1,20 @@
+module ec;
+
+import system;
+
+procedure prologue;
+begin
+ system.writeln( "[bits 32]" );
+ system.writeln( "cpu 486" );
+ system.writeln( "org 0x1000000" );
+end
+
+procedure epilogue;
+begin
+ system.writeln( "hlt" );
+end
+
+begin
+ prologue;
+ epilogue;
+end
diff --git a/minie/test4.e b/minie/test4.e
index 22a69d8..0ad93e0 100644
--- a/minie/test4.e
+++ b/minie/test4.e
@@ -15,10 +15,15 @@ begin
c := 45;
x := c;
if false do
- res := 1;
- b := true;
+ if true do
+ res := 1;
+ else
+ res := 2;
+ if false do res := 3 end
+ end;
+ b := true
else
res := 2;
- b := false;
+ b := false
end
end