120 lines
3.9 KiB
C
120 lines
3.9 KiB
C
#include "compiler.h"
|
|
#include "parser.h"
|
|
|
|
uint32_t test_add_compile(Value *memory) {
|
|
uint32_t i = 0;
|
|
memory[i++].u = OP(OP_LOADU, 0, 0, 0);
|
|
memory[i++].u = 1;
|
|
memory[i++].u = OP(OP_LOADU, 1, 0, 0);
|
|
memory[i++].u = 2;
|
|
memory[i++].u = OP(OP_ADD_UINT, 2, 1, 0); /* let sum = 1 + 2; */
|
|
memory[i++].u = OP(OP_UINT_TO_STRING, 3, 2, 0);
|
|
memory[i++].u = OP(OP_PRINT_STRING, 0, 3, 0); /* print(sum.toS()); */
|
|
memory[i++].u = OP(OP_HALT, 0, 0, 0); /* explicit halt */
|
|
return i;
|
|
}
|
|
|
|
uint32_t test_loop_compile(Value *memory) {
|
|
uint32_t i = 0;
|
|
memory[i++].u = OP(OP_LOADF, 0, 0, 0); /* let a = 5.0 */
|
|
memory[i++].f = 5.0f;
|
|
memory[i++].u = OP(OP_LOADI, 1, 0, 0); /* do (i = 5, 0, -1) { */
|
|
memory[i++].i = 5;
|
|
memory[i++].u = OP(OP_LOADI, 2, 0, 0); /* loop check value */
|
|
memory[i++].i = 0;
|
|
memory[i++].u = OP(OP_LOADI, 3, 0, 0); /* loop incriment value */
|
|
memory[i++].i = -1;
|
|
memory[i++].u = OP(OP_LOADU, 4, 0, 0); /* loop start */
|
|
uint32_t jmp = i + 1;
|
|
memory[i++].u = jmp;
|
|
memory[i++].u = OP(OP_LOADF, 5, 0, 0);
|
|
memory[i++].f = 5.0f;
|
|
memory[i++].u = OP(OP_ADD_REAL, 0, 0, 5); /* a += 5.0; */
|
|
memory[i++].u = OP(OP_ADD_INT, 1, 1, 3); /* (implied by loop) i = i + (-1) */
|
|
memory[i++].u = OP(OP_JGT_INT, 4, 1, 2); /* } */
|
|
memory[i++].u = OP(OP_REAL_TO_UINT, 1, 0, 0); /* let b = a as nat; */
|
|
memory[i++].u = OP(OP_READ_STRING, 2, 0, 0); /* let user_string = gets(); */
|
|
memory[i++].u = OP(OP_UINT_TO_STRING, 3, 1, 0);
|
|
memory[i++].u = OP(OP_PRINT_STRING, 0, 3, 0); /* print(a.toS()); */
|
|
memory[i++].u = OP(OP_REAL_TO_STRING, 3, 0, 0);
|
|
memory[i++].u = OP(OP_PRINT_STRING, 0, 3, 0); /* print(b.toS()); */
|
|
memory[i++].u = OP(OP_PRINT_STRING, 0, 2, 0); /* print(user_string); */
|
|
memory[i++].u = OP(OP_HALT, 0, 0, 0); /* program done */
|
|
return i;
|
|
}
|
|
|
|
uint32_t test_add_function_compile(Value *memory) {
|
|
uint32_t i = 3;
|
|
memory[i++].u = OP(OP_POPU, 0, 0, 0); /* return ptr */
|
|
memory[i++].u = OP(OP_POPI, 1, 0, 0); /* a int */
|
|
memory[i++].u = OP(OP_POPI, 2, 0, 0); /* b int */
|
|
memory[i++].u = OP(OP_ADD_INT, 3, 2, 1); /* a + b */
|
|
memory[i++].u = OP(OP_PUSHI, 3, 0, 0); /* return */
|
|
memory[i++].u = OP(OP_RETURN, 0, 0, 0);
|
|
uint32_t main = i;
|
|
memory[0].u = OP(OP_LOADF, 0, 0, 0);
|
|
memory[1].u = main;
|
|
memory[2].u = OP(OP_JMP, 0, 0, 0); /* jump to 'main' */
|
|
memory[i++].u = OP(OP_LOADI, 0, 0, 0); /* 1 */
|
|
memory[i++].i = 1;
|
|
memory[i++].u = OP(OP_PUSHI, 0, 0, 0);
|
|
memory[i++].u = OP(OP_LOADI, 0, 0, 0); /* 1 */
|
|
memory[i++].i = 1;
|
|
memory[i++].u = OP(OP_PUSHI, 0, 0, 0);
|
|
uint32_t add_fn_return = i + 6; /* after the call */
|
|
memory[i++].u = OP(OP_LOADI, 0, 0, 0); /* return */
|
|
memory[i++].u = add_fn_return;
|
|
memory[i++].u = OP(OP_PUSHU, 0, 0, 0);
|
|
memory[i++].u = OP(OP_LOADU, 0, 0, 0); /* add fn ptr */
|
|
memory[i++].u = 3;
|
|
memory[i++].u = OP(OP_CALL, 0, 0, 0); /* ); */
|
|
memory[i++].u = OP(OP_POPI, 0, 0, 0); /* get return value */
|
|
memory[i++].u = OP(OP_INT_TO_STRING, 1, 0, 0);
|
|
memory[i++].u = OP(OP_PRINT_STRING, 0, 1, 0); /* print(sum.toS()); */
|
|
memory[i++].u = OP(OP_HALT, 0, 0, 0);
|
|
return i;
|
|
}
|
|
|
|
uint32_t test_recursive_function_compile(Value *memory) {
|
|
uint32_t i = 0;
|
|
memory[i++].u = OP(OP_HALT, 0, 0, 0);
|
|
return i;
|
|
}
|
|
|
|
static void letDeclaration() {
|
|
/* uint8_t global = parseVariable("Expect variable name."); */
|
|
|
|
/* if (match(TOKEN_EQUAL)) { */
|
|
/* expression(); */
|
|
/* } else { */
|
|
/* emitByte(OP_NIL); */
|
|
/* } */
|
|
/* consume(TOKEN_SEMICOLON, */
|
|
/* "Expect ';' after variable declaration."); */
|
|
|
|
/* defineVariable(global); */
|
|
}
|
|
|
|
static void declaration(Token t) {
|
|
if (t.type == TOKEN_LET) {
|
|
letDeclaration();
|
|
} else {
|
|
/* statement(); */
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Compile.
|
|
*/
|
|
uint32_t compile(Value *memory, char *buffer) {
|
|
uint32_t i = 0;
|
|
initTokenizer(buffer);
|
|
Token t = nextToken();
|
|
while (t.type != TOKEN_EOF) {
|
|
printToken(t);
|
|
declaration(t);
|
|
t = nextToken();
|
|
}
|
|
return i;
|
|
}
|