Add lambdas.

This commit is contained in:
Christoffer Lerno
2023-01-23 00:41:05 +01:00
committed by Christoffer Lerno
parent c9e1e2d763
commit b508a43f8f
47 changed files with 1116 additions and 487 deletions

View File

@@ -0,0 +1,81 @@
// #target: macos-x64
module abc;
import bar;
import std::io;
fn int xy(Callback a) => a();
fn void main()
{
const Callback F = bar::get_callback();
int z = xy(F);
z = xy(F);
io::printfn("val: %d", z);
}
module foo;
import bar;
import std::io;
private int xz = 0;
macro Callback get_callback()
{
return fn int() { return bar::get_callback2()(); };
}
macro Callback get_callback2()
{
return fn int() { io::printfn("Hello"); return ++xz; };
}
module bar;
import foo;
define Callback = fn int();
macro Callback get_callback()
{
return fn int() { return foo::get_callback()(); };
}
macro Callback get_callback2()
{
return fn int() { return foo::get_callback2()(); };
}
/* #expect: abc.ll
store ptr @"bar_get_callback$lambda1", ptr %F, align 8
%0 = call i32 @abc_xy(ptr @"bar_get_callback$lambda1")
%1 = call i32 @abc_xy(ptr @"bar_get_callback$lambda1")
declare i32 @"bar_get_callback$lambda1"()
// #expect: foo.ll
define i32 @"foo_get_callback$lambda1"() #0 {
%0 = call i32 @"bar_get_callback2$lambda2"()
define i32 @"foo_get_callback2$lambda2"() #0 {
%1 = load i32, ptr @foo_xz, align 4
declare i32 @"bar_get_callback2$lambda2"()
// #expect: bar.ll
define i32 @"bar_get_callback$lambda1"() #0 {
entry:
%0 = call i32 @"foo_get_callback$lambda1"()
ret i32 %0
}
define i32 @"bar_get_callback2$lambda2"() #0 {
entry:
%0 = call i32 @"foo_get_callback2$lambda2"()
ret i32 %0
}
declare i32 @"foo_get_callback$lambda1"()
declare i32 @"foo_get_callback2$lambda2"()