diff --git a/Cargo.toml b/Cargo.toml index 951ab70..09c55c4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,6 +5,6 @@ edition = "2021" [[bin]] name = "dmc" -path = "src/bin/compiler" +path = "src/bin/compiler/main.rs" [dependencies] diff --git a/src/bin/compiler/main.rs b/src/bin/compiler/main.rs index 093b5f4..476d7dd 100644 --- a/src/bin/compiler/main.rs +++ b/src/bin/compiler/main.rs @@ -1,6 +1,6 @@ use deimos::lexer::tokenize; -use std::process::exit; use deimos::parser::parse; +use std::process::exit; fn main() { let src = String::from("print 42"); @@ -19,4 +19,4 @@ fn main() { let compilation_unit = parse_result.unwrap(); println!("{:?}", compilation_unit); // TODO: compilation_unit to DmModule -} \ No newline at end of file +} diff --git a/src/lexer/mod.rs b/src/lexer/mod.rs index d39a23e..fac180d 100644 --- a/src/lexer/mod.rs +++ b/src/lexer/mod.rs @@ -80,10 +80,11 @@ pub fn tokenize(input: &String) -> Result, String> { let mut buffer = String::new(); buffer.push(c); while let Some(num_char) = peekable.next_if(|c| { - c.is_digit(10) || match c { - '_' | 'x' | 'L' | 'd' => true, - _ => false, - } + c.is_digit(10) + || match c { + '_' | 'x' | 'L' | 'd' => true, + _ => false, + } }) { buffer.push(num_char); } diff --git a/src/lib.rs b/src/lib.rs index 5e5580c..b301d39 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,3 +1,3 @@ pub mod lexer; -pub mod vm; pub mod parser; +pub mod vm; diff --git a/src/parser/mod.rs b/src/parser/mod.rs index 2fe77cf..bfe15d0 100644 --- a/src/parser/mod.rs +++ b/src/parser/mod.rs @@ -5,4 +5,4 @@ use crate::parser::types::AstNode; pub fn parse(tokens: &Vec) -> Result { todo!() -} \ No newline at end of file +} diff --git a/src/vm/mod.rs b/src/vm/mod.rs index aebec28..7398436 100644 --- a/src/vm/mod.rs +++ b/src/vm/mod.rs @@ -1,23 +1,23 @@ mod module; mod op_codes; -mod util; mod platform; mod types; +mod util; +use crate::vm::module::DmFunction; use op_codes::*; use platform::PlatformCallFrame; use std::alloc::{alloc_zeroed, dealloc, Layout}; use std::collections::HashMap; use types::DmType; use util::{get_32_le, get_64_le}; -use crate::vm::module::DmFunction; -pub type PlatformFunction = fn (&mut PlatformCallFrame) -> Result, String>; +pub type PlatformFunction = fn(&mut PlatformCallFrame) -> Result, String>; pub struct DmVirtualMachine { registers: Vec, register_types: Vec, - platform_functions: HashMap + platform_functions: HashMap, } impl DmVirtualMachine { @@ -25,7 +25,7 @@ impl DmVirtualMachine { &self, dm_function: &DmFunction, args: Vec, - arg_types: Vec + arg_types: Vec, ) -> Result<(u64, DmType), String> { run(&dm_function.byte_code, todo!(), todo!()); todo!() @@ -36,7 +36,7 @@ pub struct DmObjectType { name: String, properties: HashMap, fields: HashMap, - methods: HashMap + methods: HashMap, } pub struct DmObject { @@ -91,7 +91,7 @@ pub fn run(code: &Vec, registers: &mut Vec, register_types: &mut Vec Result { // TODO: extract module name - // Holder for Symbols we will extract from the symbol table bytes let mut symbols: Vec = Vec::new(); @@ -160,7 +159,7 @@ fn read_as_u64(bytes: &[u8]) -> u64 { let max_shift = (bytes.len() - 1) * 8; for i in 0..bytes.len() { result |= (bytes[i] as u64) << (max_shift - i * 8); - }; + } result } diff --git a/src/vm/op_codes.rs b/src/vm/op_codes.rs index 7b0bdde..01a2ecd 100644 --- a/src/vm/op_codes.rs +++ b/src/vm/op_codes.rs @@ -42,7 +42,7 @@ pub const PLATFORM_CALL: u8 = 0x10; macro_rules! push_number { ( $dest: expr, $num: expr ) => { push_bytes!($dest, $num.to_le_bytes()) - } + }; } macro_rules! push_string { diff --git a/src/vm/platform/mod.rs b/src/vm/platform/mod.rs index 5fd67b8..4451f28 100644 --- a/src/vm/platform/mod.rs +++ b/src/vm/platform/mod.rs @@ -1,17 +1,17 @@ -use std::collections::HashMap; -use crate::vm::{DmVirtualMachine, PlatformFunction}; use crate::vm::platform::std_lib::core::dm_print; +use crate::vm::{DmVirtualMachine, PlatformFunction}; +use std::collections::HashMap; mod std_lib; pub struct PlatformCallFrame { args: Vec, arg_types: Vec, - vm: DmVirtualMachine + vm: DmVirtualMachine, } pub fn init_platform_functions() -> HashMap { - let mut m: HashMap = HashMap::new(); - m.insert(String::from("std::core::print"), dm_print); - m -} \ No newline at end of file + let mut fns: HashMap = HashMap::new(); + fns.insert(String::from("std::core::print"), dm_print); + fns +} diff --git a/src/vm/platform/std_lib/core.rs b/src/vm/platform/std_lib/core.rs index af809b2..61bb57b 100644 --- a/src/vm/platform/std_lib/core.rs +++ b/src/vm/platform/std_lib/core.rs @@ -4,7 +4,7 @@ use crate::vm::DmObject; use DmType::*; pub struct DmString { - data: String + data: String, } pub fn dm_print(frame: &mut PlatformCallFrame) -> Result, String> { @@ -23,8 +23,15 @@ pub fn dm_print(frame: &mut PlatformCallFrame) -> Result, } Pointer => unsafe { let dm_object = Box::from_raw(frame.args[0] as *mut DmObject); - if let Some(&ref to_string_method) = dm_object.object_type.methods.get(&String::from("to_string")) { - let call_result = frame.vm.call(to_string_method, vec![frame.args[0]], vec![Pointer]); + if let Some(&ref to_string_method) = dm_object + .object_type + .methods + .get(&String::from("to_string")) + { + let call_result = + frame + .vm + .call(to_string_method, vec![frame.args[0]], vec![Pointer]); if call_result.is_err() { return Err(call_result.unwrap_err()); } @@ -35,8 +42,7 @@ pub fn dm_print(frame: &mut PlatformCallFrame) -> Result, } else { print!("{}@{:?}", dm_object.object_type.name, dm_object.data); } - } + }, } Ok(None) } - diff --git a/src/vm/platform/std_lib/mod.rs b/src/vm/platform/std_lib/mod.rs index 689cad3..5a7ca06 100644 --- a/src/vm/platform/std_lib/mod.rs +++ b/src/vm/platform/std_lib/mod.rs @@ -1 +1 @@ -pub mod core; \ No newline at end of file +pub mod core; diff --git a/src/vm/types.rs b/src/vm/types.rs index 2c3ecab..09cad83 100644 --- a/src/vm/types.rs +++ b/src/vm/types.rs @@ -4,4 +4,4 @@ pub enum DmType { Long, Double, Pointer, -} \ No newline at end of file +}