Rewrie assembler with macro support
This commit is contained in:
parent
e82c5bdb90
commit
6adb943754
9 changed files with 435 additions and 264 deletions
|
@ -1,3 +1,5 @@
|
|||
use anyhow::{bail, Result};
|
||||
|
||||
use crate::arg;
|
||||
use crate::assembler;
|
||||
use crate::lexer;
|
||||
|
@ -14,8 +16,8 @@ pub enum Arg {
|
|||
},
|
||||
}
|
||||
|
||||
impl assembler::ToCode for Arg {
|
||||
fn to_code(&self) -> String {
|
||||
impl assembler::ToAssembly for Arg {
|
||||
fn to_assembly(&self) -> String {
|
||||
match self {
|
||||
Arg::String(x) => format!("\"{x}\""),
|
||||
Arg::Number(x) => x.to_string(),
|
||||
|
@ -23,9 +25,9 @@ impl assembler::ToCode for Arg {
|
|||
Arg::BinaryExpression { left, right, op } => {
|
||||
format!(
|
||||
"({left} {op} {right})",
|
||||
left = left.to_code(),
|
||||
op = op.to_code(),
|
||||
right = right.to_code()
|
||||
left = left.to_assembly(),
|
||||
op = op.to_assembly(),
|
||||
right = right.to_assembly()
|
||||
)
|
||||
}
|
||||
}
|
||||
|
@ -52,7 +54,7 @@ impl assembler::ByteResolvable<assembler::Data> for Arg {
|
|||
let mut arg: Option<arg::Arg>;
|
||||
|
||||
loop {
|
||||
arg = data.contants.get(&name).cloned();
|
||||
arg = data.constants.get(&name).cloned();
|
||||
|
||||
match arg {
|
||||
Some(a) => {
|
||||
|
@ -92,7 +94,7 @@ impl assembler::ByteResolvable<assembler::Data> for Arg {
|
|||
let mut arg: Option<arg::Arg>;
|
||||
|
||||
loop {
|
||||
arg = data.contants.get(&name).cloned();
|
||||
arg = data.constants.get(&name).cloned();
|
||||
|
||||
match arg {
|
||||
Some(a) => {
|
||||
|
@ -130,8 +132,8 @@ pub enum BinaryExpressionOperator {
|
|||
Pow,
|
||||
}
|
||||
|
||||
impl assembler::ToCode for BinaryExpressionOperator {
|
||||
fn to_code(&self) -> String {
|
||||
impl assembler::ToAssembly for BinaryExpressionOperator {
|
||||
fn to_assembly(&self) -> String {
|
||||
match self {
|
||||
BinaryExpressionOperator::Add => "+".to_string(),
|
||||
BinaryExpressionOperator::Sub => "-".to_string(),
|
||||
|
@ -142,20 +144,20 @@ impl assembler::ToCode for BinaryExpressionOperator {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn parse_binary_operation(token: &lexer::Token) -> Result<BinaryExpressionOperator, String> {
|
||||
pub fn parse_binary_operation(token: &lexer::Token) -> Result<BinaryExpressionOperator> {
|
||||
match token {
|
||||
lexer::Token::Add => Ok(BinaryExpressionOperator::Add),
|
||||
lexer::Token::Sub => Ok(BinaryExpressionOperator::Sub),
|
||||
lexer::Token::Mul => Ok(BinaryExpressionOperator::Mul),
|
||||
lexer::Token::Div => Ok(BinaryExpressionOperator::Div),
|
||||
lexer::Token::Pow => Ok(BinaryExpressionOperator::Pow),
|
||||
_ => Err("Invalid binary expression operator".to_string()),
|
||||
_ => bail!("Invalid binary expression operator"),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn parse_binary_expression_arg(tokens: &mut Vec<&&lexer::Token>) -> Result<Arg, String> {
|
||||
pub fn parse_binary_expression_arg(tokens: &mut Vec<&&lexer::Token>) -> Result<Arg> {
|
||||
if tokens.is_empty() {
|
||||
return Err("Malformed binary expression".to_string());
|
||||
bail!("Malformed binary expression");
|
||||
}
|
||||
|
||||
let mut args: Vec<&&lexer::Token> = tokens.drain(..3).collect();
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue