Some clean up.

This commit is contained in:
Jesse Brault 2024-11-29 22:39:24 -06:00
parent 438d0e7317
commit 21d4f6bb69
12 changed files with 39 additions and 33 deletions

View File

@ -5,6 +5,6 @@ edition = "2021"
[[bin]]
name = "dmc"
path = "src/bin/compiler"
path = "src/bin/compiler/main.rs"
[dependencies]

View File

@ -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");

View File

@ -80,7 +80,8 @@ pub fn tokenize(input: &String) -> Result<Vec<Token>, String> {
let mut buffer = String::new();
buffer.push(c);
while let Some(num_char) = peekable.next_if(|c| {
c.is_digit(10) || match c {
c.is_digit(10)
|| match c {
'_' | 'x' | 'L' | 'd' => true,
_ => false,
}

View File

@ -1,3 +1,3 @@
pub mod lexer;
pub mod vm;
pub mod parser;
pub mod vm;

View File

@ -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<Option<(u64, DmType)>, String>;
pub type PlatformFunction = fn(&mut PlatformCallFrame) -> Result<Option<(u64, DmType)>, String>;
pub struct DmVirtualMachine {
registers: Vec<u64>,
register_types: Vec<DmType>,
platform_functions: HashMap<String, PlatformFunction>
platform_functions: HashMap<String, PlatformFunction>,
}
impl DmVirtualMachine {
@ -25,7 +25,7 @@ impl DmVirtualMachine {
&self,
dm_function: &DmFunction,
args: Vec<u64>,
arg_types: Vec<DmType>
arg_types: Vec<DmType>,
) -> Result<(u64, DmType), String> {
run(&dm_function.byte_code, todo!(), todo!());
todo!()
@ -36,7 +36,7 @@ pub struct DmObjectType {
name: String,
properties: HashMap<String, DmObjectProperty>,
fields: HashMap<String, DmObjectField>,
methods: HashMap<String, DmFunction>
methods: HashMap<String, DmFunction>,
}
pub struct DmObject {
@ -91,7 +91,7 @@ pub fn run(code: &Vec<u8>, registers: &mut Vec<u64>, register_types: &mut Vec<Dm
data: pointer,
size,
layout,
object_type: todo!()
object_type: todo!(),
});
let dm_object_pointer = Box::into_raw(dm_object) as u64;
registers[target_register] = dm_object_pointer;

View File

@ -110,7 +110,6 @@ pub fn load_module(bytes: &[u8]) -> Result<DmModule, String> {
// TODO: extract module name
// Holder for Symbols we will extract from the symbol table bytes
let mut symbols: Vec<DmSymbol> = 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
}

View File

@ -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 {

View File

@ -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<u64>,
arg_types: Vec<crate::vm::types::DmType>,
vm: DmVirtualMachine
vm: DmVirtualMachine,
}
pub fn init_platform_functions() -> HashMap<String, PlatformFunction> {
let mut m: HashMap<String, PlatformFunction> = HashMap::new();
m.insert(String::from("std::core::print"), dm_print);
m
let mut fns: HashMap<String, PlatformFunction> = HashMap::new();
fns.insert(String::from("std::core::print"), dm_print);
fns
}

View File

@ -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<Option<(u64, DmType)>, String> {
@ -23,8 +23,15 @@ pub fn dm_print(frame: &mut PlatformCallFrame) -> Result<Option<(u64, DmType)>,
}
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<Option<(u64, DmType)>,
} else {
print!("{}@{:?}", dm_object.object_type.name, dm_object.data);
}
}
},
}
Ok(None)
}