public void VisitBinaryOperatorTest(object a, object b, Type expressionType, ValueType expectedType) { var target = new TypeCheckingVisitor(); var lhs = new ConstantExpr(a); var rhs = new ConstantExpr(b); var expression = (Expression)Activator.CreateInstance(expressionType, new object[] { lhs, rhs }); var method = typeof(TypeCheckingVisitor).GetMethods() .First(m => m.Name == "Visit" && m.GetParameters().ElementAt(0).ParameterType == expressionType); ValueType actual; try { actual = (ValueType)method.Invoke(target, new object[] { expression, scope }); } catch (TargetInvocationException ex) { throw ex.InnerException; } Assert.AreEqual(expectedType, actual); }
public void AssignmentTest() { var target = new TypeCheckingVisitor(); var expr = new AssignmentExpr(new IdentifierExpr("a"), new ConstantExpr(1)); var actual = target.Visit(expr, scope); Assert.AreEqual(ValueType.Int, actual); }
public void NegationExpressionTest(object value, object expected) { var target = new TypeCheckingVisitor(); var expr = new NegationExpr(new ConstantExpr(value)); var actual = target.Visit(expr, scope); Assert.AreEqual(expected, actual); }
public void AndOrTest() { var target = new TypeCheckingVisitor(); var expr = new AndExpr(new ConstantExpr(true), new OrExpr(new ConstantExpr(true), new ConstantExpr(false))); var actual = target.Visit(expr, scope); Assert.AreEqual(ValueType.Boolean, actual); }
public void VisitIntegerConstantExpr() { var target = new TypeCheckingVisitor(); var expression = new ConstantExpr(1234); var actual = target.Visit(expression, scope); Assert.AreEqual(ValueType.Int, actual); }
public void VisitConditionMustBeBooleanType(object condition) { var target = new TypeCheckingVisitor(); var conditionExpression = new ConstantExpr(condition); var expr = new ConditionalExpr(conditionExpression, new ConstantExpr(0), new ConstantExpr(0)); target.Visit(expr, scope); }
public void VisitBooleanConstantExpr(bool value) { var target = new TypeCheckingVisitor(); var expression = new ConstantExpr(value); var actual = target.Visit(expression, scope); Assert.AreEqual(ValueType.Boolean, actual); }
public void VisitNoOpExpr() { var target = new TypeCheckingVisitor(); var expression = new NoOpStatement(); var actual = target.Visit(expression, scope); Assert.AreEqual(ValueType.Unit, actual); }
public void VisitBooleanNotExpr(bool a, ValueType expected) { var target = new TypeCheckingVisitor(); var aExpression = new ConstantExpr(a); var expr = new NotExpr(aExpression); var actual = target.Visit(expr, scope); Assert.AreEqual(expected, actual); }
static void Main(string[] args) { try { string path; if (args.Length == 0) { Console.WriteLine("Please enter a filepath: "); path = Console.ReadLine(); } else { path = args[0]; } ErrorManager em = new ErrorManager(); Lexer lexer = new Lexer(new FileSource(path)); Parser parser = new Parser(lexer); parser.HookTo(em); AST program = parser.parse(); SymbolTableBuildingVisitor tableBuilder = new SymbolTableBuildingVisitor(); tableBuilder.HookTo(em); SymbolTableManager stm = tableBuilder.buildTables(program); IdentifierUsageCheckingVisitor idChecker = new IdentifierUsageCheckingVisitor(stm); idChecker.HookTo(em); idChecker.check(program); TypeCheckingVisitor typeChecker = new TypeCheckingVisitor(stm); typeChecker.HookTo(em); typeChecker.check(program); if (!em.areErrors()) { CodeGeneratingVisitor cgv = new CodeGeneratingVisitor(stm); CProgram cProgram = cgv.generate(program); cProgram.generateFile(); Console.WriteLine("Build successful."); } else { Console.WriteLine("Build failed."); } Console.ReadLine(); } catch (Exception ex) { Console.WriteLine(ex); return; } }
public StylesheetTask Process(StylesheetTask input) { var TypeErrorExtractor = new TypeCheckingVisitor(input.QLSymbolTable); input.Ast.Accept(TypeErrorExtractor); TypeErrorExtractor.TypeErrors.ForEach(x => input.Errors.Add(x.ToString())); _canContinue = (input.Errors.Count == 0); return(input); }
public void VisitDivExpr() { var target = new TypeCheckingVisitor(); var lhs = new ConstantExpr(10); var rhs = new ConstantExpr(2); var expression = new DivExpr(lhs, rhs); var actual = target.Visit(expression, scope); Assert.AreEqual(ValueType.Int, actual); }
public void VisitPlusWithDoubleAndIntExpr() { var target = new TypeCheckingVisitor(); var lhs = new ConstantExpr(1.1); var rhs = new ConstantExpr(2); var expression = new PlusExpr(lhs, rhs); var actual = target.Visit(expression, scope); Assert.AreEqual(ValueType.Float, actual); }
private bool analyzeSemantics() { try { ProgramNode program = (ProgramNode)this.ast.getProgram(); INodeVisitor varDeclarationVisitor = new VarDeclarationVisitor(symbolTable); INodeVisitor typeCheckingVisitor = new TypeCheckingVisitor(symbolTable); program.accept(varDeclarationVisitor); program.accept(typeCheckingVisitor); return(true); } catch (SemanticException exception) { throw exception; } }
public void VisitConditionalExpr(bool condition, object thenValue, object elseValue, ValueType expected) { var target = new TypeCheckingVisitor(); var conditionExpression = new ConstantExpr(condition); var thenExpression = new ConstantExpr(thenValue); var elseExpression = new ConstantExpr(elseValue); var expr = new ConditionalExpr(conditionExpression, thenExpression, elseExpression); var actual = target.Visit(expr, scope); Assert.AreEqual(expected, actual); }
static void Main(string[] args) { try { //Initialize string path; if (args.Length == 0) { Console.WriteLine("Please enter a filepath: "); path = Console.ReadLine(); } else { path = args[0]; } FileSource source = new FileSource(path); ErrorManager errorManager = new ErrorManager(); Scanner scanner = new Scanner(source); Parser parser = new Parser(scanner); SymbolTableBuildingVisitor symbolTableBuilder = new SymbolTableBuildingVisitor(); TypeCheckingVisitor typeChecker = new TypeCheckingVisitor(symbolTableBuilder.table); Interpreter interpreter = new Interpreter(); //Hook to error manager parser.HookTo(errorManager); symbolTableBuilder.HookTo(errorManager); typeChecker.HookTo(errorManager); AST program = parser.parse(); symbolTableBuilder.build(program); typeChecker.check(program); if (!errorManager.areErrors()) { interpreter.run(program); } else { Console.WriteLine("Program couldn't be executed due to errors."); } Console.ReadKey(); } catch (Exception ex) { Console.WriteLine(ex); return; } }
public static void Main(string[] args) { string source; try { source = System.IO.File.ReadAllText(args[0]); } catch (IndexOutOfRangeException) { Console.WriteLine("Give the name of the source file as a parameter."); return; } catch (System.IO.FileNotFoundException) { Console.WriteLine("File \"" + args[0] + "\" not found."); return; } try { Parser parser = new Parser(new Scanner(source)); Program program = parser.Parse(); TypeCheckingVisitor typechecker = new TypeCheckingVisitor(); SymbolTable symboltable = typechecker.BuildSymbolTableAndTypeCheck(program); InterpretingNodeVisitor interpreter = new InterpretingNodeVisitor(symboltable); interpreter.Run(program); } catch (LexicalError e) { Console.WriteLine("Lexical error: " + e.Message + "\n"); } catch (SyntaxError e) { Console.WriteLine("Syntax error: " + e.Message + "\n"); } catch (SemanticError e) { Console.WriteLine("Semantic error: " + e.Message + "\n"); } catch (MiniPLAssertionFailed e) { Console.WriteLine(e.Message); } catch (MiniPLReadError e) { Console.WriteLine(e.Message); } }
public ParsingTask Process(ParsingTask input) { if (input.SymbolTable == null) { _canContinue = false; return(input); } var typeErrorExtractor = new TypeCheckingVisitor(input.SymbolTable); input.Ast.Accept(typeErrorExtractor); typeErrorExtractor.TypeErrors.ForEach(x => input.Errors.Add(x.ToString())); _canContinue = !typeErrorExtractor.TypeErrors.Any(); return(input); }
public void VisitMathExpressionTree() { var target = new TypeCheckingVisitor(); var one = new ConstantExpr(1); var two = new ConstantExpr(2); var three = new ConstantExpr(3); var four = new ConstantExpr(4); var five = new ConstantExpr(5); var six = new ConstantExpr(6); var expr = new DivExpr(new MultExpr(three, six), new MultExpr(new MinusExpr(five, one), new PlusExpr(four, two))); var actual = target.Visit(expr, scope); Assert.AreEqual(ValueType.Int, actual); }
public void NoneShouldThrowNotImplementedException() { var visitMethods = from method in typeof(TypeCheckingVisitor).GetMethods() where method.Name == "Visit" select method; var target = new TypeCheckingVisitor(); var errors = new List <string>(); foreach (var method in visitMethods) { var parameter = (from param in method.GetParameters() where typeof(Expression).IsAssignableFrom(param.ParameterType) select param).First(); try { method.Invoke(target, new object[] { null, null }); } catch (TargetParameterCountException) { Assert.Fail("Parameter Count Error in Reflection Call."); } catch (Exception e) { if (e is NotImplementedException || e.InnerException is NotImplementedException) { // Not implemented! This is unacceptable! errors.Add(string.Format("Unimplemented Visit method for type {0} found.", parameter.ParameterType.FullName)); } // all other exception types are fine as we are passing null to the methods. We would expect them to throw. } } if (errors.Any()) { var message = string.Join("\r\n", errors.ToArray()); Assert.Fail(message); } }
public void NoneShouldThrowNotImplementedException() { var visitMethods = from method in typeof(TypeCheckingVisitor).GetMethods() where method.Name == "Visit" select method; var target = new TypeCheckingVisitor(); var errors = new List<string>(); foreach (var method in visitMethods) { var parameter = (from param in method.GetParameters() where typeof(Expression).IsAssignableFrom(param.ParameterType) select param).First(); try { method.Invoke(target, new object[] { null, null }); } catch (TargetParameterCountException) { Assert.Fail("Parameter Count Error in Reflection Call."); } catch (Exception e) { if (e is NotImplementedException || e.InnerException is NotImplementedException) { // Not implemented! This is unacceptable! errors.Add(string.Format("Unimplemented Visit method for type {0} found.", parameter.ParameterType.FullName)); } // all other exception types are fine as we are passing null to the methods. We would expect them to throw. } } if (errors.Any()) { var message = string.Join("\r\n", errors.ToArray()); Assert.Fail(message); } }
public void InvalidLValueAssignmentTest() { var target = new TypeCheckingVisitor(); var expr = new AssignmentExpr(new ConstantExpr("a"), new ConstantExpr(1)); target.Visit(expr, scope); }
public void Run() { // VarAssignmentNotAllowedHereError RemainingSyntaxErrorsDetectionVisitor remainingSyntaxErrorsDetectionVisitor = new RemainingSyntaxErrorsDetectionVisitor(); remainingSyntaxErrorsDetectionVisitor.VisitTree(AbstractSyntaxTree); // RedefinedIdentifierError // KeywordUsedAsNameError SymbolTableCreationVisitor symbolTableCreationVisitor = new SymbolTableCreationVisitor(); symbolTableCreationVisitor.VisitTree(AbstractSyntaxTree); SymbolTable = symbolTableCreationVisitor.SymbolTable; SymbolsWithInstructions = symbolTableCreationVisitor.SymbolsWithInstructions; // UnknownTypeNameError // UnsupportedTypeError // UnsupportedArrayTypeError // UnsupportedFunctionTypeError TypeResolver typeResolver = new TypeResolver(SymbolTable); typeResolver.Resolve(symbolTableCreationVisitor.TypedSymbols); // NotClassOrPrototypeReferenceError // UndeclaredIdentifierError // InfiniteReferenceLoopError InheritanceResolver inheritanceResolver = new InheritanceResolver(SymbolTable); inheritanceResolver.Resolve(symbolTableCreationVisitor.SubclassSymbols); // UndeclaredIdentifierError // AccessToAttributeOfArrayElementNotSupportedError // AttributeOfNonInstanceError // ClassDoesNotHaveAttributeError // ReferencedSymbolIsNotArrayError ReferenceResolvingVisitor referenceResolvingVisitor = new ReferenceResolvingVisitor(SymbolTable); referenceResolvingVisitor.Visit(AbstractSyntaxTree.ReferenceNodes); // InfiniteConstReferenceLoopError // ArraySizeEqualsZeroError // TooBigArraySizeError // ArraySizeNotConstIntegerError // InconsistentArraySizeError // IndexOutOfRangeError // TooBigArrayIndex // ConstIntegerExpectedError // ArrayIndexNotConstIntegerError // ArithmeticOperationOverflowError // DivideByZeroError // InvalidUnaryOperationError // InvalidBinaryOperationError // IntegerLiteralTooLargeError // CannotInitializeConstWithValueOfDifferentTypeError // CannotInitializeArrayElementWithValueOfDifferentTypeError ConstEvaluationVisitor constEvaluationVisitor = new ConstEvaluationVisitor(); constEvaluationVisitor.Visit(symbolTableCreationVisitor.ConstDefinitionNodes); constEvaluationVisitor.Visit(symbolTableCreationVisitor.ArrayDeclarationNodes); constEvaluationVisitor.Visit(referenceResolvingVisitor.ArrayIndexNodes); // ArgumentsCountDoesNotMatchError TypeCheckingVisitor typeCheckingVisitor = new TypeCheckingVisitor(SymbolTable); typeCheckingVisitor.VisitTree(AbstractSyntaxTree); // UnusedSymbolWarning // NamesNotMatchingCaseWiseWarning DeclarationUsagesChecker declarationUsagesChecker = new DeclarationUsagesChecker(SymbolTable, _zenFileNodes); declarationUsagesChecker.Check(symbolTableCreationVisitor.DeclarationNodes); // UsageOfNonInitializedVariableWarning UninitializedSymbolUsageDetectionVisitor uninitializedSymbolUsageDetectionVisitor = new UninitializedSymbolUsageDetectionVisitor(); uninitializedSymbolUsageDetectionVisitor.VisitTree(AbstractSyntaxTree); // IterationStatementNotInLoopError // IntegerLiteralTooLargeError // SingleExpressionWarning // WrongClassSizeError // ConstValueChangedWarning // UsageOfNonInitializedVariableWarning RemainingAnnotationsAdditionVisitor remainingAnnotationsAdditionVisitor = new RemainingAnnotationsAdditionVisitor(SymbolTable); remainingAnnotationsAdditionVisitor.VisitTree(AbstractSyntaxTree); }
public void SetUp() { statementlist = new List<Statement>(); symbolTableBuilder = new TypeCheckingVisitor(); }