Add array types/values and misc. work.
This commit is contained in:
parent
b90111dfd5
commit
0dd58cdfdc
5
dm_lib/std/core/array.dm
Normal file
5
dm_lib/std/core/array.dm
Normal file
@ -0,0 +1,5 @@
|
||||
ns std::core
|
||||
|
||||
decl pub int Array {
|
||||
length: Int
|
||||
}
|
5
dm_lib/std/core/print.dm
Normal file
5
dm_lib/std/core/print.dm
Normal file
@ -0,0 +1,5 @@
|
||||
ns std::core
|
||||
|
||||
decl pub fn print(message: Any)
|
||||
|
||||
decl pub fn println(message: Any)
|
@ -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<DmValue>, &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)
|
||||
|
@ -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<DmValue>, vm: &mut DmVirtualMachine) -> DmValue {
|
||||
if args.len() != 1 {
|
||||
return DmException(todo!("make an Exception object."))
|
||||
return DmUnit // TODO: make exception
|
||||
}
|
||||
match &args[0] {
|
||||
DmByte(b) => {
|
||||
print!("{}", *b);
|
||||
}
|
||||
match args[0] {
|
||||
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<DmValue>, 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
|
||||
}
|
||||
|
@ -3,7 +3,12 @@ pub enum DmType {
|
||||
Int,
|
||||
Long,
|
||||
Double,
|
||||
Boolean,
|
||||
Pointer,
|
||||
IntArray,
|
||||
LongArray,
|
||||
DoubleArray,
|
||||
BooleanArray,
|
||||
PointerArray,
|
||||
Unit,
|
||||
Exception
|
||||
}
|
||||
|
18
src/vm/values.rs
Normal file
18
src/vm/values.rs
Normal file
@ -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<u8>),
|
||||
DmIntArray(Vec<i32>),
|
||||
DmLongArray(Vec<i64>),
|
||||
DmDoubleArray(Vec<f64>),
|
||||
DmBooleanArray(Vec<bool>),
|
||||
DmPointerArray(Vec<*mut DmObject>),
|
||||
DmUnit,
|
||||
}
|
Loading…
Reference in New Issue
Block a user