Пример #1
0
        private AST.Definition Definition()
        {
            var definition = new AST.Definition();


            // function can be either "static private xyz" or "private static xyz"
            // and Enforce actually allows a function to be "static private static xyz"
            if (Accept("static"))
            {
                definition.@static = true;
            }

            if (Accept("private"))
            {
                definition.access_modifier = AccessModifier.@private;
            }
            else if (Accept("protected"))
            {
                definition.access_modifier = AccessModifier.@protected;
            }
            else // default is private
            {
                definition.access_modifier = AccessModifier.@private;
            }

            if (Accept("static"))
            {
                definition.@static = true;
            }


            var type = GetNextSymbol();

            definition.name = GetNextSymbol();



            // now we know it's a function definition
            if (Accept("("))
            {
                GoToPreviousSymbol();
                AST.FunctionDefinition function = new AST.FunctionDefinition(definition);
                function.return_type = type;
                symbol_table.AddDefinition(function);
                function.args = ArgList();
                function.body = Block();
                return(function);
            }
            else
            {
                AST.VariableDefinition variable = new AST.VariableDefinition(definition);
                variable.type = type;
                symbol_table.AddDefinition(variable);

                // No Value assigned to the variable
                if (Accept(";"))
                {
                    return(variable);
                }
                else if (Accept("="))
                {
                    var assignment = new AST.Assignment();
                    assignment.left  = variable;
                    assignment.right = Expression();
                    variable.init    = assignment;
                    return(variable);
                }
                else
                {
                    throw new AST.UnexpectedSymbolException(GetLastSymbol());
                }
            }
            throw new Exception("We should never be here!");
        }
Пример #2
0
 public void AddDefinition(AST.FunctionDefinition def)
 {
     table.Push(new Symbol {
         type = Type.function_definition, def = def
     });
 }
 virtual public void visit(FunctionDefinition node)
 {
     node.body.accept(this);
 }