diff --git a/dm_lib/std/core/array.dm b/dm_lib/std/core/array.dm new file mode 100644 index 0000000..5236e08 --- /dev/null +++ b/dm_lib/std/core/array.dm @@ -0,0 +1,5 @@ +ns std::core + +decl pub int Array { + length: Int +} diff --git a/dm_lib/std/core/print.dm b/dm_lib/std/core/print.dm new file mode 100644 index 0000000..0b5c401 --- /dev/null +++ b/dm_lib/std/core/print.dm @@ -0,0 +1,5 @@ +ns std::core + +decl pub fn print(message: Any) + +decl pub fn println(message: Any) diff --git a/src/vm/mod.rs b/src/vm/mod.rs index 902411d..c9a3e23 100644 --- a/src/vm/mod.rs +++ b/src/vm/mod.rs @@ -2,7 +2,8 @@ pub mod module; pub mod op_codes; pub mod platform; pub mod types; -mod util; +pub mod util; +pub mod values; use crate::vm::module::DmFunction; use crate::vm::platform::init_platform_functions; @@ -12,20 +13,11 @@ use std::collections::HashMap; use std::ops::Index; use types::DmType; use util::{get_32_le, get_64_le}; -use crate::vm::DmValue::{DmInt, DmLong, DmPointer, DmUnit}; +use values::DmValue; +use values::DmValue::*; pub type PlatformFunction = fn(args: Vec, &mut DmVirtualMachine) -> DmValue; -#[derive(Debug, Clone, Copy, PartialEq)] -pub enum DmValue { - DmInt(i32), - DmLong(i64), - DmDouble(f64), - DmPointer(*mut DmObject), - DmUnit, - DmException(*mut DmObject) -} - enum CallFrame { PlatformCall(PlatformCallFrame), DeimosCall(DeimosCallFrame) diff --git a/src/vm/platform/std_lib/core.rs b/src/vm/platform/std_lib/core.rs index 4847092..fa516a6 100644 --- a/src/vm/platform/std_lib/core.rs +++ b/src/vm/platform/std_lib/core.rs @@ -1,8 +1,6 @@ -use crate::vm::types::DmType; +use crate::vm::module::DmFunction; use crate::vm::DmValue::*; use crate::vm::{DmObject, DmValue, DmVirtualMachine}; -use DmType::*; -use crate::vm::module::DmFunction; fn get_method<'a>(ptr: *mut DmObject, name: String) -> Option<&'a DmFunction> { unsafe { @@ -10,9 +8,9 @@ fn get_method<'a>(ptr: *mut DmObject, name: String) -> Option<&'a DmFunction> { } } -fn get_rust_string_from_dm_object(ptr: *mut DmObject) -> String { +fn get_rust_string_from_dm_object(ptr: &*mut DmObject) -> String { unsafe { - let dm_object = Box::from_raw(ptr); + let dm_object = Box::from_raw(*ptr); format!("{}@{:?}", dm_object.object_type.name, dm_object.data) } } @@ -26,20 +24,26 @@ fn get_rust_string_from_dm_string(ptr: *mut DmObject) -> String { pub fn dm_print(args: Vec, vm: &mut DmVirtualMachine) -> DmValue { if args.len() != 1 { - return DmException(todo!("make an Exception object.")) + return DmUnit // TODO: make exception } - match args[0] { + match &args[0] { + DmByte(b) => { + print!("{}", *b); + } DmInt(i) => { - print!("{}", i); + print!("{}", *i); } DmLong(l) => { - print!("{}", l); + print!("{}", *l); } DmDouble(d) => { - print!("{}", d); + print!("{}", *d); + } + DmBoolean(b) => { + print!("{}", *b); } DmPointer(ptr) => { - if let Some(to_string) = get_method(ptr, String::from("to_string")) { + if let Some(to_string) = get_method(*ptr, String::from("to_string")) { let call_result = vm.call(&to_string, vec![args[0]]); match call_result { DmPointer(dm_string_ptr) => { @@ -54,12 +58,19 @@ pub fn dm_print(args: Vec, vm: &mut DmVirtualMachine) -> DmValue { print!("{}", get_rust_string_from_dm_object(ptr)); } }, + DmByteArray(bs) => { + + } + DmIntArray(is) => { + + } + DmLongArray(ls) => {} + DmDoubleArray(ds) => {} + DmBooleanArray(bs) => {} + DmPointerArray(ptrs) => {} DmUnit => { print!("Unit") }, - DmException(e) => { - print!("Exception") - } } DmUnit } diff --git a/src/vm/types.rs b/src/vm/types.rs index ecd8fc7..36ebf99 100644 --- a/src/vm/types.rs +++ b/src/vm/types.rs @@ -3,7 +3,12 @@ pub enum DmType { Int, Long, Double, + Boolean, Pointer, + IntArray, + LongArray, + DoubleArray, + BooleanArray, + PointerArray, Unit, - Exception } diff --git a/src/vm/values.rs b/src/vm/values.rs new file mode 100644 index 0000000..2d92c31 --- /dev/null +++ b/src/vm/values.rs @@ -0,0 +1,18 @@ +use crate::vm::DmObject; + +#[derive(Debug, Clone, PartialEq)] +pub enum DmValue { + DmByte(u8), + DmInt(i32), + DmLong(i64), + DmDouble(f64), + DmBoolean(bool), + DmPointer(*mut DmObject), + DmByteArray(Vec), + DmIntArray(Vec), + DmLongArray(Vec), + DmDoubleArray(Vec), + DmBooleanArray(Vec), + DmPointerArray(Vec<*mut DmObject>), + DmUnit, +} \ No newline at end of file