Example #1
0
        public override int VisitVariableedit([NotNull] CBluntParser.VariableeditContext context)
        {
#if DEBUG
            Console.WriteLine("VisitVariableedit");
#endif
            this.AddText(context.ID().GetText());
            Visit(context.equals());
            Visit(context.expression());
            return(0);
        }
Example #2
0
 /// <summary>
 /// Visit a parse tree produced by <see cref="CBluntParser.variableedit"/>.
 /// <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 VisitVariableedit([NotNull] CBluntParser.VariableeditContext context)
 {
     return(VisitChildren(context));
 }
Example #3
0
        public override int VisitVariableedit([NotNull] CBluntParser.VariableeditContext context)
        {
#if DEBUG
            Console.WriteLine("VisitVariableedit");
#endif
            // The name of the variable
            var variableName = context.ID().GetText();

            // The operator type (For example: = += /= so on)
            var operatorType = context.equals().GetText();

            // The assignment value
            var assignmentValue = context.expression().GetText();

            /// TODO: USE UTILITY METHOD

            // First iterate over the current scope and all previous scopes
            var currNode = SymbolTable.MethodScopeLinkedList.Last;

            // The properties of the variable we found
            VariableProperties variableProperties = null;

            variableProperties = GetDeclaredVariable(variableName);

            if (variableProperties == null)
            {
                SyntaxError(context, "Variable with name " + variableName + " cannot be assigned a value as it does not exist");
                return(1);
            }

            // Get the expected assignment value, aka the value we expect the variable to be assigned
            SymbolTable.ExpressionStoreLinkedList.AddLast(new ExpressionStore());

            Visit(context.expression());

            // Set assignment type from expression, compare it against operator type
            var assignmentType = SymbolTable.ExpressionStoreLinkedList.Last.Value.Type;

            SymbolTable.ExpressionStoreLinkedList.RemoveLast();

            switch (operatorType)
            {
            // Always allow default assignment
            case "=":
                break;

            case "+=":
                if (assignmentType != "number")
                {
                    SyntaxError(context, "Cannot use addition assignment operator on a type " + assignmentType);
                    return(1);
                }
                break;

            case "-=":
                if (assignmentType != "number")
                {
                    SyntaxError(context, "Cannot use subtraction assignment operator on a type " + assignmentType);
                    return(1);
                }
                break;

            case "*=":
                if (assignmentType != "number")
                {
                    SyntaxError(context, "Cannot use multiplication assignment operator on a type " + assignmentType);
                    return(1);
                }
                break;

            case "/=":
                if (assignmentType != "number")
                {
                    SyntaxError(context, "Cannot use division assignment operator on a type " + assignmentType);
                    return(1);
                }
                break;
            }

            // Now test if this variable's type is the type we are trying to assign
            if (variableProperties.Type != assignmentType)
            {
                SyntaxError(context, "Variable " + variableName + " is of type " + variableProperties.Type + ", cannot assign it a value of type " + assignmentType);
                return(1);
            }

            // If all checks passes, the variable will also be initialized
            variableProperties.Initialized = true;

            return(0);
        }