diff --git a/src/enviroment.rs b/src/enviroment.rs index 08c9a4f..c5ef1d1 100644 --- a/src/enviroment.rs +++ b/src/enviroment.rs @@ -1,4 +1,4 @@ -use std::{collections::HashMap, io::{BufReader, Read, Write}, net::{self, TcpListener, TcpStream}, process, thread::sleep, time::Duration, 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}}; @@ -739,6 +739,35 @@ fn tabla_kulcsok(machine: &mut Machine, op: &DecompiledOperation, args: Vec) -> VMMemory { + arg_expect(&args, 0, "table", machine, op); + let tbl = match &args[0] { + VMMemory::Table(tbl) => tbl.clone(), + _ => { + error(String::from("Expected a table"), machine, op); + return VMMemory::Null(VMMemoryNull { variable_id: 0 }); + } + }; + let mut vals = VMMemoryTable { + values: vec![], + variable_id: 0, + }; + for kv in &tbl.values { + vals.values.push(TableValue { key: VMMemory::Number(VMMemoryNumber { value: vals.values.len() as f64, variable_id: 0 }), value: kv.value.clone() }); + } + return VMMemory::Table(vals); +} +fn tabla_hossz(machine: &mut Machine, op: &DecompiledOperation, args: Vec) -> VMMemory { + arg_expect(&args, 0, "table", machine, op); + let tbl = match &args[0] { + VMMemory::Table(tbl) => tbl.clone(), + _ => { + error(String::from("Expected a table"), machine, op); + return VMMemory::Null(VMMemoryNull { variable_id: 0 }); + } + }; + return VMMemory::Number(VMMemoryNumber { value: tbl.values.len() as f64, variable_id: 0 }); +} fn szenvedes_vege(machine: &mut Machine, op: &DecompiledOperation, args: Vec) -> VMMemory { arg_expect(&args, 0, "number", machine, op); @@ -752,6 +781,64 @@ fn szenvedes_vege(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(e) => { + error(format!("Failed to check if file exists: {}", e), machine, op); + false + } + }; + return VMMemory::Boolean(VMMemoryBoolean { value: res, variable_id: 0 }); +} +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() + }; + let res = match fs::metadata(str) { + Ok(exists) => exists, + Err(e) => { + error(format!("Failed to check if file exists: {}", e), machine, op); + panic!() + } + }; + let modf = match res.modified() { + Ok(time) => time.duration_since(UNIX_EPOCH).unwrap().as_millis() as f64, + Err(e) => { + error(format!("Failed to get file modification time: {}", e), machine, op); + 0.0 + } + }; + let creatf = match res.created() { + Ok(time) => time.duration_since(UNIX_EPOCH).unwrap().as_millis() as f64, + Err(e) => { + error(format!("Failed to get file modification time: {}", e), machine, op); + 0.0 + } + }; + let mut to_tbl: HashMap = HashMap::new(); + to_tbl.insert(String::from("mappa-e"), VMMemory::Boolean(VMMemoryBoolean { value: res.is_dir(), variable_id: 0 })); + to_tbl.insert(String::from("fájl-e"), VMMemory::Boolean(VMMemoryBoolean { value: res.is_file(), variable_id: 0 })); + to_tbl.insert(String::from("szerkesztve"), VMMemory::Number(VMMemoryNumber { value: modf, variable_id: 0 })); + to_tbl.insert(String::from("létrehozva"), VMMemory::Number(VMMemoryNumber { value: creatf, variable_id: 0 })); + to_tbl.insert(String::from("hossz"), VMMemory::Number(VMMemoryNumber { value: res.len() as f64, variable_id: 0 })); + let mut res_tbl = VMMemoryTable { + values: vec![], + variable_id: 0, + }; + for (k, v) in to_tbl { + set_mem_tbl_val(&mut res_tbl, VMMemory::String(VMMemoryString { value: k, variable_id: 0 }), v); + } + return VMMemory::Table(res_tbl); +} + pub fn generate() -> HashMap { let mut mem: HashMap = HashMap::new(); @@ -865,6 +952,14 @@ pub fn generate() -> HashMap { TableValue { key: VMMemory::String(VMMemoryString { value: String::from("kulcsok"), variable_id: 0 }), value: VMMemory::NativeFunction(VMMemoryNativeFunction { func: tabla_kulcsok, variable_id: 0 }), + }, + TableValue { + key: VMMemory::String(VMMemoryString { value: String::from("értékek"), variable_id: 0 }), + value: VMMemory::NativeFunction(VMMemoryNativeFunction { func: tabla_ertekek, variable_id: 0 }), + }, + TableValue { + key: VMMemory::String(VMMemoryString { value: String::from("hossz"), variable_id: 0 }), + value: VMMemory::NativeFunction(VMMemoryNativeFunction { func: tabla_hossz, variable_id: 0 }), } ]; mem.insert(String::from("tábla"), VMMemory::Table(VMMemoryTable { values: table, variable_id: 0 })); diff --git a/src/main.rs b/src/main.rs index bf38df7..a127b70 100644 --- a/src/main.rs +++ b/src/main.rs @@ -10,7 +10,7 @@ mod virtualmachine; mod errors; mod decompiler; -const CLIVER: [u8; 3] = [0, 2, 0]; +const CLIVER: [u8; 3] = [0, 3, 0]; #[derive(Clone)] struct Context {