START OF COMPILER

This commit is contained in:
2024-10-11 17:31:03 -04:00
parent bb3a0ad113
commit de79445ede
39 changed files with 710 additions and 94 deletions

53
src/compiler/program.rs Normal file
View File

@@ -0,0 +1,53 @@
use std::collections::HashMap;
pub struct Program {
data: Vec<u8>,
ro_map: HashMap<String, usize>,
}
impl Program {
pub fn new(data: HashMap<String, Vec<u8>>) -> Self {
let mut ro_data = Vec::new();
let mut ro_map = HashMap::new();
for (key, val) in data {
ro_map.insert(key, ro_data.len());
ro_data.extend(val);
}
Self {
data: ro_data,
ro_map,
}
}
}
pub fn create_program<I: Instr>(
ro_data: HashMap<String, Vec<u8>>,
functions: Vec<Function<I>>,
) -> Vec<u8> {
let mut data = Vec::new();
let mut ro_map = HashMap::new();
for (key, val) in ro_data {
ro_map.insert(key, data.len());
data.extend(val);
}
// let mut fn_map = HashMap::new();
for fun in functions {
for i in fun.instructions {
data.extend(i.to_le_bytes());
}
}
data
}
pub struct Function<I: Instr> {
label: String,
instructions: Vec<I>,
}
pub trait Instr {
fn to_le_bytes(&self) -> impl IntoIterator<Item = u8>;
}
struct SymbolInstr {
i: usize
}