Skip to content

Commit

Permalink
Add basic assembly
Browse files Browse the repository at this point in the history
Add RISC-V assembly generation for + and - operations
  • Loading branch information
JakeRoggenbuck committed Sep 14, 2024
1 parent 464007f commit 2f1070b
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 4 deletions.
13 changes: 11 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ struct Opt {
#[structopt(short, long)]
verbose: bool,

#[structopt(short, long)]
asm: bool,

#[structopt(short, long)]
filename: Option<String>,
}
Expand Down Expand Up @@ -75,7 +78,7 @@ fn run_file(filename: String, verbose: bool) {
}
}

fn interactive(verbose: bool) {
fn interactive(verbose: bool, asm: bool) {
let mut p = create_parser(verbose);

loop {
Expand Down Expand Up @@ -112,6 +115,12 @@ fn interactive(verbose: bool) {

let out = p.parse(tokens);

if asm {
for x in p.output_asm() {
println!("{}", color!(Color::BLACK, x.as_str()));
}
}

if out.token_type != TokenType::NoType {
println!(
"{} {}",
Expand All @@ -128,6 +137,6 @@ fn main() {
if let Some(filename) = opt.filename {
run_file(filename, opt.verbose);
} else {
interactive(opt.verbose);
interactive(opt.verbose, opt.asm);
}
}
57 changes: 55 additions & 2 deletions src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ pub trait Parser {
fn parse(&mut self, tokens: Vec<Token>) -> Token;
fn assign_value(&mut self, first: Option<Token>, second: Option<Token>, token: Token);
fn convert_to_bool(&mut self, first: Option<Token>, token: Token);
fn asm_li(&mut self, token: Token);
fn asm_add(&mut self);
fn asm_sub(&mut self);
fn output_asm(&mut self) -> Vec<String>;
fn reset_asm(&mut self);
}

#[derive(Debug)]
Expand All @@ -24,9 +29,20 @@ pub struct ParserState {
function_stack: Vec<Token>,
function_memory: HashMap<String, Vec<Token>>,
token_index: usize,
assembly: Vec<String>,
temp_reg_index: i8,
}

impl Parser for ParserState {
fn output_asm(&mut self) -> Vec<String> {
return self.assembly.clone();
}

fn reset_asm(&mut self) {
self.assembly = vec![];
self.temp_reg_index = 0;
}

fn convert_to_bool(&mut self, first: Option<Token>, token: Token) {
match first {
Some(a) => match a.token_type {
Expand Down Expand Up @@ -122,6 +138,32 @@ impl Parser for ParserState {
return first;
}

fn asm_li(&mut self, token: Token) {
self.assembly
.push(format!("li t{} {}", self.temp_reg_index, token.value));
self.temp_reg_index += 1;
}

fn asm_add(&mut self) {
self.assembly.push(format!(
"add t{} t{} t{}",
self.temp_reg_index - 2,
self.temp_reg_index - 2,
self.temp_reg_index - 1
));
self.temp_reg_index -= 1;
}

fn asm_sub(&mut self) {
self.assembly.push(format!(
"sub t{} t{} t{}",
self.temp_reg_index - 2,
self.temp_reg_index - 2,
self.temp_reg_index - 1
));
self.temp_reg_index -= 1;
}

fn match_token_type(&mut self, token: Token) {
// Match the type of token
// if it's a literal, add it to the stack
Expand All @@ -131,7 +173,12 @@ impl Parser for ParserState {
self.function_mode = true;
}

TokenType::NumericIntLiteral | TokenType::NumericDecLiteral => {
TokenType::NumericIntLiteral => {
self.asm_li(token.clone());
self.stack.push(token);
}

TokenType::NumericDecLiteral => {
self.stack.push(token);
}

Expand Down Expand Up @@ -334,7 +381,8 @@ impl Parser for ParserState {
self.stack.push(Token {
token_type: t_type,
value: v.to_string(),
})
});
self.asm_add();
}
TokenType::Multiplication => {
let v = a_float * b_float;
Expand All @@ -359,6 +407,7 @@ impl Parser for ParserState {
token_type: t_type,
value: v.to_string(),
});
self.asm_sub();
}
TokenType::Division => {
let v = a_float / b_float;
Expand Down Expand Up @@ -436,6 +485,8 @@ impl Parser for ParserState {
}

fn parse(&mut self, tokens: Vec<Token>) -> Token {
self.reset_asm();

if self.verbose {
println!(
"{}",
Expand Down Expand Up @@ -598,6 +649,8 @@ pub fn create_parser(verbose: bool) -> ParserState {
token_stack: Vec::<Token>::new(),
function_stack: Vec::<Token>::new(),
token_index: 0,
assembly: Vec::<String>::new(),
temp_reg_index: 0,
}
}

Expand Down

0 comments on commit 2f1070b

Please sign in to comment.