def testTypeParameterCycleForeign(self): package = Package(name=Name(["foo"])) barClass = package.addClass(Name(["Bar"]), sourceName="Bar", typeParameters=[], supertypes=[getRootClassType()], constructors=[], fields=[], methods=[], flags=frozenset([PUBLIC])) loader = FakePackageLoader([package]) source = "class Baz <: foo.Bar\n" + \ "def f[static T <: Baz >: foo.Bar] = ()" self.assertRaises(InheritanceException, self.analyzeFromSource, source, packageLoader=loader)
def testInheritFromImportedClass(self): foo = Package(name=Name(["foo"])) Bar = foo.addClass(Name(["Bar"]), sourceName="Bar", typeParameters=[], supertypes=[getRootClassType()], constructors=[], fields=[], methods=[], flags=frozenset([PUBLIC])) x = foo.newField(Name(["Bar", "x"]), sourceName="x", flags=frozenset([PUBLIC, LET])) Bar.fields.append(x) source = "import foo.Bar\n" + \ "class Baz <: Bar" info = self.analyzeFromSource(source, packageLoader=FakePackageLoader([foo])) bazScope = info.getScope(info.ast.modules[0].definitions[1]) self.assertTrue(bazScope.isBound("x"))
def testInheritForeignTypeInForeignTypeInSamePackage(self): package = Package(name=Name(["foo"])) barClass = package.addClass(Name(["Bar"]), sourceName="Bar", typeParameters=[], supertypes=[getRootClassType()], constructors=[], fields=[], methods=[], flags=frozenset([PUBLIC])) bazClass = package.addClass(Name(["Baz"]), sourceName="Baz", typeParameters=[], supertypes=[ClassType(barClass), getRootClassType()], constructors=[], fields=[], methods=[], flags=frozenset([PUBLIC])) loader = FakePackageLoader([package]) info = self.analyzeFromSource("class Quux <: foo.Baz", packageLoader=loader) quuxClass = info.package.findClass(name="Quux") self.assertEquals([ClassType(bazClass), ClassType(barClass), getRootClassType()], quuxClass.supertypes)
def testInheritFromForeignType(self): package = Package(name=Name(["foo"])) foreignClass = package.addClass(Name(["Bar"]), sourceName="Bar", typeParameters=[], supertypes=[getRootClassType()], constructors=[], fields=[], methods=[], flags=frozenset([PUBLIC])) field = package.newField(Name(["Bar", "x"]), sourceName="x", flags=frozenset([PUBLIC])) foreignClass.fields = [field] loader = FakePackageLoader([package]) source = "class Baz <: foo.Bar" info = self.analyzeFromSource(source, packageLoader=loader) bazClass = info.package.findClass(name="Baz") self.assertEquals([ClassType(foreignClass), getRootClassType()], bazClass.supertypes) bazScope = info.getScope(bazClass) self.assertTrue(bazScope.isBound("x"))
def analyzeFromSource(self, source, packageLoader=None): if packageLoader is None: packageLoader = FakePackageLoader([]) ast = self.parseFromSource(source) info = CompileInfo(ast, package=Package(ids.TARGET_PACKAGE_ID), packageLoader=packageLoader, isUsingStd=False) analyzeDeclarations(info) analyzeTypeDeclarations(info) analyzeInheritance(info) return info
def loadPackage(self, name, loc=NoLoc): assert name in self.packageNames if name not in self.packages: self.packages[name] = Package(name=name) package = self.packages[name] if package.id not in self.loadedIds: self.loadedIds.add(package.id) for dep in package.dependencies: dep.package = self.loadPackage(dep.name, loc) self._runLoadHooks(package) return package
def __init__(self, packagesOrPackageNames): super(FakePackageLoader, self).__init__() if len(packagesOrPackageNames) == 0: self.packageNames = [] self.packages = {} elif isinstance(packagesOrPackageNames[0], Package): self.packageNames = [p.name for p in packagesOrPackageNames] self.packages = {p.name: p for p in packagesOrPackageNames} else: assert isinstance(packagesOrPackageNames[0], Name) self.packageNames = packagesOrPackageNames self.packages = { name: Package(name=name) for name in packagesOrPackageNames } self.loadedIds = set()
def testInheritForeignTypeInForeignTypeInDifferentPackage(self): fooPackage = Package(name=Name(["foo"])) barClass = fooPackage.addClass(Name(["Bar"]), sourceName="Bar", typeParameters=[], supertypes=[getRootClassType()], constructors=[], fields=[], methods=[], flags=frozenset([PUBLIC])) bazPackage = Package(name=Name(["baz"])) loader = FakePackageLoader([fooPackage, bazPackage]) bazPackage.dependencies.append(PackageDependency.fromPackage(fooPackage)) quuxClass = bazPackage.addClass(Name(["Quux"]), sourceName="Quux", typeParameters=[], supertypes=[ClassType(barClass), getRootClassType()], constructors=[], fields=[], methods=[], flags=frozenset([PUBLIC])) info = self.analyzeFromSource("class Zzyzx <: baz.Quux", packageLoader=loader) zzyzxClass = info.package.findClass(name="Zzyzx") self.assertEquals([ClassType(quuxClass), ClassType(barClass), getRootClassType()], zzyzxClass.supertypes)
def main(): sys.setrecursionlimit(10000) PackageName = lambda s: Name.fromString(s, isPackageName=True) cmdline = argparse.ArgumentParser( description="Compile source files into CodeSwitch packages") cmdline.add_argument("sources", metavar="source", type=str, nargs="+", help="Source file names") cmdline.add_argument("-p", "--package-name", action="store", type=PackageName, default=PackageName("default"), help="Name of the package being compiled") cmdline.add_argument("-v", "--package-version", action="store", type=PackageVersion.fromString, default=PackageVersion([0]), help="Version of the package being compiled") cmdline.add_argument("-d", "--depends", action="append", type=str, default=[], help="Additional package dependencies") cmdline.add_argument( "--no-std", action="store_true", help="Do not add a dependency on the standard library") cmdline.add_argument( "-P", "--package-path", action="append", type=str, default=[], help="Directories containing packages that could be imported") cmdline.add_argument("-o", "--output", action="store", default="out.csp", help="Name of the output file") cmdline.add_argument("--print-tokens", action="store_true", help="Print tokens after lexical analysis") cmdline.add_argument( "--print-ast", action="store_true", help="Print abstract syntax tree after syntax analysis") cmdline.add_argument("--print-scope", action="store_true", help="Print scope info after scope analysis") cmdline.add_argument("--print-types", action="store_true", help="Print types after type analysis") cmdline.add_argument( "--print-ir", action="store_true", help="Print intermediate representation after compilation") cmdline.add_argument("--print-stack", action="store_true", help="Print compiler stack on error") args = cmdline.parse_args() try: astModules = [] for sourceFileName in args.sources: with open(sourceFileName) as inFile: source = inFile.read() tokens = lex(sourceFileName, source) if args.print_tokens: for tok in tokens: sys.stdout.write(str(tok) + "\n") astModule = parse(sourceFileName, tokens) if args.print_ast: printer = ast.Printer(sys.stdout) printer.visit(astModule) astModules.append(astModule) astPackage = ast.Package(astModules, NoLoc) astPackage.id = AstId(-1) package = Package(TARGET_PACKAGE_ID, args.package_name, args.package_version) loader = PackageLoader( args.package_path if len(args.package_path) > 0 else None) loader.ensurePackageInfo() if len(args.depends) > 0: depPackages = loader.loadPackageFiles(args.depends) each(package.ensureDependency, depPackages) isUsingStd = not args.no_std if isUsingStd: stdPackage = loader.loadPackage(STD_NAME, NoLoc) package.ensureDependency(stdPackage) info = CompileInfo(astPackage, package, loader, isUsingStd=isUsingStd) analyzeDeclarations(info) if args.print_scope: sys.stderr.write("--print-scope not supported right now\n") analyzeTypeDeclarations(info) analyzeInheritance(info) analyzeTypes(info) if args.print_types: sys.stderr.write("--print-types not supported right now\n") convertClosures(info) externalize(info) compile(info) package = info.package if args.print_ir: sys.stdout.write("%s\n" % str(package)) serialize(package, args.output) except (CompileException, IOError) as err: if args.print_stack: raise if isinstance(err, CompileException): sys.stderr.write("%s\n" % str(err)) else: sys.stderr.write("%s: error: %s\n" % (sourceFileName, str(err))) sys.exit(1)