public override InterpreterResult Interpret(InterpreterEnvironment Env) { InterpreterVariable variable = null; if (Variable.IsLast) { if (Env.CurrentContext.LastVariable == null) { throw new InterpreterException("Invalid use of " + Variable.T.Value, Variable.T); } variable = Env.CurrentContext.LastVariable; } else if (Env.CurrentContext.VariableExists(Variable.Name)) { variable = Env.CurrentContext.GetVariable(Variable.Name); } else { variable = new InterpreterVariable(Variable.Name, InterpreterVariableType.Undefined); Env.CurrentContext.AddVariable(variable); } InterpreterResult result = Value.Interpret(Env); variable.Type = result.Type; variable.Value = result.Value; return(new InterpreterResult() { Value = variable.Value, Type = result.Type }); }
public Option <InterpreterResult> TryRun() { Guard.IsNotNull(Source, nameof(Source)); using MemoryOwner <Brainf_ckOperation>?operations = Brainf_ckParser.TryParse <Brainf_ckOperation>(Source.Value.Span, out SyntaxValidationResult validationResult); if (!validationResult.IsSuccess) { return(Option <InterpreterResult> .From(validationResult)); } if (InitialState is TuringMachineState initialState) { Guard.IsNull(MemorySize, nameof(MemorySize)); Guard.IsNull(OverflowMode, nameof(OverflowMode)); initialState = (TuringMachineState)initialState.Clone(); } else { int size = MemorySize ?? Specs.DefaultMemorySize; Guard.IsBetweenOrEqualTo(size, Specs.MinimumMemorySize, Specs.MaximumMemorySize, nameof(MemorySize)); initialState = new TuringMachineState(size, OverflowMode ?? Specs.DefaultOverflowMode); } InterpreterResult result = Brainf_ckInterpreter.Release.Run( operations !.Span, Stdin.GetValueOrDefault().Span, initialState, ExecutionToken); return(Option <InterpreterResult> .From(validationResult, result)); }
public override InterpreterResult Interpret(InterpreterEnvironment Env) { InterpreterResult lhsResult = LHS.Interpret(Env); InterpreterResult rhsResult = RHS.Interpret(Env); if (lhsResult.Type == InterpreterVariableType.Undefined || rhsResult.Type == InterpreterVariableType.Undefined || lhsResult.Value == null || rhsResult.Value == null) { throw new InterpreterException("Unable to divide mysterious", T); } decimal?lhs = InterpreterVariable.GetNumericValueFor(lhsResult.Value); decimal?rhs = InterpreterVariable.GetNumericValueFor(rhsResult.Value); if (!lhs.HasValue || !rhs.HasValue) { throw new InterpreterException("Unable to divide mysterious", T); } if (rhs.Value == 0) { throw new InterpreterException("Divide by zero error", T); } return(new InterpreterResult() { Type = InterpreterVariableType.Numeric, Value = lhs.Value / rhs.Value }); }
private bool CallElaborator( MgaProject project, MgaFCO currentobj, MgaFCOs selectedobjs, int param, bool expand = true) { bool result = false; try { this.Logger.WriteDebug("Elaborating model..."); var elaborator = new CyPhyElaborateCS.CyPhyElaborateCSInterpreter(); elaborator.Initialize(project); int verbosity = 128; result = elaborator.RunInTransaction(project, currentobj, selectedobjs, verbosity); this.Logger.WriteDebug("Elaboration is done."); } catch (Exception ex) { this.Logger.WriteError("Exception occurred in Elaborator : {0}", ex.ToString()); result = false; } return(result); }
public override InterpreterResult Interpret(InterpreterEnvironment Env) { object value = null; if (ToSay is VariableParseNode) { VariableParseNode variableNode = ToSay as VariableParseNode; InterpreterVariable variable = null; if (variableNode.IsLast) { if (Env.CurrentContext.LastVariable != null) { variable = Env.CurrentContext.LastVariable; } else { throw new InterpreterException("Invalid use of " + variableNode.T.Value, variableNode.T); } } else if (Env.CurrentContext.VariableExists(variableNode.Name)) { variable = Env.CurrentContext.GetVariable(variableNode.Name); } else { Console.WriteLine("mysterious"); return(new InterpreterResult()); } if (variable.Type == InterpreterVariableType.Null) { Console.WriteLine("null"); return(new InterpreterResult()); } if (variable.Type == InterpreterVariableType.Undefined) { Console.WriteLine("mysterious"); return(new InterpreterResult()); } value = variable.Value; } else { InterpreterResult result = ToSay.Interpret(Env); if (result.Type == InterpreterVariableType.Null) { Console.WriteLine("null"); } else if (result.Type == InterpreterVariableType.Undefined) { Console.WriteLine("mysterious"); } else { value = result.Value; } } Console.WriteLine(value); return(new InterpreterResult()); }
public IInterpreterResult Main(IInterpreterMainParameters parameters) { var result = new InterpreterResult(); result.RunCommand = "runCarSim.bat"; try { MgaGateway.PerformInTransaction(delegate { if (parameters.CurrentFCO.MetaBase.Name == "CarTestBench") { try { var elaborator = new CyPhyElaborateCS.CyPhyElaborateCSInterpreter(); elaborator.Logger = new GMELogger(parameters.Project); //elaborator.Logger.AddWriter(Logger.Instance); var elaboratorresult = elaborator.RunInTransaction(parameters.Project, parameters.CurrentFCO, parameters.SelectedFCOs, parameters.StartModeParam); if (elaboratorresult == false) { throw new ApplicationException("see elaborator log"); } } catch (Exception e) { //Logger.Instance.AddLogMessage("Elaborator exception occurred: " + e.Message, Severity.Error); throw new Exception(e.Message); } var testBench = CyPhyClasses.CarTestBench.Cast(parameters.CurrentFCO); TestBenchProcessor processor = new TestBenchProcessor() { GMEConsole = GMEConsole, OutputDir = parameters.OutputDirectory, ProjectDir = parameters.ProjectDirectory }; processor.Process(testBench); } else { GMEConsole.Error.WriteLine("Not a Car Test Bench."); return; } }, transactiontype_enum.TRANSACTION_NON_NESTED, abort: true); result.Success = true; } catch (Exception ex) { GMEConsole.Error.WriteLine("Exception was raised: {0}", ex.ToString()); result.Success = false; } finally { // parameters.Project.AbortTransaction(); MgaGateway = null; GC.Collect(); GC.WaitForPendingFinalizers(); } return(result); }
public void ShouldDescribeSuccessfulInterpretation() { object value = 123; var result = new InterpreterResult(value); result.Value.ShouldEqual(value); result.Errors.ShouldBeEmpty(); result.Language.ShouldEqual(Language.Rook); }
public void GivenInterpreterResult_WhenAndSingleResult_ThenResultIsFalse() { var left = new InterpreterResult(true, false); var right = new InterpreterResult(false, false); var result = InterpreterResult.And(left, right); Assert.IsFalse(result.Result); }
public void GivenInterpreterResult_WhenOrBothResult_ThenResultIsTrue() { var left = new InterpreterResult(true, false); var right = new InterpreterResult(true, false); var result = InterpreterResult.Or(left, right); Assert.IsTrue(result.Result); }
public void GivenInterpreterResult_WhenOrNoneResult_ThenResultIsFalse() { var left = new InterpreterResult(false, false); var right = new InterpreterResult(false, false); var result = InterpreterResult.Or(left, right); Assert.IsFalse(result.Result); }
public void GivenInterpreterResult_WhenAndSingleIsAlwaysFalse_ThenIsNotAlwaysFalse() { var left = new InterpreterResult(false, true); var right = new InterpreterResult(false, false); var result = InterpreterResult.And(left, right); Assert.IsTrue(result.IsAlwaysFalse.Value); }
public void GivenInterpreterResult_WhenOrNoneIsAlwaysFalse_ThenIsNotAlwaysFalse() { var left = new InterpreterResult(false, false); var right = new InterpreterResult(false, false); var result = InterpreterResult.Or(left, right); Assert.IsFalse(result.IsAlwaysFalse.Value); }
public void ShouldDescribeFailedInterpretation() { var errorA = new CompilerError(new Position(1, 10), "Error A"); var errorB = new CompilerError(new Position(2, 20), "Error B"); var result = new InterpreterResult(Language.CSharp, errorA, errorB); result.Value.ShouldBeNull(); result.Errors.ShouldList(errorA, errorB); result.Language.ShouldEqual(Language.CSharp); }
public override InterpreterResult Interpret(InterpreterEnvironment Env) { InterpreterResult result = null; while ((result = Condition.Interpret(Env)).Type == InterpreterVariableType.Boolean && !(result.Value as bool?).Value) { Block.Interpret(Env); } return(new InterpreterResult()); }
public IInterpreterResult Main(IInterpreterMainParameters parameters) { var result = new InterpreterResult(); this.parameters = parameters; result.Success = this.RunInTransaction(parameters.CurrentFCO.Project, parameters.CurrentFCO, parameters.SelectedFCOs, 128, parameters.OutputDirectory); result.RunCommand = "cmd.exe /c \"\""; return(result); }
private async Task <EmbedBuilder> SearchAsync(InterpreterResult interpreterResult, SearchType type) { interpreterResult.Search = type; var searchResult = new Search(interpreterResult, Cache).Run(); if (searchResult.Count != 0) { return(await new ResultDisplay(searchResult, Cache, interpreterResult.IsList, GithubRest).RunAsync()); } return(null); }
public void Main() { if (CurrentObjectRequired == true && MainParameters.CurrentFCO == null) { throw new InterpreterException("Current object is not set for COM object."); } if (MgaComponent == null) { throw new ArgumentNullException("MgaComponent"); } if (MgaComponent is ICyPhyInterpreter) { MainParameters.config = InterpreterConfig; result = ((ICyPhyInterpreter)MgaComponent).Main(MainParameters); } else { // set up the interpreter specific parameters if (ProgId == "MGA.Decorator.CyPhy2CAD") { MgaComponent.ComponentParameter["generateCAD"] = "true"; } MgaComponent.ComponentParameter["automation"] = "true"; MgaComponent.ComponentParameter["output_dir"] = MainParameters.OutputDirectory; MgaComponent.ComponentParameter["console_messages"] = MainParameters.ConsoleMessages ? "on" : "off"; MgaComponent.ComponentParameter["original_project_file"] = Path.Combine(MainParameters.ProjectDirectory, "fake.mga"); MgaComponent.ComponentParameter["do_config"] = "false"; MgaComponent.InvokeEx( MainParameters.Project, MainParameters.CurrentFCO, MainParameters.SelectedFCOs, MainParameters.StartModeParam); InterpreterResult result = new InterpreterResult(); result.Labels = MgaComponent.ComponentParameter["labels"] as string; result.RunCommand = MgaComponent.ComponentParameter["runCommand"] as string; result.Success = true; result.ZippyServerSideHook = MgaComponent.ComponentParameter["results_zip_py"] as string; // FIXME result.LogFileDirectory = Path.Combine(MainParameters.ProjectDirectory, "log"); result.BuildQuery = MgaComponent.ComponentParameter["build_query"] as string; this.result = result; } if (this.result.Success == false) { throw new InterpreterException(this.Name + " execution failed"); } }
public void Can_Convert_Empty_Single_Capture_To_Json() { var fakeResult = new InterpreterResult(); var script = "Stuff: [Text];"; var json = fakeResult.ConvertToJson(script); Assert.IsNotNull(json); var result = JsonConvert.DeserializeObject <Dictionary <string, string> >(json); Assert.AreEqual(0, result["Stuff"].Length); }
public void GivenInterpreterResult_WhenComplex_Then3() { var leftPairLeft = new InterpreterResult(false, true); var leftPairRight = new InterpreterResult(false, true); var rightPairLeft = new InterpreterResult(false, true); var rightPairRight = new InterpreterResult(false, true); var leftPairResult = InterpreterResult.And(leftPairLeft, leftPairRight); var rightPairResult = InterpreterResult.And(rightPairLeft, rightPairRight); var result = InterpreterResult.Or(leftPairResult, rightPairResult); Assert.IsFalse(result.Result); Assert.IsTrue(result.IsAlwaysFalse.Value); }
public override InterpreterResult Interpret(InterpreterEnvironment Env) { InterpreterResult comparisonResult = Comparison.Interpret(Env); if ((comparisonResult.Value as bool?).Value) { return(new InterpreterResult() { Value = false, Type = InterpreterVariableType.Boolean }); } return(new InterpreterResult() { Value = true, Type = InterpreterVariableType.Boolean }); }
public static string ConvertToJson(this InterpreterResult interpreterResult, string script) { var responseObject = new Dictionary <string, object>(); foreach (var analyzedQuery in AnalyzeScript(script)) { if (analyzedQuery.Value.NumberOfCaptures > 1 && analyzedQuery.Value.IsArray) { responseObject.Add(analyzedQuery.Key, interpreterResult.Results .Where(x => x.Key == analyzedQuery.Key) .Select(x => x.Value.Result) ); } else if (analyzedQuery.Value.NumberOfCaptures > 1 && !analyzedQuery.Value.IsArray) { responseObject.Add(analyzedQuery.Key, interpreterResult.Results ?.FirstOrDefault(x => x.Key == analyzedQuery.Key) .Value?.Result ?? new Dictionary <string, string>() { } ); } else if (analyzedQuery.Value.IsArray) { responseObject.Add(analyzedQuery.Key, interpreterResult.Results .Where(x => x.Key == analyzedQuery.Key) .SelectMany(x => x.Value.Result.Select(y => y.Value)) ?? new string[] { } ); } else { responseObject.Add(analyzedQuery.Key, interpreterResult.Results .Where(x => x.Key == analyzedQuery.Key) ?.FirstOrDefault() .Value ?.Result ?.FirstOrDefault().Value ?? string.Empty ); } } return(JsonConvert.SerializeObject(responseObject, Formatting.Indented)); }
public void Can_Convert_Single_Multi_Capture() { var fakeResult = new InterpreterResult(); var script = "Stuff: 'A': [Text] 'B': [Text];"; fakeResult.AddResult("Stuff", "A", "1"); fakeResult.AddResult("Stuff", "B", "2"); var json = fakeResult.ConvertToJson(script); Assert.IsNotNull(json); var result = JsonConvert.DeserializeObject <Dictionary <string, Dictionary <string, string> > >(json); Assert.AreEqual("1", result["Stuff"]["A"]); Assert.AreEqual("2", result["Stuff"]["B"]); }
public void Can_Convert_Array_To_Json() { var fakeResult = new InterpreterResult(); var script = "Stuff: Any [Text];"; fakeResult.AddResult("Stuff", "0", "1"); fakeResult.AddResult("Stuff", "1", "4"); var json = fakeResult.ConvertToJson(script); Assert.IsNotNull(json); var result = JsonConvert.DeserializeObject <Dictionary <string, string[]> >(json); Assert.AreEqual(2, result["Stuff"].Length); Assert.AreEqual("1", result["Stuff"][0]); Assert.AreEqual("4", result["Stuff"][1]); }
private bool CallElaborator( MgaProject project, MgaFCO currentobj, MgaFCOs selectedobjs, int param, bool expand = true) { bool result = false; try { var elaborator = new CyPhyElaborateCS.CyPhyElaborateCSInterpreter(); elaborator.Initialize(project); Logger.WriteInfo("Elaborating model..."); System.Windows.Forms.Application.DoEvents(); int verbosity = 128; result = elaborator.RunInTransaction(project, currentobj, selectedobjs, verbosity); if (this.result.Traceability == null) { this.result.Traceability = new META.MgaTraceability(); } if (elaborator.Traceability != null) { elaborator.Traceability.CopyTo(this.result.Traceability); } //this.Logger.WriteDebug("Elaboration is done."); } catch (Exception ex) { Logger.WriteError(ex.Message); return(false); } return(true); }
private void Update() { Debugger.INSTANCE.FlushMessageQueue(); if (isHaltedByDebugger) { if (Debugger.INSTANCE.IsResumeWanted) { Debugger.INSTANCE.IsResumeWanted = false; isHaltedByDebugger = false; } else { return; } } InterpreterResult result = TranslationHelper.RunInterpreter(this.executionContextId); int vmStatus = result.status; if (vmStatus == 1 || // Finished vmStatus == 3) // Error { // Because sometimes once isn't enough. this.Close(); this.Exit(); System.Environment.Exit(0); return; } if (vmStatus == 7) { isHaltedByDebugger = true; string breakpointId = result.loadAssemblyInformation; Debugger.INSTANCE.BroadcastMessage(new string[] { "breakpoint-hit", breakpointId }); return; } }
private bool CallElaborator( MgaProject project, MgaFCO currentobj, MgaFCOs selectedobjs, int param, bool expand = true) { bool result = false; try { GMEConsole.Info.WriteLine("Calling elaborator..."); var elaborator = new CyPhyElaborateCS.CyPhyElaborateCSInterpreter(); elaborator.Initialize(project); int verbosity = 128; elaborator.UnrollConnectors = true; result = elaborator.RunInTransaction(project, currentobj, selectedobjs, verbosity); if (this.result.Traceability == null) { this.result.Traceability = new META.MgaTraceability(); } if (elaborator.Traceability != null) { elaborator.Traceability.CopyTo(this.result.Traceability); } GMEConsole.Info.WriteLine("Elaboration is done."); } catch (Exception ex) { GMEConsole.Error.WriteLine("Exception occurred in Elaborator : {0}", ex.ToString()); result = false; } return(result); }
public void GivenInterpreterResult_WhenCreateTrueTrue_ThenThrow() { var result = new InterpreterResult(true, true); }
public override InterpreterResult Interpret(InterpreterEnvironment Env) { InterpreterResult lhsResult = LHS.Interpret(Env); InterpreterResult rhsResult = RHS.Interpret(Env); //string concatenation if (lhsResult.Type == InterpreterVariableType.String || rhsResult.Type == InterpreterVariableType.String) { string lhsString = ""; string rhsString = ""; if (lhsResult.Type == InterpreterVariableType.Undefined) { lhsString = "mysterious"; } else if (lhsResult.Type == InterpreterVariableType.Null) { rhsString = "null"; } else if (lhsResult.Type == InterpreterVariableType.NaN) { lhsString = "NaN"; } else { lhsString = lhsResult.Value.ToString(); } if (rhsResult.Type == InterpreterVariableType.Undefined) { rhsString = "mysterious"; } else if (rhsResult.Type == InterpreterVariableType.Null) { rhsString = "null"; } else if (rhsResult.Type == InterpreterVariableType.NaN) { rhsString = "NaN"; } else { rhsString = rhsResult.Value.ToString(); } return(new InterpreterResult() { Value = lhsString + rhsString, Type = InterpreterVariableType.String }); } if (lhsResult.Type == InterpreterVariableType.Undefined || rhsResult.Type == InterpreterVariableType.Undefined || lhsResult.Value == null || rhsResult.Value == null) { throw new InterpreterException("Unable to add mysterious", T); } decimal?lhs = InterpreterVariable.GetNumericValueFor(lhsResult.Value); decimal?rhs = InterpreterVariable.GetNumericValueFor(rhsResult.Value); if (!lhs.HasValue || !rhs.HasValue) { throw new InterpreterException("Unable to add mysterious", T); } return(new InterpreterResult() { Type = InterpreterVariableType.Numeric, Value = lhs.Value + rhs.Value }); }
public override InterpreterResult Interpret(InterpreterEnvironment Env) { InterpreterResult lhsResult = LHS.Interpret(Env); InterpreterResult rhsResult = RHS.Interpret(Env); InterpreterResult trueResult = new InterpreterResult() { Value = true, Type = InterpreterVariableType.Boolean }; InterpreterResult falseResult = new InterpreterResult() { Value = false, Type = InterpreterVariableType.Boolean }; if (lhsResult.Value == null && rhsResult.Value == null) { return(trueResult); } if (lhsResult.Type == InterpreterVariableType.NaN && rhsResult.Type == InterpreterVariableType.NaN) { return(trueResult); } if (lhsResult.Type == InterpreterVariableType.Null && rhsResult.Type == InterpreterVariableType.Null) { return(trueResult); } if (lhsResult.Type == InterpreterVariableType.Undefined && rhsResult.Type == InterpreterVariableType.Undefined) { return(trueResult); } if (lhsResult.Type == InterpreterVariableType.Numeric && rhsResult.Type == InterpreterVariableType.Numeric) { if ((lhsResult.Value as decimal?).Value == (rhsResult.Value as decimal?).Value) { return(trueResult); } return(falseResult); } if (lhsResult.Type == InterpreterVariableType.String && lhsResult.Type == InterpreterVariableType.String) { if (lhsResult.Value as string == rhsResult.Value as string) { return(trueResult); } return(falseResult); } decimal?lhsNumVal = InterpreterVariable.GetNumericValueFor(lhsResult.Value); decimal?rhsNumVal = InterpreterVariable.GetNumericValueFor(rhsResult.Value); if (lhsNumVal.HasValue && rhsNumVal.HasValue && lhsNumVal.Value == rhsNumVal.Value) { return(trueResult); } return(falseResult); }
/// <summary> /// Creates a new <see cref="IdeResultWithSectionInfo"/> instance with the specified parameters /// </summary> /// <param name="section">The current section being targeted</param> /// <param name="result">The <see cref="InterpreterResult"/> instance to wrap</param> public IdeResultWithSectionInfo(IdeResultSection section, InterpreterResult result) { Section = section; Result = result; }
public void GivenInterpreterResult_WhenCreateFalseTrue_ThenSuccess() { var result = new InterpreterResult(false, true); }
public IInterpreterResult Main(IInterpreterMainParameters parameters) { this.mainParameters = (InterpreterMainParameters)parameters; try { MgaGateway = new MgaGateway(mainParameters.Project); var result = new InterpreterResult() { Success = true, RunCommand = "cmd.exe /c \"\"" }; MgaGateway.PerformInTransaction(delegate { MainInTransaction((InterpreterMainParameters)parameters); // TODO: this part needs to be refactored! var workflowRef = this.mainParameters .CurrentFCO .ChildObjects .OfType <MgaReference>() .FirstOrDefault(x => x.Meta.Name == "WorkflowRef"); if (workflowRef != null) { string Parameters = workflowRef .Referred .ChildObjects .OfType <MgaAtom>() .FirstOrDefault(fco => fco.Meta.Name == typeof(CyPhy.Task).Name && String.Equals(CyPhyClasses.Task.Cast(fco).Attributes.COMName, this.ComponentProgID, StringComparison.InvariantCultureIgnoreCase)) .StrAttrByName["Parameters"]; Dictionary <string, string> workflowParameters = new Dictionary <string, string>(); try { workflowParameters = (Dictionary <string, string>)Newtonsoft.Json.JsonConvert.DeserializeObject(Parameters, typeof(Dictionary <string, string>)); if (workflowParameters == null) { workflowParameters = new Dictionary <string, string>(); } } catch (Newtonsoft.Json.JsonReaderException) { } META.AnalysisTool.ApplyToolSelection(this.ComponentProgID, workflowParameters, result, this.mainParameters); } }); return(result); } finally { MgaGateway = null; GC.Collect(); GC.WaitForPendingFinalizers(); } }