commit eeb5d0dfd38350cd294c1448b6443eaf1fc20eb6 parent c12bff5f13ecdcc73bd4048a6ee129f9bcec28c8 Author: Paul Longtine <paullongtine@gmail.com> Date: Fri Mar 11 08:09:22 2016 Fixed small bug, got it to run fibb! Diffstat: doc/SPECIFICATION | 2 +- src/vm/inc/helper.h | 6 +- src/vm/inc/ins_def.h | 8 +- src/vm/inc/ins_mdata.h | 14 ++-- src/vm/inc/var.h | 11 ++- src/vm/inc/var_ops.h | 12 ++++- src/vm/src/bc.c | 2 +- src/vm/src/ins_def.c | 59 ++++++++++++++---- src/vm/src/ns.c | 26 ++++---- src/vm/src/pc.c | 18 ++--- src/vm/src/proc.c | 18 ++--- src/vm/src/rt.c | 14 ++-- src/vm/src/var.c | 62 +++++++++--------- src/vm/src/var_ops.c | 151 +++++++++++++++++++++++++++++++++++++++++++-- src/vm/tests/bytecodes/fibb | Bin 0 -> 87 bytes 15 files changed, 311 insertions(+), 92 deletions(-)
diff --git a/doc/SPECIFICATION b/doc/SPECIFICATION @@ -168,6 +168,8 @@ to TOS 51 LTHAN - Less than 52 EQ - Equal to 53 NOT - Inverts TOS if TOS is boolean +54 OR - Boolean OR +55 AND - Boolean AND ------------------------------------------------------------------------------- 6 - Loops ------------------------------------------------------------------------------- diff --git a/src/vm/inc/helper.h b/src/vm/inc/helper.h @@ -20,16 +20,18 @@ exit(1); \ } -#define M_ASSERT(x) \ +#define M_ASSERT(x) \ if (x == NULL) \ { \ fprintf(stderr, "Could not allocate memory\n");\ + exit(1); \ } -#define N_ASSERT(x) \ +#define N_ASSERT(x, id) \ if (x == NULL) \ { \ fprintf(stderr, "Null Exception\n");\ + fprintf(stderr, id); \ exit(1); \ } \ diff --git a/src/vm/inc/ins_def.h b/src/vm/inc/ins_def.h @@ -62,14 +62,16 @@ void _ins_def_ISIN (rt_t*, bc_cont*); void _ins_def_ICOS (rt_t*, bc_cont*); void _ins_def_ITAN (rt_t*, bc_cont*); void _ins_def_MOD (rt_t*, bc_cont*); -void _ins_def_OR (rt_t*, bc_cont*); -void _ins_def_XOR (rt_t*, bc_cont*); -void _ins_def_NAND (rt_t*, bc_cont*); +void _ins_def_BOR (rt_t*, bc_cont*); +void _ins_def_BXOR (rt_t*, bc_cont*); +void _ins_def_BNAND (rt_t*, bc_cont*); void _ins_def_GTHAN (rt_t*, bc_cont*); void _ins_def_LTHAN (rt_t*, bc_cont*); void _ins_def_EQ (rt_t*, bc_cont*); void _ins_def_NOT (rt_t*, bc_cont*); +void _ins_def_OR (rt_t*, bc_cont*); +void _ins_def_AND (rt_t*, bc_cont*); void _ins_def_STARTL (rt_t*, bc_cont*); void _ins_def_CLOOP (rt_t*, bc_cont*); diff --git a/src/vm/inc/ins_mdata.h b/src/vm/inc/ins_mdata.h @@ -36,7 +36,7 @@ /* DEC */ INS_MDATA[0x20] = encode(3, A_BYTE, A_BYTE, A_NAME); \ /* LOV */ INS_MDATA[0x21] = encode(2, A_BYTE, A_NAME, A_BYTE); \ /* STV */ INS_MDATA[0x22] = encode(2, A_BYTE, A_NAME, A_BYTE); \ -/* LOC */ INS_MDATA[0x23] = encode(3, A_BYTE, A_NAME, A_DYNC); \ +/* CTV */ INS_MDATA[0x23] = encode(3, A_BYTE, A_NAME, A_DYNC); \ /* CTS */ INS_MDATA[0x24] = encode(1, A_DYNC, A_BYTE, A_BYTE); \ \ /* TYPEOF */ INS_MDATA[0x30] = encode(0, A_BYTE, A_BYTE, A_BYTE); \ @@ -55,14 +55,16 @@ /* ICOS */ INS_MDATA[0x4A] = encode(0, A_BYTE, A_BYTE, A_BYTE); \ /* ITAN */ INS_MDATA[0x4B] = encode(0, A_BYTE, A_BYTE, A_BYTE); \ /* MOD */ INS_MDATA[0x4C] = encode(0, A_BYTE, A_BYTE, A_BYTE); \ -/* OR */ INS_MDATA[0x4D] = encode(0, A_BYTE, A_BYTE, A_BYTE); \ -/* XOR */ INS_MDATA[0x4E] = encode(0, A_BYTE, A_BYTE, A_BYTE); \ -/* NAND */ INS_MDATA[0x4F] = encode(0, A_BYTE, A_BYTE, A_BYTE); \ +/* BOR */ INS_MDATA[0x4D] = encode(0, A_BYTE, A_BYTE, A_BYTE); \ +/* BXOR */ INS_MDATA[0x4E] = encode(0, A_BYTE, A_BYTE, A_BYTE); \ +/* BNAND */ INS_MDATA[0x4F] = encode(0, A_BYTE, A_BYTE, A_BYTE); \ \ /* GTHAN */ INS_MDATA[0x50] = encode(0, A_BYTE, A_BYTE, A_BYTE); \ /* LTHAN */ INS_MDATA[0x51] = encode(0, A_BYTE, A_BYTE, A_BYTE); \ /* EQ */ INS_MDATA[0x52] = encode(0, A_BYTE, A_BYTE, A_BYTE); \ /* NOT */ INS_MDATA[0x53] = encode(0, A_BYTE, A_BYTE, A_BYTE); \ +/* OR */ INS_MDATA[0x54] = encode(0, A_BYTE, A_BYTE, A_BYTE); \ +/* AND */ INS_MDATA[0x55] = encode(0, A_BYTE, A_BYTE, A_BYTE); \ \ /* STARTL */ INS_MDATA[0x60] = encode(0, A_BYTE, A_BYTE, A_BYTE); \ /* CLOOP */ INS_MDATA[0x61] = encode(0, A_BYTE, A_BYTE, A_BYTE); \ @@ -111,7 +113,7 @@ /* DEC */ INS_ADATA[0x20] = encode(3, BTOI, BTOI, BTOI); \ /* LOV */ INS_ADATA[0x21] = encode(2, BTOI, BTOI, BTOI); \ /* STV */ INS_ADATA[0x22] = encode(2, BTOI, BTOI, BTOI); \ -/* CTV */ INS_ADATA[0x23] = encode(3, BTOI, BTOI, BTOI); \ +/* CTV */ INS_ADATA[0x23] = encode(3, BTOI, BTOI, DTOV); \ /* CTS */ INS_ADATA[0x24] = encode(1, DTOV, BTOI, BTOI); \ \ /* TYPEOF */ INS_ADATA[0x30] = encode(0, BTOI, BTOI, BTOI); \ @@ -138,6 +140,8 @@ /* LTHAN */ INS_ADATA[0x51] = encode(0, BTOI, BTOI, BTOI); \ /* EQ */ INS_ADATA[0x52] = encode(0, BTOI, BTOI, BTOI); \ /* NOT */ INS_ADATA[0x53] = encode(0, BTOI, BTOI, BTOI); \ +/* OR */ INS_ADATA[0x54] = encode(0, BTOI, BTOI, BTOI); \ +/* AND */ INS_ADATA[0x55] = encode(0, BTOI, BTOI, BTOI); \ \ /* STARTL */ INS_ADATA[0x60] = encode(0, BTOI, BTOI, BTOI); \ /* CLOOP */ INS_ADATA[0x61] = encode(0, BTOI, BTOI, BTOI); \ diff --git a/src/vm/inc/var.h b/src/vm/inc/var.h @@ -119,9 +119,10 @@ var_cont* var_data_cpy(var_cont*); /* Lovely little function to take bytes and turn it into an integer. * int - sizeof(bytes) + * int - offset * byte_t* - array of bytes */ -var_cont* raw_to_int(int, byte_t*); +var_cont* raw_to_int(int, int, byte_t*); /* Byte to b_type. * byte_t - value maps to enum b_type @@ -134,6 +135,14 @@ var_cont* byte_to_type(byte_t); * byte_t* - array of bytes */ var_cont* raw_to_plist(int, byte_t*); + +/* Converts raw (ascii) string into normal string type + * int - sizeof(bytes) + * int - offset + * byte_t* - array of bytes + */ +var_cont* raw_to_str(int, int, byte_t*); + /* Raw variable to var_cont * * int - sizeof(bytes) diff --git a/src/vm/inc/var_ops.h b/src/vm/inc/var_ops.h @@ -26,4 +26,16 @@ var_cont* var_div_float(var_cont*, var_cont*); var_cont* var_div_int(var_cont*, var_cont*); var_cont* var_div(var_cont*, var_cont*); +var_cont* var_gthan_float(var_cont*, var_cont*); +var_cont* var_gthan_int(var_cont*, var_cont*); +var_cont* var_gthan(var_cont*, var_cont*); + +var_cont* var_lthan_float(var_cont*, var_cont*); +var_cont* var_lthan_int(var_cont*, var_cont*); +var_cont* var_lthan(var_cont*, var_cont*); + +var_cont* var_eq_float(var_cont*, var_cont*); +var_cont* var_eq_int(var_cont*, var_cont*); +var_cont* var_eq(var_cont*, var_cont*); + #endif //VAR_OPS_H diff --git a/src/vm/src/bc.c b/src/vm/src/bc.c @@ -144,7 +144,7 @@ void process_args(bc_cont* ins) { if (arg_types[x] == BTOI) { - ins->varg[x] = raw_to_int(ins->sarg[x], ins->args[x]); + ins->varg[x] = raw_to_int(ins->sarg[x], 0, ins->args[x]); } else if (arg_types[x] == BTOT) { diff --git a/src/vm/src/ins_def.c b/src/vm/src/ins_def.c @@ -50,14 +50,16 @@ void init_ins_def( void ) INS_DEF[0x4A] = _ins_def_ICOS; INS_DEF[0x4B] = _ins_def_ITAN; INS_DEF[0x4C] = _ins_def_MOD; - INS_DEF[0x4D] = _ins_def_OR; - INS_DEF[0x4E] = _ins_def_XOR; - INS_DEF[0x4F] = _ins_def_NAND; + INS_DEF[0x4D] = _ins_def_BOR; + INS_DEF[0x4E] = _ins_def_BXOR; + INS_DEF[0x4F] = _ins_def_BNAND; INS_DEF[0x50] = _ins_def_GTHAN; INS_DEF[0x51] = _ins_def_LTHAN; INS_DEF[0x52] = _ins_def_EQ; INS_DEF[0x53] = _ins_def_NOT; + INS_DEF[0x54] = _ins_def_OR; + INS_DEF[0x55] = _ins_def_AND; INS_DEF[0x60] = _ins_def_STARTL; INS_DEF[0x61] = _ins_def_CLOOP; @@ -98,12 +100,22 @@ void _ins_def_NULL (rt_t* ctx, bc_cont* line) } void _ins_def_PRINT (rt_t* ctx, bc_cont* line) { - var_cont* var = stk_at(ctx->stack, 0); + var_cont* var = stk_pop(&ctx->stack); if (var->type == G_STR) { char* str = var_data_get_G_STR(var); printf("%s\n", str); + } else + if (var->type == G_INT) + { + int val = var_data_get_G_INT(var); + printf("%i\n", val); + } else + if (var->type == G_FLOAT) + { + double val = var_data_get_G_FLOAT(var); + printf("%f\n", val); } pc_inc(ctx->pc, 1); @@ -304,25 +316,39 @@ void _ins_def_MOD (rt_t* ctx, bc_cont* line) { pc_inc(ctx->pc, 1); } -void _ins_def_OR (rt_t* ctx, bc_cont* line) +void _ins_def_BOR (rt_t* ctx, bc_cont* line) { pc_inc(ctx->pc, 1); } -void _ins_def_XOR (rt_t* ctx, bc_cont* line) +void _ins_def_BXOR (rt_t* ctx, bc_cont* line) { pc_inc(ctx->pc, 1); } -void _ins_def_NAND (rt_t* ctx, bc_cont* line) +void _ins_def_BNAND (rt_t* ctx, bc_cont* line) { pc_inc(ctx->pc, 1); } void _ins_def_GTHAN (rt_t* ctx, bc_cont* line) { + var_cont* A = stk_pop(&ctx->stack); + var_cont* B = stk_pop(&ctx->stack); + + var_cont* C = var_gthan(A, B); + + stk_push(&ctx->stack, C); + pc_inc(ctx->pc, 1); } void _ins_def_LTHAN (rt_t* ctx, bc_cont* line) { + var_cont* A = stk_pop(&ctx->stack); + var_cont* B = stk_pop(&ctx->stack); + + var_cont* C = var_lthan(A, B); + + stk_push(&ctx->stack, C); + pc_inc(ctx->pc, 1); } void _ins_def_EQ (rt_t* ctx, bc_cont* line) @@ -330,6 +356,9 @@ void _ins_def_EQ (rt_t* ctx, bc_cont* line) var_cont* A = stk_pop(&ctx->stack); var_cont* B = stk_pop(&ctx->stack); + var_cont* C = var_eq(A, B); + + stk_push(&ctx->stack, C); pc_inc(ctx->pc, 1); } @@ -337,6 +366,14 @@ void _ins_def_NOT (rt_t* ctx, bc_cont* line) { pc_inc(ctx->pc, 1); } +void _ins_def_OR (rt_t* ctx, bc_cont* line) +{ + pc_inc(ctx->pc, 1); +} +void _ins_def_AND (rt_t* ctx, bc_cont* line) +{ + pc_inc(ctx->pc, 1); +} void _ins_def_STARTL (rt_t* ctx, bc_cont* line) { @@ -349,7 +386,7 @@ void _ins_def_CLOOP (rt_t* ctx, bc_cont* line) int value = var_data_get_G_INT(var); - if (value <= 0) + if (value < 1) { pc_return(ctx->pc); while (pc_safe(ctx->pc)) @@ -362,11 +399,13 @@ void _ins_def_CLOOP (rt_t* ctx, bc_cont* line) break; } } + } else + { + pc_inc(ctx->pc, 1); } } void _ins_def_BREAK (rt_t* ctx, bc_cont* line) { - pc_inc(ctx->pc, 1); } void _ins_def_ENDL (rt_t* ctx, bc_cont* line) @@ -388,7 +427,7 @@ void _ins_def_IFDO (rt_t* ctx, bc_cont* line) int value = var_data_get_G_INT(var); - if (value <= 0) + if (value < 1) { while (pc_safe(ctx->pc)) { diff --git a/src/vm/src/ns.c b/src/vm/src/ns.c @@ -45,8 +45,8 @@ ns_t* ns_init(ns_addr size) */ void ns_cont_del(ns_cont* container) { - N_ASSERT(container); - N_ASSERT(container->names); + N_ASSERT(container, "ns_cont_del\n"); + N_ASSERT(container->names, "ns_cont_del\n"); for (int i = 0; i < container->size; i++) { @@ -63,7 +63,7 @@ void ns_cont_del(ns_cont* container) */ void ns_del(ns_t* ns) { - N_ASSERT(ns); + N_ASSERT(ns, "ns_del\n"); if (ns->last != NULL) while (ns->last->next != NULL) @@ -78,7 +78,7 @@ void ns_del(ns_t* ns) */ void ns_push(ns_t* ns, ns_addr size) { - N_ASSERT(ns); + N_ASSERT(ns, "ns_push\n"); ns_cont* new = ns_cont_init(size); @@ -98,7 +98,7 @@ void ns_push(ns_t* ns, ns_addr size) */ void ns_pop(ns_t* ns) { - N_ASSERT(ns); + N_ASSERT(ns, "ns_pop\n"); if (ns->last->next != NULL) { ns_cont* newlast = ns->last->next; @@ -121,7 +121,7 @@ void ns_pop(ns_t* ns) */ void ns_dec(ns_t* ns, b_type type, int scope, ns_addr address) { - N_ASSERT(ns); + N_ASSERT(ns, "ns_dec\n"); ns_cont* scoped_ns = scope ? ns->root : ns->last; @@ -136,7 +136,7 @@ void ns_dec(ns_t* ns, b_type type, int scope, ns_addr address) void ns_cont_dec(ns_cont* ns, b_type type, ns_addr address) { - N_ASSERT(ns); + N_ASSERT(ns, "ns_cont_dec\n"); SIZE_ASSERT( ns->size > address ); @@ -155,8 +155,8 @@ void ns_cont_dec(ns_cont* ns, b_type type, ns_addr address) */ void ns_set(ns_t* ns, int scope, ns_addr address, var_cont* var) { - N_ASSERT(ns); - N_ASSERT(var); + N_ASSERT(ns, "ns_set\n"); + N_ASSERT(var, "ns_set\n"); ns_cont* scoped_ns = scope ? ns->root : ns->last; @@ -170,8 +170,8 @@ void ns_set(ns_t* ns, int scope, ns_addr address, var_cont* var) */ void ns_cont_set(ns_cont* ns, var_cont* var, ns_addr address) { - N_ASSERT(ns); - N_ASSERT(var); + N_ASSERT(ns, "ns_cont_set\n"); + N_ASSERT(var, "ns_cont_set\n"); SIZE_ASSERT( ns->size > address ); var_set(ns->names[ address ], var->data, var->type); @@ -189,7 +189,7 @@ void ns_cont_set(ns_cont* ns, var_cont* var, ns_addr address) */ var_cont* ns_get(ns_t* ns, int scope, ns_addr address) { - N_ASSERT(ns); + N_ASSERT(ns, "ns_get\n"); ns_cont* scoped_ns = scope ? ns->root : ns->last; @@ -202,7 +202,7 @@ var_cont* ns_get(ns_t* ns, int scope, ns_addr address) */ var_cont* ns_cont_get(ns_cont* ns, ns_addr address) { - N_ASSERT(ns); + N_ASSERT(ns, "ns_cont_get\n"); SIZE_ASSERT( ns->size > address ); return ns->names[ address ]; diff --git a/src/vm/src/pc.c b/src/vm/src/pc.c @@ -13,7 +13,7 @@ */ pc_t* pc_new(char* fname) { - N_ASSERT(fname); + N_ASSERT(fname, "pc_new\n"); pc_t* pc = (pc_t*)malloc(sizeof(pc_t)); M_ASSERT(pc); @@ -45,9 +45,9 @@ pc_stk* pc_stk_new(ns_addr address) */ void pc_del(pc_t* pc) { - N_ASSERT(pc); - N_ASSERT(pc->stk); - N_ASSERT(pc->root); + N_ASSERT(pc, "pc_del\n"); + N_ASSERT(pc->stk, "pc_del\n"); + N_ASSERT(pc->root, "pc_del\n"); bc_cont_del(pc->root); @@ -58,7 +58,7 @@ void pc_del(pc_t* pc) void pc_stk_del(pc_stk* stk) { - N_ASSERT(stk); + N_ASSERT(stk, "pc_stk_del\n"); if (stk->next != NULL) { @@ -71,7 +71,7 @@ void pc_stk_del(pc_stk* stk) */ void pc_update(pc_t* pc) { - N_ASSERT(pc); + N_ASSERT(pc, "pc_update\n"); pc->line = bc_scan(pc->line, (pc->stk->address - pc->line->real_addr) ); } @@ -79,7 +79,7 @@ void pc_update(pc_t* pc) */ void pc_inc(pc_t* pc, pc_addr addr) { - N_ASSERT(pc); + N_ASSERT(pc, "pc_inc\n"); pc->stk->address = pc->stk->address + addr; } @@ -88,7 +88,7 @@ void pc_inc(pc_t* pc, pc_addr addr) */ void pc_branch(pc_t* pc, pc_addr address) { - N_ASSERT(pc); + N_ASSERT(pc, "pc_branch\n"); pc_stk* new = pc_stk_new(address); @@ -101,7 +101,7 @@ void pc_branch(pc_t* pc, pc_addr address) */ void pc_return(pc_t* pc) { - N_ASSERT(pc); + N_ASSERT(pc, "pc_return\n"); pc_stk* tmp = pc->stk; pc->stk = pc->stk->next; diff --git a/src/vm/src/proc.c b/src/vm/src/proc.c @@ -17,7 +17,7 @@ */ rt_t* proc_init(char* fname) { - N_ASSERT(fname); + N_ASSERT(fname, "proc_init\n"); rt_t* ctx = rt_ctx_new(fname, stk_new()); return ctx; @@ -28,13 +28,13 @@ rt_t* proc_init(char* fname) */ void proc_run(rt_t* ctx) { - N_ASSERT(ctx); + N_ASSERT(ctx, "proc_run\n"); int n; for (n = 0; pc_safe(ctx->pc); pc_update(ctx->pc)) { - //printf("%i - %i: %x\n", n, ctx->pc->stk->address, ctx->pc->line->op); +// printf("%i - %i: %x\n", n, ctx->pc->stk->address, ctx->pc->line->op); INS_DEF[ctx->pc->line->op](ctx, ctx->pc->line); @@ -47,7 +47,7 @@ void proc_run(rt_t* ctx) */ void proc_clean(rt_t* ctx) { - N_ASSERT(ctx); + N_ASSERT(ctx, "proc_clean\n"); rt_ctx_del(ctx); } @@ -59,7 +59,7 @@ void proc_clean(rt_t* ctx) */ var_cont* proc_callfun(rt_t* ctx, var_cont* func) { - N_ASSERT(ctx); + N_ASSERT(ctx, "proc_callfun\n"); return 0; } @@ -73,7 +73,7 @@ var_cont* proc_callfun(rt_t* ctx, var_cont* func) */ void proc_decvar(rt_t* ctx, b_type type, int scope, ns_addr name) { - N_ASSERT(ctx); + N_ASSERT(ctx, "proc_decvar\n"); ns_dec(ctx->vars, type, scope, name); } @@ -87,7 +87,7 @@ void proc_decvar(rt_t* ctx, b_type type, int scope, ns_addr name) */ void proc_setvar(rt_t* ctx, int scope, ns_addr name, var_cont* var) { - N_ASSERT(ctx); + N_ASSERT(ctx, "proc_setvar\n"); ns_set(ctx->vars, scope, name, var); } @@ -98,9 +98,9 @@ void proc_setvar(rt_t* ctx, int scope, ns_addr name, var_cont* var) */ var_cont* proc_getvar(rt_t* ctx, int scope, ns_addr name) { - N_ASSERT(ctx); + N_ASSERT(ctx, "proc_getvar\n"); var_cont* rv; - + rv = ns_get(ctx->vars, scope, name); return rv; diff --git a/src/vm/src/rt.c b/src/vm/src/rt.c @@ -16,8 +16,8 @@ */ rt_t* rt_ctx_new(char* fname, stk_t* args) { - N_ASSERT(fname); - N_ASSERT(args); + N_ASSERT(fname, "rt_ctx_new\n"); + N_ASSERT(args, "rt_ctx_new\n"); rt_t* ctx = (rt_t*)malloc(sizeof(rt_t)); M_ASSERT(ctx); @@ -34,18 +34,18 @@ rt_t* rt_ctx_new(char* fname, stk_t* args) */ void rt_ctx_del(rt_t* ctx) { - N_ASSERT(ctx); + N_ASSERT(ctx, "rt_ctx_del\n"); - N_ASSERT(ctx->stack); + N_ASSERT(ctx->stack, "rt_ctx_del\n"); stk_del(ctx->stack); - N_ASSERT(ctx->argstk); + N_ASSERT(ctx->argstk, "rt_ctx_del\n"); stk_del(ctx->argstk); - N_ASSERT(ctx->vars); + N_ASSERT(ctx->vars, "rt_ctx_del\n"); ns_del(ctx->vars); - N_ASSERT(ctx->pc); + N_ASSERT(ctx->pc, "rt_ctx_del\n"); pc_del(ctx->pc); } diff --git a/src/vm/src/var.c b/src/vm/src/var.c @@ -95,7 +95,7 @@ var_cont* var_new(b_type type) void var_del(var_cont* var) { - N_ASSERT(var); + N_ASSERT(var, "var_del\n"); if (var->data != NULL) var_data_free(var->data, var->type); @@ -105,7 +105,7 @@ void var_del(var_cont* var) void var_data_free(void* data, b_type type) { - N_ASSERT(data); + N_ASSERT(data, "var_data_free\n"); if (type == FUNC) { @@ -161,7 +161,7 @@ void var_set(var_cont* var, void* data, b_type type) b_type var_data_get_TYPE(var_cont* var) { - N_ASSERT(var); + N_ASSERT(var, "var_data_get_TYPE\n"); ASSERT( var->type == TYPE, "TypeError" ); var_data_type* t = var->data; @@ -171,10 +171,10 @@ b_type var_data_get_TYPE(var_cont* var) b_type* var_data_get_PLIST(var_cont* var) { - N_ASSERT(var); + N_ASSERT(var, "var_data_get_PLIST\n"); ASSERT( var->type == PLIST, "TypeError" ); - N_ASSERT(var->data); + N_ASSERT(var->data, "var_data_get_PLIST\n"); var_data_plist* t = var->data; @@ -183,7 +183,7 @@ b_type* var_data_get_PLIST(var_cont* var) var_data_func* var_data_get_FUNC(var_cont* var) { - N_ASSERT(var); + N_ASSERT(var, "var_data_get_FUNC\n"); ASSERT( var->type == FUNC, "TypeError" ); var_data_func* t = var->data; @@ -193,10 +193,10 @@ var_data_func* var_data_get_FUNC(var_cont* var) int var_data_get_G_INT(var_cont* var) { - N_ASSERT(var); + N_ASSERT(var, "var_data_get_G_INT\n"); ASSERT( var->type == G_INT, "TypeError" ); - N_ASSERT(var->data); + N_ASSERT(var->data, "var_data_get_G_INT\n"); var_data_int* t = var->data; @@ -205,10 +205,10 @@ int var_data_get_G_INT(var_cont* var) double var_data_get_G_FLOAT(var_cont* var) { - N_ASSERT(var); + N_ASSERT(var, "var_data_get_G_FLOAT\n"); ASSERT( var->type == G_FLOAT, "TypeError" ); - N_ASSERT(var->data); + N_ASSERT(var->data, "var_data_get_G_FLOAT\n"); var_data_float* t = var->data; @@ -217,10 +217,10 @@ double var_data_get_G_FLOAT(var_cont* var) char var_data_get_G_CHAR(var_cont* var) { - N_ASSERT(var); + N_ASSERT(var, "var_data_get_G_CHAR\n"); ASSERT( var->type == G_CHAR, "TypeError" ); - N_ASSERT(var->data); + N_ASSERT(var->data, "var_data_get_G_CHAR\n"); var_data_char* t = var->data; @@ -229,10 +229,10 @@ char var_data_get_G_CHAR(var_cont* var) char* var_data_get_G_STR(var_cont* var) { - N_ASSERT(var); + N_ASSERT(var, "var_data_get_G_STR\n"); ASSERT( var->type == G_STR, "TypeError" ); - N_ASSERT(var->data); + N_ASSERT(var->data, "var_data_get_G_STR\n"); var_data_str* t = var->data; @@ -280,7 +280,7 @@ void* var_data_cpy_G_STR(var_data_str* data) */ var_cont* var_data_cpy(var_cont* var) { - N_ASSERT(var); + N_ASSERT(var, "var_data_cpy\n"); var_cont* rv = var_new(var->type); @@ -313,15 +313,14 @@ var_cont* var_data_cpy(var_cont* var) * int - sizeof(bytes) * byte_t* - array of bytes */ -var_cont* raw_to_int(int size, byte_t* bytes) +var_cont* raw_to_int(int size, int start, byte_t* bytes) { var_cont* rv = var_new(G_INT); int i, data; - data = 0; - for ( i = 0; i < size; i++) + for ( i = start; i < size; i++) { data = (data << 8 | bytes[i]); } @@ -366,16 +365,24 @@ var_cont* raw_to_plist(int n, byte_t* bytes) return rv; } -var_data_str* raw_to_str(int n, byte_t* bytes) +/* Converts raw (ascii) string into normal string type + * int - sizeof(bytes) + * int - offset + * byte_t* - array of bytes + */ +var_cont* raw_to_str(int n, int offset, byte_t* bytes) { + var_cont* rv = var_new(G_STR); var_data_str* data = var_data_alloc_G_STR(n); int i; - for (i = 0; n > i; i++) + for (i = offset; n > i; i++) { data->v[i] = (char)bytes[i]; } - return data; + var_set(rv, data, G_STR); + + return rv; } /* Raw variable to var_cont @@ -385,27 +392,26 @@ var_data_str* raw_to_str(int n, byte_t* bytes) */ var_cont* raw_to_var(int n, byte_t* bytes) { - N_ASSERT(bytes); + N_ASSERT(bytes, "raw_to_var\n"); b_type type = (b_type)bytes[0]; - var_cont* rv = var_new(type); - - byte_t* data = ++bytes; + var_cont* rv; if (type == G_INT) { - rv->data = raw_to_int(n - 1, data); + rv = raw_to_int(n, 1, bytes); } else if (type == G_STR) { - rv->data = raw_to_str(n - 1, data); + + rv= raw_to_str(n, 1, bytes); } else { printf("Type {%x} is not a seralizeable type\n", type); } - N_ASSERT(rv->data); + N_ASSERT(rv, "raw_to_var"); return rv; } diff --git a/src/vm/src/var_ops.c b/src/vm/src/var_ops.c @@ -48,7 +48,7 @@ var_cont* var_add(var_cont* A, var_cont* B) rv = var_add_float(A, B); } - N_ASSERT(rv); + N_ASSERT(rv, "var_add\n"); return rv; } @@ -95,7 +95,7 @@ var_cont* var_sub(var_cont* A, var_cont* B) rv = var_sub_float(A, B); } - N_ASSERT(rv); + N_ASSERT(rv, "var_sub\n"); return rv; } @@ -142,7 +142,7 @@ var_cont* var_mult(var_cont* A, var_cont* B) rv = var_mult_float(A, B); } - N_ASSERT(rv); + N_ASSERT(rv, "var_mult\n"); return rv; } @@ -189,7 +189,150 @@ var_cont* var_div(var_cont* A, var_cont* B) rv = var_div_float(A, B); } - N_ASSERT(rv); + N_ASSERT(rv, "var_div\n"); return rv; } + +var_cont* var_gthan_float(var_cont* A, var_cont* B) +{ + var_cont* var = var_new(G_INT); + double AV = var_data_get_G_FLOAT(A); + double BV = var_data_get_G_FLOAT(B); + + int S = (AV > BV); + + var_data_int* data = var_data_alloc_G_INT(S); + + var_set(var, data, G_INT); + + return var; +} +var_cont* var_gthan_int(var_cont* A, var_cont* B) +{ + var_cont* var = var_new(G_INT); + int AV = var_data_get_G_INT(A); + int BV = var_data_get_G_INT(B); + + int S = (AV > BV); + + var_data_int* data = var_data_alloc_G_INT(S); + + var_set(var, data, G_INT); + + return var; + +} +var_cont* var_gthan(var_cont* A, var_cont* B) +{ + var_cont* rv; + + ASSERT(( A->type == B->type ), "Inconsistent Types\n"); + if (A->type == G_INT && B->type == G_INT) + { + rv = var_gthan_int(A, B); + } else + if (A->type == G_FLOAT && B->type == G_FLOAT) + { + rv = var_gthan_float(A, B); + } + + N_ASSERT(rv, "var_gthan\n"); + + return rv; +} + +var_cont* var_lthan_float(var_cont* A, var_cont* B) +{ + var_cont* var = var_new(G_INT); + double AV = var_data_get_G_FLOAT(A); + double BV = var_data_get_G_FLOAT(B); + + int S = (AV < BV); + + var_data_int* data = var_data_alloc_G_INT(S); + + var_set(var, data, G_INT); + + return var; +} +var_cont* var_lthan_int(var_cont* A, var_cont* B) +{ + var_cont* var = var_new(G_INT); + int AV = var_data_get_G_INT(A); + int BV = var_data_get_G_INT(B); + + int S = (AV < BV); + + var_data_int* data = var_data_alloc_G_INT(S); + + var_set(var, data, G_INT); + + return var; +} +var_cont* var_lthan(var_cont* A, var_cont* B) +{ + var_cont* rv; + + ASSERT(( A->type == B->type ), "Inconsistent Types\n"); + if (A->type == G_INT && B->type == G_INT) + { + rv = var_lthan_int(A, B); + } else + if (A->type == G_FLOAT && B->type == G_FLOAT) + { + rv = var_lthan_float(A, B); + } + + N_ASSERT(rv, "var_lthan\n"); + + return rv; +} + +var_cont* var_eq_float(var_cont* A, var_cont* B) +{ + var_cont* var = var_new(G_INT); + double AV = var_data_get_G_FLOAT(A); + double BV = var_data_get_G_FLOAT(B); + + int S = (AV == BV); + + var_data_int* data = var_data_alloc_G_INT(S); + + var_set(var, data, G_INT); + + return var; +} +var_cont* var_eq_int(var_cont* A, var_cont* B) +{ + var_cont* var = var_new(G_INT); + int AV = var_data_get_G_INT(A); + int BV = var_data_get_G_INT(B); + + int S = (AV == BV); + + var_data_int* data = var_data_alloc_G_INT(S); + + var_set(var, data, G_INT); + + return var; +} +var_cont* var_eq(var_cont* A, var_cont* B) +{ + var_cont* rv; + + ASSERT(( A->type == B->type ), "Inconsistent Types\n"); + if (A->type == G_INT && B->type == G_INT) + { + rv = var_eq_int(A, B); + } else + if (A->type == G_FLOAT && B->type == G_FLOAT) + { + rv = var_eq_float(A, B); + } + + N_ASSERT(rv, "var_eq\n"); + + return rv; +} + diff --git a/src/vm/tests/bytecodes/fibb b/src/vm/tests/bytecodes/fibb Binary files differ