remove display
This commit is contained in:
parent
1a721a22ac
commit
586ddd1aec
10
src/lexer.rs
10
src/lexer.rs
|
@ -4,7 +4,7 @@ use std::{fmt, iter::Peekable};
|
||||||
pub struct Ident(String);
|
pub struct Ident(String);
|
||||||
impl fmt::Display for Ident {
|
impl fmt::Display for Ident {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
write!(f, "{}", self.0)
|
self.0.fmt(f)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -66,13 +66,13 @@ pub enum Token {
|
||||||
#[derive(PartialEq, Eq, PartialOrd, Ord)]
|
#[derive(PartialEq, Eq, PartialOrd, Ord)]
|
||||||
pub enum Precedence {
|
pub enum Precedence {
|
||||||
Min,
|
Min,
|
||||||
Equality,
|
Assign,
|
||||||
Relational,
|
|
||||||
Logical,
|
|
||||||
AddSub,
|
AddSub,
|
||||||
MulDiv,
|
MulDiv,
|
||||||
Pow,
|
Pow,
|
||||||
Assign,
|
Logical,
|
||||||
|
Relational,
|
||||||
|
Equality,
|
||||||
}
|
}
|
||||||
#[derive(PartialEq, Eq)]
|
#[derive(PartialEq, Eq)]
|
||||||
pub enum Associativity {
|
pub enum Associativity {
|
||||||
|
|
|
@ -9,5 +9,5 @@ fn main() {
|
||||||
let lexer = Lexer::new(script.chars());
|
let lexer = Lexer::new(script.chars());
|
||||||
let mut parser = Parser::new(lexer.map(Result::unwrap));
|
let mut parser = Parser::new(lexer.map(Result::unwrap));
|
||||||
let block = parser.parse().unwrap();
|
let block = parser.parse().unwrap();
|
||||||
println!("{block}");
|
println!("{block:?}");
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,9 +1,6 @@
|
||||||
use std::{
|
use std::iter::Peekable;
|
||||||
fmt::{self, Pointer},
|
|
||||||
iter::Peekable,
|
|
||||||
};
|
|
||||||
|
|
||||||
use crate::lexer::{Associativity, Ident, LexError, Literal, Precedence, Token};
|
use crate::lexer::{Associativity, LexError, Literal, Precedence, Token};
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub enum Expr {
|
pub enum Expr {
|
||||||
|
@ -35,61 +32,11 @@ pub enum Expr {
|
||||||
Divide(Box<Expr>, Box<Expr>),
|
Divide(Box<Expr>, Box<Expr>),
|
||||||
Exponent(Box<Expr>, Box<Expr>),
|
Exponent(Box<Expr>, Box<Expr>),
|
||||||
}
|
}
|
||||||
impl fmt::Display for Expr {
|
|
||||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
||||||
match self {
|
|
||||||
Expr::Assignment(l, r) => write!(f, "({l} = {r})\n"),
|
|
||||||
Expr::Literal(l) => write!(f, "{l}"),
|
|
||||||
Expr::Call(l, r) => {
|
|
||||||
write!(f, "{l}(")?;
|
|
||||||
for e in r {
|
|
||||||
write!(f, "{e}, ")?;
|
|
||||||
}
|
|
||||||
write!(f, ")")?;
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
Expr::Return(l) => {
|
|
||||||
write!(f, "return {l}")
|
|
||||||
}
|
|
||||||
Expr::Block(b) => {
|
|
||||||
write!(f, "{{\n")?;
|
|
||||||
for e in &b.exprs {
|
|
||||||
write!(f, "\t{e}\n")?;
|
|
||||||
}
|
|
||||||
write!(f, "}}")?;
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
Expr::Negate(l) => write!(f, "(-{l})"),
|
|
||||||
Expr::Not(l) => write!(f, "(!{l})"),
|
|
||||||
Expr::EqualTo(l, r) => write!(f, "({l} == {r})"),
|
|
||||||
Expr::NotEqualTo(l, r) => write!(f, "({l} != {r})"),
|
|
||||||
Expr::And(l, r) => write!(f, "({l} && {r})"),
|
|
||||||
Expr::Or(l, r) => write!(f, "({l} !|| {r})"),
|
|
||||||
Expr::LessThan(l, r) => write!(f, "({l} < {r})"),
|
|
||||||
Expr::LessThanOrEqualTo(l, r) => write!(f, "({l} <= {r})"),
|
|
||||||
Expr::GreaterThan(l, r) => write!(f, "({l} > {r})"),
|
|
||||||
Expr::GreaterThanOrEqualTo(l, r) => write!(f, "({l} >= {r})"),
|
|
||||||
Expr::Add(l, r) => write!(f, "({l} + {r})"),
|
|
||||||
Expr::Subtract(l, r) => write!(f, "({l} - {r})"),
|
|
||||||
Expr::Multiply(l, r) => write!(f, "({l} * {r})"),
|
|
||||||
Expr::Divide(l, r) => write!(f, "({l} / {r})"),
|
|
||||||
Expr::Exponent(l, r) => write!(f, "({l} ^ {r})"),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, Default)]
|
#[derive(Debug, Default)]
|
||||||
pub struct Block {
|
pub struct Block {
|
||||||
exprs: Vec<Expr>,
|
exprs: Vec<Expr>,
|
||||||
}
|
}
|
||||||
impl fmt::Display for Block {
|
|
||||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
||||||
for e in &self.exprs {
|
|
||||||
e.fmt(f)?;
|
|
||||||
}
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub enum ParseError {
|
pub enum ParseError {
|
||||||
|
|
Loading…
Reference in New Issue