protected T WithTypes <T>(T syntaxTree, TypeChecker typeChecker, params TypeMapping[] symbols) where T : Expression { var typedExpression = typeChecker.TypeCheck(syntaxTree, Scope(symbols)); typeChecker.HasErrors.ShouldBeFalse(); return((T)typedExpression); }
public void CanCreateFullyTypedInstance() { var @class = @"class Foo { bool Even(int n) { if (n==0) true else Odd(n-1) } bool Odd(int n) { if (n==0) false else Even(n-1) } int Test() { if (Even(4)) 0 else 1 } }".ParseClass(); var constructorReturningFoo = NamedType.Constructor(new NamedType(@class)); @class.Methods.ShouldList( even => { even.Name.Identifier.ShouldEqual("Even"); even.Type.ShouldEqual(Unknown); even.Body.Type.ShouldEqual(Unknown); }, odd => { odd.Name.Identifier.ShouldEqual("Odd"); odd.Type.ShouldEqual(Unknown); odd.Body.Type.ShouldEqual(Unknown); }, test => { test.Name.Identifier.ShouldEqual("Test"); test.Type.ShouldEqual(Unknown); test.Body.Type.ShouldEqual(Unknown); }); @class.Type.ShouldEqual(Unknown); var typeRegistry = new TypeRegistry(); typeRegistry.Add(@class); var typeChecker = new TypeChecker(typeRegistry); var typedClass = typeChecker.TypeCheck(@class, Scope()); typedClass.Methods.ShouldList( even => { even.Name.Identifier.ShouldEqual("Even"); even.Type.ToString().ShouldEqual("System.Func<int, bool>"); even.Body.Type.ShouldEqual(Boolean); }, odd => { odd.Name.Identifier.ShouldEqual("Odd"); odd.Type.ToString().ShouldEqual("System.Func<int, bool>"); odd.Body.Type.ShouldEqual(Boolean); }, test => { test.Name.Identifier.ShouldEqual("Test"); test.Type.ToString().ShouldEqual("System.Func<int>"); test.Body.Type.ShouldEqual(Integer); }); typedClass.Type.ShouldEqual(constructorReturningFoo); }
protected Vector <CompilerError> ShouldFailTypeChecking(string source, TypeChecker typeChecker, params TypeMapping[] symbols) { var expression = Parse(source); typeChecker.TypeCheck(expression, Scope(symbols)); typeChecker.HasErrors.ShouldBeTrue(); return(typeChecker.Errors); }
protected DataType Type(string source, TypeChecker typeChecker, params TypeMapping[] symbols) { var expression = Parse(source); var typedExpression = typeChecker.TypeCheck(expression, Scope(symbols)); typedExpression.ShouldNotBeNull(); typeChecker.HasErrors.ShouldBeFalse(); return(typedExpression.Type); }
private Vector <CompilerError> ShouldFailTypeChecking(string source, params TypeMapping[] symbols) { var @class = Parse(source); var typeChecker = new TypeChecker(); typeChecker.TypeCheck(@class, Scope(symbols)); typeChecker.HasErrors.ShouldBeTrue(); return(typeChecker.Errors); }
private Vector <CompilerError> ShouldFailTypeChecking(string source) { var compilationUnit = Parse(source); var typeChecker = new TypeChecker(); typeChecker.TypeCheck(compilationUnit); typeChecker.HasErrors.ShouldBeTrue(); return(typeChecker.Errors); }
public CompilerResult Build(CompilationUnit compilationUnit) { var typeChecker = new TypeChecker(); var typedCompilationUnit = typeChecker.TypeCheck(compilationUnit); if (typeChecker.HasErrors) return new CompilerResult(Language.Rook, typeChecker.Errors); string translatedCode = Translate(typedCompilationUnit); return csCompiler.Build(translatedCode); }
private DataType Type(string source, params TypeMapping[] symbols) { var function = Parse(source); var typeChecker = new TypeChecker(); var typedFunction = typeChecker.TypeCheck(function, Scope(symbols)); typedFunction.ShouldNotBeNull(); typeChecker.HasErrors.ShouldBeFalse(); return(typedFunction.Type); }
public void TypeChecksAsExtensionMethodCallWhenInstanceTypeDoesNotContainMethodWithExpectedName() { var typeChecker = new TypeChecker(); var node = (MethodInvocation)Parse("math.SomeExtensionMethod(false)"); var typedCall = (Call)typeChecker.TypeCheck(node, Scope(math => mathType, SomeExtensionMethod => Function(new[] { mathType, Boolean }, Integer))); typedCall.Callable.Type.ShouldEqual(Function(new[] { mathType, Boolean }, Integer)); typedCall.Arguments.ShouldHaveTypes(mathType, Boolean); typedCall.Type.ShouldEqual(Integer); }
private CompilerResult Build(CompilationUnit compilationUnit, out string translation) { translation = ""; var typeChecker = new TypeChecker(); var typedCompilationUnit = typeChecker.TypeCheck(compilationUnit); if (typeChecker.HasErrors) return new CompilerResult(Language.Rook, typeChecker.Errors); translation = Translate(typedCompilationUnit); return csCompiler.Build(translation); }
public void HasATypeCorrespondingWithTheDefaultConstructor() { var fooClass = "class Foo { }".ParseClass(); var typeRegistry = new TypeRegistry(); var constructorReturningFoo = NamedType.Constructor(new NamedType(fooClass)); typeRegistry.Add(fooClass); var typeChecker = new TypeChecker(typeRegistry); typeChecker.TypeCheck(fooClass, Scope()).Type.ShouldEqual(constructorReturningFoo); typeChecker.HasErrors.ShouldBeFalse(); }
public void PassesTypeCheckingEvenWhenMethodNamesAreTheSameAsNamesInTheSurroundingScope() { var fooClass = "class Foo { int A() {0} int B() {2} }".ParseClass(); var typeRegistry = new TypeRegistry(); var constructorReturningFoo = NamedType.Constructor(new NamedType(fooClass)); typeRegistry.Add(fooClass); var typeChecker = new TypeChecker(typeRegistry); typeChecker.TypeCheck(fooClass, Scope(B => NamedType.Function(NamedType.Boolean))).Type.ShouldEqual(constructorReturningFoo); typeChecker.HasErrors.ShouldBeFalse(); }
public void CanCreateFullyTypedInstance() { var node = Parse("bool foo(int x, int y, bool z) {x+y>0 && z}"); node.Parameters.ShouldHaveTypes(Unknown, Unknown, Unknown); node.Body.Type.ShouldEqual(Unknown); node.Type.ShouldEqual(Unknown); var typeChecker = new TypeChecker(); var typedNode = typeChecker.TypeCheck(node, Scope()); typedNode.Parameters.ShouldHaveTypes(Integer, Integer, Boolean); typedNode.Body.Type.ShouldEqual(Boolean); typedNode.Type.ShouldEqual(NamedType.Function(new[] { Integer, Integer, Boolean }, Boolean)); }
public Expression WithTypes(TypeChecker visitor, Scope scope) { return(visitor.TypeCheck(this, scope)); }
public Class WithTypes(TypeChecker visitor, Scope scope) { return(visitor.TypeCheck(this, scope)); }
public CompilationUnit WithTypes(TypeChecker visitor) { return(visitor.TypeCheck(this)); }
protected DataType Type(string source, Scope scope, TypeChecker typeChecker) { var expression = Parse(source); return(typeChecker.TypeCheck(expression, scope).Type); }
public void TypesAllClassesAndFunctions() { var compilationUnit = Parse( @"class Foo { } class Bar { } bool Even(int n) {if (n==0) true else Odd(n-1)} bool Odd(int n) {if (n==0) false else Even(n-1)} int Main() {if (Even(4)) 0 else 1}"); var fooType = new NamedType(compilationUnit.Classes.Single(c => c.Name.Identifier == "Foo")); var barType = new NamedType(compilationUnit.Classes.Single(c => c.Name.Identifier == "Bar")); var fooConstructorType = NamedType.Constructor(fooType); var barConstructorType = NamedType.Constructor(barType); var typeChecker = new TypeChecker(); var typedCompilationUnit = typeChecker.TypeCheck(compilationUnit); compilationUnit.Classes.ShouldList( foo => foo.Type.ShouldEqual(Unknown), bar => bar.Type.ShouldEqual(Unknown)); compilationUnit.Functions.ShouldList( even => { even.Name.Identifier.ShouldEqual("Even"); even.Type.ShouldEqual(Unknown); even.Body.Type.ShouldEqual(Unknown); }, odd => { odd.Name.Identifier.ShouldEqual("Odd"); odd.Type.ShouldEqual(Unknown); odd.Body.Type.ShouldEqual(Unknown); }, main => { main.Name.Identifier.ShouldEqual("Main"); main.Type.ShouldEqual(Unknown); main.Body.Type.ShouldEqual(Unknown); }); typedCompilationUnit.Classes.ShouldList( foo => foo.Type.ShouldEqual(fooConstructorType), bar => bar.Type.ShouldEqual(barConstructorType)); typedCompilationUnit.Functions.ShouldList( even => { even.Name.Identifier.ShouldEqual("Even"); even.Type.ToString().ShouldEqual("System.Func<int, bool>"); even.Body.Type.ShouldEqual(Boolean); }, odd => { odd.Name.Identifier.ShouldEqual("Odd"); odd.Type.ToString().ShouldEqual("System.Func<int, bool>"); odd.Body.Type.ShouldEqual(Boolean); }, main => { main.Name.Identifier.ShouldEqual("Main"); main.Type.ToString().ShouldEqual("System.Func<int>"); main.Body.Type.ShouldEqual(Integer); }); }