start parser
This commit is contained in:
60
src/parser/SolsNode.c
Normal file
60
src/parser/SolsNode.c
Normal file
@@ -0,0 +1,60 @@
|
||||
#include "SolsNode.h"
|
||||
|
||||
#include <stdarg.h>
|
||||
|
||||
#include "../include/error.h"
|
||||
#include "../lexer/SolsLiteral.h"
|
||||
#include "../lexer/SolsType.h"
|
||||
|
||||
ResultType(SolsNode, charptr) createSolsNode(SolsNodeType type, ...) {
|
||||
va_list args;
|
||||
va_start(args, type);
|
||||
|
||||
SolsNode node = {
|
||||
.type = type,
|
||||
.children.capacity = 32,
|
||||
.children.count = 0,
|
||||
.children.at = malloc(sizeof(SolsNode) * 32)
|
||||
};
|
||||
|
||||
if (node.children.at == NULL) {
|
||||
return Error(SolsNode, charptr, "Failed to allocate memory for children (in createSolsNode() function)");
|
||||
}
|
||||
|
||||
switch (type) {
|
||||
case SNT_LITERAL: {
|
||||
node.as.literal = va_arg(args, SolsLiteral);
|
||||
break;
|
||||
}
|
||||
case SNT_TYPE: {
|
||||
node.as.type = va_arg(args, SolsType);
|
||||
break;
|
||||
}
|
||||
case SNT_IDENTIFIER: {
|
||||
node.as.idName = va_arg(args, char*);
|
||||
break;
|
||||
}
|
||||
case SNT_GROUND: {
|
||||
node.as.inlineGround = va_arg(args, char*);
|
||||
break;
|
||||
}
|
||||
default: break;
|
||||
}
|
||||
|
||||
va_end(args);
|
||||
return Success(SolsNode, charptr, node);
|
||||
}
|
||||
|
||||
ResultType(Nothing, charptr) addChildToSolsNode(SolsNode* parent, SolsNode child) {
|
||||
if (parent->children.count + 1 >= parent->children.capacity) {
|
||||
parent->children.capacity *= 2;
|
||||
SolsNode* tmp = realloc(parent->children.at, sizeof(SolsNode) * parent->children.capacity);
|
||||
if (tmp == NULL) {
|
||||
return Error(Nothing, charptr, "Failed to increase memory for children (in addChildToSolsNode() function)");
|
||||
}
|
||||
parent->children.at = tmp;
|
||||
}
|
||||
parent->children.at[parent->children.capacity] = child;
|
||||
parent->children.capacity++;
|
||||
return Success(Nothing, charptr, {});
|
||||
}
|
||||
Reference in New Issue
Block a user