added ifs

This commit is contained in:
afonya2 2025-05-30 22:01:40 +02:00
parent b186e45067
commit 0c60e98d9f
Signed by: afonya
GPG key ID: EBB9C4CAFAAFB2DC
2 changed files with 21 additions and 3 deletions

View file

@ -166,7 +166,6 @@ fn do_ast_op(ast_op: ASTPart, op_count: &mut usize, ops: &mut Vec<Operation>, va
}, },
ASTPart::Function(func) => { ASTPart::Function(func) => {
let func_id = *next_function_id; let func_id = *next_function_id;
functions.insert(func_id, compile_function(func.body, Some(func.args), registers)); functions.insert(func_id, compile_function(func.body, Some(func.args), registers));
*next_function_id += 1; *next_function_id += 1;
let reg = allocate_register(registers); let reg = allocate_register(registers);
@ -185,13 +184,28 @@ fn do_ast_op(ast_op: ASTPart, op_count: &mut usize, ops: &mut Vec<Operation>, va
} }
ops.push(Operation { opcode: 27, arg1: Some(func), arg2: None, arg3: None }); ops.push(Operation { opcode: 27, arg1: Some(func), arg2: None, arg3: None });
}, },
ASTPart::If(if_part) => {
let condition_reg = do_ast_op(*if_part.condition, op_count, ops, var_ids, next_var_id, strings, next_string_id, functions, next_function_id, registers);
ops.push(Operation { opcode: 24, arg1: Some(condition_reg), arg2: Some(condition_reg as i64), arg3: None });
let op_placeholder = ops.len();
ops.push(Operation { opcode: 0, arg1: None, arg2: None, arg3: None });
for if_op in if_part.body {
do_ast_op(if_op, op_count, ops, var_ids, next_var_id, strings, next_string_id, functions, next_function_id, registers);
}
ops[op_placeholder] = Operation {
opcode: 26,
arg1: Some(condition_reg),
arg2: Some(ops.len() as i64),
arg3: None,
};
garbage_collect_registers(registers);
},
ASTPart::Operation(op) => { ASTPart::Operation(op) => {
let left_reg = do_ast_op(*op.left, op_count, ops, var_ids, next_var_id, strings, next_string_id, functions, next_function_id, registers); let left_reg = do_ast_op(*op.left, op_count, ops, var_ids, next_var_id, strings, next_string_id, functions, next_function_id, registers);
let right_reg = do_ast_op(*op.right, op_count, ops, var_ids, next_var_id, strings, next_string_id, functions, next_function_id, registers); let right_reg = do_ast_op(*op.right, op_count, ops, var_ids, next_var_id, strings, next_string_id, functions, next_function_id, registers);
let reg = allocate_register(registers); let reg = allocate_register(registers);
if reg.unbind_before { if reg.unbind_before {
ops.push(Operation { 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() { let opcode = match op.operator.as_str() {
"+" => 10, "+" => 10,
@ -271,6 +285,7 @@ fn compile_function(ast: Vec<ASTPart>, args: Option<Vec<String>>, registers: &mu
for ast_op in ast { for ast_op in ast {
do_ast_op(ast_op, &mut op_count, &mut ops, &mut var_ids, &mut next_var_id, &mut strings, &mut next_string_id, &mut functions, &mut next_function_id, 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 functions, &mut next_function_id, registers);
} }
ops.push(Operation { opcode: 29, arg1: Some(0), arg2: None, arg3: None });
println!("Operations: {:?}\n", ops); println!("Operations: {:?}\n", ops);
println!("Registers: {:?}\n", registers); println!("Registers: {:?}\n", registers);
println!("Variable IDs: {:?}", var_ids); println!("Variable IDs: {:?}", var_ids);

View file

@ -1,2 +1,5 @@
gethelj a = 1 gethelj a = 1
b = 2 ha geny (1==1) {
gethelj b = 2
}
gethelj c = 3