def getNineType(theType): from ast.external import ExternalType from ast.vartypes import Type if isinstance(theType, Type): return theType elif isinstance(theType, type): return ExternalType.getNineType(clr.GetClrType(theType)) elif isinstance(theType, basestring): return ExternalType.getNineType(System.Type.GetType(theType)) return ExternalType.getNineType(theType)
def getNineType(theType): from ast.external import ExternalType from ast.vartypes import Type if isinstance(theType, CLRMeta): theType = typeToType(theType) # all classes from the CLR namespace are .NET classes. if theType.__module__.startswith('CLR'): return ExternalType.getNineType(theType) elif isinstance(theType, basestring): return ExternalType.getNineType(System.Type.GetType(theType)) elif isinstance(theType, Type): return theType else: assert False, (theType, type(theType))
def __init__(self, position, arrayType, arity): self.position = position self.arrayType = arrayType self.arity = arity self.__builder = None from ast.external import ExternalType from CLR import System self.bases = [ExternalType.getNineType(System.Array)]
def semantic(self, scope): from ast import vartypes from nine import error, util import CLR expr = self.expr.semantic(scope) from ast.external import ExternalType # if not util.getNineType(System.Exception).isParentClass(expr.getType()): if not ExternalType.getNineType(util.typeToType(CLR.System.Exception)).isParentClass(expr.getType()): raise error.SyntaxError( self.position, "You can only throw object instances which inherit System.Exception, not %r" % expr.getType(), ) return RaiseStatement(self.position, expr)
def fixPrimitives(self): # HACK: make the primitive types match up. (disregard this, as it is wholly insane) import System from nine.util import typeToType as t2t from ast.external import ExternalType from ast import vartypes vartypes.IntType = ExternalType.getNineType(t2t(System.Int32)) vartypes.FloatType = ExternalType.getNineType(t2t(System.Single)) vartypes.DoubleType = ExternalType.getNineType(t2t(System.Double)) vartypes.CharType = ExternalType.getNineType(t2t(System.Char)) vartypes.StringType = ExternalType.getNineType(t2t(System.String)) vartypes.BooleanType = ExternalType.getNineType(t2t(System.Boolean)) vartypes.VoidType = ExternalType.getNineType(t2t(System.Void)) vartypes.PrimitiveTypes = { 'int' : vartypes.IntType, 'float' : vartypes.FloatType, 'double' : vartypes.DoubleType, 'boolean' : vartypes.BooleanType, 'char' : vartypes.CharType, 'string' : vartypes.StringType, 'void' : vartypes.VoidType }
def fixPrimitives(self): # HACK: make the primitive types match up. (disregard this, as it is wholly insane) import CLR from nine.util import typeToType as t2t from ast.external import ExternalType from ast import vartypes vartypes.IntType = ExternalType.getNineType(t2t(CLR.System.Int32)) vartypes.FloatType = ExternalType.getNineType(t2t(CLR.System.Single)) vartypes.CharType = ExternalType.getNineType(t2t(CLR.System.Char)) vartypes.StringType = ExternalType.getNineType(t2t(CLR.System.String)) vartypes.BooleanType = ExternalType.getNineType(t2t(CLR.System.Boolean)) vartypes.VoidType = ExternalType.getNineType(t2t(CLR.System.Void)) vartypes.PrimitiveTypes = { "int": vartypes.IntType, "float": vartypes.FloatType, "boolean": vartypes.BooleanType, "char": vartypes.CharType, "string": vartypes.StringType, "void": vartypes.VoidType, }
def _scanAssembly(self, globalNs, assemblyName): from ast.namespace import Namespace from ast.vartypes import Type from ast.external import ExternalType import System from System import Reflection from System.Reflection import Assembly #assembly = Assembly.LoadWithPartialName(assemblyName) assembly = self._loadAssembly(assemblyName) if assembly is None: raise Exception, 'Unable to load assembly %s' % assemblyName namespaces = globalNs def getNs(name, curNs=None): ns = curNs or globalNs if len(name) == 0: return curNs else: n = name[0] if n not in ns.symbols: ns.symbols[n] = Namespace(n) return getNs(name[1:], ns.symbols[n]) '''Absurdly complex, given how little it does. :P For each type whose full name does not begin with something freaky like $ create namespace instances and a Type instance within the whole heirarchy to correspond with all the types in the assembly. For instance, storing the System.Console class requires creating or reopening of the System namespace, in which the Console Type gets poured. FIXME: nested types are not correctly handled at all. ''' for type in assembly.GetTypes(): if not type.Name[0].isalpha() and type.Name[0] != '_': continue if type.IsNestedPublic: outerName = type.FullName outerName, innerName = type.FullName.split('+', 1) outerName = outerName.split('.') outerNs = getNs(outerName[:-1]) assert outerName[-1] in outerNs.symbols, "Internal badness: have to process a nested class before we've processed the enclosing class." outerClass = outerNs.symbols[outerName[-1]] outerClass.symbols[type.Name] = ExternalType.getNineType(type) else: if type.Namespace is None: ns = globalNs else: ns = getNs(type.Namespace.split('.')) ns.symbols[type.Name] = ExternalType.getNineType(type) return namespaces