diff --git a/src/compiler.rs b/src/compiler.rs index 9800d8f..d5b82ce 100644 --- a/src/compiler.rs +++ b/src/compiler.rs @@ -7,7 +7,7 @@ const ASXVERSION: [u8; 3] = [0,1,0]; pub struct Operation { pub opcode: u8, pub arg1: Option, - pub arg2: Option, + pub arg2: Option, pub arg3: Option, pub pos: u32, } @@ -179,7 +179,7 @@ fn do_ast_op(ast_op: ASTPart, op_count: &mut usize, ops: &mut Vec, va if reg.unbind_before { ops.push(Operation { opcode: 8, arg1: Some(reg.register), arg2: None, arg3: None, pos: str.pos as u32 }); } - ops.push(Operation { opcode: 1, arg1: Some(reg.register), arg2: Some(str_id as i64), arg3: None, pos: str.pos as u32 }); + ops.push(Operation { opcode: 1, arg1: Some(reg.register), arg2: Some(str_id as f64), arg3: None, pos: str.pos as u32 }); set_register(registers, RegisterState { id: reg.register, used: true, variable: 0, last_used: *op_count }); return reg.register; }, @@ -188,7 +188,7 @@ fn do_ast_op(ast_op: ASTPart, op_count: &mut usize, ops: &mut Vec, va if reg.unbind_before { ops.push(Operation { opcode: 8, arg1: Some(reg.register), arg2: None, arg3: None, pos: bool.pos as u32 }); } - ops.push(Operation { opcode: 4, arg1: Some(reg.register), arg2: Some(bool.value as i64), arg3: None, pos: bool.pos as u32 }); + ops.push(Operation { opcode: 4, arg1: Some(reg.register), arg2: Some((bool.value as i64) as f64), arg3: None, pos: bool.pos as u32 }); set_register(registers, RegisterState { id: reg.register, used: true, variable: 0, last_used: *op_count }); return reg.register; }, @@ -197,7 +197,7 @@ fn do_ast_op(ast_op: ASTPart, op_count: &mut usize, ops: &mut Vec, va if reg.unbind_before { ops.push(Operation { opcode: 8, arg1: Some(reg.register), arg2: None, arg3: None, pos: nul.pos as u32 }); } - ops.push(Operation { opcode: 6, arg1: Some(reg.register), arg2: Some(0), arg3: None, pos: nul.pos as u32 }); + ops.push(Operation { opcode: 6, arg1: Some(reg.register), arg2: Some(0 as f64), arg3: None, pos: nul.pos as u32 }); set_register(registers, RegisterState { id: reg.register, used: true, variable: 0, last_used: *op_count }); return reg.register; }, @@ -210,8 +210,8 @@ fn do_ast_op(ast_op: ASTPart, op_count: &mut usize, ops: &mut Vec, va let str_id = *next_string_id; strings.insert(str_id, var_read.variable.clone()); *next_string_id += 1; - ops.push(Operation { opcode: 1, arg1: Some(reg.register), arg2: Some(str_id as i64), arg3: None, pos: var_read.pos as u32 }); - ops.push(Operation { opcode: 30, arg1: Some(0), arg2: Some(reg.register as i64), arg3: Some(reg.register), pos: var_read.pos as u32 }); + ops.push(Operation { opcode: 1, arg1: Some(reg.register), arg2: Some(str_id as f64), arg3: None, pos: var_read.pos as u32 }); + ops.push(Operation { opcode: 30, arg1: Some(0), arg2: Some(reg.register as f64), arg3: Some(reg.register), pos: var_read.pos as u32 }); set_register(registers, RegisterState { id: reg.register, used: true, variable: 0, last_used: *op_count }); return reg.register; } @@ -221,7 +221,7 @@ fn do_ast_op(ast_op: ASTPart, op_count: &mut usize, ops: &mut Vec, va if reg.unbind_before { ops.push(Operation { opcode: 8, arg1: Some(reg.register), arg2: None, arg3: None, pos: var_read.pos as u32 }); } - ops.push(Operation { opcode: 2, arg1: Some(reg.register), arg2: Some(get_variable_by_name(variables, &var_read.variable, ops.len(), traceback).expect("Variable should exist").id as i64), arg3: None, pos: var_read.pos as u32 }); + ops.push(Operation { opcode: 2, arg1: Some(reg.register), arg2: Some(get_variable_by_name(variables, &var_read.variable, ops.len(), traceback).expect("Variable should exist").id as f64), arg3: None, pos: var_read.pos as u32 }); set_register(registers, RegisterState { id: reg.register, used: true, variable: get_variable_by_name(variables, &var_read.variable, ops.len(), traceback).expect("Variable should exist").id, last_used: *op_count }); return reg.register; } else { @@ -240,7 +240,7 @@ fn do_ast_op(ast_op: ASTPart, op_count: &mut usize, ops: &mut Vec, va if reg.unbind_before { ops.push(Operation { opcode: 8, arg1: Some(reg.register), arg2: None, arg3: None, pos: func.pos as u32 }); } - ops.push(Operation { opcode: 5, arg1: Some(reg.register), arg2: Some(func_id as i64), arg3: None, pos: func.pos as u32 }); + ops.push(Operation { opcode: 5, arg1: Some(reg.register), arg2: Some(func_id as f64), arg3: None, pos: func.pos as u32 }); set_register(registers, RegisterState { id: reg.register, used: true, variable: 0, last_used: *op_count }); return reg.register; }, @@ -258,20 +258,20 @@ fn do_ast_op(ast_op: ASTPart, op_count: &mut usize, ops: &mut Vec, va if ret_reg.unbind_before { ops.push(Operation { opcode: 8, arg1: Some(ret_reg.register), arg2: None, arg3: None, pos: call.pos as u32 }); } - ops.push(Operation { opcode: 27, arg1: Some(func), arg2: Some(ret_reg.register as i64), arg3: None, pos: call.pos as u32 }); + ops.push(Operation { opcode: 27, arg1: Some(func), arg2: Some(ret_reg.register as f64), arg3: None, pos: call.pos as u32 }); set_register(registers, RegisterState { id: ret_reg.register, used: true, variable: 0, last_used: *op_count }); return ret_reg.register; }, ASTPart::If(if_part) => { let condition_reg = do_ast_op(*if_part.condition, op_count, ops, variables, next_var_id, strings, next_string_id, functions, next_function_id, registers, ctx, traceback); - ops.push(Operation { opcode: 24, arg1: Some(condition_reg), arg2: Some(condition_reg as i64), arg3: None, pos: if_part.pos as u32 }); + ops.push(Operation { opcode: 24, arg1: Some(condition_reg), arg2: Some(condition_reg as f64), arg3: None, pos: if_part.pos as u32 }); //Update the lastif variable if get_variable_by_name(variables, "__LASTIF", ops.len(), traceback).is_none() { variables.push(Variable { name: String::from("__LASTIF"), id: *next_var_id, start: ops.len(), end: 0, no_end: true }); *next_var_id += 1; } - ops.push(Operation { opcode: 7, arg1: Some(condition_reg), arg2: Some(get_variable_by_name(variables, "__LASTIF", ops.len(), traceback).expect("__LASTIF should exist").id as i64), arg3: None, pos: if_part.pos as u32 }); + ops.push(Operation { opcode: 7, arg1: Some(condition_reg), arg2: Some(get_variable_by_name(variables, "__LASTIF", ops.len(), traceback).expect("__LASTIF should exist").id as f64), arg3: None, pos: if_part.pos as u32 }); set_register(registers, RegisterState { id: condition_reg, used: true, variable: get_variable_by_name(variables, "__LASTIF", ops.len(), traceback).expect("__LASTIF should exist").id, last_used: *op_count }); let op_placeholder = ops.len(); @@ -286,7 +286,7 @@ fn do_ast_op(ast_op: ASTPart, op_count: &mut usize, ops: &mut Vec, va ops[op_placeholder] = Operation { opcode: 26, arg1: Some(condition_reg), - arg2: Some(ops.len() as i64), + arg2: Some(ops.len() as f64), arg3: None, pos: if_part.pos as u32, }; @@ -311,13 +311,13 @@ fn do_ast_op(ast_op: ASTPart, op_count: &mut usize, ops: &mut Vec, va if reg.unbind_before { ops.push(Operation { opcode: 8, arg1: Some(reg.register), arg2: None, arg3: None, pos: else_part.pos as u32 }); } - ops.push(Operation { opcode: 2, arg1: Some(reg.register), arg2: Some(get_variable_by_name(variables, "__LASTIF", ops.len(), traceback).expect("__LASTIF should exist").id as i64), arg3: None, pos: else_part.pos as u32 }); + ops.push(Operation { opcode: 2, arg1: Some(reg.register), arg2: Some(get_variable_by_name(variables, "__LASTIF", ops.len(), traceback).expect("__LASTIF should exist").id as f64), arg3: None, pos: else_part.pos as u32 }); set_register(registers, RegisterState { id: reg.register, used: true, variable: get_variable_by_name(variables, "__LASTIF", ops.len(), traceback).expect("__LASTIF should exist").id, last_used: *op_count }); else_condition_reg = reg.register; } else { else_condition_reg = reg.id; } - ops.push(Operation { opcode: 24, arg1: Some(else_condition_reg), arg2: Some(else_condition_reg as i64), arg3: None, pos: else_part.pos as u32 }); + ops.push(Operation { opcode: 24, arg1: Some(else_condition_reg), arg2: Some(else_condition_reg as f64), arg3: None, pos: else_part.pos as u32 }); let op_placeholder = ops.len(); ops.push(Operation { opcode: 0, arg1: None, arg2: None, arg3: None, pos: else_part.pos as u32 }); @@ -331,7 +331,7 @@ fn do_ast_op(ast_op: ASTPart, op_count: &mut usize, ops: &mut Vec, va ops[op_placeholder] = Operation { opcode: 26, arg1: Some(else_condition_reg), - arg2: Some(ops.len() as i64), + arg2: Some(ops.len() as f64), arg3: None, pos: else_part.pos as u32, }; @@ -355,20 +355,20 @@ fn do_ast_op(ast_op: ASTPart, op_count: &mut usize, ops: &mut Vec, va if reg.unbind_before { ops.push(Operation { opcode: 8, arg1: Some(reg.register), arg2: None, arg3: None, pos: elseif_part.pos as u32 }); } - ops.push(Operation { opcode: 2, arg1: Some(reg.register), arg2: Some(get_variable_by_name(variables, "__LASTIF", ops.len(), traceback).expect("__LASTIF should exist").id as i64), arg3: None, pos: elseif_part.pos as u32 }); + ops.push(Operation { opcode: 2, arg1: Some(reg.register), arg2: Some(get_variable_by_name(variables, "__LASTIF", ops.len(), traceback).expect("__LASTIF should exist").id as f64), arg3: None, pos: elseif_part.pos as u32 }); set_register(registers, RegisterState { id: reg.register, used: true, variable: get_variable_by_name(variables, "__LASTIF", ops.len(), traceback).expect("__LASTIF should exist").id, last_used: *op_count }); else_condition_reg = reg.register; } else { else_condition_reg = reg.id; } - ops.push(Operation { opcode: 24, arg1: Some(else_condition_reg), arg2: Some(else_condition_reg as i64), arg3: None, pos: elseif_part.pos as u32 }); + ops.push(Operation { opcode: 24, arg1: Some(else_condition_reg), arg2: Some(else_condition_reg as f64), arg3: None, pos: elseif_part.pos as u32 }); let condition_reg = do_ast_op(*elseif_part.condition, op_count, ops, variables, next_var_id, strings, next_string_id, functions, next_function_id, registers, ctx, traceback); - ops.push(Operation { opcode: 16, arg1: Some(else_condition_reg), arg2: Some(condition_reg as i64), arg3: Some(condition_reg), pos: elseif_part.pos as u32 }); - ops.push(Operation { opcode: 24, arg1: Some(condition_reg), arg2: Some(condition_reg as i64), arg3: None, pos: elseif_part.pos as u32 }); + ops.push(Operation { opcode: 16, arg1: Some(else_condition_reg), arg2: Some(condition_reg as f64), arg3: Some(condition_reg), pos: elseif_part.pos as u32 }); + ops.push(Operation { opcode: 24, arg1: Some(condition_reg), arg2: Some(condition_reg as f64), arg3: None, pos: elseif_part.pos as u32 }); //Update the lastif variable - ops.push(Operation { opcode: 7, arg1: Some(condition_reg), arg2: Some(get_variable_by_name(variables, "__LASTIF", ops.len(), traceback).expect("__LASTIF should exist").id as i64), arg3: None, pos: elseif_part.pos as u32 }); + ops.push(Operation { opcode: 7, arg1: Some(condition_reg), arg2: Some(get_variable_by_name(variables, "__LASTIF", ops.len(), traceback).expect("__LASTIF should exist").id as f64), arg3: None, pos: elseif_part.pos as u32 }); set_register(registers, RegisterState { id: condition_reg, used: true, variable: get_variable_by_name(variables, "__LASTIF", ops.len(), traceback).expect("__LASTIF should exist").id, last_used: *op_count }); let op_placeholder = ops.len(); @@ -383,7 +383,7 @@ fn do_ast_op(ast_op: ASTPart, op_count: &mut usize, ops: &mut Vec, va ops[op_placeholder] = Operation { opcode: 26, arg1: Some(condition_reg), - arg2: Some(ops.len() as i64), + arg2: Some(ops.len() as f64), arg3: None, pos: elseif_part.pos as u32 }; @@ -397,7 +397,7 @@ fn do_ast_op(ast_op: ASTPart, op_count: &mut usize, ops: &mut Vec, va ASTPart::While(while_part) => { let start = ops.len(); let condition_reg = do_ast_op(*while_part.condition, op_count, ops, variables, next_var_id, strings, next_string_id, functions, next_function_id, registers, ctx, traceback); - ops.push(Operation { opcode: 24, arg1: Some(condition_reg), arg2: Some(condition_reg as i64), arg3: None, pos: while_part.pos as u32 }); + ops.push(Operation { opcode: 24, arg1: Some(condition_reg), arg2: Some(condition_reg as f64), arg3: None, pos: while_part.pos as u32 }); let op_placeholder = ops.len(); let mut breaks: Vec = vec![]; @@ -418,11 +418,11 @@ fn do_ast_op(ast_op: ASTPart, op_count: &mut usize, ops: &mut Vec, va } } } - ops.push(Operation { opcode: 25, arg1: None, arg2: Some(start as i64), arg3: None, pos: while_part.pos as u32 }); + ops.push(Operation { opcode: 25, arg1: None, arg2: Some(start as f64), arg3: None, pos: while_part.pos as u32 }); ops[op_placeholder] = Operation { opcode: 26, arg1: Some(condition_reg), - arg2: Some(ops.len() as i64), + arg2: Some(ops.len() as f64), arg3: None, pos: while_part.pos as u32 }; @@ -430,7 +430,7 @@ fn do_ast_op(ast_op: ASTPart, op_count: &mut usize, ops: &mut Vec, va ops[brk] = Operation { opcode: 25, arg1: None, - arg2: Some(ops.len() as i64), + arg2: Some(ops.len() as f64), arg3: None, pos: while_part.pos as u32 }; @@ -439,7 +439,7 @@ fn do_ast_op(ast_op: ASTPart, op_count: &mut usize, ops: &mut Vec, va ops[cont] = Operation { opcode: 25, arg1: None, - arg2: Some(start as i64), + arg2: Some(start as f64), arg3: None, pos: while_part.pos as u32 }; @@ -459,7 +459,7 @@ fn do_ast_op(ast_op: ASTPart, op_count: &mut usize, ops: &mut Vec, va do_ast_op(*for_part.init, op_count, ops, variables, next_var_id, strings, next_string_id, functions, next_function_id, registers, ctx, traceback); let start = ops.len(); let condition_reg = do_ast_op(*for_part.condition, op_count, ops, variables, next_var_id, strings, next_string_id, functions, next_function_id, registers, ctx, traceback); - ops.push(Operation { opcode: 24, arg1: Some(condition_reg), arg2: Some(condition_reg as i64), arg3: None, pos: for_part.pos as u32 }); + ops.push(Operation { opcode: 24, arg1: Some(condition_reg), arg2: Some(condition_reg as f64), arg3: None, pos: for_part.pos as u32 }); let op_placeholder = ops.len(); let mut breaks: Vec = vec![]; @@ -482,11 +482,11 @@ fn do_ast_op(ast_op: ASTPart, op_count: &mut usize, ops: &mut Vec, va } do_ast_op(*for_part.update, op_count, ops, variables, next_var_id, strings, next_string_id, functions, next_function_id, registers, ctx, traceback); - ops.push(Operation { opcode: 25, arg1: None, arg2: Some(start as i64), arg3: None, pos: for_part.pos as u32 }); + ops.push(Operation { opcode: 25, arg1: None, arg2: Some(start as f64), arg3: None, pos: for_part.pos as u32 }); ops[op_placeholder] = Operation { opcode: 26, arg1: Some(condition_reg), - arg2: Some(ops.len() as i64), + arg2: Some(ops.len() as f64), arg3: None, pos: for_part.pos as u32 }; @@ -494,7 +494,7 @@ fn do_ast_op(ast_op: ASTPart, op_count: &mut usize, ops: &mut Vec, va ops[brk] = Operation { opcode: 25, arg1: None, - arg2: Some(ops.len() as i64), + arg2: Some(ops.len() as f64), arg3: None, pos: for_part.pos as u32 }; @@ -503,7 +503,7 @@ fn do_ast_op(ast_op: ASTPart, op_count: &mut usize, ops: &mut Vec, va ops[cont] = Operation { opcode: 25, arg1: None, - arg2: Some(start as i64), + arg2: Some(start as f64), arg3: None, pos: for_part.pos as u32 }; @@ -536,7 +536,7 @@ fn do_ast_op(ast_op: ASTPart, op_count: &mut usize, ops: &mut Vec, va "<" => 22, "<=" => 23, "!" => { - ops.push(Operation { opcode: 24, arg1: Some(left_reg), arg2: Some(reg.register as i64), arg3: None, pos: op.pos as u32 }); + ops.push(Operation { opcode: 24, arg1: Some(left_reg), arg2: Some(reg.register as f64), arg3: None, pos: op.pos as u32 }); set_register(registers, RegisterState { id: reg.register, used: true, variable: 0, last_used: *op_count }); return reg.register; }, @@ -546,7 +546,7 @@ fn do_ast_op(ast_op: ASTPart, op_count: &mut usize, ops: &mut Vec, va process::exit(1); }, }; - ops.push(Operation { opcode, arg1: Some(left_reg), arg2: Some(right_reg as i64), arg3: Some(reg.register), pos: op.pos as u32 }); + ops.push(Operation { opcode, arg1: Some(left_reg), arg2: Some(right_reg as f64), arg3: Some(reg.register), pos: op.pos as u32 }); set_register(registers, RegisterState { id: reg.register, used: true, variable: 0, last_used: *op_count }); return reg.register; }, @@ -562,7 +562,7 @@ fn do_ast_op(ast_op: ASTPart, op_count: &mut usize, ops: &mut Vec, va ops.push(Operation { opcode: 8, arg1: Some(reg.id), arg2: None, arg3: None, pos: upd.pos as u32 }); set_register(registers, RegisterState { id: reg.id, used: false, variable: 0, last_used: 0 }); } - ops.push(Operation { opcode: 7, arg1: Some(value_reg), arg2: Some(get_variable_by_name(variables, &upd.variable, ops.len(), traceback).expect("Variable should exist").id as i64), arg3: None, pos: upd.pos as u32 }); + ops.push(Operation { opcode: 7, arg1: Some(value_reg), arg2: Some(get_variable_by_name(variables, &upd.variable, ops.len(), traceback).expect("Variable should exist").id as f64), arg3: None, pos: upd.pos as u32 }); set_register(registers, RegisterState { id: value_reg, used: true, variable: get_variable_by_name(variables, &upd.variable, ops.len(), traceback).expect("Variable should exist").id, last_used: *op_count }); garbage_collect_registers(registers); }, @@ -575,7 +575,7 @@ fn do_ast_op(ast_op: ASTPart, op_count: &mut usize, ops: &mut Vec, va let reg = do_ast_op(*asign.value, op_count, ops, variables, next_var_id, strings, next_string_id, functions, next_function_id, registers, ctx, traceback); variables.push(Variable { name: asign.variable.clone(), id: *next_var_id, start: ops.len(), end: 0, no_end: true }); *next_var_id += 1; - ops.push(Operation { opcode: 7, arg1: Some(reg), arg2: Some(get_variable_by_name(variables, &asign.variable, ops.len(), traceback).expect("Variable should exist").id as i64), arg3: None, pos: asign.pos as u32 }); + ops.push(Operation { opcode: 7, arg1: Some(reg), arg2: Some(get_variable_by_name(variables, &asign.variable, ops.len(), traceback).expect("Variable should exist").id as f64), arg3: None, pos: asign.pos as u32 }); set_register(registers, RegisterState { id: reg, used: true, variable: get_variable_by_name(variables, &asign.variable, ops.len(), traceback).expect("Variable should exist").id, last_used: *op_count }); garbage_collect_registers(registers); }, @@ -584,12 +584,12 @@ fn do_ast_op(ast_op: ASTPart, op_count: &mut usize, ops: &mut Vec, va if reg.unbind_before { ops.push(Operation { opcode: 8, arg1: Some(reg.register), arg2: None, arg3: None, pos: tbl.pos as u32 }); } - ops.push(Operation { opcode: 6, arg1: Some(reg.register), arg2: Some(1), arg3: None, pos: tbl.pos as u32 }); + ops.push(Operation { opcode: 6, arg1: Some(reg.register), arg2: Some(1 as f64), arg3: None, pos: tbl.pos as u32 }); set_register(registers, RegisterState { id: reg.register, used: true, variable: 0, last_used: *op_count }); for val in tbl.values { let key_reg = do_ast_op(val.key, op_count, ops, variables, next_var_id, strings, next_string_id, functions, next_function_id, registers, ctx, traceback); let value_reg = do_ast_op(val.value, op_count, ops, variables, next_var_id, strings, next_string_id, functions, next_function_id, registers, ctx, traceback); - ops.push(Operation { opcode: 31, arg1: Some(reg.register), arg2: Some(key_reg as i64), arg3: Some(value_reg), pos: tbl.pos as u32 }); + ops.push(Operation { opcode: 31, arg1: Some(reg.register), arg2: Some(key_reg as f64), arg3: Some(value_reg), pos: tbl.pos as u32 }); } return reg.register; }, @@ -600,7 +600,7 @@ fn do_ast_op(ast_op: ASTPart, op_count: &mut usize, ops: &mut Vec, va if reg.unbind_before { ops.push(Operation { opcode: 8, arg1: Some(reg.register), arg2: None, arg3: None, pos: tbl_get.pos as u32 }); } - ops.push(Operation { opcode: 30, arg1: Some(table_reg), arg2: Some(key_reg as i64), arg3: Some(reg.register), pos: tbl_get.pos as u32 }); + ops.push(Operation { opcode: 30, arg1: Some(table_reg), arg2: Some(key_reg as f64), arg3: Some(reg.register), pos: tbl_get.pos as u32 }); set_register(registers, RegisterState { id: reg.register, used: true, variable: 0, last_used: *op_count }); return reg.register; }, @@ -608,7 +608,7 @@ fn do_ast_op(ast_op: ASTPart, op_count: &mut usize, ops: &mut Vec, va let table_reg = do_ast_op(*tbl_set.table, op_count, ops, variables, next_var_id, strings, next_string_id, functions, next_function_id, registers, ctx, traceback); let key_reg = do_ast_op(*tbl_set.key, op_count, ops, variables, next_var_id, strings, next_string_id, functions, next_function_id, registers, ctx, traceback); let value_reg = do_ast_op(*tbl_set.value, op_count, ops, variables, next_var_id, strings, next_string_id, functions, next_function_id, registers, ctx, traceback); - ops.push(Operation { opcode: 31, arg1: Some(table_reg), arg2: Some(key_reg as i64), arg3: Some(value_reg), pos: tbl_set.pos as u32 }); + ops.push(Operation { opcode: 31, arg1: Some(table_reg), arg2: Some(key_reg as f64), arg3: Some(value_reg), pos: tbl_set.pos as u32 }); }, ASTPart::Import(impr) => { let fi = fs::read_to_string(&impr.path); @@ -633,8 +633,8 @@ fn do_ast_op(ast_op: ASTPart, op_count: &mut usize, ops: &mut Vec, va if reg.unbind_before { ops.push(Operation { opcode: 8, arg1: Some(reg.register), arg2: None, arg3: None, pos: impr.pos as u32 }); } - ops.push(Operation { opcode: 5, arg1: Some(reg.register), arg2: Some((*next_function_id-1) as i64), arg3: None, pos: impr.pos as u32 }); - ops.push(Operation { opcode: 27, arg1: Some(reg.register), arg2: Some(reg.register as i64), arg3: None, pos: impr.pos as u32 }); + ops.push(Operation { opcode: 5, arg1: Some(reg.register), arg2: Some((*next_function_id-1) as f64), arg3: None, pos: impr.pos as u32 }); + ops.push(Operation { opcode: 27, arg1: Some(reg.register), arg2: Some(reg.register as f64), arg3: None, pos: impr.pos as u32 }); set_register(registers, RegisterState { id: reg.register, used: true, variable: 0, last_used: *op_count }); return reg.register; }, @@ -783,7 +783,7 @@ fn compile_body(compiled: Compiled, fpos: &mut usize, ctx: &Context) -> (Vec op_bin.push_str(&num_to_bin(0, 4)); } if let Some(arg2) = ops.arg2 { - op_bin.push_str(&num_to_bin(arg2 as usize, 64)); + op_bin.push_str(&num_to_bin(arg2.to_bits() as usize, 64)); } else { op_bin.push_str(&num_to_bin(0, 64)); } diff --git a/src/decompiler.rs b/src/decompiler.rs index da294b9..aaffa16 100644 --- a/src/decompiler.rs +++ b/src/decompiler.rs @@ -13,7 +13,7 @@ pub struct DecompiledFunction { pub struct DecompiledOperation { pub opcode: u8, pub arg1: u8, - pub arg2: i64, + pub arg2: f64, pub arg3: u8, pub pos: usize, } @@ -100,22 +100,8 @@ fn bin_to_num(bin: String) -> usize { return result; } -fn bin_to_snum(bin: String) -> i64 { - let mut result: i64 = 0; - let mut is_negative = false; - for (i, c) in bin.chars().rev().enumerate() { - if c == '1' { - if i == bin.len() - 1 { - is_negative = true; - } else { - result += 1 << i; - } - } - } - if is_negative { - result = -result; - } - return result; +fn bin_to_snum(bin: String) -> f64 { + f64::from_bits(bin_to_num(bin) as u64) } fn load_func(data: Vec, offset: &mut usize) -> DecompiledFunction { diff --git a/src/enviroment.rs b/src/enviroment.rs index 38da895..7c88a05 100644 --- a/src/enviroment.rs +++ b/src/enviroment.rs @@ -321,7 +321,7 @@ fn kabel_keres(machine: &mut Machine, op: &DecompiledOperation, args: Vec s.value.clone(), _ => String::new() }; - return VMMemory::Number(VMMemoryNumber { value: str.len() as i64, variable_id: 0 }); + return VMMemory::Number(VMMemoryNumber { value: str.len() as f64, variable_id: 0 }); } fn szaft_ismeteld(machine: &mut Machine, op: &DecompiledOperation, args: Vec) -> VMMemory { arg_expect(&args, 0, "string", machine, op); @@ -459,7 +459,7 @@ fn szaft_szamma(machine: &mut Machine, op: &DecompiledOperation, args: Vec n.value as usize, _ => 0 }; - return VMMemory::Number(VMMemoryNumber { value: str.as_bytes()[num] as i64, variable_id: 0 }); + return VMMemory::Number(VMMemoryNumber { value: str.as_bytes()[num] as f64, variable_id: 0 }); } fn szaft_betuve(machine: &mut Machine, op: &DecompiledOperation, args: Vec) -> VMMemory { arg_expect(&args, 0, "number", machine, op); @@ -499,7 +499,7 @@ fn tabla_hozzaad(machine: &mut Machine, op: &DecompiledOperation, args: Vec) -> VMMemory { @@ -544,7 +544,7 @@ fn tabla_kulcsok(machine: &mut Machine, op: &DecompiledOperation, args: Vec Vec { while pos < splitted.len() { let nchar = splitted[pos]; pos += 1; - if !is_number(nchar) { + if !is_number(nchar) && nchar != "." { break; } num += nchar; diff --git a/src/parser.rs b/src/parser.rs index b36849a..c507b29 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -35,7 +35,7 @@ pub struct AstString { } #[derive(Debug, Clone, PartialEq)] pub struct AstNumber { - pub value: i64, + pub value: f64, pub pos: usize } #[derive(Debug, Clone, PartialEq)] @@ -418,7 +418,7 @@ fn read_table(input: &Vec, pos: &mut usize, ctx: &Context) -> ASTPart { tbl.push(TableValue { key: keyy, value: value, pos: input[*pos].pos }); } else { let value = read_exp(pos, input, &ends, &ends, ctx); - tbl.push(TableValue { key: ASTPart::Number(AstNumber { value: key, pos: 0 }), value: value, pos: input[*pos].pos }); + tbl.push(TableValue { key: ASTPart::Number(AstNumber { value: key as f64, pos: 0 }), value: value, pos: input[*pos].pos }); key += 1; } if input[*pos].typ == TokenType::SEPARATOR && input[*pos].value == "}" { @@ -461,7 +461,25 @@ fn read_exp(pos: &mut usize, input: &Vec, ends: &Vec, parse_ends: if token.typ == TokenType::STRING { expressions.push(ASTPart::String(AstString { value: token.value.clone(), pos: token.pos })); } else if token.typ == TokenType::NUMBER { - expressions.push(ASTPart::Number(AstNumber { value: token.value.parse().unwrap(), pos: token.pos })); + if expressions.len() == 1 { + match &expressions[0] { + ASTPart::Operation(op) => { + if op.operator == "+" || op.operator == "-" { + let mut nm = op.operator.clone(); + nm.push_str(&token.value); + expressions.push(ASTPart::Number(AstNumber { value: nm.parse().unwrap(), pos: token.pos })); + expressions.remove(0); + } else { + expressions.push(ASTPart::Number(AstNumber { value: token.value.parse().unwrap(), pos: token.pos })); + } + }, + _ => { + expressions.push(ASTPart::Number(AstNumber { value: token.value.parse().unwrap(), pos: token.pos })); + } + } + } else { + expressions.push(ASTPart::Number(AstNumber { value: token.value.parse().unwrap(), pos: token.pos })); + } } else if token.typ == TokenType::KEYWORD { if token.value == "piszv" { expressions.push(ASTPart::Boolean(AstBool { value: true, pos: token.pos })); diff --git a/src/virtualmachine.rs b/src/virtualmachine.rs index acb990f..6ff9e46 100644 --- a/src/virtualmachine.rs +++ b/src/virtualmachine.rs @@ -19,7 +19,7 @@ pub struct VMMemoryString { } #[derive(Debug, Clone)] pub struct VMMemoryNumber { - pub value: i64, + pub value: f64, pub variable_id: u32, } #[derive(Debug, Clone)] @@ -206,7 +206,7 @@ fn do_operation_operation(registers: &mut Vec, memory: &mut Vec { - if num2.value == 0 { + if num2.value == 0.0 { let err = create_error(&format!("Division by zero"), operation.pos, ErrorType::MathError, ErrorSubType::DivisionByZero); print_error(&err, &ctx); process::exit(1); @@ -214,7 +214,7 @@ fn do_operation_operation(registers: &mut Vec, memory: &mut Vec { - result = VMMemory::Number(VMMemoryNumber { value: num1.value.pow(num2.value as u32), variable_id: 0 }); + result = VMMemory::Number(VMMemoryNumber { value: num1.value.powf(num2.value), variable_id: 0 }); }, 15 => { result = VMMemory::Number(VMMemoryNumber { value: num1.value % num2.value, variable_id: 0 }); @@ -428,7 +428,7 @@ impl Machine { }, 4 => { //LDB - self.memory.push(VMMemory::Boolean(VMMemoryBoolean { value: operation.arg2 != 0, variable_id: 0 })); + self.memory.push(VMMemory::Boolean(VMMemoryBoolean { value: operation.arg2 != 0.0, variable_id: 0 })); set_register(&mut self.registers, Register { id: operation.arg1, pointer: self.memory.len() - 1 }); }, 5 => { @@ -454,7 +454,7 @@ impl Machine { }, 6 => { //LDN - if operation.arg2 > 0 { + if operation.arg2 > 0.0 { self.memory.push(VMMemory::Table(VMMemoryTable { values: vec![], variable_id: 0 })); set_register(&mut self.registers, Register { id: operation.arg1, pointer: self.memory.len() - 1 }); } else { diff --git a/test.asl b/test.asl index e12008f..426b530 100644 --- a/test.asl +++ b/test.asl @@ -1,8 +1 @@ -lőcsve test(a) { - ugass(a) -} - -test(97) -lőcsve(a) { - ugass(a) -}(98) \ No newline at end of file +ugass(3.3) \ No newline at end of file