add return statement and refactor let
This commit is contained in:
parent
1da18a14b7
commit
e76b4014cd
38
src/ast.rs
38
src/ast.rs
|
@ -4,13 +4,9 @@ use crate::token::Token;
|
||||||
|
|
||||||
pub trait Node: Debug {}
|
pub trait Node: Debug {}
|
||||||
|
|
||||||
pub trait Statement: Node {
|
pub trait Statement: Node {}
|
||||||
fn statement_node(&self);
|
|
||||||
}
|
|
||||||
|
|
||||||
pub trait Expression: Node {
|
pub trait Expression: Node {}
|
||||||
fn expression_node(&self);
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct Program {
|
pub struct Program {
|
||||||
pub statements: Vec<Rc<dyn Statement>>,
|
pub statements: Vec<Rc<dyn Statement>>,
|
||||||
|
@ -32,19 +28,31 @@ impl Debug for Program {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// FIXME: rename ExampleStatement structs to Example.
|
||||||
|
// we already know it's a statement since it impls
|
||||||
|
// `Statement`, this isn't Java world.
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct LetStatement {
|
pub struct Let {
|
||||||
pub token: Token,
|
pub token: Token,
|
||||||
pub name: Identifier,
|
pub name: Identifier,
|
||||||
pub value: Rc<dyn Expression>,
|
pub value: Rc<dyn Expression>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Node for LetStatement {}
|
impl Node for Let {}
|
||||||
|
|
||||||
impl Statement for LetStatement {
|
impl Statement for Let {}
|
||||||
fn statement_node(&self) {}
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub struct Return {
|
||||||
|
pub token: Token,
|
||||||
|
pub value: Rc<dyn Expression>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Statement for Return {}
|
||||||
|
|
||||||
|
impl Node for Return {}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct Identifier {
|
pub struct Identifier {
|
||||||
pub token: Token,
|
pub token: Token,
|
||||||
|
@ -52,17 +60,11 @@ pub struct Identifier {
|
||||||
|
|
||||||
impl Node for Identifier {}
|
impl Node for Identifier {}
|
||||||
|
|
||||||
impl Expression for Identifier {
|
impl Expression for Identifier {}
|
||||||
fn expression_node(&self) {}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct DummyExpression {}
|
pub struct DummyExpression {}
|
||||||
|
|
||||||
impl Node for DummyExpression {}
|
impl Node for DummyExpression {}
|
||||||
|
|
||||||
impl Expression for DummyExpression {
|
impl Expression for DummyExpression {}
|
||||||
fn expression_node(&self) {
|
|
||||||
panic!("this is dummy");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
use std::{fmt, rc::Rc};
|
use std::{fmt, rc::Rc};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
ast::{DummyExpression, Expression, Identifier, LetStatement, Program, Statement},
|
ast::{DummyExpression, Expression, Identifier, Let, Program, Statement},
|
||||||
lexer::Lexer,
|
lexer::Lexer,
|
||||||
token::Token,
|
token::Token,
|
||||||
};
|
};
|
||||||
|
@ -102,7 +102,7 @@ impl Parser {
|
||||||
|
|
||||||
let value = self.parse_expression()?;
|
let value = self.parse_expression()?;
|
||||||
|
|
||||||
Some(Rc::new(LetStatement { token, name, value }))
|
Some(Rc::new(Let { token, name, value }))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn parse_expression(&mut self) -> Option<Rc<dyn Expression>> {
|
fn parse_expression(&mut self) -> Option<Rc<dyn Expression>> {
|
||||||
|
@ -204,7 +204,7 @@ mod tests {
|
||||||
fn test_let_statement(stmt: Rc<dyn Statement>, name: &str) {
|
fn test_let_statement(stmt: Rc<dyn Statement>, name: &str) {
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
format!("{stmt:?}"),
|
format!("{stmt:?}"),
|
||||||
format!("LetStatement {{ token: Let, name: Identifier {{ token: Ident(\"{name}\") }}, value: DummyExpression }}"),
|
format!("Let {{ token: Let, name: Identifier {{ token: Ident(\"{name}\") }}, value: DummyExpression }}"),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue