Skip to content

Commit

Permalink
Refactor position handling
Browse files Browse the repository at this point in the history
  • Loading branch information
lgyanf committed Sep 23, 2023
1 parent e348c63 commit 3e0b146
Show file tree
Hide file tree
Showing 12 changed files with 857 additions and 537 deletions.
135 changes: 117 additions & 18 deletions src/ast/expr.rs
Original file line number Diff line number Diff line change
@@ -1,27 +1,126 @@
use crate::token::{Token, TokenType};
use std::fmt::Display;

use crate::{token::{Token, TokenType}, position::PositionRange};

#[derive(PartialEq, Debug, Clone)]
pub enum BinaryOp {
Plus,
Minus,
Star,
Slash,
Greater,
GreaterEqual,
Less,
LessEqual,
EqualEqual,
BangEqual,
}

impl Display for BinaryOp {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let s = match self {
BinaryOp::Plus => "+",
BinaryOp::Minus => "-",
BinaryOp::Star => "*",
BinaryOp::Slash => "/",
BinaryOp::Greater => ">",
BinaryOp::GreaterEqual => ">=",
BinaryOp::Less => "<",
BinaryOp::LessEqual => "<=",
BinaryOp::EqualEqual => "==",
BinaryOp::BangEqual => "!=",
};
write!(f, "{}", s)
}
}

impl From<Token> for BinaryOp {
fn from(t: Token) -> Self {
match t.type_ {
TokenType::Plus => Self::Plus,
TokenType::Minus => Self::Minus,
TokenType::Star => Self::Star,
TokenType::Slash => Self::Slash,
TokenType::Greater => Self::Greater,
TokenType::GreaterEqual => Self::GreaterEqual,
TokenType::Less => Self::Less,
TokenType::LessEqual => Self::LessEqual,
_ => unreachable!("Cannot create binary operator from {} at {:?}", t.type_, t.position),
}
}
}

#[derive(PartialEq, Debug, Clone)]
pub enum UnaryOp {
Minus,
Bang,
}

impl Display for UnaryOp {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let s = match self {
UnaryOp::Minus => "-",
UnaryOp::Bang => "!",
};
write!(f, "{}", s)
}
}

impl From<Token> for UnaryOp {
fn from(t: Token) -> Self {
match t.type_ {
TokenType::Minus => Self::Minus,
TokenType::Bang => Self::Bang,
_ => unreachable!("Cannot create unary operator from {} at {:?}", t.type_, t.position),
}
}
}

#[derive(PartialEq, Debug, Clone)]
pub enum Expr {
Literal(Token),
Unary(Token, Box<Expr>),
Binary(Box<Expr>, Token, Box<Expr>),
pub enum ExprType {
StringLiteral(String),
NumberLiteral(f64),
BooleanLiteral(bool),
Nil,
Unary(UnaryOp, Box<Expr>),
Binary(Box<Expr>, BinaryOp, Box<Expr>),
Grouping(Box<Expr>),
Variable { name: String, line: u32 },
Variable { name: String },
}

#[derive(PartialEq, Debug, Clone)]
pub struct Expr {
pub expr_type: ExprType,
pub position: PositionRange,
}

impl Expr {
pub fn number_literal(value: f64, line: u32) -> Self {
Self::Literal(Token {
type_: TokenType::Number(value),
line,
})
}

pub fn string_literal(value: &str, line: u32) -> Self {
Self::Literal(Token {
type_: TokenType::String(value.to_owned()),
line,
})
pub fn nil(position: PositionRange) -> Self {
Self {
expr_type: ExprType::Nil,
position,
}
}

pub fn boolean_literal(value: bool, position: PositionRange) -> Self {
Self {
expr_type: ExprType::BooleanLiteral(value),
position,
}
}

pub fn number_literal(value: f64, position: PositionRange) -> Self {
Self {
expr_type: ExprType::NumberLiteral(value),
position,
}
}

pub fn string_literal(value: &str, position: PositionRange) -> Self {
Self {
expr_type: ExprType::StringLiteral(value.to_owned()),
position,
}
}

pub fn boxed(&self) -> Box<Self> {
Expand Down
1 change: 1 addition & 0 deletions src/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ pub mod parser;
pub mod printer;
pub mod statement;
pub mod visitor;
mod token_iterator;

pub use self::expr::Expr;
pub use self::statement::Statement;
Expand Down
Loading

0 comments on commit 3e0b146

Please sign in to comment.