language

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

commit 196b9fcd340b1b8c19946d90bf5e609f9e146eb5
parent 2c745091611a5dc65649f638e46097e30f782918
Author: Paul Longtine <paullongtine@gmail.com>
Date:   Thu Feb 25 13:19:01 2016

Mapped opcodes to instructions, Updated 'bc' testcase, Made testcases more automated

Diffstat:
 doc/SPECIFICATION                      | 63 +++++++++++++++++++++++++++++++----
 src/vm/inc/ins_mdata.h                 | 60 ++++++++++++++++++++++++++++++---
 src/vm/inc/is.h                        |  2 +-
 src/vm/src/fh.c                        |  6 ++-
 src/vm/tests/bc/Makefile               | 29 +----------------
 src/vm/tests/bc/bytecode               | Bin 14 -> 0 bytes
 src/vm/tests/bc/expected_output        |  4 +--
 src/vm/tests/bc/test.c                 | 43 +------------------------
 src/vm/tests/cases/bc/Makefile         | 28 ++++++++++++++++-
 src/vm/tests/cases/bc/bytecode         | Bin 0 -> 22 bytes
 src/vm/tests/cases/bc/expected_output  |  3 ++-
 src/vm/tests/cases/bc/test.c           | 54 ++++++++++++++++++++++++++++++-
 src/vm/tests/cases/ht/Makefile         | 25 ++++++++++++++-
 src/vm/tests/cases/ht/expected_output  |  6 +++-
 src/vm/tests/cases/ht/test.c           | 29 ++++++++++++++++-
 src/vm/tests/cases/ns/Makefile         | 25 ++++++++++++++-
 src/vm/tests/cases/ns/expected_output  |  3 ++-
 src/vm/tests/cases/ns/test.c           | 26 ++++++++++++++-
 src/vm/tests/cases/stk/Makefile        | 26 ++++++++++++++-
 src/vm/tests/cases/stk/expected_output |  4 ++-
 src/vm/tests/cases/stk/test.c          | 48 +++++++++++++++++++++++++++-
 src/vm/tests/cases/var/Makefile        | 23 +++++++++++++-
 src/vm/tests/cases/var/expected_output |  3 ++-
 src/vm/tests/cases/var/test.c          | 30 +++++++++++++++++-
 src/vm/tests/ht/Makefile               | 26 +--------------
 src/vm/tests/ht/expected_output        |  6 +---
 src/vm/tests/ht/test.c                 | 29 +----------------
 src/vm/tests/ns/Makefile               | 26 +--------------
 src/vm/tests/ns/expected_output        |  3 +--
 src/vm/tests/ns/test.c                 | 26 +--------------
 src/vm/tests/run.sh                    |  7 ++++-
 src/vm/tests/stk/Makefile              | 26 +--------------
 src/vm/tests/stk/expected_output       |  4 +--
 src/vm/tests/stk/test.c                | 48 +---------------------------
 src/vm/tests/tools/clean.sh            | 12 +++++++-
 src/vm/tests/tools/test.sh             | 18 ++++++++++-
 src/vm/tests/var/Makefile              | 24 +-------------
 src/vm/tests/var/expected_output       |  3 +--
 src/vm/tests/var/test.c                | 30 +-----------------
 39 files changed, 488 insertions(+), 340 deletions(-)

