Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: closures #148

Merged
merged 18 commits into from
Oct 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions quartz/ast.qz
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,11 @@ enum Expression {
}?,
expansion: LExpression?,
},
t_closure_call: struct {
callee_type: Type,
callee: LExpression,
args: vec[LExpression],
},
t_project: struct {
expr: LExpression,
field: Ident,
Expand Down Expand Up @@ -324,6 +329,13 @@ enum Expression {
expr: LExpression,
expr_type: Type,
},
t_closure: struct {
func: Function,
captures: vec[struct {
name: string,
type_: Type,
}],
},
}

module Expression {
Expand Down Expand Up @@ -382,6 +394,10 @@ enum Type {
params: vec[Type],
result: Type,
},
t_closure: struct {
params: vec[Type],
result: Type,
},
t_variadic_func: struct {
params: vec[Type],
result: Type,
Expand Down Expand Up @@ -497,6 +513,20 @@ module Type {
return builder.to_string();
} else if self.t_ptr != nil {
return "ptr[{}]".format(self.t_ptr!.to_string());
} else if self.t_closure != nil {
let builder = stringbuilder::new();
builder.append("closure (");
for i in 0..self.t_closure!.params.length {
let param = self.t_closure!.params.at(i);
builder.append(param.to_string());

if i != self.t_closure!.params.length - 1 {
builder.append(", ");
}
}
builder.append(format(") -> {}", self.t_closure!.result.to_string()));

return builder.to_string();
}

panic("Unknown type: {}", derive::to_string(self));
Expand Down
2 changes: 2 additions & 0 deletions quartz/compiler.qz
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import quartz::value;
import quartz::location;
import quartz::preprocessor;
import quartz::ir_path::let_call;
import quartz::ir_path::escaping;

struct LoadedModule {
path: Path,
Expand Down Expand Up @@ -193,6 +194,7 @@ module Compiler {
let term = irgen.run(module_).context("ircodegen").try;

term = transform_let_call(term);
term = transform_escaping(term);

let envs = environs();
if envs.has("GENERATE_IR") {
Expand Down
1 change: 1 addition & 0 deletions quartz/generator.qz
Original file line number Diff line number Diff line change
Expand Up @@ -625,6 +625,7 @@ module Generator {
}]](),
result_type: self.main_signature_result!,
body: start_body,
escaping: make[vec[string]](),
..nil,
});

Expand Down
14 changes: 14 additions & 0 deletions quartz/ir.qz
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ struct IrFunc {
result_type: IrType,
body: vec[IrTerm],
ffi_export: string?,
escaping: vec[string],
}

struct IrCall {
Expand Down Expand Up @@ -329,6 +330,19 @@ module IrType {
},
};
}
if t.t_closure != nil {
let params = make[vec[IrType]]();
for param in t.t_closure!.params {
params.push(IrType::new(param));
}

return IrType {
t_func: struct {
params: params,
result_type: IrType::new(t.t_closure!.result),
},
};
}

return panic("unknown type: {} (IrType::new)", t.to_string());
}
Expand Down
179 changes: 177 additions & 2 deletions quartz/ir_code_gen.qz
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,16 @@ module FunctionTable {

return (self.functions.length - 1) as i32;
}

fun has(self, key: string): bool {
for i in 0..self.functions.length {
if self.functions.at(i).equal(key) {
return true;
}
}

return false;
}
}

struct InterfaceTable {
Expand Down Expand Up @@ -291,6 +301,15 @@ struct IrCodeGenerator {
test_mode: bool,
test_functions: vec[string],
entrypoint: string,
closures: map[string, struct {
closure: IrTerm,
captures: vec[Type],
}],
captures: vec[struct {
name: string,
type_: Type,
}],
escaping: vec[string],
}

module IrCodeGenerator {
Expand All @@ -314,6 +333,15 @@ module IrCodeGenerator {
test_mode: test_mode,
test_functions: make[vec[string]](),
entrypoint: entrypoint,
closures: make[map[string, struct {
closure: IrTerm,
captures: vec[Type],
}]](),
captures: make[vec[struct {
name: string,
type_: Type,
}]](),
escaping: make[vec[string]](),
};
}

Expand Down Expand Up @@ -477,6 +505,7 @@ module IrCodeGenerator {
},
},
}),
escaping: make[vec[string]](),
..nil,
},
});
Expand Down Expand Up @@ -523,6 +552,7 @@ module IrCodeGenerator {
t_address: true,
},
body: body,
escaping: make[vec[string]](),
..nil,
},
});
Expand Down Expand Up @@ -598,7 +628,10 @@ module IrCodeGenerator {
}

