diff --git a/src/compiler.rs b/src/compiler.rs index 6e98c67..5431925 100644 --- a/src/compiler.rs +++ b/src/compiler.rs @@ -103,7 +103,6 @@ fn allocate_register(registers: &Vec) -> AllocateResult { let mut oldest_register: u8 = 0; let mut oldest_temp: usize = 0; let mut oldest_temp_register: u8 = 0; - let mut tempc = 0; for register in registers { if ((register.last_used < oldest) || (oldest == 0)) && register.id != 0 { oldest = register.last_used; @@ -113,16 +112,13 @@ fn allocate_register(registers: &Vec) -> AllocateResult { oldest_temp = register.last_used; oldest_temp_register = register.id; } - if register.id != 0 && register.variable == 0 { - tempc += 1; - } } - if oldest_temp_register != 0 && tempc > 3 { + /*if oldest_temp_register != 0 { return AllocateResult { register: oldest_temp_register, unbind_before: false, }; - } + }*/ return AllocateResult { register: oldest_register, unbind_before: true, @@ -249,6 +245,7 @@ fn do_ast_op(ast_op: ASTPart, op_count: &mut usize, ops: &mut Vec, va return reg.register; }, ASTPart::Call(call) => { + let func = do_ast_op(*call.function, op_count, ops, variables, next_var_id, strings, next_string_id, functions, next_function_id, registers, ctx, traceback); for arg in call.args { let arg_reg = do_ast_op(arg, op_count, ops, variables, next_var_id, strings, next_string_id, functions, next_function_id, registers, ctx, traceback); ops.push(Operation { opcode: 28, arg1: Some(arg_reg), arg2: None, arg3: None, pos: call.pos as u32 }); @@ -257,7 +254,6 @@ fn do_ast_op(ast_op: ASTPart, op_count: &mut usize, ops: &mut Vec, va set_register(registers, RegisterState { id: arg_reg, used: false, variable: 0, last_used: 0 }); } } - let func = do_ast_op(*call.function, op_count, ops, variables, next_var_id, strings, next_string_id, functions, next_function_id, registers, ctx, traceback); let ret_reg = allocate_register(registers); if ret_reg.unbind_before { ops.push(Operation { opcode: 8, arg1: Some(ret_reg.register), arg2: None, arg3: None, pos: call.pos as u32 }); diff --git a/src/enviroment.rs b/src/enviroment.rs index 0f00927..c5ef1d1 100644 --- a/src/enviroment.rs +++ b/src/enviroment.rs @@ -1,4 +1,4 @@ -use std::{collections::HashMap, fs, io::{stdin, BufReader, Read, Write}, net::{self, TcpListener, TcpStream}, process, thread::sleep, time::{Duration, UNIX_EPOCH}, vec}; +use std::{collections::HashMap, fs, io::{BufReader, Read, Write}, net::{self, TcpListener, TcpStream}, process, thread::sleep, time::{Duration, UNIX_EPOCH}, vec}; use crate::{decompiler::DecompiledOperation, errors::{create_error, print_error, ErrorSubType, ErrorType}, virtualmachine::{Machine, TableValue, VMMemory, VMMemoryBoolean, VMMemoryNativeFunction, VMMemoryNull, VMMemoryNumber, VMMemoryString, VMMemoryTable}}; @@ -204,27 +204,6 @@ fn bimbabemb(machine: &mut Machine, op: &DecompiledOperation, args: Vec) -> VMMemory { - let now = match UNIX_EPOCH.elapsed() { - Ok(duration) => duration.as_millis() as f64, - Err(e) => { - error(format!("Failed to get elapsed time: {}", e), machine, op); - 0.0 - } - }; - return VMMemory::Number(VMMemoryNumber { value: now, variable_id: 0 }); -} -fn joink(machine: &mut Machine, op: &DecompiledOperation, _args: Vec) -> VMMemory { - let mut str = String::new(); - match stdin().read_line(&mut str) { - Err(e) => { - error(format!("Failed to read from stdin: {}", e), machine, op); - return VMMemory::Null(VMMemoryNull { variable_id: 0 }); - }, - _ => {} - }; - return VMMemory::String(VMMemoryString { value: str, variable_id: 0 }); -} fn nerd_abs(machine: &mut Machine, op: &DecompiledOperation, args: Vec) -> VMMemory { arg_expect(&args, 0, "number", machine, op); @@ -826,8 +805,8 @@ fn intezo_info(machine: &mut Machine, op: &DecompiledOperation, args: Vec exists, Err(e) => { - error(format!("Failed to get file metadata: {}", e), machine, op); - return VMMemory::Null(VMMemoryNull { variable_id: 0 }); + error(format!("Failed to check if file exists: {}", e), machine, op); + panic!() } }; let modf = match res.modified() { @@ -859,113 +838,6 @@ fn intezo_info(machine: &mut Machine, op: &DecompiledOperation, args: Vec) -> VMMemory { - arg_expect(&args, 0, "string", machine, op); - let str = match &args[0] { - VMMemory::String(s) => s.value.clone(), - _ => String::new() - }; - match fs::create_dir_all(str) { - Err(e) => { - error(format!("Failed to create directory: {}", e), machine, op); - }, - _ => {} - } - return VMMemory::Null(VMMemoryNull { variable_id: 0 }); -} -fn intezo_mappattorol(machine: &mut Machine, op: &DecompiledOperation, args: Vec) -> VMMemory { - arg_expect(&args, 0, "string", machine, op); - let str = match &args[0] { - VMMemory::String(s) => s.value.clone(), - _ => String::new() - }; - let res = match fs::exists(&str) { - Ok(exists) => exists, - Err(_) => { - error(format!("Folder does not exist"), machine, op); - return VMMemory::Null(VMMemoryNull { variable_id: 0 }); - } - }; - if !res { - error(format!("Folder does not exist"), machine, op); - } - match fs::remove_dir_all(&str) { - Err(e) => { - error(format!("Failed to remove directory: {}", e), machine, op); - }, - _ => {} - }; - return VMMemory::Null(VMMemoryNull { variable_id: 0 }); -} -fn intezo_fajlttorol(machine: &mut Machine, op: &DecompiledOperation, args: Vec) -> VMMemory { - arg_expect(&args, 0, "string", machine, op); - let str = match &args[0] { - VMMemory::String(s) => s.value.clone(), - _ => String::new() - }; - let res = match fs::exists(&str) { - Ok(exists) => exists, - Err(_) => { - error(format!("File does not exist"), machine, op); - return VMMemory::Null(VMMemoryNull { variable_id: 0 }); - } - }; - if !res { - error(format!("File does not exist"), machine, op); - } - match fs::remove_file(&str) { - Err(e) => { - error(format!("Failed to remove file: {}", e), machine, op); - }, - _ => {} - }; - return VMMemory::Null(VMMemoryNull { variable_id: 0 }); -} -fn intezo_olvass(machine: &mut Machine, op: &DecompiledOperation, args: Vec) -> VMMemory { - arg_expect(&args, 0, "string", machine, op); - let str = match &args[0] { - VMMemory::String(s) => s.value.clone(), - _ => String::new() - }; - let res = match fs::exists(&str) { - Ok(exists) => exists, - Err(_) => { - error(format!("File does not exist"), machine, op); - return VMMemory::Null(VMMemoryNull { variable_id: 0 }); - } - }; - if !res { - error(format!("File does not exist"), machine, op); - } - let data = match fs::read_to_string(&str) { - Ok(data) => data, - Err(e) => { - error(format!("Failed to read file: {}", e), machine, op); - return VMMemory::Null(VMMemoryNull { variable_id: 0 }); - } - }; - return VMMemory::String(VMMemoryString { value: data, variable_id: 0 }); -} -fn intezo_irj(machine: &mut Machine, op: &DecompiledOperation, args: Vec) -> VMMemory { - arg_expect(&args, 0, "string", machine, op); - arg_expect(&args, 1, "string", machine, op); - let str = match &args[0] { - VMMemory::String(s) => s.value.clone(), - _ => String::new() - }; - let str2 = match &args[1] { - VMMemory::String(s) => s.value.clone(), - _ => String::new() - }; - match fs::write(&str, str2) { - Err(e) => { - error(format!("Failed to write file: {}", e), machine, op); - return VMMemory::Null(VMMemoryNull { variable_id: 0 }); - }, - _ => {} - }; - return VMMemory::Null(VMMemoryNull { variable_id: 0 }); -} pub fn generate() -> HashMap { let mut mem: HashMap = HashMap::new(); @@ -975,8 +847,6 @@ pub fn generate() -> HashMap { mem.insert(String::from("csömör"), VMMemory::NativeFunction(VMMemoryNativeFunction { func: csomor, variable_id: 0 })); mem.insert(String::from("tarh"), VMMemory::NativeFunction(VMMemoryNativeFunction { func: tarh, variable_id: 0 })); mem.insert(String::from("bimbabemb"), VMMemory::NativeFunction(VMMemoryNativeFunction { func: bimbabemb, variable_id: 0 })); - mem.insert(String::from("mennyi az idő"), VMMemory::NativeFunction(VMMemoryNativeFunction { func: mennyiazido, variable_id: 0 })); - mem.insert(String::from("joink"), VMMemory::NativeFunction(VMMemoryNativeFunction { func: joink, variable_id: 0 })); let nerd: Vec = vec![ TableValue { @@ -1102,37 +972,5 @@ pub fn generate() -> HashMap { ]; mem.insert(String::from("szenvedés"), VMMemory::Table(VMMemoryTable { values: szenvedes, variable_id: 0 })); - let intezo: Vec = vec![ - TableValue { - key: VMMemory::String(VMMemoryString { value: String::from("létezik"), variable_id: 0 }), - value: VMMemory::NativeFunction(VMMemoryNativeFunction { func: intezo_letezik, variable_id: 0 }), - }, - TableValue { - key: VMMemory::String(VMMemoryString { value: String::from("infó"), variable_id: 0 }), - value: VMMemory::NativeFunction(VMMemoryNativeFunction { func: intezo_info, variable_id: 0 }), - }, - TableValue { - key: VMMemory::String(VMMemoryString { value: String::from("mappít"), variable_id: 0 }), - value: VMMemory::NativeFunction(VMMemoryNativeFunction { func: intezo_mappit, variable_id: 0 }), - }, - TableValue { - key: VMMemory::String(VMMemoryString { value: String::from("mappát töröl"), variable_id: 0 }), - value: VMMemory::NativeFunction(VMMemoryNativeFunction { func: intezo_mappattorol, variable_id: 0 }), - }, - TableValue { - key: VMMemory::String(VMMemoryString { value: String::from("fájlt töröl"), variable_id: 0 }), - value: VMMemory::NativeFunction(VMMemoryNativeFunction { func: intezo_fajlttorol, variable_id: 0 }), - }, - TableValue { - key: VMMemory::String(VMMemoryString { value: String::from("olvass"), variable_id: 0 }), - value: VMMemory::NativeFunction(VMMemoryNativeFunction { func: intezo_olvass, variable_id: 0 }), - }, - TableValue { - key: VMMemory::String(VMMemoryString { value: String::from("írj"), variable_id: 0 }), - value: VMMemory::NativeFunction(VMMemoryNativeFunction { func: intezo_irj, variable_id: 0 }), - }, - ]; - mem.insert(String::from("intéző"), VMMemory::Table(VMMemoryTable { values: intezo, variable_id: 0 })); - return mem; } \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 1a24992..a127b70 100644 --- a/src/main.rs +++ b/src/main.rs @@ -195,14 +195,13 @@ fn main() { let mut longest = 0; for op in &func.body { let op_name = operation_to_name(op.opcode); - let str = format!(" {}{} {}{} {}{} {}{} at {}", - " ".repeat(longest_cols[0] - op_name.len()), + let str = format!(" {}{} {}{} {}{} {} at {}", op_name, - " ".repeat(longest_cols[1] - op.arg1.to_string().len()), + " ".repeat(longest_cols[0] - op_name.len()), op.arg1, - " ".repeat(longest_cols[2] - op.arg2.to_string().len()), + " ".repeat(longest_cols[1] - op.arg1.to_string().len()), op.arg2, - " ".repeat(longest_cols[3] - op.arg3.to_string().len()), + " ".repeat(longest_cols[2] - op.arg2.to_string().len()), op.arg3, op.pos); longest = longest.max(str.len()+4); diff --git a/src/parser.rs b/src/parser.rs index 3ec85e0..929ee50 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -213,7 +213,7 @@ fn shunt(input: Vec, ctx: &Context) -> ASTPart { output.push(part); }, ASTPart::Call(_) => { - output.push(part); + stack.push(part); }, ASTPart::VarRead(_) => { output.push(part); diff --git a/test.asl b/test.asl index de2e50f..005e58d 100644 --- a/test.asl +++ b/test.asl @@ -1,11 +1,18 @@ -gethelj time = mennyi az idő() -ugass(time) -gethelj bemenet = joink() -ugass(bemenet) -gethelj time2 = mennyi az idő() -gethelj msg = szaft"Took: "szaft+bimbabemb(time2-time, szaft"string"szaft)+szaft"ms"szaft -ugass(msg) - -ugass(intéző.infó(szaft"test.asl"szaft)) -ugass(intéző.olvass(szaft"test.asl"szaft)) -ugass(intéző.írj(szaft"bimbabemb.txt"szaft,szaft"óóóóó bimbabemb!"szaft)) \ No newline at end of file +gethelj a = 125 +gethelj b = 567 +gethelj c = 8765 +gethelj d = 123 +gethelj e = 123 +gethelj f = 123 +gethelj g = 123 +gethelj h = 123 +gethelj i = 123 +gethelj j = 123 +gethelj k = 123 +gethelj l = 123 +gethelj m = 123 +gethelj n = 123 +gethelj o = 123 +gethelj p = 123 +gethelj q = 123 +ugass(a+b+c) \ No newline at end of file