Skip to content

Commit

Permalink
fn defs (a4)
Browse files Browse the repository at this point in the history
  • Loading branch information
SantriptaSharma committed Nov 21, 2023
1 parent 679d044 commit 4379e37
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 13 deletions.
76 changes: 70 additions & 6 deletions 15_A4.y
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ TODO: write everything up in assignment pdf
} decl;
ArgList *argument_list;
ArgListElem arg;
QuadList *next_list;
char *string;
int val;

Expand Down Expand Up @@ -199,17 +200,35 @@ declaration:

case FUNC_T:
$$.sym->type.func.return_type->primitive = $1;

current_table = $$.sym->inner_table;

Symbol *sym = SymInit($$.sym->type.func.return_type->kind);
sym->name = strdup("__retval");
sym->type = *$$.sym->type.func.return_type;
sym->size = GetSize(sym->type);

SymInsert(sym);

current_table = &glb_table;
break;
}

if (SymLookup($$.sym->name) != NULL) {
Symbol *existing = SymLookup($$.sym->name);

if (existing != NULL && $$.sym->type.kind != FUNC_T) {
char err[384];
sprintf(err, "redeclaration of symbol %s", $$.sym->name);
yyerror(err);
YYABORT;
} else if (existing != NULL) {
// TODO: validate signature against existing entry
SymFree($$.sym);
$$.sym = existing;
} else {
SymInsert($$.sym);
}

SymInsert($$.sym);

// TODO: verify types before emitting assignment
if ($$.has_init == 1) {
Expand Down Expand Up @@ -267,13 +286,18 @@ direct_declarator:
$$.sym->name = $1;
}
| IDENTIFIER '(' parameter_list ')' {
if (current_table != &glb_table) {
yyerror("function declaration/definition outside of global scope");
YYABORT;
}

$$.sym = SymInit(FUNC_T);
$$.sym->type.func.arg_list = $3;
$$.sym->type.func.return_type = calloc(1, sizeof(*$$.sym->type.func.return_type));
*$$.sym->type.func.return_type = prim2type(INT_T);
$$.sym->name = $1;
$$.sym->inner_table = Create_SymbolTable($$.sym->name, FUNC, &glb_table);

SymbolTable *t = current_table;
;
current_table = $$.sym->inner_table;

ArgList *it = $$.sym->type.func.arg_list;
Expand All @@ -290,12 +314,18 @@ direct_declarator:
it = it->next;
}

current_table = t;
current_table = &glb_table;
}
| IDENTIFIER '(' ')' {
if (current_table != &glb_table) {
yyerror("function declaration/definition outside of global scope");
YYABORT;
}

$$.sym = SymInit(FUNC_T);
$$.sym->type.func.arg_list = NULL;
$$.sym->type.func.return_type = calloc(1, sizeof(*$$.sym->type.func.return_type));
*$$.sym->type.func.return_type = prim2type(INT_T);
$$.sym->name = $1;
$$.sym->inner_table = Create_SymbolTable($$.sym->name, FUNC, &glb_table);
}
Expand Down Expand Up @@ -369,7 +399,41 @@ external_declaration:
| declaration {log("external-declaration")}

function_definition:
type_specifier declarator compound_statement {log("function-definition")}
type_specifier declarator {
if ($2.sym->type.kind != FUNC_T) {
yyerror("function definition must be a function");
YYABORT;
}

$2.sym->type.func.return_type->primitive = $1;

current_table = $2.sym->inner_table;

Symbol *sym = SymInit($2.sym->type.func.return_type->kind);
sym->name = strdup("__retval");
sym->type = *$2.sym->type.func.return_type;
sym->size = GetSize(sym->type);

SymInsert(sym);

current_table = &glb_table;

Symbol *existing = SymLookup($2.sym->name);

if (existing != NULL) {
// TODO: validate signature against existing entry
SymFree($2.sym);
$2.sym = existing;
} else {
SymInsert($2.sym);
}

current_table = $2.sym->inner_table;

// TODO: annotate statements with next lists and fill them in properly, function code guard???
} compound_statement {
current_table = &glb_table;
}
%%

void yyerror(char *s) {
Expand Down
18 changes: 12 additions & 6 deletions 15_A4_translator.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ void Insert(QuadList *list, int ind) {
}

QuadList *Merge(QuadList *list1, QuadList *list2) {
// could be in place but im the RAM devil 😈
QuadList *new = MakeList(-1);

QuadList *it = list1;
Expand Down Expand Up @@ -323,12 +324,12 @@ void TypeFree(Type *type)
}

static const char *TypeSym[] = {
[PRIMITIVE_T] "PRIMITIVE_T",
[PRIMITIVE_PTR] "PRIMITIVE_PTR",
[ARRAY_PTR] "ARRAY_PTR",
[TEMP_T] "TEMP_T",
[ARRAY_T] "ARRAY_T",
[FUNC_T] "FUNC_T"
[PRIMITIVE_T] "PRIM",
[PRIMITIVE_PTR] "PRIM*",
[ARRAY_PTR] "ARR*",
[TEMP_T] "TEMP",
[ARRAY_T] "ARR",
[FUNC_T] "FUNC"
};

static const char *PrimitiveSym[] = {
Expand Down Expand Up @@ -360,6 +361,11 @@ void TypeDispl(Type t) {
break;

case FUNC_T:
if (arg == NULL) {
printf("NIL -> %s", PrimitiveSym[t.func.return_type->primitive]);
break;
}

while (arg != NULL) {
if (arg->elem.decl.type.kind == PRIMITIVE_PTR || arg->elem.decl.type.kind == ARRAY_PTR)
printf("*");
Expand Down
9 changes: 8 additions & 1 deletion testthing.nc
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
int a = 5 + 3 + "garbo king";
int d = a + 5;
int *make_fib_list(int a, int d, char *thing, void *list_copied);
int b = 110 + 'c' + "garbo king" + a;
void garbo();
char c = 352[10] + 12 * 32 - 3 * 4 * "okiii";
void garbo();

int *make_fib_list(int a, int d, char *thing, void *list_copied);
int *make_fib_list(int a, char d, int *af) {
int car = "dream_machine" + a;
garbo();
int thing = 10;
}

0 comments on commit 4379e37

Please sign in to comment.