char* InvalidCommand(ListExecutor* self, SlimList* instruction) { char* id = SlimList_GetStringAt(instruction, 0); char* command = SlimList_GetStringAt(instruction, 1); static char msg[128]; snprintf(msg, 128, "__EXCEPTION__:message:<<INVALID_STATEMENT: [\"%s\", \"%s\"].>>", id, command); return buyString(msg); }
char* Make(ListExecutor* self, SlimList* instruction) { char* instanceName = SlimList_GetStringAt(instruction, 2); char* className = SlimList_GetStringAt(instruction, 3); SlimList* args = SlimList_GetTailAt(instruction, 4); char * result = buyString(StatementExecutor_Make(self->executor, instanceName, className, args)); SlimList_Destroy(args); return result; }
char* Call(ListExecutor* self, SlimList* instruction) { if (SlimList_GetLength(instruction) < 4) return MalformedInstruction(self, instruction); char* instanceName = SlimList_GetStringAt(instruction, 2); char* methodName = SlimList_GetStringAt(instruction, 3); SlimList* args = SlimList_GetTailAt(instruction, 4); char* result = buyString(StatementExecutor_Call(self->executor, instanceName, methodName, args)); SlimList_Destroy(args); return result; }
char* CallAndAssign(ListExecutor* self, SlimList* instruction) { if (SlimList_GetLength(instruction) < 5) return MalformedInstruction(self, instruction); char* symbolName = SlimList_GetStringAt(instruction, 2); char* instanceName = SlimList_GetStringAt(instruction, 3); char* methodName = SlimList_GetStringAt(instruction, 4); SlimList* args = SlimList_GetTailAt(instruction, 5); char* result = CSlim_BuyString(StatementExecutor_Call(self->executor, instanceName, methodName, args)); StatementExecutor_SetSymbol(self->executor, symbolName, result); SlimList_Destroy(args); return result; }
char* nodeStringAt(SlimList* self, int i) { char * nodeString = SlimList_GetStringAt(self, i); if (nodeString == NULL) nodeString = "null"; return nodeString; }
static char* setDenominator(void* void_self, SlimList* args) { Division* self = (Division*)void_self; self->denominator = atof(SlimList_GetStringAt(args, 0)); if (self->denominator == 0.0) return SLIM_EXCEPTION("You shouldn't divide by zero now should ya?"); return ""; }
SlimList* SlimList_GetTailAt(SlimList* self, int index) { SlimList * tail = SlimList_Create(); int length = SlimList_GetLength(self); for(; index < length; index++) { SlimList_AddString(tail, SlimList_GetStringAt(self, index)); } return tail; }
char* Dispatch(ListExecutor* self, SlimList* instruction) { char* command = SlimList_GetStringAt(instruction, 1); if (strcmp(command, "import") == 0) return Import(self, instruction); else if (strcmp(command, "make") == 0) return Make(self, instruction); else if (strcmp(command, "call") == 0) return Call(self, instruction); else if (strcmp(command, "callAndAssign") == 0) return CallAndAssign(self, instruction); else return InvalidCommand(self, instruction); }
SlimList* ListExecutor_Execute(ListExecutor* self, SlimList* instructions) { SlimList* results = SlimList_Create(); int numberOfInstructions = SlimList_GetLength(instructions); int n; for (n=0; n<numberOfInstructions; n++) { SlimList* instruction = SlimList_GetListAt(instructions, n); char* id = SlimList_GetStringAt(instruction, 0); char* result = Dispatch(self, instruction); AddResult(results, id, result); if (result) free(result); } return results; }
void replaceSymbols(SymbolTable* symbolTable, SlimList* list) { int i; for (i=0; i<SlimList_GetLength(list); i++) { char* string = SlimList_GetStringAt(list, i); SlimList* embeddedList = SlimList_Deserialize(string); if (embeddedList == NULL) { char* replacedString = replaceString(symbolTable, string); SlimList_ReplaceAt(list, i, replacedString); free(replacedString); } else { replaceSymbols(symbolTable, embeddedList); char* serializedReplacedList = SlimList_Serialize(embeddedList); SlimList_ReplaceAt(list, i, serializedReplacedList); SlimList_Destroy(embeddedList); free(serializedReplacedList); } } }
char* SlimList_ToString(SlimList* self) { static char string[128]; char buf[128]; buf[0] = '\0'; strncat(buf, "[", 128); int length = SlimList_GetLength(self); int i; for (i = 0; i<length; i++) { SlimList* sublist = SlimList_GetListAt(self, i); if (sublist != NULL) { strncat(buf, SlimList_ToString(sublist), 128); } else { strncat(buf, "\"", 128); strncat(buf, SlimList_GetStringAt(self, i), 128); strncat(buf, "\"", 128); } if (i != (length-1)) { strncat(buf, ", ", 128); } } strncat(buf, "]", 128); strncpy(string, buf, 128); return string; }
static int getInt(SlimList* args) { return atoi(SlimList_GetStringAt(args, 0)); }
static char* setNumerator(void* void_self, SlimList* args) { Division* self = (Division*)void_self; self->numerator = atof(SlimList_GetStringAt(args, 0)); return ""; }
const char* getStringAt(SlimList *args, int index) { return SlimList_GetStringAt(args, index); }