Exemplo n.º 1
0
 def sequence(self):
     x = self.ifthen()
     while self.tl[-1] == ';':
         self.unify(x.type, types.Unit())
         self.tl.pop()
         x = Sequence(x, self.ifthen())
     return x
Exemplo n.º 2
0
    def fun_f(self):
        argtoken = self.tl.pop()
        var = LambdaVar()
        if argtoken == '(':
            assert (self.tl.pop() == ')')
            pname = '()'
            self.unify(var.type, types.Unit())
        else:
            pname = argtoken.name

        self.lambdaTypes.add(var.type)
        # I have no idea what I'm doing
        #ngExtra = {v for v in types.freeVariables(var.type, self.subst) if v not in self.lambdaTypes}
        #self.lambdaTypes |= ngExtra
        attrs = set()
        while self.tl[-1].startswith('@'):
            attrs.add(self.tl.pop()[1:])
        assert (self.tl.pop() == '->')
        self.bind(pname, var)
        x = self.expr()
        self.unbind(pname)
        ret = Lambda(var, x)
        ret.attrs = attrs
        self.lambdaTypes.remove(var.type)
        #self.lambdaTypes -= ngExtra
        return ret
Exemplo n.º 3
0
 def __init__(self, string, unify):
     self.type = VarType()
     unify(self.type,
           types.Sum(types.Product(types.Char, self.type), types.Unit()))
     for c in string:
         assert (ord(c) < 256)
     self.string = string
Exemplo n.º 4
0
 def ifthen(self):
     if self.tl[-1] != 'if':
         return self.product()
     self.tl.pop()
     cond = self.ifthen()
     self.unify(cond.type, types.Bool)
     assert (self.tl.pop() == 'then')
     xTrue = self.ifthen()
     if self.tl[-1] == 'else':
         self.tl.pop()
         xFalse = self.ifthen()
         ret = If3(cond, xTrue, xFalse)
         self.unify(xTrue.type, xFalse.type)
     else:
         ret = If2(cond, xTrue)
         self.unify(xTrue.type, types.Unit())
     return ret
Exemplo n.º 5
0
define %Char @char(%voidptr, %Int %code)
{
    %c = trunc %Int %code to %Char
    ret %Char %c
}
''')

reg('char_to_int', types.Arrow(types.Char, types.Int), '''
define %Int @char_to_int(%voidptr, %Char %ch)
{
    %i = zext %Char %ch to %Int
    ret %Int %i
}
''')

reg('ml_putchar', types.Arrow(types.Char, types.Unit()), '''
declare i32 @putchar(i32)
define %Unit @ml_putchar(%voidptr, %Char %c)
{
    %int_c = zext %Char %c to i32
    call i32 @putchar(i32 %int_c)
    ret UNIT_VALUE
}
''')

reg('ml_getchar', types.Arrow(types.Unit(), types.Int), '''
declare i32 @getchar()
define %Int @ml_getchar(%voidptr, %Unit)
{
    %c32 = call i32 @getchar()
    %c64 = sext i32 %c32 to %Int
Exemplo n.º 6
0
 def __init__(self):
     self.type = types.Unit()
Exemplo n.º 7
0
 def __init__(self, cond, expr):
     self.cond = cond
     self.expr = expr
     self.type = types.Unit()