diff --git a/scripts/example.toy b/scripts/example.toy index 3676895..2404148 100644 --- a/scripts/example.toy +++ b/scripts/example.toy @@ -37,6 +37,22 @@ print 1.0 / 2.0; } print "Back to the outer scope."; +//test scope will delegate to higher scope +var a = 1; +{ + a = 2; + print a; +} +print a; + +//test scope will shadow higher scope on redefine +var b = 3; +{ + var b = 4; + print b; +} +print b; + //test compounds, repeatedly print [1, 2, 3]; print [4, 5]; @@ -69,4 +85,6 @@ assert x, "This won't be seen"; assert true, "This won't be seen"; assert false, "This is a failed assert, and will end execution"; -print "This will not be printed because of the above assert"; \ No newline at end of file +print "This will not be printed because of the above assert"; + +//TODO: use a proper assert-based version of this file diff --git a/scripts/small.toy b/scripts/small.toy index 2af1cec..139597f 100644 --- a/scripts/small.toy +++ b/scripts/small.toy @@ -1,2 +1,2 @@ -var name : [string const, int] = ["foo" : 42]; -var something = 0; + + diff --git a/source/compiler.c b/source/compiler.c index 905407a..dbde911 100644 --- a/source/compiler.c +++ b/source/compiler.c @@ -6,6 +6,8 @@ #include "literal_array.h" #include "literal_dictionary.h" +#include "console_colors.h" + #include void initCompiler(Compiler* compiler) { @@ -171,7 +173,7 @@ void writeCompiler(Compiler* compiler, Node* node) { switch(node->type) { //TODO: more types, like variables, etc. case NODE_ERROR: { - fprintf(stderr, "[Internal] NODE_ERROR encountered in writeCompiler()"); + fprintf(stderr, ERROR "[Internal] NODE_ERROR encountered in writeCompiler()\n" RESET); compiler->bytecode[compiler->count++] = OP_EOF; //1 byte } break; @@ -248,7 +250,7 @@ void writeCompiler(Compiler* compiler, Node* node) { break; case NODE_PAIR: - fprintf(stderr, "[Internal] NODE_PAIR encountered in writeCompiler()"); + fprintf(stderr, ERROR "[Internal] NODE_PAIR encountered in writeCompiler()\n" RESET); break; case NODE_VAR_TYPES: { //TODO: the "type" keyword diff --git a/source/node.c b/source/node.c index ddd97eb..3f1a0fd 100644 --- a/source/node.c +++ b/source/node.c @@ -195,7 +195,7 @@ void printNode(Node* node) { printNode(&(node->block.nodes[i])); } - printf("}\n"); + printf("\n}\n"); break; case NODE_COMPOUND: @@ -229,5 +229,8 @@ void printNode(Node* node) { printNode(node->varDecl.expression); printf(")"); break; + + default: + printf("[internal] unkown node type in printNode: %d\n", node->type); } } \ No newline at end of file diff --git a/source/parser.c b/source/parser.c index 153b132..26e6a7b 100644 --- a/source/parser.c +++ b/source/parser.c @@ -57,6 +57,10 @@ static void consume(Parser* parser, TokenType tokenType, const char* msg) { } static void synchronize(Parser* parser) { + if (command.verbose) { + printf(ERROR "synchronizing\n" RESET); + } + while (parser->current.type != TOKEN_EOF) { switch(parser->current.type) { //these tokens can start a line @@ -244,7 +248,7 @@ static Opcode string(Parser* parser, Node** nodeHandle, bool canBeAssigned) { } static Opcode grouping(Parser* parser, Node** nodeHandle, bool canBeAssigned) { - //handle three diffent types of groupings: (), {}, [] + //handle groupings with () switch(parser->previous.type) { case TOKEN_PAREN_LEFT: { Node* tmpNode = NULL; @@ -685,11 +689,19 @@ static void blockStmt(Parser* parser, Node** nodeHandle) { //use the next node in sequence (*nodeHandle)->block.nodes[(*nodeHandle)->block.count].type = NODE_ERROR; //BUGFIX: so freeing won't break the damn thing - Node* ptr = &((*nodeHandle)->block.nodes[(*nodeHandle)->block.count++]); + Node* ptr = &((*nodeHandle)->block.nodes[(*nodeHandle)->block.count]); //process the grammar rule for this line declaration(parser, &ptr); + //BUGFIX: if ptr has been re-assigned, copy the new value into the block's child + if (&((*nodeHandle)->block.nodes[(*nodeHandle)->block.count]) != ptr) { + ((*nodeHandle)->block.nodes[(*nodeHandle)->block.count]) = *ptr; + FREE(Node, ptr); + } + + (*nodeHandle)->block.count++; + // Ground floor: perfumery / Stationery and leather goods / Wigs and haberdashery / Kitchenware and food / Going up! if (parser->panic) { return; @@ -720,7 +732,13 @@ static void assertStmt(Parser* parser, Node** nodeHandle) { //precedence functions static void expressionStmt(Parser* parser, Node** nodeHandle) { - expression(parser, nodeHandle); + //BUGFIX: statements assume the node exists, expressions assume it doens't + Node* ptr = NULL; + expression(parser, &ptr); + + **nodeHandle = *ptr; + FREE(Node, ptr); //BUGFIX: this thread of execution is nuts + consume(parser, TOKEN_SEMICOLON, "Expected ';' at the end of expression statement"); }