diff --git a/docs/spec.md b/docs/spec.md index 4534e15..fa5ed4b 100644 --- a/docs/spec.md +++ b/docs/spec.md @@ -49,6 +49,7 @@ The following list of keywords cannot be used as names, due to their significanc * string * true * type +* typeas * typeof * var * while @@ -117,12 +118,11 @@ var t: type = int; var u: t = 42; ``` -To force a type instead of an array, use the `type` keyword: +To force a type instead of an array, use the `typeas` keyword: ``` -var a = [type type]; //array of types -var b = type [type]; //type of array of types -var c = [type]; //error! +var a = [typeas type]; //array of types +var b = typeas [type]; //type of array of types var d = b; //types can be re-assigned to other variables ``` diff --git a/scripts/test/imports-and-exports.toy b/scripts/test/imports-and-exports.toy index 0225dc1..e05bf03 100644 --- a/scripts/test/imports-and-exports.toy +++ b/scripts/test/imports-and-exports.toy @@ -42,7 +42,7 @@ { import t; - assert typeof t == type type, "type retention failed"; + assert typeof t == type, "type retention failed"; } print "All good"; diff --git a/scripts/test/types.toy b/scripts/test/types.toy index a2bd3d3..6ccc920 100644 --- a/scripts/test/types.toy +++ b/scripts/test/types.toy @@ -7,15 +7,15 @@ assert u == 42, "first-class types are screwing with values"; //differentiate by the "type" value -var v: type = type [int]; +var v: type = typeas [int]; var w = [int]; var x = v; assert w == [int], "defining an array of types failed"; -assert x == type [int], "re-assigning a type value failed"; +assert x == typeas [int], "re-assigning a type value failed"; //complex type -var complex: type = type [string : [int]]; +var complex: type = typeas [string : [int]]; var dict: complex = [ "first array": [1, 2, 3], "second array": [4, 5, 6], diff --git a/source/keyword_types.c b/source/keyword_types.c index 07bab6e..269ff25 100644 --- a/source/keyword_types.c +++ b/source/keyword_types.c @@ -33,6 +33,7 @@ KeywordType keywordTypes[] = { {TOKEN_PRINT, "print"}, {TOKEN_RETURN, "return"}, {TOKEN_TYPE, "type"}, + {TOKEN_TYPEAS, "typeas"}, {TOKEN_TYPEOF, "typeof"}, {TOKEN_VAR, "var"}, {TOKEN_WHILE, "while"}, diff --git a/source/parser.c b/source/parser.c index a8262ca..47751ec 100644 --- a/source/parser.c +++ b/source/parser.c @@ -120,11 +120,11 @@ static void parsePrecedence(Parser* parser, Node** nodeHandle, PrecedenceRule ru static Literal readTypeToLiteral(Parser* parser); //the expression rules -static Opcode forceType(Parser* parser, Node** nodeHandle) { +static Opcode typeAs(Parser* parser, Node** nodeHandle) { Literal literal = readTypeToLiteral(parser); if (!IS_TYPE(literal)) { - error(parser, parser->previous, "Expected type after 'type' keyword"); + error(parser, parser->previous, "Expected type after 'typeas' keyword"); freeLiteral(literal); return OP_EOF; } @@ -509,6 +509,17 @@ static Opcode atomic(Parser* parser, Node** nodeHandle) { return OP_EOF; } + case TOKEN_TYPE: { + if (match(parser, TOKEN_CONST)) { + emitNodeLiteral(nodeHandle, TO_TYPE_LITERAL(LITERAL_TYPE, true)); + } + else { + emitNodeLiteral(nodeHandle, TO_TYPE_LITERAL(LITERAL_TYPE, false)); + } + + return OP_EOF; + } + default: error(parser, parser->previous, "Unexpected token passed to atomic precedence rule"); return OP_EOF; @@ -736,7 +747,8 @@ ParseRule parseRules[] = { //must match the token types {NULL, NULL, PREC_NONE},// TOKEN_OF, {NULL, NULL, PREC_NONE},// TOKEN_PRINT, {NULL, NULL, PREC_NONE},// TOKEN_RETURN, - {forceType, NULL, PREC_PRIMARY},// TOKEN_TYPE, + {atomic, NULL, PREC_NONE},// TOKEN_TYPE, + {typeAs, NULL, PREC_PRIMARY},// TOKEN_TYPEAS, {typeOf, NULL, PREC_CALL},// TOKEN_TYPEOF, {NULL, NULL, PREC_NONE},// TOKEN_VAR, {NULL, NULL, PREC_NONE},// TOKEN_WHILE, diff --git a/source/token_types.h b/source/token_types.h index a04b591..76b3180 100644 --- a/source/token_types.h +++ b/source/token_types.h @@ -31,6 +31,7 @@ typedef enum TokenType { TOKEN_PRINT, TOKEN_RETURN, TOKEN_TYPE, + TOKEN_TYPEAS, TOKEN_TYPEOF, TOKEN_VAR, TOKEN_WHILE, diff --git a/test/sample_code.toy b/test/sample_code.toy index e6a2e4a..6cc47ed 100644 --- a/test/sample_code.toy +++ b/test/sample_code.toy @@ -1,8 +1,8 @@ -var complex: type = type [string: [int]]; -var deep: type = type [[[ int ]]]; +var complex: type = typeas [string: [int]]; +var deep: type = typeas [[[ int ]]];