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)
}
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;
.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<nedges; i++) {
struct node *from = bsearch(edgedata[i].from, nodes, nnodes,
sizeof(struct node), nodecmp);
fprintf(stderr, "edge to references unknown node\n");
goto end;
}
- edges[i] = (struct edge2){.from=from, .to=to};
+ edges[i] = (struct edge){.from=from, .to=to};
}
struct node **stack = safe_malloc(nnodes*sizeof(struct node *));
#ifndef SCC_H
#define SCC_H
-#include <stdbool.h>
struct edge {
void *from;
void *to;
* @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
#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;
struct ast *type(struct ast *ast)
{
- for (int i = 0; i<ast->ndecls; i++)
- ast->decls[i] = type_decl(ast->decls[i]);
+ for (int i = 0; i<ast->ndecls; i++) {
+ if (ast->decls[i]->type == dvardecl) {
+ //Check globals
+ check_expr_constant(ast->decls[i]->data.dvar->expr);
+ break;
+ }
+ }
return ast;
}
+