Added the typeas keyword to avoid ambiguity

This commit is contained in:
2022-09-05 12:24:42 +01:00
parent 3d8871abe1
commit 82c03ecb33
7 changed files with 27 additions and 13 deletions

View File

@@ -49,6 +49,7 @@ The following list of keywords cannot be used as names, due to their significanc
* string * string
* true * true
* type * type
* typeas
* typeof * typeof
* var * var
* while * while
@@ -117,12 +118,11 @@ var t: type = int;
var u: t = 42; 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 a = [typeas type]; //array of types
var b = type [type]; //type of array of types var b = typeas [type]; //type of array of types
var c = [type]; //error!
var d = b; //types can be re-assigned to other variables var d = b; //types can be re-assigned to other variables
``` ```

View File

@@ -42,7 +42,7 @@
{ {
import t; import t;
assert typeof t == type type, "type retention failed"; assert typeof t == type, "type retention failed";
} }
print "All good"; print "All good";

View File

@@ -7,15 +7,15 @@ assert u == 42, "first-class types are screwing with values";
//differentiate by the "type" value //differentiate by the "type" value
var v: type = type [int]; var v: type = typeas [int];
var w = [int]; var w = [int];
var x = v; var x = v;
assert w == [int], "defining an array of types failed"; 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 //complex type
var complex: type = type [string : [int]]; var complex: type = typeas [string : [int]];
var dict: complex = [ var dict: complex = [
"first array": [1, 2, 3], "first array": [1, 2, 3],
"second array": [4, 5, 6], "second array": [4, 5, 6],

View File

@@ -33,6 +33,7 @@ KeywordType keywordTypes[] = {
{TOKEN_PRINT, "print"}, {TOKEN_PRINT, "print"},
{TOKEN_RETURN, "return"}, {TOKEN_RETURN, "return"},
{TOKEN_TYPE, "type"}, {TOKEN_TYPE, "type"},
{TOKEN_TYPEAS, "typeas"},
{TOKEN_TYPEOF, "typeof"}, {TOKEN_TYPEOF, "typeof"},
{TOKEN_VAR, "var"}, {TOKEN_VAR, "var"},
{TOKEN_WHILE, "while"}, {TOKEN_WHILE, "while"},

View File

@@ -120,11 +120,11 @@ static void parsePrecedence(Parser* parser, Node** nodeHandle, PrecedenceRule ru
static Literal readTypeToLiteral(Parser* parser); static Literal readTypeToLiteral(Parser* parser);
//the expression rules //the expression rules
static Opcode forceType(Parser* parser, Node** nodeHandle) { static Opcode typeAs(Parser* parser, Node** nodeHandle) {
Literal literal = readTypeToLiteral(parser); Literal literal = readTypeToLiteral(parser);
if (!IS_TYPE(literal)) { if (!IS_TYPE(literal)) {
error(parser, parser->previous, "Expected type after 'type' keyword"); error(parser, parser->previous, "Expected type after 'typeas' keyword");
freeLiteral(literal); freeLiteral(literal);
return OP_EOF; return OP_EOF;
} }
@@ -509,6 +509,17 @@ static Opcode atomic(Parser* parser, Node** nodeHandle) {
return OP_EOF; 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: default:
error(parser, parser->previous, "Unexpected token passed to atomic precedence rule"); error(parser, parser->previous, "Unexpected token passed to atomic precedence rule");
return OP_EOF; 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_OF,
{NULL, NULL, PREC_NONE},// TOKEN_PRINT, {NULL, NULL, PREC_NONE},// TOKEN_PRINT,
{NULL, NULL, PREC_NONE},// TOKEN_RETURN, {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, {typeOf, NULL, PREC_CALL},// TOKEN_TYPEOF,
{NULL, NULL, PREC_NONE},// TOKEN_VAR, {NULL, NULL, PREC_NONE},// TOKEN_VAR,
{NULL, NULL, PREC_NONE},// TOKEN_WHILE, {NULL, NULL, PREC_NONE},// TOKEN_WHILE,

View File

@@ -31,6 +31,7 @@ typedef enum TokenType {
TOKEN_PRINT, TOKEN_PRINT,
TOKEN_RETURN, TOKEN_RETURN,
TOKEN_TYPE, TOKEN_TYPE,
TOKEN_TYPEAS,
TOKEN_TYPEOF, TOKEN_TYPEOF,
TOKEN_VAR, TOKEN_VAR,
TOKEN_WHILE, TOKEN_WHILE,

View File

@@ -1,8 +1,8 @@
var complex: type = type [string: [int]]; var complex: type = typeas [string: [int]];
var deep: type = type [[[ int ]]]; var deep: type = typeas [[[ int ]]];