forked from ground/cground
List stuff
This commit is contained in:
@@ -136,6 +136,13 @@ void interpretGroundInstruction(GroundInstruction inst, GroundScope* scope) {
|
||||
case CREATELABEL: {
|
||||
break;
|
||||
}
|
||||
|
||||
/*
|
||||
* CONTROL FLOW
|
||||
* These instructions are for controlling how the program is executed.
|
||||
* Instructions:
|
||||
* if, jump, end
|
||||
*/
|
||||
case IF: {
|
||||
if (in->args.length < 2) {
|
||||
runtimeError(TOO_FEW_ARGS, "Expecting 2 arguments", in, currentInstruction);
|
||||
@@ -187,6 +194,13 @@ void interpretGroundInstruction(GroundInstruction inst, GroundScope* scope) {
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
/*
|
||||
* I/O
|
||||
* These instructions take information from the user, and print it out.
|
||||
* Instructions:
|
||||
* print, println, input
|
||||
*/
|
||||
case PRINT: {
|
||||
if (in->args.length < 1) {
|
||||
runtimeError(TOO_FEW_ARGS, "Expecting 1 or more args", in, currentInstruction);
|
||||
@@ -225,6 +239,14 @@ void interpretGroundInstruction(GroundInstruction inst, GroundScope* scope) {
|
||||
break;
|
||||
}
|
||||
|
||||
/*
|
||||
* VARIABLES AND LISTS
|
||||
* These instructions are for initializing variables and lists.
|
||||
* Instructions:
|
||||
* set, gettype, exists
|
||||
* WIP Instructions:
|
||||
* setlist, setlistat, getlistat, getlistsize, listappend
|
||||
*/
|
||||
case SET: {
|
||||
if (in->args.length < 2) {
|
||||
runtimeError(TOO_FEW_ARGS, "Expecting 2 args", in, currentInstruction);
|
||||
@@ -242,7 +264,82 @@ void interpretGroundInstruction(GroundInstruction inst, GroundScope* scope) {
|
||||
addVariable(scope->variables, in->args.args[0].value.refName, in->args.args[1].value.value);
|
||||
break;
|
||||
}
|
||||
case GETTYPE: {
|
||||
if (in->args.length < 2) {
|
||||
runtimeError(TOO_FEW_ARGS, "Expecting 2 args", in, currentInstruction);
|
||||
}
|
||||
if (in->args.length > 2) {
|
||||
runtimeError(TOO_MANY_ARGS, "Expecting 2 args", in, currentInstruction);
|
||||
}
|
||||
if (in->args.args[0].type != VALUE) {
|
||||
runtimeError(ARG_TYPE_MISMATCH, "Expecting a Value for arg 1", in, currentInstruction);
|
||||
}
|
||||
if (in->args.args[1].type != DIRREF) {
|
||||
runtimeError(ARG_TYPE_MISMATCH, "Expecting a DirectRef for arg 2", in, currentInstruction);
|
||||
}
|
||||
|
||||
switch (in->args.args[0].value.value.type) {
|
||||
case INT: {
|
||||
addVariable(scope->variables, in->args.args[1].value.refName, createStringGroundValue("int"));
|
||||
break;
|
||||
}
|
||||
case DOUBLE: {
|
||||
addVariable(scope->variables, in->args.args[1].value.refName, createStringGroundValue("double"));
|
||||
break;
|
||||
}
|
||||
case STRING: {
|
||||
addVariable(scope->variables, in->args.args[1].value.refName, createStringGroundValue("string"));
|
||||
break;
|
||||
}
|
||||
case CHAR: {
|
||||
addVariable(scope->variables, in->args.args[1].value.refName, createStringGroundValue("char"));
|
||||
break;
|
||||
}
|
||||
case BOOL: {
|
||||
addVariable(scope->variables, in->args.args[1].value.refName, createStringGroundValue("bool"));
|
||||
break;
|
||||
}
|
||||
case LIST: {
|
||||
addVariable(scope->variables, in->args.args[1].value.refName, createStringGroundValue("list"));
|
||||
break;
|
||||
}
|
||||
case CUSTOM: {
|
||||
addVariable(scope->variables, in->args.args[1].value.refName, createStringGroundValue("custom"));
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
case EXISTS: {
|
||||
if (in->args.length < 2) {
|
||||
runtimeError(TOO_FEW_ARGS, "Expecting 2 args", in, currentInstruction);
|
||||
}
|
||||
if (in->args.length > 2) {
|
||||
runtimeError(TOO_MANY_ARGS, "Expecting 2 args", in, currentInstruction);
|
||||
}
|
||||
if (in->args.args[0].type != DIRREF) {
|
||||
runtimeError(ARG_TYPE_MISMATCH, "Expecting a DirectRef for arg 1", in, currentInstruction);
|
||||
}
|
||||
if (in->args.args[0].type != DIRREF) {
|
||||
runtimeError(ARG_TYPE_MISMATCH, "Expecting a DirectRef for arg 2", in, currentInstruction);
|
||||
}
|
||||
|
||||
if (findVariable(*scope->variables, in->args.args[0].value.refName)) {
|
||||
addVariable(scope->variables, in->args.args[1].value.refName, createBoolGroundValue(true));
|
||||
} else {
|
||||
addVariable(scope->variables, in->args.args[1].value.refName, createBoolGroundValue(false));
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
/*
|
||||
* MATHS
|
||||
* These instructions allow running mathematical operations on values.
|
||||
* Instructions:
|
||||
* add, subtract, multiply, divide
|
||||
*/
|
||||
case ADD: {
|
||||
if (in->args.length < 3) {
|
||||
runtimeError(TOO_FEW_ARGS, "Expecting 3 args", in, currentInstruction);
|
||||
@@ -427,6 +524,10 @@ void interpretGroundInstruction(GroundInstruction inst, GroundScope* scope) {
|
||||
if (right->type != INT && right->type != DOUBLE) {
|
||||
runtimeError(ARG_TYPE_MISMATCH, "Expecting an Int or Double for arg 2", in, currentInstruction);
|
||||
}
|
||||
|
||||
if (right->type == INT && right->data.intVal == 0) {
|
||||
|
||||
}
|
||||
|
||||
if (left->type == DOUBLE || right->type == DOUBLE) {
|
||||
double result = 0;
|
||||
@@ -453,6 +554,13 @@ void interpretGroundInstruction(GroundInstruction inst, GroundScope* scope) {
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
/*
|
||||
* COMPARISONS
|
||||
* Allows comparing values.
|
||||
* Instructions:
|
||||
* equal, inequal, not, greater, lesser
|
||||
*/
|
||||
case EQUAL: {
|
||||
if (in->args.length < 3) {
|
||||
runtimeError(TOO_FEW_ARGS, "Expecting 3 args", in, currentInstruction);
|
||||
@@ -583,6 +691,27 @@ void interpretGroundInstruction(GroundInstruction inst, GroundScope* scope) {
|
||||
}
|
||||
break;
|
||||
}
|
||||
case NOT: {
|
||||
if (in->args.length < 2) {
|
||||
runtimeError(TOO_FEW_ARGS, "Expecting 2 args", in, currentInstruction);
|
||||
}
|
||||
if (in->args.length > 2) {
|
||||
runtimeError(TOO_MANY_ARGS, "Expecting 2 args", in, currentInstruction);
|
||||
}
|
||||
if (in->args.args[0].type != VALUE && in->args.args[0].value.value.type != BOOL) {
|
||||
runtimeError(ARG_TYPE_MISMATCH, "Expecting a Bool for arg 1", in, currentInstruction);
|
||||
}
|
||||
if (in->args.args[1].type != DIRREF) {
|
||||
runtimeError(ARG_TYPE_MISMATCH, "Expecting a DirectRef for arg 2", in, currentInstruction);
|
||||
}
|
||||
|
||||
bool condition = !in->args.args[1].value.value.data.boolVal;
|
||||
|
||||
addVariable(scope->variables, in->args.args[1].value.refName, createBoolGroundValue(condition));
|
||||
}
|
||||
default: {
|
||||
runtimeError(FIXME, "Currently unimplemented instruction", in, currentInstruction);
|
||||
}
|
||||
case GREATER: {
|
||||
if (in->args.length < 3) {
|
||||
runtimeError(TOO_FEW_ARGS, "Expecting 3 args", in, currentInstruction);
|
||||
@@ -695,27 +824,6 @@ void interpretGroundInstruction(GroundInstruction inst, GroundScope* scope) {
|
||||
}
|
||||
break;
|
||||
}
|
||||
case NOT: {
|
||||
if (in->args.length < 2) {
|
||||
runtimeError(TOO_FEW_ARGS, "Expecting 2 args", in, currentInstruction);
|
||||
}
|
||||
if (in->args.length > 2) {
|
||||
runtimeError(TOO_MANY_ARGS, "Expecting 2 args", in, currentInstruction);
|
||||
}
|
||||
if (in->args.args[0].type != VALUE && in->args.args[0].value.value.type != BOOL) {
|
||||
runtimeError(ARG_TYPE_MISMATCH, "Expecting a Bool for arg 1", in, currentInstruction);
|
||||
}
|
||||
if (in->args.args[1].type != DIRREF) {
|
||||
runtimeError(ARG_TYPE_MISMATCH, "Expecting a DirectRef for arg 2", in, currentInstruction);
|
||||
}
|
||||
|
||||
bool condition = !in->args.args[1].value.value.data.boolVal;
|
||||
|
||||
addVariable(scope->variables, in->args.args[1].value.refName, createBoolGroundValue(condition));
|
||||
}
|
||||
default: {
|
||||
runtimeError(FIXME, "Currently unimplemented instruction", in, currentInstruction);
|
||||
}
|
||||
}
|
||||
|
||||
freeGroundInstruction(in);
|
||||
|
||||
Reference in New Issue
Block a user