This repository has been archived by the owner on Jun 28, 2024. It is now read-only.
generated from dthain/compilerbook-starter-code
-
Notifications
You must be signed in to change notification settings - Fork 0
/
param_list.c
64 lines (59 loc) · 1.31 KB
/
param_list.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include <stdlib.h>
#include <stdio.h>
#include "param_list.h"
#include "type.h"
#include "expr.h"
#include "symbol.h"
#include "scope.h"
struct param_list* param_list_create(char* name, struct type* type, struct param_list* next)
{
struct param_list* p = malloc(sizeof(*p));
p->name = name;
p->type = type;
p->next = next;
return p;
}
void param_list_print(struct param_list* p)
{
if (!p) return;
printf("%s: ", p->name);
type_print(p->type);
if (p->next)
{
printf(", ");
param_list_print(p->next);
}
}
void param_list_resolve(struct param_list* p)
{
if (!p)
{
cur_param = 0;
return;
}
if (scope_lookup_current(p->name))
{
fprintf(stderr, "Resolve Error | duplicate parameter '%s'\n", p->name);
res_errors++;
}
struct symbol* s = symbol_create(SYMBOL_PARAM, p->type, p->name, cur_param++);
scope_bind(p->name, s);
if (p->type->kind == TYPE_ARRAY)
expr_resolve(p->type->size);
param_list_resolve(p->next);
}
int param_typecheck(const struct param_list* params, const struct expr* args)
{
if (!params && !args) return 1; // Both null
if (!params || !args) return 0; // One null
const struct param_list* p = params;
const struct expr* a = args;
while (p && a)
{
if (!type_equals(p->type, expr_typecheck(a->left)))
return 0;
p = p->next;
a = a->right;
}
return !p && !a; // Both null
}