some fixing + strings
This commit is contained in:
parent
b3ba7162b7
commit
1561abcc31
2 changed files with 29 additions and 14 deletions
|
@ -3,10 +3,10 @@ use std::{collections::HashMap};
|
|||
use crate::parser::ASTPart;
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Opeartion {
|
||||
pub struct Operation {
|
||||
pub opcode: u8,
|
||||
pub arg1: Option<u8>,
|
||||
pub arg2: Option<u32>,
|
||||
pub arg2: Option<i64>,
|
||||
pub arg3: Option<u8>,
|
||||
}
|
||||
|
||||
|
@ -87,24 +87,36 @@ fn garbage_collect_registers(registers: &mut Vec<RegisterState>) {
|
|||
}
|
||||
}
|
||||
|
||||
fn do_ast_op(ast_op: ASTPart, op_count: &mut usize, ops: &mut Vec<Opeartion>, var_ids: &mut HashMap<String, u32>, next_var_id: &mut u32, registers: &mut Vec<RegisterState>) -> u8 {
|
||||
fn do_ast_op(ast_op: ASTPart, op_count: &mut usize, ops: &mut Vec<Operation>, var_ids: &mut HashMap<String, u32>, next_var_id: &mut u32, strings: &mut HashMap<u32, String>, next_string_id: &mut u32, registers: &mut Vec<RegisterState>) -> u8 {
|
||||
*op_count += 1;
|
||||
match ast_op {
|
||||
ASTPart::Number(num) => {
|
||||
let reg = allocate_register(registers);
|
||||
if reg.unbind_before {
|
||||
ops.push(Opeartion { opcode: 8, arg1: Some(reg.register), arg2: None, arg3: None });
|
||||
ops.push(Operation { opcode: 8, arg1: Some(reg.register), arg2: None, arg3: None });
|
||||
}
|
||||
ops.push(Opeartion { opcode: 3, arg1: Some(reg.register), arg2: Some(num.value as u32), arg3: None });
|
||||
ops.push(Operation { opcode: 3, arg1: Some(reg.register), arg2: Some(num.value), arg3: None });
|
||||
set_register(registers, RegisterState { id: reg.register, used: true, variable: 0, last_used: *op_count });
|
||||
return reg.register;
|
||||
},
|
||||
ASTPart::String(str) => {
|
||||
let str_id = *next_string_id;
|
||||
strings.insert(str_id, str.value);
|
||||
*next_string_id += 1;
|
||||
let reg = allocate_register(registers);
|
||||
if reg.unbind_before {
|
||||
ops.push(Operation { opcode: 8, arg1: Some(reg.register), arg2: None, arg3: None });
|
||||
}
|
||||
ops.push(Operation { opcode: 1, arg1: Some(reg.register), arg2: Some(str_id as i64), arg3: None });
|
||||
set_register(registers, RegisterState { id: reg.register, used: true, variable: 0, last_used: *op_count });
|
||||
return reg.register;
|
||||
},
|
||||
ASTPart::Operation(op) => {
|
||||
let left_reg = do_ast_op(*op.left, op_count, ops, var_ids, next_var_id, registers);
|
||||
let right_reg = do_ast_op(*op.right, op_count, ops, var_ids, next_var_id, registers);
|
||||
let left_reg = do_ast_op(*op.left, op_count, ops, var_ids, next_var_id, strings, next_string_id, registers);
|
||||
let right_reg = do_ast_op(*op.right, op_count, ops, var_ids, next_var_id, strings, next_string_id, registers);
|
||||
let reg = allocate_register(registers);
|
||||
if reg.unbind_before {
|
||||
ops.push(Opeartion { opcode: 8, arg1: Some(reg.register), arg2: None, arg3: None });
|
||||
ops.push(Operation { opcode: 8, arg1: Some(reg.register), arg2: None, arg3: None });
|
||||
set_register(registers, RegisterState { id: reg.register, used: true, variable: 0, last_used: *op_count });
|
||||
}
|
||||
let opcode = match op.operator.as_str() {
|
||||
|
@ -124,7 +136,7 @@ fn do_ast_op(ast_op: ASTPart, op_count: &mut usize, ops: &mut Vec<Opeartion>, va
|
|||
"<=" => 23,
|
||||
_ => panic!("Unknown operator {}", op.operator),
|
||||
};
|
||||
ops.push(Opeartion { opcode, arg1: Some(left_reg), arg2: Some(right_reg as u32), arg3: Some(reg.register) });
|
||||
ops.push(Operation { opcode, arg1: Some(left_reg), arg2: Some(right_reg as i64), arg3: Some(reg.register) });
|
||||
set_register(registers, RegisterState { id: reg.register, used: true, variable: 0, last_used: *op_count });
|
||||
return reg.register;
|
||||
},
|
||||
|
@ -134,8 +146,8 @@ fn do_ast_op(ast_op: ASTPart, op_count: &mut usize, ops: &mut Vec<Opeartion>, va
|
|||
}
|
||||
var_ids.insert(asign.variable.clone(), *next_var_id);
|
||||
*next_var_id += 1;
|
||||
let reg = do_ast_op(*asign.value, op_count, ops, var_ids, next_var_id, registers);
|
||||
ops.push(Opeartion { opcode: 7, arg1: Some(reg), arg2: Some(var_ids[&asign.variable.clone()]), arg3: None });
|
||||
let reg = do_ast_op(*asign.value, op_count, ops, var_ids, next_var_id, strings, next_string_id, registers);
|
||||
ops.push(Operation { opcode: 7, arg1: Some(reg), arg2: Some(var_ids[&asign.variable.clone()] as i64), arg3: None });
|
||||
set_register(registers, RegisterState { id: reg, used: true, variable: var_ids[&asign.variable.clone()], last_used: *op_count });
|
||||
garbage_collect_registers(registers);
|
||||
},
|
||||
|
@ -145,10 +157,12 @@ fn do_ast_op(ast_op: ASTPart, op_count: &mut usize, ops: &mut Vec<Opeartion>, va
|
|||
}
|
||||
|
||||
pub fn compile(ast: Vec<ASTPart>) -> Vec<u8> {
|
||||
let mut ops: Vec<Opeartion> = vec![];
|
||||
let mut ops: Vec<Operation> = vec![];
|
||||
|
||||
let mut var_ids: HashMap<String, u32> = HashMap::new();
|
||||
let mut next_var_id: u32 = 1;
|
||||
let mut strings: HashMap<u32, String> = HashMap::new();
|
||||
let mut next_string_id: u32 = 1;
|
||||
let mut registers: Vec<RegisterState> = vec![];
|
||||
for i in 0..17 {
|
||||
registers.push(RegisterState {
|
||||
|
@ -161,10 +175,11 @@ pub fn compile(ast: Vec<ASTPart>) -> Vec<u8> {
|
|||
|
||||
let mut op_count = 0;
|
||||
for ast_op in ast {
|
||||
do_ast_op(ast_op, &mut op_count, &mut ops, &mut var_ids, &mut next_var_id, &mut registers);
|
||||
do_ast_op(ast_op, &mut op_count, &mut ops, &mut var_ids, &mut next_var_id, &mut strings, &mut next_string_id, &mut registers);
|
||||
println!("Operations: {:?}\n", ops);
|
||||
println!("Registers: {:?}\n", registers);
|
||||
println!("Variable IDs: {:?}", var_ids);
|
||||
println!("Strings: {:?}", strings);
|
||||
println!("==========================");
|
||||
}
|
||||
return vec![];
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue