From 4137b7f0573109cb2cf6a2230335ac3aba1afd55 Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Wed, 7 Sep 2022 14:47:57 +0100 Subject: [PATCH] switched typeof for oftype, switched typeas for astype --- docs/TODO.txt | 2 +- docs/spec.md | 10 +++++----- scripts/example.toy | 2 +- scripts/small.toy | 10 +++++----- scripts/test/imports-and-exports.toy | 4 ++-- scripts/test/types.toy | 6 +++--- source/keyword_types.c | 4 ++-- source/parser.c | 10 +++++----- source/token_types.h | 4 ++-- test/sample_code.toy | 4 ++-- 10 files changed, 28 insertions(+), 28 deletions(-) diff --git a/docs/TODO.txt b/docs/TODO.txt index ec13803..f58d02a 100644 --- a/docs/TODO.txt +++ b/docs/TODO.txt @@ -31,7 +31,7 @@ DONE: are compounds shallow or deep copies? Deep copies DONE: third output stream, for lexer/parser/compiler/interpreter errors DONE: Assertion-based test scripts DONE: Import/export keywords -DONE: A way to check the type of a variable (typeOf keyword) +DONE: A way to check the type of a variable (oftype keyword) DONE: slice and dot notation around the builtin _index and _dot functions diff --git a/docs/spec.md b/docs/spec.md index fa5ed4b..82a116e 100644 --- a/docs/spec.md +++ b/docs/spec.md @@ -24,6 +24,7 @@ The following list of keywords cannot be used as names, due to their significanc * any * as +* astype * assert * bool * break @@ -44,13 +45,12 @@ The following list of keywords cannot be used as names, due to their significanc * int * null * of +* oftype * print * return * string * true * type -* typeas -* typeof * var * while @@ -118,11 +118,11 @@ var t: type = int; var u: t = 42; ``` -To force a type instead of an array, use the `typeas` keyword: +To force a type instead of an array, use the `astype` keyword: ``` -var a = [typeas type]; //array of types -var b = typeas [type]; //type of array of types +var a = [type]; //array containing the type "type" +var b = astype [type]; //type of array of types var d = b; //types can be re-assigned to other variables ``` diff --git a/scripts/example.toy b/scripts/example.toy index 1efe9b4..1489908 100644 --- a/scripts/example.toy +++ b/scripts/example.toy @@ -72,7 +72,7 @@ print [[1, 2, 3], [4, 5, 6], [7, 8, 9]]; var x = 31; var y : int = 42; var arr : [int] = [1, 2, 3, 42]; -var dict : [string, int] = ["hello": 1, "world":2]; +var dict : [string:int] = ["hello": 1, "world":2]; //printing expressions print x; diff --git a/scripts/small.toy b/scripts/small.toy index f1a3d08..b5921ba 100644 --- a/scripts/small.toy +++ b/scripts/small.toy @@ -2,14 +2,14 @@ -var t: type = typeas [int]; -var u: type = typeas [t]; +var t: type = astype [int]; +var u: type = astype [t]; var a: u; -t = typeas [float]; //redefnition +t = astype [float]; //redefnition var b: u; -print typeof a; //<[]> -print typeof b; //<[]> \ No newline at end of file +print oftype a; //<[]> +print oftype b; //<[]> \ No newline at end of file diff --git a/scripts/test/imports-and-exports.toy b/scripts/test/imports-and-exports.toy index e05bf03..f759d10 100644 --- a/scripts/test/imports-and-exports.toy +++ b/scripts/test/imports-and-exports.toy @@ -32,7 +32,7 @@ assert func() == 69, "import/export of functions failed"; } -//test that variables retain their types with the typeof keyword +//test that variables retain their types with the oftype keyword { var t: type = int; @@ -42,7 +42,7 @@ { import t; - assert typeof t == type, "type retention failed"; + assert oftype t == type, "type retention failed"; } print "All good"; diff --git a/scripts/test/types.toy b/scripts/test/types.toy index 6ccc920..3f7b281 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 = typeas [int]; +var v: type = astype [int]; var w = [int]; var x = v; assert w == [int], "defining an array of types failed"; -assert x == typeas [int], "re-assigning a type value failed"; +assert x == astype [int], "re-assigning a type value failed"; //complex type -var complex: type = typeas [string : [int]]; +var complex: type = astype [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 269ff25..e047487 100644 --- a/source/keyword_types.c +++ b/source/keyword_types.c @@ -33,8 +33,8 @@ KeywordType keywordTypes[] = { {TOKEN_PRINT, "print"}, {TOKEN_RETURN, "return"}, {TOKEN_TYPE, "type"}, - {TOKEN_TYPEAS, "typeas"}, - {TOKEN_TYPEOF, "typeof"}, + {TOKEN_ASTYPE, "astype"}, + {TOKEN_OFTYPE, "oftype"}, {TOKEN_VAR, "var"}, {TOKEN_WHILE, "while"}, diff --git a/source/parser.c b/source/parser.c index d052159..225d8d8 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 typeAs(Parser* parser, Node** nodeHandle) { +static Opcode asType(Parser* parser, Node** nodeHandle) { Literal literal = readTypeToLiteral(parser); if (!IS_TYPE(literal)) { - error(parser, parser->previous, "Expected type after 'typeas' keyword"); + error(parser, parser->previous, "Expected type after 'astype' keyword"); freeLiteral(literal); return OP_EOF; } @@ -136,7 +136,7 @@ static Opcode typeAs(Parser* parser, Node** nodeHandle) { return OP_EOF; } -static Opcode typeOf(Parser* parser, Node** nodeHandle) { +static Opcode ofType(Parser* parser, Node** nodeHandle) { Node* rhs = NULL; parsePrecedence(parser, &rhs, PREC_TERNARY); emitNodeUnary(nodeHandle, OP_TYPE_OF, rhs); @@ -820,8 +820,8 @@ ParseRule parseRules[] = { //must match the token types {NULL, NULL, PREC_NONE},// TOKEN_PRINT, {NULL, NULL, PREC_NONE},// TOKEN_RETURN, {atomic, NULL, PREC_NONE},// TOKEN_TYPE, - {typeAs, NULL, PREC_PRIMARY},// TOKEN_TYPEAS, - {typeOf, NULL, PREC_CALL},// TOKEN_TYPEOF, + {asType, NULL, PREC_PRIMARY},// TOKEN_ASTYPE, + {ofType, NULL, PREC_CALL},// TOKEN_OFTYPE, {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 76b3180..2249a55 100644 --- a/source/token_types.h +++ b/source/token_types.h @@ -31,8 +31,8 @@ typedef enum TokenType { TOKEN_PRINT, TOKEN_RETURN, TOKEN_TYPE, - TOKEN_TYPEAS, - TOKEN_TYPEOF, + TOKEN_ASTYPE, + TOKEN_OFTYPE, TOKEN_VAR, TOKEN_WHILE, diff --git a/test/sample_code.toy b/test/sample_code.toy index 6cc47ed..9afff30 100644 --- a/test/sample_code.toy +++ b/test/sample_code.toy @@ -1,8 +1,8 @@ -var complex: type = typeas [string: [int]]; -var deep: type = typeas [[[ int ]]]; +var complex: type = astype [string: [int]]; +var deep: type = astype [[[ int ]]];