Skip to content
This repository has been archived by the owner on Feb 14, 2022. It is now read-only.

Commit

Permalink
Actually compiling return codes!
Browse files Browse the repository at this point in the history
  • Loading branch information
Wilfred committed Jul 12, 2014
1 parent e54125c commit 65f3a01
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 3 deletions.
7 changes: 6 additions & 1 deletion minimal_c.l
Expand Up @@ -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; }
Expand Down
9 changes: 7 additions & 2 deletions minimal_c.y
Expand Up @@ -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");

Expand All @@ -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");
Expand Down Expand Up @@ -73,5 +77,6 @@ function:

expression:
RETURN NUMBER ';'
{ return_code = $2; }
;

3 changes: 3 additions & 0 deletions test_programs/two.c
@@ -0,0 +1,3 @@
int main() {
return 2;
}

0 comments on commit 65f3a01

Please sign in to comment.