public override string VisitFunctionDeclaration(FunctionDeclarationNode faDeclarationNode) { var str = ""; var procedureDeclaration = faDeclarationNode; //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); var type = typesConverter.GetTypeName(procedureDeclaration.ReturnType.TypeValue); if (procedureDeclaration.Annotations.ContainsKey("Nested")) { str += $"{AddSpaces()}{type} {procedureDeclaration.FunctionName}({param})\r\n"; } else { str += $"{AddSpaces()}public static {type} {procedureDeclaration.FunctionName}({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); }
private string CreateParameters(DeclarationNode procedureDeclaration, ScopedSymbolTable symbols) { string param = ""; for (var index = 0; index < procedureDeclaration.Parameters.Count; index++) { var p = procedureDeclaration.Parameters[index]; var name = p.Declaration.VarNode.VariableName; var matches = symbols.LookupSymbols <Symbol>(name, true); if (matches.Count > 1) { name = current.AddAlias(name); } param += $"{typesConverter.GetTypeName(p.Declaration.TypeNode.TypeValue)} {name}"; if (index != procedureDeclaration.Parameters.Count - 1) { param += ","; } } return(param); }
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); }
public string VisitProgram(PascalProgramNode program) { var zero = new ScopedSymbolTable(program.ProgramName); PascalSemanticAnalyzer.DefineBuiltIns(zero); CurrentScope = zero; var programStr = VisitProgramBlockNode(program); return(programStr); }
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); }
private string VisitProgramBlockNode(PascalProgramNode program) { var programStr = $"program {program.ProgramName}{CurrentScope.ScopeLevel};\r\n"; var global = new ScopedSymbolTable("Global", CurrentScope); CurrentScope = global; foreach (var blockDeclaration in program.Block.Declarations) { programStr += VisitNode(blockDeclaration); } programStr += $"{AddSpaces(-3)}begin\r\n"; foreach (var compoundStatementNode in program.Block.CompoundStatement.Nodes) { programStr += VisitNode(compoundStatementNode); } CurrentScope = CurrentScope.ParentScope; programStr += AddSpaces(-3) + "end. {END OF " + CurrentScope.ScopeName + "}\r\n"; return(programStr); }
public static Symbol Create([NotNull] Type_declarationContext context, ScopedSymbolTable scopedTable) { List <string> modifiers = new List <string>(); string symbolname = null; string fullName = null; List <DefinitionSymbol> baseTypes = new List <DefinitionSymbol>(); List <GenericInfo> genericParameters = new List <GenericInfo>(); var modifierContexts = context.all_member_modifiers()?.all_member_modifier(); foreach (var modifierCon in modifierContexts) { modifiers.Add(modifierCon.GetText()); } var classdefinition = context.class_definition(); symbolname = classdefinition.identifier().GetText(); fullName = scopedTable.Path + "." + symbolname; ClassSymbol classSymbol = new ClassSymbol(symbolname, fullName, modifiers); return(classSymbol); }
public override string VisitProgram(PascalProgramNode program) { // var zero = new ScopedSymbolTable(program.ProgramName); // PascalSemanticAnalyzer.DefineBuiltIns(zero); //CurrentScope = zero; var block = "{\r\n"; indentLevel++; current = program.Annotations["SymbolTable"] as ScopedSymbolTable; block += VisitBlock(program.Block); current = current.ParentScope; indentLevel--; block += "}\r\n"; var assems = ""; foreach (var s in _assembliesCalled) { assems += s + "\r\n"; } var str = $"{assems}public static class {program.ProgramName}\r\n{block}\r\n"; return(str.Trim()); }
public void ScopedSymbolTable_Basics() { var res = default(Indexed <Indexed <string> >); var sst = new ScopedSymbolTable <string, string> { { "glb", "val" } }; var assertGlobal = new Action(() => { Assert.IsTrue(sst.TryLookup("glb", out res)); Assert.AreEqual(-1, res.Index); Assert.AreEqual(0, res.Value.Index); Assert.AreEqual("val", res.Value.Value); Assert.IsFalse(sst.TryLookup("blg", out res)); }); assertGlobal(); AssertPushPop(sst, () => { assertGlobal(); Assert.IsFalse(sst.TryLookup("bar", out res)); }, () => { sst.Add("bar", "foo"); sst.Add("baz", "qux"); AssertPushPop(sst, () => { assertGlobal(); Assert.IsTrue(sst.TryLookup("bar", out res)); Assert.AreEqual(0, res.Index); Assert.AreEqual(0, res.Value.Index); Assert.AreEqual("foo", res.Value.Value); Assert.IsTrue(sst.TryLookup("baz", out res)); Assert.AreEqual(0, res.Index); Assert.AreEqual(1, res.Value.Index); Assert.AreEqual("qux", res.Value.Value); }, () => { sst.Add("foo", "bar"); sst.Add("bar", "qux"); AssertPushPop(sst, () => { assertGlobal(); Assert.IsTrue(sst.TryLookup("foo", out res)); Assert.AreEqual(0, res.Index); Assert.AreEqual(0, res.Value.Index); Assert.AreEqual("bar", res.Value.Value); Assert.IsTrue(sst.TryLookup("bar", out res)); Assert.AreEqual(0, res.Index); Assert.AreEqual(1, res.Value.Index); Assert.AreEqual("qux", res.Value.Value); Assert.IsTrue(sst.TryLookup("baz", out res)); Assert.AreEqual(1, res.Index); Assert.AreEqual(1, res.Value.Index); Assert.AreEqual("qux", res.Value.Value); }, () => { sst.Add("qux", "baz"); AssertPushPop(sst, () => { assertGlobal(); Assert.IsTrue(sst.TryLookup("qux", out res)); Assert.AreEqual(0, res.Index); Assert.AreEqual(0, res.Value.Index); Assert.AreEqual("baz", res.Value.Value); Assert.IsTrue(sst.TryLookup("foo", out res)); Assert.AreEqual(1, res.Index); Assert.AreEqual(0, res.Value.Index); Assert.AreEqual("bar", res.Value.Value); Assert.IsTrue(sst.TryLookup("bar", out res)); Assert.AreEqual(1, res.Index); Assert.AreEqual(1, res.Value.Index); Assert.AreEqual("qux", res.Value.Value); Assert.IsTrue(sst.TryLookup("baz", out res)); Assert.AreEqual(2, res.Index); Assert.AreEqual(1, res.Value.Index); Assert.AreEqual("qux", res.Value.Value); }, () => { // NOTE: The test method pushes a frame before entering, so we're off by 1 for the outer indices. Assert.IsNotNull(((IEnumerable)sst).GetEnumerator()); var symbols = sst.SelectMany(ist => ist.Value, (o, i) => (Outer: o.Index, Inner: i.Index, Symbol: i.Value.Key, i.Value.Value)).ToArray(); Assert.IsTrue(symbols.SequenceEqual(new[] { (Outer: 1, Inner: 0, Symbol: "qux", Value: "baz"), (Outer: 2, Inner: 0, Symbol: "foo", Value: "bar"), (Outer: 2, Inner: 1, Symbol: "bar", Value: "qux"), (Outer: 3, Inner: 0, Symbol: "bar", Value: "foo"), (Outer: 3, Inner: 1, Symbol: "baz", Value: "qux"), (Outer: -1, Inner: 0, Symbol: "glb", Value: "val"), }));
protected ScopedExpressionVisitor() #endif { _symbolTable = new ScopedSymbolTable <ParameterExpression, TState>(); }