primitive types

This commit is contained in:
pca006132 2020-12-22 16:53:53 +08:00 committed by pca006132
parent ae65b2dd09
commit a719945779

69
toy-impl/primitives.py Normal file
View File

@ -0,0 +1,69 @@
from type_def import *
types = {
'int32': PrimitiveType('int32'),
'int64': PrimitiveType('int64'),
'float': PrimitiveType('float'),
'bool': PrimitiveType('bool')
}
i32 = types['int32']
i64 = types['int64']
f32 = types['float']
b = types['bool']
I = TypeVariable('I', [i32, i64, f32])
def impl_math(ty):
ty.methods['__add__'] = ([SelfType(), ty], ty, set())
ty.methods['__sub__'] = ([SelfType(), ty], ty, set())
ty.methods['__mul__'] = ([SelfType(), ty], ty, set())
ty.methods['__neg__'] = ([SelfType()], ty, set())
ty.methods['__truediv__'] = ([SelfType(), ty], f32, set())
ty.methods['__floordiv__'] = ([SelfType(), ty], ty, set())
ty.methods['__mod__'] = ([SelfType(), ty], ty, set())
ty.methods['__pow__'] = ([SelfType(), ty], ty, set())
def impl_bits(ty):
ty.methods['__lshift__'] = ([SelfType(), i32], ty, set())
ty.methods['__rshift__'] = ([SelfType(), i32], ty, set())
ty.methods['__xor__'] = ([SelfType(), ty], ty, set())
def impl_logic(ty):
ty.methods['__and__'] = ([SelfType(), ty], ty, set())
ty.methods['__or__'] = ([SelfType(), ty], ty, set())
def impl_eq(ty):
ty.methods['__eq__'] = ([SelfType(), ty], b, set())
ty.methods['__ne__'] = ([SelfType(), ty], b, set())
def impl_order(ty):
ty.methods['__lt__'] = ([SelfType(), ty], b, set())
ty.methods['__gt__'] = ([SelfType(), ty], b, set())
ty.methods['__le__'] = ([SelfType(), ty], b, set())
ty.methods['__ge__'] = ([SelfType(), ty], b, set())
impl_logic(b)
impl_eq(b)
impl_math(i32)
impl_bits(i32)
impl_logic(i32)
impl_eq(i32)
impl_order(i32)
impl_math(i64)
impl_bits(i64)
impl_logic(i64)
impl_eq(i64)
impl_order(i64)
impl_math(f32)
impl_eq(f32)
impl_order(f32)
i32.methods['__init__'] = ([SelfType(), I], None, {'I'})
i64.methods['__init__'] = ([SelfType(), I], None, {'I'})
f32.methods['__init__'] = ([SelfType(), I], None, {'I'})
simplest_ctx = Context({}, types)