aboutsummaryrefslogtreecommitdiffstats
path: root/src/bytecode/compiler.h
diff options
context:
space:
mode:
authorBad Diode <bd@badd10de.dev>2021-10-23 18:19:14 +0200
committerBad Diode <bd@badd10de.dev>2021-10-23 18:19:14 +0200
commitb07ece568d8d62ca80a8ba3b43fb46a98e117d5a (patch)
tree1b52b7c3c4d73d154ca565e57cf5e062d4495a2a /src/bytecode/compiler.h
parentc3fe9367986520b08a36bf693e6c74eb309377c5 (diff)
downloadbdl-b07ece568d8d62ca80a8ba3b43fb46a98e117d5a.tar.gz
bdl-b07ece568d8d62ca80a8ba3b43fb46a98e117d5a.zip
Add logic operations
Diffstat (limited to 'src/bytecode/compiler.h')
-rwxr-xr-xsrc/bytecode/compiler.h88
1 files changed, 74 insertions, 14 deletions
diff --git a/src/bytecode/compiler.h b/src/bytecode/compiler.h
index fd5cdbc..6f43416 100755
--- a/src/bytecode/compiler.h
+++ b/src/bytecode/compiler.h
@@ -58,7 +58,7 @@ parse_fixnum(Chunk *chunk, Token tok) {
58void parse_tree(Chunk *chunk, Visitor *vs); 58void parse_tree(Chunk *chunk, Visitor *vs);
59 59
60void 60void
61compile_list_op(Chunk *chunk, Visitor *vs, Token list_start, Ops op) { 61compile_list_binary_op(Chunk *chunk, Visitor *vs, Token list_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);
@@ -73,7 +73,15 @@ compile_list_op(Chunk *chunk, Visitor *vs, Token list_start, Ops op) {
73 } 73 }
74 if (tok.type == TOKEN_RPAREN) { 74 if (tok.type == TOKEN_RPAREN) {
75 next_token(vs); 75 next_token(vs);
76 break; 76 if (n <= 1) {
77 error_push((Error){
78 .type = ERR_TYPE_COMPILER,
79 .value = ERR_NOT_ENOUGH_ARGS,
80 .line = list_start.line,
81 .col = list_start.column,
82 });
83 }
84 return;
77 } 85 }
78 parse_tree(chunk, vs); 86 parse_tree(chunk, vs);
79 n++; 87 n++;
@@ -81,14 +89,58 @@ compile_list_op(Chunk *chunk, Visitor *vs, Token list_start, Ops op) {
81 add_code(chunk, op, list_start.line, list_start.column); 89 add_code(chunk, op, list_start.line, list_start.column);
82 } 90 }
83 } 91 }
84 if (n == 0) { 92 error_push((Error){
85 error_push((Error){ 93 .type = ERR_TYPE_COMPILER,
86 .type = ERR_TYPE_COMPILER, 94 .value = ERR_NOT_ENOUGH_ARGS,
87 .value = ERR_NOT_ENOUGH_ARGS, 95 .line = list_start.line,
88 .line = list_start.line, 96 .col = list_start.column,
89 .col = list_start.column, 97 });
90 }); 98}
99
100void
101compile_list_unary_op(Chunk *chunk, Visitor *vs, Token list_start, Ops op) {
102 size_t n = 0;
103 while (has_next_token(vs)) {
104 Token tok = peek_token(vs);
105 if (tok.type == TOKEN_EOF) {
106 error_push((Error){
107 .type = ERR_TYPE_COMPILER,
108 .value = ERR_UNBALANCED_PAREN,
109 .line = list_start.line,
110 .col = list_start.column,
111 });
112 return;
113 }
114 if (tok.type == TOKEN_RPAREN) {
115 next_token(vs);
116 if (n == 0) {
117 error_push((Error){
118 .type = ERR_TYPE_COMPILER,
119 .value = ERR_NOT_ENOUGH_ARGS,
120 .line = list_start.line,
121 .col = list_start.column,
122 });
123 }
124 return;
125 }
126 parse_tree(chunk, vs);
127 add_code(chunk, op, list_start.line, list_start.column);
128 n++;
129 if (n > 1) {
130 error_push((Error){
131 .type = ERR_TYPE_COMPILER,
132 .value = ERR_TOO_MANY_ARGS,
133 .line = list_start.line,
134 .col = list_start.column,
135 });
136 }
91 } 137 }
138 error_push((Error){
139 .type = ERR_TYPE_COMPILER,
140 .value = ERR_UNBALANCED_PAREN,
141 .line = list_start.line,
142 .col = list_start.column,
143 });
92} 144}
93 145
94void 146void
@@ -103,11 +155,19 @@ parse_list(Chunk *chunk, Visitor *vs, Token list_start) {
103 } 155 }
104 Token tok = next_token(vs); 156 Token tok = next_token(vs);
105 switch (tok.type) { 157 switch (tok.type) {
106 case TOKEN_ADD: { compile_list_op(chunk, vs, list_start, OP_SUM); } break; 158 case TOKEN_ADD: { compile_list_binary_op(chunk, vs, list_start, OP_SUM); } break;
107 case TOKEN_SUB: { compile_list_op(chunk, vs, list_start, OP_SUB); } break; 159 case TOKEN_SUB: { compile_list_binary_op(chunk, vs, list_start, OP_SUB); } break;
108 case TOKEN_MUL: { compile_list_op(chunk, vs, list_start, OP_MUL); } break; 160 case TOKEN_MUL: { compile_list_binary_op(chunk, vs, list_start, OP_MUL); } break;
109 case TOKEN_DIV: { compile_list_op(chunk, vs, list_start, OP_DIV); } break; 161 case TOKEN_DIV: { compile_list_binary_op(chunk, vs, list_start, OP_DIV); } break;
110 case TOKEN_MOD: { compile_list_op(chunk, vs, list_start, OP_MOD); } break; 162 case TOKEN_MOD: { compile_list_binary_op(chunk, vs, list_start, OP_MOD); } break;
163 case TOKEN_NOT: { compile_list_unary_op(chunk, vs, list_start, OP_NOT); } break;
164 case TOKEN_AND: { compile_list_binary_op(chunk, vs, list_start, OP_AND); } break;
165 case TOKEN_OR: { compile_list_binary_op(chunk, vs, list_start, OP_OR); } break;
166 case TOKEN_EQUAL: { compile_list_binary_op(chunk, vs, list_start, OP_EQUAL); } break;
167 case TOKEN_LESS: { compile_list_binary_op(chunk, vs, list_start, OP_LESS); } break;
168 case TOKEN_GREATER: { compile_list_binary_op(chunk, vs, list_start, OP_GREATER); } break;
169 case TOKEN_LESS_EQUAL: { compile_list_binary_op(chunk, vs, list_start, OP_LESS_EQUAL); } break;
170 case TOKEN_GREATER_EQUAL: { compile_list_binary_op(chunk, vs, list_start, OP_GREATER_EQUAL); } break;
111 default: { 171 default: {
112 error_push((Error){ 172 error_push((Error){
113 .type = ERR_TYPE_COMPILER, 173 .type = ERR_TYPE_COMPILER,