diff --git a/docs/TODO.txt b/docs/TODO.txt index ab42494..38a5656 100644 --- a/docs/TODO.txt +++ b/docs/TODO.txt @@ -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 diff --git a/source/compiler.c b/source/compiler.c index 9133480..d9f94fe 100644 --- a/source/compiler.c +++ b/source/compiler.c @@ -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 diff --git a/source/parser.c b/source/parser.c index 5642ad5..f45511b 100644 --- a/source/parser.c +++ b/source/parser.c @@ -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 diff --git a/test/type-type.toy b/test/type-type.toy deleted file mode 100644 index 2350382..0000000 --- a/test/type-type.toy +++ /dev/null @@ -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"; diff --git a/test/types.toy b/test/types.toy new file mode 100644 index 0000000..767aad9 --- /dev/null +++ b/test/types.toy @@ -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";