added floats, negative numbers
This commit is contained in:
parent
8aee85680a
commit
cef2217c9e
7 changed files with 79 additions and 82 deletions
|
@ -7,7 +7,7 @@ const ASXVERSION: [u8; 3] = [0,1,0];
|
|||
pub struct Operation {
|
||||
pub opcode: u8,
|
||||
pub arg1: Option<u8>,
|
||||
pub arg2: Option<i64>,
|
||||
pub arg2: Option<f64>,
|
||||
pub arg3: Option<u8>,
|
||||
pub pos: u32,
|
||||
}
|
||||
|
@ -179,7 +179,7 @@ fn do_ast_op(ast_op: ASTPart, op_count: &mut usize, ops: &mut Vec<Operation>, 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<Operation>, 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<Operation>, 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<Operation>, 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<Operation>, 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<Operation>, 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<Operation>, 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<Operation>, 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<Operation>, 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<Operation>, 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<Operation>, 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<Operation>, 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<Operation>, 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<usize> = vec![];
|
||||
|
@ -418,11 +418,11 @@ fn do_ast_op(ast_op: ASTPart, op_count: &mut usize, ops: &mut Vec<Operation>, 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<Operation>, 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<Operation>, 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<Operation>, 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<usize> = vec![];
|
||||
|
@ -482,11 +482,11 @@ fn do_ast_op(ast_op: ASTPart, op_count: &mut usize, ops: &mut Vec<Operation>, 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<Operation>, 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<Operation>, 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<Operation>, 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<Operation>, 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<Operation>, 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<Operation>, 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<Operation>, 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<Operation>, 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<Operation>, 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<Operation>, 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<u8>
|
|||
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));
|
||||
}
|
||||
|
|
|
@ -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<u8>, offset: &mut usize) -> DecompiledFunction {
|
||||
|
|
|
@ -321,7 +321,7 @@ fn kabel_keres(machine: &mut Machine, op: &DecompiledOperation, args: Vec<VMMemo
|
|||
},
|
||||
TableValue {
|
||||
key: VMMemory::String(VMMemoryString { value: String::from("id"), variable_id: 0 }),
|
||||
value: VMMemory::Number(VMMemoryNumber { value: (machine.storage.len()-1) as i64, variable_id: 0 }),
|
||||
value: VMMemory::Number(VMMemoryNumber { value: (machine.storage.len()-1) as f64, variable_id: 0 }),
|
||||
}
|
||||
];
|
||||
return VMMemory::Table(VMMemoryTable { values: ret_table, variable_id: 0 });
|
||||
|
@ -348,7 +348,7 @@ fn kabel_halgass(machine: &mut Machine, op: &DecompiledOperation, args: Vec<VMMe
|
|||
},
|
||||
TableValue {
|
||||
key: VMMemory::String(VMMemoryString { value: String::from("id"), variable_id: 0 }),
|
||||
value: VMMemory::Number(VMMemoryNumber { value: (machine.storage.len()-1) as i64, variable_id: 0 }),
|
||||
value: VMMemory::Number(VMMemoryNumber { value: (machine.storage.len()-1) as f64, variable_id: 0 }),
|
||||
}
|
||||
];
|
||||
return VMMemory::Table(VMMemoryTable { values: ret_table, variable_id: 0 });
|
||||
|
@ -376,7 +376,7 @@ fn szaft_hossz(machine: &mut Machine, op: &DecompiledOperation, args: Vec<VMMemo
|
|||
VMMemory::String(s) => 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>) -> VMMemory {
|
||||
arg_expect(&args, 0, "string", machine, op);
|
||||
|
@ -459,7 +459,7 @@ fn szaft_szamma(machine: &mut Machine, op: &DecompiledOperation, args: Vec<VMMem
|
|||
VMMemory::Number(n) => 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>) -> VMMemory {
|
||||
arg_expect(&args, 0, "number", machine, op);
|
||||
|
@ -499,7 +499,7 @@ fn tabla_hozzaad(machine: &mut Machine, op: &DecompiledOperation, args: Vec<VMMe
|
|||
}
|
||||
}
|
||||
}
|
||||
tbl.values.push(TableValue { key: VMMemory::Number(VMMemoryNumber { value: next_index as i64, variable_id: 0 }), value: val.clone() });
|
||||
tbl.values.push(TableValue { key: VMMemory::Number(VMMemoryNumber { value: next_index as f64, variable_id: 0 }), value: val.clone() });
|
||||
return VMMemory::Table(VMMemoryTable { values: tbl.values.clone(), variable_id: 0 });
|
||||
}
|
||||
fn tabla_torol(machine: &mut Machine, op: &DecompiledOperation, args: Vec<VMMemory>) -> VMMemory {
|
||||
|
@ -544,7 +544,7 @@ fn tabla_kulcsok(machine: &mut Machine, op: &DecompiledOperation, args: Vec<VMMe
|
|||
variable_id: 0,
|
||||
};
|
||||
for kv in &tbl.values {
|
||||
keys.values.push(TableValue { key: VMMemory::Number(VMMemoryNumber { value: keys.values.len() as i64, variable_id: 0 }), value: kv.key.clone() });
|
||||
keys.values.push(TableValue { key: VMMemory::Number(VMMemoryNumber { value: keys.values.len() as f64, variable_id: 0 }), value: kv.key.clone() });
|
||||
}
|
||||
return VMMemory::Table(keys);
|
||||
}
|
||||
|
|
|
@ -237,7 +237,7 @@ pub fn lex(input: String, ctx: &Context) -> Vec<Token> {
|
|||
while pos < splitted.len() {
|
||||
let nchar = splitted[pos];
|
||||
pos += 1;
|
||||
if !is_number(nchar) {
|
||||
if !is_number(nchar) && nchar != "." {
|
||||
break;
|
||||
}
|
||||
num += nchar;
|
||||
|
|
|
@ -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<Token>, 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<Token>, ends: &Vec<Token>, 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 }));
|
||||
|
|
|
@ -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<Register>, memory: &mut Vec<VMMemo
|
|||
result = VMMemory::Number(VMMemoryNumber { value: num1.value * num2.value, variable_id: 0 });
|
||||
},
|
||||
13 => {
|
||||
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<Register>, memory: &mut Vec<VMMemo
|
|||
result = VMMemory::Number(VMMemoryNumber { value: num1.value / num2.value, variable_id: 0 });
|
||||
},
|
||||
14 => {
|
||||
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 {
|
||||
|
|
9
test.asl
9
test.asl
|
@ -1,8 +1 @@
|
|||
lőcsve test(a) {
|
||||
ugass(a)
|
||||
}
|
||||
|
||||
test(97)
|
||||
lőcsve(a) {
|
||||
ugass(a)
|
||||
}(98)
|
||||
ugass(3.3)
|
Loading…
Add table
Add a link
Reference in a new issue