drop instruction for saving memory and time

This commit is contained in:
2025-12-22 14:05:40 +11:00
parent 579720015a
commit 90361e6bee
5 changed files with 27 additions and 1 deletions

View File

@@ -1468,6 +1468,24 @@ GroundValue interpretGroundInstruction(GroundInstruction inst, GroundScope* scop
currentInstruction = currentCurrentInstruction; currentInstruction = currentCurrentInstruction;
break; break;
} }
case DROP: {
if (in->args.length < 1) {
runtimeError(TOO_FEW_ARGS, "Expecting 1 arg", in, currentInstruction);
}
if (in->args.length > 1) {
runtimeError(TOO_MANY_ARGS, "Expecting 1 arg", in, currentInstruction);
}
if (in->args.args[0].type != DIRREF) {
runtimeError(ARG_TYPE_MISMATCH, "Expecting a DirectRef for arg 1", in, currentInstruction);
}
GroundVariable* var = findVariable(*scope->variables, in->args.args[0].value.refName);
if (!var) {
runtimeError(UNKNOWN_VARIABLE, NULL, in, currentInstruction);
}
deleteVariable(scope->variables, var);
break;
}
default: { default: {
runtimeError(FIXME, "Currently unimplemented instruction", in, currentInstruction); runtimeError(FIXME, "Currently unimplemented instruction", in, currentInstruction);
} }

View File

@@ -169,6 +169,7 @@ static GroundInstType getInstructionType(const char* inst) {
if (strcmp(inst, "init") == 0) return INIT; if (strcmp(inst, "init") == 0) return INIT;
if (strcmp(inst, "use") == 0) return USE; if (strcmp(inst, "use") == 0) return USE;
if (strcmp(inst, "extern") == 0) return EXTERN; if (strcmp(inst, "extern") == 0) return EXTERN;
if (strcmp(inst, "drop") == 0) return DROP;
if (strcmp(inst, "PAUSE") == 0) return PAUSE; if (strcmp(inst, "PAUSE") == 0) return PAUSE;
fprintf(stderr, "Error: Unknown instruction: %s\n", inst); fprintf(stderr, "Error: Unknown instruction: %s\n", inst);

View File

@@ -371,6 +371,9 @@ void printGroundInstruction(GroundInstruction* gi) {
case EXTERN: case EXTERN:
printf("extern"); printf("extern");
break; break;
case DROP:
printf("drop");
break;
case CREATELABEL: case CREATELABEL:
break; break;
default: default:

View File

@@ -8,7 +8,7 @@
#include <string.h> #include <string.h>
typedef enum GroundInstType { typedef enum GroundInstType {
IF, JUMP, END, INPUT, PRINT, PRINTLN, SET, GETTYPE, EXISTS, SETLIST, SETLISTAT, GETLISTAT, GETLISTSIZE, LISTAPPEND, GETSTRSIZE, GETSTRCHARAT, ADD, SUBTRACT, MULTIPLY, DIVIDE, EQUAL, INEQUAL, NOT, GREATER, LESSER, STOI, STOD, TOSTRING, FUN, RETURN, ENDFUN, PUSHARG, CALL, STRUCT, ENDSTRUCT, INIT, USE, EXTERN, CREATELABEL, PAUSE IF, JUMP, END, INPUT, PRINT, PRINTLN, SET, GETTYPE, EXISTS, SETLIST, SETLISTAT, GETLISTAT, GETLISTSIZE, LISTAPPEND, GETSTRSIZE, GETSTRCHARAT, ADD, SUBTRACT, MULTIPLY, DIVIDE, EQUAL, INEQUAL, NOT, GREATER, LESSER, STOI, STOD, TOSTRING, FUN, RETURN, ENDFUN, PUSHARG, CALL, STRUCT, ENDSTRUCT, INIT, USE, EXTERN, CREATELABEL, PAUSE, DROP
} GroundInstType; } GroundInstType;
typedef enum GroundValueType { typedef enum GroundValueType {

4
tests/drop.grnd Normal file
View File

@@ -0,0 +1,4 @@
set &x "dingus"
PAUSE
drop &x
PAUSE