Website Summary Files Commits Branches @commit @diff @stats
path: root / assembler / src / parser
diff options
context:
space:
mode:
2 files changed, 37 insertions, 1 deletions
-rw-r--r--assembler/src/parser/code_gen.rs11
-rw-r--r--assembler/src/parser/mod.rs27
diff --git a/assembler/src/parser/code_gen.rs b/assembler/src/parser/code_gen.rs
index 77f2c2f..4352e9e 100644
--- a/assembler/src/parser/code_gen.rs
+++ b/assembler/src/parser/code_gen.rs
@@ -52,4 +52,15 @@ impl Program {
}
self.instructions.clear();
}
+
+ pub(crate) fn to_bytes(self, absolute_start: usize) -> Vec<u8> {
+ let mut res = Vec::new();
+ res.resize(absolute_start, Default::default());
+ res.extend(self.constants);
+ res
+ .into_iter()
+ .map(|x| x.to_be_bytes())
+ .flatten()
+ .collect()
+ }
}
diff --git a/assembler/src/parser/mod.rs b/assembler/src/parser/mod.rs
index 5dbedf6..79b8835 100644
--- a/assembler/src/parser/mod.rs
+++ b/assembler/src/parser/mod.rs
@@ -2,7 +2,10 @@ mod code_gen;
mod syntax;
mod instruction;
-use std::collections::HashMap;
+use std::{
+ collections::HashMap,
+ fmt::{ Display, Formatter, Result },
+};
use crate::Word;
#[derive(Debug, Default)]
@@ -12,6 +15,28 @@ pub(crate) struct Program {
instructions: Vec<Instruction>,
}
+impl Display for Program {
+ fn fmt(&self, f: &mut Formatter) -> Result {
+ writeln!(f, "-- Definitions --")?;
+ for (name, loc) in self.definitions.iter() {
+ writeln!(f, "{} := {:?}", name, loc)?;
+ }
+ writeln!(f, "-- Constants --")?;
+ for (i, cnst) in self.constants.iter().enumerate() {
+ writeln!(f, "{: >8}: {} ", i, cnst)?;
+ }
+ writeln!(f, "-- Instructions --")?;
+ for (i, instr) in self.instructions.iter().enumerate() {
+ write!(f, "{: >8}: {} ", i, instr.name)?;
+ for arg in &instr.args {
+ write!(f, "{:?}, ", arg)?;
+ }
+ writeln!(f, "")?;
+ }
+ Ok(())
+ }
+}
+
#[derive(Debug, Default, PartialEq, Eq, Clone)]
pub(crate) enum Address {
Memory(usize),