From 9daf28262678c34eb86b3d37fd791bfa214090dc Mon Sep 17 00:00:00 2001 From: sbosse Date: Mon, 16 Mar 2026 11:10:09 +0100 Subject: [PATCH] Mon 16 Mar 11:09:06 CET 2026 --- src/test.c | 87 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 87 insertions(+) create mode 100644 src/test.c diff --git a/src/test.c b/src/test.c new file mode 100644 index 0000000..1169e5c --- /dev/null +++ b/src/test.c @@ -0,0 +1,87 @@ +#include "config.h" +#include "types.h" +#include "lexer.h" +#include "tokens.h" +#include "mem.h" +#include "reg.h" +#include "stack.h" +#include "ops.h" +#include "code.h" +#include "debug.h" +#include "vm.h" +#include "var.h" +#include "printf.h" + +// char input[]="a=123 + 567 * 7:a=a+1"; +char input[]="a=1: if a>0 then a=-a\n else a=a+1\nb=1"; +//char input[]="a=1: if a>0 { a=-a } else { a=a+1 } \nb=1"; + +lexer_t L; +mem_t M; +reg_t R; +stack_t DS,FS; + +int main() { + mem_t *mp=&M; + + M.size=1000; + M.data=(unsigned char*)malloc(1000); + M.top=M.size-1; // empty! + M.bottom=0; + + + //address_t a1=allocate(&M,&R,"a",MVAR,1); + //print_format("$a1=%d\n",a1); + //address_t b1=allocate(&M,&R,"b",MVAR,1); + //print_format("$b1=%d\n",b1); + + DS.size=FS.size=32; + DS.data=(number_t*)malloc(32); + FS.data=(number_t*)malloc(32); + DS.sp=FS.sp=0; + + R.pc=R.error=0; + R.debug=1; + + + LexerSetup(&L,input); + do { + NextToken(&L); +#ifdef DEBUG + PrintLexerToken(&L); +#endif + } while (R.error==0 && L.token!=EOL); + if (R.error) { + printf("Lexer error %d\n",R.error); + return -1; + } + + LexerSetup(&L,input); + do { + CompileInstr(&L,&M,&R); + } while (R.error==0 && *L.input!=EOL); + if (R.error) { + printf("Compiler error %d in line %d\n",R.error,L.line); + return -1; + } + CODESTORE0(mp,M.bottom,END); M.bottom++; + +#ifdef DEBUG + PrintCode(&M); +#endif + // return 0; + int status; + do { + status=run(&DS,&FS,&M,&R); + } while (R.error==0 && status==1); + if (R.error) { + printf("Run-time error %d at pc=%d\n",R.error,R.pc); + return -1; + } +#ifdef DEBUG + PrintStack("DS",&DS); + PrintStack("FS",&FS); + + PrintHeap(&M); +#endif +}