summaryrefslogtreecommitdiff
path: root/com.c
diff options
context:
space:
mode:
authorubq323 <ubq323@ubq323.website>2024-06-24 16:51:32 +0100
committerubq323 <ubq323@ubq323.website>2024-06-24 16:51:32 +0100
commitb2c8538a45aeb154d7cb8ced6ea6a8e0eafb0814 (patch)
tree7b1cd4351f210ec62dcdf2e918be59fbb77b6d71 /com.c
parent08d7465f5a841366ec4c66f11475ede4e9082a8d (diff)
chunk_wbc -> compile_opcode
Diffstat (limited to 'com.c')
-rw-r--r--com.c58
1 files changed, 29 insertions, 29 deletions
diff --git a/com.c b/com.c
index 72975ed..df4ea9c 100644
--- a/com.c
+++ b/com.c
@@ -13,8 +13,8 @@
static size_t placeholder(Compiler *C) {
size_t old_ix = BYTECODE(C).len;
- chunk_wbc(C, 0x00);
- chunk_wbc(C, 0x00);
+ compile_opcode(C, 0x00);
+ compile_opcode(C, 0x00);
return old_ix;
}
@@ -36,7 +36,7 @@ Compiler compiler_new(Compiler *outer, Chunk *ch) {
}
-size_t chunk_wbc(Compiler *C, uint8_t byte) {
+size_t compile_opcode(Compiler *C, uint8_t byte) {
Chunk *ch = C->ch;
if (ch->bc.len == ch->bc.cap) {
size_t newsz = (ch->bc.cap == 0 ? 8 : ch->bc.cap * 2);
@@ -82,7 +82,7 @@ typedef struct {
void single_form(Compiler *C, AstVec l, Op op) {
compile_node(C, l.vals[1]);
- chunk_wbc(C, op);
+ compile_opcode(C, op);
}
void set_form(Compiler *C, AstVec l, Op _) {
@@ -90,14 +90,14 @@ void set_form(Compiler *C, AstVec l, Op _) {
CHECK(ident.ty == AST_IDENT, "set's first argument must be identifier");
ObjString *o = objstring_copy_cstr(C->S, ident.as.str);
compile_node(C, l.vals[2]);
- chunk_wbc(C, OP_SETGLOBAL);
- chunk_wbc(C, chunk_wconst(C, VAL_OBJ(o)));
+ compile_opcode(C, OP_SETGLOBAL);
+ compile_opcode(C, chunk_wconst(C, VAL_OBJ(o)));
}
void do_form(Compiler *C, AstVec l, Op _) {
for (int i = 1; i < l.len - 1; i++) {
compile_node(C, l.vals[i]);
- chunk_wbc(C, OP_DROP);
+ compile_opcode(C, OP_DROP);
}
compile_node(C, l.vals[l.len - 1]);
}
@@ -111,10 +111,10 @@ void if_form(Compiler *C, AstVec l, Op _) {
// A: if-false
// B:
compile_node(C, l.vals[1]);
- chunk_wbc(C, OP_0BRANCH);
+ compile_opcode(C, OP_0BRANCH);
size_t ph_a = placeholder(C);
compile_node(C, l.vals[2]);
- chunk_wbc(C, OP_SKIP);
+ compile_opcode(C, OP_SKIP);
size_t ph_b = placeholder(C);
size_t dest_a = BYTECODE(C).len;
compile_node(C, l.vals[3]);
@@ -135,16 +135,16 @@ void while_form(Compiler *C, AstVec l, Op _) {
// nil (while loop always returns nil)
size_t dest_a = BYTECODE(C).len;
compile_node(C, l.vals[1]);
- chunk_wbc(C, OP_0BRANCH);
+ compile_opcode(C, OP_0BRANCH);
size_t ph_b = placeholder(C);
for (int i = 2; i < l.len; i++) {
compile_node(C, l.vals[i]);
- chunk_wbc(C, OP_DROP);
+ compile_opcode(C, OP_DROP);
}
- chunk_wbc(C, OP_REDO);
+ compile_opcode(C, OP_REDO);
size_t ph_a = placeholder(C);
size_t dest_b = BYTECODE(C).len;
- chunk_wbc(C, OP_NIL);
+ compile_opcode(C, OP_NIL);
patch(C, ph_a, ph_a - dest_a + 2);
patch(C, ph_b, dest_b - ph_b - 2);
@@ -153,7 +153,7 @@ void while_form(Compiler *C, AstVec l, Op _) {
void arith_form(Compiler *C, AstVec l, Op op) {
compile_node(C, l.vals[1]);
compile_node(C, l.vals[2]);
- chunk_wbc(C, op);
+ compile_opcode(C, op);
}
void fn_form(Compiler *C, AstVec l, Op op) {
@@ -168,13 +168,13 @@ void fn_form(Compiler *C, AstVec l, Op op) {
for (int i = 2; i < l.len - 1; i++) {
compile_node(&subcompiler, l.vals[i]);
- chunk_wbc(&subcompiler, OP_DROP);
+ compile_opcode(&subcompiler, OP_DROP);
}
compile_node(&subcompiler, l.vals[l.len-1]);
- chunk_wbc(&subcompiler, OP_RET);
+ compile_opcode(&subcompiler, OP_RET);
- chunk_wbc(C, OP_LOADK);
- chunk_wbc(C, chunk_wconst(C, VAL_OBJ(func)));
+ compile_opcode(C, OP_LOADK);
+ compile_opcode(C, chunk_wconst(C, VAL_OBJ(func)));
}
@@ -218,7 +218,7 @@ static void compile_node(Compiler *C, AstNode a) {
bool found_builtin = false;
for (BuiltinIdent *b = builtin_idents; b->name != NULL; b++) {
if (0 == strcmp(b->name, ident)) {
- chunk_wbc(C, b->op);
+ compile_opcode(C, b->op);
found_builtin = true;
break;
}
@@ -226,18 +226,18 @@ static void compile_node(Compiler *C, AstNode a) {
if (!found_builtin) {
// read global variable
ObjString *o = objstring_copy_cstr(C->S, a.as.str);
- chunk_wbc(C, OP_GETGLOBAL);
- chunk_wbc(C, chunk_wconst(C, VAL_OBJ(o)));
+ compile_opcode(C, OP_GETGLOBAL);
+ compile_opcode(C, chunk_wconst(C, VAL_OBJ(o)));
}
break;
case AST_NUM:
- chunk_wbc(C, OP_LOADK);
- chunk_wbc(C, chunk_wconst(C, VAL_NUM(a.as.num)));
+ compile_opcode(C, OP_LOADK);
+ compile_opcode(C, chunk_wconst(C, VAL_NUM(a.as.num)));
break;
case AST_STRING: {
ObjString *o = objstring_copy_cstr(C->S, a.as.str);
- chunk_wbc(C, OP_LOADK);
- chunk_wbc(C, chunk_wconst(C, VAL_OBJ(o)));
+ compile_opcode(C, OP_LOADK);
+ compile_opcode(C, chunk_wconst(C, VAL_OBJ(o)));
break;
}
case AST_LIST: {
@@ -277,8 +277,8 @@ static void compile_node(Compiler *C, AstNode a) {
for (int i = 0; i < l.len; i++) {
compile_node(C, l.vals[i]);
}
- chunk_wbc(C, OP_CALL);
- chunk_wbc(C, l.len);
+ compile_opcode(C, OP_CALL);
+ compile_opcode(C, l.len);
}
break;
@@ -319,8 +319,8 @@ int main(int argc, char **argv) {
compile_node(C, an);
- chunk_wbc(C, OP_PUTS);
- chunk_wbc(C, OP_HALT);
+ compile_opcode(C, OP_PUTS);
+ compile_opcode(C, OP_HALT);
Thread th = thread_new(S);
th.ch = &ch;