Beispiel #1
0
        public override int VisitCalculation([NotNull] CBluntParser.CalculationContext context)
        {
#if DEBUG
            Console.WriteLine("VisitCalculation");
#endif
            //operator has @ because operator is normally a reserved keyword
            Visit(context.@operator());
            //Visits either a parameter or expression
            Visit(context.GetChild(1));
            return(0);
        }
Beispiel #2
0
 /// <summary>
 /// Visit a parse tree produced by <see cref="CBluntParser.calculation"/>.
 /// <para>
 /// The default implementation returns the result of calling <see cref="AbstractParseTreeVisitor{Result}.VisitChildren(IRuleNode)"/>
 /// on <paramref name="context"/>.
 /// </para>
 /// </summary>
 /// <param name="context">The parse tree.</param>
 /// <return>The visitor result.</return>
 public virtual Result VisitCalculation([NotNull] CBluntParser.CalculationContext context)
 {
     return(VisitChildren(context));
 }
Beispiel #3
0
        public override int VisitCalculation([NotNull] CBluntParser.CalculationContext context)
        {
#if DEBUG
            Console.WriteLine("VisitCalculation");
#endif

            var operatorContext = context.@operator().GetText();

            // If a parameter was found, add it to the store
            if (context.parameter() != null)
            {
                var parameter     = context.parameter();
                var parameterType = GetParameterType(context, parameter);

                var prevExpressionStoreType = SymbolTable.ExpressionStoreLinkedList.Last.Value.ExpressionTypes.Last;

                if (prevExpressionStoreType == null)
                {
                    if (!AddToExpressionStore(parameterType))
                    {
                        return(1);
                    }

                    return(0);
                }

                var prevExpressionStoreValue = prevExpressionStoreType.Value;

                // Perform semantics on operator, it is not possible to subtract ex. a string from a number
                // If the type is already text, the numbers cannot turn the expression store into a number again. It will remain text
                switch (operatorContext)
                {
                case "+":
                    if (prevExpressionStoreValue == "number" && parameterType == "number")
                    {
                        if (SymbolTable.ExpressionStoreLinkedList.Last.Value.Type == "text")
                        {
                            break;
                        }

                        SymbolTable.ExpressionStoreLinkedList.Last.Value.Type = "number";
                        break;
                    }

                    if (prevExpressionStoreValue == "text" && parameterType == "text")
                    {
                        SymbolTable.ExpressionStoreLinkedList.Last.Value.Type = "text";
                        break;
                    }


                    if (prevExpressionStoreValue == "number" && parameterType == "text")
                    {
                        SymbolTable.ExpressionStoreLinkedList.Last.Value.Type = "text";
                        break;
                    }

                    if (prevExpressionStoreValue == "text" && parameterType == "number")
                    {
                        SymbolTable.ExpressionStoreLinkedList.Last.Value.Type = "text";
                        break;
                    }

                    SyntaxError(context, "Cannot add a variable of type " + prevExpressionStoreValue + " with a variable of type " + parameterType);
                    return(1);

                case "-":
                    if (prevExpressionStoreValue == "number" && parameterType == "number")
                    {
                        if (SymbolTable.ExpressionStoreLinkedList.Last.Value.Type == "text")
                        {
                            break;
                        }

                        SymbolTable.ExpressionStoreLinkedList.Last.Value.Type = "number";
                        break;
                    }

                    SyntaxError(context, "Cannot subtract a variable of type " + prevExpressionStoreValue + " with a variable of type " + parameterType);
                    return(1);

                case "*":
                    if (prevExpressionStoreValue == "number" && parameterType == "number")
                    {
                        if (SymbolTable.ExpressionStoreLinkedList.Last.Value.Type == "text")
                        {
                            break;
                        }

                        SymbolTable.ExpressionStoreLinkedList.Last.Value.Type = "number";
                        break;
                    }

                    SyntaxError(context, "Cannot multiply a variable of type " + prevExpressionStoreValue + " with a variable of type " + parameterType);
                    return(1);

                case "/":
                    if (prevExpressionStoreValue == "number" && parameterType == "number")
                    {
                        if (SymbolTable.ExpressionStoreLinkedList.Last.Value.Type == "text")
                        {
                            break;
                        }

                        SymbolTable.ExpressionStoreLinkedList.Last.Value.Type = "number";
                        break;
                    }

                    SyntaxError(context, "Cannot divide a variable of type " + prevExpressionStoreValue + " with a variable of type " + parameterType);
                    return(1);
                }

                if (!AddToExpressionStore(parameterType))
                {
                    return(1);
                }
            }

            // If there is an expression instead, visit it
            if (context.expression() != null)
            {
                if (Visit(context.expression()) == 1)
                {
                    return(1);
                }
            }

            return(base.VisitCalculation(context));
        }