Exemple #1
0
 def tuple(module, elts):
     if not elts:
         return ir.Expr(expr=ir.Const(()), type=types.unit_t)
     if len(elts) is 1:
         ret = module.eval(elts[0])
         return ret
     level = tc_state.push_level()
     elts = list(map(module.eval_with_implicit, elts))
     type = te.Tuple(tuple(e.type for e in elts))
     loc = module.clc.location
     filename = module.clc.filename
     type = generalise_type(tc_state,
                            type,
                            loc=loc,
                            filename=filename,
                            level=level)
     return ir.Expr(expr=ir.Tuple(elts), type=type)
Exemple #2
0
        def type(module, typename, typedef):
            clc = module.clc
            path = clc.path
            loc, typename = sexpr.unloc(typename)
            qual_typename = '{}.{}'.format(path, typename)
            sym_typename = clc.scope.enter(typename)
            if typedef:
                # type alias
                nom_type = tc_state.infer(Typing(clc).eval(typedef))
            else:
                nom_type = types.Nom(qual_typename,
                                     loc=loc,
                                     filename=clc.filename)
            tenv[sym_typename] = te.App(types.type_type, nom_type)

            return wrap_loc(
                loc,
                ignore(
                    ir.Set(sym_typename,
                           ir.Expr(type=nom_type, expr=ir.Const(nom_type)))))
Exemple #3
0
}


def ignore(ex: ir.BaseExpr):
    return ir.Expr(type=types.unit_t, expr=ex)


def anyway(x: ir.BaseExpr):
    return ir.Expr(expr=x, type=types.anyway_type)


def wrap_loc(loc: Location, expr: ir.Expr):
    return ir.Expr(type=expr.type, expr=ir.WrapLoc(loc, expr))


unit = ir.Expr(type=types.unit_t, expr=ir.Const(()))


## Compiler contexts
class CompilerGlobalContext:
    tc_state: TCState
    tenv: typing.Dict[Sym, te.T]
    backend: 'BackEnd'

    def __init__(self, tc_state: TCState, tenv: typing.Dict[Sym, te.T],
                 backend: 'BackEnd'):
        self.tc_state = tc_state
        self.tenv = tenv
        self.backend = backend

    @classmethod
Exemple #4
0
 def literal(module, val):
     my_type = type_map[type(val)]
     return ir.Expr(type=my_type, expr=ir.Const(val))
Exemple #5
0
 def type(module, name, definition):
     assert name is None
     t = Typing(module.clc).eval(definition)
     return ir.Expr(expr=ir.Const(t), type=te.App(types.type_type, t))
Exemple #6
0
        def module(module_eval, is_rec, name, stmts, loc=None):
            loc, name = sexpr.unloc(name)
            clc = module_eval.clc
            scope = clc.scope
            filename = clc.filename
            path = clc.path = '.'.join(
                (*filter(None, clc.path.split('.')), name))

            # only for recursive modules.
            sym_mod: typing.Optional[Sym] = None

            # generated ir outside module;
            # only for recursive modules.
            outer_stmts: typing.List[ir.Expr] = []
            # generated ir for module itself
            inner_stmts: typing.List[ir.Expr] = []

            # type of module. will be a non-extensible record.
            type_mod = None

            # recursive module.
            if is_rec:
                sym_mod = scope.enter(name)
                new_var = tc_state.new_var(Var=types.Var,
                                           loc=loc,
                                           filename=filename,
                                           name=path)
                type_mod = tenv[sym_mod] = new_var
                outer_stmts.append(
                    ignore(ir.Set(sym_mod, ignore(ir.Const(())))))

            # for preprocessing
            # 1. recursive functions and
            # 2. type definitions.
            filter_stmts = []
            for stmt in stmts:
                _, stmt_unloc = sexpr.unloc(stmt)

                # type definition
                if stmt_unloc[0] is sexpr.type_k:
                    inner_stmts.append(module_eval.eval(stmt))
                    continue

                filter_stmts.append(stmt)
                if stmt_unloc[0] is sexpr.def_k:
                    if stmt_unloc[1]:
                        # Recursive definition. It's guaranteed to
                        # be a function in syntax level.
                        loc, n = sexpr.unloc(stmt_unloc[2])
                        sym = scope.enter(n)
                        tenv[sym] = tc_state.new_var(Var=types.Var,
                                                     loc=loc,
                                                     filename=filename,
                                                     name=n)

            for stmt in filter_stmts:
                inner_stmts.append(module_eval.eval(stmt))

            syms = [
                sym for sym in scope.get_newest_bounds() if sym is not sym_mod
            ]

            row_mod = te.row_from_list([(s.name, tenv[s]) for s in syms],
                                       te.empty_row)
            if type_mod:
                # recursive module
                tc_state.unify(type_mod, te.Record(row_mod))
            else:
                type_mod = te.Record(row_mod)

            elts_mod = [ir.Expr(type=tenv[sym], expr=sym) for sym in syms]
            elts_mod.sort(key=_sort_sym)

            val_mod = ir.Expr(type=type_mod,
                              expr=ir.Tuple([
                                  anyway(ir.Tuple(elts_mod)),
                                  anyway(ir.Tuple([]))
                              ]))
            inner_stmts.append(val_mod)

            exp_mod = ir.Expr(type=type_mod, expr=ir.Block(inner_stmts))

            if not is_rec:
                return exp_mod

            outer_stmts.append(ignore(ir.Set(sym_mod, exp_mod)))
            outer_stmts.append(ir.Expr(type=type_mod, expr=sym_mod))
            return ir.Expr(type=type_mod, expr=ir.Block(outer_stmts))
Exemple #7
0
 def tellme(self, name, _):
     sym = self.clc.scope.require(name)
     backend.types_to_show.append(
         (self.clc.location, self.clc.filename, sym))
     return ignore(ir.Const(()))