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]] [[bin]]
name = "dmc" name = "dmc"
path = "src/bin/compiler" path = "src/bin/compiler/main.rs"
[dependencies] [dependencies]

View File

@ -1,6 +1,6 @@
use deimos::lexer::tokenize; use deimos::lexer::tokenize;
use std::process::exit;
use deimos::parser::parse; use deimos::parser::parse;
use std::process::exit;
fn main() { fn main() {
let src = String::from("print 42"); let src = String::from("print 42");

View File

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

View File

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

View File

@ -1,23 +1,23 @@
mod module; mod module;
mod op_codes; mod op_codes;
mod util;
mod platform; mod platform;
mod types; mod types;
mod util;
use crate::vm::module::DmFunction;
use op_codes::*; use op_codes::*;
use platform::PlatformCallFrame; use platform::PlatformCallFrame;
use std::alloc::{alloc_zeroed, dealloc, Layout}; use std::alloc::{alloc_zeroed, dealloc, Layout};
use std::collections::HashMap; use std::collections::HashMap;
use types::DmType; use types::DmType;
use util::{get_32_le, get_64_le}; 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 { pub struct DmVirtualMachine {
registers: Vec<u64>, registers: Vec<u64>,
register_types: Vec<DmType>, register_types: Vec<DmType>,
platform_functions: HashMap<String, PlatformFunction> platform_functions: HashMap<String, PlatformFunction>,
} }
impl DmVirtualMachine { impl DmVirtualMachine {
@ -25,7 +25,7 @@ impl DmVirtualMachine {
&self, &self,
dm_function: &DmFunction, dm_function: &DmFunction,
args: Vec<u64>, args: Vec<u64>,
arg_types: Vec<DmType> arg_types: Vec<DmType>,
) -> Result<(u64, DmType), String> { ) -> Result<(u64, DmType), String> {
run(&dm_function.byte_code, todo!(), todo!()); run(&dm_function.byte_code, todo!(), todo!());
todo!() todo!()
@ -36,7 +36,7 @@ pub struct DmObjectType {
name: String, name: String,
properties: HashMap<String, DmObjectProperty>, properties: HashMap<String, DmObjectProperty>,
fields: HashMap<String, DmObjectField>, fields: HashMap<String, DmObjectField>,
methods: HashMap<String, DmFunction> methods: HashMap<String, DmFunction>,
} }
pub struct DmObject { pub struct DmObject {
@ -91,7 +91,7 @@ pub fn run(code: &Vec<u8>, registers: &mut Vec<u64>, register_types: &mut Vec<Dm
data: pointer, data: pointer,
size, size,
layout, layout,
object_type: todo!() object_type: todo!(),
}); });
let dm_object_pointer = Box::into_raw(dm_object) as u64; let dm_object_pointer = Box::into_raw(dm_object) as u64;
registers[target_register] = dm_object_pointer; 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 // TODO: extract module name
// Holder for Symbols we will extract from the symbol table bytes // Holder for Symbols we will extract from the symbol table bytes
let mut symbols: Vec<DmSymbol> = Vec::new(); 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; let max_shift = (bytes.len() - 1) * 8;
for i in 0..bytes.len() { for i in 0..bytes.len() {
result |= (bytes[i] as u64) << (max_shift - i * 8); result |= (bytes[i] as u64) << (max_shift - i * 8);
}; }
result result
} }

View File

@ -42,7 +42,7 @@ pub const PLATFORM_CALL: u8 = 0x10;
macro_rules! push_number { macro_rules! push_number {
( $dest: expr, $num: expr ) => { ( $dest: expr, $num: expr ) => {
push_bytes!($dest, $num.to_le_bytes()) push_bytes!($dest, $num.to_le_bytes())
} };
} }
macro_rules! push_string { 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::platform::std_lib::core::dm_print;
use crate::vm::{DmVirtualMachine, PlatformFunction};
use std::collections::HashMap;
mod std_lib; mod std_lib;
pub struct PlatformCallFrame { pub struct PlatformCallFrame {
args: Vec<u64>, args: Vec<u64>,
arg_types: Vec<crate::vm::types::DmType>, arg_types: Vec<crate::vm::types::DmType>,
vm: DmVirtualMachine vm: DmVirtualMachine,
} }
pub fn init_platform_functions() -> HashMap<String, PlatformFunction> { pub fn init_platform_functions() -> HashMap<String, PlatformFunction> {
let mut m: HashMap<String, PlatformFunction> = HashMap::new(); let mut fns: HashMap<String, PlatformFunction> = HashMap::new();
m.insert(String::from("std::core::print"), dm_print); fns.insert(String::from("std::core::print"), dm_print);
m fns
} }

View File

@ -4,7 +4,7 @@ use crate::vm::DmObject;
use DmType::*; use DmType::*;
pub struct DmString { pub struct DmString {
data: String data: String,
} }
pub fn dm_print(frame: &mut PlatformCallFrame) -> Result<Option<(u64, DmType)>, 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 { Pointer => unsafe {
let dm_object = Box::from_raw(frame.args[0] as *mut DmObject); 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")) { if let Some(&ref to_string_method) = dm_object
let call_result = frame.vm.call(to_string_method, vec![frame.args[0]], vec![Pointer]); .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() { if call_result.is_err() {
return Err(call_result.unwrap_err()); return Err(call_result.unwrap_err());
} }
@ -35,8 +42,7 @@ pub fn dm_print(frame: &mut PlatformCallFrame) -> Result<Option<(u64, DmType)>,
} else { } else {
print!("{}@{:?}", dm_object.object_type.name, dm_object.data); print!("{}@{:?}", dm_object.object_type.name, dm_object.data);
} }
} },
} }
Ok(None) Ok(None)
} }