|
|
@ -4,7 +4,8 @@ use crate::expr::{BinaryExpr, CallExpr, ExprValue, LogicalExpr, UnaryExpr, Varia |
|
|
|
use crate::function::Function;
|
|
|
|
use crate::opcodes::OpCode;
|
|
|
|
use crate::stmt::{
|
|
|
|
AssignStmt, ExpressionStmt, ForStmt, IfStmt, InputStmt, PrintStmt, ReturnStmt, Stmt, WhileStmt,
|
|
|
|
AssignStmt, BinaryAssignStmt, ExpressionStmt, ForStmt, IfStmt, InputStmt, PrintStmt,
|
|
|
|
ReturnStmt, Stmt, WhileStmt,
|
|
|
|
};
|
|
|
|
use crate::tokens::{Token, TokenType};
|
|
|
|
use crate::visitor::{ExprVisitor, StmtVisitor};
|
|
|
@ -270,6 +271,27 @@ impl StmtVisitor<()> for Compiler { |
|
|
|
stmt.return_value.accept(self);
|
|
|
|
self.emit_opcode(OpCode::Ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_binary_assign_stmt(&mut self, stmt: &BinaryAssignStmt, _: usize) -> () {
|
|
|
|
let var_id = self.get_variable(stmt.name.clone());
|
|
|
|
self.emit_opcode(OpCode::Var);
|
|
|
|
self.emit_byte(var_id);
|
|
|
|
|
|
|
|
stmt.value.accept(self);
|
|
|
|
|
|
|
|
self.emit_opcode(match stmt.operator {
|
|
|
|
TokenType::PlusEqual => OpCode::Add,
|
|
|
|
TokenType::MinusEqual => OpCode::Sub,
|
|
|
|
TokenType::StarEqual => OpCode::Mul,
|
|
|
|
TokenType::SlashEqual => OpCode::Div,
|
|
|
|
TokenType::CaretEqual => OpCode::Pow,
|
|
|
|
TokenType::PercentEqual => OpCode::Mod,
|
|
|
|
_ => unreachable!(),
|
|
|
|
});
|
|
|
|
|
|
|
|
self.emit_opcode(OpCode::SetVar);
|
|
|
|
self.emit_byte(var_id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ExprVisitor<()> for Compiler {
|
|
|
|