static void LoadGlobals(RegenCompiler compiler) { foreach (var content in Globals) { compiler.CompileGlobal(content); } }
/// <summary> /// Runs the following code: return new Interperter(code, code).Run().Variables; /// </summary> /// <param name="code">The input code to compile</param> /// <param name="variables">Optional variables to be passed to the interperter</param> /// <param name="modules">The modules to include into the interpreter</param> public override Dictionary <string, object> UnpackedVariables(string code, Dictionary <string, object> variables = null, params RegenModule[] modules) { var parsed = ExpressionParser.Parse(code, variables); var comp = new RegenCompiler(modules); var output = comp.Compile(parsed); Debug(output, comp.Context.Variables); return(comp.Context.Variables.ToDictionary(kv => kv.Key, kv => kv.Value is Data d ? d.Value : kv.Value)); }
/// <summary> /// Runs the following code: return new Interperter(code, code).Run().Variables; /// </summary> /// <param name="code">The input code to compile</param> /// <param name="variables">Optional variables to be passed to the interperter</param> /// <param name="modules">The modules to include into the interpreter</param> public override (ParsedCode ParsedCode, string Output) Compile(string code, Dictionary <string, object> variables = null, params RegenModule[] modules) { var parsed = ExpressionParser.Parse(code, variables); var comp = new RegenCompiler(modules); var output = comp.Compile(parsed); Debug(output, comp.Context.Variables); return(parsed, output); }
public void import_module_via_add() { var @input = @" %(mymod.add(1.0f, 2)) "; var parsed = ExpressionParser.Parse(input); var comp = new RegenCompiler(new RegenModule("mymod", new MyModule())); var output = comp.Compile(parsed); output.Trim('\n', '\r', ' ', '\t', '\0').All(char.IsDigit).Should().BeTrue(); }
private static RegenCompiler _prepareCompiler() { var compiler = new RegenCompiler(); //compile all globals foreach (var glob in RegenEngine.Globals) { compiler.CompileGlobal(glob); } return(compiler); }
/// <summary> /// This function is the callback used to execute the command when the menu item is clicked. /// See the constructor to see how the menu item is associated with this function using /// OleMenuCommandService service and MenuCommand class. /// </summary> /// <param name="sender">Event sender.</param> /// <param name="e">Event args.</param> private void Execute(object sender, EventArgs _) { ThreadHelper.ThrowIfNotOnUIThread(); // open the file in a VS code window and activate the pane DTE dte = Package.GetGlobalService(typeof(DTE)) as DTE; Document doc = dte?.ActiveDocument; string solutionDir = System.IO.Path.GetDirectoryName(dte.Solution.FullName); Logger.Log("Searching for *.regen files at: " + solutionDir); var files = Directory.GetFiles(solutionDir, "*.regen", SearchOption.AllDirectories); if (files.Length == 0) { return; } RegenEngine.Globals.Clear(); //clear existing. foreach (var file in files) { Logger.Log($"Loading globals from {file}"); try { var content = File.ReadAllText(file); var compiler = new RegenCompiler(); compiler.CompileGlobal(content); //just compile to see if it is compilable. RegenEngine.Globals.Add(content); } catch (Exception e) { Logger.Log($"Failed parsing \"{file}\", stopping load..."); Logger.Log(e); break; } } // now set the cursor to the beginning of the function //textSelection.MoveToPoint(function.StartPoint); void Message(string msg) { // Show a message box to prove we were here VsShellUtilities.ShowMessageBox( this.package, msg, "Regen", OLEMSGICON.OLEMSGICON_INFO, OLEMSGBUTTON.OLEMSGBUTTON_OK, OLEMSGDEFBUTTON.OLEMSGDEFBUTTON_FIRST); } }
public void import_module_remove_by_name() { var @input = @" %(mymod.add(1.0f, 2)) "; var parsed = ExpressionParser.Parse(input); var comp = new RegenCompiler(); var mod = new RegenModule("mymod", new MyModule()); comp.AddModule(mod); comp.Compile(parsed).Trim('\n', '\r', ' ', '\t', '\0').All(char.IsDigit).Should().BeTrue(); comp.RemoveModule("mymod"); new Action(() => { comp.Compile(ExpressionParser.Parse(input)); }) .Should().Throw <ExpressionCompileException>().Where(e => e.InnerException.Message.Contains("variable with the name 'mymod'")); }
public static CodeFrame CompileFrame(CodeFrame frame, string code) { var compiler = new RegenCompiler(); //todo modules here? var parsedCode = ExpressionParser.Parse(frame.Input); LoadGlobals(compiler); //handle globals var globals = CodeFrame.CreateGlobals(code); foreach (var globalFrame in globals) { compiler.CompileGlobal(globalFrame.Input); } frame.Output = compiler.Compile(parsedCode); return(frame); }