Skip to content

Commit

Permalink
wip(ir): parser of insts
Browse files Browse the repository at this point in the history
  • Loading branch information
JuniMay committed Feb 21, 2024
1 parent 27fea9b commit f778959
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 8 deletions.
4 changes: 2 additions & 2 deletions src/ir/frontend/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,10 +195,10 @@ impl Inst {
}))
}

pub fn new_boxed_call(dest: AstNodeBox, ty: Type, callee: AstNodeBox) -> AstNodeBox {
pub fn new_boxed_call(dest: Option<AstNodeBox>, ty: Type, callee: AstNodeBox) -> AstNodeBox {
Box::new(AstNode::Inst(Inst {
kind: InstKind::Call,
dest: None,
dest,
operands: vec![callee],
ty: Some(ty),
}))
Expand Down
36 changes: 30 additions & 6 deletions src/ir/frontend/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ where
Inst::Call => {
let ty = self.parse_type()?;
let callee = self.parse_operand()?;
let node = ast::Inst::new_boxed_call(dest, ty, callee);
let node = ast::Inst::new_boxed_call(Some(dest), ty, callee);
node
}
Inst::GetElemPtr => {
Expand Down Expand Up @@ -402,11 +402,35 @@ where
todo!()
}
TokenKind::Inst(ref inst) => match inst {
Inst::Store => todo!(),
Inst::Jump => todo!(),
Inst::Branch => todo!(),
Inst::Call => todo!(),
Inst::Return => todo!(),
Inst::Store => {
let val = self.parse_operand()?;
self.expect(TokenKind::Comma)?;
let ptr = self.parse_operand()?;
Ok(ast::Inst::new_boxed(InstKind::Store, None, vec![val, ptr]))
}
Inst::Jump => {
let dst = self.parse_operand()?;
Ok(ast::Inst::new_boxed(InstKind::Jump, None, vec![dst]))
}
Inst::Branch => {
let then = self.parse_operand()?;
self.expect(TokenKind::Comma)?;
let else_ = self.parse_operand()?;
Ok(ast::Inst::new_boxed(
InstKind::Branch,
None,
vec![then, else_],
))
}
Inst::Call => {
let ty = self.parse_type()?;
let callee = self.parse_operand()?;
Ok(ast::Inst::new_boxed_call(None, ty, callee))
}
Inst::Return => {
let val = self.parse_operand()?;
Ok(ast::Inst::new_boxed(InstKind::Return, None, vec![val]))
}
_ => Err(ParseError::UnexpectedToken),
},
_ => Err(ParseError::UnexpectedToken),
Expand Down

0 comments on commit f778959

Please sign in to comment.