Skip to content
Merged
Changes from 1 commit
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
9fbd359
initial stub implementation of match statements
arihant2math Jan 19, 2025
bfb4091
formatting
arihant2math Jan 19, 2025
c021326
solved compilation errors
arihant2math Jan 20, 2025
6c7a543
almost working
arihant2math Jan 20, 2025
8617786
formatting
arihant2math Jan 20, 2025
a01240f
as and * support for switch case
arihant2math Jan 20, 2025
d8c4cff
replace todos with errors
arihant2math Jan 20, 2025
aaccadb
fix compile
arihant2math Jan 20, 2025
f9d2418
added Noop instruction
arihant2math Jan 20, 2025
5839dfd
finished default for switch case
arihant2math Jan 20, 2025
40e380e
fix compile
arihant2math Jan 20, 2025
8af2c42
formatting
arihant2math Jan 20, 2025
0774cbd
more implementation
arihant2math Jan 20, 2025
cd0f1cf
rename codegen_* to compile_*
arihant2math Jan 21, 2025
223fbe4
implement SWAP instruction
arihant2math Jan 21, 2025
4ef2a50
applied fix
youknowone Jan 21, 2025
aa6a040
enabled IR debug and attempted to fix codegen
arihant2math Jan 22, 2025
822d565
formatting
arihant2math Jan 22, 2025
ad164c0
basic match statement working
arihant2math Jan 22, 2025
e955ede
basic match statement working
arihant2math Jan 22, 2025
4649898
working as pattern (default works too by extension)
arihant2math Jan 23, 2025
493fc6e
fix rust tests
arihant2math Jan 23, 2025
fe7f8a3
cleanup
arihant2math Jan 23, 2025
805757a
fix clippy errors
arihant2math Jan 23, 2025
b176f9c
formatting
arihant2math Jan 23, 2025
8e78c6d
trivial edits
youknowone Jan 25, 2025
4beef2c
Remove currently unused Nop,Swap op
youknowone Jan 25, 2025
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
Prev Previous commit
Next Next commit
more implementation
  • Loading branch information
arihant2math committed Jan 22, 2025
commit 0774cbdd536b68e9b96c3cbac4c75bbbf6180d89
32 changes: 24 additions & 8 deletions compiler/codegen/src/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1904,6 +1904,20 @@ impl Compiler {
Ok(())
}

fn codegen_pattern_helper_rotate(
&mut self,
count: usize,
) -> CompileResult<()> {
let mut count = count;
while 1 < count {
todo!("below");
// ADDOP_I(c, loc, SWAP, count--);
// emit!(self, Instruction::Swap { count: count as u32 });
count -= 1;
}
Ok(())
}

// static int
// codegen_pattern_helper_store_name(compiler *c, location loc,
// identifier n, pattern_context *pc)
Expand All @@ -1926,7 +1940,6 @@ impl Compiler {
// }
fn codegen_pattern_helper_store_name(
&mut self,
loc: SourceLocation,
n: Option<&str>,
pattern_context: &mut PatternContext,
) -> CompileResult<()> {
Expand All @@ -1940,8 +1953,7 @@ impl Compiler {
return Err(self.error(CodegenErrorType::DuplicateStore(n.to_string())));
}
let rotations = pattern_context.on_top + pattern_context.stores.len() + 1;
todo!("below");
// self.codegen_pattern_helper_rotate(loc, rotations)?;
self.codegen_pattern_helper_rotate(rotations)?;
pattern_context.stores.push(n.to_string());
Ok(())
}
Expand All @@ -1953,7 +1965,6 @@ impl Compiler {
) -> CompileResult<()> {
// codegen_pattern_helper_store_name(c, LOC(p), p->v.MatchStar.name, pc));
self.codegen_pattern_helper_store_name(
star.location(),
star.name.as_deref(),
pattern_context,
)?;
Expand Down Expand Up @@ -1993,14 +2004,14 @@ impl Compiler {
if as_pattern.pattern.is_none() {
// An irrefutable match:
if !pattern_context.allow_irrefutable {
// TODO: better error message
if let Some(name) = &as_pattern.name {
return Err(self.error(CodegenErrorType::InvalidMatchCase));
} else {
return Err(self.error(CodegenErrorType::InvalidMatchCase));
}
}
return self.codegen_pattern_helper_store_name(
as_pattern.location(),
as_pattern.name.as_deref(),
pattern_context,
);
Expand All @@ -2010,7 +2021,6 @@ impl Compiler {
self.codegen_pattern(as_pattern.pattern.as_ref().unwrap(), pattern_context)?;
pattern_context.on_top -= 1;
self.codegen_pattern_helper_store_name(
as_pattern.location(),
as_pattern.name.as_deref(),
pattern_context,
)?;
Expand Down Expand Up @@ -2045,8 +2055,9 @@ impl Compiler {
pattern_context: &mut PatternContext,
) -> CompileResult<()> {
self.compile_expression(subject)?;
// Block at the end of the switch statement that we jump to after finishing a branch
let end_block = self.new_block();
// Blocks at the end of the switch statement that we jump to after finishing a branch
// TODO: optimize, we can reuse the same block for all cases
let mut end_block = self.new_block();

let match_case_type = cases.last().expect("cases is not empty");
let has_default = match_case_type.pattern.is_match_star() && 1 < cases.len();
Expand Down Expand Up @@ -2105,6 +2116,7 @@ impl Compiler {
}
self.compile_statements(&m.body)?;
}

self.switch_to_block(end_block);
Ok(())
}
Expand Down Expand Up @@ -3401,6 +3413,10 @@ impl Compiler {
ir::BlockIdx::NULL,
"switching {prev:?} -> {block:?} to completed block"
);
println!("{}", prev.0);
for (count, b) in code.blocks.iter().enumerate() {
println!("{count}: {} {}", b.next.0, b.instructions.len());
}
let prev_block = &mut code.blocks[prev.0 as usize];
assert_eq!(
prev_block.next.0,
Expand Down