Skip to content

Commit 1a56239

Browse files
committed
refactor: refactor more expressions and statements
1 parent 00790f5 commit 1a56239

26 files changed

+397
-353
lines changed

packages/engine/src/lexer/tokens.rs

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -135,23 +135,38 @@ pub enum LexerTokenValue {
135135

136136
impl LexerTokenValue {
137137
pub fn as_identifier(&self) -> Option<&String> {
138-
self.try_into().ok()
138+
match self {
139+
Self::Identifier(identifier) => Some(identifier),
140+
_ => None,
141+
}
139142
}
140143

141144
pub fn as_string(&self) -> Option<&String> {
142-
self.try_into().ok()
145+
match self {
146+
Self::String(string) => Some(string),
147+
_ => None,
148+
}
143149
}
144150

145151
pub fn as_integer(&self) -> Option<&isize> {
146-
self.try_into().ok()
152+
match self {
153+
Self::Integer(int) => Some(int),
154+
_ => None,
155+
}
147156
}
148157

149158
pub fn as_boolean(&self) -> Option<&bool> {
150-
self.try_into().ok()
159+
match self {
160+
Self::Boolean(bool) => Some(bool),
161+
_ => None,
162+
}
151163
}
152164

153165
pub fn as_shell_command(&self) -> Option<&ShellCommand> {
154-
self.try_into().ok()
166+
match self {
167+
Self::ShellCommand(cmd) => Some(cmd),
168+
_ => None,
169+
}
155170
}
156171
}
157172

@@ -186,7 +201,7 @@ impl LexerToken {
186201
}
187202

188203
pub fn as_integer(&self) -> EngineResult<&isize> {
189-
as_value!(self, LexerTokenKind::Identifier, |v| => v.as_identifier())
204+
as_value!(self, LexerTokenKind::Identifier, |v| => v.as_integer())
190205
}
191206

192207
pub fn as_boolean(&self) -> EngineResult<&bool> {

packages/engine/src/lib.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ use component::ComponentErrors;
1111
use error::EngineResult;
1212
use lexer::Lexer;
1313
use parser::Parser;
14-
use transpiler::Transpiler;
1514

1615
pub mod component;
1716
pub mod constants;
@@ -76,8 +75,8 @@ impl Engine {
7675
parser.parse();
7776
parser.print_errors();
7877

79-
let mut transpiler = Transpiler::create(&transpiler::TranspilerTarget::Bash, parser.parse());
80-
println!("---START---\n{}\n---END---", transpiler.transpile());
78+
// let mut transpiler = Transpiler::create(&transpiler::TranspilerTarget::Bash, parser.parse());
79+
// println!("---START---\n{}\n---END---", transpiler.transpile());
8180

8281

8382
Ok(0)

packages/engine/src/parser/ast.rs

Lines changed: 78 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,83 @@
1-
use super::{stmt::Statement, Parser, ParserResult};
1+
use super::{expr::ExpressionKind, stmt::{Statement, StatementKind}, Parser, ParserResult};
22

33
pub type ProgramTree = Vec<Statement>;
44

55
pub trait Parse<T> {
6-
fn parse(parser: &mut Parser) -> ParserResult<T>;
6+
fn parse(parser: &mut Parser) -> ParserResult<Option<T>>;
7+
}
8+
9+
#[macro_export]
10+
macro_rules! parseable {
11+
($name:ident = |$parser:ident| $body:block) => {
12+
impl $crate::parser::ast::Parse<$name> for $name {
13+
fn parse($parser: &mut $crate::parser::Parser) -> $crate::parser::ParserResult<Option<$name>> {
14+
#[allow(unused_imports)]
15+
use $crate::{component::ComponentIter, lexer::tokens::LexerTokenKind, parser::{expr::ExpressionKind, stmt::StatementKind, ParserErrorKind}};
16+
17+
$body
18+
}
19+
}
20+
};
21+
}
22+
23+
#[macro_export]
24+
macro_rules! parse {
25+
($parser:expr, $expr:ident = $name:ty) => {
26+
let Some($expr) = <$name>::parse($parser)? else {
27+
return Ok(None);
28+
};
29+
};
30+
}
31+
32+
#[macro_export]
33+
macro_rules! ast {
34+
($name:ident { $($param_k:ident: $param_v:ty),* $(,)* }) => {
35+
#[derive(Debug, Clone, PartialEq, Eq)]
36+
pub struct $name {
37+
$(pub $param_k: $param_v),*
38+
}
39+
};
40+
41+
($name:ident($($param:ty),*) $(,)*) => {
42+
#[derive(Debug, Clone, PartialEq, Eq)]
43+
pub struct $name($(pub $param),*);
44+
};
45+
}
46+
47+
pub trait ToExpression {
48+
fn as_expr(self) -> ExpressionKind;
49+
}
50+
51+
#[macro_export]
52+
macro_rules! as_expr {
53+
($name:ident) => {
54+
$crate::as_expr!($name = $name);
55+
};
56+
57+
($name:ident = $kind:ident) => {
58+
impl $crate::parser::ast::ToExpression for $name {
59+
fn as_expr(self) -> $crate::parser::expr::ExpressionKind {
60+
$crate::parser::expr::ExpressionKind::$kind(self)
61+
}
62+
}
63+
};
64+
}
65+
66+
pub trait ToStatement {
67+
fn as_stmt(self) -> StatementKind;
68+
}
69+
70+
#[macro_export]
71+
macro_rules! as_stmt {
72+
($name:ident) => {
73+
$crate::as_stmt!($name = $name);
74+
};
75+
76+
($name:ident = $kind:ident) => {
77+
impl $crate::parser::ast::ToStatement for $name {
78+
fn as_stmt(self) -> $crate::parser::stmt::StatementKind {
79+
$crate::parser::stmt::StatementKind::$kind(self)
80+
}
81+
}
82+
};
783
}

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

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,37 @@
1-
use crate::{component::ComponentIter, lexer::tokens::LexerTokenKind, parser::{ast::Parse, expr::Literal, ParserErrorKind}};
1+
use crate::{as_expr, ast, lexer::tokens::LexerTokenKind, parseable, parser::{expr::Identifier, ParserErrorKind}};
22

3-
use super::{Expression, ExpressionKind};
3+
use super::Expression;
44

5-
#[derive(Debug, Clone, PartialEq, Eq)]
6-
pub struct Assignment(pub Expression, pub AssignmentOperator, pub Expression);
5+
ast!(Assignment(Expression, AssignmentOperator, Expression));
6+
as_expr!(Assignment);
77

8-
impl Parse<ExpressionKind> for Assignment {
9-
fn parse(parser: &mut crate::parser::Parser) -> crate::parser::ParserResult<ExpressionKind> {
8+
parseable! {
9+
Assignment = |parser| {
1010
let lhs = parser.expr_logic_or()?;
1111

12-
if let Some(op_token) = parser.next_if_eq_mul(&[
12+
let Some(op_token) = parser.next_if_eq_mul(&[
1313
&LexerTokenKind::Equal,
1414
&LexerTokenKind::PlusEqual,
1515
&LexerTokenKind::MinusEqual,
1616
&LexerTokenKind::MultiplyEqual,
1717
&LexerTokenKind::DivideEqual,
18-
]) {
19-
let rhs = parser.expr_logic_or()?;
20-
21-
if let ExpressionKind::Literal(Literal::Identifier(identifier)) = lhs
22-
{
23-
let operator: AssignmentOperator = op_token.kind.clone().try_into()?;
24-
25-
return Ok(
26-
ExpressionKind::Assignment(Assignment(
27-
lhs,
28-
operator,
29-
rhs,
30-
)),
31-
);
32-
}
33-
}
34-
35-
Ok(lhs)
18+
]) else {
19+
return Ok(None);
20+
};
21+
22+
let rhs = parser.expr_logic_or()?;
23+
24+
let ExpressionKind::Identifier(Identifier(identifier)) = lhs else {
25+
return Ok(None);
26+
};
27+
28+
let operator: AssignmentOperator = op_token.kind.clone().try_into()?;
29+
30+
Ok(Some(Assignment(
31+
lhs,
32+
operator,
33+
rhs,
34+
)))
3635
}
3736
}
3837

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

Whitespace-only changes.

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

Lines changed: 0 additions & 5 deletions
This file was deleted.
Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
1-
use crate::{component::ComponentIter, lexer::tokens::LexerTokenKind, parser::{ast::Parse, stmt::Statement}};
1+
use crate::{as_expr, ast, parseable, parser::stmt::Statement};
22

3-
use super::ExpressionKind;
3+
ast!(Block(Vec<Statement>));
4+
as_expr!(Block);
45

5-
#[derive(Debug, Clone, PartialEq, Eq)]
6-
pub struct Block(pub Vec<Statement>);
7-
8-
impl Parse<ExpressionKind> for Block {
9-
fn parse(parser: &mut crate::parser::Parser) -> crate::parser::ParserResult<ExpressionKind> {
6+
parseable! {
7+
Block = |parser| {
108
parser.expect_terminator()?;
119

1210
let mut statements = vec![];
@@ -23,6 +21,6 @@ impl Parse<ExpressionKind> for Block {
2321
parser.next();
2422
}
2523

26-
Ok(ExpressionKind::Block(Block(statements)))
24+
Ok(Some(Block(statements)))
2725
}
28-
}
26+
}

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

Lines changed: 0 additions & 4 deletions
This file was deleted.
Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,42 @@
1+
use crate::{as_expr, ast, parseable};
2+
13
use super::{Expression, Identifier};
24

3-
#[derive(Debug, Clone, PartialEq, Eq)]
4-
pub struct FunctionCall(pub Identifier, pub Vec<Expression>);
5+
ast!(FunctionCall(Identifier, Vec<Expression>));
6+
as_expr!(FunctionCall);
7+
8+
parseable! {
9+
FunctionCall = |parser| {
10+
let expr = parser.expr_primary()?;
11+
12+
let ExpressionKind::Identifier(identifier) = &expr.value else {
13+
return Ok(None);
14+
};
15+
16+
if parser.next_if_eq(&&LexerTokenKind::LParen).is_none() {
17+
return Ok(None);
18+
}
19+
20+
let mut args = vec![];
21+
let mut end = parser.cursor;
22+
23+
while let Some(token) = parser.peek() {
24+
dbg!(token);
25+
26+
if parser.next_if_eq(&&LexerTokenKind::RParen).is_some() {
27+
end = parser.cursor;
28+
break;
29+
}
30+
31+
if parser.next_if_eq(&&LexerTokenKind::Comma).is_some() {
32+
continue;
33+
}
34+
35+
let arg = parser.expression()?;
36+
37+
args.push(arg);
38+
}
39+
40+
Ok(Some(FunctionCall(identifier, args)))
41+
}
42+
}
Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,18 @@
1+
use crate::{as_expr, ast, parseable};
2+
13
use super::Expression;
24

3-
#[derive(Debug, Clone, PartialEq, Eq)]
4-
pub struct Group(pub Expression);
5+
ast!(Group(Expression));
6+
as_expr!(Group);
7+
8+
parseable! {
9+
Group = |parser| {
10+
let mut expr = parser.expression()?;
11+
12+
parser.expect_token(&LexerTokenKind::RParen)?;
13+
14+
expr.value = ExpressionKind::Group(expr.value);
15+
16+
Ok(expr)
17+
}
18+
}

0 commit comments

Comments
 (0)