Example #1
0
        private Func <Dictionary <string, bool>, bool> BuildFuncFromStr(Token token)
        {
            string codeStr = "";

            foreach (Token subToken in token.SubTokens)
            {
                if (subToken.Type == TokenType.COMMENT)
                {
                    continue;
                }
                else if (subToken.Type == TokenType.STR)
                {
                    codeStr = subToken.Value;
                }
                else
                {
                    throw new Exception("Unknown token in file!");
                }
            }

            return
                (delegate(Dictionary <string, bool> inputs) {
                Script <bool> script = CSharpScript.Create <bool>(
                    codeStr,
                    ScriptOptions.Default.WithImports("System.Collections.Generic"),
                    typeof(ExternalFuncGlobal));
                script.Compile();

                ExternalFuncGlobal globals = new ExternalFuncGlobal(inputs);
                bool result = script.RunAsync(globals).Result.ReturnValue;

                return result;
            });
        }
Example #2
0
        private Func <Dictionary <string, bool>, bool> BuildFuncFromFile(Token token)
        {
            string fileName = "";

            foreach (Token subToken in token.SubTokens)
            {
                if (subToken.Type == TokenType.ATTR)
                {
                    if (subToken.SubTokens[0].Type != TokenType.IDENT || subToken.SubTokens[1].Type != TokenType.STR)
                    {
                        throw new Exception("Malformed attribute. System error. Sorry :(");
                    }
                    else if (subToken.SubTokens[0].Value == "src")
                    {
                        fileName = subToken.SubTokens[1].Value;
                    }
                    else
                    {
                        throw new Exception("Unknown and attribute: " + subToken.SubTokens[0].Value);
                    }
                }
                else if (subToken.Type == TokenType.COMMENT)
                {
                    continue;
                }
                else
                {
                    throw new Exception("Unknown token in file!");
                }
            }

            return
                (delegate(Dictionary <string, bool> inputs) {
                string funcStr = File.ReadAllText(fileName);

                Script <bool> script = CSharpScript.Create <bool>(
                    funcStr,
                    ScriptOptions.Default.WithImports("System.Collections.Generic"),
                    typeof(ExternalFuncGlobal));
                script.Compile();

                ExternalFuncGlobal globals = new ExternalFuncGlobal(inputs);
                bool result = script.RunAsync(globals).Result.ReturnValue;

                return result;
            });
        }