Files
Toy/source/node.h
Kayne Ruse 6d5549fc8e Added scopes using '{}' symbols, read more
I've also added a new literal type called 'identifier'. This will be
used for variable names, and has a type mask embedded in it.
2022-08-08 15:02:12 +01:00

67 lines
1.3 KiB
C

#pragma once
#include "literal.h"
#include "opcodes.h"
//nodes are the intermediaries between parsers and compilers
typedef union _node Node;
typedef enum NodeType {
NODE_ERROR,
NODE_LITERAL, //a simple value
NODE_UNARY, //one child
NODE_BINARY, //two children, left and right
NODE_GROUPING, //one child
NODE_BLOCK, //contains bytecode
// NODE_CONDITIONAL, //three children: conditional, then path, else path
} NodeType;
typedef struct NodeLiteral {
NodeType type;
Literal literal;
} NodeLiteral;
typedef struct NodeUnary {
NodeType type;
Opcode opcode;
Node* child;
} NodeUnary;
typedef struct NodeBinary {
NodeType type;
Opcode opcode;
Node* left;
Node* right;
} NodeBinary;
typedef struct NodeGrouping {
NodeType type;
Node* child;
} NodeGrouping;
typedef struct NodeBlock {
NodeType type;
Node* nodes;
int capacity;
int count;
} NodeBlock;
union _node {
NodeType type;
NodeLiteral atomic;
NodeUnary unary;
NodeBinary binary;
NodeGrouping grouping;
NodeBlock block;
};
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 emitNodeGrouping(Node** nodeHandle);
void emitNodeBlock(Node** nodeHandle);
void printNode(Node* node);