Add expression parsing

Signed-off-by: Alek Ratzloff <alekratz@gmail.com>
This commit is contained in:
2020-05-02 18:42:01 -04:00
parent 28d29c2270
commit d9edf21d16
9 changed files with 542 additions and 101 deletions

View File

@@ -1,11 +1,29 @@
use crate::syn::{op::*, span::*};
use derivative::Derivative;
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Stmt {
Assign(Expr, Expr),
Expr(Expr),
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Expr {
Base(BaseExpr),
Bin(Box<BinExpr>),
Un(Box<UnExpr>),
}
impl Spanned for Expr {
fn span(&self) -> Span {
match self {
Expr::Base(b) => b.span(),
Expr::Bin(b) => b.span(),
Expr::Un(u) => u.span(),
}
}
}
impl From<UnExpr> for Expr {
fn from(un: UnExpr) -> Self {
Expr::Un(Box::new(un))
@@ -24,18 +42,38 @@ impl From<BaseExpr> for Expr {
}
}
#[derive(Derivative, Clone, PartialEq, Eq)]
#[derivative(Debug)]
pub struct BinExpr {
pub lhs: Expr,
pub op: BinOp,
pub rhs: Expr,
}
pub struct UnExpr {
pub op: UnOp,
pub expr: Expr,
#[derivative(Debug = "ignore")]
pub span: Span,
}
impl Spanned for BinExpr {
fn span(&self) -> Span {
self.span
}
}
#[derive(Derivative, Clone, PartialEq, Eq)]
#[derivative(Debug)]
pub struct UnExpr {
pub op: UnOp,
pub expr: Expr,
#[derivative(Debug = "ignore")]
pub span: Span,
}
impl Spanned for UnExpr {
fn span(&self) -> Span {
self.span
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum BaseExprKind {
Ident,
Num,
@@ -46,7 +84,16 @@ pub enum BaseExprKind {
Tuple(Vec<Expr>),
}
#[derive(Derivative, Clone, PartialEq, Eq)]
#[derivative(Debug)]
pub struct BaseExpr {
pub kind: BaseExprKind,
#[derivative(Debug = "ignore")]
pub span: Span,
}
impl Spanned for BaseExpr {
fn span(&self) -> Span {
self.span
}
}