summaryrefslogtreecommitdiff
path: root/decls_cgen.c
blob: a596f53f61fa2488860f309ee34d8e171e8a1a03 (plain)
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/* C declarations of functions and global variables. e must be a function expression */
static bool cgen_decl_fn(CGenerator *g, FnExpr *f) {
	/* assign an ID to the function */
	if (f->name && g->block == NULL) {
		f->id = f->name->c_fn_reps++;
	} else {
		f->id = g->anon_fn_count++;
	}
	
	if (!cgen_fn_header(g, f)) return false;
	cgen_writeln(g, ";");
	return true;
}

static bool cgen_decls_stmt(CGenerator *g, Statement *s);

static bool cgen_decls_expr(CGenerator *g, Expression *e) {
	switch (e->kind) {
	case EXPR_FN: {
		FnExpr *f = &e->fn;
		if (f->name && g->block == NULL) { /* write named function prototypes in global scope to header file */
			g->writing_to = CGEN_WRITING_TO_H;
		} else {
			g->writing_to = CGEN_WRITING_TO_C;
		}
		if (!cgen_decl_fn(g, f))
			return false;
		g->writing_to = CGEN_WRITING_TO_C;

		
		bool ret = true;
		Block *prev_block = g->block;
		cgen_block_enter(g, &f->body);
		arr_foreach(&f->body.stmts, Statement, s) {
			if (!cgen_decls_stmt(g, s))
				ret = false;
		}
		cgen_block_exit(g, prev_block);
		return ret;
	}
	case EXPR_CALL:
		if (!cgen_decls_expr(g, e->call.fn))
			return false;
		arr_foreach(&e->call.args, Expression, arg) {
			if (!cgen_decls_expr(g, arg))
				return false;
		}
		break;
	default: break;
	}
	return true;
}


static bool cgen_expr(CGenerator *g, Expression *e);
static bool cgen_decls_stmt(CGenerator *g, Statement *s) {
	switch (s->kind) {
	case STMT_EXPR:
		return cgen_decls_expr(g, &s->expr);
	case STMT_DECL: {
		Declaration *d = &s->decl;
		bool is_const_fn = (d->flags & DECL_FLAG_HAS_EXPR) && (d->flags & DECL_FLAG_CONST)
			&& d->expr.kind == EXPR_FN;

		if (is_const_fn) {
			/* e.g. foo @= fn() {}; (we want to set the function's name to "foo") */
			d->expr.fn.name = *(Identifier*)d->idents.data;
		}

		if (d->flags & DECL_FLAG_HAS_EXPR) {
			cgen_decls_expr(g, &d->expr);
		}
		
		if (!is_const_fn) {
			if (g->block == NULL) {
				/* declare this/these global variable(s) */
				arr_foreach(&d->idents, Identifier, i) {
					cgen_type_pre(g, &d->type);
					cgen_ident(g, *i, NULL);
					cgen_type_post(g, &d->type);
					if (d->flags & DECL_FLAG_HAS_EXPR) { /* TODO: check if expr is const */
						cgen_space(g);
						cgen_write(g, "=");
						cgen_space(g);
						if (!cgen_expr(g, &d->expr))
							return false;
					}
					cgen_write(g, ";");
					cgen_space(g);
				}
				cgen_writeln(g, "");
			}
		}
		
	} break;
	}
	return true;
}

static bool cgen_decls_file(CGenerator *g, ParsedFile *f) {
	arr_foreach(&f->stmts, Statement, s) {
		if (!cgen_decls_stmt(g, s))
			return false;
	}
	return true;
}