mirror of
https://github.com/krgamestudios/Toy.git
synced 2026-04-15 14:54:07 +10:00
Store complex types in variables
This commit is contained in:
@@ -12,11 +12,21 @@ DONE: break and continue statements
|
||||
DONE: truthiness rethink
|
||||
DONE: string concat with the + operator
|
||||
DONE: increment & decrement operators
|
||||
DONE: store compound types in variables
|
||||
|
||||
|
||||
TODO: functions take a number of parameters
|
||||
TODO: functions can return any number of values
|
||||
TODO: function arguments can have specified types
|
||||
TODO: function returns can have specified types
|
||||
TODO: functions are invoked by calling thier names
|
||||
TODO: functions are first-class citizens
|
||||
TODO: functions last argument can be a rest parameter
|
||||
|
||||
TODO: && and || operators
|
||||
TODO: A way to check the type of a variable (typeOf keyword)
|
||||
TODO: a = b = c = 1; ?
|
||||
TODO: are compounds shallow or deep copies?
|
||||
TODO: functions, and all of their features
|
||||
TODO: Assertion-based test scripts
|
||||
TODO: standard library
|
||||
TODO: external runner library
|
||||
|
||||
@@ -157,9 +157,16 @@ static int writeLiteralTypeToCache(LiteralArray* literalCache, Literal literal)
|
||||
static int writeLiteralToCompiler(Compiler* compiler, Literal literal) {
|
||||
//get the index
|
||||
int index = findLiteralIndex(&compiler->literalCache, literal);
|
||||
|
||||
if (index < 0) {
|
||||
if (IS_TYPE(literal)) {
|
||||
//check for the type literal as value
|
||||
index = writeLiteralTypeToCache(&compiler->literalCache, literal);
|
||||
}
|
||||
else {
|
||||
index = pushLiteralArray(&compiler->literalCache, literal);
|
||||
}
|
||||
}
|
||||
|
||||
//push the literal to the bytecode
|
||||
if (index >= 256) {
|
||||
|
||||
@@ -1231,8 +1231,15 @@ static void varDecl(Parser* parser, Node** nodeHandle) {
|
||||
//variable definition is an expression
|
||||
Node* expressionNode = NULL;
|
||||
if (match(parser, TOKEN_ASSIGN)) {
|
||||
//BUGFIX: if reading into a "type" variable, expect a type value
|
||||
if (AS_TYPE(typeLiteral).typeOf == LITERAL_TYPE) { //This may cause issues when reading function returns
|
||||
Literal val = readTypeToLiteral(parser);
|
||||
|
||||
emitNodeLiteral(&expressionNode, val);
|
||||
} else {
|
||||
expression(parser, &expressionNode);
|
||||
}
|
||||
}
|
||||
else {
|
||||
//values are null by default
|
||||
emitNodeLiteral(&expressionNode, TO_NULL_LITERAL);
|
||||
|
||||
@@ -1,7 +0,0 @@
|
||||
var t: type = int;
|
||||
var u: t = 42;
|
||||
|
||||
assert t == int, "types are not first class";
|
||||
assert u == 42, "first-class types are screwing with values";
|
||||
|
||||
print "All good";
|
||||
24
test/types.toy
Normal file
24
test/types.toy
Normal file
@@ -0,0 +1,24 @@
|
||||
//basic types
|
||||
var t: type = int;
|
||||
var u: t = 42;
|
||||
|
||||
assert t == int, "types are not first class";
|
||||
assert u == 42, "first-class types are screwing with values";
|
||||
|
||||
|
||||
//differentiate by the "type" value
|
||||
var v: type = [int]; //TODO: still can't check for this
|
||||
var w = [int];
|
||||
|
||||
assert w == [int], "defining an array of types failed";
|
||||
|
||||
|
||||
//complex type
|
||||
var complex: type = [string, [int]];
|
||||
var dict: complex = [
|
||||
"first array": [1, 2, 3],
|
||||
"second array": [4, 5, 6],
|
||||
"third array": [7, 8, 9]
|
||||
];
|
||||
|
||||
print "All good";
|
||||
Reference in New Issue
Block a user