Permalink
Browse files

Actually compiling return codes!

  • Loading branch information...
Wilfred committed Jul 12, 2014
1 parent e54125c commit 65f3a0171e25075db886f4d5cc4127ed04a77a88
Showing with 16 additions and 3 deletions.
  1. +6 −1 minimal_c.l
  2. +7 −2 minimal_c.y
  3. +3 −0 test_programs/two.c
@@ -13,7 +13,12 @@
"(" { return '('; }
")" { return ')'; }
";" { return ';'; }
[0-9]+ { return NUMBER; }
[0-9]+ {
/* TODO: check numbers are in the legal range, and don't start with 0. */
printf("yytext: %s\n", yytext);
printf("yylval: %d\n", atoi(yytext));
yylval=atoi(yytext); return NUMBER;
}
"return" { return RETURN; }

"int" { return TYPE; }
@@ -14,6 +14,9 @@ int yywrap()
extern FILE *yyin;
// Shameful hack. We should build a proper AST and traverse it.
static int return_code;
void write_skeleton() {
FILE *out = fopen("out.s", "wb");
@@ -22,8 +25,9 @@ void write_skeleton() {
fprintf(out, " .global _start\n\n");
fprintf(out, "_start:\n");
// Exit code of zero.
fprintf(out, " movl $0, %%ebx\n");
// Exit code as specified.
// TODO: convert to hex properly.
fprintf(out, " movl $%d, %%ebx\n", return_code);
fprintf(out, " movl $1, %%eax\n");
fprintf(out, " int $0x80\n");
@@ -73,5 +77,6 @@ function:
expression:
RETURN NUMBER ';'
{ return_code = $2; }
;
@@ -0,0 +1,3 @@
int main() {
return 2;
}

0 comments on commit 65f3a01

Please sign in to comment.