merge commit

This commit is contained in:
2025-06-17 23:50:16 +01:00
25 changed files with 5387 additions and 102 deletions
+14 -13
View File
@@ -3,7 +3,7 @@ use std::{
collections::HashSet,
fs,
hash::{DefaultHasher, Hash, Hasher},
path::PathBuf,
path::{Path, PathBuf},
};
use common::prelude::Instruction;
@@ -19,27 +19,27 @@ pub mod model;
pub mod parser;
pub mod resolver;
pub fn assemble(src: &PathBuf) -> Vec<Instruction> {
pub fn assemble(src: &Path) -> Vec<Instruction> {
let mut modules = HashSet::<u64>::new();
let mut program = Program::new();
let hash = quick_hash(src);
modules.insert(hash);
match prepare_dependency(src.clone(), &mut modules, &mut program) {
match prepare_dependency(src, &mut modules, &mut program) {
Ok(_) => {}
Err(err) => println!("BIG ERROR {:?}", err),
Err(err) => println!("BIG ERROR {err:?}"),
}
for node in program.nodes {
println!("{:?}", node);
println!("{node:?}");
}
vec![]
}
fn prepare_dependency(
path: PathBuf,
path: &Path,
modules: &mut HashSet<u64>,
program: &mut Program,
) -> Result<(), AssembleError> {
@@ -53,9 +53,9 @@ fn prepare_dependency(
));
}
let src = fs::read_to_string(&path)
.map_err(|_| AssembleError::InvalidFile(path.clone()))?;
let file_hash = quick_hash(&path);
let src = fs::read_to_string(path)
.map_err(|_| AssembleError::InvalidFile(path.to_path_buf()))?;
let file_hash = quick_hash(path);
log(&format!("{:20} {:20}", "Tokenising", filename));
let tokens = lexer::lexer(src, file_hash)?;
@@ -84,14 +84,14 @@ fn prepare_dependency(
if !modules.contains(&quick_hash(&dep)) {
modules.insert(quick_hash(&dep));
prepare_dependency(dep, modules, program)?
prepare_dependency(dep.as_path(), modules, program)?
}
}
Ok(())
}
fn build(src: Vec<Node>) -> Result<Vec<Instruction>, AssembleError> {
fn _build(_src: Vec<Node>) -> Result<Vec<Instruction>, AssembleError> {
Ok(vec![])
}
@@ -125,14 +125,15 @@ impl fmt::Display for AssembleError {
}
}
fn quick_hash(value: &PathBuf) -> u64 {
fn quick_hash(value: &Path) -> u64 {
let mut hasher = DefaultHasher::new();
value.canonicalize().unwrap().to_str().hash(&mut hasher);
hasher.finish()
}
// TODO: Use an actual logging or tracing library for pretty (scoped) output.
fn log(message: &str) {
println!("\x1b[32mINFO:\x1b[0m {}", message);
println!("\x1b[32mINFO:\x1b[0m {message}");
}
// create a macro that lexes and parses the input string into Nodes
+2 -5
View File
@@ -1,8 +1,5 @@
use std::{fs, io::Write, path::PathBuf};
use assembler::{lexer, parser::Parser};
use common::prelude::{ITypeArgs, Instruction, RTypeArgs, Register};
fn main() {
// parse args:
let args: Vec<String> = std::env::args().collect();
@@ -16,10 +13,10 @@ fn main() {
let src = PathBuf::from(input_path);
let mut output_file = fs::File::create(output_path).unwrap();
let res = assembler::assemble(&src)
assembler::assemble(&src)
.iter()
.map(|i| i.encode())
.for_each(|i| {
output_file.write_all(&i.to_be_bytes()).unwrap();
output_file.write_all(&i.to_le_bytes()).unwrap();
});
}
+54 -5
View File
@@ -3,13 +3,12 @@ use std::{fmt, str::FromStr};
use common::prelude::Register;
#[derive(Debug, Clone)]
#[expect(dead_code)]
pub struct Node(pub Option<Symbol>, pub Opcode, pub Vec<Token>);
impl fmt::Display for Node {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let symbol = match &self.0 {
Some(symbol) => format!("{}", symbol),
Some(symbol) => format!("{symbol}"),
None => "".to_string(),
};
@@ -26,15 +25,65 @@ impl fmt::Display for Symbol {
impl fmt::Display for Module {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Module::Unresolved(name) => write!(f, "{}", name),
Module::Resolved(name) => write!(f, "{}", name),
Module::Unresolved(name) => write!(f, "{name}"),
Module::Resolved(name) => write!(f, "{name}"),
}
}
}
impl fmt::Display for Opcode {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self)
match self {
Opcode::Nop => write!(f, "nop"),
Opcode::Mov => write!(f, "mov"),
Opcode::Movs => write!(f, "movs"),
Opcode::Ldb => write!(f, "ldb"),
Opcode::Ldbs => write!(f, "ldbs"),
Opcode::Ldh => write!(f, "ldh"),
Opcode::Ldhs => write!(f, "ldhs"),
Opcode::Ldw => write!(f, "ldw"),
Opcode::Stb => write!(f, "stb"),
Opcode::Sth => write!(f, "sth"),
Opcode::Stw => write!(f, "stw"),
Opcode::Lli => write!(f, "lli"),
Opcode::Lui => write!(f, "lui"),
Opcode::Jmp => write!(f, "jmp"),
Opcode::Jeq => write!(f, "jeq"),
Opcode::Jne => write!(f, "jne"),
Opcode::Jgt => write!(f, "jgt"),
Opcode::Jge => write!(f, "jge"),
Opcode::Jlt => write!(f, "jlt"),
Opcode::Jle => write!(f, "jle"),
Opcode::Cmp => write!(f, "cmp"),
Opcode::Inc => write!(f, "inc"),
Opcode::Dec => write!(f, "dec"),
Opcode::Shl => write!(f, "shl"),
Opcode::Shr => write!(f, "shr"),
Opcode::Add => write!(f, "add"),
Opcode::Sub => write!(f, "sub"),
Opcode::And => write!(f, "and"),
Opcode::Or => write!(f, "or"),
Opcode::Not => write!(f, "not"),
Opcode::Xor => write!(f, "xor"),
Opcode::Nand => write!(f, "nand"),
Opcode::Nor => write!(f, "nor"),
Opcode::Xnor => write!(f, "xnor"),
Opcode::Int => write!(f, "int"),
Opcode::Irt => write!(f, "irt"),
Opcode::Hlt => write!(f, "hlt"),
Opcode::Iadd => write!(f, "iadd"),
Opcode::Isub => write!(f, "isub"),
Opcode::Db => write!(f, "db"),
Opcode::Dh => write!(f, "dh"),
Opcode::Dw => write!(f, "dw"),
Opcode::Resb => write!(f, "resb"),
Opcode::Resh => write!(f, "resh"),
Opcode::Resw => write!(f, "resw"),
Opcode::Push => write!(f, "push"),
Opcode::Pop => write!(f, "pop"),
Opcode::Lwi => write!(f, "lwi"),
Opcode::Include => write!(f, "include"),
}
}
}
+6 -2
View File
@@ -1,6 +1,4 @@
use core::fmt;
use std::path::PathBuf;
use std::str::FromStr;
use common::prelude::{Instruction, Register};
@@ -33,6 +31,12 @@ impl Program {
}
}
impl Default for Program {
fn default() -> Self {
Self::new()
}
}
impl Parser {
pub fn parse_nodes(tokens: Vec<Token>) -> Result<Vec<Node>, AssembleError> {
let mut self_ = Parser {