コード例 #1
0
ファイル: CodeGenerator.cs プロジェクト: sw417f19/CBlunt
        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);
        }
コード例 #2
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));
        }