public void Ifc_SemanticModel_ShouldCreateSymbolTable(string path) { var parser = new ExpressParser(); using (var stream = File.Open(path, FileMode.Open, FileAccess.Read)) { var result = parser.Parse(stream); Assert.IsTrue(result.Success); var builder = new SymbolTableBuilder(result.Schemas); var symbolTable = builder.Build(); var declarations = new DeclarationSyntaxWalker(); foreach (var schema in result.Schemas) { schema.Accept(declarations); } foreach (var declaration in declarations.Declarations) { if (declaration is LocalRuleDeclaration) { continue; } var symbolInfo = symbolTable.GetSymbolInfo(declaration); Assert.IsNotNull(symbolInfo, $"Missing ISymbolInfo for {declaration.Name}({declaration.Kind})"); ValidateSymbolInfo(symbolInfo); } } }
private static Dictionary <string, int> BuildSymbolTable(LineOfCode[] parsedLines) { var symbolTableBuilder = new SymbolTableBuilder(); Dictionary <string, int> symbolTable = symbolTableBuilder.BuildSymbolTable(parsedLines); return(symbolTable); }
public void SymbolTableTest() { var stb = new SymbolTableBuilder(); var tree = new Parser(new Lexer(Program)).Parse(); stb.Visit(tree); }
public void NameError() { var nameError = "" + "PROGRAM NameError1;\n" + "VAR\n" + " a : INTEGER;\n" + "\n" + "BEGIN\n" + " a := 2 + b;\n" + "END.\n"; var stb = new SymbolTableBuilder(); var tree = new Parser(new Lexer(nameError)).Parse(); var ex = Assert.Throws <SymbolTableBuilder.UndeclaredVariableException>(() => stb.Visit(tree)); Assert.Equal(ex.Message, "b"); }
public SemanticModel Build() { //create symbol table (constructs mapping Declaration->ISymbolInfo) var builder = new SymbolTableBuilder(schemas_); symbolTable_ = builder.Build(); //initialize state scopes_.Clear(); scopes_.Push(Scope.CreateRootScope(schemas_)); //visit expressions (constructs mapping Reference->TypeInfo and Exoression->TypeInfo) //foreach (var schema in schemas_) // schema.Accept(this); //TODO: expressions return new SemanticModel(symbolTable_); }
static void Main(string[] args) { if (string.IsNullOrEmpty(args[0])) throw new Exception("No file name"); string inputFile = args[0]; string outputFile = GetOutputFilePath(inputFile); Parser p = new Parser(inputFile); Translator t = new Translator(); SymbolTableBuilder b = new SymbolTableBuilder(); List<ParsedCommand> parsedCommands = p.Parse(); Dictionary<string, int> symbolTable = b.Build(parsedCommands); List<string> codedCommands = t.Translate(parsedCommands, symbolTable); File.WriteAllLines(outputFile, codedCommands.ToArray()); }
public void Setup() { symbolTableBuilder = new SymbolTableBuilder(); }
static void Main(string[] args) { Console.ForegroundColor = ConsoleColor.Green; Console.Title = "RainDay Interpreter"; Console.SetWindowSize((int)(Console.LargestWindowWidth * 0.75f), (int)(Console.LargestWindowHeight * 0.75f)); var interpreter = new Interpreter(); while (true) { interpreter.ClearErrors(); Ast.AstNode tree; Console.Write("@> "); var input = Console.ReadLine(); if (string.IsNullOrEmpty(input)) { continue; } var isSymbolMode = input.Contains("-s"); if (input.ToLowerInvariant().StartsWith("run")) { var lineArgs = input.Split('-'); var path = lineArgs[lineArgs.Length - 1]; if (!path.Contains(":")) { path = $@"C:\RainyDay\{path}"; } input = File.ReadAllText(path); var lexer = new Lexer(input); var parser = new Parser(lexer); tree = parser.ParseScript(); } else if ("cls".Equals(input.ToLowerInvariant())) { Console.Clear(); continue; } else if ("quit".Contains(input.ToLowerInvariant())) { break; } else if ("restart".Equals(input.ToLowerInvariant())) { interpreter = new Interpreter(); continue; } else { var lexer = new Lexer(input); var parser = new Parser(lexer); tree = parser.ParseStatement(); } if (isSymbolMode) { var tableBuilder = new SymbolTableBuilder(); tableBuilder.Visit(tree); Console.WriteLine($"{tableBuilder}\n"); } interpreter.Interpret(tree); if (interpreter.Errors.Length > 0) { Console.ForegroundColor = ConsoleColor.Red; foreach (var error in interpreter.Errors) { Console.WriteLine($"\t{error.Message}"); } Console.ForegroundColor = ConsoleColor.Green; } Console.WriteLine($"\n{interpreter}"); } }
/// <summary> /// Task for assembling one individual file. /// </summary> /// <param name="inputFile">The input file to assemble.</param> /// <param name="logger">The logging implementation to log errors/info to.</param> /// <param name="options">The options to use while assembling.</param> /// <returns>True if the assembler could successfully generate code for the file; otherwise returns false.</returns> public AssemblerResult AssembleFile(string inputFile, string outputFile, ILogger logger, AssemblerOptions options) { var result = new AssemblerResult(); logger.Log(LogLevel.Info, "Invoking assembler for file " + inputFile); try { bool furtherProcessingNeeded = true; if (File.Exists(inputFile) && File.Exists(outputFile)) { DateTime inputFileWriteTime = File.GetLastWriteTimeUtc(inputFile); DateTime outputFileWriteTime = File.GetLastWriteTimeUtc(outputFile); if (outputFileWriteTime > inputFileWriteTime) { logger.Log(LogLevel.Info, "Nothing to do for file " + inputFile); furtherProcessingNeeded = false; } } if (furtherProcessingNeeded) { using (var reader = new StreamReader(File.OpenRead(inputFile))) { var symTable = new SymbolTable(); // build the symbol table var instructionProcFac = new InstructionProcessorFactory(symTable); var symTableBuilder = new SymbolTableBuilder(logger, instructionProcFac); symTableBuilder.GenerateSymbolTableForSegment(reader, SegmentType.Data, symTable); symTableBuilder.GenerateSymbolTableForSegment(reader, SegmentType.Text, symTable); // use the symbol table to generate code with references resolved. var objFile = new BasicObjectFile(inputFile, symTable); var codeGenerator = new CodeGenerator(logger, symTable, instructionProcFac); codeGenerator.GenerateCode(inputFile, reader, objFile); if (!objFile.TextElements.Any()) { logger.Log(LogLevel.Warning, "No .text segment to assemble. Stop."); result.OperationSuccessful = false; } else { // write the object file out. var writerFac = new ObjectFileWriterFactory(); IObjectFileWriter writer = writerFac.GetWriterForOutputFile(outputFile); writer.WriteObjectFile(outputFile, objFile); } } } } catch (AggregateAssemblyError ex) { foreach (AssemblyException asEx in ex.AssemblyErrors) { logger.Log(LogLevel.Critical, "In file \"" + inputFile + "\" (line " + asEx.LineNumber + "):\n\t"); logger.Log(LogLevel.Critical, asEx.Message); result.AddUserAssemblyError(asEx); } } catch (Exception ex) { logger.Log(LogLevel.Critical, ex.StackTrace); logger.Log(LogLevel.Critical, ex.Message); if (ex.InnerException != null) { logger.Log(LogLevel.Critical, ex.InnerException.StackTrace); logger.Log(LogLevel.Critical, ex.InnerException.Message); } result.AddInternalAssemblerError(ex); } return(result); }