diff --git a/doc/SPECIFICATION b/doc/SPECIFICATION @@ -47,7 +47,12 @@ To set variable: Both of these functions require some safeguards against hard crashes, where the program will segfault if given incorrect values. +________________________________________________________________________________ RUNTIME +-------------------------------------------------------------------------------- + +TODO: SPEC VARIABLE SERIALZATION + Code in the runtime is organized on a per-function basis. @@ -127,8 +132,9 @@ Keywords: Types are in the air at this moment. I'll detail what types there are when the time comes ------------------------------------------------------------------------------- -30 TYPEOF D<ref> - returns type structure on TOS (used for comparing types) -31 CAST D<ref> S<type> - Tries to cast <ref> to <type> +30 TYPEOF - returns type structure on TOS + (used for comparing types) +31 CAST S<type> - Tries to cast TOS to <type> ------------------------------------------------------------------------------- 4 - Binary Ops OPS take the two top elements of the stack, preform an operation and push @@ -175,8 +181,7 @@ to TOS ------------------------------------------------------------------------------- 70 GOTO A<addr> - Goes to address 71 JUMPF S<n> - Goes forward <n> lines -7F CALL D<ref> - Calls function, pushes return value on to STACK. Expects array - on TOS to be used for arguements +7F CALL D<ref> - Calls function, pushes return value on to STACK. ------------------------------------------------------------------------------- 8 - Manipulating High-order Object Variables @@ -187,6 +192,15 @@ to TOS 81 DEL - Delete element in structure at TOS (assumes index of sorts) for <ref> object. + +82 GET - Gets key, TOS must be a keyed datastructure, and TOS1 must be + a string that is a key in TOS. + +83 GETP D<ref> - Get property of <ref> in object contained in TOS. pushes + to stack + +84 CALLM D<ref> - Call method of <ref> in object contained in TOS. Uses + arguement stack ------------------------------------------------------------------------------- F - Functions/classes ------------------------------------------------------------------------------- @@ -194,11 +208,48 @@ FF DEFUN D<ref> S<type> D<args> - Un-funs everything. no, no- it defines a function. D<ref> is its name, S<type> is the return value, D<args> is the args. -FE DECLASS D<ref> - Defines a class +FE DECLASS D<ref> D<args> - Defines a class. ------------------------------------------------------------------------------- 0 - SPECIAL BYTES ------------------------------------------------------------------------------- -00 - Terminator byte +00 NULL - Terminator byte + +01 SYNC - Updates global namespace with global namespace cache of current + thread + +0E ARGB - Builds arguement stack + +0F LIBC A<ref> - Library call + +------------------------------------------------------------------------------- +General Architecture Overview +------------------------------------------------------------------------------- +RUNTIME CONTEXT + + The runtime context keeps track of a invidual threads metadata, such as: + + * The operating stack + + * Local namespace scope stack + + * Arguement stack + + * Thread instance + + * Global Namespace cache + + * Program counter + + +FUNCTION CALL PROCEDURE + + When calling a function, arguements are assumed to be built up using the +`ARGB` instruction. TOS is popped into a special stack, named 'arguement stack' +which is a member of the runtime context. + +________________________________________________________________________________ +COMPILER/TRANSLATOR/ASSEMBLER SHINDIGS +-------------------------------------------------------------------------------- LEXICAL ANALYSIS diff --git a/src/vm/inc/ins_mdata.h b/src/vm/inc/ins_mdata.h @@ -1,10 +1,60 @@ #ifndef INS_MDATA_H #define INS_MDATA_H -#define INS_MDATA_DEF() \ -INS_MDATA[1] = encode(0, A_NULL, A_NULL, A_NULL); \ -INS_MDATA[2] = encode(1, A_BYTE, A_NULL, A_NULL); \ -INS_MDATA[3] = encode(2, A_BYTE, A_WORD, A_NULL); \ -INS_MDATA[4] = encode(1, A_DYNC, A_NULL, A_NULL); +#define INS_MDATA_DEF() \ +/* NULL */ INS_MDATA[0x00] = encode(0, A_NULL, A_NULL, A_NULL); \ +/* POP */ INS_MDATA[0x10] = encode(1, A_BYTE, A_NULL, A_NULL); \ +/* ROT */ INS_MDATA[0x11] = encode(0, A_NULL, A_NULL, A_NULL); \ +/* DUP */ INS_MDATA[0x12] = encode(0, A_NULL, A_NULL, A_NULL); \ +/* ROT_THREE */ INS_MDATA[0x13] = encode(0, A_NULL, A_NULL, A_NULL); \ + \ +/* DEC */ INS_MDATA[0x20] = encode(3, A_BYTE, A_BYTE, A_WORD); \ +/* LOV */ INS_MDATA[0x21] = encode(2, A_BYTE, A_WORD, A_NULL); \ +/* STV */ INS_MDATA[0x22] = encode(2, A_BYTE, A_WORD, A_NULL); \ +/* LOC */ INS_MDATA[0x23] = encode(3, A_BYTE, A_WORD, A_DYNC); \ +/* CTS */ INS_MDATA[0x24] = encode(1, A_DYNC, A_NULL, A_NULL); \ + \ +/* TYPEOF */ INS_MDATA[0x30] = encode(0, A_NULL, A_NULL, A_NULL); \ +/* CAST */ INS_MDATA[0x31] = encode(1, A_BYTE, A_NULL, A_NULL); \ + \ +/* ADD */ INS_MDATA[0x40] = encode(0, A_NULL, A_NULL, A_NULL); \ +/* SUB */ INS_MDATA[0x41] = encode(0, A_NULL, A_NULL, A_NULL); \ +/* MULT */ INS_MDATA[0x42] = encode(0, A_NULL, A_NULL, A_NULL); \ +/* DIV */ INS_MDATA[0x43] = encode(0, A_NULL, A_NULL, A_NULL); \ +/* POW */ INS_MDATA[0x44] = encode(0, A_NULL, A_NULL, A_NULL); \ +/* BRT */ INS_MDATA[0x45] = encode(0, A_NULL, A_NULL, A_NULL); \ +/* SIN */ INS_MDATA[0x46] = encode(0, A_NULL, A_NULL, A_NULL); \ +/* COS */ INS_MDATA[0x47] = encode(0, A_NULL, A_NULL, A_NULL); \ +/* TAN */ INS_MDATA[0x48] = encode(0, A_NULL, A_NULL, A_NULL); \ +/* ISIN */ INS_MDATA[0x49] = encode(0, A_NULL, A_NULL, A_NULL); \ +/* ICOS */ INS_MDATA[0x4A] = encode(0, A_NULL, A_NULL, A_NULL); \ +/* ITAN */ INS_MDATA[0x4B] = encode(0, A_NULL, A_NULL, A_NULL); \ +/* MOD */ INS_MDATA[0x4C] = encode(0, A_NULL, A_NULL, A_NULL); \ +/* OR */ INS_MDATA[0x4D] = encode(0, A_NULL, A_NULL, A_NULL); \ +/* XOR */ INS_MDATA[0x4E] = encode(0, A_NULL, A_NULL, A_NULL); \ +/* NAND */ INS_MDATA[0x4F] = encode(0, A_NULL, A_NULL, A_NULL); \ + \ +/* GTHAN */ INS_MDATA[0x50] = encode(0, A_NULL, A_NULL, A_NULL); \ +/* LTHAN */ INS_MDATA[0x51] = encode(0, A_NULL, A_NULL, A_NULL); \ +/* EQ */ INS_MDATA[0x52] = encode(0, A_NULL, A_NULL, A_NULL); \ +/* NOT */ INS_MDATA[0x53] = encode(0, A_NULL, A_NULL, A_NULL); \ + \ +/* STARTL */ INS_MDATA[0x60] = encode(0, A_NULL, A_NULL, A_NULL); \ +/* CLOOP */ INS_MDATA[0x61] = encode(0, A_NULL, A_NULL, A_NULL); \ +/* BREAK */ INS_MDATA[0x6E] = encode(0, A_NULL, A_NULL, A_NULL); \ +/* DONE */ INS_MDATA[0x6F] = encode(0, A_NULL, A_NULL, A_NULL); \ + \ +/* GOTO */ INS_MDATA[0x70] = encode(1, A_WORD, A_NULL, A_NULL); \ +/* JUMPF */ INS_MDATA[0x71] = encode(1, A_BYTE, A_NULL, A_NULL); \ +/* CALL */ INS_MDATA[0x7F] = encode(1, A_WORD, A_NULL, A_NULL); \ + \ +/* PUSH */ INS_MDATA[0x80] = encode(0, A_NULL, A_NULL, A_NULL); \ +/* DEL */ INS_MDATA[0x81] = encode(0, A_NULL, A_NULL, A_NULL); \ +/* GET */ INS_MDATA[0x82] = encode(0, A_NULL, A_NULL, A_NULL); \ +/* GETP */ INS_MDATA[0x83] = encode(0, A_WORD, A_NULL, A_NULL); \ +/* CALLM */ INS_MDATA[0x84] = encode(0, A_WORD, A_NULL, A_NULL); \ + \ +/* DECLASS */ INS_MDATA[0xFE] = encode(0, A_WORD, A_DYNC, A_NULL); \ +/* DEFUN */ INS_MDATA[0xFF] = encode(0, A_WORD, A_BYTE, A_DYNC); #endif diff --git a/src/vm/inc/is.h b/src/vm/inc/is.h @@ -23,7 +23,7 @@ #define encode(n, a0, a1, a2) \ ( n << 6 | a0 << 4 | a1 << 2 | a2 ); -// This array is populated by inc/is_mdata +// This array is populated by inc/is_mdata.h byte_t INS_MDATA[256]; /* Takes an opcode, fills metadata about that opcode (given that it exists) in diff --git a/src/vm/src/fh.c b/src/vm/src/fh.c @@ -20,14 +20,18 @@ byte_t* read_until_null(FILE* f) // 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)); + ASSERT(buffer != NULL, "Could not allocate memory\n"); - fread(buffer, bytes, 1, f); + + fread(buffer, bytes, sizeof(byte_t), f); return buffer; } diff --git a/src/vm/tests/bc/Makefile b/src/vm/tests/bc/Makefile @@ -1,29 +0,0 @@ -SRC_DIR = ../../src -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 \ - -OBJ = test.o \ - $(SRC_DIR)/fh.o \ - $(SRC_DIR)/is.o \ - $(SRC_DIR)/bc.o - -OUT = test - -%.o: %.c $(DEPS) - $(CC) $(CFLAGS) -c -o $@ $< - -$(OUT): $(OBJ) - $(CC) $(CFLAGS) -o $@ $^ - -clean: - rm *.o - rm $(SRC_DIR)/*.o - rm $(OUT) diff --git a/src/vm/tests/bc/bytecode b/src/vm/tests/bc/bytecode Binary files differ diff --git a/src/vm/tests/bc/expected_output b/src/vm/tests/bc/expected_output @@ -1,4 +0,0 @@ -1: -2: 33 -3: 44, 12 34 -4: 12 34 56 78 90 0 0 0 diff --git a/src/vm/tests/bc/test.c b/src/vm/tests/bc/test.c @@ -1,43 +0,0 @@ -#include <stdio.h> - -#include "fh.h" -#include "is.h" -#include "bc.h" - -int main(int argc, char** argv) -{ - if (argc < 2) return -1; - init(); - - // start testing - bc_cont* bc = bc_read(argv[1]); - bc_cont* ptr; - - for (ptr = bc; ptr->next != NULL; ptr = ptr->next) - { - if (ptr->op == 1) - { - printf("%x:\n", ptr->op); - } - if (ptr->op == 2) - { - printf("%x: %x\n", ptr->op, ptr->args[0][0]); - } - if (ptr->op == 3) - { - printf("%x: %x, %x %x\n", - ptr->op, ptr->args[0][0], ptr->args[1][0], ptr->args[1][1]); - } - if (ptr->op == 4) - { - printf("%x: ", ptr->op); - int len = sizeof(ptr->args[0]); - for (int i = 0; i < len; i++) printf("%x ", ptr->args[0][i]); - printf("\n"); - } - } - - bc_cont_del(bc); - // end testing - return 0; -} diff --git a/src/vm/tests/cases/bc/Makefile b/src/vm/tests/cases/bc/Makefile @@ -0,0 +1,28 @@ +SRC_DIR = ../../../src +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 \ + +OBJ = test.o \ + $(SRC_DIR)/fh.o \ + $(SRC_DIR)/is.o \ + $(SRC_DIR)/bc.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/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 @@ -0,0 +1,3 @@ +20: 0, 1, 0 f0, +21: 0, 0 f0, +24: 1 1 2 3 4 5 6 7 8 9 a , diff --git a/src/vm/tests/cases/bc/test.c b/src/vm/tests/cases/bc/test.c @@ -0,0 +1,54 @@ +#include <stdio.h> + +#include "fh.h" +#include "is.h" +#include "bc.h" + +void print_op(bc_cont* op) +{ + int num_args, + arg_types[3]; + + get_mdata(op->mdata, &num_args, arg_types); + + printf("%x: ", op->op); + + for (int i = 0; i < num_args; i++) + { + if (arg_types[i] == 1) + { + printf("%x", op->args[i][0]); + } else + if (arg_types[i] == 2) + { + printf("%x %x", op->args[i][0], op->args[i][1]); + } else + if (arg_types[i] == 3) + { + for (int x = 0; op->args[i][x] != 0; x++) + printf("%x ", op->args[i][x]); + } + printf(", "); + } + + printf("\n"); +} + +int main(int argc, char** argv) +{ + if (argc < 2) return -1; + init(); + + // start testing + bc_cont* bc = bc_read("bytecode"); + bc_cont* ptr; + + for (ptr = bc; ptr->next != NULL; ptr = ptr->next) + { + print_op(ptr); + } + + bc_cont_del(bc); + // end testing + return 0; +} diff --git a/src/vm/tests/cases/ht/Makefile b/src/vm/tests/cases/ht/Makefile @@ -0,0 +1,25 @@ +SRC_DIR = ../../../src +INC_DIR = ../../../inc + +CC = gcc +CFLAGS = -std=c99 -Wall -I$(INC_DIR) + +DEPS = i$(INC_DIR)/helper.h \ + var.h \ + ht.h + +OBJ = test.o \ + $(SRC_DIR)/var.o \ + $(SRC_DIR)/ht.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/ht/expected_output b/src/vm/tests/cases/ht/expected_output @@ -0,0 +1,6 @@ +0 +3 +4 +key2 value: 0 +key2 value: 32 +key2 value: 32 diff --git a/src/vm/tests/cases/ht/test.c b/src/vm/tests/cases/ht/test.c @@ -0,0 +1,29 @@ +#include <stdio.h> + +#include "var.h" +#include "ht.h" + +int main(int argc, char* argv[]) +{ + ht_t* hashtable = ht_init(2); + ht_set(hashtable, "key1", var_new(VOID)); + ht_set(hashtable, "key2", var_new(G_INT)); + ht_set(hashtable, "key3", var_new(G_FLOAT)); + + printf("%i\n", ht_get(hashtable, "key1")->type); + printf("%i\n", ht_get(hashtable, "key2")->type); + printf("%i\n", ht_get(hashtable, "key3")->type); + + var_cont* var = ht_get(hashtable, "key2"); + + printf("key2 value: %i\n", *(var->data)); + + var_set(var, var_data_alloc_G_INT(32), G_INT); + + printf("key2 value: %i\n", *(var->data)); + + printf("key2 value: %i\n", *(ht_get(hashtable, "key2")->data)); + + ht_destroy(hashtable); + return 0; +} diff --git a/src/vm/tests/cases/ns/Makefile b/src/vm/tests/cases/ns/Makefile @@ -0,0 +1,25 @@ +SRC_DIR = ../../../src +INC_DIR = ../../../inc + +CC = gcc +CFLAGS = -std=c99 -Wall -I$(INC_DIR) + +DEPS = i$(INC_DIR)/helper.h \ + ns.h \ + var.h \ + +OBJ = test.o \ + $(SRC_DIR)/ns.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/ns/expected_output b/src/vm/tests/cases/ns/expected_output @@ -0,0 +1,3 @@ +2 +5 +10 diff --git a/src/vm/tests/cases/ns/test.c b/src/vm/tests/cases/ns/test.c @@ -0,0 +1,26 @@ +#include <stdio.h> + +#include "ns.h" + +int main( void ) +{ + ns_t* test = ns_init(10); + + ns_push(test, 5); + + ns_push(test, 2); + + printf("%d\n", test->last->size); + + ns_pop(test); + + printf("%d\n", test->last->size); + + ns_pop(test); + + printf("%d\n", test->last->size); + + ns_del(test); + + return 0; +} diff --git a/src/vm/tests/cases/stk/Makefile b/src/vm/tests/cases/stk/Makefile @@ -0,0 +1,26 @@ +SRC_DIR = ../../../src +INC_DIR = ../../../inc + +CC = gcc +CFLAGS = -std=c99 -Wall -I$(INC_DIR) + +DEPS = i$(INC_DIR)/helper.h \ + stk.h \ + var.h \ + +OBJ = test.o \ + $(SRC_DIR)/stk.o \ + $(SRC_DIR)/var.o + +OUT = test + +%.o: %.c $(DEPS) + $(CC) $(CFLAGS) -c -o $@ $< + +$(OUT): $(OBJ) + $(CC) $(CFLAGS) -o $@ $^ + +clean: + rm *.o + rm $(OUT) + rm $(SRC_DIR)/*.o diff --git a/src/vm/tests/cases/stk/expected_output b/src/vm/tests/cases/stk/expected_output @@ -0,0 +1,4 @@ +7, 5, 4, 3 +5, 4, 3, 0 +4, 5, 3, 0 +3, 4, 5, 0 diff --git a/src/vm/tests/cases/stk/test.c b/src/vm/tests/cases/stk/test.c @@ -0,0 +1,48 @@ +#include <stdio.h> + +#include "stk.h" +#include "var.h" + + +void printstk(stk_t* stk) +{ + var_cont* test0 = stk_at(stk, 0); + var_cont* test1 = stk_at(stk, 1); + var_cont* test2 = stk_at(stk, 2); + var_cont* test3 = stk_at(stk, 3); + printf("%i, %i, %i, %i\n", + test0->type, test1->type, test2->type, test3->type); +} + +int main(int argc, char* argv[]) +{ + stk_t* new = stk_new(); + + new->data = var_new(VOID); + + stk_push(&new, var_new(VOID)); + stk_push(&new, var_new(G_INT)); + stk_push(&new, var_new(G_FLOAT)); + stk_push(&new, var_new(G_CHAR)); + stk_push(&new, var_new(G_STR)); + + printstk(new); + + stk_pop(&new); + + printstk(new); + + stk_rot_top(&new); + + printstk(new); + + stk_rot_top(&new); + + stk_rot_three(&new); + + printstk(new); + + stk_del(new); + + return 0; +} diff --git a/src/vm/tests/cases/var/Makefile b/src/vm/tests/cases/var/Makefile @@ -0,0 +1,23 @@ +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 @@ -0,0 +1,3 @@ +1 +1 +3 -> 4 diff --git a/src/vm/tests/cases/var/test.c b/src/vm/tests/cases/var/test.c @@ -0,0 +1,30 @@ +#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); +} diff --git a/src/vm/tests/ht/Makefile b/src/vm/tests/ht/Makefile @@ -1,26 +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 \ - ht.h - -OBJ = test.o \ - $(SRC_DIR)/var.o \ - $(SRC_DIR)/ht.o - -OUT = test - -%.o: %.c $(DEPS) - $(CC) $(CFLAGS) -c -o $@ $< - -$(OUT): $(OBJ) - $(CC) $(CFLAGS) -o $@ $^ - -clean: - rm *.o - rm $(SRC_DIR)/*.o - rm $(OUT) diff --git a/src/vm/tests/ht/expected_output b/src/vm/tests/ht/expected_output @@ -1,6 +0,0 @@ -0 -3 -4 -key2 value: 0 -key2 value: 32 -key2 value: 32 diff --git a/src/vm/tests/ht/test.c b/src/vm/tests/ht/test.c @@ -1,29 +0,0 @@ -#include <stdio.h> - -#include "var.h" -#include "ht.h" - -int main(int argc, char* argv[]) -{ - ht_t* hashtable = ht_init(2); - ht_set(hashtable, "key1", var_new(VOID)); - ht_set(hashtable, "key2", var_new(G_INT)); - ht_set(hashtable, "key3", var_new(G_FLOAT)); - - printf("%i\n", ht_get(hashtable, "key1")->type); - printf("%i\n", ht_get(hashtable, "key2")->type); - printf("%i\n", ht_get(hashtable, "key3")->type); - - var_cont* var = ht_get(hashtable, "key2"); - - printf("key2 value: %i\n", *(var->data)); - - var_set(var, var_data_alloc_G_INT(32), G_INT); - - printf("key2 value: %i\n", *(var->data)); - - printf("key2 value: %i\n", *(ht_get(hashtable, "key2")->data)); - - ht_destroy(hashtable); - return 0; -} diff --git a/src/vm/tests/ns/Makefile b/src/vm/tests/ns/Makefile @@ -1,26 +0,0 @@ -SRC_DIR = ../../src -INC_DIR = ../../inc - -CC = gcc -CFLAGS = -std=c99 -Wall -I$(INC_DIR) - -DEPS = i$(INC_DIR)/helper.h \ - ns.h \ - var.h \ - -OBJ = test.o \ - $(SRC_DIR)/ns.o \ - $(SRC_DIR)/var.o - -OUT = test - -%.o: %.c $(DEPS) - $(CC) $(CFLAGS) -c -o $@ $< - -$(OUT): $(OBJ) - $(CC) $(CFLAGS) -o $@ $^ - -clean: - rm *.o - rm $(SRC_DIR)/*.o - rm $(OUT) diff --git a/src/vm/tests/ns/expected_output b/src/vm/tests/ns/expected_output @@ -1,3 +0,0 @@ -2 -5 -10 diff --git a/src/vm/tests/ns/test.c b/src/vm/tests/ns/test.c @@ -1,26 +0,0 @@ -#include <stdio.h> - -#include "ns.h" - -int main( void ) -{ - ns_t* test = ns_init(10); - - ns_push(test, 5); - - ns_push(test, 2); - - printf("%d\n", test->last->size); - - ns_pop(test); - - printf("%d\n", test->last->size); - - ns_pop(test); - - printf("%d\n", test->last->size); - - ns_del(test); - - return 0; -} diff --git a/src/vm/tests/run.sh b/src/vm/tests/run.sh @@ -0,0 +1,7 @@ +#!/bin/bash + +cd cases + +../tools/test.sh $1 + +../tools/clean.sh $1 diff --git a/src/vm/tests/stk/Makefile b/src/vm/tests/stk/Makefile @@ -1,26 +0,0 @@ -SRC_DIR = ../../src -INC_DIR = ../../inc - -CC = gcc -CFLAGS = -std=c99 -Wall -I$(INC_DIR) - -DEPS = i$(INC_DIR)/helper.h \ - stk.h \ - var.h \ - -OBJ = test.o \ - $(SRC_DIR)/stk.o \ - $(SRC_DIR)/var.o - -OUT = test - -%.o: %.c $(DEPS) - $(CC) $(CFLAGS) -c -o $@ $< - -$(OUT): $(OBJ) - $(CC) $(CFLAGS) -o $@ $^ - -clean: - rm *.o - rm $(SRC_DIR)/*.o - rm $(OUT) diff --git a/src/vm/tests/stk/expected_output b/src/vm/tests/stk/expected_output @@ -1,4 +0,0 @@ -7, 5, 4, 3 -5, 4, 3, 0 -4, 5, 3, 0 -3, 4, 5, 0 diff --git a/src/vm/tests/stk/test.c b/src/vm/tests/stk/test.c @@ -1,48 +0,0 @@ -#include <stdio.h> - -#include "stk.h" -#include "var.h" - - -void printstk(stk_t* stk) -{ - var_cont* test0 = stk_at(stk, 0); - var_cont* test1 = stk_at(stk, 1); - var_cont* test2 = stk_at(stk, 2); - var_cont* test3 = stk_at(stk, 3); - printf("%i, %i, %i, %i\n", - test0->type, test1->type, test2->type, test3->type); -} - -int main(int argc, char* argv[]) -{ - stk_t* new = stk_new(); - - new->data = var_new(VOID); - - stk_push(&new, var_new(VOID)); - stk_push(&new, var_new(G_INT)); - stk_push(&new, var_new(G_FLOAT)); - stk_push(&new, var_new(G_CHAR)); - stk_push(&new, var_new(G_STR)); - - printstk(new); - - stk_pop(&new); - - printstk(new); - - stk_rot_top(&new); - - printstk(new); - - stk_rot_top(&new); - - stk_rot_three(&new); - - printstk(new); - - stk_del(new); - - return 0; -} diff --git a/src/vm/tests/tools/clean.sh b/src/vm/tests/tools/clean.sh @@ -0,0 +1,12 @@ +#!/bin/bash + +dir=$(ls) + +for i in $dir +do + if [ "$1" == 'all' ] || [ "$1" == $i ]; then + cd $i + make clean + cd .. + fi +done diff --git a/src/vm/tests/tools/test.sh b/src/vm/tests/tools/test.sh @@ -0,0 +1,18 @@ +#!/bin/bash + +dir=$(ls) + +for i in $dir +do + if [ "$1" == 'all' ] || [ "$1" == $i ]; then + cd $i + make + + printf "\nTESTCASE: $(basename $PWD)\n" + printf "The following differences between valid test vs. current test:" + printf "\n" + + ./test bytecode | diff /dev/stdin expected_output + cd .. + fi +done diff --git a/src/vm/tests/var/Makefile b/src/vm/tests/var/Makefile @@ -1,24 +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 $(SRC_DIR)/*.o - rm $(OUT) diff --git a/src/vm/tests/var/expected_output b/src/vm/tests/var/expected_output @@ -1,3 +0,0 @@ -1 -1 -3 -> 4 diff --git a/src/vm/tests/var/test.c b/src/vm/tests/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); -}