Files
c3c/test/test_suite/generic/generic_lambda_complex.c3t
Christoffer Lerno 49c4595457 Dynamic protocols.
2023-10-05 15:20:41 +02:00

252 lines
6.2 KiB
C

// #target: macos-x64
/**
* @require Type.kindof == STRUCT
**/
module abc(<Type>);
import std::io;
import std::collections::list;
def TextTagList = List(<TextTag>);
fault TextError
{
UNTERMINATED_TAG,
EMPTY_TAG,
MISSING_TAG,
UNSUPPORTED_TAG,
}
enum TextTagKind: char
{
STRING,
TEMPLATE,
}
struct TextTemplate
{
Allocator* allocator;
String template;
TextTag[] tags;
Type data;
}
struct TextTag
{
usz start;
usz end;
TextTagKind kind;
union {
String* data;
Stream template;
}
}
/**
* @require self.tags.len == 0 "template already initialized"
* @require tag_start != tag_end
**/
fn void! TextTemplate.init(&self, String template, String tag_start = "{{", String tag_end = "}}", Allocator* using = mem::heap())
{
TextTagList tags;
String tmpl = template;
uptr data = (uptr)&self.data;
while (true)
{
usz! start = tmpl.index_of(tag_start);
if (catch start) break;
tmpl = tmpl[start + tag_start.len..];
usz! end = tmpl.index_of(tag_end);
if (catch end) return TextError.UNTERMINATED_TAG?;
String name = tmpl[:end].trim();
if (name == "") return TextError.EMPTY_TAG?;
// Check that the tag exists in the data struct.
TextTag tag = {|
$foreach ($m : Type.membersof)
if (name == $m.nameof)
{
$switch ($m.typeid)
$case String.typeid:
return TextTag{
.kind = STRING,
.data = (String*)(data + $m.offsetof),
};
$default:
$if $checks(self.data.$eval($m.nameof).as_stream()):
return TextTag{
.kind = TEMPLATE,
.template = self.data.$eval($m.nameof).as_stream(),
};
$endif
$endswitch
//return TextError.UNSUPPORTED_TAG?;
}
$endforeach
return TextError.MISSING_TAG?;
|}!;
tmpl = tmpl[end + tag_end.len..];
tag.start = start;
tag.end = start + tag_start.len + end + tag_end.len;
tags.push(tag);
}
*self = { .allocator = using, .template = template, .tags = tags.array_view() };
}
fn void! TextTemplate.free(&self)
{
self.allocator.free(self.tags)!;
*self = {};
}
fn StreamWrapper TextTemplate.as_stream(&self)
{
return { .stream.fns = &texttemplate_interface, .data = self };
}
StreamInterface texttemplate_interface @private = {
.write_stream_fn = fn (s, out) => ((TextTemplate*)(((StreamWrapper*)s).data)).write_to(out),
};
fn usz! TextTemplate.write_to(&self, Stream* writer)
{
usz n;
usz pos;
foreach (tag : self.tags)
{
n += writer.write(self.template[pos:tag.start])!;
pos += tag.end;
n += tag.write(writer)!;
}
n += writer.write(self.template[pos..])!;
return n;
}
fn usz! TextTag.write(&self, Stream* writer)
{
switch (self.kind)
{
case STRING:
return writer.write(*self.data);
case TEMPLATE:
return self.template.write_to(writer);
}
}
module text_test;
import abc;
import std::io;
def Tmpl = TextTemplate(<Data>);
struct Data
{
String user;
String world;
bool ok;
}
def FooTmpl = TextTemplate(<Foo>);
def BarTmpl = TextTemplate(<Bar>);
struct Foo
{
String foo;
BarTmpl* bar;
}
struct Bar
{
String bar;
}
fn void! main()
{
String foo_tmpl = "<<{{foo}} | {{bar}}>>";
FooTmpl ft;
ft.init(foo_tmpl, .using = mem::temp())!;
defer ft.free()!!;
}
/* #expect: text_test.ll
; Function Attrs: nounwind
define i64 @text_test.main() #0 {
entry:
%foo_tmpl = alloca %"char[]", align 8
%ft = alloca %TextTemplate, align 8
%error_var = alloca i64, align 8
%indirectarg = alloca %"char[]", align 8
%error_var1 = alloca i64, align 8
%varargslots = alloca [1 x %"any*"], align 16
%indirectarg5 = alloca %"any*[]", align 8
store %"char[]" { ptr @.str, i64 21 }, ptr %foo_tmpl, align 8
call void @llvm.memset.p0.i64(ptr align 8 %ft, i8 0, i64 64, i1 false)
%0 = getelementptr inbounds %"char[]", ptr %foo_tmpl, i32 0, i32 0
%lo = load ptr, ptr %0, align 8
%1 = getelementptr inbounds %"char[]", ptr %foo_tmpl, i32 0, i32 1
%hi = load i64, ptr %1, align 8
store %"char[]" { ptr @.str.2, i64 2 }, ptr %indirectarg, align 8
%2 = load ptr, ptr @std.core.mem.thread_temp_allocator, align 8
%not = icmp eq ptr %2, null
br i1 %not, label %if.then, label %if.exit
if.then: ; preds = %entry
call void @std.core.mem.init_default_temp_allocators()
br label %if.exit
if.exit: ; preds = %if.then, %entry
%3 = load ptr, ptr @std.core.mem.thread_temp_allocator, align 8
%4 = call i64 @"abc$text_test.Foo$.TextTemplate.init"(ptr %ft, ptr %lo, i64 %hi, ptr @.str.1, i64 2, ptr byval(%"char[]") align 8 %indirectarg, ptr %3)
%not_err = icmp eq i64 %4, 0
%5 = call i1 @llvm.expect.i1(i1 %not_err, i1 true)
br i1 %5, label %after_check, label %assign_optional
assign_optional: ; preds = %if.exit
store i64 %4, ptr %error_var, align 8
br label %guard_block
after_check: ; preds = %if.exit
br label %noerr_block
guard_block: ; preds = %assign_optional
%6 = load i64, ptr %error_var, align 8
ret i64 %6
noerr_block: ; preds = %after_check
%7 = call i64 @"abc$text_test.Foo$.TextTemplate.free"(ptr %ft)
%not_err2 = icmp eq i64 %7, 0
%8 = call i1 @llvm.expect.i1(i1 %not_err2, i1 true)
br i1 %8, label %after_check4, label %assign_optional3
assign_optional3: ; preds = %noerr_block
store i64 %7, ptr %error_var1, align 8
br label %panic_block
after_check4: ; preds = %noerr_block
br label %noerr_block6
panic_block: ; preds = %assign_optional3
%9 = insertvalue %"any*" undef, ptr %error_var1, 0
%10 = insertvalue %"any*" %9, i64 ptrtoint (ptr @"$ct.anyfault" to i64), 1
%11 = getelementptr inbounds [1 x %"any*"], ptr %varargslots, i64 0, i64 0
store %"any*" %10, ptr %11, align 16
%12 = insertvalue %"any*[]" undef, ptr %varargslots, 0
%"#temp#" = insertvalue %"any*[]" %12, i64 1, 1
store %"any*[]" %"#temp#", ptr %indirectarg5, align 8
call void @std.core.builtin.panicf(ptr @.panic_msg, i64 36, ptr @.file, i64 25, ptr @.func, i64 4, i32 173, ptr byval(%"any*[]") align 8 %indirectarg5)
unreachable
noerr_block6: ; preds = %after_check4
ret i64 0
}