diff --git a/source/compiler.c b/source/compiler.c index d2926c9..217c2e0 100644 --- a/source/compiler.c +++ b/source/compiler.c @@ -300,25 +300,6 @@ static void writeCompilerWithJumps(Compiler* compiler, Node* node, void* breakAd fprintf(stderr, ERROR "[Internal] NODE_PAIR encountered in writeCompilerWithJumps()\n" RESET); break; - case NODE_VAR_TYPES: { //TODO: remove this - int index = writeLiteralTypeToCache(&compiler->literalCache, node->varTypes.typeLiteral); - - //embed the info into the bytecode - if (index >= 256) { - //push a "long" index - compiler->bytecode[compiler->count++] = OP_TYPE_DECL_LONG; //1 byte - *((unsigned short*)(compiler->bytecode + compiler->count)) = (unsigned short)index; //2 bytes - - compiler->count += sizeof(unsigned short); - } - else { - //push the index - compiler->bytecode[compiler->count++] = OP_TYPE_DECL; //1 byte - compiler->bytecode[compiler->count++] = (unsigned char)index; //1 byte - } - } - break; - case NODE_VAR_DECL: { //first, embed the expression (leaves it on the stack) writeCompilerWithJumps(compiler, node->varDecl.expression, breakAddressesPtr, continueAddressesPtr); diff --git a/source/node.c b/source/node.c index 50dafce..b70251d 100644 --- a/source/node.c +++ b/source/node.c @@ -52,10 +52,6 @@ void freeNode(Node* node) { freeNode(node->pair.right); break; - case NODE_VAR_TYPES: - freeLiteral(node->varTypes.typeLiteral); - break; - case NODE_VAR_DECL: freeLiteral(node->varDecl.identifier); freeLiteral(node->varDecl.typeLiteral); @@ -108,13 +104,13 @@ void emitNodeLiteral(Node** nodeHandle, Literal literal) { (*nodeHandle)->atomic.literal = literal; } -void emitNodeUnary(Node** nodeHandle, Opcode opcode) { +void emitNodeUnary(Node** nodeHandle, Opcode opcode, Node* child) { //allocate a new node *nodeHandle = ALLOCATE(Node, 1); (*nodeHandle)->type = NODE_UNARY; (*nodeHandle)->unary.opcode = opcode; - (*nodeHandle)->unary.child = NULL; + (*nodeHandle)->unary.child = child; } void emitNodeBinary(Node** nodeHandle, Node* rhs, Opcode opcode) { @@ -132,7 +128,7 @@ void emitNodeGrouping(Node** nodeHandle) { Node* tmp = ALLOCATE(Node, 1); tmp->type = NODE_GROUPING; - tmp->grouping.child = NULL; + tmp->grouping.child = *nodeHandle; *nodeHandle = tmp; } @@ -170,21 +166,12 @@ void emitNodePair(Node** nodeHandle, Node* left, Node* right) { *nodeHandle = tmp; } -void emitNodeVarTypes(Node** nodeHandle, Literal literal) { - Node* tmp = ALLOCATE(Node, 1); - - tmp->type = NODE_VAR_TYPES; - tmp->varTypes.typeLiteral = literal; - - *nodeHandle = tmp; -} - -void emitNodeVarDecl(Node** nodeHandle, Literal identifier, Literal type, Node* expression) { +void emitNodeVarDecl(Node** nodeHandle, Literal identifier, Literal typeLiteral, Node* expression) { Node* tmp = ALLOCATE(Node, 1); tmp->type = NODE_VAR_DECL; tmp->varDecl.identifier = identifier; - tmp->varDecl.typeLiteral = type; + tmp->varDecl.typeLiteral = typeLiteral; tmp->varDecl.expression = expression; *nodeHandle = tmp; @@ -211,7 +198,7 @@ void emitFnCall(Node** nodeHandle, Node* arguments) { *nodeHandle = tmp; } -void emitNodeFnCollection(Node** nodeHandle) { +void emitNodeFnCollection(Node** nodeHandle) { //a collection of nodes, intended for use with functions Node* tmp = ALLOCATE(Node, 1); tmp->type = NODE_FN_COLLECTION; @@ -235,7 +222,7 @@ void emitNodePath(Node** nodeHandle, NodeType type, Node* preClause, Node* postC *nodeHandle = tmp; } -void emiteNodePrefixIncrement(Node** nodeHandle, Literal identifier, int increment) { +void emitNodePrefixIncrement(Node** nodeHandle, Literal identifier, int increment) { Node* tmp = ALLOCATE(Node, 1); tmp->type = NODE_INCREMENT_PREFIX; @@ -245,7 +232,7 @@ void emiteNodePrefixIncrement(Node** nodeHandle, Literal identifier, int increme *nodeHandle = tmp; } -void emiteNodePostfixIncrement(Node** nodeHandle, Literal identifier, int increment) { +void emitNodePostfixIncrement(Node** nodeHandle, Literal identifier, int increment) { Node* tmp = ALLOCATE(Node, 1); tmp->type = NODE_INCREMENT_POSTFIX; @@ -254,105 +241,3 @@ void emiteNodePostfixIncrement(Node** nodeHandle, Literal identifier, int increm *nodeHandle = tmp; } - -void printNode(Node* node) { - if (node == NULL) { - return; - } - - switch(node->type) { - case NODE_ERROR: - printf("error"); - break; - - case NODE_LITERAL: - printf("literal:"); - printLiteral(node->atomic.literal); - break; - - case NODE_UNARY: - printf("unary:"); - printNode(node->unary.child); - break; - - case NODE_BINARY: - printf("binary-left:"); - printNode(node->binary.left); - printf(";binary-right:"); - printNode(node->binary.right); - printf(";"); - break; - - case NODE_GROUPING: - printf("("); - printNode(node->grouping.child); - printf(")"); - break; - - case NODE_BLOCK: - printf("{\n"); - - for (int i = 0; i < node->block.count; i++) { - printNode(&(node->block.nodes[i])); - } - - printf("\n}\n"); - break; - - case NODE_COMPOUND: - printf("compound[\n"); - - for (int i = 0; i < node->compound.count; i++) { - printNode(&(node->compound.nodes[i])); - } - - printf("]\n"); - break; - - case NODE_PAIR: - printf("pair-left:"); - printNode(node->pair.left); - printf(";pair-right:"); - printNode(node->pair.right); - printf(";"); - break; - - case NODE_VAR_TYPES: - printLiteral(node->varTypes.typeLiteral); - break; - - case NODE_VAR_DECL: - printf("vardecl("); - printLiteral(node->varDecl.identifier); - printf("; "); - printLiteral(node->varDecl.typeLiteral); - printf("; "); - printNode(node->varDecl.expression); - printf(")"); - break; - - case NODE_PATH_IF: - case NODE_PATH_WHILE: - case NODE_PATH_FOR: - printf("path("); - printNode(node->path.preClause); - printf("; "); - printNode(node->path.condition); - printf("; "); - printNode(node->path.postClause); - printf("):("); - printNode(node->path.thenPath); - printf(")else("); - printNode(node->path.elsePath); - printf(")"); - break; - - // case NODE_INCREMENT_PREFIX: - // case NODE_INCREMENT_POSTFIX: - // //TODO - // break; - - default: - printf("[internal] unkown node type in printNode: %d\n", node->type); - } -} \ No newline at end of file diff --git a/source/node.h b/source/node.h index 4e51dfa..b88d3d8 100644 --- a/source/node.h +++ b/source/node.h @@ -17,7 +17,6 @@ typedef enum NodeType { NODE_BLOCK, //contains a sub-node array NODE_COMPOUND, //contains a sub-node array NODE_PAIR, //contains a left and right - NODE_VAR_TYPES, //contains a type and a sub-node array for compound types NODE_VAR_DECL, //contains identifier literal, typenode, expression definition NODE_FN_DECL, //containd identifier literal, arguments node, returns node, block node NODE_FN_COLLECTION, //parts of a function @@ -76,11 +75,6 @@ typedef struct NodePair { Node* right; } NodePair; -typedef struct NodeVarTypes { - NodeType type; - Literal typeLiteral; -} NodeVarTypes; - typedef struct NodeVarDecl { NodeType type; Literal identifier; @@ -132,7 +126,6 @@ union _node { NodeBlock block; NodeCompound compound; NodePair pair; - NodeVarTypes varTypes; NodeVarDecl varDecl; NodeFnDecl fnDecl; NodeFnCollection fnCollection; @@ -143,19 +136,16 @@ union _node { void freeNode(Node* node); void emitNodeLiteral(Node** nodeHandle, Literal literal); -void emitNodeUnary(Node** nodeHandle, Opcode opcode); -void emitNodeBinary(Node** nodeHandle, Node* rhs, Opcode opcode); +void emitNodeUnary(Node** nodeHandle, Opcode opcode, Node* child); +void emitNodeBinary(Node** nodeHandle, Node* rhs, Opcode opcode); //handled node becomes lhs void emitNodeGrouping(Node** nodeHandle); void emitNodeBlock(Node** nodeHandle); void emitNodeCompound(Node** nodeHandle, LiteralType literalType); void emitNodePair(Node** nodeHandle, Node* left, Node* right); -void emitNodeVarTypes(Node** nodeHandle, Literal literal); void emitNodeVarDecl(Node** nodeHandle, Literal identifier, Literal type, Node* expression); void emitNodeFnDecl(Node** nodeHandle, Literal identifier, Node* arguments, Node* returns, Node* block); void emitFnCall(Node** nodeHandle, Node* arguments); void emitNodeFnCollection(Node** nodeHandle); void emitNodePath(Node** nodeHandle, NodeType type, Node* preClause, Node* postClause, Node* condition, Node* thenPath, Node* elsePath); -void emiteNodePrefixIncrement(Node** nodeHandle, Literal identifier, int increment); -void emiteNodePostfixIncrement(Node** nodeHandle, Literal identifier, int increment); - -void printNode(Node* node); +void emitNodePrefixIncrement(Node** nodeHandle, Literal identifier, int increment); +void emitNodePostfixIncrement(Node** nodeHandle, Literal identifier, int increment); diff --git a/source/parser.c b/source/parser.c index 8cb6d89..9c374fa 100644 --- a/source/parser.c +++ b/source/parser.c @@ -271,14 +271,11 @@ static Opcode grouping(Parser* parser, Node** nodeHandle) { //handle groupings with () switch(parser->previous.type) { case TOKEN_PAREN_LEFT: { - Node* tmpNode = NULL; - parsePrecedence(parser, &tmpNode, PREC_TERNARY); + parsePrecedence(parser, nodeHandle, PREC_TERNARY); consume(parser, TOKEN_PAREN_RIGHT, "Expected ')' at end of grouping"); //process the result without optimisations emitNodeGrouping(nodeHandle); - nodeHandle = &((*nodeHandle)->unary.child); //re-align after append - (*nodeHandle) = tmpNode; return OP_EOF; } @@ -430,8 +427,7 @@ static Opcode unary(Parser* parser, Node** nodeHandle) { } //actually emit the negation - emitNodeUnary(nodeHandle, OP_NEGATE); - (*nodeHandle)->unary.child = tmpNode; //set negate's child to the literal + emitNodeUnary(nodeHandle, OP_NEGATE, tmpNode); } else if (parser->previous.type == TOKEN_NOT) { @@ -458,8 +454,7 @@ static Opcode unary(Parser* parser, Node** nodeHandle) { } //actually emit the negation - emitNodeUnary(nodeHandle, OP_INVERT); - (*nodeHandle)->unary.child = tmpNode; //set negate's child to the literal + emitNodeUnary(nodeHandle, OP_INVERT, tmpNode); } else { @@ -589,7 +584,7 @@ static Opcode incrementPrefix(Parser* parser, Node** nodeHandle) { Node* node = NULL; identifier(parser, &node); - emiteNodePrefixIncrement(nodeHandle, node->atomic.literal, 1); + emitNodePrefixIncrement(nodeHandle, node->atomic.literal, 1); return OP_EOF; } @@ -600,7 +595,7 @@ static Opcode incrementInfix(Parser* parser, Node** nodeHandle) { advance(parser); - emiteNodePostfixIncrement(nodeHandle, node->atomic.literal, 1); + emitNodePostfixIncrement(nodeHandle, node->atomic.literal, 1); return OP_EOF; } @@ -611,7 +606,7 @@ static Opcode decrementPrefix(Parser* parser, Node** nodeHandle) { Node* node = NULL; identifier(parser, &node); - emiteNodePrefixIncrement(nodeHandle, node->atomic.literal, -1); + emitNodePrefixIncrement(nodeHandle, node->atomic.literal, -1); return OP_EOF; } @@ -622,7 +617,7 @@ static Opcode decrementInfix(Parser* parser, Node** nodeHandle) { advance(parser); - emiteNodePostfixIncrement(nodeHandle, node->atomic.literal, -1); + emitNodePostfixIncrement(nodeHandle, node->atomic.literal, -1); return OP_EOF; } @@ -1001,10 +996,7 @@ static void expression(Parser* parser, Node** nodeHandle) { //statements static void blockStmt(Parser* parser, Node** nodeHandle) { //init - (*nodeHandle)->type = NODE_BLOCK; - (*nodeHandle)->block.nodes = NULL; - (*nodeHandle)->block.capacity = 0; - (*nodeHandle)->block.count = 0; + emitNodeBlock(nodeHandle); //sub-scope, compile it and push it up in a node while (!match(parser, TOKEN_BRACE_RIGHT)) { @@ -1023,11 +1015,11 @@ static void blockStmt(Parser* parser, Node** nodeHandle) { //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); - } + // //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++; diff --git a/test/test_node.c b/test/test_node.c index 18b7d68..8b8ed31 100644 --- a/test/test_node.c +++ b/test/test_node.c @@ -15,6 +15,36 @@ int main() { freeNode(node); } + { + //test compound (dictionary) + char* idn = "foobar"; + char* str = "hello world"; + + Node* dictionary; + Node* left; + Node* right; + + emitNodeCompound(&dictionary, LITERAL_DICTIONARY); + emitNodeLiteral(&left, TO_IDENTIFIER_LITERAL(copyString(idn, strlen(idn)), strlen(idn)) ); + emitNodeLiteral(&right, TO_STRING_LITERAL(copyString(str, strlen(str)), strlen(str)) ); + + //grow the node if needed + if (dictionary->compound.capacity < dictionary->compound.count + 1) { + int oldCapacity = dictionary->compound.capacity; + + dictionary->compound.capacity = GROW_CAPACITY(oldCapacity); + dictionary->compound.nodes = GROW_ARRAY(Node, dictionary->compound.nodes, oldCapacity, dictionary->compound.capacity); + } + + //store the left and right in the node + Node* pair = NULL; + emitNodePair(&pair, left, right); + dictionary->compound.nodes[dictionary->compound.count++] = *pair; + + //the real test + freeNode(dictionary); + } + printf(NOTICE "All good\n" RESET); return 0; }