mirror of
https://github.com/krgamestudios/Toy.git
synced 2026-04-15 14:54:07 +10:00
Reviewed the spec, fixed some bugs
This commit is contained in:
@@ -72,6 +72,63 @@ bool parseIdentifierToValue(Interpreter* interpreter, Literal* literalPtr) {
|
||||
return true;
|
||||
}
|
||||
|
||||
void parseCompoundToPureValues(Interpreter* interpreter, Literal* literalPtr) {
|
||||
parseIdentifierToValue(interpreter, literalPtr);
|
||||
|
||||
//parse out an array
|
||||
if (IS_ARRAY(*literalPtr)) {
|
||||
for (int i = 0; i < AS_ARRAY(*literalPtr)->count; i++) {
|
||||
Literal index = TO_INTEGER_LITERAL(i);
|
||||
Literal entry = getLiteralArray(AS_ARRAY(*literalPtr), index);
|
||||
|
||||
if (IS_IDENTIFIER( entry )) {
|
||||
Literal idn = entry;
|
||||
parseCompoundToPureValues(interpreter, &entry);
|
||||
|
||||
setLiteralArray(AS_ARRAY(*literalPtr), index, entry);
|
||||
|
||||
freeLiteral(idn);
|
||||
}
|
||||
|
||||
freeLiteral(index);
|
||||
freeLiteral(entry);
|
||||
}
|
||||
}
|
||||
|
||||
//parse out a dictionary
|
||||
if (IS_DICTIONARY(*literalPtr)) {
|
||||
LiteralDictionary* ret = ALLOCATE(LiteralDictionary, 1);
|
||||
initLiteralDictionary(ret);
|
||||
|
||||
for (int i = 0; i < AS_DICTIONARY(*literalPtr)->capacity; i++) {
|
||||
if ( IS_NULL(AS_DICTIONARY(*literalPtr)->entries[i].key) ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
Literal key = TO_NULL_LITERAL;
|
||||
Literal value = TO_NULL_LITERAL;
|
||||
|
||||
key = copyLiteral(AS_DICTIONARY(*literalPtr)->entries[i].key);
|
||||
value = copyLiteral(AS_DICTIONARY(*literalPtr)->entries[i].value);
|
||||
|
||||
//
|
||||
if (IS_IDENTIFIER( key ) || IS_IDENTIFIER(value)) {
|
||||
parseCompoundToPureValues(interpreter, &key);
|
||||
parseCompoundToPureValues(interpreter, &value);
|
||||
}
|
||||
|
||||
setLiteralDictionary(ret, key, value);
|
||||
|
||||
//
|
||||
freeLiteral(key);
|
||||
freeLiteral(value);
|
||||
}
|
||||
|
||||
freeLiteralDictionary(AS_DICTIONARY(*literalPtr));
|
||||
*literalPtr = TO_DICTIONARY_LITERAL(ret);
|
||||
}
|
||||
}
|
||||
|
||||
//utilities for the host program
|
||||
void setInterpreterPrint(Interpreter* interpreter, PrintFn printOutput) {
|
||||
interpreter->printOutput = printOutput;
|
||||
@@ -479,6 +536,10 @@ static bool execVarDecl(Interpreter* interpreter, bool lng) {
|
||||
freeLiteral(idn);
|
||||
}
|
||||
|
||||
if (IS_ARRAY(val) || IS_DICTIONARY(val)) {
|
||||
parseCompoundToPureValues(interpreter, &val);
|
||||
}
|
||||
|
||||
if (!IS_NULL(val) && !setScopeVariable(interpreter->scope, identifier, val, false)) {
|
||||
interpreter->errorOutput("Incorrect type assigned to variable \"");
|
||||
printLiteralCustom(identifier, interpreter->errorOutput);
|
||||
@@ -549,6 +610,10 @@ static bool execVarAssign(Interpreter* interpreter) {
|
||||
freeLiteral(idn);
|
||||
}
|
||||
|
||||
if (IS_ARRAY(rhs) || IS_DICTIONARY(rhs)) {
|
||||
parseCompoundToPureValues(interpreter, &rhs);
|
||||
}
|
||||
|
||||
if (!IS_IDENTIFIER(lhs)) {
|
||||
interpreter->errorOutput("Can't assign to a non-variable \"");
|
||||
printLiteralCustom(lhs, interpreter->errorOutput);
|
||||
@@ -1305,6 +1370,11 @@ static bool execFnReturn(Interpreter* interpreter) {
|
||||
parseIdentifierToValue(interpreter, &lit);
|
||||
freeLiteral(idn);
|
||||
}
|
||||
|
||||
if (IS_ARRAY(lit) || IS_DICTIONARY(lit)) {
|
||||
parseCompoundToPureValues(interpreter, &lit);
|
||||
}
|
||||
|
||||
pushLiteralArray(&returns, lit); //reverses the order
|
||||
freeLiteral(lit);
|
||||
}
|
||||
|
||||
@@ -1471,7 +1471,9 @@ static Literal readTypeToLiteral(Parser* parser) {
|
||||
}
|
||||
break;
|
||||
|
||||
//TODO: function?
|
||||
case TOKEN_FUNCTION:
|
||||
AS_TYPE(literal).typeOf = LITERAL_FUNCTION;
|
||||
break;
|
||||
|
||||
case TOKEN_ANY:
|
||||
AS_TYPE(literal).typeOf = LITERAL_ANY;
|
||||
|
||||
@@ -97,7 +97,7 @@ static bool checkType(Literal typeLiteral, Literal original, Literal value, bool
|
||||
return false;
|
||||
}
|
||||
|
||||
//if null, assume it's a new entry
|
||||
//if null, assume it's a new entry to a parent
|
||||
if (IS_NULL(original)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user