Skip to content

Commit

Permalink
Merge pull request #6 from ZakFarmer/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
ZakFarmer authored Oct 22, 2023
2 parents c99cd42 + 57f178f commit 920ba7d
Show file tree
Hide file tree
Showing 8 changed files with 764 additions and 457 deletions.
59 changes: 40 additions & 19 deletions src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,24 @@ impl std::fmt::Display for Node {
}
}

#[derive(Clone, Debug, PartialEq)]
pub enum Literal {
Integer(Integer),
Boolean(Boolean),
String(StringLiteral)
String(StringLiteral),
}

impl std::fmt::Display for Literal {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
match self {
Literal::Integer(Integer { token, value }) => write!(f, "{}", value),
Literal::Boolean(Boolean { token, value }) => write!(f, "{}", value),
Literal::String(StringLiteral { token, value }) => write!(f, "{}", value),
Literal::Integer(Integer { token: _, value }) => write!(f, "{}", value),
Literal::Boolean(Boolean { token: _, value }) => write!(f, "{}", value),
Literal::String(StringLiteral { token: _, value }) => write!(f, "{}", value),
}
}
}

#[derive(Clone, Debug, PartialEq)]
pub enum Expression {
Identifier(Identifier),
Literal(Literal),
Expand All @@ -48,39 +50,46 @@ impl std::fmt::Display for Expression {
Expression::Identifier(identifier) => write!(f, "{}", identifier),
Expression::Literal(literal) => write!(f, "{}", literal),
Expression::Infix(InfixExpression {
token,
token: _,
left,
operator,
right,
}) => write!(f, "({} {} {})", left, operator, right),
Expression::Prefix(PrefixExpression {
token,
token: _,
operator,
right,
}) => write!(f, "({}{})", operator, right),
Expression::If(IfExpression {
token,
token: _,
condition,
consequence,
alternative,
}) => {
if let Some(alternative) = alternative {
write!(f, "if {} {{\n{}\n}} else {{\n{}\n}}", condition, consequence, alternative)
write!(
f,
"if {} {{\n{}\n}} else {{\n{}\n}}",
condition, consequence, alternative
)
} else {
write!(f, "if {} {{\n{}\n}}", condition, consequence)
}
},
}
Expression::Function(FunctionLiteral {
token,
token: _,
parameters,
body,
}) => {
let params = parameters.iter().map(|p| p.to_string()).collect::<Vec<String>>();
let params = parameters
.iter()
.map(|p| p.to_string())
.collect::<Vec<String>>();

write!(f, "fn({}) {{\n{}\n}}", params.join(", "), body)
},
}
Expression::Call(CallExpression {
token,
token: _,
function,
arguments,
}) => {
Expand All @@ -100,6 +109,7 @@ impl std::fmt::Display for Expression {
}
}

#[derive(Clone, Debug, PartialEq)]
pub enum Statement {
Assign(Assignment),
Expr(Expression),
Expand All @@ -109,11 +119,9 @@ pub enum Statement {
impl std::fmt::Display for Statement {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
match self {
Statement::Assign(Assignment {
token,
name,
value,
}) => write!(f, "{} {} = {}", token, name, value),
Statement::Assign(Assignment { token, name, value }) => {
write!(f, "{} {} = {}", token, name, value)
}
Statement::Expr(expression) => write!(f, "{}", expression),
Statement::Return(ReturnStatement {
token,
Expand All @@ -123,6 +131,7 @@ impl std::fmt::Display for Statement {
}
}

#[derive(Clone, Debug, PartialEq)]
pub struct Program {
pub statements: Vec<Statement>,
}
Expand All @@ -148,34 +157,40 @@ impl std::fmt::Display for Program {
}

// LITERALS
#[derive(Clone, Debug, PartialEq)]
pub struct Boolean {
pub token: Token,
pub value: bool,
}

#[derive(Clone, Debug, PartialEq)]
pub struct Integer {
pub token: Token,
pub value: i64,
}

#[derive(Clone, Debug, PartialEq)]
pub struct StringLiteral {
pub token: Token,
pub value: String,
}

// EXPRESSIONS
#[derive(Clone, Debug, PartialEq)]
pub struct FunctionLiteral {
pub token: Token,
pub parameters: Vec<Identifier>,
pub body: BlockStatement,
}

#[derive(Clone, Debug, PartialEq)]
pub struct CallExpression {
pub token: Token,
pub function: Box<Expression>,
pub arguments: Vec<Expression>,
}

#[derive(Clone, Debug, PartialEq)]
pub struct Identifier {
pub token: Token,
pub value: String,
Expand All @@ -187,33 +202,38 @@ impl std::fmt::Display for Identifier {
}
}

#[derive(Clone, Debug, PartialEq)]
pub struct IfExpression {
pub token: Token,
pub condition: Box<Expression>,
pub consequence: BlockStatement,
pub alternative: Option<BlockStatement>,
}

#[derive(Clone, Debug, PartialEq)]
pub struct InfixExpression {
pub token: Token,
pub left: Box<Expression>,
pub operator: String,
pub right: Box<Expression>,
}

#[derive(Clone, Debug, PartialEq)]
pub struct PrefixExpression {
pub token: Token,
pub operator: String,
pub right: Box<Expression>,
}

// STATEMENTS
#[derive(Clone, Debug, PartialEq)]
pub struct Assignment {
pub token: Token,
pub name: Identifier,
pub value: Expression,
}

#[derive(Clone, Debug, PartialEq)]
pub struct BlockStatement {
pub token: Token,
pub statements: Vec<Statement>,
Expand All @@ -231,7 +251,8 @@ impl std::fmt::Display for BlockStatement {
}
}

#[derive(Clone, Debug, PartialEq)]
pub struct ReturnStatement {
pub token: Token,
pub return_value: Expression,
}
}
Loading

0 comments on commit 920ba7d

Please sign in to comment.