Skip to content

Commit

Permalink
Refactor quote implementation and tests, add new Quote struct
Browse files Browse the repository at this point in the history
  • Loading branch information
DaviRain-Su committed Oct 10, 2023
1 parent 0a7bb81 commit 07800da
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 19 deletions.
15 changes: 3 additions & 12 deletions src/evaluator/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,18 +103,9 @@ pub fn eval(node: Node, env: &mut Environment) -> anyhow::Result<Object> {
fn quote(node: Node) -> anyhow::Result<Object> {
match node {
Node::Program(program) => Err(Error::UnknownTypeError(format!("{program:?}")).into()),
Node::Expression(expression) => Ok(Quote {
node: Box::new(expression.into()),
}
.into()),
Node::Statement(statement) => Ok(Quote {
node: Box::new(statement.into()),
}
.into()),
Node::Object(object) => Ok(Quote {
node: Box::new(object.into()),
}
.into()),
Node::Expression(expression) => Ok(Quote::new(expression.into()).into()),
Node::Statement(statement) => Ok(Quote::new(statement.into()).into()),
Node::Object(object) => Ok(Quote::new(object.into()).into()),
}
}

Expand Down
12 changes: 6 additions & 6 deletions src/evaluator/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -976,12 +976,12 @@ fn test_quote() -> anyhow::Result<()> {
let quote = Quote::try_from(evaluated)?;
println!("evaluated: {}", quote);

if format!("{}", quote.node) == *"null" {
if format!("{}", quote.node()) == *"null" {
eprintln!("quote.node is null");
}

if format!("{}", quote.node) != tt.expected {
eprintln!("not equal. got={}, want={}", quote.node, tt.expected);
if format!("{}", quote.node()) != tt.expected {
eprintln!("not equal. got={}, want={}", quote.node(), tt.expected);
}
}

Expand Down Expand Up @@ -1017,12 +1017,12 @@ fn test_quote_unquote() -> anyhow::Result<()> {
let evaluated = test_eval(tt.input)?;
let quote = Quote::try_from(evaluated)?;

if format!("{}", quote.node) == "null" {
if format!("{}", quote.node()) == "null" {
eprintln!("quote.node is null");
}

if format!("{}", quote.node) != tt.expected {
eprintln!("no equal. got={}, want={}", quote.node, tt.expected);
if format!("{}", quote.node()) != tt.expected {
eprintln!("no equal. got={}, want={}", quote.node(), tt.expected);
}
}

Expand Down
14 changes: 13 additions & 1 deletion src/object/macro/quote.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,19 @@ const QUOTE: &str = "quote";

#[derive(Debug, Clone, Hash, PartialOrd, PartialEq, Eq, Ord)]
pub struct Quote {
pub node: Box<Node>,
node: Box<Node>,
}

impl Quote {
pub fn new(node: Node) -> Self {
Self {
node: Box::new(node),
}
}

pub fn node(&self) -> &Node {
&self.node
}
}

impl Display for Quote {
Expand Down

0 comments on commit 07800da

Please sign in to comment.