Skip to content

Commit

Permalink
stmt (a4): complete statements grammar i think
Browse files Browse the repository at this point in the history
  • Loading branch information
SantriptaSharma committed Nov 21, 2023
1 parent 4379e37 commit e8ff4da
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 27 deletions.
94 changes: 73 additions & 21 deletions 15_A4.y
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,28 @@ TODO: write everything up in assignment pdf

%union {
ExprAttrib expr;

struct _decl_attrib {
Symbol *sym;
ExprAttrib init;
char has_init;
} decl;

ArgList *argument_list;
ArgListElem arg;

struct _opt_expr {
ExprAttrib expr;
char has_expr;
} opt_expr;

PRIMITIVE_TYPE type_spec;

QuadList *next_list;
size_t quad_index;

char *string;
int val;

PRIMITIVE_TYPE type_spec;
}

/* %token KEYWORD */
Expand Down Expand Up @@ -82,9 +92,26 @@ TODO: write everything up in assignment pdf
%type <arg> parameter_declaration
%type <expr> initializer

%type <next_list> statement
%type <next_list> compound_statement
%type <next_list> block_item_list
%type <next_list> block_item
%type <opt_expr> opt_expression
%type <next_list> expression_statement
%type <next_list> selection_statement
%type <next_list> iteration_statement
%type <next_list> jump_statement

%type <quad_index> marker
%type <next_list> guard

%start translation_unit

%%
/* auxiliary symbols */
marker: { $$ = quads_size; }
guard: { $$ = MakeList(quads_size); Emit(Jump(AImm(0))); }

/* expressions */
/* TODO: validate types and add implicit conversions
Compatible Types: (int, char, temp, array, any_ptr)
Expand Down Expand Up @@ -350,44 +377,67 @@ parameter_declaration:
}

initializer:
assignment_expression {log("initializer")}
assignment_expression

/* Statements */
statement:
compound_statement {log("statement")}
| expression_statement {log("statement")}
| selection_statement {log("statement")}
| iteration_statement {log("statement")}
| jump_statement {log("statement")}
compound_statement
| expression_statement
| selection_statement
| iteration_statement
| jump_statement

compound_statement:
'{' '}' {log("compound-statement")}
| '{' block_item_list '}' {log("compound-statement")}
'{' '}' { $$ = NULL; }
| '{' block_item_list '}' { $$ = $2; }

block_item_list:
block_item {log("block-item-list")}
| block_item_list block_item {log("block-item-list")}
block_item
| block_item_list marker block_item { Backpatch($1, $2); $$ = $3; }

block_item:
declaration {log("block-item")}
| statement {log("block-item")}
declaration { $$ = NULL; }
| statement

opt_expression:
expression
| /* empty */
expression { $$.expr = $1; $$.has_expr = 1; }
| /* empty */ { $$.has_expr = 0; }

expression_statement:
opt_expression ';' {log("expression-statement")}
opt_expression ';' { $$ = NULL; }

selection_statement:
IF '(' expression ')' statement {log("selection-statement")}
| IF '(' expression ')' statement ELSE statement {log("selection-statement")}
IF '(' expression ')' marker statement { Backpatch($3.truelist, $5); $$ = Merge($3.falselist, $6); }
| IF '(' expression ')' marker statement guard ELSE marker statement {
Backpatch($3.truelist, $5); Backpatch($3.falselist, $9);
$$ = Merge($6, $7); $$ = Merge($$, $10);
}

iteration_statement:
FOR '(' opt_expression ';' opt_expression ';' opt_expression ')' statement {log("iteration-statement")}
FOR '(' opt_expression ';' marker opt_expression ';' marker opt_expression guard ')' marker statement {
QuadList *tl = $6.has_expr ? $6.expr.truelist : NULL;
QuadList *fl = $6.has_expr ? $6.expr.falselist : NULL;

Backpatch(tl, $12); Backpatch($10, $5); Backpatch($13, $8);
Emit(Jump(AImm($5)));
$$ = fl;
}

