示例#1
0
        public Lib(string txt)
        {
            FunctionsExec = new List <FuncExec>();
            ReturnFunc    = new List <object>();
            FunctionCall  = false;
            Txt           = txt;

            var functions    = new List <Function>();
            var functionsTxt = txt.SplitSkip(";");

            foreach (var functionTxt in functionsTxt)
            {
                var functionTxtTrim = functionTxt.Trim();

                if (functionTxtTrim.StartsWith("class"))
                {
                    var l          = "class".Length + 1;
                    var classStruc = functionTxtTrim.Substring(l);
                    var idenEnd    = classStruc.IndexOf('{');

                    var iden = classStruc.Substring(0, idenEnd).Trim();

                    var classTxt = Ext.GetExpressionBr(classStruc);

                    var c = new CustomClass(iden, classTxt.txt, this);

                    Memory.Classes.Add(c);
                }
                else
                {
                    var function = new Function(functionTxtTrim, this);
                    functions.Add(function);
                }
            }
            Memory.Functions.AddRange(functions);
        }
示例#2
0
        public Expression(string txt, string block, Statement statement)
        {
            txt                = txt.Trim();
            Txt                = txt;
            Block              = block;
            Statement          = statement;
            CallFunctionReturn = null;
            AwaitReturn        = false;
            Lib                = Statement.Lib;

            var binaryOperators = _tokens[0]
                                  .OrderByDescending(x => x.Length)
                                  .ToArray();
            var unitaryOperators = _tokens[1]
                                   .OrderByDescending(x => x.Length)
                                   .ToArray();

            if (int.TryParse(txt, out _))
            {
                Type = EType.Int;

                var p = txt;

                Exec = () =>
                {
                    var value = p.GetInt();

                    var i = new IntClass(value);

                    return(i);
                };
                return;
            }

            if (GetStr(txt, out var qtxt))
            {
                if (qtxt.Length == txt.Length - 2)
                {
                    Type = EType.Str;

                    Exec = () =>
                    {
                        var value = qtxt.GetStr();

                        var s = new StrClass(value);

                        return(s);
                    };
                    return;
                }
            }

            if (bool.TryParse(txt, out var valueBool))
            {
                Type = EType.Bool;

                Exec = () =>
                {
                    var value = new BoolClass(valueBool);

                    return(value);
                };
                return;
            }

            if (txt.StartsWith("new"))
            {
                var ident = txt.Substring(3);
                if (GetStr(ident, out var brackets, '(', ')'))
                {
                    CustomClass customClass          = null;
                    var         i                    = ident.IndexOf("(" + brackets + ")", StringComparison.Ordinal);
                    var         identWb              = ident.Remove(i, brackets.Length + 2).Trim();
                    var         parametersExpresions = new List <Expression>();

                    if (!string.IsNullOrWhiteSpace(brackets))
                    {
                        var parametersTxt = brackets.Split(',');

                        foreach (var parantese in parametersTxt)
                        {
                            var paranteseTrim = parantese.Trim();

                            var expresion = new Expression(paranteseTrim, Block, Statement);
                            parametersExpresions.Add(expresion);
                        }
                    }

                    Exec = () =>
                    {
                        if (!AwaitReturn)
                        {
                            var parametersValue = parametersExpresions.Select(x => x.GetValue()).ToList();

                            customClass = Memory.GetClass(identWb, Block).Clone();

                            customClass.Assign(parametersValue);
                            AwaitReturn = true;

                            return(new Return(null, Flag.Function));
                        }
                        else
                        {
                            AwaitReturn = false;
                            return((IClass)customClass);
                        }
                    };
                }
                return;
            }

            var l = txt.First() == '(';
            var f = txt.Last() == ')';

            if (l && f)
            {
                txt = txt.Substring(1, txt.Length - 2);

                Type = EType.Par;

                var exp = new Expression(txt, Block, Statement);

                Exec = exp.Exec;
                return;
            }

            foreach (var @operator in unitaryOperators)
            {
                if (txt.StartsWith(@operator))
                {
                    Type = EType.None;


                    var len     = @operator.Length;
                    var exprTxt = txt.Substring(len, txt.Length - len);
                    var expr    = new Expression(exprTxt, Block, Statement);

                    if (@operator == "-")
                    {
                        Exec = () =>
                        {
                            var value = expr.GetValue();

                            if (value is int i)
                            {
                                value = -i;
                            }

                            if (value is string s)
                            {
                                if (s[0] == '-')
                                {
                                    value = s.Substring(1, s.Length - 1);
                                }
                                else
                                {
                                    value = "-" + s;
                                }
                            }

                            if (expr.Type == EType.Ind)
                            {
                                var variable  = expr.Exec().ToString();
                                var variable1 = Memory.GetVariable(variable, Block);
                                if (value is string ss)
                                {
                                    variable1.SetValue(ss);
                                }

                                if (value is int ii)
                                {
                                    variable1.SetValue(ii);
                                }
                            }

                            return(value);
                        };
                        return;
                    }
                    if (@operator == "!")
                    {
                        Exec = () =>
                        {
                            var value = expr.GetValue();

                            if (value is int i)
                            {
                                value = i ^ int.MaxValue;
                            }

                            if (value is string s)
                            {
                                if (char.IsLower(s, 0))
                                {
                                    value = s.ToUpper();
                                }
                                else
                                {
                                    value = s.ToLower();
                                }
                            }

                            if (expr.Type == EType.Ind)
                            {
                                var variable  = expr.Exec().ToString();
                                var variable1 = Memory.GetVariable(variable, Block);
                                if (value is string ss)
                                {
                                    variable1.SetValue(ss);
                                }

                                if (value is int ii)
                                {
                                    variable1.SetValue(ii);
                                }
                            }

                            return(value);
                        };
                        return;
                    }
                    if (@operator == "++")
                    {
                        Exec = () =>
                        {
                            var value = expr.GetValue();

                            if (value is int i)
                            {
                                value = i + 1;
                            }

                            if (value is string s)
                            {
                                value = s.ToUpper();
                            }

                            if (expr.Type == EType.Ind)
                            {
                                var variable  = expr.Exec().ToString();
                                var variable1 = Memory.GetVariable(variable, Block);
                                if (value is string ss)
                                {
                                    variable1.SetValue(ss);
                                }

                                if (value is int ii)
                                {
                                    variable1.SetValue(ii);
                                }
                            }

                            return(value);
                        };
                        return;
                    }
                    if (@operator == "--")
                    {
                        Exec = () =>
                        {
                            var value = expr.GetValue();

                            if (value is int i)
                            {
                                value = i - 1;
                            }

                            if (value is string s)
                            {
                                value = s.ToLower();
                            }

                            if (expr.Type == EType.Ind)
                            {
                                var variable  = expr.Exec().ToString();
                                var variable1 = Memory.GetVariable(variable, Block);
                                if (value is string ss)
                                {
                                    variable1.SetValue(ss);
                                }

                                if (value is int ii)
                                {
                                    variable1.SetValue(ii);
                                }
                            }

                            return(value);
                        };
                        return;
                    }

                    return;
                }
                else if (txt.EndsWith(@operator))
                {
                    Type = EType.None;


                    var len     = @operator.Length;
                    var exprTxt = txt.Substring(0, txt.Length - len);
                    var expr    = new Expression(exprTxt, Block, Statement);


                    if (@operator == "++")
                    {
                        Exec = () =>
                        {
                            var value = expr.GetValue();

                            if (value is int i)
                            {
                                value = i + 1;
                            }

                            if (value is string s)
                            {
                                value = s.ToUpper();
                            }

                            if (expr.Type == EType.Ind)
                            {
                                var variable  = expr.Exec().ToString();
                                var variable1 = Memory.GetVariable(variable, Block);
                                if (value is string ss)
                                {
                                    variable1.SetValue(ss);
                                }

                                if (value is int ii)
                                {
                                    variable1.SetValue(ii);
                                }
                            }

                            return(value);
                        };
                        return;
                    }
                    if (@operator == "--")
                    {
                        Exec = () =>
                        {
                            var value = expr.GetValue();

                            if (value is int i)
                            {
                                value = i - 1;
                            }

                            if (value is string s)
                            {
                                value = s.ToLower();
                            }

                            if (expr.Type == EType.Ind)
                            {
                                var variable  = expr.Exec().ToString();
                                var variable1 = Memory.GetVariable(variable, Block);
                                if (value is string ss)
                                {
                                    variable1.SetValue(ss);
                                }

                                if (value is int ii)
                                {
                                    variable1.SetValue(ii);
                                }
                            }

                            return(value);
                        };
                        return;
                    }
                }
            }

            var ops = new List <(int i, int l, string o)>();

            foreach (var @operator in binaryOperators)
            {
                for (int i = txt.IndexOf(@operator, StringComparison.Ordinal); i > -1; i = txt.IndexOf(@operator, i + 1, StringComparison.Ordinal))
                {
                    var toAdd = (i, @operator.Length, @operator);
                    var add   = true;

                    foreach (var op in ops)
                    {
                        var overlap = toAdd.i < op.i + op.l && op.i < toAdd.i + toAdd.Length;
                        if (overlap)
                        {
                            add = false;
                            break;
                        }
                    }

                    if (add)
                    {
                        ops.Add(toAdd);
                    }
                }
            }

            ops = ops.OrderBy(x =>
            {
                if (priority.Contains(x.o))
                {
                    return(0);
                }
                else
                {
                    return(x.i);
                }
            }).ToList();

            var exprsStr = txt.Split(binaryOperators, 2, StringSplitOptions.None);

            if (exprsStr.Length > 1)
            {
                var op = txt.Substring(ops[0].i, ops[0].l);

                var p = txt.IndexOf('(');
                if (p > ops[0].i || p == -1)
                {
                    var exps1 = txt.Substring(0, ops[0].i);
                    var exps2 = txt.Substring(ops[0].i + 1 + ops[0].l);

                    var exp1 = new Expression(exps1, Block, Statement);
                    var exp2 = new Expression(exps2, Block, Statement);

                    Type = EType.Asg;

                    SetOperation(op, exp1, exp2);
                    return;
                }
            }


            if (GetStr(Txt, out var paranteses, '(', ')'))
            {
                Type = EType.Func;

                var indexOfParant        = Txt.IndexOf('(' + paranteses + ')', StringComparison.Ordinal);
                var function             = Txt.Substring(0, indexOfParant).Trim();
                var parametersExpresions = new List <Expression>();

                if (!string.IsNullOrWhiteSpace(paranteses))
                {
                    parametersExpresions = paranteses.Split(',')
                                           .Select(x => new Expression(x.Trim(),
                                                                       Block,
                                                                       Statement))
                                           .ToList();
                }

                if (function.Contains("."))
                {
                    var classAccess     = function;
                    var exprClassAccess = new Expression(classAccess, block, Statement);
                    Exec = () =>
                    {
                        if (!AwaitReturn)
                        {
                            var value = exprClassAccess.GetValue();

                            if (value is Function func)
                            {
                                var parameters = parametersExpresions.Select(x => x.GetValue()).ToList();

                                Lib.AccessNewFunction(func, parameters);
                                AwaitReturn = true;
                            }
                            else if (value is Variable variable)
                            {
                                if (variable.Type == typeof(Function))
                                {
                                    var func2      = (Function)variable.GetValue();
                                    var parameters = parametersExpresions.Select(x => x.GetValue()).ToList();

                                    Lib.AccessNewFunction(func2, parameters);
                                    AwaitReturn = true;
                                }
                            }

                            return(null);
                        }
                        else
                        {
                            CallFunctionReturn = Lib.GetReturn();
                            AwaitReturn        = false;

                            if (CallFunctionReturn is null)
                            {
                                return(new Return());
                            }
                            else if (CallFunctionReturn is Return r)
                            {
                                return(r.Value);
                            }
                            else
                            {
                                return(CallFunctionReturn);
                            }
                        }
                    };
                    return;
                }
                else
                {
                    Exec = () =>
                    {
                        if (!AwaitReturn)
                        {
                            AwaitReturn = true;

                            var parameters = parametersExpresions.Select(x => x.GetValue()).ToList();

                            Lib.AccessNewFunction(function, parameters);

                            return(new Return(null, Flag.Function));
                        }
                        else
                        {
                            CallFunctionReturn = Lib.GetReturn();
                            AwaitReturn        = false;

                            if (CallFunctionReturn is null)
                            {
                                return(new Return());
                            }
                            else if (CallFunctionReturn is Return r)
                            {
                                return(r.Value);
                            }
                            else
                            {
                                return(CallFunctionReturn);
                            }
                        }
                    };
                }

                return;
            }

            if (Txt.Contains("."))
            {
                Type = EType.Cls;

                var dotIndex = Txt.LastIndexOf('.');
                var split    = Txt.SplitAtIndex(dotIndex);
                var second   = split[1].Substring(1);

                var exprTxt = split[0];
                var expr    = new Expression(exprTxt, block, Statement);
                Exec = () =>
                {
                    var value = expr.GetValue();
                    if (value is IClass @class)
                    {
                        return(@class.Get(second));
                    }

                    return(null);
                };

                return;
            }

            Type = EType.Ind;
            Exec = () => GetIdentifier(txt);
        }
示例#3
0
        public CustomClass Clone()
        {
            var classClone = new CustomClass(Identifier, Txt, Lib, Block);

            return(classClone);
        }