mirror of
https://github.com/krgamestudios/Toy.git
synced 2026-04-19 16:54:08 +10:00
Lexer partially working
This commit is contained in:
53
source/node.c
Normal file
53
source/node.c
Normal file
@@ -0,0 +1,53 @@
|
||||
#include "node.h"
|
||||
|
||||
#include "memory.h"
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
void freeNode(Node* node) {
|
||||
switch(node->type) {
|
||||
case NODE_ATOMIC:
|
||||
freeLiteral(node->atomic.literal);
|
||||
break;
|
||||
|
||||
case NODE_UNARY:
|
||||
freeNode(node->unary.child);
|
||||
break;
|
||||
|
||||
case NODE_BINARY:
|
||||
freeNode(node->binary.left);
|
||||
freeNode(node->binary.right);
|
||||
break;
|
||||
}
|
||||
|
||||
FREE(Node, node);
|
||||
}
|
||||
|
||||
void emitAtomicLiteral(Node** nodeHandle, Literal literal) {
|
||||
//allocate a new node
|
||||
*nodeHandle = ALLOCATE(Node, 1);
|
||||
|
||||
(*nodeHandle)->type = NODE_ATOMIC;
|
||||
(*nodeHandle)->atomic.literal = literal;
|
||||
}
|
||||
|
||||
void printNode(Node* node) {
|
||||
switch(node->type) {
|
||||
case NODE_ATOMIC:
|
||||
printf("atomic:");
|
||||
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);
|
||||
break;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user