1
0
Fork 0
undar-lang-register/src/compiler.c

77 lines
1.8 KiB
C

#include "compiler.h"
#include "parser.h"
uint32_t demo_add_compile(Value *memory) {
uint32_t i = 0;
memory[i++].u = OP(OP_LOADU, 0, 0, 0);
memory[i++].u = 0;
memory[i++].u = OP(OP_LOADU, 1, 0, 0);
memory[i++].u = 5;
memory[i++].u = OP(OP_LOADU, 2, 0, 0);
memory[i++].u = 1;
memory[i++].u = OP(OP_LOADF, 3, 0, 0);
memory[i++].f = 5.0f;
memory[i++].u = OP(OP_LOADF, 4, 0, 0);
memory[i++].f = 5.0f;
memory[i++].u = OP(OP_LOADU, 5, 0, 0);
uint32_t jmp = i + 1;
memory[i++].u = jmp;
memory[i++].u = OP(OP_ADD_REAL, 4, 4, 3);
memory[i++].u = OP(OP_SUB_UINT, 1, 1, 2);
memory[i++].u = OP(OP_JGT_UINT, 5, 1, 0);
memory[i++].u = OP(OP_REAL_TO_STRING, 6, 4, 0);
memory[i++].u = OP(OP_REAL_TO_UINT, 1, 4, 4);
memory[i++].u = OP(OP_UINT_TO_STRING, 7, 1, 0);
memory[i++].u = OP(OP_READ_STRING, 8, 0, 0);
memory[i++].u = OP(OP_PRINT_STRING, 0, 6, 0);
memory[i++].u = OP(OP_PRINT_STRING, 0, 7, 0);
memory[i++].u = OP(OP_PRINT_STRING, 0, 8, 0);
memory[i++].u = OP(OP_HALT, 0, 0, 0); /* explicit halt */
return i;
}
uint32_t demo_function_call_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;
}