aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorBad Diode <bd@badd10de.dev>2021-10-23 14:16:45 +0200
committerBad Diode <bd@badd10de.dev>2021-10-23 14:16:45 +0200
commitbd1480fd2cb80680933b80900c5fb13b5f88ca42 (patch)
tree26443fdbacae70f25642dcc3936d2b054aa4c4be /src
parent74f9badefe95c070c0294d90cbf70965d8f68b7a (diff)
downloadbdl-bd1480fd2cb80680933b80900c5fb13b5f88ca42.tar.gz
bdl-bd1480fd2cb80680933b80900c5fb13b5f88ca42.zip
Fix a bug in signed fixnum compilation
Diffstat (limited to 'src')
-rwxr-xr-xsrc/bytecode/compiler.h96
-rwxr-xr-xsrc/bytecode/debug.h4
2 files changed, 36 insertions, 64 deletions
diff --git a/src/bytecode/compiler.h b/src/bytecode/compiler.h
index 201dca6..ae8fac1 100755
--- a/src/bytecode/compiler.h
+++ b/src/bytecode/compiler.h
@@ -52,33 +52,13 @@ parse_fixnum(Chunk *chunk, Token tok) {
52 } 52 }
53 num = num * 10 + (c - '0'); 53 num = num * 10 + (c - '0');
54 } 54 }
55 emit_constant(chunk, tok, num); 55 emit_constant(chunk, tok, num * sign);
56} 56}
57 57
58void parse_tree(Chunk *chunk, Visitor *vs); 58void parse_tree(Chunk *chunk, Visitor *vs);
59 59
60void 60void
61compile_list_primitive(Chunk *chunk, Visitor *vs, Token op_tok) { 61compile_list_op(Chunk *chunk, Visitor *vs, Token list_start, Ops op) {
62 Ops op;
63 switch (op_tok.type) {
64 case TOKEN_ADD: {
65 op = OP_SUM;
66 } break;
67 case TOKEN_SUB: {
68 op = OP_SUB;
69 } break;
70 case TOKEN_MUL: {
71 op = OP_MUL;
72 } break;
73 case TOKEN_DIV: {
74 op = OP_DIV;
75 } break;
76 case TOKEN_MOD: {
77 op = OP_MOD;
78 } break;
79 default: {
80 } break;
81 }
82 size_t n = 0; 62 size_t n = 0;
83 while (has_next_token(vs)) { 63 while (has_next_token(vs)) {
84 Token tok = peek_token(vs); 64 Token tok = peek_token(vs);
@@ -86,8 +66,8 @@ compile_list_primitive(Chunk *chunk, Visitor *vs, Token op_tok) {
86 error_push((Error){ 66 error_push((Error){
87 .type = ERR_TYPE_COMPILER, 67 .type = ERR_TYPE_COMPILER,
88 .value = ERR_UNBALANCED_PAREN, 68 .value = ERR_UNBALANCED_PAREN,
89 .line = op_tok.line, 69 .line = list_start.line,
90 .col = op_tok.column, 70 .col = list_start.column,
91 }); 71 });
92 return; 72 return;
93 } 73 }
@@ -98,42 +78,44 @@ compile_list_primitive(Chunk *chunk, Visitor *vs, Token op_tok) {
98 parse_tree(chunk, vs); 78 parse_tree(chunk, vs);
99 n++; 79 n++;
100 if (n > 1) { 80 if (n > 1) {
101 add_code(chunk, op, tok.line, tok.column); 81 add_code(chunk, op, list_start.line, list_start.column);
102 } 82 }
103 } 83 }
104 if (n == 0) { 84 if (n == 0) {
105 error_push((Error){ 85 error_push((Error){
106 .type = ERR_TYPE_COMPILER, 86 .type = ERR_TYPE_COMPILER,
107 .value = ERR_NOT_ENOUGH_ARGS, 87 .value = ERR_NOT_ENOUGH_ARGS,
108 .line = op_tok.line, 88 .line = list_start.line,
109 .col = op_tok.column, 89 .col = list_start.column,
110 }); 90 });
111 } 91 }
112} 92}
113 93
114void 94void
115parse_list(Chunk *chunk, Visitor *vs) { 95parse_list(Chunk *chunk, Visitor *vs, Token list_start) {
116 if (has_next_token(vs)) { 96 if (!has_next_token(vs)) {
117 Token tok = next_token(vs); 97 error_push((Error){
118 print_token(tok); 98 .type = ERR_TYPE_COMPILER,
119 // TODO: check if is function call. 99 .value = ERR_UNBALANCED_PAREN,
120 switch (tok.type) { 100 .line = list_start.line,
121 case TOKEN_ADD: 101 .col = list_start.column,
122 case TOKEN_SUB: 102 });
123 case TOKEN_MUL: 103 }
124 case TOKEN_DIV: 104 Token tok = next_token(vs);
125 case TOKEN_MOD:{ 105 switch (tok.type) {
126 compile_list_primitive(chunk, vs, tok); 106 case TOKEN_ADD: { compile_list_op(chunk, vs, list_start, OP_SUM); } break;
127 } break; 107 case TOKEN_SUB: { compile_list_op(chunk, vs, list_start, OP_SUB); } break;
128 default: { 108 case TOKEN_MUL: { compile_list_op(chunk, vs, list_start, OP_MUL); } break;
129 error_push((Error){ 109 case TOKEN_DIV: { compile_list_op(chunk, vs, list_start, OP_DIV); } break;
130 .type = ERR_TYPE_COMPILER, 110 case TOKEN_MOD: { compile_list_op(chunk, vs, list_start, OP_MOD); } break;
131 .value = ERR_OBJ_NOT_CALLABLE, 111 default: {
132 .line = tok.line, 112 error_push((Error){
133 .line = tok.column, 113 .type = ERR_TYPE_COMPILER,
134 }); 114 .value = ERR_OBJ_NOT_CALLABLE,
135 } break; 115 .line = tok.line,
136 } 116 .col = tok.column,
117 });
118 } break;
137 } 119 }
138} 120}
139 121
@@ -175,7 +157,7 @@ parse_tree(Chunk *chunk, Visitor *vs) {
175 return; 157 return;
176 } break; 158 } break;
177 case TOKEN_LPAREN: { 159 case TOKEN_LPAREN: {
178 parse_list(chunk, vs); 160 parse_list(chunk, vs, tok);
179 // Object *obj = parse_list(vs); 161 // Object *obj = parse_list(vs);
180 // if (obj == obj_err) { 162 // if (obj == obj_err) {
181 // error_push((Error){ 163 // error_push((Error){
@@ -226,21 +208,11 @@ compile(Token *tokens) {
226 .tokens = tokens, 208 .tokens = tokens,
227 .current = 0, 209 .current = 0,
228 }; 210 };
211 Token start_tok = peek_token(&visitor);
229 while (has_next_token(&visitor) && peek_token(&visitor).type != TOKEN_EOF) { 212 while (has_next_token(&visitor) && peek_token(&visitor).type != TOKEN_EOF) {
230 parse_tree(chunk, &visitor); 213 parse_tree(chunk, &visitor);
231 } 214 }
232 // error_push((Error){ 215 add_code(chunk, OP_RETURN, start_tok.line, start_tok.column);
233 // .type = ERR_TYPE_COMPILER,
234 // .value = ERR_UNKNOWN,
235 // });
236 // size_t const_a = add_constant(chunk, 7);
237 // add_code(chunk, OP_CONSTANT, 1, 1);
238 // add_code(chunk, const_a, 1, 1);
239 // size_t const_b = add_constant(chunk, 2);
240 // add_code(chunk, OP_CONSTANT, 1, 2);
241 // add_code(chunk, const_b, 1, 2);
242 // add_code(chunk, OP_MOD, 1, 3);
243 add_code(chunk, OP_RETURN, 1, 1);
244 return chunk; 216 return chunk;
245} 217}
246 218
diff --git a/src/bytecode/debug.h b/src/bytecode/debug.h
index 05f9d8e..25ac693 100755
--- a/src/bytecode/debug.h
+++ b/src/bytecode/debug.h
@@ -38,9 +38,9 @@ disassemble_instruction(Chunk *chunk, size_t offset) {
38 switch (instruction) { 38 switch (instruction) {
39 case OP_CONSTANT: { 39 case OP_CONSTANT: {
40 u8 constant = chunk->code[offset + 1]; 40 u8 constant = chunk->code[offset + 1];
41 printf("%-16s %4d (", "OP_CONSTANT", constant); 41 printf("%-16s %4d -> ", "OP_CONSTANT", constant);
42 display(chunk->constants[constant]); 42 display(chunk->constants[constant]);
43 printf(")\n"); 43 printf("\n");
44 return offset + 2; 44 return offset + 2;
45 } break; 45 } break;
46 case OP_SUM: { printf("OP_SUM\n"); return offset + 1; } break; 46 case OP_SUM: { printf("OP_SUM\n"); return offset + 1; } break;