Skip to content

Commit

Permalink
allow empty file
Browse files Browse the repository at this point in the history
  • Loading branch information
gfx committed Aug 6, 2024
1 parent bd52cb8 commit d0212b7
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 7 deletions.
Empty file added spec/empty.ts
Empty file.
1 change: 1 addition & 0 deletions spec/empty.ts.stdout
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
15 changes: 8 additions & 7 deletions src/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ impl Compiler {
last_result = Some(ex);
}
Statement::Block(stmts) => {
last_result = Some(self.compile_stmts_or_nop(stmts)?);
last_result = Some(self.compile_stmts_or_none(stmts)?);
}
Statement::If {
cond,
Expand All @@ -372,7 +372,7 @@ impl Compiler {
let Statement::Block(true_branch) = &**true_branch else {
unreachable!()
};
self.compile_stmts_or_nop(true_branch)?;
self.compile_stmts_or_none(true_branch)?;
self.coerce_stack(StkIdx(stack_size_before + 1));
let jmp_inst = self.add_inst(OpCode::Jmp, 0);
self.fixup_jmp(jf_inst);
Expand All @@ -381,10 +381,10 @@ impl Compiler {
// false branch may be a If statement or a Block statement.
match &**false_branch {
Statement::Block(false_branch) => {
self.compile_stmts_or_nop(false_branch)?;
self.compile_stmts_or_none(false_branch)?;
}
Statement::If { .. } => {
self.compile_stmts_or_nop(&vec![(**false_branch).clone()])?;
self.compile_stmts_or_none(&vec![(**false_branch).clone()])?;
}
_ => unreachable!(),
}
Expand Down Expand Up @@ -453,16 +453,17 @@ impl Compiler {
Ok(last_result)
}

fn compile_stmts_or_nop(&mut self, stmts: &Statements) -> Result<StkIdx, Box<dyn Error>> {
fn compile_stmts_or_none(&mut self, stmts: &Statements) -> Result<StkIdx, Box<dyn Error>> {
Ok(self.compile_stmts(stmts)?.unwrap_or_else(|| {
self.add_inst(OpCode::Nop, 0);
let id = self.add_literal(Default::default());
self.add_load_literal_inst(id);
self.stack_top()
}))
}

pub fn compile(&mut self, stmts: &Statements) -> Result<(), Box<dyn std::error::Error>> {
let name = "main";
self.compile_stmts_or_nop(stmts)?;
self.compile_stmts_or_none(stmts)?;
self.add_fn(name.to_string(), &[], false);
Ok(())
}
Expand Down

0 comments on commit d0212b7

Please sign in to comment.