mirror of
https://github.com/TheM1Stery/izanami.git
synced 2025-04-20 08:51:12 +00:00
Compare commits
No commits in common. "9f903556f9e3c1eae571a0baee23136c2e328b4c" and "8338c360e042569476ccc455169b9f686bb22f42" have entirely different histories.
9f903556f9
...
8338c360e0
@ -2,13 +2,6 @@ use crate::token::{LiteralType, Token};
|
|||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub enum Expr {
|
pub enum Expr {
|
||||||
Ternary {
|
|
||||||
first: Box<Expr>,
|
|
||||||
first_op: Token,
|
|
||||||
second: Box<Expr>,
|
|
||||||
second_op: Token,
|
|
||||||
third: Box<Expr>,
|
|
||||||
},
|
|
||||||
Binary {
|
Binary {
|
||||||
left: Box<Expr>,
|
left: Box<Expr>,
|
||||||
op: Token,
|
op: Token,
|
||||||
|
@ -28,32 +28,10 @@ impl Parser<'_> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Challenge #1. We're writing comma before equality, because it has the lowest precedence
|
// Challenge #1. We're writing comma before equality, because it has the lowest precedence
|
||||||
// comma -> equality ("," equality)* ; // expression grammar
|
// comma -> equality ((,) equality)* ; // expression grammar
|
||||||
fn comma(&mut self) -> Result<Expr, ParseError> {
|
fn comma(&mut self) -> Result<Expr, ParseError> {
|
||||||
use TokenType::*;
|
use TokenType::*;
|
||||||
self.left_association_binary(&[Comma], Self::ternary)
|
self.left_association_binary(&[Comma], Parser::equality)
|
||||||
}
|
|
||||||
|
|
||||||
// ternary -> equality ("?" expression : ternary)? // expression grammar
|
|
||||||
fn ternary(&mut self) -> Result<Expr, ParseError> {
|
|
||||||
use TokenType::*;
|
|
||||||
let expr = self.equality()?;
|
|
||||||
|
|
||||||
if self.match_token(&[Question]) {
|
|
||||||
let first_op = self.previous().clone();
|
|
||||||
let second = self.expression()?;
|
|
||||||
let second_op = self.consume(Colon, "Expected : after ternary operator ?")?;
|
|
||||||
let third = self.ternary()?;
|
|
||||||
return Ok(Expr::Ternary {
|
|
||||||
first: Box::new(expr),
|
|
||||||
first_op,
|
|
||||||
second: Box::new(second),
|
|
||||||
second_op,
|
|
||||||
third: Box::new(third),
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(expr)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn equality(&mut self) -> Result<Expr, ParseError> {
|
fn equality(&mut self) -> Result<Expr, ParseError> {
|
||||||
@ -90,11 +68,6 @@ impl Parser<'_> {
|
|||||||
self.primary()
|
self.primary()
|
||||||
}
|
}
|
||||||
|
|
||||||
/* error boundaries:
|
|
||||||
("!=" | "==") equality
|
|
||||||
| (">" | ">=" | "<" | "<=") comparison
|
|
||||||
| ("+") term
|
|
||||||
| ("/" | "*") factor ; */
|
|
||||||
fn primary(&mut self) -> Result<Expr, ParseError> {
|
fn primary(&mut self) -> Result<Expr, ParseError> {
|
||||||
use LiteralType::*;
|
use LiteralType::*;
|
||||||
use TokenType::*;
|
use TokenType::*;
|
||||||
@ -128,38 +101,6 @@ impl Parser<'_> {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
if self.match_token(&[Equal, BangEqual]) {
|
|
||||||
let _ = self.equality();
|
|
||||||
return Err(ParseError {
|
|
||||||
token: self.previous().clone(),
|
|
||||||
msg: "Missing left-hand operand.".to_string(),
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
if self.match_token(&[Greater, GreaterEqual, Less, LessEqual]) {
|
|
||||||
let _ = self.comparison();
|
|
||||||
return Err(ParseError {
|
|
||||||
token: self.previous().clone(),
|
|
||||||
msg: "Missing left-hand operand.".to_string(),
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
if self.match_token(&[Plus]) {
|
|
||||||
let _ = self.term();
|
|
||||||
return Err(ParseError {
|
|
||||||
token: self.previous().clone(),
|
|
||||||
msg: "Missing left-hand operand.".to_string(),
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
if self.match_token(&[Star, Slash]) {
|
|
||||||
let _ = self.factor();
|
|
||||||
return Err(ParseError {
|
|
||||||
token: self.previous().clone(),
|
|
||||||
msg: "Missing left-hand operand.".to_string(),
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
Err(ParseError {
|
Err(ParseError {
|
||||||
token: self.peek().clone(),
|
token: self.peek().clone(),
|
||||||
msg: "Expect expression.".to_string(),
|
msg: "Expect expression.".to_string(),
|
||||||
@ -178,7 +119,6 @@ impl Parser<'_> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// will not be used for the time being (per the book)
|
// will not be used for the time being (per the book)
|
||||||
// used for error recovery
|
|
||||||
fn synchronize(&mut self) {
|
fn synchronize(&mut self) {
|
||||||
use TokenType::*;
|
use TokenType::*;
|
||||||
self.advance();
|
self.advance();
|
||||||
|
@ -12,16 +12,6 @@ pub fn pretty_print(expr: &Expr) -> String {
|
|||||||
None => "None".to_string(),
|
None => "None".to_string(),
|
||||||
},
|
},
|
||||||
Expr::Unary { op, right } => parenthesize(&op.lexeme, &[right]),
|
Expr::Unary { op, right } => parenthesize(&op.lexeme, &[right]),
|
||||||
Expr::Ternary {
|
|
||||||
first,
|
|
||||||
first_op,
|
|
||||||
second,
|
|
||||||
second_op,
|
|
||||||
third,
|
|
||||||
} => parenthesize(
|
|
||||||
&format!("{}{}", first_op.lexeme, second_op.lexeme),
|
|
||||||
&[first, second, third],
|
|
||||||
),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -99,8 +99,6 @@ impl Scanner {
|
|||||||
'<' => self.add_token(TokenType::Less),
|
'<' => self.add_token(TokenType::Less),
|
||||||
'>' if self.peek_and_match('=') => self.add_token(TokenType::GreaterEqual),
|
'>' if self.peek_and_match('=') => self.add_token(TokenType::GreaterEqual),
|
||||||
'>' => self.add_token(TokenType::Greater),
|
'>' => self.add_token(TokenType::Greater),
|
||||||
'?' => self.add_token(TokenType::Question),
|
|
||||||
':' => self.add_token(TokenType::Colon),
|
|
||||||
// checking for comments and just advance the iterator if it's a comment
|
// checking for comments and just advance the iterator if it's a comment
|
||||||
'/' if self.peek_and_match('/') => {
|
'/' if self.peek_and_match('/') => {
|
||||||
while self.peek().is_some_and(|x| x != '\n') {
|
while self.peek().is_some_and(|x| x != '\n') {
|
||||||
|
@ -22,8 +22,6 @@ pub enum TokenType {
|
|||||||
GreaterEqual,
|
GreaterEqual,
|
||||||
Less,
|
Less,
|
||||||
LessEqual,
|
LessEqual,
|
||||||
Question,
|
|
||||||
Colon,
|
|
||||||
|
|
||||||
Identifier,
|
Identifier,
|
||||||
String,
|
String,
|
||||||
|
Loading…
Reference in New Issue
Block a user