minor changes to assembler
This commit is contained in:
@@ -7,7 +7,6 @@ use common::prelude::Register;
|
|||||||
pub fn lexer(mut program: String, module: u64) -> Result<Vec<Token>, AssembleError> {
|
pub fn lexer(mut program: String, module: u64) -> Result<Vec<Token>, AssembleError> {
|
||||||
let mut tokens = Vec::new();
|
let mut tokens = Vec::new();
|
||||||
|
|
||||||
program = program.replace(',', "");
|
|
||||||
let lines = program.lines();
|
let lines = program.lines();
|
||||||
let mut literal = String::new();
|
let mut literal = String::new();
|
||||||
|
|
||||||
@@ -39,6 +38,11 @@ pub fn lexer(mut program: String, module: u64) -> Result<Vec<Token>, AssembleErr
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let token = token.trim_end_matches(',');
|
||||||
|
if token.is_empty() {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
if let Some(token) = parse_register(token)? {
|
if let Some(token) = parse_register(token)? {
|
||||||
tokens.push(token);
|
tokens.push(token);
|
||||||
} else if let Some(token) = parse_opcode(token)? {
|
} else if let Some(token) = parse_opcode(token)? {
|
||||||
@@ -61,6 +65,8 @@ pub fn lexer(mut program: String, module: u64) -> Result<Vec<Token>, AssembleErr
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
println!("{:#?}", tokens);
|
||||||
|
|
||||||
Ok(tokens)
|
Ok(tokens)
|
||||||
}
|
}
|
||||||
pub fn parse_register(token: &str) -> Result<Option<Token>, AssembleError> {
|
pub fn parse_register(token: &str) -> Result<Option<Token>, AssembleError> {
|
||||||
|
|||||||
@@ -51,19 +51,26 @@ impl fmt::Display for Node {
|
|||||||
.as_ref()
|
.as_ref()
|
||||||
.map_or_else(String::new, |symbol| format!("{symbol}:\n"));
|
.map_or_else(String::new, |symbol| format!("{symbol}:\n"));
|
||||||
|
|
||||||
|
let args = self
|
||||||
|
.args()
|
||||||
|
.into_iter()
|
||||||
|
.map(|arg| arg.to_string())
|
||||||
|
.collect::<Vec<_>>()
|
||||||
|
.join(" ");
|
||||||
|
|
||||||
write!(
|
write!(
|
||||||
f,
|
f,
|
||||||
"\x1b[93m{} \t\x1b[94m{} \x1b[37m{:?} \x1b[0m",
|
"\x1b[93m{} \t\x1b[94m{} \x1b[37m{} \x1b[0m",
|
||||||
symbol,
|
symbol,
|
||||||
self.opcode(),
|
self.opcode(),
|
||||||
self.args()
|
args,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl fmt::Display for Symbol {
|
impl fmt::Display for Symbol {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
write!(f, "{} ( module: {})", self.name, self.module)
|
write!(f, "{} [ID:{}]", self.name, self.module)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -174,6 +181,18 @@ pub enum Token {
|
|||||||
Opcode(Opcode),
|
Opcode(Opcode),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl fmt::Display for Token {
|
||||||
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
|
match self {
|
||||||
|
Self::Symbol(symbol) => write!(f, "{}", symbol),
|
||||||
|
Self::Register(register) => write!(f, "{}", register),
|
||||||
|
Self::Immediate(immediate) => write!(f, "{}", immediate),
|
||||||
|
Self::StringLit(string_lit) => write!(f, "{}", string_lit),
|
||||||
|
Self::Opcode(opcode) => write!(f, "{}", opcode),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Debug, PartialEq, Eq, Copy, Clone)]
|
#[derive(Debug, PartialEq, Eq, Copy, Clone)]
|
||||||
pub enum TokenType {
|
pub enum TokenType {
|
||||||
Symbol,
|
Symbol,
|
||||||
|
|||||||
Reference in New Issue
Block a user