public Program ParseProgram(string program) { var scanner = new MiniJavaScanner(new StringReader(program)); var errorLog = new ErrorLogger(); var parser = new Parser(scanner, errorLog, true); Program ast; if (parser.TryParse(out ast)) { return ast; } return null; }
public void ShouldConflictWithMainClassTest() { var mainMethod = MethodDeclaration.CreateMainMethodDeclaration(new List<IStatement>(), 0, 0); var mainClass = ClassDeclaration.CreateMainClassDeclaration("Foo", mainMethod, 0, 0); var otherClass = new ClassDeclaration("Foo", null, new List<Declaration>(), 2, 0); var program = new Program(mainClass, new List<ClassDeclaration>( new ClassDeclaration[] { otherClass })); var errorReporter = new ErrorLogger(); var builder = new SymbolTableBuilder(program, errorReporter); Assert.AreEqual(SymbolTableBuilder.ExitCode.FatalError, builder.BuildSymbolTable()); var errors = errorReporter.Errors; Assert.AreEqual(errors.Count, 1); Assert.AreEqual("Conflicting definitions for Foo.", errors[0].Content); Assert.AreEqual(errors[0].Row, 2); }
public static SemanticsChecker SetUpTypeAndReferenceChecker(string program, out IErrorReporter errorLog) { var reader = new StringReader(program); var scanner = new MiniJavaScanner(reader); var errors = new ErrorLogger(); var parser = new Parser(scanner, errors, true); Program syntaxTree; parser.TryParse(out syntaxTree); reader.Close(); Assert.That(errors.Errors, Is.Empty); var symbolTableBuilder = new SymbolTableBuilder(syntaxTree, errors); Assert.That(errors.Errors, Is.Empty); Assert.DoesNotThrow(() => symbolTableBuilder.BuildSymbolTable()); errorLog = new ErrorLogger(); return new SemanticsChecker(syntaxTree, errorLog); }
public void ShouldConflictWithPredefinedTypesTest() { var mainMethod = MethodDeclaration.CreateMainMethodDeclaration(new List<IStatement>(), 0, 0); var mainClass = ClassDeclaration.CreateMainClassDeclaration("int", mainMethod, 1, 0); var secondClass = new ClassDeclaration("boolean", null, new List<Declaration>(), 2, 0); var thirdClass = new ClassDeclaration("int", null, new List<Declaration>(), 3, 0); var program = new Program(mainClass, new List<ClassDeclaration> ( new [] { secondClass, thirdClass })); var errorReporter = new ErrorLogger(); var builder = new SymbolTableBuilder(program, errorReporter); Assert.AreEqual(SymbolTableBuilder.ExitCode.FatalError, builder.BuildSymbolTable()); var errors = errorReporter.Errors; Assert.AreEqual(3, errors.Count); Assert.AreEqual("Conflicting definitions for int.", errors[0].Content); Assert.AreEqual(1, errors[0].Row); Assert.AreEqual("Conflicting definitions for boolean.", errors[1].Content); Assert.AreEqual(2, errors[1].Row); Assert.AreEqual("Conflicting definitions for int.", errors[2].Content); Assert.AreEqual(3, errors[2].Row); }
public void ValidTypeDefinitionsTest() { var mainMethod = MethodDeclaration.CreateMainMethodDeclaration(new List<IStatement>(), 0, 0); var mainClass = ClassDeclaration.CreateMainClassDeclaration("Foo", mainMethod, 0, 0); var secondClass = new ClassDeclaration("Bar", null, new List<Declaration>(), 2, 0); var thirdClass = new ClassDeclaration("Baz", null, new List<Declaration>(), 3, 0); var program = new Program(mainClass, new List<ClassDeclaration>( new [] { secondClass, thirdClass })); var errorReporter = new ErrorLogger(); var builder = new SymbolTableBuilder(program, errorReporter); builder.BuildSymbolTable(); var types = (program.Scope as GlobalScope).UserDefinedTypeNames.ToList(); Assert.Contains("Foo", types); Assert.Contains("Bar", types); Assert.Contains("Baz", types); Assert.That(types, Is.Not.Contains("int")); Assert.That(types, Is.Not.Contains("boolean")); Assert.That(types.Count, Is.EqualTo(3)); }
public void SetUpParser(string program) { var scanner = new MiniJavaScanner(new StringReader(program)); _errorLog = new ErrorLogger(); _parser = new Parser(scanner, _errorLog); }