#include #include #include "Code.h" #include "Compiler.h" #include "Values.h" #define POP_VALUE(name) \ Value name; \ if(popValue(&name)) \ return true static bool iAdd() { POP_VALUE(a); POP_VALUE(b); return pushValue(INT_VALUE(a.intValue + b.intValue)); } static bool iPushConstantString() { return pushValue(CSTRING_VALUE(codeReadConstantString())); } static bool iPushInt() { return pushValue(INT_VALUE(codeReadI64())); } static bool iPrint() { POP_VALUE(a); switch(a.type) { case INT64: printf("%ld", a.intValue); break; case CONSTANT_STRING: printf("%s", a.constantStringValue); break; } return false; } static bool iPrintNewline() { putchar('\n'); return false; } static bool execute(Instruction command) { switch(command) { case ADD: return iAdd(); case PUSH_CONSTANT_STRING: return iPushConstantString(); case PUSH_INT64: return iPushInt(); case PRINT: return iPrint(); case PRINT_NEWLINE: return iPrintNewline(); case STOP: return true; } return false; } int main(int argCount, const char** args) { if(argCount < 2) { return 0; } const Error* e = compileFile(args[1]); if(hasError(e)) { puts(e->text); return 0; } // if(strcmp(args[1], "./test/Print.basic") == 0) { // (void)pushInstruction(PUSH_CONSTANT_STRING); // (void)pushConstantString("Hi"); // (void)pushInstruction(PRINT); // (void)pushInstruction(PRINT_NEWLINE); // (void)pushInstruction(PUSH_INT); // (void)pushI64(6); // (void)pushInstruction(PRINT); // (void)pushInstruction(PRINT_NEWLINE); // (void)pushInstruction(PUSH_CONSTANT_STRING); // (void)pushConstantString("Hi there "); // (void)pushInstruction(PRINT); // (void)pushInstruction(PUSH_INT); // (void)pushI64(8); // (void)pushInstruction(PRINT); // (void)pushInstruction(PUSH_CONSTANT_STRING); // (void)pushConstantString(" great"); // (void)pushInstruction(PRINT); // (void)pushInstruction(PRINT_NEWLINE); //} else if(strcmp(args[1], "./test/Add.basic") == 0) { // (void)pushInstruction(PUSH_INT); // (void)pushI64(1); // (void)pushInstruction(PUSH_INT); // (void)pushI64(20); // (void)pushInstruction(ADD); // (void)pushInstruction(PRINT); // (void)pushInstruction(PRINT_NEWLINE); //} while(!execute(codeReadInstruction())) {} // char line[256]; // while(true) { // fgets(line, sizeof(line), stdin); // if(strcmp(line, "quit\n") == 0) { // break; // } // puts(line); // } // puts("quit"); return 0; }