forked from ground/cground
91 lines
2.0 KiB
C
91 lines
2.0 KiB
C
|
|
#include "types.h"
|
||
|
|
|
||
|
|
GroundValue createIntGroundValue(int64_t in) {
|
||
|
|
GroundValue gv;
|
||
|
|
gv.data.intVal = in;
|
||
|
|
gv.type = INT;
|
||
|
|
return gv;
|
||
|
|
}
|
||
|
|
|
||
|
|
GroundValue createDoubleGroundValue(double in) {
|
||
|
|
GroundValue gv;
|
||
|
|
gv.data.doubleVal = in;
|
||
|
|
gv.type = DOUBLE;
|
||
|
|
return gv;
|
||
|
|
}
|
||
|
|
|
||
|
|
GroundValue createStringGroundValue(const char* in) {
|
||
|
|
GroundValue gv;
|
||
|
|
gv.data.stringVal = strdup(in);
|
||
|
|
gv.type = STRING;
|
||
|
|
return gv;
|
||
|
|
}
|
||
|
|
|
||
|
|
GroundValue createCharGroundValue(char in) {
|
||
|
|
GroundValue gv;
|
||
|
|
gv.data.charVal = in;
|
||
|
|
gv.type = CHAR;
|
||
|
|
return gv;
|
||
|
|
}
|
||
|
|
|
||
|
|
GroundValue createBoolGroundValue(bool in) {
|
||
|
|
GroundValue gv;
|
||
|
|
gv.data.boolVal = in;
|
||
|
|
gv.type = BOOL;
|
||
|
|
return gv;
|
||
|
|
}
|
||
|
|
|
||
|
|
void freeGroundValue(GroundValue* gv) {
|
||
|
|
if (gv->type == STRING && gv->data.stringVal != NULL) {
|
||
|
|
free(gv->data.stringVal);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
GroundArg createValueGroundArg(GroundValue value) {
|
||
|
|
GroundArg ga;
|
||
|
|
ga.value.value = value;
|
||
|
|
ga.type = VALUE;
|
||
|
|
return ga;
|
||
|
|
}
|
||
|
|
|
||
|
|
GroundArg createRefGroundArg(GroundArgType type, const char* refname) {
|
||
|
|
GroundArg ga;
|
||
|
|
ga.value.refName = strdup(refname);
|
||
|
|
ga.type = type;
|
||
|
|
return ga;
|
||
|
|
}
|
||
|
|
|
||
|
|
void freeGroundArg(GroundArg* ga) {
|
||
|
|
if (ga->type == VALUE) {
|
||
|
|
freeGroundValue(&ga->value.value);
|
||
|
|
} else {
|
||
|
|
free(ga->value.refName);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
GroundInstruction createGroundInstruction(GroundInstType type) {
|
||
|
|
GroundInstruction gi;
|
||
|
|
gi.type = type;
|
||
|
|
gi.args.args = NULL;
|
||
|
|
gi.args.length = 0;
|
||
|
|
return gi;
|
||
|
|
}
|
||
|
|
|
||
|
|
void freeGroundInstruction(GroundInstruction* gi) {
|
||
|
|
for (size_t i = 0; i < gi-> args.length; i++) {
|
||
|
|
freeGroundArg(&gi->args.args[i]);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
void addArgToInstruction(GroundInstruction* gi, GroundArg arg) {
|
||
|
|
gi->args.length ++;
|
||
|
|
GroundArg* newArgs = realloc(gi->args.args, gi->args.length * sizeof(GroundArg));
|
||
|
|
if (newArgs == NULL) {
|
||
|
|
perror("Failed to allocate memory for instruction argument");
|
||
|
|
exit(EXIT_FAILURE);
|
||
|
|
}
|
||
|
|
gi->args.args = newArgs;
|
||
|
|
gi->args.args[gi->args.length - 1] = arg;
|
||
|
|
}
|
||
|
|
|