public static IExpression GetExpression(ParseInfo parseInfo, Scope scope, DeltinScriptParser.ExprContext exprContext, bool selfContained = true, bool usedAsValue = true, Scope getter = null)
        {
            if (getter == null)
            {
                getter = scope;
            }

            switch (exprContext)
            {
            case DeltinScriptParser.E_numberContext number: return(new NumberAction(parseInfo.Script, number.number()));

            case DeltinScriptParser.E_trueContext @true: return(new BoolAction(parseInfo.Script, true));

            case DeltinScriptParser.E_falseContext @false: return(new BoolAction(parseInfo.Script, false));

            case DeltinScriptParser.E_nullContext @null: return(new NullAction());

            case DeltinScriptParser.E_stringContext @string: return(new StringAction(parseInfo.Script, @string.@string()));

            case DeltinScriptParser.E_formatted_stringContext formattedString: return(new StringAction(parseInfo, scope, formattedString.formatted_string()));

            case DeltinScriptParser.E_variableContext variable: return(GetVariable(parseInfo, scope, variable.variable(), selfContained));

            case DeltinScriptParser.E_methodContext method: return(new CallMethodAction(parseInfo, scope, method.method(), usedAsValue, getter));

            case DeltinScriptParser.E_new_objectContext newObject: return(new CreateObjectAction(parseInfo, scope, newObject.create_object()));

            case DeltinScriptParser.E_expr_treeContext exprTree: return(new ExpressionTree(parseInfo, scope, exprTree, usedAsValue));

            case DeltinScriptParser.E_array_indexContext arrayIndex: return(new ValueInArrayAction(parseInfo, scope, arrayIndex));

            case DeltinScriptParser.E_create_arrayContext createArray: return(new CreateArrayAction(parseInfo, scope, createArray.createarray()));

            case DeltinScriptParser.E_expr_groupContext group: return(GetExpression(parseInfo, scope, group.exprgroup().expr()));

            case DeltinScriptParser.E_type_convertContext typeConvert: return(new TypeConvertAction(parseInfo, scope, typeConvert.typeconvert()));

            case DeltinScriptParser.E_notContext not: return(new NotAction(parseInfo, scope, not.expr()));

            case DeltinScriptParser.E_inverseContext inverse: return(new InverseAction(parseInfo, scope, inverse.expr()));

            case DeltinScriptParser.E_op_1Context op1: return(new OperatorAction(parseInfo, scope, op1));

            case DeltinScriptParser.E_op_2Context op2: return(new OperatorAction(parseInfo, scope, op2));

            case DeltinScriptParser.E_op_boolContext opBool: return(new OperatorAction(parseInfo, scope, opBool));

            case DeltinScriptParser.E_op_compareContext opCompare: return(new OperatorAction(parseInfo, scope, opCompare));

            case DeltinScriptParser.E_ternary_conditionalContext ternary: return(new TernaryConditionalAction(parseInfo, scope, ternary));

            case DeltinScriptParser.E_rootContext root: return(new RootAction(parseInfo.TranslateInfo));

            default: throw new Exception($"Could not determine the expression type '{exprContext.GetType().Name}'.");
            }
        }
