Skip to content

Commit

Permalink
small stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
Ianyourgod committed Jun 26, 2024
1 parent d0907e3 commit 2cffc45
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 18 deletions.
6 changes: 3 additions & 3 deletions src/code_gen/firstpass.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@ impl Pass {

fn emit_instruction(&self, statement: &tacky::nodes::Instruction, instructions: &mut Vec<nodes::Instruction>) {
match statement {
tacky::nodes::Instruction::Return(return_statement) => {
let return_value = self.emit_value(&return_statement.return_value);
tacky::nodes::Instruction::Return(return_value) => {
let value = self.emit_value(return_value);
instructions.push(nodes::Instruction::Mov(nodes::BinOp {
src: return_value,
src: value,
dest: nodes::Operand::Register(nodes::Reg::AX),
suffix: Some(LONG.to_string()),
}));
Expand Down
14 changes: 10 additions & 4 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ impl Parser {

let condition = self.parse_expression(0);

// todo: require braces
// todo: require braces, or else weird variable scoping issues
let consequence = self.parse_statement();
self.next_token();
let alternative = if self.cur_token.kind == lexer::TokenType::Keyword && self.cur_token.literal == "else" {
Expand Down Expand Up @@ -354,7 +354,9 @@ impl Parser {

fn get_precidence(&self, token: &lexer::TokenType) -> i8 {
match token {
lexer::TokenType::Assign => 1,
lexer::TokenType::Assign |
lexer::TokenType::AddAssign | lexer::TokenType::SubtractAssign |
lexer::TokenType::MultiplyAssign | lexer::TokenType::DivideAssign => 1,
lexer::TokenType::Or => 5,
lexer::TokenType::And => 10,
lexer::TokenType::Equal | lexer::TokenType::NotEqual => 30,
Expand Down Expand Up @@ -408,14 +410,18 @@ impl Parser {
lexer::TokenType::LessThan | lexer::TokenType::GreaterThan |
lexer::TokenType::LessThanEqual | lexer::TokenType::GreaterThanEqual |
lexer::TokenType::And | lexer::TokenType::Or => true,
lexer::TokenType::Assign => true,
lexer::TokenType::Assign |
lexer::TokenType::AddAssign | lexer::TokenType::SubtractAssign |
lexer::TokenType::MultiplyAssign | lexer::TokenType::DivideAssign => true,
_ => false,
}
}

fn is_assignment(&self) -> bool {
match self.cur_token.kind {
lexer::TokenType::Assign => true,
lexer::TokenType::Assign |
lexer::TokenType::AddAssign | lexer::TokenType::SubtractAssign |
lexer::TokenType::MultiplyAssign | lexer::TokenType::DivideAssign => true,
_ => false,
}
}
Expand Down
1 change: 0 additions & 1 deletion src/semantic_analysis/variable_resolution_pass.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ impl Pass {
return_value: self.resolve_expression(ret.return_value)
}))
},

_ => statement,
}
}
Expand Down
5 changes: 2 additions & 3 deletions src/tacky.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ impl Tacky {
instructions: Vec::new(),
};
self.emit_tacky_statement(&*statement.body, &mut instructions);
instructions.instructions.push(nodes::Instruction::Return(nodes::Value::Constant(0)));
program.function_definitions.push(nodes::FunctionDefinition {
function_name: statement.function_name.clone(),
body: instructions,
Expand All @@ -58,9 +59,7 @@ impl Tacky {
match statement {
parser::nodes::Statement::ReturnStatement(return_statement) => {
let return_value = self.emit_tacky_expression(&*return_statement.return_value, instructions);
instructions.instructions.push(nodes::Instruction::Return(nodes::Return {
return_value
}));
instructions.instructions.push(nodes::Instruction::Return(return_value));
}
parser::nodes::Statement::FunctionDeclaration(ref function_declaration) => {
let mut body = nodes::CompoundInstruction {
Expand Down
7 changes: 1 addition & 6 deletions src/tacky/nodes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub struct CompoundInstruction {

#[derive(Debug, Clone, PartialEq)]
pub enum Instruction {
Return(Return),
Return(Value),
Unary(Unary),
Binary(Binary),
Copy(Copy),
Expand All @@ -35,11 +35,6 @@ pub struct Copy {
pub dest: Value,
}

#[derive(Debug, Clone, PartialEq)]
pub struct Return {
pub return_value: Value,
}

#[derive(Debug, Clone, PartialEq)]
pub struct Unary {
pub operator: UnaryOperator,
Expand Down
2 changes: 1 addition & 1 deletion test.ag
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
fn main(argc: int) -> int {
let a: int = 6;
let b: int = 5;
return;
return a*b;
}

0 comments on commit 2cffc45

Please sign in to comment.