def getTables(s):
    tSugar = hash.create(128, getShugarFromeScope(s))
    tTypes2 = Type.getTypesFromScope(tree[1])

    tTypes = hash.create(256, tTypes2[0])
    tTypeConstructor = hash.create(256, tTypes2[1])
    return (tSugar, tTypes, tTypeConstructor)
Esempio n. 2
0
	def unifi(a,b,at,bt):
		if type(a) != list and type(b)!=list:
			if ord(a[0]) < ord('a') and ord(b[0]) < ord('a'):
				if a == b:
					return (at,bt,True)
				else:
					return (at,bt,False)
			else:
				if ord(a[0]) >= ord('a'):
					nnn = hash.get(at,a)
					if nnn == None:
						return (hash.create(256,hash.toList(at)+[(a,b)]),bt,True)
					else:
						if type(nnn) != list:
							if ord(nnn[0]) >= ord('a'):
								return (at,bt,True)
						return unifi(nnn,b,at,bt)
				else:
					nnn = hash.get(bt,b)
					if nnn == None:
						return (at,hash.create(256,hash.toList(bt)+[(b,a)]),True)
					else:
						if type(nnn) != list:
							if ord(nnn[0]) >= ord('a'):
								return (at,bt,True)
						return unifi(a,nnn,at,bt)
		elif type(a) != list:
			if ord(a[0]) >= ord('a'):
				nnn = hash.get(at,a)
				if nnn == None:
					return (hash.create(256,hash.toList(at)+[(a,b)]),bt,True)
				else:
					if type(nnn) != list:
						if ord(nnn[0]) >= ord('a'):
							return (at,bt,True)
					return unifi(nnn,b,at,bt)
			return (at,bt,False)
		elif type(b) != list:
			if ord(b[0]) >= ord('a'):
				nnn = hash.get(bt,b)
				if nnn == None:
					return (at,hash.create(256,hash.toList(bt)+[(b,a)]),True)
				else:
					if type(nnn) != list:
						if ord(nnn[0]) >= ord('a'):
							return (at,bt,True)
					return unifi(a,nnn,at,bt)
			return (at,bt,False)
		else:
			at1,bt1,b1 = unifi(a[0],b[0],at,bt)
			at2,bt2,b2 = unifi(a[1],b[1],at1,bt1)
			return (at2,bt2,b1 and b2)
		return (at,bt,False)
def checkOnUseSugar(x, t):
    listOfAll = f.reduce(lambda a, x: a + hash.toList(x), t, [])
    tOfAll = hash.create(1024, listOfAll)
    res = hash.get(tOfAll, x[0])
    if res == None:
        return True
    sys.exit('ERROR: name conflict:  syntax sugar and type constructor\n' +
             'name: ' + x[0] + '\n' + 'syntax sugar declared in line: ' +
             str(x[1]) + '\n' + 'type constructor declared in line: ' +
             str(res[1]) + '\n')
    return False
Esempio n. 4
0
def checkRedefined(t,msg):
	if t == []:
		return
	listOfAll = f.reduce(lambda a,x:a+hash.toList(x),t,[])
	tOfAll = hash.create(1024,listOfAll);
	def checkForName(x):
		res = hash.getAll(tOfAll,x[0])
#		print(res)
		if len(res) > 1:
			sys.exit(msg +
			'name: ' + x[0] + '\n' +
			'declared in lines: ' +
			f.reduce(lambda a,x: a + (str(x[1]) if x[1]>=0 else 'Generic') +', ', res,"") +'\n')
		return
	list(map(checkForName,listOfAll))
Esempio n. 5
0
tree = tree[0]


# print(tree)
# print(sss)
def getAllInfix(s):
    def tmp(a, x):
        if x[0] == parser.pP_Dec:
            if x[1][1][1] != []:
                mode = x[1][1][1][0][1]
                modeTo = mode == 'infixr'
                return a + [(x[1][0][1], modeTo)]
        return a

    return f.reduce(tmp, s, [])


tInfix = getAllInfix(tree[1])
tInfix = hash.create(127, tInfix)


def In(tInfix, s):
    return hash.get(tInfix, s) != None


get = lambda t: lambda s: hash.get(t, s)
isR = get(tInfix)

print(isR('>>>'))
dt.draw(tree)
s
s = list(
    filter(
        lambda x: not x[0] in
        [lexer.tT_comment, lexer.tT_skip, lexer.tT_preproc], s))