Ejemplo n.º 2
0
        Element ParseExpression(DeltinScriptParser.ExprContext context)
        {
            // If the expression is a(n)...

            #region Operation

            //   0       1      2
            // (expr operation expr)
            // count == 3
            if (context.ChildCount == 3 &&
                (Constants.MathOperations.Contains(context.GetChild(1).GetText()) ||
                 Constants.CompareOperations.Contains(context.GetChild(1).GetText()) ||
                 Constants.BoolOperations.Contains(context.GetChild(1).GetText())))
            {
                Element left      = ParseExpression(context.GetChild(0) as DeltinScriptParser.ExprContext);
                string  operation = context.GetChild(1).GetText();
                Element right     = ParseExpression(context.GetChild(2) as DeltinScriptParser.ExprContext);

                if (Constants.BoolOperations.Contains(context.GetChild(1).GetText()))
                {
                    if (left.ElementData.ValueType != Elements.ValueType.Any && left.ElementData.ValueType != Elements.ValueType.Boolean)
                    {
                        throw new SyntaxErrorException($"Expected boolean datatype, got {left .ElementData.ValueType.ToString()} instead.", context.start);
                    }
                    if (right.ElementData.ValueType != Elements.ValueType.Any && right.ElementData.ValueType != Elements.ValueType.Boolean)
                    {
                        throw new SyntaxErrorException($"Expected boolean datatype, got {right.ElementData.ValueType.ToString()} instead.", context.start);
                    }
                }

                switch (operation)
                {
                case "^":
                    return(Element.Part <V_RaiseToPower>(left, right));

                case "*":
                    return(Element.Part <V_Multiply>(left, right));

                case "/":
                    return(Element.Part <V_Divide>(left, right));

                case "+":
                    return(Element.Part <V_Add>(left, right));

                case "-":
                    return(Element.Part <V_Subtract>(left, right));

                case "%":
                    return(Element.Part <V_Modulo>(left, right));

                // COMPARE : '<' | '<=' | '==' | '>=' | '>' | '!=';

                case "&":
                    return(Element.Part <V_And>(left, right));

                case "|":
                    return(Element.Part <V_Or>(left, right));

                case "<":
                    return(Element.Part <V_Compare>(left, Operators.LessThan, right));

                case "<=":
                    return(Element.Part <V_Compare>(left, Operators.LessThanOrEqual, right));

                case "==":
                    return(Element.Part <V_Compare>(left, Operators.Equal, right));

                case ">=":
                    return(Element.Part <V_Compare>(left, Operators.GreaterThanOrEqual, right));

                case ">":
                    return(Element.Part <V_Compare>(left, Operators.GreaterThan, right));

                case "!=":
                    return(Element.Part <V_Compare>(left, Operators.NotEqual, right));
                }
            }

            #endregion

            #region Not

            if (context.GetChild(0) is DeltinScriptParser.NotContext)
            {
                return(Element.Part <V_Not>(ParseExpression(context.GetChild(1) as DeltinScriptParser.ExprContext)));
            }

            #endregion

            #region Number

            if (context.GetChild(0) is DeltinScriptParser.NumberContext)
            {
                var number = context.GetChild(0);

                double num = double.Parse(number.GetChild(0).GetText());

                /*
                 * // num will have the format expr(number(X)) if positive, expr(number(neg(X))) if negative.
                 * if (number.GetChild(0) is DeltinScriptParser.NegContext)
                 *  // Is negative, use '-' before int.parse to make it negative.
                 *  num = -double.Parse(number.GetChild(0).GetText());
                 * else
                 *  // Is positive
                 *  num = double.Parse(number.GetChild(0).GetText());
                 */

                return(new V_Number(num));
            }

            #endregion

            #region Boolean

            // True
            if (context.GetChild(0) is DeltinScriptParser.TrueContext)
            {
                return(new V_True());
            }

            // False
            if (context.GetChild(0) is DeltinScriptParser.FalseContext)
            {
                return(new V_False());
            }

            #endregion

            #region String

            if (context.GetChild(0) is DeltinScriptParser.StringContext)
            {
                return(V_String.ParseString(
                           context.start,
                           // String will look like "hey this is the contents", trim the quotes.
                           (context.GetChild(0) as DeltinScriptParser.StringContext).STRINGLITERAL().GetText().Trim('\"'),
                           null
                           ));
            }

            #endregion

            #region Formatted String

            if (context.GetChild(1) is DeltinScriptParser.StringContext)
            {
                Element[] values = context.expr().Select(expr => ParseExpression(expr)).ToArray();
                return(V_String.ParseString(
                           context.start,
                           (context.GetChild(1) as DeltinScriptParser.StringContext).STRINGLITERAL().GetText().Trim('\"'),
                           values
                           ));
            }

            #endregion

            #region null

            if (context.GetChild(0) is DeltinScriptParser.NullContext)
            {
                return(new V_Null());
            }

            #endregion

            #region Group ( expr )

            if (context.ChildCount == 3 && context.GetChild(0).GetText() == "(" &&
                context.GetChild(1) is DeltinScriptParser.ExprContext &&
                context.GetChild(2).GetText() == ")")
            {
                Console.WriteLine("Group type:" + context.GetChild(0).GetType());
                return(ParseExpression(context.GetChild(1) as DeltinScriptParser.ExprContext));
            }

            #endregion

            #region Method

            if (context.GetChild(0) is DeltinScriptParser.MethodContext)
            {
                return(ParseMethod(context.GetChild(0) as DeltinScriptParser.MethodContext, true));
            }

            #endregion

            #region Variable

            if (context.GetChild(0) is DeltinScriptParser.VariableContext)
            {
                return(DefinedVar.GetVar((context.GetChild(0) as DeltinScriptParser.VariableContext).PART().GetText(), context.start).GetVariable(new V_EventPlayer()));
            }

            #endregion

            #region Array

            if (context.ChildCount == 4 && context.GetChild(1).GetText() == "[" && context.GetChild(3).GetText() == "]")
            {
                return(Element.Part <V_ValueInArray>(
                           ParseExpression(context.expr(0) as DeltinScriptParser.ExprContext),
                           ParseExpression(context.expr(1) as DeltinScriptParser.ExprContext)));
            }

            #endregion

            #region Create Array

            if (context.ChildCount >= 4 && context.GetChild(0).GetText() == "[")
            {
                var      expressions = context.expr();
                V_Append prev        = null;
                V_Append current     = null;

                for (int i = 0; i < expressions.Length; i++)
                {
                    current = new V_Append()
                    {
                        ParameterValues = new object[2]
                    };

                    if (prev != null)
                    {
                        current.ParameterValues[0] = prev;
                    }
                    else
                    {
                        current.ParameterValues[0] = new V_EmptyArray();
                    }

                    current.ParameterValues[1] = ParseExpression(expressions[i]);
                    prev = current;
                }

                return(current);
            }

            #endregion

            #region Empty Array

            if (context.ChildCount == 2 && context.GetText() == "[]")
            {
                return(Element.Part <V_EmptyArray>());
            }

            #endregion

            #region Seperator/enum

            if (context.ChildCount == 3 && context.GetChild(1).GetText() == ".")
            {
                Element left         = ParseExpression(context.GetChild(0) as DeltinScriptParser.ExprContext);
                string  variableName = context.GetChild(2).GetChild(0).GetText();

                DefinedVar var = DefinedVar.GetVar(variableName, context.start);

                return(var.GetVariable(left));
            }

            #endregion

            throw new Exception($"What's a {context.GetType().Name}?");
        }