Skip to content

Commit 41a2dfa

Browse files
authored
Merge pull request #109 from matt-cornell/type-rework
Rework type system
2 parents 4ac5cb7 + 49228f3 commit 41a2dfa

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+10586
-8828
lines changed

cobalt-ast/Cargo.toml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,10 @@ either = "1.8.1"
1717
glob = "0.3.1"
1818
owned_chars = "0.3.2"
1919
bstr = { version = "1.5.0", default-features = false, features = ["std"] }
20+
derive_more = "0.99.17"
21+
ref-cast = "1.0.20"
22+
slab = "0.4.8"
23+
inventory = "0.3.11"
24+
once_cell = "1.18.0"
25+
hashbrown = "0.14.0"
26+
flurry = "0.4.0"

cobalt-ast/src/ast.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,21 @@ pub trait AST<'src>: ASTClone<'src> + std::fmt::Debug {
5353
fn fwddef_prepass(&self, _ctx: &CompCtx<'src, '_>) {} // create forward definitions for functions in LLVM
5454

5555
// code generation
56-
fn codegen<'ctx>(
56+
/// Implementation point for AST code generation
57+
fn codegen_impl<'ctx>(
5758
&self,
5859
ctx: &CompCtx<'src, 'ctx>,
5960
) -> (Value<'src, 'ctx>, Vec<CobaltError<'src>>);
61+
/// Generate code
62+
fn codegen<'ctx>(
63+
&self,
64+
ctx: &CompCtx<'src, 'ctx>,
65+
) -> (Value<'src, 'ctx>, Vec<CobaltError<'src>>) {
66+
let (mut val, errs) = self.codegen_impl(ctx);
67+
val.loc = self.loc();
68+
(val, errs)
69+
}
70+
/// Generate code in a constant context
6071
fn const_codegen<'ctx>(
6172
&self,
6273
ctx: &CompCtx<'src, 'ctx>,

cobalt-ast/src/ast/flow.rs

Lines changed: 33 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ impl<'src> AST<'src> for IfAST<'src> {
2828
fn nodes(&self) -> usize {
2929
self.cond.nodes() + self.if_true.nodes() + self.if_false.nodes() + 1
3030
}
31-
fn codegen<'ctx>(
31+
fn codegen_impl<'ctx>(
3232
&self,
3333
ctx: &CompCtx<'src, 'ctx>,
3434
) -> (Value<'src, 'ctx>, Vec<CobaltError<'src>>) {
@@ -38,19 +38,15 @@ impl<'src> AST<'src> for IfAST<'src> {
3838
let mut errs = vec![];
3939
let (cond, mut es) = self.cond.codegen(ctx);
4040
errs.append(&mut es);
41-
let cv = ops::expl_convert(
42-
self.cond.loc(),
43-
(cond, None),
44-
(Type::Int(1, false), None),
45-
ctx,
46-
)
47-
.unwrap_or_else(|e| {
48-
errs.push(e);
49-
Value::compiled(
50-
ctx.context.bool_type().const_int(0, false).into(),
51-
Type::Int(1, false),
52-
)
53-
});
41+
let cv = cond
42+
.impl_convert((types::Int::bool(), None), ctx)
43+
.unwrap_or_else(|e| {
44+
errs.push(e);
45+
Value::compiled(
46+
ctx.context.bool_type().const_int(0, false).into(),
47+
types::Int::bool(),
48+
)
49+
});
5450
if let Some(inkwell::values::BasicValueEnum::IntValue(v)) = cv.value(ctx) {
5551
(
5652
{
@@ -67,31 +63,20 @@ impl<'src> AST<'src> for IfAST<'src> {
6763
let if_true = self.if_true.codegen_errs(ctx, &mut errs);
6864
ctx.builder.position_at_end(ifb);
6965
let if_false = self.if_false.codegen_errs(ctx, &mut errs);
70-
if let Some(ty) = ops::common(&if_true.data_type, &if_false.data_type, ctx)
71-
{
66+
if let Some(ty) = if_true.data_type.common(if_false.data_type, ctx) {
7267
ctx.builder.position_at_end(itb);
73-
let if_true = ops::impl_convert(
74-
self.if_true.loc(),
75-
(if_true, None),
76-
(ty.clone(), None),
77-
ctx,
78-
)
79-
.unwrap_or_else(|e| {
80-
errs.push(e);
81-
Value::error()
82-
});
68+
let if_true =
69+
if_true.impl_convert((ty, None), ctx).unwrap_or_else(|e| {
70+
errs.push(e);
71+
Value::error().with_loc(self.if_true.loc())
72+
});
8373
ctx.builder.build_unconditional_branch(mb);
8474
ctx.builder.position_at_end(ifb);
85-
let if_false = ops::impl_convert(
86-
self.if_false.loc(),
87-
(if_false, None),
88-
(ty.clone(), None),
89-
ctx,
90-
)
91-
.unwrap_or_else(|e| {
92-
errs.push(e);
93-
Value::error()
94-
});
75+
let if_false =
76+
if_false.impl_convert((ty, None), ctx).unwrap_or_else(|e| {
77+
errs.push(e);
78+
Value::error().with_loc(self.if_false.loc())
79+
});
9580
ctx.builder.build_unconditional_branch(mb);
9681
ctx.builder.position_at_end(mb);
9782
Value::new(
@@ -166,7 +151,7 @@ impl<'src> AST<'src> for WhileAST<'src> {
166151
fn nodes(&self) -> usize {
167152
self.cond.nodes() + self.body.nodes() + 1
168153
}
169-
fn codegen<'ctx>(
154+
fn codegen_impl<'ctx>(
170155
&self,
171156
ctx: &CompCtx<'src, 'ctx>,
172157
) -> (Value<'src, 'ctx>, Vec<CobaltError<'src>>) {
@@ -184,17 +169,17 @@ impl<'src> AST<'src> for WhileAST<'src> {
184169
ctx.builder.build_unconditional_branch(cond);
185170
ctx.builder.position_at_end(cond);
186171
let (c, mut errs) = self.cond.codegen(ctx);
187-
let val =
188-
ops::expl_convert(self.cond.loc(), (c, None), (Type::Int(1, false), None), ctx)
189-
.unwrap_or_else(|e| {
190-
errs.push(e);
191-
Value::compiled(
192-
ctx.context.bool_type().const_int(0, false).into(),
193-
Type::Int(1, false),
194-
)
195-
})
196-
.into_value(ctx)
197-
.unwrap_or(ctx.context.bool_type().const_int(0, false).into());
172+
let val = c
173+
.expl_convert((types::Int::bool(), None), ctx)
174+
.unwrap_or_else(|e| {
175+
errs.push(e);
176+
Value::compiled(
177+
ctx.context.bool_type().const_int(0, false).into(),
178+
types::Int::bool(),
179+
)
180+
})
181+
.value(ctx)
182+
.unwrap_or(ctx.context.bool_type().const_int(0, false).into());
198183
ctx.builder
199184
.build_conditional_branch(val.into_int_value(), body, exit);
200185
ctx.builder.position_at_end(body);

0 commit comments

Comments
 (0)