Files
cground/src/interface.c

93 lines
2.3 KiB
C

#include "lexer.h"
#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, int));
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;
}
default: {
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");
}
}
GroundProgram groundParseFile(const char* code) {
return parseFile(code);
}