Store complex types in variables

This commit is contained in:
2022-08-22 13:28:44 +01:00
parent 45920f763c
commit 300f8e382b
5 changed files with 51 additions and 10 deletions

View File

@@ -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

View File

@@ -157,8 +157,15 @@ 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) {
index = pushLiteralArray(&compiler->literalCache, literal);
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

View File

@@ -1231,7 +1231,14 @@ static void varDecl(Parser* parser, Node** nodeHandle) {
//variable definition is an expression
Node* expressionNode = NULL;
if (match(parser, TOKEN_ASSIGN)) {
expression(parser, &expressionNode);
//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

View File

@@ -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
View 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";