tree, l = parser.P_START([], s)
if tree == None:
    msg = ' '.join(list(map(lambda x: x[1], l[1:10])))
    sys.exit('ERROR: syntax about ' + str(l[1][2]) + ' line\n' +
             'it is possinle near this text: ' + msg + '\n' +
             'I want to token ' + str(l[0]) + '\n')
tree = tree[0]

##############
tDefTypes = hash.create(128, [('Ptr', (['Ptr', 'a'], -1))] + list(
    map(lambda x:
        (x, (x, -1)), 'I8,I16,I32,U8,U16,U32,F8,F16,F32,F64'.split(','))))
tDefConstructor = hash.create(128,
                              [('Ptr', (Type.strToType('a -> Ptr a'), -1))])

tSugar, tTypes, tTypeConstructor = getTables(tree[1])
checkTables(tSugar,[tTypes,tDefTypes],[tTypeConstructor,tDefConstructor])\

#########################################################
tmpL = (listNameFromScopeWithOptions(tree[1]) + hash.toList(tTypeConstructor) +
        hash.toList(tDefConstructor))
tFromThis = hash.create(256, tmpL)
tree = fixAst.fixAst(tree[1][-1][1][2][1], tFromThis, tSugar)
print(tree)

#########################################################
Esempio n. 7
0
def checkType(t,ts,line):
	if ts == []:
		return
	listOfAll = f.reduce(lambda a,x: a + hash.toList(x),ts,[])
	tOfAll = hash.create(1024,listOfAll)
	def check(x):
		if type(x) == list:
			return check(x[0]) and check(x[1])
		if ord(x[0]) >= ord('a') and ord(x[0]) <= ord('z'):
			return True
#		print(x)
		res = hash.getAll(tOfAll,x)
#		print(len(res))
		if len(res) == 0:
			sys.exit('ERROR: missing type declaration\n'+
				'Undeclared type: ' + x +'\n' +
				'in line: ' + str(line))
		return True


	def checkNameUses(x):
#		print('In check: ',x)
		if type(x) == list:
			if type(x[0]) != list:
				if ord(x[0][0]) >= ord('A') and ord(x[0][0]) <= ord('X'):
					isThisC = hash.get(tOfAll,x[0])
#					print(x,isThisC[0])
					if type(isThisC[0]) != list:
#						pass
						return checkNameUses(x[0]) and checkNameUses(x[1])
					else:
#						_,_,b = unifi(x, isThisC[0])
#						if b == False:
#							sys.exit('ERROR: wrong parameters for type\n' +
#									 'in line: ' + str(line) + '\n' +
#									 'for type: ' + x[0] + '\n' +
#									 'declared in line: ' + str(isThisC[1]) + '\n' +
#									 'with pattern: ' + str(isThisC[0]) + '\n' +
#									 '            | ' + toString(isThisC[0]) + '\n' +
#									 'in full type: ' + str(t) + '\n' +
#									 '            | ' + toString(t) + '\n' +
#									 'in local pattern: ' + str(x) + '\n' +
#									 '                | ' + toString(x) + '\n' )
#							return False
#							print('Error:',toString(x),x,isThisC[1])
						return checkNameUses(x[1])
				else:
					return checkNameUses(x[1])
			else:
				return checkNameUses(x[0]) and checkNameUses(x[1])
		else:
#			print(x)
			if ord(x[0]) >= ord('A') and ord(x[0]) <= ord('X'):
				isThisC = hash.get(tOfAll,x)
				if isThisC != None:
#					print(isThisC)
					if type(isThisC[0]) != list:
						return True
					else:
						sys.exit('ERROR: missing parameters for type\n' +
								 'in line: ' + str(line) + '\n' +
								 'for type: ' + x + '\n' +
								 'declared in line: ' + str(isThisC[1]) + '\n' +
								 'with pattern: ' + str(isThisC[0]) + '\n' +
								 '            | ' + toString(isThisC[0]) + '\n' +
								 'in full type: ' + str(t) + '\n' +
								 '            | ' + toString(t) + '\n' )
#						print('Error:',x,isThisC[1],isThisC[0])
						return False
				else:
					sys.exit('ERROR: undefined type:' + str(x) +'\n' +
							  'in line: ' + str(line) + '\n')
					return False
			else:
				return True

	if check(t) == True:
		if checkNameUses(t):
			return True
		else:
			return False
	else:
		return False