#include "compiler.h" #include "parser.h" void 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); memory[i++].u = 200; memory[i++].u = OP(OP_LOADU, 6, 0, 0); memory[i++].u = 250; memory[i++].u = OP(OP_LOADU, 7, 0, 0); memory[i++].u = 252; uint32_t jmp = i; 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, jmp, 1, 0); memory[i++].u = OP(OP_REAL_TO_STRING, 5, 4, 0); memory[i++].u = OP(OP_PRINT_STRING, 0, 5, 0); memory[i++].u = OP(OP_REAL_TO_UINT, 1, 4, 4); memory[i++].u = OP(OP_UINT_TO_STRING, 6, 1, 0); memory[i++].u = OP(OP_PRINT_STRING, 0, 6, 0); memory[i++].u = OP(OP_READ_STRING, 7, 0, 0); memory[i++].u = OP(OP_PRINT_STRING, 0, 7, 0); memory[i++].u = OP(OP_HALT, 0, 0, 0); } 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. */ void compile(Value *memory, char *buffer) { initTokenizer(buffer); Token t = nextToken(); while (t.type != TOKEN_EOF) { printToken(t); declaration(t); t = nextToken(); } }