Basic work on core lib and impl.

This commit is contained in:
Jesse Brault 2026-08-27 11:15:17 -05:00
parent b8f2620279
commit f0f377e44e
12 changed files with 161 additions and 1 deletions

7
Cargo.lock generated
View File

@ -119,6 +119,13 @@ dependencies = [
"dvm-lib", "dvm-lib",
] ]
[[package]]
name = "dm-core-impl"
version = "0.1.0"
dependencies = [
"dvm-lib",
]
[[package]] [[package]]
name = "dm-std-lib" name = "dm-std-lib"
version = "0.1.0" version = "0.1.0"

View File

@ -1,3 +1,3 @@
[workspace] [workspace]
resolver = "3" resolver = "3"
members = ["dm", "dm-std-lib", "dmc-lib", "dvm-lib", "e2e-tests"] members = ["dm", "dm-core-impl", "dm-std-lib", "dmc-lib", "dvm-lib", "e2e-tests"]

7
dm-core-impl/Cargo.toml Normal file
View File

@ -0,0 +1,7 @@
[package]
name = "dm-core-impl"
version = "0.1.0"
edition = "2024"
[dependencies]
dvm-lib = { path = "../dvm-lib" }

31
dm-core-impl/src/error.rs Normal file
View File

@ -0,0 +1,31 @@
use std::error::Error;
use std::fmt::{Display, Formatter};
#[derive(Debug)]
pub struct IllegalArgumentError {
message: String,
}
// Eventually we'll want support for source position, so we can highlight which argument(s)
// are wrong
impl IllegalArgumentError {
pub fn wrong_type(expected: &str, found: &str) -> Self {
Self {
message: format!("Expected {} but found {}", expected, found),
}
}
pub fn wrong_number(expected: usize, found: usize) -> Self {
Self {
message: format!("Expected {} argument(s) but found {}", expected, found),
}
}
}
impl Display for IllegalArgumentError {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "IllegalArgumentError: {}", self.message)
}
}
impl Error for IllegalArgumentError {}

7
dm-core-impl/src/lib.rs Normal file
View File

@ -0,0 +1,7 @@
mod error;
pub mod print;
pub mod string;
mod util;
pub use print::*;
pub use string::*;

56
dm-core-impl/src/print.rs Normal file
View File

@ -0,0 +1,56 @@
use crate::error::IllegalArgumentError;
use dvm_lib::vm::value::Value;
use std::error::Error;
use std::ops::Deref;
pub fn core_print(args: &[Value]) -> Result<Value, Box<dyn Error>> {
match args.get(0) {
None => Err(Box::new(IllegalArgumentError::wrong_number(1, 0))),
Some(msg) => {
match msg {
Value::Object(object) => {
todo!("printing objects")
}
Value::Int(i) => {
print!("{}", i);
}
Value::Double(d) => {
print!("{}", d);
}
Value::String(s) => {
print!("{}", s.deref());
}
Value::Null => {
print!("Null");
}
}
Ok(Value::Null)
}
}
}
pub fn core_println(args: &[Value]) -> Result<Value, Box<dyn Error>> {
match args.get(0) {
None => Err(Box::new(IllegalArgumentError::wrong_number(1, 0))),
Some(msg) => {
match msg {
Value::Object(object) => {
todo!("printing objects")
}
Value::Int(i) => {
println!("{}", i);
}
Value::Double(d) => {
println!("{}", d);
}
Value::String(s) => {
println!("{}", s.deref());
}
Value::Null => {
print!("Null");
}
}
Ok(Value::Null)
}
}
}

View File

@ -0,0 +1,21 @@
use crate::error::IllegalArgumentError;
use crate::util::value_to_variant_name;
use dvm_lib::vm::value::Value;
use std::error::Error;
pub fn core_string_len(args: &[Value]) -> Result<Value, Box<dyn Error>> {
let this = args.get(0);
match this {
None => Err(Box::new(IllegalArgumentError::wrong_number(1, 0))),
Some(this) => match this {
Value::String(s) => {
let len: i64 = s.len().try_into()?;
todo!("Long Values")
}
_ => Err(Box::new(IllegalArgumentError::wrong_type(
"String",
value_to_variant_name(this),
))),
},
}
}

11
dm-core-impl/src/util.rs Normal file
View File

@ -0,0 +1,11 @@
use dvm_lib::vm::value::Value;
pub fn value_to_variant_name(value: &Value) -> &'static str {
match value {
Value::Object(_) => "Object", // todo: lookup class
Value::Int(_) => "Int",
Value::Double(_) => "Double",
Value::String(_) => "String",
Value::Null => "Null",
}
}

6
dm-core/src/lib.dm Normal file
View File

@ -0,0 +1,6 @@
mod ops
mod print
mod string
pub use print::*
pub use string::*

6
dm-core/src/ops.dm Normal file
View File

@ -0,0 +1,6 @@
pub trait Add
type Right = Self
type Result = Self
fn add(right: Self::Right) -> Self::Result
end

3
dm-core/src/print.dm Normal file
View File

@ -0,0 +1,3 @@
pub extern fn print(msg: Any) -> Void
pub extern fn println(msg: Any) -> Void

5
dm-core/src/string.dm Normal file
View File

@ -0,0 +1,5 @@
pub extern class String
pub extern fn len() -> Long
end
intrinsic impl<T> Add<Right = T> for String end