Exemplo n.º 1
0
        public override SucoExpression Optimize(SucoEnvironment env, int?[] givens)
        {
            var constants      = ElementType.CreateArray(Elements.Count);
            var expressions    = new SucoExpression[Elements.Count];
            var anyExpressions = false;

            for (var i = 0; i < Elements.Count; i++)
            {
                var opt = Elements[i].Optimize(env, givens);
                if (opt is SucoConstant c)
                {
                    constants.SetValue(c.Value, i);
                }
                else
                {
                    expressions[i] = opt;
                    anyExpressions = true;
                }
            }

            if (!anyExpressions)
            {
                return(new SucoConstant(StartIndex, EndIndex, Type, constants));
            }
            return(new SucoOptimizedArrayExpression(StartIndex, EndIndex, constants, expressions, Type));
        }
Exemplo n.º 2
0
        public override SucoExpression Optimize(SucoEnvironment env, int?[] givens)
        {
            var optimizedOperand = Operand.Optimize(env, givens);
            var optimizedArgs    = Arguments.Select(arg => arg.Optimize(env, givens)).ToArray();

            return(optimizedOperand is SucoConstant constOperand && optimizedArgs.All(arg => arg is SucoConstant)
                ? new SucoConstant(StartIndex, EndIndex, Type, ((SucoFunction)constOperand.Value).Interpret(optimizedArgs.Select(a => a.Type).ToArray(), optimizedArgs.Select(a => ((SucoConstant)a).Value).ToArray()))
                : new SucoCallExpression(StartIndex, EndIndex, optimizedOperand, optimizedArgs, Type));
        }
Exemplo n.º 3
0
        public override object Interpret(SucoEnvironment env, int?[] grid)
        {
            var result = Operand.Interpret(env, grid);

            if (result is SucoFunction fnc)
            {
                return(fnc.Interpret(Arguments.Select(a => a.Type).ToArray(), Arguments.Select(a => a.Interpret(env, grid)).ToArray()));
            }
            throw new SucoCompileException($"Operand isn’t a function.", Operand.StartIndex, Operand.EndIndex);
        }
Exemplo n.º 4
0
        public override object Interpret(SucoEnvironment env, int?[] grid)
        {
            var array = ElementType.CreateArray(Elements.Count);

            for (var i = 0; i < Elements.Count; i++)
            {
                array.SetValue(Elements[i].Interpret(env, grid), i);
            }
            return(array);
        }
Exemplo n.º 5
0
        public override object Interpret(SucoEnvironment env, int?[] grid)
        {
            var result = (Array)Constants.Clone();

            for (var i = 0; i < Expressions.Length; i++)
            {
                if (Expressions[i] != null)
                {
                    result.SetValue(Expressions[i].Interpret(env, grid), i);
                }
            }
            return(result);
        }
Exemplo n.º 6
0
        public override SucoExpression Optimize(SucoEnvironment env, int?[] givens)
        {
            var newPieces = new List <SucoStringLiteralPiece>();

            foreach (var piece in Pieces)
            {
                string addString = null;
                if (piece is SucoStringLiteralPieceString st)
                {
                    addString = st.StringValue;
                }
                else if (piece is SucoStringLiteralPieceExpression expr)
                {
                    var optimized = expr.Expression.Optimize(env, givens);
                    if (optimized is SucoConstant c)
                    {
                        addString = (string)c.Value;
                    }
                    else
                    {
                        newPieces.Add(piece);
                    }
                }

                if (addString != null && addString.Length > 0)
                {
                    if (newPieces.Count > 0 && newPieces[newPieces.Count - 1] is SucoStringLiteralPieceString s)
                    {
                        newPieces[newPieces.Count - 1] = new SucoStringLiteralPieceString(s.StringValue + addString);
                    }
                    else
                    {
                        newPieces.Add(new SucoStringLiteralPieceString(addString));
                    }
                }
            }
            if (newPieces.Count == 1 && newPieces[0] is SucoStringLiteralPieceString str)
            {
                return(new SucoConstant(StartIndex, EndIndex, Type, str.StringValue));
            }
            return(new SucoStringLiteralExpression(StartIndex, EndIndex, newPieces.ToArray(), Type));
        }
Exemplo n.º 7
0
        public override object Interpret(SucoEnvironment env, int?[] grid)
        {
            var result = new StringBuilder();

            foreach (var piece in Pieces)
            {
                if (piece is SucoStringLiteralPieceString str)
                {
                    result.Append(str.StringValue);
                }
                else if (piece is SucoStringLiteralPieceExpression expr)
                {
                    result.Append(expr.Expression.Interpret(env, grid));
                }
                else
                {
                    throw new SucoTempCompileException($"Unexpected type of string literal piece: “{piece.GetType().Name}”.");
                }
            }
            return(result.ToString());
        }
Exemplo n.º 8
0
 public abstract object Interpret(SucoEnvironment env, int?[] grid);
Exemplo n.º 9
0
 public abstract SucoExpression Optimize(SucoEnvironment env, int?[] givens);
Exemplo n.º 10
0
 public override bool?Interpret(SucoEnvironment env, int?[] grid) => Name switch
 {
     "first" => env.GetLastPosition() == 1,
Exemplo n.º 11
0
        public override object Optimize(SucoEnvironment env, int?[] givens)
        {
            var result = Interpret(env, givens);

            return(result == null ? this : result);
        }
Exemplo n.º 12
0
 public virtual object InterpretMemberAccess(string memberName, object operand, SucoEnvironment env, int?[] grid) => throw new SucoTempCompileException($"Member “{memberName}” not defined on type “{this}”.");
Exemplo n.º 13
0
 public override object InterpretMemberAccess(string memberName, object operand, SucoEnvironment env, int?[] grid) => memberName switch
 {
     "hash" => MD5.ComputeHex((string)operand),
Exemplo n.º 14
0
 public override SucoExpression Optimize(SucoEnvironment env, int?[] givens) => this;