jump_statement:
RETURN opt_expression ';' {log("jump-statement")}
RETURN opt_expression ';' {
if (current_table->scope != FUNC) {
yyerror("return statement outside of function");
YYABORT;
}

$$ = NULL;

if ($2.has_expr == 1) {
Emit(Mov(((Addr){SYMBOL_A, .sym = SymLookup("__retval")}), ASym($2.expr)));
}

Emit(Return(AImm(0)));
}

/* TLU */
translation_unit:
Expand Down Expand Up @@ -433,6 +483,8 @@ function_definition:
// TODO: annotate statements with next lists and fill them in properly, function code guard???
} compound_statement {
current_table = &glb_table;

Emit(Return(AImm(0)));
}
%%

Expand Down
21 changes: 17 additions & 4 deletions 15_A4_translator.c
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ static void DisplayAddr(Addr a) {
}
}

// TODO: too many assumptions about type of address, generalise to DisplayAddr
void DisplayQuad(Quad q) {
switch (q.opcode) {
case ADD:
Expand All @@ -149,17 +150,17 @@ void DisplayQuad(Quad q) {
DisplayAddr(q.rs);
break;
case JMP:
printf("goto %s", q.rd.sym->name);
printf("goto %d", q.rd.imm);
break;
case JIF:
printf("if ");
DisplayAddr(q.rs);
printf(" goto %s", q.rd.sym->name);
printf(" goto %d", q.rd.imm);
break;
case JNT:
printf("ifFalse ");
DisplayAddr(q.rs);
printf(" goto %s", q.rd.sym->name);
printf(" goto %d", q.rd.imm);
break;
case JLT:
case JGT:
Expand All @@ -171,7 +172,7 @@ void DisplayQuad(Quad q) {
DisplayAddr(q.rs);
printf(" %s ", OpSym[q.opcode]);
DisplayAddr(q.rt);
printf(" goto %s", q.rd.sym->name);
printf(" goto %d", q.rd.imm);
break;
case PAR:
printf("param ");
Expand All @@ -186,6 +187,9 @@ void DisplayQuad(Quad q) {
break;
case RET:
printf("return");
if (q.rs.kind != IMMEDIATE) {
printf(" %s", q.rs.sym->name);
}
break;
case INDR:
printf("%s = %s[", q.rd.sym->name, q.rs.sym->name);
Expand Down Expand Up @@ -416,6 +420,15 @@ Symbol *SymLookup(const char *name) {
sym = sym->next;
}

SymbolTable *tab = current_table;
if (current_table->parent != NULL) {
current_table = current_table->parent;
sym = SymLookup(name);
current_table = tab;

return sym;
}

return NULL;
}

Expand Down
2 changes: 1 addition & 1 deletion 15_A4_translator.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ void Emit(Quad q);
#define Param(source) ((Quad){PAR, source, AImm(0), AImm(0)})
#define Call(func) ((Quad){CAL, AImm(0), AImm(0), func})
#define CallAss(dest, func) ((Quad){CAL, dest, AImm(0), func})
#define Return() ((Quad){RET, AImm(0), AImm(0), AImm(0)})
#define Return(val) ((Quad){RET, val, AImm(0), AImm(0)})
#define IndexRead(dest, source, index) ((Quad){INDR, source, index, dest})
#define IndexWrite(dest, index, source) ((Quad){INDW, source, index, dest})
#define DerefWrite(dest, source) ((Quad){PTRW, dest, AImm(0), source})
Expand Down
13 changes: 13 additions & 0 deletions simpletest.nc
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
int GLOB = 1;
char POPPING_thing = 'c';

void printf();

int main(int argc, char *argv) {
int i = GLOB;

for (i = 0; i < argc; i = i + 1)
{
printf("%d, ", argv);
}
}
2 changes: 1 addition & 1 deletion testthing.nc
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ void garbo();
int *make_fib_list(int a, char d, int *af) {
int car = "dream_machine" + a;
garbo();
int thing = 10;
// int thing = 10
}

0 comments on commit e8ff4da

Please sign in to comment.