Skip to content

Commit

Permalink
cargo fix & cargo fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
ZakFarmer committed Oct 14, 2023
1 parent 183eb79 commit 1634075
Show file tree
Hide file tree
Showing 7 changed files with 240 additions and 170 deletions.
52 changes: 31 additions & 21 deletions src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,24 @@ impl std::fmt::Display for dyn Statement {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
let statement_string = match self.as_any().type_id() {
id if id == std::any::TypeId::of::<ExpressionStatement>() => {
let expression_statement = self.as_any().downcast_ref::<ExpressionStatement>().unwrap();
let expression_statement =
self.as_any().downcast_ref::<ExpressionStatement>().unwrap();
format!("{:?}", expression_statement)
},
}
id if id == std::any::TypeId::of::<ReturnStatement>() => {
let return_statement = self.as_any().downcast_ref::<ReturnStatement>().unwrap();
format!("{:?}", return_statement)
},
}
id if id == std::any::TypeId::of::<VariableAssignment>() => {
let variable_assignment = self.as_any().downcast_ref::<VariableAssignment>().unwrap();
let variable_assignment =
self.as_any().downcast_ref::<VariableAssignment>().unwrap();
format!("{:?}", variable_assignment)
},
}
id if id == std::any::TypeId::of::<VariableReference>() => {
let variable_reference = self.as_any().downcast_ref::<VariableReference>().unwrap();
format!("${:?}", variable_reference)
},
_ => "".to_string()
}
_ => "".to_string(),
};

write!(f, "{}", statement_string)
Expand All @@ -41,18 +43,20 @@ impl Debug for dyn Statement {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
let statement_string = match self.as_any().type_id() {
id if id == std::any::TypeId::of::<ExpressionStatement>() => {
let expression_statement = self.as_any().downcast_ref::<ExpressionStatement>().unwrap();
let expression_statement =
self.as_any().downcast_ref::<ExpressionStatement>().unwrap();
format!("Expression: {:?}", expression_statement)
},
}
id if id == std::any::TypeId::of::<ReturnStatement>() => {
let return_statement = self.as_any().downcast_ref::<ReturnStatement>().unwrap();
format!("Return: {:?}", return_statement)
},
}
id if id == std::any::TypeId::of::<VariableAssignment>() => {
let variable_assignment = self.as_any().downcast_ref::<VariableAssignment>().unwrap();
let variable_assignment =
self.as_any().downcast_ref::<VariableAssignment>().unwrap();
format!("Assignment: {:?}", variable_assignment)
},
_ => "".to_string()
}
_ => "".to_string(),
};

write!(f, "{}", statement_string)
Expand All @@ -71,10 +75,18 @@ impl std::fmt::Debug for dyn Expression {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
if self.as_any().type_id() == std::any::TypeId::of::<InfixExpression>() {
let infix_expression = self.as_any().downcast_ref::<InfixExpression>().unwrap();
write!(f, "({:?} {} {:?})", infix_expression.left, infix_expression.operator, infix_expression.right)
write!(
f,
"({:?} {} {:?})",
infix_expression.left, infix_expression.operator, infix_expression.right
)
} else if self.as_any().type_id() == std::any::TypeId::of::<PrefixExpression>() {
let prefix_expression = self.as_any().downcast_ref::<PrefixExpression>().unwrap();
write!(f, "({}{:?})", prefix_expression.operator, prefix_expression.right)
write!(
f,
"({}{:?})",
prefix_expression.operator, prefix_expression.right
)
} else {
write!(f, "{}", self.token_literal())
}
Expand Down Expand Up @@ -210,14 +222,14 @@ impl std::fmt::Debug for ExpressionStatement {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
match &self.expression {
Some(expr) => write!(f, "{:?}", expr),
None => Ok(())
None => Ok(()),
}
}
}

pub struct ReturnStatement {
pub token: Token,
pub return_value: Option<Box<dyn Expression>>
pub return_value: Option<Box<dyn Expression>>,
}

impl std::fmt::Debug for ReturnStatement {
Expand Down Expand Up @@ -325,9 +337,7 @@ impl std::fmt::Debug for Program {

impl Default for Program {
fn default() -> Self {
Self {
statements: vec![]
}
Self { statements: vec![] }
}
}

Expand All @@ -343,4 +353,4 @@ impl Node for Program {
""
}
}
}
}
19 changes: 10 additions & 9 deletions src/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ impl<'a> Lexer<'a> {
} else {
(TokenType::Assign, "=".to_string())
}
},
}
Some(';') => (TokenType::Semicolon, ";".to_string()),
Some('(') => (TokenType::LParen, "(".to_string()),
Some(')') => (TokenType::RParen, ")".to_string()),
Expand All @@ -48,7 +48,7 @@ impl<'a> Lexer<'a> {
} else {
(TokenType::Bang, "!".to_string())
}
},
}
Some('/') => (TokenType::Slash, "/".to_string()),
Some('*') => (TokenType::Asterisk, "*".to_string()),
Some('<') => (TokenType::Lt, "<".to_string()),
Expand All @@ -60,14 +60,14 @@ impl<'a> Lexer<'a> {

return Token {
token_type: TokenType::lookup_ident(&literal),
literal
literal,
};
} else if ch.is_ascii_digit() {
let literal = self.read_number();

return Token {
token_type: TokenType::Int,
literal
literal,
};
} else {
(TokenType::Illegal, ch.to_string())
Expand All @@ -77,7 +77,7 @@ impl<'a> Lexer<'a> {
};

self.read_char();

Token {
token_type,
literal,
Expand Down Expand Up @@ -148,8 +148,8 @@ mod tests {
use super::*;

#[test]
fn test_lexer() -> Result<(), Error>{
let input = String::from("
fn test_lexer() -> Result<(), Error> {
let input = "
$five = 5;
$ten = five + 5;
Expand All @@ -159,7 +159,8 @@ mod tests {
}
$result = add(five, ten);
");
"
.to_string();

let expected_tokens = [
Token {
Expand Down Expand Up @@ -330,4 +331,4 @@ mod tests {

Ok(())
}
}
}
6 changes: 3 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::env;

use anyhow::{Result, Error};
use anyhow::{Error, Result};
use repl::init_repl;

mod ast;
Expand All @@ -13,11 +13,11 @@ mod token;
pub const NAME: &str = env!("CARGO_PKG_NAME");
pub const VERSION: &str = env!("CARGO_PKG_VERSION");

fn main() -> Result<(), Error>{
fn main() -> Result<(), Error> {
env_logger::init();

let args: Vec<String> = env::args().collect();

let file_path = match args.len() {
1 => None,
2 => Some(args[1].clone()),
Expand Down
Loading

0 comments on commit 1634075

Please sign in to comment.