Skip to content

Commit 826323e

Browse files
committed
idk what I did
1 parent a15e775 commit 826323e

File tree

8 files changed

+67
-92
lines changed

8 files changed

+67
-92
lines changed

packages/engine/src/parser/ast.rs

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,22 +20,6 @@ macro_rules! parseable {
2020
};
2121
}
2222

23-
#[macro_export]
24-
macro_rules! parse_expr {
25-
($parser:expr, $expr:ident = $name:ty) => {
26-
let start = $parser.cursor;
27-
let kind = $crate::ok_or_none!(<$name>::parse($parser)?);
28-
let end = $parser.cursor;
29-
30-
let kind = {
31-
use $crate::parser::ast::ToExpressionKind;
32-
kind.as_expr_kind()
33-
};
34-
35-
let $expr = $crate::parser::expr::Expression::new(start, kind, end);
36-
};
37-
}
38-
3923
#[macro_export]
4024
macro_rules! ast {
4125
($name:ident { $($param_k:ident: $param_v:ty),* $(,)* }) => {
Lines changed: 4 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,16 @@
1-
use crate::{ast, parse_bin_op, parse_expr, parseable, parser::expr::{Expression, Or}, to_expr_kind};
1+
use crate::{ast, parse_bin_op, parser::expr::Expression, to_expr_kind};
22

33
ast!(And(Expression, Expression));
44
to_expr_kind!(And);
55

66
parse_bin_op! {
77
And = |parser| {
88
left = {
9-
todo!();
9+
parser.expr_cmp_equality()
1010
}
1111

1212
right = {
13-
todo!();
13+
parser.expr_cmp_equality()
1414
}
1515
}
16-
}
17-
18-
parseable! {
19-
And = |parser| {
20-
parse_expr!(parser, lhs = Or);
21-
parse_expr!(parser, rhs = Or);
22-
Ok(Some(And(lhs, rhs)))
23-
}
24-
}
25-
26-
// fn expr_logic_and(&mut self) -> ParserResult<Option<WithCursor<ExpressionKind>>> {
27-
// let_expr!(mut lhs = self.expr_cmp_equality()?);
28-
29-
// while let Some(token_and) = self.next_if_eq(&&LexerTokenKind::And) {
30-
// let_expr!(rhs = self.expr_cmp_equality()?);
31-
32-
// let operator: LogicalOperator = LogicalOperator::And;
33-
34-
// lhs = WithCursor::create_with(
35-
// lhs.start,
36-
// rhs.end,
37-
// ExpressionKind::Logic(Box::from((
38-
// lhs,
39-
// WithCursor::create_with(token_and.start, token_and.end, operator),
40-
// rhs,
41-
// ))),
42-
// );
43-
// }
44-
45-
// Ok(Some(lhs))
46-
// }
16+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
use crate::{ast, parse_bin_op, parser::expr::Expression, to_expr_kind};
2+
3+
ast!(CmpEquality(Expression, Expression));
4+
to_expr_kind!(CmpEquality);
5+
6+
parse_bin_op! {
7+
CmpEquality = |parser| {
8+
left = {
9+
parser.expr_logic_and()
10+
}
11+
12+
right = {
13+
parser.expr_logic_and()
14+
}
15+
}
16+
}

packages/engine/src/parser/expr/bin_op/mod.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use super::Expression;
44

55
pub mod or_op;
66
pub mod and_op;
7+
pub mod cmp_eq_or;
78

89
pub trait BinOp<T> {
910
fn parse_left(parser: &mut Parser) -> ParserResult<Option<Expression>>;
@@ -28,5 +29,15 @@ macro_rules! parse_bin_op {
2829
$parse_right
2930
}
3031
}
32+
33+
$crate::parseable! {
34+
$name = |parser| {
35+
use $crate::parser::expr::bin_op::BinOp;
36+
let lhs = $crate::ok_or_none!($name::parse_left(parser)?);
37+
let rhs = $crate::ok_or_none!($name::parse_right(parser)?);
38+
39+
Ok(Some($name(lhs, rhs)))
40+
}
41+
}
3142
};
3243
}
Lines changed: 3 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,16 @@
1-
use crate::{to_expr_kind, ast, parse, parse_bin_op, parseable, parser::{ast::Parse, expr::{bin_op::{and_op::And, BinOp}, Expression}}};
1+
use crate::{ast, parse_bin_op, parser::expr::Expression, to_expr_kind};
22

33
ast!(Or(Expression, Expression));
44
to_expr_kind!(Or);
55

