mirror of
https://github.com/krgamestudios/Toy.git
synced 2026-04-15 23:04:08 +10:00
Added the typeas keyword to avoid ambiguity
This commit is contained in:
@@ -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
|
||||
```
|
||||
|
||||
@@ -42,7 +42,7 @@
|
||||
{
|
||||
import t;
|
||||
|
||||
assert typeof t == type type, "type retention failed";
|
||||
assert typeof t == type, "type retention failed";
|
||||
}
|
||||
|
||||
print "All good";
|
||||
|
||||
@@ -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],
|
||||
|
||||
@@ -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"},
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -31,6 +31,7 @@ typedef enum TokenType {
|
||||
TOKEN_PRINT,
|
||||
TOKEN_RETURN,
|
||||
TOKEN_TYPE,
|
||||
TOKEN_TYPEAS,
|
||||
TOKEN_TYPEOF,
|
||||
TOKEN_VAR,
|
||||
TOKEN_WHILE,
|
||||
|
||||
@@ -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 ]]];
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user