//protected CompileFuncDelegate CompileFunc;
        public void LiCompileFunction()
        {
            foreach (var ID in FunctionIdentifiers)
            {
                GotoLine(ID.Key);
                var FMeta = GetFunctionMeta();
                NextLine();
                var Ret = Expression.Label();
                Contextual.Push(Expression.Goto(Ret, FMeta.ReturnType));
                ExpressionTypeBeam.PushScope();
                foreach (var arg in FMeta.Parameters)
                {
                    ExpressionTypeBeam.CurrentScope.SetVarCached(arg.Name, Expression.Constant(arg.ArgType.DefaultValue(), arg.ArgType), false);
                }
                ExpressionTypeBeam.CurrentScope.DefineFunc(ID.Value, Expression.Lambda(ParseBlock(), ExpressionTypeBeam.PopScope()));
            }

            /*
             * return;
             * var Matches = Script.Where(x => Regex.IsMatch(x, "^(?:\t| )*func(.*?)$", RegexOptions.IgnoreCase)).ToList();
             * var Lines = Matches.Select(x => Array.IndexOf(Script, x));
             * List<ParameterExpression> Params = new List<ParameterExpression>();
             * foreach (var L in Lines)
             * {
             *  GotoLine(L);
             *  var FMeta = GetFunctionMeta();
             *  ExpressionTypeBeam.PushScope();
             *  List<Expression> PreFunction = new List<Expression>();
             *
             *  var MyFunc = ExpressionTypeBeam.CurrentScope.Parent.ScopeFunctions.First(x => x.Name == FMeta.Name && x.Parameters.SequenceEqual(FMeta.Parameters) && x.ReturnType == FMeta.ReturnType);
             *
             *  PreFunction.Add(VarAutExpression.VariableAccess("Return-store", false)
             *      .Setter(Expression.Constant(FMeta.ReturnType.DefaultValue(), FMeta.ReturnType)));
             *  PreFunction.AddRange(
             *      MyFunc.Parameters.Select(x =>
             *      {
             *          var PType = x.GetTypeInfo().IsValueType ? typeof(Nullable<>).MakeGenericType(x) : x;
             *          Params.Add(Expression.Parameter(PType));
             *          return VarAutExpression.VariableAccess(x.ArgName).Setter(
             *              Expression.Coalesce(Params.Last(), x.DefaultValue().ConvertTo(x))
             *          );
             *      })
             *  );
             *  var Ret = Expression.Label();
             *  Contextual.Push(Expression.Goto(Ret, FMeta.ReturnType));
             *
             *  var Block = ParseBlock();
             *
             *  Block.Add(Expression.Label(Ret));
             *  Block.Add(VarAutExpression.VariableAccess("Return-store", false).Getter(L.FDef.ReturnType));
             *  ExpressionTypeBeam.CurrentScope.DefineFunc(, Expression.Lambda(Expression.Block(ExpressionTypeBeam.PopScope(), Block), Params));
             *  Block.Clear();
             *  Params.Clear();
             * }*/
        }
Example #2
0
        public static LambdaExpression Parse(
            string s,
            IScope GlobalScope,
            IEnumerable <Assembly> RequireASM = null,
            IEnumerable <Type> RequireType    = null)
        {
            var l = new LiParser(
                Regex.Replace(s, ";(.*)((\r\n)|(\r)|(\n))", "\r\n")
                .Replace("\r\n", "\r")
                );

            l.Included = RequireASM != null?RequireASM.ToList() : new List <Assembly>();

            l.IncludedType = RequireType != null?RequireType.ToList() : new List <Type>();

            /*l.DefineFunc = D;
            *  l.CompileFunc = C;
            *  l.GetVar = G;
            *  l.SetVar = S;
            *  l.CreateVar = CR;*/
            GlobalScope.Parent = new ClrScope((RequireType ?? new List <Type>()).Concat((RequireASM ?? new List <Assembly>()).SelectMany(x => x.ExportedTypes)));
            ExpressionTypeBeam.InitializeParameterEngine(GlobalScope);


            List <Expression> Output = new List <Expression>();

            l.DefineGlobal();
            l.DeclareAllFunctions();
            l.LiCompileFunction();

            ExpressionTypeBeam.PushScope();
            l.Script = Regex.Replace(string.Join("\r", l.Script), "func(.*?)endfunc", "", RegexOptions.IgnoreCase | RegexOptions.Singleline).Split('\r');
            if (l.Script.Length > 0)
            {
                l.GotoLine(0);
            }

            Expression ex;

            var cmd = AutExpression.VariableAccess("CmdLine", false, Expression.Constant(1, typeof(int)), typeof(string)).Generator();

            l.ConsumeWS();
            if (l.ScriptLine != "" && !l.ScriptLine.StartsWith(";") && !l.EOF && !l.EOL)
            {
                ex = l.ParseBoolean();
                if (ex is VarAutExpression)
                {
                    ex = (ex as VarAutExpression).Generator();
                }
                Output.Add(ex);
            }
            while (!l.EOF)
            {
                try
                {
                    l.NextLine();
                }
                catch { break; }
                l.ConsumeWS();
                if (l.ScriptLine == "" || l.ScriptLine.StartsWith(";") || l.EOF || l.EOL)
                {
                    continue;
                }

                ex = l.ParseBoolean();
                if (ex is VarAutExpression)
                {
                    ex = (ex as VarAutExpression).Generator();
                }
                Output.Add(ex);
            }
            if (Output.Count(x => x != null) <= 0)
            {
                return(null);
            }
            var             SC = ExpressionTypeBeam.PopScope();
            BlockExpression e  = Expression.Block(SC.Where(x => x.Name != "CmdLine" || x.Type != typeof(string[])), Output.ToArray().Where(x => x != null));

            return(Expression.Lambda <Action <string[]> >(e, SC.First(x => x.Name == "CmdLine" && x.Type == typeof(string[]))));
        }