66
parse_bin_op! {
77
Or = |parser| {
88
left = {
9-
And::parse(parser)
9+
parser.expr_logic_and()
1010
}
1111

1212
right = {
1313
parser.expr_logic_and()
1414
}
1515
}
16-
}
17-
18-
parseable! {
19-
Or = |parser| {
20-
let lhs = Or::parse_left(parser);
21-
parse!(parser, rhs = And);
22-
Ok(Some(Or(lhs, rhs)))
23-
}
24-
}
25-
26-
// let_expr!(mut lhs = self.expr_logic_and()?);
27-
28-
// while let Some(token_or) = self.next_if_eq(&&LexerTokenKind::Or) {
29-
// let_expr!(rhs = self.expr_logic_and()?);
30-
31-
// let operator: LogicalOperator = LogicalOperator::Or;
32-
33-
// lhs = WithCursor::create_with(
34-
// lhs.start,
35-
// rhs.end,
36-
// ExpressionKind::Logic(Box::from((
37-
// lhs,
38-
// WithCursor::create_with(token_or.start, token_or.end, operator),
39-
// rhs,
40-
// ))),
41-
// );
42-
// }
43-
44-
// Ok(Some(lhs))
16+
}

packages/engine/src/parser/expr/expr_identifier.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ use crate::{ast, lexer::tokens::LexerToken, parseable, parser::ParserErrorKind,
33
ast!(Identifier(String));
44
to_expr_kind!(Identifier);
55

6-
parseable!(
6+
parseable! {
77
Identifier = |parser| {
8-
parser.exp
8+
todo!()
99
}
10-
)
10+
}
1111

1212
impl TryFrom<LexerToken> for Identifier {
1313
type Error = ParserErrorKind;

packages/engine/src/parser/expr/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ mod expr_shell_command;
1212
mod expr_unary;
1313
mod expr_identifier;
1414

15-
use bin_op::and_op::And;
15+
pub use bin_op::and_op::And;
1616
pub use bin_op::or_op::Or;
1717
pub use expr_identifier::Identifier;
1818
pub use literal::{boolean::BooleanLiteral, integer::IntegerLiteral, string::StringLiteral};

packages/engine/src/parser/mod.rs

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use std::{iter::Peekable, slice::Iter};
22

3-
use ast::ProgramTree;
3+
use ast::{Parse, ProgramTree};
44
use error::ParserResult;
5-
use expr::Expression;
5+
use expr::*;
66
use stmt::Statement;
77

88
use crate::{
@@ -31,6 +31,21 @@ pub struct Parser<'a> {
3131
source: crate::error::SourceFile,
3232
}
3333

34+
macro_rules! parse_expr {
35+
($parser:expr, $name:ty) => {{
36+
let start = $parser.cursor;
37+
let kind = $crate::ok_or_none!(<$name>::parse($parser)?);
38+
let end = $parser.cursor;
39+
40+
let kind = {
41+
use $crate::parser::ast::ToExpressionKind;
42+
kind.as_expr_kind()
43+
};
44+
45+
$crate::parser::expr::Expression::new(start, kind, end)
46+
}};
47+
}
48+
3449
impl<'a> Parser<'a> {
3550
pub fn create(
3651
tokens: &'a LexerTokenList,
@@ -114,15 +129,22 @@ impl<'a> Parser<'a> {
114129
todo!()
115130
}
116131

117-
// // MARK: Assignment
132+
// MARK: Assignment
118133
// fn expr_assignment(&mut self) -> ParserResult<Option<WithCursor<ExpressionKind>>> {
119134

120135
// }
121136

122-
// // MARK: Logic OR
123-
// fn expr_logic_or(&mut self) -> ParserResult<Option<WithCursor<ExpressionKind>>> {
124-
125-
// }
137+
fn expr_logic_or(&mut self) -> ParserResult<Option<Expression>> {
138+
Ok(Some(parse_expr!(self, Or)))
139+
}
140+
141+
fn expr_logic_and(&mut self) -> ParserResult<Option<Expression>> {
142+
Ok(Some(parse_expr!(self, And)))
143+
}
144+
145+
fn expr_cmp_equality(&mut self) -> ParserResult<Option<Expression>> {
146+
Ok(Some(parse_expr!(self, CmpEquality)))
147+
}
126148

127149
// // MARK: Logic AND
128150
// fn expr_logic_and(&mut self) -> ParserResult<Option<WithCursor<ExpressionKind>>> {

0 commit comments

Comments
 (0)