public override string VisitProcedureDeclaration(ProcedureDeclarationNode procedureDeclaration)
        {
            var str = "";
            //var str = $"{AddSpaces()}public void {procedureDeclaration.ProcedureId}\r\n";
            // str += AddSpaces() + "{\r\n";
            var symbols = procedureDeclaration.Annotations["SymbolTable"] as ScopedSymbolTable;

            current = symbols;
            var param = CreateParameters(procedureDeclaration, symbols);

            if (procedureDeclaration.Annotations.ContainsKey("Nested"))
            {
                str += $"{AddSpaces()}void {procedureDeclaration.ProcedureId}({param})\r\n";
            }
            else
            {
                str += $"{AddSpaces()}public static void {procedureDeclaration.ProcedureId}({param})\r\n";
            }
            //CurrentScope = new ScopedSymbolTable(procedureDeclaration.ProcedureId, CurrentScope);

            str += VisitBlock(procedureDeclaration.Block);

            if (procedureDeclaration.Annotations.ContainsKey("Nested"))
            {
                // str = str.Remove(str.Length - 2);
                // str += ";\r\n";
            }
            //CurrentScope = CurrentScope.ParentScope;
            current = symbols.ParentScope;
            //str += AddSpaces() + "}\r\n";
            return(str);
        }
Beispiel #2
0
        public void Visit(ProcedureDeclarationNode node)
        {
            var    procedureName = node.AnchorToken.Lexeme;
            var    procedure     = procedureTable[procedureName];
            Type   type          = procedure.type;
            string returnType    = type.ToCilType();
            var    lastScope     = currentScope;

            currentScope = procedureName;

            builder.Append($"\t.method public static {returnType} '{procedureName}'(");
            builder.Append(DeclareParameters(procedureName));

            builder.AppendLine("){");
            builder.AppendLine(DeclareVariablesOnScope(procedureName));
            builder.AppendLine(InitializeVariablesOnScope(currentScope));

            if (node.Last() is StatementListNode)
            {
                Visit((dynamic)node.Last());
            }

            builder.AppendLine("\t\tret");
            builder.AppendLine("\t}");

            currentScope = lastScope;
        }
Beispiel #3
0
        public Node ProcedureDeclaration()
        {
            Expect(TokenCategory.PROCEDURE);
            var procedure_node = new ProcedureDeclarationNode()
            {
                AnchorToken = Expect(TokenCategory.IDENTIFIER)
            };

            Expect(TokenCategory.PARENTHESIS_OPEN);

            procedure_node.Add(new ParameterDeclarationNode()
            {
                ZeroOrMore(TokenCategory.IDENTIFIER, ParameterDeclaration)
            });

            Expect(TokenCategory.PARENTHESIS_CLOSE);

            var typeNode = Optional(TokenCategory.COLON, Type, true);

            if (typeNode == null)
            {
                typeNode = new VoidTypeNode();
            }
            procedure_node.Add(typeNode);

            Expect(TokenCategory.SEMICOLON);
            procedure_node.Add(Optional(TokenCategory.CONST, () =>
            {
                var node = new ConstantListNode()
                {
                    AnchorToken = Expect(TokenCategory.CONST)
                };
                node.Add(OneOrMore(TokenCategory.IDENTIFIER, ConstantDeclaration));
                return(node);
            }));

            procedure_node.Add(Optional(TokenCategory.VAR, () =>
            {
                var node = new VariableDeclarationNode()
                {
                    AnchorToken = Expect(TokenCategory.VAR)
                };
                node.Add(OneOrMore(TokenCategory.IDENTIFIER, VariableDeclaration));
                return(node);
            }));

            Expect(TokenCategory.BEGIN);

            procedure_node.Add(new StatementListNode()
            {
                ZeroOrMore(firstOfStatement, Statement)
            });

            Expect(TokenCategory.END);
            Expect(TokenCategory.SEMICOLON);

            return(procedure_node);
        }
        private string VisitProcedureDec(ProcedureDeclarationNode procedureDeclaration)
        {
            var prev    = CurrentScope;
            var namedec = $"{AddSpaces()}procedure {procedureDeclaration.ProcedureId}{CurrentScope.ScopeLevel}";

            CurrentScope = new ScopedSymbolTable(procedureDeclaration.ProcedureId, prev);
            var dec =
                $"{namedec}({VisitProcedureDecParams(procedureDeclaration.Parameters)});\r\n" +
                VisitBlock(procedureDeclaration.Block);

            CurrentScope = prev;
            return(dec);
        }
Beispiel #5
0
        public Type Visit(ProcedureDeclarationNode node)
        {
            var procedureName = node.AnchorToken.Lexeme;

            if (procedureTable.Contains(procedureName))
            {
                throw new SemanticError($"Duplicate procedure {procedureName}", node.AnchorToken);
            }
            Type procedureType = Visit((dynamic)node[1]);

            procedureTable[procedureName] = new ProcedureTable.Row(procedureType, false);
            currentScope = procedureName;

            Visit((dynamic)node[0]);
            VisitChildren(node, 2);

            currentScope = "";
            return(procedureType);
        }
Beispiel #6
0
 public virtual T VisitProcedureDeclaration(ProcedureDeclarationNode procedureDeclaration)
 {
     throw new NotImplementedException();
 }
 public string VisitProcedureDeclaration(ProcedureDeclarationNode procedureDeclaration)
 {
     throw new NotImplementedException();
 }
Beispiel #8
0
 public AddressNode VisitProcedureDeclaration(ProcedureDeclarationNode procedureDeclaration)
 {
     throw new System.NotImplementedException();
 }