Skip to content

Commit d9170c9

Browse files
committed
no idea
1 parent 1a56239 commit d9170c9

34 files changed

+230
-91
lines changed

packages/engine/src/parser/ast.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -44,38 +44,38 @@ macro_rules! ast {
4444
};
4545
}
4646

47-
pub trait ToExpression {
48-
fn as_expr(self) -> ExpressionKind;
47+
pub trait ToExpressionKind {
48+
fn as_expr_kind(self) -> ExpressionKind;
4949
}
5050

5151
#[macro_export]
52-
macro_rules! as_expr {
52+
macro_rules! to_expr_kind {
5353
($name:ident) => {
54-
$crate::as_expr!($name = $name);
54+
$crate::to_expr_kind!($name = $name);
5555
};
5656

5757
($name:ident = $kind:ident) => {
58-
impl $crate::parser::ast::ToExpression for $name {
59-
fn as_expr(self) -> $crate::parser::expr::ExpressionKind {
58+
impl $crate::parser::ast::ToExpressionKind for $name {
59+
fn as_expr_kind(self) -> $crate::parser::expr::ExpressionKind {
6060
$crate::parser::expr::ExpressionKind::$kind(self)
6161
}
6262
}
6363
};
6464
}
6565

66-
pub trait ToStatement {
67-
fn as_stmt(self) -> StatementKind;
66+
pub trait ToStatementKind {
67+
fn as_stmt_kind(self) -> StatementKind;
6868
}
6969

7070
#[macro_export]
71-
macro_rules! as_stmt {
71+
macro_rules! as_stmt_kind {
7272
($name:ident) => {
73-
$crate::as_stmt!($name = $name);
73+
$crate::as_stmt_kind!($name = $name);
7474
};
7575

7676
($name:ident = $kind:ident) => {
77-
impl $crate::parser::ast::ToStatement for $name {
78-
fn as_stmt(self) -> $crate::parser::stmt::StatementKind {
77+
impl $crate::parser::ast::ToStatementKind for $name {
78+
fn as_stmt_kind(self) -> $crate::parser::stmt::StatementKind {
7979
$crate::parser::stmt::StatementKind::$kind(self)
8080
}
8181
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
use crate::{lexer::tokens::LexerTokenKind, parser::ParserErrorKind};
1+
use crate::{to_expr_kind, ast, lexer::tokens::LexerTokenKind, parser::ParserErrorKind};
22

33
use super::Expression;
44

5-
#[derive(Debug, Clone, PartialEq, Eq)]
6-
pub struct Arithmetic(pub Expression, pub ArithmeticOperator, pub Expression);
5+
ast!(Arithmetic(Expression, ArithmeticOperator, Expression));
6+
to_expr_kind!(Arithmetic);
77

88
#[derive(lang_macro::EnumVariants, Debug, Clone, Copy, PartialEq, Eq)]
99
pub enum ArithmeticOperator {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
use crate::{as_expr, ast, lexer::tokens::LexerTokenKind, parseable, parser::{expr::Identifier, ParserErrorKind}};
1+
use crate::{to_expr_kind, ast, lexer::tokens::LexerTokenKind, parseable, parser::{expr::Identifier, ParserErrorKind}};
22

33
use super::Expression;
44

55
ast!(Assignment(Expression, AssignmentOperator, Expression));
6-
as_expr!(Assignment);
6+
to_expr_kind!(Assignment);
77

88
parseable! {
99
Assignment = |parser| {
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
use crate::{to_expr_kind, ast, parse, parse_bin_op, parseable, parser::{ast::ToExpressionKind, expr::{bin_op::BinOp, Expression, Or}}};
2+
3+
ast!(And(Expression, Expression));
4+
to_expr_kind!(And);
5+
6+
parse_bin_op! {
7+
And = |parser| {
8+
left = {
9+
todo!();
10+
}
11+
12+
right = {
13+
todo!();
14+
}
15+
}
16+
}
17+
18+
parseable! {
19+
And = |parser| {
20+
parse!(parser, lhs = Or);
21+
parse!(parser, rhs = Or);
22+
Ok(Some(And(lhs.as_expr_kind(), rhs.as_expr_kind())))
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))
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
use crate::parser::{Parser, ParserResult};
2+
3+
use super::Expression;
4+
5+
pub mod or_op;
6+
pub mod and_op;
7+
8+
pub trait BinOp<T> {
9+
fn parse_left(parser: &mut Parser) -> ParserResult<Option<Expression>>;
10+
fn parse_right(parser: &mut Parser) -> ParserResult<Option<Expression>>;
11+
}
12+
13+
#[macro_export]
14+
macro_rules! parse_bin_op {
15+
($name:ident = |$parser:ident| { left = $parse_left:block right = $parse_right:block }) => {
16+
impl $crate::parser::expr::bin_op::BinOp<$name> for $name {
17+
fn parse_left($parser: &mut $crate::parser::Parser) -> $crate::parser::ParserResult<Option<Expression>> {
18+
#[allow(unused_imports)]
19+
use $crate::{component::ComponentIter, lexer::tokens::LexerTokenKind, parser::{expr::ExpressionKind, stmt::StatementKind, ParserErrorKind}};
20+
21+
$parse_left
22+
}
23+
24+
fn parse_right($parser: &mut $crate::parser::Parser) -> $crate::parser::ParserResult<Option<Expression>> {
25+
#[allow(unused_imports)]
26+
use $crate::{component::ComponentIter, lexer::tokens::LexerTokenKind, parser::{expr::ExpressionKind, stmt::StatementKind, ParserErrorKind}};
27+
28+
$parse_right
29+
}
30+
}
31+
};
32+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
use crate::{to_expr_kind, ast, parse, parse_bin_op, parseable, parser::{ast::Parse, expr::{bin_op::{and_op::And, BinOp}, Expression}}};
2+
3+
ast!(Or(Expression, Expression));
4+
to_expr_kind!(Or);
5+
6+
parse_bin_op! {
7+
Or = |parser| {
8+
left = {
9+
And::parse(parser)
10+
}
11+
12+
right = {
13+
parser.expr_logic_and()
14+
}
15+
}
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))

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
use crate::{as_expr, ast, parseable, parser::stmt::Statement};
1+
use crate::{to_expr_kind, ast, parseable, parser::stmt::Statement};
22

33
ast!(Block(Vec<Statement>));
4-
as_expr!(Block);
4+
to_expr_kind!(Block);
55

66
parseable! {
77
Block = |parser| {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
use crate::{as_expr, ast, parseable};
1+
use crate::{to_expr_kind, ast, parseable};
22

33
use super::{Expression, Identifier};
44

55
ast!(FunctionCall(Identifier, Vec<Expression>));
6-
as_expr!(FunctionCall);
6+
to_expr_kind!(FunctionCall);
77

88
parseable! {
99
FunctionCall = |parser| {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
use crate::{as_expr, ast, parseable};
1+
use crate::{to_expr_kind, ast, parseable};
22

33
use super::Expression;
44

55
ast!(Group(Expression));
6-
as_expr!(Group);
6+
to_expr_kind!(Group);
77

88
parseable! {
99
Group = |parser| {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
use crate::{as_expr, ast, lexer::tokens::LexerToken, parser::ParserErrorKind};
1+
use crate::{to_expr_kind, ast, lexer::tokens::LexerToken, parser::ParserErrorKind};
22

33
ast!(Identifier(String));
4-
as_expr!(Identifier);
4+
to_expr_kind!(Identifier);
55

66
impl TryFrom<LexerToken> for Identifier {
77
type Error = ParserErrorKind;

0 commit comments

Comments
 (0)