From 7a5539455e07826c0d21c01602a40d0abc09741c Mon Sep 17 00:00:00 2001 From: Mart Lubbers Date: Thu, 11 Feb 2021 14:47:07 +0100 Subject: [PATCH] constant globals checking --- ast.c | 1 + parse.y | 1 + scc.c | 11 +++-------- scc.h | 3 +-- type.c | 39 +++++++++++++++++++++++++++++++++++++-- 5 files changed, 43 insertions(+), 12 deletions(-) diff --git a/ast.c b/ast.c index e9ebb56..72af738 100644 --- a/ast.c +++ b/ast.c @@ -4,6 +4,7 @@ #include "util.h" #include "ast.h" +#include "list.h" #include "parse.h" const char *binop_str[] = { diff --git a/parse.y b/parse.y index 6229385..8a8dca7 100644 --- a/parse.y +++ b/parse.y @@ -2,6 +2,7 @@ #include #include "ast.h" +#include "list.h" #include "parse.h" int yylex(void); diff --git a/scc.c b/scc.c index 3c690a2..a3743e2 100644 --- a/scc.c +++ b/scc.c @@ -14,11 +14,6 @@ struct node { void *data; }; -struct edge2 { - struct node *from; - struct node *to; -}; - int ptrcmp(const void *l, const void *r) { return ((ptrdiff_t)((struct node *)l)->data) @@ -31,7 +26,7 @@ int nodecmp(const void *l, const void *r) } struct components *strongconnect(struct node *v, struct node **stack, int *sp, - int *index, int nedges, struct edge2 *edges, struct components *components) + int *index, int nedges, struct edge *edges, struct components *components) { struct node *w; v->index = *index; @@ -76,7 +71,7 @@ struct components *scc(int nnodes, void **nodedata, int nedges, struct edge *edg .onStack=false, .data=nodedata[i]}; qsort(nodes, nnodes, sizeof(struct node), ptrcmp); - struct edge2 *edges = safe_malloc(nedges*sizeof(struct edge2)); + struct edge *edges = safe_malloc(nedges*sizeof(struct edge)); for (int i = 0; i struct edge { void *from; void *to; @@ -33,6 +32,6 @@ struct components *scc(int nnodes, void **nodedata, int nedges, struct edge *edg * @param cs components * @param freefun function to free the data with, if NULL, data isn't freed */ -void components_free(struct components *cs, void (*free)(void *)); +void components_free(struct components *cs, void (*freefun)(void *)); #endif diff --git a/type.c b/type.c index f1828bc..3325de8 100644 --- a/type.c +++ b/type.c @@ -1,5 +1,34 @@ #include "ast.h" +void type_error(const char *msg, ...) +{ + va_list ap; + va_start(ap, msg); + fprintf(stderr, "type error: "); + vfprintf(stderr, msg, ap); + va_end(ap); + die(""); +} + +void check_expr_constant(struct expr *expr) +{ + switch (expr->type) { + case ebinop: + check_expr_constant(expr->data.ebinop.l); + check_expr_constant(expr->data.ebinop.r); + break; + case eunop: + check_expr_constant(expr->data.eunop.l); + break; + case efuncall: + case eident: + type_error("Initialiser is not constant\n"); + break; + default: + break; + } +} + struct vardecl *type_vardecl(struct vardecl *vardecl) { return vardecl; @@ -23,7 +52,13 @@ struct decl *type_decl(struct decl *decl) struct ast *type(struct ast *ast) { - for (int i = 0; indecls; i++) - ast->decls[i] = type_decl(ast->decls[i]); + for (int i = 0; indecls; i++) { + if (ast->decls[i]->type == dvardecl) { + //Check globals + check_expr_constant(ast->decls[i]->data.dvar->expr); + break; + } + } return ast; } + -- 2.20.1