Mon 16 Mar 11:09:06 CET 2026

This commit is contained in:
sbosse 2026-03-16 11:10:09 +01:00
parent 9fec172b3a
commit 9daf282626

87
src/test.c Normal file
View File

@ -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
}