Eventually gonna squash all of today's work

This commit is contained in:
2022-08-29 04:08:31 +01:00
parent 61efb96fe2
commit a6f0200255
5 changed files with 55 additions and 177 deletions

View File

@@ -300,25 +300,6 @@ static void writeCompilerWithJumps(Compiler* compiler, Node* node, void* breakAd
fprintf(stderr, ERROR "[Internal] NODE_PAIR encountered in writeCompilerWithJumps()\n" RESET); fprintf(stderr, ERROR "[Internal] NODE_PAIR encountered in writeCompilerWithJumps()\n" RESET);
break; 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: { case NODE_VAR_DECL: {
//first, embed the expression (leaves it on the stack) //first, embed the expression (leaves it on the stack)
writeCompilerWithJumps(compiler, node->varDecl.expression, breakAddressesPtr, continueAddressesPtr); writeCompilerWithJumps(compiler, node->varDecl.expression, breakAddressesPtr, continueAddressesPtr);

View File

@@ -52,10 +52,6 @@ void freeNode(Node* node) {
freeNode(node->pair.right); freeNode(node->pair.right);
break; break;
case NODE_VAR_TYPES:
freeLiteral(node->varTypes.typeLiteral);
break;
case NODE_VAR_DECL: case NODE_VAR_DECL:
freeLiteral(node->varDecl.identifier); freeLiteral(node->varDecl.identifier);
freeLiteral(node->varDecl.typeLiteral); freeLiteral(node->varDecl.typeLiteral);
@@ -108,13 +104,13 @@ void emitNodeLiteral(Node** nodeHandle, Literal literal) {
(*nodeHandle)->atomic.literal = literal; (*nodeHandle)->atomic.literal = literal;
} }
void emitNodeUnary(Node** nodeHandle, Opcode opcode) { void emitNodeUnary(Node** nodeHandle, Opcode opcode, Node* child) {
//allocate a new node //allocate a new node
*nodeHandle = ALLOCATE(Node, 1); *nodeHandle = ALLOCATE(Node, 1);
(*nodeHandle)->type = NODE_UNARY; (*nodeHandle)->type = NODE_UNARY;
(*nodeHandle)->unary.opcode = opcode; (*nodeHandle)->unary.opcode = opcode;
(*nodeHandle)->unary.child = NULL; (*nodeHandle)->unary.child = child;
} }
void emitNodeBinary(Node** nodeHandle, Node* rhs, Opcode opcode) { void emitNodeBinary(Node** nodeHandle, Node* rhs, Opcode opcode) {
@@ -132,7 +128,7 @@ void emitNodeGrouping(Node** nodeHandle) {
Node* tmp = ALLOCATE(Node, 1); Node* tmp = ALLOCATE(Node, 1);
tmp->type = NODE_GROUPING; tmp->type = NODE_GROUPING;
tmp->grouping.child = NULL; tmp->grouping.child = *nodeHandle;
*nodeHandle = tmp; *nodeHandle = tmp;
} }
@@ -170,21 +166,12 @@ void emitNodePair(Node** nodeHandle, Node* left, Node* right) {
*nodeHandle = tmp; *nodeHandle = tmp;
} }
void emitNodeVarTypes(Node** nodeHandle, Literal literal) { void emitNodeVarDecl(Node** nodeHandle, Literal identifier, Literal typeLiteral, Node* expression) {
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) {
Node* tmp = ALLOCATE(Node, 1); Node* tmp = ALLOCATE(Node, 1);
tmp->type = NODE_VAR_DECL; tmp->type = NODE_VAR_DECL;
tmp->varDecl.identifier = identifier; tmp->varDecl.identifier = identifier;
tmp->varDecl.typeLiteral = type; tmp->varDecl.typeLiteral = typeLiteral;
tmp->varDecl.expression = expression; tmp->varDecl.expression = expression;
*nodeHandle = tmp; *nodeHandle = tmp;
@@ -211,7 +198,7 @@ void emitFnCall(Node** nodeHandle, Node* arguments) {
*nodeHandle = tmp; *nodeHandle = tmp;
} }
void emitNodeFnCollection(Node** nodeHandle) { void emitNodeFnCollection(Node** nodeHandle) { //a collection of nodes, intended for use with functions
Node* tmp = ALLOCATE(Node, 1); Node* tmp = ALLOCATE(Node, 1);
tmp->type = NODE_FN_COLLECTION; tmp->type = NODE_FN_COLLECTION;
@@ -235,7 +222,7 @@ void emitNodePath(Node** nodeHandle, NodeType type, Node* preClause, Node* postC
*nodeHandle = tmp; *nodeHandle = tmp;
} }
void emiteNodePrefixIncrement(Node** nodeHandle, Literal identifier, int increment) { void emitNodePrefixIncrement(Node** nodeHandle, Literal identifier, int increment) {
Node* tmp = ALLOCATE(Node, 1); Node* tmp = ALLOCATE(Node, 1);
tmp->type = NODE_INCREMENT_PREFIX; tmp->type = NODE_INCREMENT_PREFIX;
@@ -245,7 +232,7 @@ void emiteNodePrefixIncrement(Node** nodeHandle, Literal identifier, int increme
*nodeHandle = tmp; *nodeHandle = tmp;
} }
void emiteNodePostfixIncrement(Node** nodeHandle, Literal identifier, int increment) { void emitNodePostfixIncrement(Node** nodeHandle, Literal identifier, int increment) {
Node* tmp = ALLOCATE(Node, 1); Node* tmp = ALLOCATE(Node, 1);
tmp->type = NODE_INCREMENT_POSTFIX; tmp->type = NODE_INCREMENT_POSTFIX;
@@ -254,105 +241,3 @@ void emiteNodePostfixIncrement(Node** nodeHandle, Literal identifier, int increm
*nodeHandle = tmp; *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);
}
}

View File

@@ -17,7 +17,6 @@ typedef enum NodeType {
NODE_BLOCK, //contains a sub-node array NODE_BLOCK, //contains a sub-node array
NODE_COMPOUND, //contains a sub-node array NODE_COMPOUND, //contains a sub-node array
NODE_PAIR, //contains a left and right 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_VAR_DECL, //contains identifier literal, typenode, expression definition
NODE_FN_DECL, //containd identifier literal, arguments node, returns node, block node NODE_FN_DECL, //containd identifier literal, arguments node, returns node, block node
NODE_FN_COLLECTION, //parts of a function NODE_FN_COLLECTION, //parts of a function
@@ -76,11 +75,6 @@ typedef struct NodePair {
Node* right; Node* right;
} NodePair; } NodePair;
typedef struct NodeVarTypes {
NodeType type;
Literal typeLiteral;
} NodeVarTypes;
typedef struct NodeVarDecl { typedef struct NodeVarDecl {
NodeType type; NodeType type;
Literal identifier; Literal identifier;
@@ -132,7 +126,6 @@ union _node {
NodeBlock block; NodeBlock block;
NodeCompound compound; NodeCompound compound;
NodePair pair; NodePair pair;
NodeVarTypes varTypes;
NodeVarDecl varDecl; NodeVarDecl varDecl;
NodeFnDecl fnDecl; NodeFnDecl fnDecl;
NodeFnCollection fnCollection; NodeFnCollection fnCollection;
@@ -143,19 +136,16 @@ union _node {
void freeNode(Node* node); void freeNode(Node* node);
void emitNodeLiteral(Node** nodeHandle, Literal literal); void emitNodeLiteral(Node** nodeHandle, Literal literal);
void emitNodeUnary(Node** nodeHandle, Opcode opcode); void emitNodeUnary(Node** nodeHandle, Opcode opcode, Node* child);
void emitNodeBinary(Node** nodeHandle, Node* rhs, Opcode opcode); void emitNodeBinary(Node** nodeHandle, Node* rhs, Opcode opcode); //handled node becomes lhs
void emitNodeGrouping(Node** nodeHandle); void emitNodeGrouping(Node** nodeHandle);
void emitNodeBlock(Node** nodeHandle); void emitNodeBlock(Node** nodeHandle);
void emitNodeCompound(Node** nodeHandle, LiteralType literalType); void emitNodeCompound(Node** nodeHandle, LiteralType literalType);
void emitNodePair(Node** nodeHandle, Node* left, Node* right); 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 emitNodeVarDecl(Node** nodeHandle, Literal identifier, Literal type, Node* expression);
void emitNodeFnDecl(Node** nodeHandle, Literal identifier, Node* arguments, Node* returns, Node* block); void emitNodeFnDecl(Node** nodeHandle, Literal identifier, Node* arguments, Node* returns, Node* block);
void emitFnCall(Node** nodeHandle, Node* arguments); void emitFnCall(Node** nodeHandle, Node* arguments);
void emitNodeFnCollection(Node** nodeHandle); void emitNodeFnCollection(Node** nodeHandle);
void emitNodePath(Node** nodeHandle, NodeType type, Node* preClause, Node* postClause, Node* condition, Node* thenPath, Node* elsePath); 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 emitNodePrefixIncrement(Node** nodeHandle, Literal identifier, int increment);
void emiteNodePostfixIncrement(Node** nodeHandle, Literal identifier, int increment); void emitNodePostfixIncrement(Node** nodeHandle, Literal identifier, int increment);
void printNode(Node* node);

View File

@@ -271,14 +271,11 @@ static Opcode grouping(Parser* parser, Node** nodeHandle) {
//handle groupings with () //handle groupings with ()
switch(parser->previous.type) { switch(parser->previous.type) {
case TOKEN_PAREN_LEFT: { case TOKEN_PAREN_LEFT: {
Node* tmpNode = NULL; parsePrecedence(parser, nodeHandle, PREC_TERNARY);
parsePrecedence(parser, &tmpNode, PREC_TERNARY);
consume(parser, TOKEN_PAREN_RIGHT, "Expected ')' at end of grouping"); consume(parser, TOKEN_PAREN_RIGHT, "Expected ')' at end of grouping");
//process the result without optimisations //process the result without optimisations
emitNodeGrouping(nodeHandle); emitNodeGrouping(nodeHandle);
nodeHandle = &((*nodeHandle)->unary.child); //re-align after append
(*nodeHandle) = tmpNode;
return OP_EOF; return OP_EOF;
} }
@@ -430,8 +427,7 @@ static Opcode unary(Parser* parser, Node** nodeHandle) {
} }
//actually emit the negation //actually emit the negation
emitNodeUnary(nodeHandle, OP_NEGATE); emitNodeUnary(nodeHandle, OP_NEGATE, tmpNode);
(*nodeHandle)->unary.child = tmpNode; //set negate's child to the literal
} }
else if (parser->previous.type == TOKEN_NOT) { else if (parser->previous.type == TOKEN_NOT) {
@@ -458,8 +454,7 @@ static Opcode unary(Parser* parser, Node** nodeHandle) {
} }
//actually emit the negation //actually emit the negation
emitNodeUnary(nodeHandle, OP_INVERT); emitNodeUnary(nodeHandle, OP_INVERT, tmpNode);
(*nodeHandle)->unary.child = tmpNode; //set negate's child to the literal
} }
else { else {
@@ -589,7 +584,7 @@ static Opcode incrementPrefix(Parser* parser, Node** nodeHandle) {
Node* node = NULL; Node* node = NULL;
identifier(parser, &node); identifier(parser, &node);
emiteNodePrefixIncrement(nodeHandle, node->atomic.literal, 1); emitNodePrefixIncrement(nodeHandle, node->atomic.literal, 1);
return OP_EOF; return OP_EOF;
} }
@@ -600,7 +595,7 @@ static Opcode incrementInfix(Parser* parser, Node** nodeHandle) {
advance(parser); advance(parser);
emiteNodePostfixIncrement(nodeHandle, node->atomic.literal, 1); emitNodePostfixIncrement(nodeHandle, node->atomic.literal, 1);
return OP_EOF; return OP_EOF;
} }
@@ -611,7 +606,7 @@ static Opcode decrementPrefix(Parser* parser, Node** nodeHandle) {
Node* node = NULL; Node* node = NULL;
identifier(parser, &node); identifier(parser, &node);
emiteNodePrefixIncrement(nodeHandle, node->atomic.literal, -1); emitNodePrefixIncrement(nodeHandle, node->atomic.literal, -1);
return OP_EOF; return OP_EOF;
} }
@@ -622,7 +617,7 @@ static Opcode decrementInfix(Parser* parser, Node** nodeHandle) {
advance(parser); advance(parser);
emiteNodePostfixIncrement(nodeHandle, node->atomic.literal, -1); emitNodePostfixIncrement(nodeHandle, node->atomic.literal, -1);
return OP_EOF; return OP_EOF;
} }
@@ -1001,10 +996,7 @@ static void expression(Parser* parser, Node** nodeHandle) {
//statements //statements
static void blockStmt(Parser* parser, Node** nodeHandle) { static void blockStmt(Parser* parser, Node** nodeHandle) {
//init //init
(*nodeHandle)->type = NODE_BLOCK; emitNodeBlock(nodeHandle);
(*nodeHandle)->block.nodes = NULL;
(*nodeHandle)->block.capacity = 0;
(*nodeHandle)->block.count = 0;
//sub-scope, compile it and push it up in a node //sub-scope, compile it and push it up in a node
while (!match(parser, TOKEN_BRACE_RIGHT)) { while (!match(parser, TOKEN_BRACE_RIGHT)) {
@@ -1023,11 +1015,11 @@ static void blockStmt(Parser* parser, Node** nodeHandle) {
//process the grammar rule for this line //process the grammar rule for this line
declaration(parser, &ptr); declaration(parser, &ptr);
//BUGFIX: if ptr has been re-assigned, copy the new value into the block's child // //BUGFIX: if ptr has been re-assigned, copy the new value into the block's child
if (&((*nodeHandle)->block.nodes[(*nodeHandle)->block.count]) != ptr) { // if (&((*nodeHandle)->block.nodes[(*nodeHandle)->block.count]) != ptr) {
((*nodeHandle)->block.nodes[(*nodeHandle)->block.count]) = *ptr; // ((*nodeHandle)->block.nodes[(*nodeHandle)->block.count]) = *ptr;
FREE(Node, ptr); // FREE(Node, ptr);
} // }
(*nodeHandle)->block.count++; (*nodeHandle)->block.count++;

View File

@@ -15,6 +15,36 @@ int main() {
freeNode(node); 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); printf(NOTICE "All good\n" RESET);
return 0; return 0;
} }