diff options
author | Bad Diode <bd@badd10de.dev> | 2021-10-24 09:52:09 +0200 |
---|---|---|
committer | Bad Diode <bd@badd10de.dev> | 2021-10-24 09:52:09 +0200 |
commit | b743e03fc6042e3e2d55cfa0387c092824de64c5 (patch) | |
tree | 1c74213017e20fc5bf675f571de2a264cf104cd3 /src/bytecode/compiler.h | |
parent | f372586069ea0a92db65bc90cf844c1a35187430 (diff) | |
download | bdl-b743e03fc6042e3e2d55cfa0387c092824de64c5.tar.gz bdl-b743e03fc6042e3e2d55cfa0387c092824de64c5.zip |
Add print/display/newline ops
Diffstat (limited to 'src/bytecode/compiler.h')
-rwxr-xr-x | src/bytecode/compiler.h | 103 |
1 files changed, 70 insertions, 33 deletions
diff --git a/src/bytecode/compiler.h b/src/bytecode/compiler.h index 02d938f..d404a5a 100755 --- a/src/bytecode/compiler.h +++ b/src/bytecode/compiler.h | |||
@@ -58,7 +58,7 @@ parse_fixnum(Chunk *chunk, Token tok) { | |||
58 | void parse_tree(Chunk *chunk, Visitor *vs); | 58 | void parse_tree(Chunk *chunk, Visitor *vs); |
59 | 59 | ||
60 | void | 60 | void |
61 | compile_list_binary_op(Chunk *chunk, Visitor *vs, Token list_start, Ops op) { | 61 | compile_list_binary_op(Chunk *chunk, Visitor *vs, Token start, Ops op) { |
62 | size_t n = 0; | 62 | size_t n = 0; |
63 | while (has_next_token(vs)) { | 63 | while (has_next_token(vs)) { |
64 | Token tok = peek_token(vs); | 64 | Token tok = peek_token(vs); |
@@ -66,8 +66,8 @@ compile_list_binary_op(Chunk *chunk, Visitor *vs, Token list_start, Ops op) { | |||
66 | error_push((Error){ | 66 | error_push((Error){ |
67 | .type = ERR_TYPE_COMPILER, | 67 | .type = ERR_TYPE_COMPILER, |
68 | .value = ERR_UNBALANCED_PAREN, | 68 | .value = ERR_UNBALANCED_PAREN, |
69 | .line = list_start.line, | 69 | .line = start.line, |
70 | .col = list_start.column, | 70 | .col = start.column, |
71 | }); | 71 | }); |
72 | return; | 72 | return; |
73 | } | 73 | } |
@@ -77,8 +77,8 @@ compile_list_binary_op(Chunk *chunk, Visitor *vs, Token list_start, Ops op) { | |||
77 | error_push((Error){ | 77 | error_push((Error){ |
78 | .type = ERR_TYPE_COMPILER, | 78 | .type = ERR_TYPE_COMPILER, |
79 | .value = ERR_NOT_ENOUGH_ARGS, | 79 | .value = ERR_NOT_ENOUGH_ARGS, |
80 | .line = list_start.line, | 80 | .line = start.line, |
81 | .col = list_start.column, | 81 | .col = start.column, |
82 | }); | 82 | }); |
83 | return; | 83 | return; |
84 | } | 84 | } |
@@ -87,12 +87,12 @@ compile_list_binary_op(Chunk *chunk, Visitor *vs, Token list_start, Ops op) { | |||
87 | parse_tree(chunk, vs); | 87 | parse_tree(chunk, vs); |
88 | n++; | 88 | n++; |
89 | } | 89 | } |
90 | emit_constant(chunk, list_start, FIXNUM_VAL(n)); | 90 | emit_constant(chunk, start, FIXNUM_VAL(n)); |
91 | add_code(chunk, op, list_start.line, list_start.column); | 91 | add_code(chunk, op, start.line, start.column); |
92 | } | 92 | } |
93 | 93 | ||
94 | void | 94 | void |
95 | compile_list_unary_op(Chunk *chunk, Visitor *vs, Token list_start, Ops op) { | 95 | compile_list_unary_op(Chunk *chunk, Visitor *vs, Token start, Ops op) { |
96 | size_t n = 0; | 96 | size_t n = 0; |
97 | while (has_next_token(vs)) { | 97 | while (has_next_token(vs)) { |
98 | Token tok = peek_token(vs); | 98 | Token tok = peek_token(vs); |
@@ -100,8 +100,8 @@ compile_list_unary_op(Chunk *chunk, Visitor *vs, Token list_start, Ops op) { | |||
100 | error_push((Error){ | 100 | error_push((Error){ |
101 | .type = ERR_TYPE_COMPILER, | 101 | .type = ERR_TYPE_COMPILER, |
102 | .value = ERR_UNBALANCED_PAREN, | 102 | .value = ERR_UNBALANCED_PAREN, |
103 | .line = list_start.line, | 103 | .line = start.line, |
104 | .col = list_start.column, | 104 | .col = start.column, |
105 | }); | 105 | }); |
106 | return; | 106 | return; |
107 | } | 107 | } |
@@ -111,57 +111,94 @@ compile_list_unary_op(Chunk *chunk, Visitor *vs, Token list_start, Ops op) { | |||
111 | error_push((Error){ | 111 | error_push((Error){ |
112 | .type = ERR_TYPE_COMPILER, | 112 | .type = ERR_TYPE_COMPILER, |
113 | .value = ERR_NOT_ENOUGH_ARGS, | 113 | .value = ERR_NOT_ENOUGH_ARGS, |
114 | .line = list_start.line, | 114 | .line = start.line, |
115 | .col = list_start.column, | 115 | .col = start.column, |
116 | }); | 116 | }); |
117 | } | 117 | } |
118 | return; | 118 | return; |
119 | } | 119 | } |
120 | parse_tree(chunk, vs); | 120 | parse_tree(chunk, vs); |
121 | add_code(chunk, op, list_start.line, list_start.column); | 121 | add_code(chunk, op, start.line, start.column); |
122 | n++; | 122 | n++; |
123 | if (n > 1) { | 123 | if (n > 1) { |
124 | error_push((Error){ | 124 | error_push((Error){ |
125 | .type = ERR_TYPE_COMPILER, | 125 | .type = ERR_TYPE_COMPILER, |
126 | .value = ERR_TOO_MANY_ARGS, | 126 | .value = ERR_TOO_MANY_ARGS, |
127 | .line = list_start.line, | 127 | .line = start.line, |
128 | .col = list_start.column, | 128 | .col = start.column, |
129 | }); | 129 | }); |
130 | } | 130 | } |
131 | } | 131 | } |
132 | error_push((Error){ | 132 | error_push((Error){ |
133 | .type = ERR_TYPE_COMPILER, | 133 | .type = ERR_TYPE_COMPILER, |
134 | .value = ERR_UNBALANCED_PAREN, | 134 | .value = ERR_UNBALANCED_PAREN, |
135 | .line = list_start.line, | 135 | .line = start.line, |
136 | .col = list_start.column, | 136 | .col = start.column, |
137 | }); | 137 | }); |
138 | } | 138 | } |
139 | 139 | ||
140 | void | 140 | void |
141 | parse_list(Chunk *chunk, Visitor *vs, Token list_start) { | 141 | compile_list_simple_op(Chunk *chunk, Visitor *vs, Token start, Ops op) { |
142 | if (has_next_token(vs)) { | ||
143 | Token tok = peek_token(vs); | ||
144 | if (tok.type == TOKEN_EOF) { | ||
145 | error_push((Error){ | ||
146 | .type = ERR_TYPE_COMPILER, | ||
147 | .value = ERR_UNBALANCED_PAREN, | ||
148 | .line = start.line, | ||
149 | .col = start.column, | ||
150 | }); | ||
151 | return; | ||
152 | } | ||
153 | if (tok.type != TOKEN_RPAREN) { | ||
154 | error_push((Error){ | ||
155 | .type = ERR_TYPE_COMPILER, | ||
156 | .value = ERR_TOO_MANY_ARGS, | ||
157 | .line = start.line, | ||
158 | .col = start.column, | ||
159 | }); | ||
160 | return; | ||
161 | } | ||
162 | next_token(vs); | ||
163 | add_code(chunk, op, start.line, start.column); | ||
164 | return; | ||
165 | } | ||
166 | error_push((Error){ | ||
167 | .type = ERR_TYPE_COMPILER, | ||
168 | .value = ERR_UNBALANCED_PAREN, | ||
169 | .line = start.line, | ||
170 | .col = start.column, | ||
171 | }); | ||
172 | } | ||
173 | |||
174 | void | ||
175 | parse_list(Chunk *chunk, Visitor *vs, Token start) { | ||
142 | if (!has_next_token(vs)) { | 176 | if (!has_next_token(vs)) { |
143 | error_push((Error){ | 177 | error_push((Error){ |
144 | .type = ERR_TYPE_COMPILER, | 178 | .type = ERR_TYPE_COMPILER, |
145 | .value = ERR_UNBALANCED_PAREN, | 179 | .value = ERR_UNBALANCED_PAREN, |
146 | .line = list_start.line, | 180 | .line = start.line, |
147 | .col = list_start.column, | 181 | .col = start.column, |
148 | }); | 182 | }); |
149 | } | 183 | } |
150 | Token tok = next_token(vs); | 184 | Token tok = next_token(vs); |
151 | switch (tok.type) { | 185 | switch (tok.type) { |
152 | case TOKEN_ADD: { compile_list_binary_op(chunk, vs, list_start, OP_SUM); } break; | 186 | case TOKEN_ADD: { compile_list_binary_op(chunk, vs, start, OP_SUM); } break; |
153 | case TOKEN_SUB: { compile_list_binary_op(chunk, vs, list_start, OP_SUB); } break; | 187 | case TOKEN_SUB: { compile_list_binary_op(chunk, vs, start, OP_SUB); } break; |
154 | case TOKEN_MUL: { compile_list_binary_op(chunk, vs, list_start, OP_MUL); } break; | 188 | case TOKEN_MUL: { compile_list_binary_op(chunk, vs, start, OP_MUL); } break; |
155 | case TOKEN_DIV: { compile_list_binary_op(chunk, vs, list_start, OP_DIV); } break; | 189 | case TOKEN_DIV: { compile_list_binary_op(chunk, vs, start, OP_DIV); } break; |
156 | case TOKEN_MOD: { compile_list_binary_op(chunk, vs, list_start, OP_MOD); } break; | 190 | case TOKEN_MOD: { compile_list_binary_op(chunk, vs, start, OP_MOD); } break; |
157 | case TOKEN_NOT: { compile_list_unary_op(chunk, vs, list_start, OP_NOT); } break; | 191 | case TOKEN_NOT: { compile_list_unary_op(chunk, vs, start, OP_NOT); } break; |
158 | case TOKEN_AND: { compile_list_binary_op(chunk, vs, list_start, OP_AND); } break; | 192 | case TOKEN_AND: { compile_list_binary_op(chunk, vs, start, OP_AND); } break; |
159 | case TOKEN_OR: { compile_list_binary_op(chunk, vs, list_start, OP_OR); } break; | 193 | case TOKEN_OR: { compile_list_binary_op(chunk, vs, start, OP_OR); } break; |
160 | case TOKEN_EQUAL: { compile_list_binary_op(chunk, vs, list_start, OP_EQUAL); } break; | 194 | case TOKEN_EQUAL: { compile_list_binary_op(chunk, vs, start, OP_EQUAL); } break; |
161 | case TOKEN_LESS: { compile_list_binary_op(chunk, vs, list_start, OP_LESS); } break; | 195 | case TOKEN_LESS: { compile_list_binary_op(chunk, vs, start, OP_LESS); } break; |
162 | case TOKEN_GREATER: { compile_list_binary_op(chunk, vs, list_start, OP_GREATER); } break; | 196 | case TOKEN_GREATER: { compile_list_binary_op(chunk, vs, start, OP_GREATER); } break; |
163 | case TOKEN_LESS_EQUAL: { compile_list_binary_op(chunk, vs, list_start, OP_LESS_EQUAL); } break; | 197 | case TOKEN_LESS_EQUAL: { compile_list_binary_op(chunk, vs, start, OP_LESS_EQUAL); } break; |
164 | case TOKEN_GREATER_EQUAL: { compile_list_binary_op(chunk, vs, list_start, OP_GREATER_EQUAL); } break; | 198 | case TOKEN_GREATER_EQUAL: { compile_list_binary_op(chunk, vs, start, OP_GREATER_EQUAL); } break; |
199 | case TOKEN_PRINT: { compile_list_unary_op(chunk, vs, start, OP_PRINT); } break; | ||
200 | case TOKEN_DISPLAY: { compile_list_unary_op(chunk, vs, start, OP_DISPLAY); } break; | ||
201 | case TOKEN_NEWLINE: { compile_list_simple_op(chunk, vs, start, OP_NEWLINE); } break; | ||
165 | default: { | 202 | default: { |
166 | error_push((Error){ | 203 | error_push((Error){ |
167 | .type = ERR_TYPE_COMPILER, | 204 | .type = ERR_TYPE_COMPILER, |