protected override Variable Evaluate(ParsingScript script) { List <Variable> args = script.GetFunctionArgs(); string item = Utils.GetSafeString(args, 0); #if __ANDROID__ == false && __IOS__ == false switch (m_mode) { case EditMode.ADD_DEFINITION: Precompiler.AddDefinition(item); break; case EditMode.ADD_NAMESPACE: Precompiler.AddNamespace(item); break; case EditMode.CLEAR_DEFINITIONS: Precompiler.ClearDefinitions(); break; case EditMode.CLEAR_NAMESPACES: Precompiler.ClearNamespaces(); break; } #endif return(Variable.EmptyInstance); }
public void Precompiler_Precompile_sortforeach_Success() { var parser = new Parser(); var tokens = parser.Parse("#foreach(sort(Customers, Name), {})"); var tokens2 = Precompiler.Precompile(tokens); Assert.IsNotNull(tokens2); Assert.AreEqual(6, tokens2.Count); Assert.AreEqual("#foreach", tokens2[0].Value); Assert.AreEqual("(", tokens2[1].Value); Assert.AreEqual(",", tokens2[3].Value); Assert.AreEqual("{}", tokens2[4].Value); Assert.AreEqual(")", tokens2[5].Value); var exprToken = tokens2[2] as ExpressionToken; Assert.IsNotNull(exprToken); Assert.AreEqual(6, exprToken.Children.Count); Assert.AreEqual("sort", exprToken.Children[0].Value); Assert.AreEqual("(", exprToken.Children[1].Value); Assert.AreEqual("Customers", exprToken.Children[2].Value); Assert.AreEqual(",", exprToken.Children[3].Value); Assert.AreEqual("Name", exprToken.Children[4].Value); Assert.AreEqual(")", exprToken.Children[5].Value); }
protected override Variable Evaluate(ParsingScript script) { string funcReturn, funcName; Utils.GetCompiledArgs(script, out funcReturn, out funcName); #if __ANDROID__ == false && __IOS__ == false Precompiler.RegisterReturnType(funcName, funcReturn); Dictionary <string, Variable> argsMap; string[] args = Utils.GetCompiledFunctionSignature(script, out argsMap); script.MoveForwardIf(Constants.START_GROUP, Constants.SPACE); int parentOffset = script.Pointer; string body = Utils.GetBodyBetween(script, Constants.START_GROUP, Constants.END_GROUP); Precompiler precompiler = new Precompiler(funcName, args, argsMap, body, script); precompiler.Compile(m_scriptInCSharp); CustomCompiledFunction customFunc = new CustomCompiledFunction(funcName, body, args, precompiler, argsMap, script); customFunc.ParentScript = script; customFunc.ParentOffset = parentOffset; ParserFunction.RegisterFunction(funcName, customFunc, false /* not native */); #endif return(new Variable(funcName)); }
public void Precompiler_Precompile_tertiary_complex_expression() { var parser = new Parser(); var tokens = parser.Parse("Name == 'bob' && City == 'Seattle' ? 'Jones' : 'Anderson'"); var tokens2 = Precompiler.Precompile(tokens); Assert.IsNotNull(tokens2); Assert.AreEqual(5, tokens2.Count); Assert.AreEqual(Token.TokenType.Expression, tokens2[0].Type); Assert.AreEqual("?", tokens2[1].Value); Assert.AreEqual("Jones", tokens2[2].Value); Assert.AreEqual(":", tokens2[3].Value); Assert.AreEqual("Anderson", tokens2[4].Value); var exprToken1 = tokens2[0] as ExpressionToken; Assert.AreEqual(3, exprToken1.Children.Count); Assert.AreEqual(Token.TokenType.Expression, exprToken1.Children[0].Type); Assert.AreEqual("&&", exprToken1.Children[1].Value); Assert.AreEqual(Token.TokenType.Expression, exprToken1.Children[2].Type); var exprToken3 = exprToken1.Children[0] as ExpressionToken; var exprToken4 = exprToken1.Children[2] as ExpressionToken; Assert.AreEqual("Name", exprToken3.Children[0].Value); Assert.AreEqual("==", exprToken3.Children[1].Value); Assert.AreEqual("bob", exprToken3.Children[2].Value); Assert.AreEqual("City", exprToken4.Children[0].Value); Assert.AreEqual("==", exprToken4.Children[1].Value); Assert.AreEqual("Seattle", exprToken4.Children[2].Value); }
public void Precompiler_Precompile_complex_wParens_expression() { var parser = new Parser(); var tokens = parser.Parse("(Name == 'bob') && (City == 'San Francisco')"); var tokens2 = Precompiler.Precompile(tokens); Assert.IsNotNull(tokens2); Assert.AreEqual(3, tokens2.Count); Assert.AreEqual(Token.TokenType.Expression, tokens2[0].Type); Assert.AreEqual("&&", tokens2[1].Value); Assert.AreEqual(Token.TokenType.Expression, tokens2[2].Type); var exprToken1 = tokens2[0] as ExpressionToken; var exprToken2 = tokens2[2] as ExpressionToken; Assert.AreEqual(3, exprToken1.Children.Count); Assert.AreEqual(3, exprToken2.Children.Count); Assert.AreEqual("Name", exprToken1.Children[0].Value); Assert.AreEqual("==", exprToken1.Children[1].Value); Assert.AreEqual("bob", exprToken1.Children[2].Value); Assert.AreEqual("City", exprToken2.Children[0].Value); Assert.AreEqual("==", exprToken2.Children[1].Value); Assert.AreEqual("San Francisco", exprToken2.Children[2].Value); }
/// <summary> /// <see cref="CurrentModule"/>でファイルを指定したエンコードで読み込み、実行します。 /// </summary> /// <param name="fileName">ファイル名</param> /// <param name="enc">読み込む際に利用する<see cref="Encoding"/></param> public KecaknoahObject DoFile(string fileName, Encoding enc) { var le = Lexer.AnalyzeFromFile(fileName, enc); if (!le.Success) { throw new KecaknoahException(le.Error); } var ast = Parser.Parse(le); if (!ast.Success) { throw new KecaknoahException(le.Error); } var src = Precompiler.PrecompileAll(ast); CurrentModule.RegisterSource(src); if (CurrentModule["main"] != KecaknoahNil.Instance) { return(new KecaknoahContext(CurrentModule).Execute(CurrentModule["main"])); } else { return(KecaknoahNil.Instance); } }
public void Precompiler_Precompile_func_wParams_nested() { var parser = new Parser(); var tokens = parser.Parse("getcity(Name == 'bob', getstate(ZipCode == 98033, Role == 'Engineer'))"); var tokens2 = Precompiler.Precompile(tokens); Assert.IsNotNull(tokens2); Assert.AreEqual(6, tokens2.Count); Assert.AreEqual("getcity", tokens2[0].Value); Assert.AreEqual("(", tokens2[1].Value); Assert.AreEqual(Token.TokenType.Expression, tokens2[2].Type); Assert.AreEqual(",", tokens2[3].Value); Assert.AreEqual(Token.TokenType.Expression, tokens2[4].Type); Assert.AreEqual(")", tokens2[5].Value); var expr1 = tokens2[2] as ExpressionToken; var expr2 = tokens2[4] as ExpressionToken; Assert.AreEqual(3, expr1.Children.Count); Assert.AreEqual("Name", expr1.Children[0].Value); Assert.AreEqual("==", expr1.Children[1].Value); Assert.AreEqual("bob", expr1.Children[2].Value); Assert.AreEqual(6, expr2.Children.Count); Assert.AreEqual("getstate", expr2.Children[0].Value); Assert.AreEqual("(", expr2.Children[1].Value); Assert.AreEqual(Token.TokenType.Expression, expr2.Children[2].Type); Assert.AreEqual(",", expr2.Children[3].Value); Assert.AreEqual(Token.TokenType.Expression, expr2.Children[4].Type); Assert.AreEqual(")", expr2.Children[5].Value); }
/// <summary> /// <see cref="CurrentModule"/>で指定したソースコードを直接解析し、実行します。 /// </summary> /// <param name="source">ソースコード</param> public KecaknoahObject DoString(string source) { var le = Lexer.AnalyzeFromSource(source); if (!le.Success) { throw new KecaknoahException(le.Error); } var ast = Parser.Parse(le); if (!ast.Success) { throw new KecaknoahException(le.Error); } var src = Precompiler.PrecompileAll(ast); CurrentModule.RegisterSource(src); if (CurrentModule["main"] != KecaknoahNil.Instance) { return(new KecaknoahContext(CurrentModule).CallInstant(CurrentModule["main"])); } else { return(KecaknoahNil.Instance); } }
public void Precompiler_Precompile_multipart2_expression() { var parser = new Parser(); var tokens = parser.Parse("$Customer.Name.Last"); var tokens2 = Precompiler.Precompile(tokens); Assert.IsNotNull(tokens2); Assert.AreEqual(1, tokens2.Count); Assert.AreEqual("$Customer.Name.Last", tokens2[0].Value); }
internal CustomCompiledFunction(string funcName, string body, string[] args, Precompiler precompiler, Dictionary <string, Variable> argsMap, ParsingScript script) : base(funcName, body, args, script) { m_precompiler = precompiler; m_argsMap = argsMap; }
public void Precompiler_Precompile_multipart3_expression() { var parser = new Parser(); var tokens = parser.Parse("$Customer.FirstName + $Customer.LastName"); var tokens2 = Precompiler.Precompile(tokens); Assert.IsNotNull(tokens2); Assert.AreEqual(3, tokens2.Count); Assert.AreEqual("$Customer.FirstName", tokens2[0].Value); Assert.AreEqual("+", tokens2[1].Value); Assert.AreEqual("$Customer.LastName", tokens2[2].Value); }
public void Precompiler_Precompile_simple_expression() { var parser = new Parser(); var tokens = parser.Parse("Name == 'bob'"); var tokens2 = Precompiler.Precompile(tokens); Assert.IsNotNull(tokens2); Assert.AreEqual(3, tokens2.Count); Assert.AreEqual("Name", tokens2[0].Value); Assert.AreEqual("==", tokens2[1].Value); Assert.AreEqual("bob", tokens2[2].Value); }
public void Precompiler_Precompile_simple_wParens() { var parser = new Parser(); var tokens = parser.Parse("(Name == 'bob')"); var tokens2 = Precompiler.Precompile(tokens); Assert.IsNotNull(tokens2); Assert.AreEqual(1, tokens2.Count); Assert.AreEqual(Token.TokenType.Expression, tokens2[0].Type); Assert.IsTrue(tokens2[0] is ExpressionToken); Assert.AreEqual(3, (tokens2[0] as ExpressionToken).Children.Count); }
public void Precompiler_Precompile_simple_func() { var parser = new Parser(); var tokens = parser.Parse("getcity(Name == 'bob')"); var tokens2 = Precompiler.Precompile(tokens); Assert.IsNotNull(tokens2); Assert.AreEqual(4, tokens2.Count); Assert.AreEqual("getcity", tokens2[0].Value); Assert.AreEqual("(", tokens2[1].Value); Assert.AreEqual(Token.TokenType.Expression, tokens2[2].Type); Assert.AreEqual(")", tokens2[3].Value); }
public static void LoadSettings() { // Get the is precompiled and editor state from the settings: Module settings = Precompiler.GetModule("PowerUI"); if (settings == null) { Precompiled = false; EditorMode = true; } else { Precompiled = true; EditorMode = settings.EditorMode; } }
/// <summary> /// <see cref="CurrentModule"/>で指定したソースコードを式として解析し、実行します。 /// </summary> /// <param name="source">ソースコード</param> public KecaknoahObject DoExpressionString(string source) { var le = Lexer.AnalyzeFromSource(source); if (!le.Success) { throw new KecaknoahException(le.Error); } var ast = Parser.ParseAsExpression(le); if (!ast.Success) { throw new KecaknoahException(le.Error); } var src = Precompiler.PrecompileExpression(ast); return(new KecaknoahContext(CurrentModule).Execute(src)); }
/****************************************************************************/ internal static IList <IExpression> ParseElementParams(string elementName, string source, IList <bool> isExplicitParam) { var result = new List <IExpression>(); var parser = new Parser(); var tokens = parser.Parse(source); var tokens2 = Precompiler.Precompile(tokens); if (tokens2[0].Value != ("#" + elementName)) { throw new Transformer.SyntaxException("Error in parsing element parameters"); } var start = 1; var end = tokens2.Count; if (tokens2.Count > 1 && tokens2[1].Value == "(") { ++start; --end; } for (var i = start; i < end; i += 2) { var token = tokens2[i]; var compiler = new Compiler(); if (token is ExpressionToken exprToken) { result.Add(compiler.InnerCompile(exprToken.Children)); } else if (IsExplicitParam(isExplicitParam, i - 2)) { result.Add(new Value(token)); } else { result.Add(compiler.InnerCompile(new Token[] { token })); } } return(result); }
public void Precompiler_Precompile_tertiary_nested_expression() { var parser = new Parser(); var tokens = parser.Parse("Name == 'bob' ? 'Jones' : (City == 'Chicago' ? 'Anderson' : 'Bell')"); var tokens2 = Precompiler.Precompile(tokens); Assert.IsNotNull(tokens2); Assert.AreEqual(5, tokens2.Count); Assert.AreEqual(Token.TokenType.Expression, tokens2[0].Type); Assert.AreEqual("?", tokens2[1].Value); Assert.AreEqual("Jones", tokens2[2].Value); Assert.AreEqual(":", tokens2[3].Value); Assert.AreEqual(Token.TokenType.Expression, tokens2[4].Type); var exprToken1 = tokens2[0] as ExpressionToken; var exprToken2 = tokens2[4] as ExpressionToken; Assert.AreEqual(3, exprToken1.Children.Count); Assert.AreEqual("Name", exprToken1.Children[0].Value); Assert.AreEqual("==", exprToken1.Children[1].Value); Assert.AreEqual("bob", exprToken1.Children[2].Value); Assert.AreEqual(5, exprToken2.Children.Count); Assert.AreEqual(Token.TokenType.Expression, exprToken2.Children[0].Type); Assert.AreEqual("?", exprToken2.Children[1].Value); Assert.AreEqual("Anderson", exprToken2.Children[2].Value); Assert.AreEqual(":", exprToken2.Children[3].Value); Assert.AreEqual("Bell", exprToken2.Children[4].Value); var exprToken3 = exprToken2.Children[0] as ExpressionToken; Assert.AreEqual("City", exprToken3.Children[0].Value); Assert.AreEqual("==", exprToken3.Children[1].Value); Assert.AreEqual("Chicago", exprToken3.Children[2].Value); }
private void Precompile() { if (!Precompiled) { // Undo the "PowerUI" precompiled module. Precompiler.Reverse("PowerUI"); return; } List <string> paths = new List <string>(); // Find PowerUI: string powerUIPath = PowerUIEditor.GetPowerUIPath(); paths.Add(powerUIPath + "/Source"); paths.Add(powerUIPath + "/Wrench"); Precompiler.Precompile( paths, "PowerUI", EditorMode ); }
public void Recompile() { Precompiler.Recompile("PowerUI", EditorMode); }
public static string Precompile(string testCodeFilePath) { return(Precompiler.Precompile(new StringReader(File.OpenText("..\\..\\" + testCodeFilePath).ReadToEnd()))); }