language

Some fools attempt at an interpreted language
Log | Files | Refs

commit a7391e100eb493de0c5d3b513a15430526f2576c
parent 172246477cb11a3811f76e713248dc77ddbe87a8
Author: Paul Longtine <paullongtine@gmail.com>
Date:   Thu Mar  3 11:17:00 2016

A few days of fiddling. Refined , general updates, made  support call stacks, etc

Diffstat:
 doc/SPECIFICATION                      |   2 +-
 src/vm/inc/bc.h                        |  21 +++----
 src/vm/inc/fh.h                        |   4 +-
 src/vm/inc/pc.h                        |  15 ++++-
 src/vm/inc/proc.h                      |   8 +++-
 src/vm/inc/var.h                       |  49 ++++++++--------
 src/vm/src/bc.c                        |  71 +++++++++++++++--------
 src/vm/src/fh.c                        |  21 +-------
 src/vm/src/ins_def.c                   |  15 ++++-
 src/vm/src/is.c                        |   3 +-
 src/vm/src/pc.c                        |  49 ++++++++++++++--
 src/vm/src/proc.c                      |  17 ++++-
 src/vm/src/rt.c                        |   1 +-
 src/vm/src/var.c                       | 107 +++++++++++++---------------------
 src/vm/testing                         | Bin 0 -> 13 bytes
 src/vm/tests/cases/bc/Makefile         |  36 +++++++----
 src/vm/tests/cases/bc/bytecode         | Bin 22 -> 0 bytes
 src/vm/tests/cases/bc/expected_output  |   6 +-
 src/vm/tests/cases/bc/test.c           |  15 +++--
 src/vm/tests/cases/ht/expected_output  |   4 +-
 src/vm/tests/cases/ht/test.c           |  23 ++++++-
 src/vm/tests/cases/pc/bytecode         | Bin 22 -> 0 bytes
 src/vm/tests/cases/pc/expected_output  |   4 +-
 src/vm/tests/cases/pc/test.c           |   3 +-
 src/vm/tests/cases/var/Makefile        |  23 +-------
 src/vm/tests/cases/var/expected_output |   3 +-
 src/vm/tests/cases/var/test.c          |  30 +----------
 27 files changed, 293 insertions(+), 237 deletions(-)

