コード例 #1
0
        override public void Visit(AST_variable_declaration variable_declaration)
        {
            if (variable_declaration.Type != null)
            {
                variable_declaration.Type.Accept(this);
            }
            string name = variable_declaration.Identifier.Name;

            if (variables.ContainsKey(name))
            {
                Error("Variable '" + name + "' has already been declared.", variable_declaration.Identifier);
            }
            else
            {
                AST_expression expr = variable_declaration.Expression;
                if (expr != null)
                {
                    expr.Accept(this);
                    if (expr.DataType != variable_declaration.Type.Kind)
                    {
                        Error("Variable and initial value must be same type.", variable_declaration.Expression);
                    }
                }
                variables.Add(name, variable_declaration);
            }
            variable_declaration.Identifier.Accept(this);
        }
コード例 #2
0
        override public void Visit(AST_for_statement for_statement)
        {
            for_statement.Identifier.Accept(this);
            if (for_statement.From != null)
            {
                for_statement.From.Accept(this);
            }
            if (for_statement.To != null)
            {
                for_statement.To.Accept(this);
            }

            string name             = for_statement.Identifier.Name;
            bool   variableReserved = false;
            AST_variable_declaration declaration = variables.GetValueOrDefault(name);

            if (declaration != null)
            {
                if (variables.GetValueOrDefault(name).Type.Kind == int_type)
                {
                    // Make sure the variable is not already a loop variable in an outer loop
                    if (loopVariables.ContainsKey(for_statement.Identifier.Name))
                    {
                        Error("'" + name + "' is already a loop variable.", for_statement.Identifier);
                    }
                    else
                    {
                        loopVariables.Add(name, false);
                        variableReserved = true;
                    }
                }
                else
                {
                    Error("Loop variable must be of int type.", for_statement.Identifier);
                }
            }
            if (for_statement.From != null && for_statement.From.DataType != int_type)
            {
                Error("Lower boundary must be of int type.", for_statement.From);
            }
            if (for_statement.To != null && for_statement.To.DataType != int_type)
            {
                Error("Upper boundary must be of int type.", for_statement.To);
            }
            if (for_statement.StatementList != null)
            {
                for_statement.StatementList.Accept(this);
            }
            if (variableReserved)
            {
                loopVariables.Remove(name);
            }
        }
コード例 #3
0
ファイル: ASTVisitor.cs プロジェクト: hannuorn/Mini-PL
 public virtual void Visit(AST_variable_declaration variable_declaration)
 {
     IncrementDepth();
     variable_declaration.Identifier.Accept(this);
     if (variable_declaration.Type != null)
     {
         variable_declaration.Type.Accept(this);
     }
     if (variable_declaration.Expression != null)
     {
         variable_declaration.Expression.Accept(this);
     }
     DecrementDepth();
 }
コード例 #4
0
        private AST_variable_declaration Parse_variable_declaration()
        {
            IncrementDepth();

            AST_variable_declaration variable_declaration;
            AST_identifier           name;
            AST_type       type       = null;
            AST_expression expression = null;

            Match(var_Keyword);
            name = Parse_identifier(Colon | Semicolon);
            if (!Match(Colon))
            {
                Error("':' expected.", lastReadToken);
                SkipUntilFollow(Semicolon);
            }
            else
            {
                type = Parse_type(Assignment | Semicolon);
                switch (LookAheadToken().Kind)
                {
                case Assignment:
                    Match(Assignment);
                    expression = Parse_expression(Semicolon);
                    break;

                case Semicolon:
                    break;

                default:
                    Error("':=' or ';' expected.", LookAheadToken());
                    SkipUntilFollow(Semicolon);
                    break;
                }
            }
            variable_declaration = new AST_variable_declaration(name, type, expression);

            DecrementDepth();

            return(variable_declaration);
        }
コード例 #5
0
        override public void Visit(AST_variable_declaration variable_declaration)
        {
            Variable variable;

            if (variable_declaration.Expression != null)
            {
                variable_declaration.Expression.Accept(this);
                variable = value.Copy();
            }
            else
            {
                variable = new Variable(variable_declaration.Type.Kind);
            }
            string name = variable_declaration.Identifier.Name;

            if (variables.ContainsKey(name))
            {
                variables.Remove(name);
            }
            variables.Add(name, variable);
        }
コード例 #6
0
 override public void Visit(AST_variable_declaration variable_declaration)
 {
     variable_declaration.Accept(evaluateVisitor);
 }