private static void callFunction(SheepMachine vm, Stack <StackItem> stack, List <SheepImport> imports, int index, int numExpectedReturns) { int numParams = getInt(stack); const int MAX_NUM_PARAMS = 16; StackItem[] parms = new StackItem[MAX_NUM_PARAMS]; if (numParams >= MAX_NUM_PARAMS) { throw new Exception("More than the maximum number of allowed parameters found"); } int numItemsOnStack = stack.Count; // find the function if (index < 0 || index >= imports.Count) { throw new Exception("Invalid import function"); } if (imports[index].Parameters.Length != numParams) { throw new Exception("Invalid number of parameters to import function"); } if (numParams > stack.Count) { throw new Exception("Stack is not in a valid state for calling this import function"); } if (imports[index].Callback != null) { // TODO: call the callback imports[index].Callback(vm); } int paramsLeftOver = numParams - (int)(numItemsOnStack - stack.Count); if (paramsLeftOver > numExpectedReturns) { // lazy bums didn't pop everything off! for (int i = numExpectedReturns; i < paramsLeftOver; i++) { stack.Pop(); } } else if (paramsLeftOver < numExpectedReturns) { // the idiots popped too much, or didn't put enough stuff on the stack! throw new Exception("Incorrect number of items on the stack after function call"); } }
private static void callIntFunction(SheepMachine vm, Stack <StackItem> stack, List <SheepImport> imports, int index) { callFunction(vm, stack, imports, index, 1); }
private static void callVoidFunction(SheepMachine vm, Stack <StackItem> stack, List <SheepImport> imports, int index) { callFunction(vm, stack, imports, index, 0); stack.Push(new StackItem(SheepSymbolType.Int, 0)); }