Got scope-based variable shadowing working

This commit is contained in:
2022-08-14 21:32:13 +01:00
parent 4aa6f75ea7
commit 9e899f5974
5 changed files with 50 additions and 9 deletions

View File

@@ -6,6 +6,8 @@
#include "literal_array.h"
#include "literal_dictionary.h"
#include "console_colors.h"
#include <stdio.h>
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

View File

@@ -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);
}
}

View File

@@ -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");
}