diff --git a/doc/SPECIFICATION b/doc/SPECIFICATION @@ -110,7 +110,7 @@ Keywords: A<[variable]> - Address Arguement. Address width will vary on file size, decided on compile time. (for now, a word) D<[variable]> - Dynamic bytecode arguement. Arguements terminated with NULL - byte. + byte. Two null bytes escapes terminator. ------------------------------------------------------------------------------- 1 - Stack manipulation ------------------------------------------------------------------------------- diff --git a/src/vm/inc/bc.h b/src/vm/inc/bc.h @@ -16,12 +16,13 @@ typedef unsigned short int bc_addr; /* 'Bytecode Container' */ typedef struct bc_cont { + bc_addr real_addr; byte_t op; - byte_t* args[3]; byte_t mdata; - void* targ[3]; byte_t adata; - bc_addr real_addr; + byte_t* args[3]; + void* targ[3]; + int sarg[3]; struct bc_cont* next; struct bc_cont* prev; } bc_cont; @@ -59,18 +60,18 @@ void bc_cont_del(bc_cont*); * function will read arguements into bc_cont. */ void get_args(FILE*, bc_cont*); -byte_t* get_byte_arg(FILE*); -byte_t* get_word_arg(FILE*); -byte_t* get_dync_arg(FILE*); +byte_t* get_byte_arg(FILE*, int*); +byte_t* get_word_arg(FILE*, int*); +byte_t* get_dync_arg(FILE*, int*); /* Process arguements into typed & readable data * bc_cont* - bytecode container */ void process_args(bc_cont*); -void arg_to_int(void*, byte_t*); -void arg_to_addr(void*, byte_t*); -void arg_to_arglist(void*, byte_t*); -void arg_to_var(void*, byte_t*); +void arg_to_int(void**, byte_t*); +void arg_to_addr(void**, byte_t*); +void arg_to_arglist(void**, int, byte_t*); +void arg_to_var(void**, int, byte_t*); /* Scan to +/- int in bytecode chain * bc_cont* - bytecode container [0] diff --git a/src/vm/inc/fh.h b/src/vm/inc/fh.h @@ -11,10 +11,6 @@ typedef unsigned char byte_t; -/* Reads passed file descriptor until NULL, returns array of byte_t - */ -byte_t* read_until_null(FILE*); - /* Reads n bytes, n passed as @param long, returns array of byte_t */ byte_t* read_bytes(FILE*, long); diff --git a/src/vm/inc/pc.h b/src/vm/inc/pc.h @@ -14,10 +14,15 @@ typedef unsigned short int pc_addr; +typedef struct pc_stk { + pc_addr address; + struct pc_stk* next; +} pc_stk; + typedef struct pc_t { bc_cont* root; bc_cont* line; - pc_addr address; + pc_stk* stk; } pc_t; /* Initalizes program counter @@ -25,10 +30,14 @@ typedef struct pc_t { */ pc_t* pc_new(char*); +pc_stk* pc_stk_new(void); + /* Frees memory assosiated with pc_t* instance */ void pc_del(pc_t*); +void pc_stk_del(pc_stk*); + /* Updates program counter on changes */ void pc_update(pc_t*); @@ -41,4 +50,8 @@ void pc_inc(pc_t*, pc_addr); */ void pc_branch(pc_t*, pc_addr); +/* Return from branch + */ +void pc_return(pc_t*); + #endif // PC_H diff --git a/src/vm/inc/proc.h b/src/vm/inc/proc.h @@ -41,6 +41,14 @@ var_cont* proc_callfun(rt_t*, var_cont*); * rt_t* - Runtime context * int - Scope * ns_addr - Name of variable + * + * This function is used to support an interface to multithreaded instances + */ +void proc_decvar(rt_t*, b_type, int, ns_addr); +/* Set a variable subroutine + * rt_t* - Runtime context + * int - Scope + * ns_addr - Name of variable * var_cont* - Variable container * * This function is used to support an interface to multithreaded instances diff --git a/src/vm/inc/var.h b/src/vm/inc/var.h @@ -7,6 +7,8 @@ #include <stdlib.h> #include <stdio.h> +#include "helper.h" + typedef enum { VOID, FUNC, @@ -25,21 +27,29 @@ typedef enum { typedef struct var_cont { b_type type; - void** data; + void* data; } var_cont; -typedef void var_data_void; +typedef struct var_data_void { + void* v; +} var_data_void; -typedef int var_data_int; +typedef struct var_data_int { + int v; +} var_data_int; -typedef double var_data_float; +typedef struct var_data_float { + double v; +} var_data_float; -typedef char var_data_char; +typedef struct var_data_char { + char v; +} var_data_char; /* typedef char var_data_u_char; */ typedef struct var_data_str { - var_data_char* str; + char* v; } var_data_str; /* typedef struct var_data_u_str { @@ -51,30 +61,25 @@ typedef struct var_data_u_str { */ var_cont* var_new(b_type); -void** var_data_alloc(b_type); -void** var_data_alloc_G_INT(var_data_int); -void** var_data_alloc_G_FLOAT(var_data_float); -void** var_data_alloc_G_CHAR(var_data_char); -void** var_data_alloc_G_STR(size_t); +void* var_data_alloc(b_type); +void* var_data_alloc_G_INT(int); +void* var_data_alloc_G_FLOAT(double); +void* var_data_alloc_G_CHAR(char); +void* var_data_alloc_G_STR(size_t); /* Frees variable */ void var_del(var_cont*); -void var_data_free(void**, b_type); +void var_data_free(void*, b_type); /* Sets variable * void** -> pointer to allocated space of memory that agrees with b_type */ -void var_set(var_cont*, void**, b_type); +void var_set(var_cont*, void*, b_type); -/* Casts type to variable - * This function will try to change the passed variable to a different type - */ -void var_cast(var_cont*, b_type); -void** var_cast_data(var_cont*, b_type); -void** var_cast_data_G_INT(void**, b_type); -void** var_cast_data_G_FLOAT(void**, b_type); -void** var_cast_data_G_CHAR(void**, b_type); -void** var_cast_data_G_STR(void**, b_type); +int var_data_get_G_INT(var_cont*); +double var_data_get_G_FLOAT(var_cont*); +char var_data_get_G_CHAR(var_cont*); +char* var_data_get_G_STR(var_cont*); #endif // TYPES_H diff --git a/src/vm/src/bc.c b/src/vm/src/bc.c @@ -23,6 +23,10 @@ bc_cont* bc_cont_new(void) new->targ[1] = NULL; new->targ[2] = NULL; + new->sarg[0] = 0; + new->sarg[1] = 0; + new->sarg[2] = 0; + new->next = NULL; new->prev = NULL; @@ -76,29 +80,44 @@ void get_args(FILE* f, bc_cont* ins) { if (arg_types[x] == A_BYTE) { - ins->args[x] = get_byte_arg(f); + ins->args[x] = get_byte_arg(f, &ins->sarg[x]); } else if (arg_types[x] == A_WORD) { - ins->args[x] = get_word_arg(f); + ins->args[x] = get_word_arg(f, &ins->sarg[x]); } else if (arg_types[x] == A_DYNC) { - ins->args[x] = get_dync_arg(f); + ins->args[x] = get_dync_arg(f, &ins->sarg[x]); } } } -byte_t* get_byte_arg(FILE* f) +byte_t* get_byte_arg(FILE* f, int* size) { + *size = 1; return read_bytes(f, 1); } -byte_t* get_word_arg(FILE* f) +byte_t* get_word_arg(FILE* f, int* size) { + *size = 2; return read_bytes(f, 2); } -byte_t* get_dync_arg(FILE* f) +byte_t* get_dync_arg(FILE* f, int* size) { - return read_until_null(f); + int n = 0; + + byte_t byte = read_byte(f); + + // This bit gets the length in bytes it needs to read into a buffer + while (byte != 0) + { + n = (n << 8 | byte); + byte = read_byte(f); + } + + *size = n; + + return read_bytes(f, n); } /* Given an instruction, convert raw arguement data into typed data @@ -115,49 +134,57 @@ void process_args(bc_cont* ins) { if (arg_types[x] == BTOI) { - arg_to_int(ins->targ[x], ins->args[x]); + arg_to_int(&ins->targ[x], ins->args[x]); } else if (arg_types[x] == WTOA) { - arg_to_addr(ins->targ[x], ins->args[x]); + arg_to_addr(&ins->targ[x], ins->args[x]); } else if (arg_types[x] == DTOL) { - arg_to_arglist(ins->targ[x], ins->args[x]); + arg_to_arglist(&ins->targ[x], ins->sarg[x], ins->args[x]); } else if (arg_types[x] == DTOV) { - arg_to_var(ins->targ[x], ins->args[x]); + arg_to_var(&ins->targ[x], ins->sarg[x], ins->args[x]); } } } -void arg_to_int(void* ptr, byte_t* byte) +void arg_to_int(void** ptr, byte_t* byte) { - ptr = (bc_targ_int*)malloc(sizeof(bc_targ_int)); - M_ASSERT(ptr); + *ptr = (bc_targ_int*)malloc(sizeof(bc_targ_int)); + M_ASSERT(*ptr); - bc_targ_int* v = ptr; + bc_targ_int* v = *ptr; v->i = (int)byte[0]; } -void arg_to_addr(void* ptr, byte_t* word) +void arg_to_addr(void** ptr, byte_t* word) { - ptr = (bc_targ_int*)malloc(sizeof(bc_targ_int)); - M_ASSERT(ptr); + *ptr = (bc_targ_int*)malloc(sizeof(bc_targ_int)); + M_ASSERT(*ptr); - bc_targ_int* v = ptr; + bc_targ_int* v = *ptr; - v->i = (int)((word[1] >> 8) | word[0]); + v->i = (int)(word[0] << 8 | word[1]); } -void arg_to_arglist(void* ptr, byte_t* bytes) +void arg_to_arglist(void** ptr, int n, byte_t* bytes) { + *ptr = (bc_targ_list*)malloc(sizeof(bc_targ_list)); + M_ASSERT(*ptr); + +// bc_targ_list* v = *ptr; } -void arg_to_var(void* ptr, byte_t* bytes) +void arg_to_var(void** ptr, int n, byte_t* bytes) { + *ptr = (bc_targ_var_cont*)malloc(sizeof(bc_targ_list)); + M_ASSERT(*ptr); + +// bc_targ_var_cont* v = *ptr; } /* Scan to +/- int in bytecode chain diff --git a/src/vm/src/fh.c b/src/vm/src/fh.c @@ -4,27 +4,6 @@ #include "fh.h" #include "helper.h" -byte_t* read_until_null(FILE* f) -{ - long f_pos_i = ftell(f); - - // This bit gets the length in bytes it needs to read into a buffer - byte_t byte = read_byte(f); - while (byte != 0) - { - byte = read_byte(f); - } - - long bytes = (ftell(f) - f_pos_i); - - // Seek backwards - fseek(f, -bytes, SEEK_CUR); - - ASSERT(f_pos_i == ftell(f), "Something bad happened.\n"); - - return read_bytes(f, bytes); -} - byte_t* read_bytes(FILE* f, long bytes) { byte_t* buffer = (byte_t*)malloc(bytes*sizeof(byte_t)); diff --git a/src/vm/src/ins_def.c b/src/vm/src/ins_def.c @@ -2,7 +2,13 @@ #include <stdio.h> #include "ins_def.h" + +#include "proc.h" #include "rt.h" +#include "bc.h" +#include "stk.h" +#include "var.h" +#include "pc.h" #include "helper.h" /* Initializes INS_DEF with pointers to each instructions function @@ -294,7 +300,11 @@ void _ins_def_DECLASS (rt_t* ctx, bc_cont* line) } void _ins_def_DEFUN (rt_t* ctx, bc_cont* line) { - //ns_dec(ctx->vars, FUNC, 0, arg_to_int(0, 0, line)); + bc_targ_int* name = line->targ[0]; + bc_targ_int* type = line->targ[1]; + bc_targ_list* args = line->targ[2]; + + proc_decvar(ctx, FUNC, 1, name->i); int nsize; @@ -311,5 +321,8 @@ void _ins_def_DEFUN (rt_t* ctx, bc_cont* line) } } + + printf("Namespace size for thingy: %i\n", nsize); + printf("Name: %i\n", name->i); } diff --git a/src/vm/src/is.c b/src/vm/src/is.c @@ -1,8 +1,9 @@ #include <stdlib.h> +#include "bc.h" #include "is.h" + #include "ins_mdata.h" -#include "bc.h" #include "fh.h" #include "ins_mdata.h" diff --git a/src/vm/src/pc.c b/src/vm/src/pc.c @@ -15,42 +15,81 @@ pc_t* pc_new(char* fname) pc_t* pc = (pc_t*)malloc(sizeof(pc_t)); M_ASSERT(pc); + pc->stk = pc_stk_new(); + pc->root = bc_read(fname); - N_ASSERT(pc->root); pc->line = pc->root; - pc->address = pc->root->real_addr; + pc->stk->address = pc->root->real_addr; return pc; } +pc_stk* pc_stk_new() +{ + pc_stk* new = (pc_stk*)malloc(sizeof(pc_stk)); + M_ASSERT(new); + + new->next = NULL; + + return new; +} + void pc_del(pc_t* pc) { N_ASSERT(pc); + N_ASSERT(pc->stk); N_ASSERT(pc->root); bc_cont_del(pc->root); + pc_stk_del(pc->stk); + free(pc); } +void pc_stk_del(pc_stk* stk) +{ + N_ASSERT(stk); + + if (stk->next != NULL) + { + pc_stk_del(stk->next); + } + + free(stk); +} + void pc_update(pc_t* pc) { N_ASSERT(pc); - pc->line = bc_scan(pc->line, pc->address - pc->line->real_addr); + pc->line = bc_scan(pc->line, (pc->stk->address - pc->line->real_addr) ); } void pc_inc(pc_t* pc, pc_addr addr) { N_ASSERT(pc); - pc->address = pc->address + addr; + pc->stk->address = pc->stk->address + addr; } void pc_branch(pc_t* pc, pc_addr addr) { N_ASSERT(pc); - pc->address = addr; + pc_stk* new = pc_stk_new(); + + new->next = pc->stk; + + pc->stk = new; +} + +void pc_return(pc_t* pc) +{ + N_ASSERT(pc); + + pc_stk* tmp = pc->stk; + pc->stk = pc->stk->next; + free(tmp); } diff --git a/src/vm/src/proc.c b/src/vm/src/proc.c @@ -34,7 +34,7 @@ void proc_run(rt_t* ctx) for (n = 0; ctx->pc->line->next != NULL; pc_update(ctx->pc)) { - printf("%i: %x\n", n, 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); @@ -65,6 +65,19 @@ var_cont* proc_callfun(rt_t* ctx, var_cont* func) /* Set a variable subroutine * rt_t* - Runtime context + * b_type - Type + * int - Scope + * ns_addr - Name of variable + * + * This function is used to support an interface to multithreaded instances + */ +void proc_decvar(rt_t* ctx, b_type type, int scope, ns_addr name) +{ + ns_dec(ctx->vars, type, scope, name); +} + +/* Set a variable subroutine + * rt_t* - Runtime context * int - Scope * ns_addr - Name of variable * var_cont* - Variable container @@ -73,7 +86,7 @@ var_cont* proc_callfun(rt_t* ctx, var_cont* func) */ void proc_setvar(rt_t* ctx, int scope, ns_addr name, var_cont* var) { - + ns_set(ctx->vars, scope, name, var); } /* Get a variable subroutine diff --git a/src/vm/src/rt.c b/src/vm/src/rt.c @@ -1,6 +1,7 @@ #include <stdlib.h> #include "rt.h" + #include "bc.h" #include "stk.h" #include "var.h" diff --git a/src/vm/src/var.c b/src/vm/src/var.c @@ -4,49 +4,49 @@ #include "var.h" #include "helper.h" -void** var_data_alloc_G_INT(var_data_int value) +void* var_data_alloc_G_INT(int value) { var_data_int* rv = (var_data_int*)malloc(sizeof(var_data_int)); M_ASSERT(rv); - *rv = value; + rv->v = value; - return (void**)rv; + return rv; } -void** var_data_alloc_G_FLOAT(var_data_float value) +void* var_data_alloc_G_FLOAT(double value) { var_data_float* rv = (var_data_float*)malloc(sizeof(var_data_float)); M_ASSERT(rv); - *rv = value; + rv->v = value; return (void**)rv; } -void** var_data_alloc_G_CHAR(var_data_char value) +void* var_data_alloc_G_CHAR(char value) { var_data_char* rv = (var_data_char*)malloc(sizeof(var_data_char)); M_ASSERT(rv); - *rv = value; + rv->v = value; - return (void**)rv; + return rv; } -void** var_data_alloc_G_STR(size_t size) +void* var_data_alloc_G_STR(size_t size) { var_data_str* rv = (var_data_str*)malloc(sizeof(var_data_str)); M_ASSERT(rv); - rv->str = (var_data_char*)malloc(sizeof(var_data_char)*size); + rv->v = (char*)malloc(sizeof(char)*size); - return (void**)rv; + return rv; } -void** var_data_alloc(b_type type) +void* var_data_alloc(b_type type) { - void** rv = NULL; + void* rv = NULL; if (type == G_INT) rv = var_data_alloc_G_INT(0); @@ -77,7 +77,7 @@ var_cont* var_new(b_type type) return new; } -void var_data_free(void** data, b_type type) +void var_data_free(void* data, b_type type) { N_ASSERT(data); @@ -92,9 +92,9 @@ void var_data_free(void** data, b_type type) if (type == G_STR) { - var_data_str* t_data = *data; - if (t_data->str != NULL) - free(t_data->str); + var_data_str* d = data; + if (d->v != NULL) + free(d->v); free(data); } @@ -110,7 +110,7 @@ void var_del(var_cont* var) free(var); } -void var_set(var_cont* var, void** data, b_type type) +void var_set(var_cont* var, void* data, b_type type) { if (var == NULL || data == NULL) return; @@ -124,71 +124,50 @@ void var_set(var_cont* var, void** data, b_type type) var->data = data; } -void** var_cast_data_G_INT(void** data, b_type type) +int var_data_get_G_INT(var_cont* var) { - N_ASSERT(data); - - free(data); + N_ASSERT(var); + ASSERT( var->type == G_INT, "TypeError" ); - return var_data_alloc(type); -} + N_ASSERT(var->data); -void** var_cast_data_G_FLOAT(void** data, b_type type) -{ - N_ASSERT(data); + var_data_int* t = var->data; - free(data); - - return var_data_alloc(type); + return t->v; } -void** var_cast_data_G_CHAR(void** data, b_type type) +double var_data_get_G_FLOAT(var_cont* var) { - N_ASSERT(data); - - free(data); - - return var_data_alloc(type); -} + N_ASSERT(var); + ASSERT( var->type == G_FLOAT, "TypeError" ); -void** var_cast_data_G_STR(void** data, b_type type) -{ - N_ASSERT(data); + N_ASSERT(var->data); - free(data); + var_data_float* t = var->data; - return var_data_alloc(type); + return t->v; } -void** var_cast_data(var_cont* var, b_type type) +char var_data_get_G_CHAR(var_cont* var) { - void** rv = NULL; - - if (var->type == G_INT) - rv = var_cast_data_G_INT(var->data, type); - - if (var->type == G_FLOAT) - rv = var_cast_data_G_FLOAT(var->data, type); + N_ASSERT(var); + ASSERT( var->type == G_CHAR, "TypeError" ); - if (var->type == G_CHAR) - rv = var_cast_data_G_CHAR(var->data, type); + N_ASSERT(var->data); - if (var->type == G_STR) - rv = var_cast_data_G_STR(var->data, type); + var_data_char* t = var->data; - return rv; + return t->v; } -void var_cast(var_cont* var, b_type type) +char* var_data_get_G_STR(var_cont* var) { - if (var == NULL) - return; - - if (var->data == NULL) - return; + N_ASSERT(var); + ASSERT( var->type == G_STR, "TypeError" ); - var->data = var_cast_data(var, type); + N_ASSERT(var->data); - var->type = type; -} + var_data_str* t = var->data; + return t->v; +} diff --git a/src/vm/testing b/src/vm/testing Binary files differ diff --git a/src/vm/tests/cases/bc/Makefile b/src/vm/tests/cases/bc/Makefile @@ -4,16 +4,32 @@ INC_DIR = ../../../inc CC = gcc CFLAGS = -std=c99 -Wall -I$(INC_DIR) -DEPS = i$(INC_DIR)/is_mdata.h \ - helper.h \ - fh.h \ - is.h \ - bc.h \ +DEPS = $(INC_DIR)/is_mdata.h \ + helper.h \ + fh.h \ + bc.h \ + is.h \ + var.h \ + stk.h \ + ht.h \ + ns.h \ + pc.h \ + rt.h \ + ins_def.h \ + proc.h -OBJ = test.o \ - $(SRC_DIR)/fh.o \ - $(SRC_DIR)/is.o \ - $(SRC_DIR)/bc.o +OBJ = test.o \ + $(SRC_DIR)/fh.o \ + $(SRC_DIR)/bc.o \ + $(SRC_DIR)/is.o \ + $(SRC_DIR)/var.o \ + $(SRC_DIR)/stk.o \ + $(SRC_DIR)/ht.o \ + $(SRC_DIR)/ns.o \ + $(SRC_DIR)/pc.o \ + $(SRC_DIR)/rt.o \ + $(SRC_DIR)/ins_def.o \ + $(SRC_DIR)/proc.o OUT = test @@ -24,5 +40,5 @@ $(OUT): $(OBJ) $(CC) $(CFLAGS) -o $@ $^ clean: - rm *.o + rm $(SRC_DIR)/*.o rm $(OUT) diff --git a/src/vm/tests/cases/bc/bytecode b/src/vm/tests/cases/bc/bytecode Binary files differ diff --git a/src/vm/tests/cases/bc/expected_output b/src/vm/tests/cases/bc/expected_output @@ -1,3 +1,3 @@ -20: 0, 1, 0 f0, -21: 0, 0 f0, -24: 1 1 2 3 4 5 6 7 8 9 a , +ff: 1 0, 1, 20 , +20: 1, 1, 1 0, +f0: diff --git a/src/vm/tests/cases/bc/test.c b/src/vm/tests/cases/bc/test.c @@ -2,6 +2,9 @@ #include "fh.h" #include "is.h" +#include "ins_mdata.h" +#include "ins_def.h" +#include "var.h" #include "bc.h" void print_op(bc_cont* op) @@ -9,11 +12,10 @@ void print_op(bc_cont* op) int num_args, arg_types[3]; - get_mdata(op->mdata, &num_args, arg_types); + unencode(op->mdata, &num_args, arg_types); printf("%x: ", op->op); - - for (int i = 0; i < num_args; i++) + for (int i = 0; i < num_args && num_args != 0; i++) { if (arg_types[i] == 1) { @@ -21,11 +23,11 @@ void print_op(bc_cont* op) } else if (arg_types[i] == 2) { - printf("%x %x", op->args[i][0], op->args[i][1]); + printf("%x %x", op->args[i][1], op->args[i][0]); } else if (arg_types[i] == 3) { - for (int x = 0; op->args[i][x] != 0; x++) + for (int x = 0; x < op->sarg[i]; x++) printf("%x ", op->args[i][x]); } printf(", "); @@ -36,7 +38,8 @@ void print_op(bc_cont* op) int main(int argc, char** argv) { - init(); + init_mdata(); + init_adata(); // start testing bc_cont* bc = bc_read("bytecode"); diff --git a/src/vm/tests/cases/ht/expected_output b/src/vm/tests/cases/ht/expected_output @@ -1,6 +1,6 @@ 0 3 4 -key2 value: 0 -key2 value: 32 +key2 value: 42 key2 value: 32 +key3 value: 3.141600 diff --git a/src/vm/tests/cases/ht/test.c b/src/vm/tests/cases/ht/test.c @@ -16,14 +16,31 @@ int main(int argc, char* argv[]) var_cont* var = ht_get(hashtable, "key2"); - printf("key2 value: %i\n", *(var->data)); + int idata; + + var_set(var, var_data_alloc_G_INT(42), G_INT); + + idata = var_data_get_G_INT(var); + + printf("key2 value: %i\n", idata); var_set(var, var_data_alloc_G_INT(32), G_INT); - printf("key2 value: %i\n", *(var->data)); + idata = var_data_get_G_INT(var); + + printf("key2 value: %i\n", idata); + + double ddata; - printf("key2 value: %i\n", *(ht_get(hashtable, "key2")->data)); + var_cont* var1 = ht_get(hashtable, "key3"); + + var_set(var1, var_data_alloc_G_FLOAT(3.1416), G_FLOAT); + + ddata = var_data_get_G_FLOAT(var1); + + printf("key3 value: %f\n", ddata); ht_destroy(hashtable); + return 0; } diff --git a/src/vm/tests/cases/pc/bytecode b/src/vm/tests/cases/pc/bytecode Binary files differ diff --git a/src/vm/tests/cases/pc/expected_output b/src/vm/tests/cases/pc/expected_output @@ -1,3 +1,3 @@ OP: ff -OP: 24 -OP: 10 +OP: 20 +OP: f0 diff --git a/src/vm/tests/cases/pc/test.c b/src/vm/tests/cases/pc/test.c @@ -8,7 +8,8 @@ int main(int argc, char *argv[]) { - init(); + init_mdata(); + init_adata(); pc_t* pc = pc_new("bytecode"); diff --git a/src/vm/tests/cases/var/Makefile b/src/vm/tests/cases/var/Makefile @@ -1,23 +0,0 @@ -SRC_DIR = ../../../src -INC_DIR = ../../../inc - -CC = gcc -CFLAGS = -std=c99 -Wall -I$(INC_DIR) - -DEPS = i$(INC_DIR)/helper.h \ - var.h \ - -OBJ = test.o \ - $(SRC_DIR)/var.o - -OUT = test - -%.o: %.c $(DEPS) - $(CC) $(CFLAGS) -c -o $@ $< - -$(OUT): $(OBJ) - $(CC) $(CFLAGS) -o $@ $^ - -clean: - rm *.o - rm $(OUT) diff --git a/src/vm/tests/cases/var/expected_output b/src/vm/tests/cases/var/expected_output @@ -1,3 +0,0 @@ -1 -1 -3 -> 4 diff --git a/src/vm/tests/cases/var/test.c b/src/vm/tests/cases/var/test.c @@ -1,30 +0,0 @@ -#include <stdio.h> - -#include "var.h" -#include "helper.h" - -int main( void ) -{ - var_cont* test0 = var_new(VOID); - if (test0->type == VOID) - printf("1\n"); - else - printf("0\n"); - - var_cont* test1 = var_new(G_INT); - - var_set(test1, var_data_alloc_G_INT(32), G_INT); - if (*(test1->data) == 32) - printf("1\n"); - else - printf("0: %d\n", *(test1->data)); - - printf("%i -> ", test1->type); - var_cast(test1, G_FLOAT); - printf("%i\n", test1->type); - - N_ASSERT(test1->data); - - var_del(test0); - var_del(test1); -}