diff --git a/src/enviroment.rs b/src/enviroment.rs index c5ef1d1..0f00927 100644 --- a/src/enviroment.rs +++ b/src/enviroment.rs @@ -1,4 +1,4 @@ -use std::{collections::HashMap, fs, io::{BufReader, Read, Write}, net::{self, TcpListener, TcpStream}, process, thread::sleep, time::{Duration, UNIX_EPOCH}, vec}; +use std::{collections::HashMap, fs, io::{stdin, 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,6 +204,27 @@ 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); @@ -805,8 +826,8 @@ fn intezo_info(machine: &mut Machine, op: &DecompiledOperation, args: Vec exists, Err(e) => { - error(format!("Failed to check if file exists: {}", e), machine, op); - panic!() + error(format!("Failed to get file metadata: {}", e), machine, op); + return VMMemory::Null(VMMemoryNull { variable_id: 0 }); } }; let modf = match res.modified() { @@ -838,6 +859,113 @@ 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(); @@ -847,6 +975,8 @@ 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 { @@ -972,5 +1102,37 @@ 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 a127b70..1a24992 100644 --- a/src/main.rs +++ b/src/main.rs @@ -195,13 +195,14 @@ fn main() { let mut longest = 0; for op in &func.body { let op_name = operation_to_name(op.opcode); - let str = format!(" {}{} {}{} {}{} {} at {}", + let str = format!(" {}{} {}{} {}{} {}{} at {}", + " ".repeat(longest_cols[0] - op_name.len()), op_name, - " ".repeat(longest_cols[0] - op_name.len()), - op.arg1, " ".repeat(longest_cols[1] - op.arg1.to_string().len()), - op.arg2, + op.arg1, " ".repeat(longest_cols[2] - op.arg2.to_string().len()), + op.arg2, + " ".repeat(longest_cols[3] - op.arg3.to_string().len()), op.arg3, op.pos); longest = longest.max(str.len()+4); diff --git a/src/parser.rs b/src/parser.rs index 929ee50..3ec85e0 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(_) => { - stack.push(part); + output.push(part); }, ASTPart::VarRead(_) => { output.push(part); diff --git a/test.asl b/test.asl index 005e58d..de2e50f 100644 --- a/test.asl +++ b/test.asl @@ -1,18 +1,11 @@ -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 +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