Files
cground/src/interface.c

126 lines
3.2 KiB
C
Raw Normal View History

#include "parser.h"
#include "interpreter.h"
#include "types.h"
#include <stdlib.h>
#include <stdarg.h>
GroundProgram groundCreateProgram() {
return (GroundProgram) {
.instructions = malloc(sizeof(GroundInstruction)),
.size = 0
};
}
void groundAddInstructionToProgram(GroundProgram* program, GroundInstruction instruction) {
addInstructionToProgram(program, instruction);
}
GroundInstruction groundCreateInstruction(GroundInstType type) {
return (GroundInstruction) {
.type = type,
.args.args = malloc(sizeof(GroundArg)),
.args.length = 0
};
}
void groundAddValueToInstruction(GroundInstruction* inst, GroundValue value) {
addArgToInstruction(inst, createValueGroundArg(value));
}
void groundAddReferenceToInstruction(GroundInstruction* inst, GroundArg value) {
addArgToInstruction(inst, value);
}
GroundArg groundCreateReference(GroundArgType type, char* ref) {
return (GroundArg) {
.type = type,
.value.refName = ref
};
}
GroundValue groundCreateValue(GroundValueType type, ...) {
va_list args;
va_start(args, type);
switch (type) {
case INT: {
return createIntGroundValue(va_arg(args, int64_t));
break;
}
case DOUBLE: {
return createDoubleGroundValue(va_arg(args, double));
break;
}
case STRING: {
return createStringGroundValue(va_arg(args, char*));
break;
}
case CHAR: {
return createCharGroundValue((char)va_arg(args, int));
break;
}
case BOOL: {
return createBoolGroundValue((bool)va_arg(args, int));
break;
}
case LIST: {
return createListGroundValue(va_arg(args, List));
break;
}
case FUNCTION: {
return createFunctionGroundValue(va_arg(args, GroundFunction*));
break;
}
case STRUCTVAL: {
GroundValue gv;
gv.type = STRUCTVAL;
gv.data.structVal = malloc(sizeof(GroundStruct));
*gv.data.structVal = va_arg(args, GroundStruct);
return gv;
}
2026-01-21 11:55:34 +11:00
case NONE: {
return createNoneGroundValue();
break;
}
2026-01-21 11:55:34 +11:00
case ERROR:
case CUSTOM: {
// FIXME
break;
}
}
return createNoneGroundValue();
va_end(args);
}
GroundValue groundRunProgram(GroundProgram* program) {
return interpretGroundProgram(program, NULL);
}
void groundPrintProgram(GroundProgram* program) {
for (size_t i = 0; i < program->size; i++) {
printGroundInstruction(&program->instructions[i]);
printf("\n");
}
}
2025-12-29 19:13:36 +11:00
GroundProgram groundParseFile(const char* code) {
return parseFile(code);
}
GroundStruct groundCreateStruct() {
GroundStruct gs;
gs.size = 0;
gs.fields = malloc(sizeof(GroundStructField));
return gs;
}
void groundAddValueToScope(GroundScope* gs, const char* name, GroundValue value) {
addVariable(gs->variables, name, value);
}
void groundAddFieldToStruct(GroundStruct* gstruct, char* name, GroundValue field) {
addFieldToStruct(gstruct, name, field);
}