elements.push(IrTerm {
t_func: self.function(d.t_func!).try,
t_func: self.function(d.t_func!, make[vec[struct {
name: string,
type_: Type,
}]]()).try,
});
}
if d.t_let != nil {
Expand Down Expand Up @@ -628,6 +661,16 @@ module IrCodeGenerator {
if d.t_interface != nil {
continue;
}
if self.closures.list_keys().length > 0 {
for cname in self.closures.list_keys() {
elements.push(self.closures.(cname).closure);
}

self.closures = make[map[string, struct {
closure: IrTerm,
captures: vec[Type],
}]]();
}
}

return elements;
Expand Down Expand Up @@ -881,6 +924,7 @@ module IrCodeGenerator {
t_nil: true,
},
body: body,
escaping: make[vec[string]](),
..nil,
},
};
Expand Down Expand Up @@ -997,6 +1041,7 @@ module IrCodeGenerator {
result_type: IrType {
t_nil: true,
},
escaping: make[vec[string]](),
..nil,
},
});
Expand Down Expand Up @@ -1146,6 +1191,7 @@ module IrCodeGenerator {
result_type: IrType {
t_nil: true,
},
escaping: make[vec[string]](),
..nil,
},
});
Expand All @@ -1164,9 +1210,16 @@ module IrCodeGenerator {
};
}

fun function(self, func: Function): IrFunc or error {
fun function(self, func: Function, captures: vec[struct {
name: string,
type_: Type,
}]): IrFunc or error {
self.current_function_name = func.name.data;
self.function_no_allocation = func.no_allocation;
self.captures = captures;

let prev_escaping = self.escaping;
self.escaping = make[vec[string]]();

let body = make[vec[IrTerm]]();
if !self.path_to(func.name.data).starts_with("quartz_core") {
Expand Down Expand Up @@ -1204,12 +1257,16 @@ module IrCodeGenerator {
});
}

let escaping_this = self.escaping;
self.escaping = prev_escaping;

return IrFunc {
name: self.path_to(func.name.data),
params: params,
result_type: IrType::new(func.result_type),
body: body,
ffi_export: func.ffi_export,
escaping: escaping_this,
};
}

Expand Down Expand Up @@ -1881,6 +1938,25 @@ module IrCodeGenerator {
t_ident: expr.t_ident!.resolved_path!.join("_"),
};
}
for i in 0..self.captures.length {
let c = self.captures.(i);
if c.name == expr.t_ident!.name {
return IrTerm {
t_load: struct {
type_: IrType::new(c.type_),
address: IrTerm {
t_ident: "env",
},
offset: IrCodeGenerator::wrap_mult_sizeof(
IrType::new(c.type_),
IrTerm {
t_i32: i,
},
),
},
};
}
}

return IrTerm {
t_ident: expr.t_ident!.name,
Expand Down Expand Up @@ -3022,6 +3098,105 @@ module IrCodeGenerator {
},
};
}
} else if expr.t_closure != nil {
expr.t_closure!.func.params.push(struct {
name: "env",
type_: Type {
t_any: true,
},
});

let captures_types = make[vec[Type]]();
for c in expr.t_closure!.captures {
captures_types.push(c.type_);
self.escaping.push(c.name);
}

self.closures.(expr.t_closure!.func.name.data) = (struct {
closure: IrTerm {
t_func: self.function(expr.t_closure!.func, expr.t_closure!.captures).try,
},
captures: captures_types,
});

let elements = make[vec[IrTerm]]();
let captures_ir = make[vec[IrType]]();
let values = make[vec[struct {
label: string?,
type_: IrType,
term: IrTerm,
}]]();
for c in expr.t_closure!.captures {
captures_ir.push(IrType::new(c.type_));
values.push(struct {
label: nil,
type_: IrType {
t_address: true,
},
term: IrTerm {
t_load: struct {
type_: IrType::new(c.type_),
address: IrTerm {
t_ident: c.name
},
offset: IrTerm {
t_i32: 0,
},
}
},
});
}

elements.push(IrTerm {
t_let: IrLet {
name: "env",
type_: IrType {
t_address: true,
},
value: self.generate_array(
"struct",
captures_ir,
values,
).try,
},
});

let symbol = self.path_to(expr.t_closure!.func.name.data);
let function_id = self.functions.get_or_insert(symbol);

elements.push(IrTerm {
t_i32: function_id,
});

return IrTerm {
t_seq: struct {
terms: elements,
},
};
} else if expr.t_closure_call != nil {
let callee_type = expr.t_closure_call!.callee_type;
callee_type.t_closure!.params.push(
Type {
t_any: true,
},
);

let terms = make[vec[IrTerm]]();
for arg in expr.t_closure_call!.args {
terms.push(self.expression(arg).try);
}

terms.push(IrTerm {
t_ident: "env",
});

return IrTerm {
t_dynamic_call: struct {
callee_type: IrType::new(callee_type),
callee_id: self.expression(expr.t_closure_call!.callee).try,
args: terms,
},
};
}

return panic("expression exhausted: {}", expr.to_string());
Expand Down
Loading