Variables now persist between statements

This commit is contained in:
2022-08-13 21:27:39 +01:00
parent 633df5f376
commit e9ab6f3f96
5 changed files with 34 additions and 18 deletions

View File

@@ -18,11 +18,11 @@ static void stderrWrapper(const char* output) {
fprintf(stderr, "\n"); //default new line
}
void initInterpreter(Interpreter* interpreter, unsigned char* bytecode, int length) {
void initInterpreter(Interpreter* interpreter) {
initLiteralArray(&interpreter->literalCache);
interpreter->scope = pushScope(NULL);
interpreter->bytecode = bytecode;
interpreter->length = length;
interpreter->bytecode = NULL;
interpreter->length = 0;
interpreter->count = 0;
initLiteralArray(&interpreter->stack);
@@ -286,6 +286,9 @@ static bool execVarDecl(Interpreter* interpreter, bool lng) {
Literal type = interpreter->literalCache.literals[typeIndex];
if (!declareScopeVariable(interpreter->scope, identifier, type)) {
printf("Can't redefine the variable \"");;
printLiteral(identifier);
printf("\"\n");
return false;
}
@@ -353,7 +356,7 @@ static void execInterpreter(Interpreter* interpreter) {
interpreter->scope = popScope(interpreter->scope);
break;
//TODO: type declarations
//TODO: custom type declarations
case OP_VAR_DECL:
case OP_VAR_DECL_LONG:
@@ -372,12 +375,22 @@ static void execInterpreter(Interpreter* interpreter) {
}
}
void runInterpreter(Interpreter* interpreter) {
void runInterpreter(Interpreter* interpreter, unsigned char* bytecode, int length) {
//prep the bytecode
interpreter->bytecode = bytecode;
interpreter->length = length;
interpreter->count = 0;
if (!interpreter->bytecode) {
printf(ERROR "Error: No valid bytecode given\n" RESET);
return;
}
//prep the literal cache
if (interpreter->literalCache.count > 0) {
freeLiteralArray(&interpreter->literalCache); //automatically inits
}
//header section
const unsigned char major = readByte(interpreter->bytecode, &interpreter->count);
const unsigned char minor = readByte(interpreter->bytecode, &interpreter->count);
@@ -515,7 +528,7 @@ void runInterpreter(Interpreter* interpreter) {
pushLiteralArray(&interpreter->literalCache, identifier);
if (command.verbose) {
printf("(identifier %s (%d))\n", AS_IDENTIFIER(identifier), identifier.as.identifier.hash);
printf("(identifier %s (hash: %x))\n", AS_IDENTIFIER(identifier), identifier.as.identifier.hash);
}
}
break;