mirror of
https://github.com/krgamestudios/Toy.git
synced 2026-04-15 23:04:08 +10:00
switched typeof for oftype, switched typeas for astype
This commit is contained in:
@@ -31,7 +31,7 @@ DONE: are compounds shallow or deep copies? Deep copies
|
|||||||
DONE: third output stream, for lexer/parser/compiler/interpreter errors
|
DONE: third output stream, for lexer/parser/compiler/interpreter errors
|
||||||
DONE: Assertion-based test scripts
|
DONE: Assertion-based test scripts
|
||||||
DONE: Import/export keywords
|
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
|
DONE: slice and dot notation around the builtin _index and _dot functions
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
10
docs/spec.md
10
docs/spec.md
@@ -24,6 +24,7 @@ The following list of keywords cannot be used as names, due to their significanc
|
|||||||
|
|
||||||
* any
|
* any
|
||||||
* as
|
* as
|
||||||
|
* astype
|
||||||
* assert
|
* assert
|
||||||
* bool
|
* bool
|
||||||
* break
|
* break
|
||||||
@@ -44,13 +45,12 @@ The following list of keywords cannot be used as names, due to their significanc
|
|||||||
* int
|
* int
|
||||||
* null
|
* null
|
||||||
* of
|
* of
|
||||||
|
* oftype
|
||||||
* print
|
* print
|
||||||
* return
|
* return
|
||||||
* string
|
* string
|
||||||
* true
|
* true
|
||||||
* type
|
* type
|
||||||
* typeas
|
|
||||||
* typeof
|
|
||||||
* var
|
* var
|
||||||
* while
|
* while
|
||||||
|
|
||||||
@@ -118,11 +118,11 @@ var t: type = int;
|
|||||||
var u: t = 42;
|
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 a = [type]; //array containing the type "type"
|
||||||
var b = typeas [type]; //type of array of types
|
var b = astype [type]; //type of array of types
|
||||||
|
|
||||||
var d = b; //types can be re-assigned to other variables
|
var d = b; //types can be re-assigned to other variables
|
||||||
```
|
```
|
||||||
|
|||||||
@@ -72,7 +72,7 @@ print [[1, 2, 3], [4, 5, 6], [7, 8, 9]];
|
|||||||
var x = 31;
|
var x = 31;
|
||||||
var y : int = 42;
|
var y : int = 42;
|
||||||
var arr : [int] = [1, 2, 3, 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
|
//printing expressions
|
||||||
print x;
|
print x;
|
||||||
|
|||||||
@@ -2,14 +2,14 @@
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
var t: type = typeas [int];
|
var t: type = astype [int];
|
||||||
var u: type = typeas [t];
|
var u: type = astype [t];
|
||||||
|
|
||||||
var a: u;
|
var a: u;
|
||||||
|
|
||||||
t = typeas [float]; //redefnition
|
t = astype [float]; //redefnition
|
||||||
|
|
||||||
var b: u;
|
var b: u;
|
||||||
|
|
||||||
print typeof a; //<[<int>]>
|
print oftype a; //<[<int>]>
|
||||||
print typeof b; //<[<float>]>
|
print oftype b; //<[<float>]>
|
||||||
@@ -32,7 +32,7 @@
|
|||||||
assert func() == 69, "import/export of functions failed";
|
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;
|
var t: type = int;
|
||||||
|
|
||||||
@@ -42,7 +42,7 @@
|
|||||||
{
|
{
|
||||||
import t;
|
import t;
|
||||||
|
|
||||||
assert typeof t == type, "type retention failed";
|
assert oftype t == type, "type retention failed";
|
||||||
}
|
}
|
||||||
|
|
||||||
print "All good";
|
print "All good";
|
||||||
|
|||||||
@@ -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 = typeas [int];
|
var v: type = astype [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 == typeas [int], "re-assigning a type value failed";
|
assert x == astype [int], "re-assigning a type value failed";
|
||||||
|
|
||||||
//complex type
|
//complex type
|
||||||
var complex: type = typeas [string : [int]];
|
var complex: type = astype [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],
|
||||||
|
|||||||
@@ -33,8 +33,8 @@ KeywordType keywordTypes[] = {
|
|||||||
{TOKEN_PRINT, "print"},
|
{TOKEN_PRINT, "print"},
|
||||||
{TOKEN_RETURN, "return"},
|
{TOKEN_RETURN, "return"},
|
||||||
{TOKEN_TYPE, "type"},
|
{TOKEN_TYPE, "type"},
|
||||||
{TOKEN_TYPEAS, "typeas"},
|
{TOKEN_ASTYPE, "astype"},
|
||||||
{TOKEN_TYPEOF, "typeof"},
|
{TOKEN_OFTYPE, "oftype"},
|
||||||
{TOKEN_VAR, "var"},
|
{TOKEN_VAR, "var"},
|
||||||
{TOKEN_WHILE, "while"},
|
{TOKEN_WHILE, "while"},
|
||||||
|
|
||||||
|
|||||||
@@ -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 typeAs(Parser* parser, Node** nodeHandle) {
|
static Opcode asType(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 'typeas' keyword");
|
error(parser, parser->previous, "Expected type after 'astype' keyword");
|
||||||
freeLiteral(literal);
|
freeLiteral(literal);
|
||||||
return OP_EOF;
|
return OP_EOF;
|
||||||
}
|
}
|
||||||
@@ -136,7 +136,7 @@ static Opcode typeAs(Parser* parser, Node** nodeHandle) {
|
|||||||
return OP_EOF;
|
return OP_EOF;
|
||||||
}
|
}
|
||||||
|
|
||||||
static Opcode typeOf(Parser* parser, Node** nodeHandle) {
|
static Opcode ofType(Parser* parser, Node** nodeHandle) {
|
||||||
Node* rhs = NULL;
|
Node* rhs = NULL;
|
||||||
parsePrecedence(parser, &rhs, PREC_TERNARY);
|
parsePrecedence(parser, &rhs, PREC_TERNARY);
|
||||||
emitNodeUnary(nodeHandle, OP_TYPE_OF, rhs);
|
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_PRINT,
|
||||||
{NULL, NULL, PREC_NONE},// TOKEN_RETURN,
|
{NULL, NULL, PREC_NONE},// TOKEN_RETURN,
|
||||||
{atomic, NULL, PREC_NONE},// TOKEN_TYPE,
|
{atomic, NULL, PREC_NONE},// TOKEN_TYPE,
|
||||||
{typeAs, NULL, PREC_PRIMARY},// TOKEN_TYPEAS,
|
{asType, NULL, PREC_PRIMARY},// TOKEN_ASTYPE,
|
||||||
{typeOf, NULL, PREC_CALL},// TOKEN_TYPEOF,
|
{ofType, NULL, PREC_CALL},// TOKEN_OFTYPE,
|
||||||
{NULL, NULL, PREC_NONE},// TOKEN_VAR,
|
{NULL, NULL, PREC_NONE},// TOKEN_VAR,
|
||||||
{NULL, NULL, PREC_NONE},// TOKEN_WHILE,
|
{NULL, NULL, PREC_NONE},// TOKEN_WHILE,
|
||||||
|
|
||||||
|
|||||||
@@ -31,8 +31,8 @@ typedef enum TokenType {
|
|||||||
TOKEN_PRINT,
|
TOKEN_PRINT,
|
||||||
TOKEN_RETURN,
|
TOKEN_RETURN,
|
||||||
TOKEN_TYPE,
|
TOKEN_TYPE,
|
||||||
TOKEN_TYPEAS,
|
TOKEN_ASTYPE,
|
||||||
TOKEN_TYPEOF,
|
TOKEN_OFTYPE,
|
||||||
TOKEN_VAR,
|
TOKEN_VAR,
|
||||||
TOKEN_WHILE,
|
TOKEN_WHILE,
|
||||||
|
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
var complex: type = typeas [string: [int]];
|
var complex: type = astype [string: [int]];
|
||||||
var deep: type = typeas [[[ int ]]];
|
var deep: type = astype [[[ int ]]];
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user