public Type Visit(ProcedureDeclaration node) { var procedureName = node[0].AnchorToken.Lexeme; if (procedureTable.Contains(procedureName)) { throw new SemanticError("Duplicated identifier: " + procedureName, node[0].AnchorToken); } else { inProcedure = true; isParam = true; Visit((dynamic)node[1]); isParam = false; Visit((dynamic)node[3]); Visit((dynamic)node[4]); var type = Visit((dynamic)node[2]); string noP = "NP"; localSTables[procedureName] = LocalSTable; procedureTable[procedureName] = new Procedures(type, listOrderParams.Count, noP, listOrderParams, LocalSTable); procName = procedureName; Visit((dynamic)node[5]); LocalSTable = new SymbolTable(); listOrderParams = new List <dynamic>(); inProcedure = false; procName = ""; } return(Type.VOID); }
//----------------------------------------------------------- private string Visit(ProcedureDeclaration node, Table table) { string retString = ""; var procName = node.AnchorToken.Lexeme; var proc = GPTable[procName]; var procParamsNames = proc.getParametersNames(); var procStrParams = ""; if (procParamsNames.Length > 0) { procStrParams += CILTypes[proc.LocalSymbols[procParamsNames[0]].LocalType] + " '" + procParamsNames[0] + "'"; for (var i = 1; i < procParamsNames.Length; i++) { procStrParams += ", " + CILTypes[proc.LocalSymbols[procParamsNames[i]].LocalType] + " '" + procParamsNames[i] + "'"; } } var body = ""; foreach (var n in node) { // if (n is ParameterDeclarationList || n is SimpleType || n is ListType) { continue; } body += Visit((dynamic)n, proc.LocalSymbols); } retString += "\t.method public static " + CILTypes[GPTable[procName].ReturnType] + " '" + procName + "' (" + procStrParams + ") {\n" + body + "\t\tret\n" + "\t}\n\n"; return(retString); }
public override void VisitProcedureDeclaration(ProcedureDeclaration procedureDeclaration) { PushScope(CurrentScope.CreateChildScope(procedureDeclaration)); base.VisitProcedureDeclaration(procedureDeclaration); PopScope(); }
private ProcedureInfo CreateProcedureInfo(ProcedureDeclaration procedureDeclaration) { var procedureInfo = new ProcedureInfo(procedureDeclaration.Name, CurrentDeclaringSymbol); CurrentDeclaringSymbol.AddDeclaration(procedureInfo); symbols_.Add(procedureDeclaration, procedureInfo); return(procedureInfo); }
public virtual void Visit(ProcedureDeclaration procedureDeclaration) { Visit(procedureDeclaration.ReturnType); Visit(procedureDeclaration.Identifier); foreach (var parameter in procedureDeclaration.Parameters) { Visit(parameter); } Visit(procedureDeclaration.Body); }
public bool TryDeclareProcedure(ProcedureDeclaration declaration, Procedure compiled, out ProcedureInfo procedure) { if (!TryDeclareProcedure(declaration, out procedure)) { return(false); } procedure.Compiled = compiled; procedure.OriginalCompiled = compiled.Clone(); return(true); }
public override void VisitProcedureDeclaration(ProcedureDeclaration procedureDeclaration) { PushScope(CurrentScope.CreateChildScope(procedureDeclaration)); var procedureInfo = CreateProcedureInfo(procedureDeclaration); PushDeclaringSymbol(procedureInfo); base.VisitProcedureDeclaration(procedureDeclaration); PopDeclaringSymbol(); PopScope(); }
public bool TryDeclareProcedure(ProcedureDeclaration declaration, out ProcedureInfo procedure) { if (TryGetProcedure(declaration.Identifier.Text, out procedure)) { return(false); } var p = new ProcedureInfo(); p.Declaration = declaration; p.Index = declaration.Index == null ? ( short )Procedures.Count : ( short )declaration.Index.Value; Debug.Assert(Procedures.All(x => x.Value.Index != p.Index), "Same procedure index used by multiple procedures"); Procedures[declaration.Identifier.Text] = procedure = p; return(true); }
public bool TryDeclareProcedure(ProcedureDeclaration declaration) { if (TryGetProcedure(declaration.Identifier.Text, out _)) { return(false); } var procedure = new Procedure(); procedure.Declaration = declaration; procedure.Index = ( short )Procedures.Count; Procedures[declaration.Identifier.Text] = procedure; return(true); }
public override void Visit(ProcedureDeclaration procedureDeclaration) { mProcedure = procedureDeclaration; WriteNewLine(); Visit(procedureDeclaration.ReturnType); Write(" "); Visit(procedureDeclaration.Identifier); WriteParameters(procedureDeclaration.Parameters); if (procedureDeclaration.Body == null) { WriteStatementEnd(); } else { Visit(procedureDeclaration.Body); } }
private bool TryDecompileProcedure(EvaluatedProcedure evaluatedProcedure, out ProcedureDeclaration declaration) { LogInfo($"Decompiling procedure: {evaluatedProcedure.Procedure.Name}"); InitializeProcedureDecompilationState(evaluatedProcedure); if (!TryCompositeEvaluatedInstructions(evaluatedProcedure.Statements, out var statements)) { LogError("Failed to composite evaluated instructions"); declaration = null; return(false); } declaration = new ProcedureDeclaration( new TypeIdentifier(evaluatedProcedure.ReturnKind), new Identifier(ValueKind.Procedure, evaluatedProcedure.Procedure.Name), evaluatedProcedure.Parameters, new CompoundStatement(statements)); return(true); }
// Declarations private bool TryResolveTypesInProcedureDeclaration(ProcedureDeclaration declaration) { LogTrace($"{nameof( TryResolveTypesInProcedureDeclaration )}( {nameof( declaration )} = {declaration})"); LogInfo(declaration, $"Resolving types in procedure '{declaration.Identifier.Text}'"); // Nothing to resolve if there's no body if (declaration.Body == null) { return(true); } // Enter procedure body scope PushScope(); foreach (var parameter in declaration.Parameters) { var parameterDeclaration = new VariableDeclaration( new VariableModifier(VariableModifierKind.Local), parameter.Type, parameter.Identifier, null); if (!TryRegisterDeclaration(parameterDeclaration)) { LogError(parameter, "Failed to register declaration for procedure parameter"); return(false); } } if (!TryResolveTypesInCompoundStatement(declaration.Body)) { return(false); } // Exit procedure body scope PopScope(); return(true); }
//----------------------------------------------------------- private Type Visit(ProcedureDeclaration node, Table table) { GlobalSymbolTable gstable = table as GlobalSymbolTable; if (table == null) { throw new TypeAccessException("Expecting a GlobalSymbolTable"); } var procedureName = node.AnchorToken.Lexeme; if (gstable.Contains(procedureName)) { throw new SemanticError("Duplicated procedure: " + procedureName, node.AnchorToken); } if (node[1] is SimpleType || node[1] is ListType) { GPTable[procedureName] = new GlobalProcedure(false, Visit((dynamic)node[1], table)); var i = 0; foreach (var n in node) { if (i != 1) { Visit((dynamic)n, GPTable[procedureName].LocalSymbols); } i++; } } else { GPTable[procedureName] = new GlobalProcedure(false); VisitChildren(node, GPTable[procedureName].LocalSymbols); } return(Type.VOID); }
public Node ProcedureDeclaration() { var procedure = new ProcedureDeclaration(); Expect(TokenCategory.PROCEDURE); procedure.AnchorToken = Expect(TokenCategory.IDENTIFIER); var consDecList = new ConstantDeclarationList(); var statement = new StatementList(); Expect(TokenCategory.INITPARENTHESIS); if (CurrentToken == TokenCategory.IDENTIFIER) { var parDecList = ParameterDeclaration(); procedure.Add(parDecList); } Expect(TokenCategory.CLOSINGPARENTHESIS); if (CurrentToken == TokenCategory.DECLARATION) { Expect(TokenCategory.DECLARATION); var type = Type(); procedure.Add(type); } Expect(TokenCategory.ENDLINE); if (CurrentToken == TokenCategory.CONST) { consDecList.AnchorToken = Expect(TokenCategory.CONST); var i = 0; do { consDecList.Add(ConstantDeclaration()); Console.WriteLine(consDecList[i]); i++; } while (CurrentToken == TokenCategory.IDENTIFIER); procedure.Add(consDecList); } if (CurrentToken == TokenCategory.VAR) { var varDecList = VariableDeclaration(); procedure.Add(varDecList); } var tempStatement = Expect(TokenCategory.BEGIN); while (firstOfStatement.Contains(CurrentToken)) { statement.Add(Statement()); } if (statement.getLength() > 0) { statement.AnchorToken = tempStatement; procedure.Add(statement); } Expect(TokenCategory.END); Expect(TokenCategory.ENDLINE); return(procedure); }
public virtual T Visit(ProcedureDeclaration stmt) => Visit(stmt as Statement);
public virtual void VisitProcedureDeclaration(ProcedureDeclaration procedureDeclaration) { DefaultVisit(procedureDeclaration); }
public Node ProcedureDeclaration() { var result = new ProcedureDeclaration() { AnchorToken = Expect(TokenCategory.PROCEDURE) }; result.Add(new Identifier() { AnchorToken = Expect(TokenCategory.IDENTIFIER) }); Expect(TokenCategory.PARENTHESIS_OPEN); var parameterList = new ParameterDeclarationList(); if (CurrentToken == TokenCategory.IDENTIFIER) { while (CurrentToken == TokenCategory.IDENTIFIER) { parameterList.Add(VariableDeclaration()); } } result.Add(parameterList); Expect(TokenCategory.PARENTHESIS_CLOSE); var type = new TypeNode(); if (CurrentToken == TokenCategory.COLON) { Expect(TokenCategory.COLON); if (CurrentToken != TokenCategory.LIST) { type.Add(SimpleType()); } else if (CurrentToken == TokenCategory.LIST) { type.Add(ListType()); } } result.Add(type); Expect(TokenCategory.SEMICOLON); var constantList = new ConstantDeclarationList(); if (firstOfDeclaration.Contains(CurrentToken) && CurrentToken == TokenCategory.CONST) { constantList.AnchorToken = Expect(TokenCategory.CONST); do { constantList.Add(ConstantDeclaration()); } while (CurrentToken == TokenCategory.IDENTIFIER); } result.Add(constantList); var variableList = new VariableDeclarationList(); if (firstOfDeclaration.Contains(CurrentToken) && CurrentToken == TokenCategory.VAR) { variableList.AnchorToken = Expect(TokenCategory.VAR); do { variableList.Add(VariableDeclaration()); } while (CurrentToken == TokenCategory.IDENTIFIER); } result.Add(variableList); Expect(TokenCategory.BEGIN); var statementList = new StatementList(); if (firstOfStatement.Contains(CurrentToken)) { while (firstOfStatement.Contains(CurrentToken)) { statementList.Add(Statement()); } } result.Add(statementList); Expect(TokenCategory.END); Expect(TokenCategory.SEMICOLON); return(result); }
protected override void visitProcedureDeclaration(ProcedureDeclaration declaration) { writer.Append(KeywordEnum.PROCEDURE.ToString()).Append(' ').AppendIdentifier(declaration); appendProcedureParameters(declaration.Parameters); writer.Append(";").NewLine(); }
public TypeG Visit(ProcedureDeclaration node) { CurrentContext.context = "LOCAL"; var procedureName = node.AnchorToken.Lexeme; //SE AGREGA TABLA NUEVA AL HACERSE EL NUEVO PROCEDURE LocalDeclarationTable nuevaTabla = new LocalDeclarationTable(); nuevaTabla.tableID = procedureName; ListLocalDeclarationTable.Add(nuevaTabla); //CurrentContext.length++; if (ProcedureDeclarationT.Contains(procedureName)) { throw new SemanticError( "Duplicated procedure: " + procedureName, node.AnchorToken); } else { dynamic tipo = TypeG.VOID; foreach (var n in node) { if (n.ToString().StartsWith("Type")) { tipo = Visit((dynamic)n); Console.WriteLine("Has type: " + tipo); } } ProcedureDeclarationT[procedureName] = new ProcedureDeclarationType(procedureName, tipo, false); Console.WriteLine("NUEVO PROC" + procedureName); } /* * if (ProcedureDeclarationList[CurrentContext.procedure].Contains(procedureName)) * { * * } * else * { * ProcedureDeclarationList[CurrentContext.procedure] = * new ProcedureDeclarationType(procedureName, TypeG.VOID, false); * } * * VisitChildren(node); * CurrentContext.context = "GLOBAL"; * return TypeG.VOID; * //CurrentContext.procedure+ * * * * * if (ProcedureDeclarationT.Contains(procedureName)) * { * throw new SemanticError( * "Duplicated procedure: " + procedureName, * node.AnchorToken); * } * else * { * * ProcedureDeclarationList[CurrentContext.procedure] = * new ProcedureDeclarationType(procedureName, TypeG.VOID, false); * ProcedureDeclarationT.a * } * * CurrentContext.current_pdt = pdt; * //ProcedureDeclarationList[CurrentContext.procedure] = pdt; * VisitChildren(node); * CurrentContext.context = "GLOBAL"; * return TypeG.VOID; * * if (ProcedureDeclarationList.Contains(pdt)) * { * Console.WriteLine("Hola"); * } else * { * LocalDeclarationTable d = new LocalDeclarationTable(); * if (CurrentContext.cantparam > 0) * { * d[variableName] = * new LocalDeclarationType(variableName, type, variableValue, CurrentContext.param, kind); * CurrentContext.cantparam--; * CurrentContext.param++; * * } * else * { * d[variableName] = * new LocalDeclarationType(variableName, type, variableValue, -1, kind); * } * ListLocalDeclarationTable.Add(d); * ListLocalDeclarationTable[0].tableID = CurrentContext.procedure; * * } * * if (ProcedureDeclarationList[CurrentContext.procedure].Contains(procedureName)) * { * throw new SemanticError( * "Duplicated procedure: " + procedureName, * node.AnchorToken); * } * else * { * ProcedureDeclarationList[CurrentContext.procedure] = * new ProcedureDeclarationType(procedureName, TypeG.VOID, false); * } */ VisitChildren(node); CurrentContext.context = "GLOBAL"; CurrentContext.index++; return(TypeG.VOID); //CurrentContext.procedure++; }
public Node ProcedureDeclaration() { var result = new ProcedureDeclaration(); Expect(TokenCategory.PROCEDURE); result.AnchorToken = Expect(TokenCategory.IDENTIFIER); Expect(TokenCategory.LEFT_PAR); var parameterDeclarationList = new ParameterDeclarationList(); while (CurrentToken == TokenCategory.IDENTIFIER) { parameterDeclarationList.Add(ParameterDeclaration()); } result.Add(parameterDeclarationList); Expect(TokenCategory.RIGHT_PAR); if (CurrentToken == TokenCategory.COLON) { Expect(TokenCategory.COLON); result.Add(Type()); } Expect(TokenCategory.SEMICOLON); if (CurrentToken == TokenCategory.CONST) { var constantDeclarationList = new ConstantDeclarationList() { AnchorToken = Expect(TokenCategory.CONST) }; do { constantDeclarationList.Add(ConstantDeclaration()); } while (CurrentToken == TokenCategory.IDENTIFIER); result.Add(constantDeclarationList); } if (CurrentToken == TokenCategory.VAR) { var variableDeclarationList = new VariableDeclarationList() { AnchorToken = Expect(TokenCategory.VAR) }; do { variableDeclarationList.Add(VariableDeclaration()); } while (CurrentToken == TokenCategory.IDENTIFIER); result.Add(variableDeclarationList); } Expect(TokenCategory.BEGIN); var statementList = new StatementList(); while (firstOfStatement.Contains(CurrentToken)) { statementList.Add(Statement()); } result.Add(statementList); Expect(TokenCategory.END); Expect(TokenCategory.SEMICOLON); return(result); }