cool stuff
This commit is contained in:
7
src/Internal/giveUpAndCry.c
Normal file
7
src/Internal/giveUpAndCry.c
Normal file
@@ -0,0 +1,7 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
void _GroundInternalGiveUpAndCry() {
|
||||
printf("Ground has crashed and cannot recover. If you believe this to be a bug, please report this to the repo on Chookspace (https://chookspace.com/ground/ground2)\n");
|
||||
exit(1);
|
||||
}
|
||||
117
src/Internal/run.c
Normal file
117
src/Internal/run.c
Normal file
@@ -0,0 +1,117 @@
|
||||
#include "../../include/ground.h"
|
||||
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <uthash.h>
|
||||
|
||||
void doLabels(GroundProgram* program, GroundState* state) {
|
||||
|
||||
for (size_t i = 0; i < program->len; i++) {
|
||||
for (size_t j = 0; j < program->len; j++) {
|
||||
GroundArg* arg = &program->at[i].args.at[j];
|
||||
if (arg->type == GroundArg_Label) {
|
||||
GroundLabel* label = NULL;
|
||||
HASH_FIND_STR(state->labels, arg->as.ref, label);
|
||||
|
||||
if (label == NULL) {
|
||||
label = malloc(sizeof(GroundLabel));
|
||||
if (label == NULL) {
|
||||
Ground.Log.Error("malloc failed in Ground.Internal.run()");
|
||||
Ground.Flags.error = true;
|
||||
return;
|
||||
}
|
||||
label->lineNum = i;
|
||||
HASH_ADD_STR(state->labels, name, label);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < program->len; i++) {
|
||||
GroundInstruction* instruction = &program->at[i];
|
||||
if (instruction->type == GroundInstruction_JUMP) {
|
||||
GroundArg* arg = &instruction->args.at[0];
|
||||
size_t* line = Ground.State.findLabel(state, arg->as.ref);
|
||||
if (line == NULL) {
|
||||
char buf[2048];
|
||||
snprintf(buf, 2047, "couldn't find label '%s' (instruction JUMP at %zu) in Ground.Internal.Run()", arg->as.ref, i);
|
||||
Ground.Log.Error(buf);
|
||||
Ground.Flags.error = true;
|
||||
return;
|
||||
}
|
||||
|
||||
} else if (instruction->type == GroundInstruction_IF) {
|
||||
GroundArg* arg = &instruction->args.at[1];
|
||||
size_t* line = Ground.State.findLabel(state, arg->as.ref);
|
||||
if (line == NULL) {
|
||||
char buf[2048];
|
||||
snprintf(buf, 2047, "couldn't find label '%s' (instruction IF at %zu) in Ground.Internal.Run()", arg->as.ref, i);
|
||||
Ground.Log.Error(buf);
|
||||
Ground.Flags.error = true;
|
||||
return;
|
||||
}
|
||||
|
||||
} else if (instruction->type == GroundInstruction_CATCH) {
|
||||
GroundArg* arg = &instruction->args.at[1];
|
||||
size_t* line = Ground.State.findLabel(state, arg->as.ref);
|
||||
if (line == NULL) {
|
||||
char buf[2048];
|
||||
snprintf(buf, 2047, "couldn't find label '%s' (instruction CATCH at %zu) in Ground.Internal.Run()", arg->as.ref, i);
|
||||
Ground.Log.Error(buf);
|
||||
Ground.Flags.error = true;
|
||||
return;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/*
|
||||
* Assigns an offset to each variable referenced.
|
||||
*/
|
||||
size_t doOffsets(GroundProgram* program, GroundState* state) {
|
||||
size_t size = 0;
|
||||
for (size_t i = 0; i < program->len; i++) {
|
||||
for (size_t j = 0; j < program->at[i].args.len; j++) {
|
||||
GroundArg* arg = &program->at[i].args.at[j];
|
||||
|
||||
if (
|
||||
arg->type == GroundArg_Value ||
|
||||
arg->type == GroundArg_Label ||
|
||||
arg->type == GroundArg_LineRef
|
||||
) continue;
|
||||
|
||||
GroundVariable* item = NULL;
|
||||
HASH_FIND_STR(state->variables, arg->as.ref, item);
|
||||
|
||||
if (item == NULL) {
|
||||
item = malloc(sizeof(GroundVariable));
|
||||
if (item == NULL) {
|
||||
Ground.Log.Error("malloc failed in Ground.Internal.run()");
|
||||
Ground.Flags.error = true;
|
||||
return 0;
|
||||
}
|
||||
item->value = Ground.New.Value.Int(0);
|
||||
strncpy(item->name, arg->as.ref, 2047);
|
||||
item->_offset = size++;
|
||||
HASH_ADD_STR(state->variables, name, item);
|
||||
}
|
||||
|
||||
arg->_offset = item->_offset;
|
||||
size++;
|
||||
}
|
||||
}
|
||||
return size;
|
||||
}
|
||||
|
||||
void _GroundInternalRun(GroundProgram* program, GroundState* state) {
|
||||
|
||||
// Precompute some really cool stuff
|
||||
doLabels(program, state);
|
||||
if (Ground.Flags.error) return;
|
||||
|
||||
size_t size = doOffsets(program, state);
|
||||
if (Ground.Flags.error) return;
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user