def make_union_constructor(builder: AstBuilder): union_call = union_test_make(builder) union_str = to_string(union_call) print('-' * 80) union_constructor = keval(union_call, ctx) print('-' * 80) result_str = to_string(union_constructor) print('\n IN > {} \nOUT > {}'.format(union_str, result_str)) return union_constructor
def make_struct_constructor(builder: AstBuilder): struct_call = struct_test_make(builder) struct_str = to_string(struct_call) print('-' * 80) struct_constructor = keval(struct_call, ctx) print('-' * 80) result_str = to_string(struct_constructor) print('\n IN > {} \nOUT > {}'.format(struct_str, result_str)) return struct_constructor
def main(): import sys sys.stderr = sys.stdout from kiwi.print import to_string from kiwi.builder import AstBuilder from kiwi.builtin import make_scope ctx = make_scope() builder = AstBuilder(ctx) float_type = builder.reference('Float') # Make the Add Function fun = builder.function() fun.args([('x', None), ('y', None)]) fun.return_type = None body = fun.unary_operator( fun.reference('return'), fun.binary_operator(fun.reference('+'), fun.reference('x'), fun.reference('y'))) fun.body(body) fun = fun.make() builder.bind('add', fun) # Done # Make a Call to `add` add_fun = builder.reference('add') two = builder.call( add_fun, [builder.value(1, float_type), builder.value(2, float_type)]) # Done # ctx.dump() print('-' * 80) no_types = to_string(fun, ctx) print('-' * 80) result = type_trace(two, ctx) #result = type_trace(fun, ctx) print('-' * 80) fun_str = to_string(fun, ctx) print('-' * 80) print(' User Input: ', no_types) print('Deduced Types: ', fun_str) print(' Result: ', result[1])
def main(): import sys sys.stderr = sys.stdout from kiwi.print import to_string from kiwi.type.deduce import type_deduce ctx = make_scope() builder = AstBuilder(ctx) fun = function_test_add_def(builder) type_deduce(fun, ctx) print(to_string(fun, ctx)) print(to_string(fun, ctx)) print(to_string(fun.type, ctx))
def main(): import sys sys.stderr = sys.stdout from kiwi.print import to_string from kiwi.builder import AstBuilder from kiwi.builtin import make_scope from kiwi.test_add import function_test_add_def ctx = make_scope() builder = AstBuilder(ctx) float_type = builder.reference('Float') bind = function_test_add_def(builder, return_bind=True) two = builder.call( bind.expr, [builder.value(1, float_type), builder.value(2, float_type)]) original = to_string(bind) print('-' * 80) type_trace(two, ctx) print('-' * 80) new = to_string(bind) print('-' * 80) ftype = to_string(bind.expr.type) print('-' * 80) print(original) print('\nAfter Inference:') print('-----------------') print(new) print() print('Fun Type: {}'.format(ftype)) print('-' * 80) module = ir.Module(name='test') r = llvm_codegen(module, ctx, bind) print('-' * 80) print(r) print('-' * 80) print(module) print('-' * 80)
def show_result(expr, ctx): type_trace(expr, ctx) print() val = keval(expr, ctx) print() val_str = to_string(val, ctx) print(val_str) print()
def match(self, match: Match, depth=0) -> Any: trace(depth, 'match') target, ttype = self.visit(match.target, depth + 1) return_type = None for pattern, branch in match.branches: self.scope = self.scope.enter_scope('match_branch_scope') if isinstance(pattern, ExpressionPattern): self.type_hint = ttype pat, pat_type = self.visit(pattern.expr, depth + 1) self.type_hint = None trace_assert( depth, ktype_equiv(pat_type, ttype, self.scope), 'Pattern must have the same type as the target {} != {}'. format(to_string(pat_type), to_string(ttype))) elif isinstance(pattern, ConstructorPattern): target_type, type = self.visit(ttype, depth + 1) assert isinstance(target_type, (Union, Struct)) if pattern.name is not None: # Name should match the struct type if isinstance(pattern.name, Expression): type, type_type = self.visit(pattern.name, depth + 1) #print('HERE1', pattern.name) # Or a member of the union elif isinstance(pattern.name, str): union_member = None for member in target_type.members: if member.name == pattern.name: union_member = member for pat in pattern.args: if isinstance(pat, NamePattern): self.scope.insert_binding( pat.name, Variable(pat.name, union_member.type)) self.type_hint = return_type rbranch, new_return = self.visit(branch, depth + 1) self.type_hint = None if return_type is not None: trace_assert( depth, ktype_equiv(new_return, return_type, self.scope), '/!\\ [TYPE MISMATCH]: Function cannot return two different types `{}` and `{}`' .format(to_string(new_return, self.scope), to_string(return_type, self.scope))) return_type = new_return self.scope = self.scope.exit_scope() if match.default is not None: self.type_hint = return_type dbranch, dtype = self.visit(match.default, depth + 1) self.type_hint = None trace_assert(depth, ktype_equiv(dtype, return_type, self.scope), 'type mismatch') return match, return_type