mirror of
https://github.com/c3lang/c3c.git
synced 2026-02-27 12:01:16 +00:00
Add lambdas.
This commit is contained in:
committed by
Christoffer Lerno
parent
c9e1e2d763
commit
b508a43f8f
45
test/test_suite/lambda/lambda_in_macro.c3t
Normal file
45
test/test_suite/lambda/lambda_in_macro.c3t
Normal file
@@ -0,0 +1,45 @@
|
||||
// #target: macos-x64
|
||||
module test;
|
||||
import std::io;
|
||||
|
||||
define Callback1 = fn double(double x);
|
||||
define Callback2 = fn int(int y);
|
||||
|
||||
macro test2(y)
|
||||
{
|
||||
var $Type = $typeof(y);
|
||||
return fn $Type($typeof(y) x) { return x * x; };
|
||||
}
|
||||
|
||||
macro test($Type)
|
||||
{
|
||||
$Type z = fn (x) { return x * x; };
|
||||
return z;
|
||||
}
|
||||
|
||||
fn void main()
|
||||
{
|
||||
io::printfn("val: %d", test(Callback2)(3));
|
||||
io::printfn("val: %s", test(Callback1)(3.3));
|
||||
io::printfn("val: %s", test(Callback1)(3.3));
|
||||
io::printfn("val: %d", test2(1)(3));
|
||||
io::printfn("val: %d", test2(1)(3));
|
||||
io::printfn("val: %s", test2(1.0)(3.3));
|
||||
}
|
||||
|
||||
/* #expect: test.ll
|
||||
|
||||
store ptr @"test_test$lambda1", ptr %z, align 8
|
||||
%1 = call i32 %0(i32 3)
|
||||
store ptr @"test_test$lambda2", ptr %z3, align 8
|
||||
%7 = call double %6(double 3.300000e+00)
|
||||
store ptr @"test_test$lambda2", ptr %z10, align 8
|
||||
%13 = call double %12(double 3.300000e+00)
|
||||
%18 = call i32 @"test_test2$lambda3"(i32 3)
|
||||
%23 = call i32 @"test_test2$lambda3"(i32 3)
|
||||
%28 = call double @"test_test2$lambda4"(double 3.300000e+00)
|
||||
|
||||
define internal double @"test_test2$lambda4"(double %0) #0 {
|
||||
define internal i32 @"test_test2$lambda3"(i32 %0) #0 {
|
||||
define internal double @"test_test$lambda2"(double %0) #0 {
|
||||
define internal i32 @"test_test$lambda1"(i32 %0) #0 {
|
||||
81
test/test_suite/lambda/nested_lambda_def.c3t
Normal file
81
test/test_suite/lambda/nested_lambda_def.c3t
Normal 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"()
|
||||
|
||||
29
test/test_suite/lambda/simple_lambda.c3t
Normal file
29
test/test_suite/lambda/simple_lambda.c3t
Normal file
@@ -0,0 +1,29 @@
|
||||
// #target: macos-x64
|
||||
import std::io;
|
||||
import std::math::vector;
|
||||
import std::math;
|
||||
|
||||
define Callback = fn int();
|
||||
|
||||
fn int xy(Callback a) => a();
|
||||
|
||||
fn void main()
|
||||
{
|
||||
const Callback F = fn int() { io::printfn("Hello"); return 23; };
|
||||
int z = xy(F);
|
||||
io::printfn("%d", z);
|
||||
int y = xy(fn () { return 3; });
|
||||
}
|
||||
|
||||
/* #expect: simple_lambda.ll
|
||||
|
||||
define void @simple_lambda_main() #0 {
|
||||
entry:
|
||||
%F = alloca ptr, align 8
|
||||
%z = alloca i32, align 4
|
||||
store ptr @"simple_lambda_main$lambda1", ptr %F, align 8
|
||||
%0 = call i32 @simple_lambda_xy(ptr @"simple_lambda_main$lambda1")
|
||||
%5 = call i32 @simple_lambda_xy(ptr @"simple_lambda_main$lambda2")
|
||||
|
||||
define internal i32 @"simple_lambda_main$lambda2"() #0 {
|
||||
define internal i32 @"simple_lambda_main$lambda1"() #0 {
|
||||
Reference in New Issue
Block a user