More debug features, update README, new makefile

This commit is contained in:
2025-12-08 15:06:29 +11:00
parent bd1a47d118
commit 2ca3789024
8 changed files with 354 additions and 21 deletions

81
src/interface.c Normal file
View File

@@ -0,0 +1,81 @@
#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);
}

View File

@@ -190,6 +190,10 @@ GroundDebugInstruction parseDebugInstruction(char* in) {
gdi.type = CONTINUE;
} else if (strcmp(instruction, "exit") == 0) {
gdi.type = EXIT;
} else if (strcmp(instruction, "step") == 0) {
gdi.type = STEP;
} else if (strcmp(instruction, "view") == 0) {
gdi.type = VIEW;
} else if (strcmp(instruction, "help") == 0) {
gdi.type = HELP;
} else {
@@ -209,6 +213,7 @@ GroundDebugInstruction parseDebugInstruction(char* in) {
GroundValue interpretGroundProgram(GroundProgram* in, GroundScope* inScope) {
GroundLabel* labels = NULL;
GroundVariable* variables = NULL;
int instructionsToPause = -1;
GroundScope scope;
if (inScope != NULL) {
@@ -277,7 +282,7 @@ GroundValue interpretGroundProgram(GroundProgram* in, GroundScope* inScope) {
}
}
}
if (in->instructions[i].type == PAUSE) {
if (in->instructions[i].type == PAUSE || instructionsToPause == 0) {
printf("Paused execution\n");
printf("Previous instruction: ");
if (i > 0) {
@@ -341,6 +346,19 @@ GroundValue interpretGroundProgram(GroundProgram* in, GroundScope* inScope) {
freeGroundProgram(&program);
break;
}
case STEP: {
if (gdi.arg != NULL && strlen(gdi.arg) > 0) {
instructionsToPause = atoi(gdi.arg);
} else {
instructionsToPause = 1;
}
instructionsToPause++;
shouldBreak = true;
break;
}
case VIEW: {
break;
}
case HELP: {
printf("Ground Debugger Help\n");
printf("Didn't mean to end up here? Type \"continue\" to escape this prompt.\n");
@@ -351,6 +369,7 @@ GroundValue interpretGroundProgram(GroundProgram* in, GroundScope* inScope) {
printf(" inspect (variable): Shows the contents of a variable\n");
printf(" eval (code): Runs Ground code in the current scope\n");
printf(" help: Shows this help message");
break;
}
case UNKNOWN: {
printf("Unknown instruction (type \"help\" for help)");
@@ -361,8 +380,11 @@ GroundValue interpretGroundProgram(GroundProgram* in, GroundScope* inScope) {
break;
}
}
continue;
if (in->instructions[i].type == PAUSE) {
continue;
}
}
instructionsToPause --;
int ci = currentInstruction;
GroundValue gv = interpretGroundInstruction(in->instructions[i], &scope);
if (gv.type != NONE) {

View File

@@ -11,7 +11,7 @@ typedef enum GroundRuntimeError {
} GroundRuntimeError;
typedef enum GroundDebugInstructionType {
DUMP, INSPECT, EVAL, CONTINUE, EXIT, HELP, UNKNOWN
DUMP, INSPECT, EVAL, CONTINUE, EXIT, STEP, VIEW, HELP, UNKNOWN
} GroundDebugInstructionType;
typedef struct GroundLabel {