summaryrefslogtreecommitdiff
path: root/com.c
blob: 6ffd7c21696fe33558d6f6c0e15d25b4e3042b30 (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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#include <stdlib.h>
#include <string.h>
#include <stdio.h>

#include "vm.h"
#include "ast.h"
#include "read.h"
#include "state.h"

static size_t placeholder(State *S, Chunk *ch) {
	size_t old_ix = ch->bc.len;
	chunk_wbc(S, ch, 0x00);
	chunk_wbc(S, ch, 0x00);
	return old_ix;
}

static void patch(State *S, Chunk *ch, size_t addr, uint16_t val) {
	ch->bc.d[addr] = val & 0xff;
	ch->bc.d[addr+1] = (val & 0xff00) >> 8;
}


static void compile_node(State *S, Chunk *ch, AstNode a) {
	switch (a.ty) {
	case AST_IDENT:;
		char *ident = a.as.str;

		if (0 == strcmp(ident, "true")) chunk_wbc(S, ch, OP_TRUE);
		else if (0 == strcmp(ident, "false")) chunk_wbc(S, ch, OP_FALSE);
		else if (0 == strcmp(ident, "nil")) chunk_wbc(S, ch, OP_NIL);
		else {
			// global read
			ObjString *o = objstring_copy_cstr(S, a.as.str);
			chunk_wbc(S, ch, OP_GETGLOBAL);
			chunk_wbc(S, ch, chunk_wconst(S, ch, VAL_OBJ(o)));
		}
		break;
	case AST_NUM:
		chunk_wbc(S, ch, OP_LOADK);
		chunk_wbc(S, ch, chunk_wconst(S, ch, VAL_NUM(a.as.num)));
		break;
	case AST_STRING: {
		ObjString *o = objstring_copy_cstr(S, a.as.str);
		chunk_wbc(S, ch, OP_LOADK);
		chunk_wbc(S, ch, chunk_wconst(S, ch, VAL_OBJ(o)));
		break;
	}
	case AST_LIST: {
		AstVec l = a.as.list;
		#define CK(cond, msg) if (!(cond)) { fputs(msg "\n", stderr); exit(1); }
		CK(l.len > 0, "can't handle empty list");
		CK(l.vals[0].ty == AST_IDENT, "can only call ops");

		char *name = l.vals[0].as.str;

		if (0 == strcmp(name, "puts")) {
			CK(l.len == 2, "puts requires exactly 1 argument");
			compile_node(S, ch, l.vals[1]);
			chunk_wbc(S, ch, OP_PUTS);
		} else if (0 == strcmp(name, "set")) {
			CK(l.len == 3, "set requires exactly 2 arguments");
			AstNode ident = l.vals[1];
			CK(ident.ty == AST_IDENT, "set's first argument must be identifier");

			ObjString *o = objstring_copy_cstr(S, ident.as.str);
			compile_node(S, ch, l.vals[2]);
			chunk_wbc(S, ch, OP_SETGLOBAL);
			chunk_wbc(S, ch, chunk_wconst(S, ch, VAL_OBJ(o)));
		} else if (0 == strcmp(name, "do")) {
			for (int i = 1; i < l.len - 1; i++) {
				compile_node(S, ch, l.vals[i]);
				chunk_wbc(S, ch, OP_DROP);
			}
			compile_node(S, ch, l.vals[l.len - 1]);
		} else if (0 == strcmp(name, "if")) {
			CK(l.len == 4, "if requires exactly 3 arguments");
			// (if cond if-true if-false)
			// cond
			// 0branch ->A
			// if-true
			// skip ->B
			// A: if-false
			// B:
			compile_node(S, ch, l.vals[1]);
			chunk_wbc(S, ch, OP_0BRANCH);
			size_t ph_a = placeholder(S, ch);
			compile_node(S, ch, l.vals[2]);
			chunk_wbc(S, ch, OP_SKIP);
			size_t ph_b = placeholder(S, ch);
			size_t dest_a = ch->bc.len;
			compile_node(S, ch, l.vals[3]);
			size_t dest_b = ch->bc.len;

			patch(S, ch, ph_a, dest_a - ph_a - 2);
			patch(S, ch, ph_b, dest_b - ph_b - 2);

		} else if (0 == strcmp(name, "while")) { 
			CK(l.len >= 3, "while requires at least 2 arguments");
			// (while cond body ...)
			// A:
			// cond
			// 0branch ->B
			// body ....
			// redo ->A
			// B:
			// nil   (while loop always returns nil)
			size_t dest_a = ch->bc.len;
			compile_node(S, ch, l.vals[1]);
			chunk_wbc(S, ch, OP_0BRANCH);
			size_t ph_b = placeholder(S, ch);
			for (int i = 2; i < l.len; i++)  {
				compile_node(S, ch, l.vals[i]);
				chunk_wbc(S, ch, OP_DROP);
			}
			chunk_wbc(S, ch, OP_REDO);
			size_t ph_a = placeholder(S, ch);
			size_t dest_b = ch->bc.len;
			chunk_wbc(S, ch, OP_NIL);

			patch(S, ch, ph_a, ph_a - dest_a + 2);
			patch(S, ch, ph_b, dest_b - ph_b - 2);

		} else {
				
			CK(l.len == 3, "can only compile binary ops");
			char opchar = l.vals[0].as.str[0];
			Op op;
			switch (opchar) {
			#define OP(char, code) case char: op = code; break;
				OP('+', OP_ADD)
				OP('-', OP_SUB)
				OP('*', OP_MUL)
				OP('/', OP_DIV)
				OP('=', OP_EQU)
				OP('<', OP_CMP)
				OP('%', OP_MOD)
			#undef OP
				default:
					printf("unkown op %s\n",l.vals[0].as.str);
					exit(1);
					break;
			}
			compile_node(S, ch, l.vals[1]);
			compile_node(S, ch, l.vals[2]);
			chunk_wbc(S, ch, op);
		}
		#undef CK
	}
	}
}


int main(int argc, char **argv) {
	State st = state_new();
	State *S = &st;
	Thread th = thread_new(S);
	st.th = &th;
	Chunk ch = chunk_new(S);
	th.ch = &ch;

	S->do_disasm = (argc > 1 && 0 == strcmp(argv[1], "-l"));

	char n1[] = "foo";
	char n2[] = "bar";
	char n3[] = "baz";
	ObjString *o1 = objstring_copy_cstr(S, n1);
	ObjString *o2 = objstring_copy_cstr(S, n2);
	ObjString *o3 = objstring_copy_cstr(S, n3);

	ht_put(S, &st.globals, o1, VAL_NUM(69)); 
	ht_put(S, &st.globals, o2, VAL_NUM(2)); 
	ht_put(S, &st.globals, o3, VAL_NUM(3)); 

	AstNode an = read();
	compile_node(S, &ch, an);

	chunk_wbc(S, &ch, OP_PUTS);
	chunk_wbc(S, &ch, OP_RET);

	return runvm(S);
}