public override IScriptType Invoke(params IScriptType[] arguments) { if (arguments.Length == 0) { PMWrapper.RaiseError("Kräver minst 1 värde till max()."); } IScriptType max = null; foreach (IScriptType v in arguments) { if (max == null) { max = v; continue; } IScriptType result = v.CompareGreaterThan(max); if (result == null) { PMWrapper.RaiseError($"Kan inte jämföra datatypen '{v.GetTypeName()}'."); } else if (result.IsTruthy()) { max = v; } } return(max); }
public override Variable runFunction(Scope currentScope, Variable[] inputParas, int lineNumber) { GameObject car = CarQueue.GetFirstCar(); if (car == null) { PMWrapper.RaiseError(lineNumber, "Kan inte hitta något att scanna."); } Scanner scanner = Scanner.Instance; scanner.Scan(car); int lampCount = 0; foreach (Transform child in car.transform) { if (child.CompareTag("Lamp")) { lampCount++; } } scanner.SetDisplayText(lampCount); return(new Variable("lampCount", lampCount)); }
public void CompileFullCode() { enabled = true; lastLineNumber = 0; theVarWindow.ResetList(); foreach (IPMCompilerStarted ev in UISingleton.FindInterfaces <IPMCompilerStarted>()) { ev.OnPMCompilerStarted(); } try { compiledCode = CreateProcessor(); compiledCode.AddBuiltin(BUILTIN_FUNCTIONS.ToArray()); compiledCode.AddBuiltin(addedFunctions.ToArray()); } catch (SyntaxException e) when(!e.SourceReference.IsFromClr) { StopCompiler(StopStatus.RuntimeError); PMWrapper.RaiseError(e.SourceReference.FromRow, e.Message); } catch (Exception e) { StopCompiler(StopStatus.RuntimeError); PMWrapper.RaiseError(e.Message); } }
public override Variable runFunction(Scope currentScope, Variable[] inputParas, int lineNumber) { if (inputParas.Length == 0) { return(new Variable("Float", 0)); } Variable v = inputParas[0]; switch (v.variableType) { case VariableTypes.boolean: return(new Variable(v.name, v.getBool() ? 1 : 0)); case VariableTypes.textString: double num; if (double.TryParse(v.getString(), out num)) { return(new Variable(v.name, num)); } else { PMWrapper.RaiseError(lineNumber, onFail); } break; case VariableTypes.number: return(v); } PMWrapper.RaiseError(lineNumber, onFail); return(null); }
public override Variable runFunction(Scope currentScope, Variable[] inputParas, int lineNumber) { Variable v = inputParas[0]; switch (v.variableType) { case VariableTypes.number: if (Math.Abs(v.getNumber() - Math.Round(v.getNumber())) <= double.Epsilon) { // Close enough to whole number return(new Variable(v.name, Convert.ToString((long)Math.Round(v.getNumber()), 8))); } PMWrapper.RaiseError(lineNumber, "Kan endast konvertera heltal till oktaler!"); return(null); case VariableTypes.boolean: return(new Variable(v.name, v.getBool() ? "1" : "0")); case VariableTypes.textString: PMWrapper.RaiseError(lineNumber, "Kan inte konvertera text sträng till octaler!"); return(null); default: PMWrapper.RaiseError(lineNumber, "Kan inte konvertera 'None' till octaler!"); return(null); } }
public override Compiler.Variable runFunction(Compiler.Scope currentScope, Compiler.Variable[] inputParas, int lineNumber) { GameObject currentCar = CarQueue.GetFirstCar(); if (currentCar == null) { PMWrapper.RaiseError(lineNumber, "Hittade inget tåg att tömma. "); } UnloadableItem[] itemsToUnload = currentCar.GetComponentsInChildren <UnloadableItem>(); if (itemsToUnload.Length == 0) { PMWrapper.RaiseError(lineNumber, "Kan inte tömma ett tomt tåg. Kom ihåg att köra fram nästa tåg innan du tömmer igen."); } foreach (UnloadableItem item in itemsToUnload) { if (item != null) { item.IsUnloading = true; } } GameObject.FindGameObjectWithTag("SceneController").GetComponent <SceneController1_3>().carsUnloaded += 1; return(new Compiler.Variable()); }
public static GameObject GetFirstCar() { if (Cars.Count == 0) { PMWrapper.RaiseError("Hittade inget tåg."); return(null); } return(Cars.First.Value); }
public void Charge(int lineNumber) { if (AtChargeStation) { isCharging = true; StartCoroutine(PlayChargeAnimation()); } else { PMWrapper.RaiseError(lineNumber, "Kan inte ladda här. Se till att köra hela vägen till laddningsstationen."); } }
public override IScriptType Invoke(params IScriptType[] arguments) { IScriptType v = arguments[0]; if (v is IScriptString s) { return(Processor.Factory.Create(s.Value.Length)); } PMWrapper.RaiseError($"Kan inte beräkna längden på värde av typen '{v.GetTypeName()}'."); return(Processor.Factory.Null); }
public override IScriptType Invoke(params IScriptType[] arguments) { var player = Object.FindObjectOfType <PlayerController>(); if (!player) { PMWrapper.RaiseError("Hittade ingen spelare."); } player.Walk(direction); return(null); }
public override Compiler.Variable runFunction(Compiler.Scope currentScope, Compiler.Variable[] inputParas, int lineNumber) { GameObject chair = GameObject.FindGameObjectWithTag("Chair"); if (chair == null) { PMWrapper.RaiseError(lineNumber, "Hittade ingen stol att lasta av."); } chair.GetComponent <UnloadableItem>().IsUnloading = true; GameObject.FindGameObjectWithTag("SceneController").GetComponent <SceneController1_2>().itemsUnloaded += 1; return(new Compiler.Variable()); }
public override IScriptType Invoke(params IScriptType[] arguments) { IScriptType v = arguments[0]; switch (v) { case IScriptInteger i: return(Processor.Factory.Create(i.Value)); case IScriptDouble d: return(Processor.Factory.Create(Math.Round(d.Value))); default: PMWrapper.RaiseError($"Kan inte avrunda värde av typen '{v.GetTypeName()}'."); return(Processor.Factory.Null); } }
public override IScriptType Invoke(params IScriptType[] arguments) { IScriptType v = arguments[0]; if (v is IScriptInteger i) { if (i.Value < 0) { return(Processor.Factory.Create("-0b" + Convert.ToString(-i.Value, 2))); } return(Processor.Factory.Create("0b" + Convert.ToString(i.Value, 2))); } PMWrapper.RaiseError($"Kan inte konvertera typen '{v.GetTypeName()}' till binärt!"); return(Processor.Factory.Null); }
public override Variable runFunction(Scope currentScope, Variable[] inputParas, int lineNumber) { if (inputParas.Length == 0) { return(new Variable("Heltal", 0)); } Variable v = inputParas[0]; Variable b = inputParas.Length == 2 ? inputParas[1] : null; // if got b but b isnt int if (b != null && (b.variableType != VariableTypes.number || Math.Abs(b.getNumber() - Math.Round(b.getNumber())) > double.Epsilon)) { PMWrapper.RaiseError(lineNumber, onFail2); } int inputBase = 10; if (b != null) { inputBase = (int)Math.Round(b.getNumber()); } if (inputBase < 2 || inputBase > 16) { PMWrapper.RaiseError(lineNumber, onFail3); } try { switch (v.variableType) { case VariableTypes.boolean: return(new Variable(v.name, v.getBool() ? 1 : 0)); case VariableTypes.textString: return(new Variable(v.name, ParseString(v.getString(), inputBase))); case VariableTypes.number: return(new Variable(v.name, (int)v.getNumber())); } } catch { PMWrapper.RaiseError(lineNumber, onFail); } return(null); }
void WalkLine() { try { IDELineMarker.SetWalkerPosition(currentLineNumber); compiledCode.WalkLine(); if (compiledCode == null) { return; } if (!compiledCode.CurrentSource.IsFromClr) { lastLineNumber = compiledCode.CurrentSource.FromRow; } theVarWindow.UpdateList(compiledCode); if (compiledCode.State == ProcessState.Yielded) { sleepTimeLeft = sleepTime * hideLineWhenTimeLeftFactor; } else { sleepTimeLeft = Mathf.Clamp(sleepTime * (1 - PMWrapper.speedMultiplier), 0.01f, 1000); } } catch (Exception e) { Debug.LogException(e); if (compiledCode != null && !compiledCode.CurrentSource.IsFromClr) { lastLineNumber = compiledCode.CurrentSource.FromRow; IDELineMarker.SetWalkerPosition(currentLineNumber); } StopCompiler(StopStatus.RuntimeError); PMWrapper.RaiseError(e.Message); throw; } }
public override Variable runFunction(Scope currentScope, Variable[] inputParas, int lineNumber) { if (inputParas.Length == 0) { return(new Variable("Heltal", 0)); } Variable v = inputParas[0]; if (inputParas.Length == 2) { PMWrapper.RaiseError("För många argument. Konvertering från annan talbas är inte tillgängligt i denna aktivitet"); } if (v.variableType == VariableTypes.textString && string.IsNullOrEmpty(v.getString())) { PMWrapper.RaiseError(lineNumber, onFail4); return(new Variable()); } try { switch (v.variableType) { case VariableTypes.boolean: return(new Variable(v.name, v.getBool() ? 1 : 0)); case VariableTypes.textString: return(new Variable(v.name, int.Parse(v.getString()))); case VariableTypes.number: return(new Variable(v.name, (int)v.getNumber())); } } catch { PMWrapper.RaiseError(lineNumber, onFail); } return(null); }
public override Variable runFunction(Scope currentScope, Variable[] inputParas, int lineNumber) { Variable v = inputParas[0]; switch (v.variableType) { case VariableTypes.number: return(new Variable("Rounded" + v.name, Math.Round(v.getNumber()))); case VariableTypes.boolean: PMWrapper.RaiseError(lineNumber, "Kan inte beräkna avrundat värdet av en boolean!"); return(null); case VariableTypes.textString: PMWrapper.RaiseError(lineNumber, "Kan inte beräkna avrundat värdet av en text sträng!"); return(null); default: PMWrapper.RaiseError(lineNumber, "Kan inte beräkna avrundat värdet av 'None'!"); return(null); } }
public override Variable runFunction(Scope currentScope, Variable[] inputParas, int lineNumber) { Variable v = inputParas[0]; switch (v.variableType) { case VariableTypes.textString: return(new Variable("LengthOf" + v.name, v.getString().Length)); case VariableTypes.boolean: PMWrapper.RaiseError(lineNumber, "En boolean har ingen längd!"); return(null); case VariableTypes.number: PMWrapper.RaiseError(lineNumber, "Siffror har ingen längd!"); return(null); default: PMWrapper.RaiseError(lineNumber, "'None' har ingen längd!"); return(null); } }
public override Variable runFunction(Scope currentScope, Variable[] inputParas, int lineNumber) { foreach (var parameter in inputParas) { if (parameter.variableType != VariableTypes.number || parameter.getNumber() != (int)parameter.getNumber()) { PMWrapper.RaiseError(lineNumber, "Fel datatyp. Parametrarna till randint får bara vara heltal."); } } var start = (int)inputParas[0].getNumber(); var end = (int)inputParas[1].getNumber(); if (start > end) { PMWrapper.RaiseError("Fel i parametrarna till randint. Första parametern ska vara mindre än den andra"); } var randomNumber = Random.Range(start, end + 1); return(new Variable("random", randomNumber)); }
public void CheckAnswer(Variable[] inputParams, int lineNumber) { if (parameterAmount == 0) { PMWrapper.RaiseError(lineNumber, "I detta problem behövs inte svara() för att klara problemet"); } if (inputParams.Length < parameterAmount) { PMWrapper.RaiseError(lineNumber, "För få svar, det ska vara " + parameterAmount + " st svar."); } if (inputParams.Length > parameterAmount) { PMWrapper.RaiseError(lineNumber, "För många svar, det ska vara " + parameterAmount + " st svar."); } foreach (Variable param in inputParams) { if (param.variableType != type) { switch (type) { case VariableTypes.boolean: PMWrapper.RaiseError(lineNumber, "Fel typ, svaret ska vara True eller False."); break; case VariableTypes.number: PMWrapper.RaiseError(lineNumber, "Fel typ, svaret ska vara ett tal."); break; case VariableTypes.textString: PMWrapper.RaiseError(lineNumber, "Fel typ, svaret ska vara en text."); break; default: PMWrapper.RaiseError(lineNumber, "Fel typ av svar."); break; } } } string guess; string ans = ""; bool correctAnswer = true; switch (type) { case VariableTypes.boolean: for (int i = 0; i < inputParams.Length; i++) { guess = inputParams[i].getBool().ToString(); if (guess != answer[i]) { correctAnswer = false; } ans += guess; if (i < inputParams.Length - 1) { ans += ", "; } else { ans += "."; } } Main.Instance.StartCoroutine(ShowAnswerBubble(lineNumber, ans, correctAnswer)); break; case VariableTypes.number: for (int i = 0; i < inputParams.Length; i++) { guess = inputParams[i].getNumber().ToString(); if (guess != answer[i]) { correctAnswer = false; } ans += guess; if (i < inputParams.Length - 1) { ans += ", "; } else { ans += "."; } } Main.Instance.StartCoroutine(ShowAnswerBubble(lineNumber, ans, correctAnswer)); break; case VariableTypes.textString: for (int i = 0; i < inputParams.Length; i++) { guess = inputParams[i].getString().ToString(); if (guess != answer[i]) { correctAnswer = false; } ans += guess; if (i < inputParams.Length - 1) { ans += ", "; } else { ans += "."; } } Main.Instance.StartCoroutine(ShowAnswerBubble(lineNumber, ans, correctAnswer)); break; } }
public override Variable runFunction(Scope currentScope, Variable[] inputParas, int lineNumber) { object max = null; for (int i = 0; i < inputParas.Length; i++) { var v = inputParas[i]; // Checka så det är rätt datatyp if ((v.variableType != VariableTypes.boolean && v.variableType != VariableTypes.number && v.variableType != VariableTypes.textString) || (v.variableType == VariableTypes.textString && v.getString() == null)) { // If (not any of the allowed types) // or (null string) then error PMWrapper.RaiseError(lineNumber, "Kan inte mäta 'None'!"); } if (max == null) { if (v.variableType == VariableTypes.number) { max = v.getNumber(); } if (v.variableType == VariableTypes.boolean) { max = v.getBool(); } if (v.variableType == VariableTypes.textString) { max = v.getString(); } continue; } switch (v.variableType) { case VariableTypes.boolean: if (max is string) { PMWrapper.RaiseError(lineNumber, "Kan inte jämföra text sträng med boolean!"); } if (max is double) { // True=1 False=0 if ((v.getBool() ? 1 : 0) > (double)max) { max = v.getBool(); } } if (max is bool) { max = (bool)max || v.getBool(); } break; case VariableTypes.number: if (max is string) { PMWrapper.RaiseError(lineNumber, "Kan inte jämföra text sträng med siffra!"); } if (max is double) { max = Math.Max((double)max, v.getNumber()); } if (max is bool) { // True=1 False=0 if (v.getNumber() > ((bool)max == true ? 1 : 0)) { max = v.getNumber(); } } break; case VariableTypes.textString: if (max is string) { if (v.getString().CompareTo((string)max) > 0) { max = v.getString(); } } if (max is double) { PMWrapper.RaiseError(lineNumber, "Kan inte jämföra text sträng med siffra!"); } if (max is bool) { PMWrapper.RaiseError(lineNumber, "Kan inte jämföra text sträng med boolean!"); } break; } } if (max is string) { return(new Variable("Minimum", (string)max)); } if (max is double) { return(new Variable("Minimum", (double)max)); } if (max is bool) { return(new Variable("Minimum", (bool)max)); } return(null); }
public override Variable runFunction(Scope currentScope, Variable[] inputParas, int lineNumber) { Variable v = inputParas[0]; switch (v.variableType) { case VariableTypes.number: // Special parsing is needed for round(2.5) to become 3 as in Python. In C# round(2.5) becomes 2. double result = 0; if (v.getNumber() >= 0) { var numberAsString = v.getNumber().ToString(CultureInfo.InvariantCulture); var decimalPointIndex = numberAsString.IndexOf(".", StringComparison.InvariantCulture); if (decimalPointIndex > 0) { if (numberAsString[decimalPointIndex + 1] == '5') { result = double.Parse(numberAsString.Substring(0, decimalPointIndex)) + 1; } else { result = Math.Round(v.getNumber()); } } else { result = Math.Round(v.getNumber()); } } else { // Remove minus sign to be able to parse to double var numberWithoutMinusSign = -v.getNumber(); var numberAsString = numberWithoutMinusSign.ToString(CultureInfo.InvariantCulture); var decimalPointIndex = numberAsString.IndexOf(".", StringComparison.InvariantCulture); if (numberAsString[decimalPointIndex + 1] == '5') { result = -double.Parse(numberAsString.Substring(0, decimalPointIndex)); } else { result = Math.Round(v.getNumber()); } } return(new Variable("Rounded" + v.name, result)); case VariableTypes.boolean: PMWrapper.RaiseError(lineNumber, "Kan inte beräkna avrundat värdet av en boolean!"); return(null); case VariableTypes.textString: PMWrapper.RaiseError(lineNumber, "Kan inte beräkna avrundat värdet av en text sträng!"); return(null); default: PMWrapper.RaiseError(lineNumber, "Kan inte beräkna avrundat värdet av 'None'!"); return(null); } }