From bd4ab2aa0401211e80e27706aa5e92514765698a Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Mon, 3 Oct 2022 21:32:09 +0100 Subject: [PATCH] Added the opaque keyword --- scripts/test/opaque-data-type.toy | 3 +-- source/interpreter.c | 2 ++ source/keyword_types.c | 1 + source/parser.c | 5 +++++ source/token_types.h | 2 +- 5 files changed, 10 insertions(+), 3 deletions(-) diff --git a/scripts/test/opaque-data-type.toy b/scripts/test/opaque-data-type.toy index b35aa6f..f0d4d52 100644 --- a/scripts/test/opaque-data-type.toy +++ b/scripts/test/opaque-data-type.toy @@ -1,6 +1,5 @@ //test the opaque data type works -var o = produce(); +var o: opaque = produce(); consume(o); - diff --git a/source/interpreter.c b/source/interpreter.c index 8151b55..5fc777f 100644 --- a/source/interpreter.c +++ b/source/interpreter.c @@ -570,6 +570,8 @@ static bool execVarDecl(Interpreter* interpreter, bool lng) { parseCompoundToPureValues(interpreter, &val); } + //TODO: could restrict opaque data to only opaque variables + //BUGFIX: allow easy coercion on decl if (AS_TYPE(type).typeOf == LITERAL_FLOAT && IS_INTEGER(val)) { val = TO_FLOAT_LITERAL(AS_INTEGER(val)); diff --git a/source/keyword_types.c b/source/keyword_types.c index d024b73..e9a30d0 100644 --- a/source/keyword_types.c +++ b/source/keyword_types.c @@ -12,6 +12,7 @@ KeywordType keywordTypes[] = { {TOKEN_FLOAT, "float"}, {TOKEN_STRING, "string"}, {TOKEN_FUNCTION, "fn"}, + {TOKEN_OPAQUE, "opaque"}, {TOKEN_ANY, "any"}, //other keywords diff --git a/source/parser.c b/source/parser.c index a026e55..97d6b3f 100644 --- a/source/parser.c +++ b/source/parser.c @@ -816,6 +816,7 @@ ParseRule parseRules[] = { //must match the token types {NULL, NULL, PREC_NONE},// TOKEN_ARRAY, {NULL, NULL, PREC_NONE},// TOKEN_DICTIONARY, {NULL, NULL, PREC_NONE},// TOKEN_FUNCTION, + {NULL, NULL, PREC_NONE},// TOKEN_OPAQUE, {NULL, NULL, PREC_NONE},// TOKEN_ANY, //keywords and reserved words @@ -1486,6 +1487,10 @@ static Literal readTypeToLiteral(Parser* parser) { AS_TYPE(literal).typeOf = LITERAL_FUNCTION; break; + case TOKEN_OPAQUE: + AS_TYPE(literal).typeOf = LITERAL_OPAQUE; + break; + case TOKEN_ANY: AS_TYPE(literal).typeOf = LITERAL_ANY; break; diff --git a/source/token_types.h b/source/token_types.h index 24c5090..7816dd7 100644 --- a/source/token_types.h +++ b/source/token_types.h @@ -10,6 +10,7 @@ typedef enum TokenType { TOKEN_ARRAY, TOKEN_DICTIONARY, TOKEN_FUNCTION, + TOKEN_OPAQUE, TOKEN_ANY, //keywords and reserved words @@ -89,4 +90,3 @@ typedef enum TokenType { TOKEN_ERROR, TOKEN_EOF, } TokenType; -