1
0
forked from ground/ground-old
This repository has been archived on 2026-03-16. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
ground_old_fork/src/compiler.c

67 lines
2.2 KiB
C
Raw Normal View History

#include "compiler.h"
2026-01-21 11:38:37 +11:00
#include "interpreter.h"
#include "types.h"
#include "include/estr.h"
2026-01-21 11:38:37 +11:00
#include <stdint.h>
char* compileGroundProgram(GroundProgram* program) {
Estr start = CREATE_ESTR("global _start\nsection .text\n_start:\n");
Estr data = CREATE_ESTR("section .rodata\n");
2026-01-21 11:38:37 +11:00
// Create data section
for (size_t i = 0; i < program->size; i++) {
2026-01-21 11:38:37 +11:00
}
// Generate assembly
for (size_t i = 0; i < program->size; i++) {
GroundInstruction gi = program->instructions[i];
switch (gi.type) {
case END: {
2026-01-21 11:38:37 +11:00
if (gi.args.length < 1) {
runtimeError(TOO_FEW_ARGS, "Expecting 1 arg for end instruction", &gi, i);
}
if (gi.args.length > 1) {
runtimeError(TOO_MANY_ARGS, "Expecting 1 arg for end instruction", &gi, i);
}
if (gi.args.args[0].type != VALUE) {
runtimeError(ARG_TYPE_MISMATCH, "Expecting an Int for end instruction", &gi, i);
}
if (gi.args.args[0].value.value.type != INT) {
runtimeError(ARG_TYPE_MISMATCH, "Expecting an Int for end instruction", &gi, i);
}
int64_t bigretint = gi.args.args[0].value.value.data.intVal;
int retint;
if (bigretint > 255) {
retint = 255;
}
else if (bigretint < 0) {
retint = 0;
}
else {
retint = bigretint;
}
char retstr[8];
snprintf(retstr, sizeof(retstr), "%d", retint);
APPEND_ESTR(start, " mov rax, 60\n");
2026-01-21 11:38:37 +11:00
APPEND_ESTR(start, " mov rdi, ");
APPEND_ESTR(start, retstr);
APPEND_ESTR(start, "\n");
APPEND_ESTR(start, " syscall\n");
break;
}
default: {
printf("no\n");
exit(1);
}
}
}
Estr complete = CREATE_ESTR("");
APPEND_ESTR(complete, start.str);
APPEND_ESTR(complete, data.str)
return complete.str;
}