diff --git a/scripts/small.toy b/scripts/small.toy index fd40910..663da06 100644 --- a/scripts/small.toy +++ b/scripts/small.toy @@ -2,3 +2,4 @@ +var dict : [string, int] = ["hello" : 1, "world" : 2]; \ No newline at end of file diff --git a/source/interpreter.c b/source/interpreter.c index d62aa4c..2555a4f 100644 --- a/source/interpreter.c +++ b/source/interpreter.c @@ -304,7 +304,7 @@ 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 \"");; + printf("Can't redefine the variable \""); printLiteral(identifier); printf("\"\n"); return false; @@ -325,14 +325,14 @@ static bool execVarAssign(Interpreter* interpreter) { Literal lhs = popLiteralArray(&interpreter->stack); if (!IS_IDENTIFIER(lhs)) { - printf("Can't assign to a non-variable \"");; + printf("Can't assign to a non-variable \""); printLiteral(lhs); printf("\"\n"); return false; } if (!isDelcaredScopeVariable(interpreter->scope, lhs)) { - printf("Undeclared variable \"");; + printf("Undeclared variable \""); printLiteral(lhs); printf("\"\n"); return false; diff --git a/source/scope.c b/source/scope.c index 65c11ce..7e2a8f1 100644 --- a/source/scope.c +++ b/source/scope.c @@ -59,11 +59,11 @@ static bool checkType(Literal typeLiteral, Literal value) { for (int i = 0; i < AS_DICTIONARY(value)->capacity; i++) { //only assigned and non-tombstoned keys if (!IS_NULL(AS_DICTIONARY(value)->entries[i].key)) { - if (checkType(((Literal*)(AS_TYPE(typeLiteral).subtypes))[0], AS_DICTIONARY(value)->entries[i].key)) { + if (!checkType(((Literal*)(AS_TYPE(typeLiteral).subtypes))[0], AS_DICTIONARY(value)->entries[i].key)) { return false; } - if (checkType(((Literal*)(AS_TYPE(typeLiteral).subtypes))[1], AS_DICTIONARY(value)->entries[i].value)) { + if (!checkType(((Literal*)(AS_TYPE(typeLiteral).subtypes))[1], AS_DICTIONARY(value)->entries[i].value)) { return false; } } @@ -112,7 +112,16 @@ bool declareScopeVariable(Scope* scope, Literal key, Literal type) { } bool isDelcaredScopeVariable(Scope* scope, Literal key) { - return existsLiteralDictionary(&scope->variables, key); + if (scope == NULL) { + return false; + } + + //if it's not in this scope, keep searching up the chain + if (!existsLiteralDictionary(&scope->variables, key)) { + return isDelcaredScopeVariable(scope->ancestor, key); + } + + return true; } //return false if undefined, or can't be assigned