Skip to content

Commit 5d94fc5

Browse files
committed
refactor: refactor parser ast into separate modules
1 parent ff0fd08 commit 5d94fc5

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+654
-318
lines changed

packages/cli/src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ fn main() {
1111

1212
debug!("initialized logger");
1313

14-
let mut engine = Engine::default();
14+
let mut engine = Engine::create();
1515

1616

1717
let args: Vec<String> = std::env::args().collect();

packages/engine/src/lexer/mod.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,7 @@ use error::LexerResult;
44
use tokens::{LexerLiteral, LexerToken, LexerTokenKind, LexerTokenList};
55

66
use crate::{
7-
component::{ComponentErrors, ComponentIter},
8-
constants::{MAX_I32_LEN, MAX_I64_LEN},
9-
error::{EngineErrorKind, ErrorList},
10-
cursor::Cursor,
7+
component::{ComponentErrors, ComponentIter}, constants::{MAX_I32_LEN, MAX_I64_LEN}, cursor::Cursor, error::{EngineErrorKind, ErrorList}, parser::expr::ShellCommand
118
};
129

1310
pub use error::{LexerError, LexerErrorKind};
@@ -326,7 +323,7 @@ impl<'a> Lexer<'a> {
326323

327324
Ok((
328325
LexerTokenKind::ShellCommand,
329-
Some(Box::from(LexerLiteral::ShellCommand(Box::from((cmd_name, cmd_args))))),
326+
Some(Box::from(LexerLiteral::ShellCommand(Box::from(ShellCommand(cmd_name, cmd_args))))),
330327
))
331328
}
332329

packages/engine/src/lexer/tokens.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@ use std::fmt::Display;
33
use lang_macro::EnumVariantsTrait;
44

55
use crate::{
6-
error::{EngineErrorKind, EngineResult},
7-
cursor::Cursor,
6+
cursor::Cursor, error::{EngineErrorKind, EngineResult}, parser::expr::ShellCommand
87
};
98

109
#[repr(u8)]
@@ -171,8 +170,6 @@ impl LexerLiteral {
171170
}
172171
}
173172

174-
pub type ShellCommand = (String, Option<String>);
175-
176173
#[derive(Debug, PartialEq, Clone)]
177174
pub struct LexerToken {
178175
pub kind: LexerTokenKind,

packages/engine/src/lib.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,26 @@
11
#![allow(incomplete_features)]
22
#![feature(try_blocks)]
33
#![feature(guard_patterns)]
4+
#![feature(trait_alias)]
45

56
#[macro_use]
67
extern crate log;
78

89
use std::path::PathBuf;
9-
1010
use component::ComponentErrors;
1111
use error::EngineResult;
1212
use lexer::Lexer;
13+
use parser::Parser;
14+
use transpiler::Transpiler;
1315

1416
pub mod component;
1517
pub mod constants;
1618
pub mod cursor;
1719
pub mod error;
1820
pub mod lexer;
1921
pub mod parser;
22+
pub mod transpiler;
2023

21-
use parser::Parser;
22-
23-
#[derive(Default)]
2424
pub struct Engine {}
2525

2626
impl Engine {
@@ -73,12 +73,12 @@ impl Engine {
7373
source_file,
7474
);
7575

76-
let ast = parser.parse();
77-
78-
println!("{:#?}", &ast);
79-
76+
parser.parse();
8077
parser.print_errors();
8178

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

8383
Ok(0)
8484
}

packages/engine/src/parser/ast.rs

Lines changed: 5 additions & 184 deletions
Original file line numberDiff line numberDiff line change
@@ -1,186 +1,7 @@
1-
use std::{collections::HashMap, fmt::Debug, rc::Rc};
1+
use super::{stmt::Statement, Parser, ParserResult};
22

3-
use crate::{cursor::WithCursor, lexer::tokens::{LexerToken, LexerTokenKind, ShellCommand}};
3+
pub type ProgramTree = Vec<Statement>;
44

5-
use super::ParserErrorKind;
6-
7-
#[derive(lang_macro::EnumVariants, Debug, Clone, PartialEq, Eq)]
8-
pub enum Expression {
9-
Literal(Box<Literal>),
10-
Group(Box<Expression>),
11-
Unary(Box<(WithCursor<UnaryOperator>, WithCursor<Expression>)>),
12-
Arithmetic(Box<(WithCursor<Expression>, WithCursor<ArithmeticOperator>, WithCursor<Expression>)>),
13-
Logical(Box<(WithCursor<Expression>, WithCursor<LogicalOperator>, WithCursor<Expression>)>),
14-
Assignment(Box<(WithCursor<Expression>, WithCursor<AssignmentOperator>, WithCursor<Expression>)>),
15-
Range(Box<(WithCursor<Expression>, WithCursor<Expression>, bool)>),
16-
ShellCommand(Box<ShellCommand>),
17-
Identifier(Box<Identifier>),
18-
FunctionCall(Box<(Identifier, Vec<WithCursor<Expression>>)>),
19-
If(Box<(WithCursor<Expression>, WithCursor<Block>, Option<Else>)>),
20-
Match(Box<(WithCursor<Expression>, MatchCase)>),
21-
Block(Box<Block>),
22-
}
23-
24-
pub type Else = WithCursor<Block>;
25-
pub type MatchCase = HashMap<WithCursor<Literal>, Rc<WithCursor<Expression>>>;
26-
27-
28-
#[derive(lang_macro::EnumVariants, Debug, Clone, PartialEq, Eq)]
29-
pub enum Statement {
30-
While(Box<(WithCursor<Expression>, WithCursor<Block>)>),
31-
For(Box<(Variable, WithCursor<Expression>, WithCursor<Block>)>),
32-
Return(Box<Option<WithCursor<Expression>>>),
33-
If(Box<WithCursor<Expression>>),
34-
Match(Box<WithCursor<Expression>>),
35-
Expression(Box<WithCursor<Expression>>),
36-
Continue,
37-
Break,
38-
Variable(Box<Variable>),
39-
Constant(Box<Variable>),
40-
Function(Box<Function>),
41-
Include(Box<String>),
42-
}
43-
44-
45-
46-
pub type ProgramTree = Block;
47-
pub type Identifier = String;
48-
pub type Block = Vec<Statement>;
49-
50-
#[derive(lang_macro::EnumVariants, Debug, Clone, PartialEq, Eq, Hash)]
51-
pub enum Literal {
52-
Integer(isize),
53-
Boolean(bool),
54-
String(Box<String>),
55-
}
56-
57-
impl TryFrom<LexerToken> for Literal {
58-
type Error = crate::parser::ParserErrorKind;
59-
60-
fn try_from(value: LexerToken) -> Result<Self, Self::Error> {
61-
Ok(match value.kind {
62-
LexerTokenKind::Integer => Self::Integer(*value.as_integer()?),
63-
LexerTokenKind::Boolean => Self::Boolean(*value.as_boolean()?),
64-
LexerTokenKind::String => Self::String(Box::from(value.as_string()?.to_owned())),
65-
_ => return Err(ParserErrorKind::ConvertError(value.kind))
66-
})
67-
}
68-
}
69-
70-
71-
72-
#[derive(lang_macro::EnumVariants, Debug, Clone, Copy, PartialEq, Eq)]
73-
pub enum ArithmeticOperator {
74-
Add,
75-
Subtract,
76-
Multiply,
77-
Divide,
78-
}
79-
80-
impl TryFrom<LexerTokenKind> for ArithmeticOperator {
81-
type Error = crate::parser::ParserErrorKind;
82-
83-
fn try_from(value: LexerTokenKind) -> Result<Self, Self::Error> {
84-
Ok(match value {
85-
LexerTokenKind::Plus => Self::Add,
86-
LexerTokenKind::Minus => Self::Subtract,
87-
LexerTokenKind::Multiply => Self::Multiply,
88-
LexerTokenKind::Divide => Self::Divide,
89-
_ => return Err(ParserErrorKind::ConvertError(value))
90-
})
91-
}
92-
}
93-
94-
95-
96-
#[derive(lang_macro::EnumVariants, Debug, Clone, Copy, PartialEq, Eq)]
97-
pub enum UnaryOperator {
98-
Not,
99-
Negative,
100-
}
101-
102-
impl TryFrom<LexerTokenKind> for UnaryOperator {
103-
type Error = crate::parser::ParserErrorKind;
104-
105-
fn try_from(value: LexerTokenKind) -> Result<Self, Self::Error> {
106-
Ok(match value {
107-
LexerTokenKind::Not => Self::Not,
108-
LexerTokenKind::Minus => Self::Negative,
109-
_ => return Err(ParserErrorKind::ConvertError(value))
110-
})
111-
}
112-
}
113-
114-
115-
116-
#[derive(lang_macro::EnumVariants, Debug, Clone, Copy, PartialEq, Eq)]
117-
pub enum LogicalOperator {
118-
Equal,
119-
NotEqual,
120-
LesserThan,
121-
LesserEqualThan,
122-
GreaterThan,
123-
GreaterEqualThan,
124-
And,
125-
Or
126-
}
127-
128-
impl TryFrom<LexerTokenKind> for LogicalOperator {
129-
type Error = crate::parser::ParserErrorKind;
130-
131-
fn try_from(value: LexerTokenKind) -> Result<Self, Self::Error> {
132-
Ok(match value {
133-
LexerTokenKind::EqualEqual => Self::Equal,
134-
LexerTokenKind::NotEqual => Self::NotEqual,
135-
LexerTokenKind::LesserThan => Self::LesserThan,
136-
LexerTokenKind::LesserEqualThan => Self::LesserEqualThan,
137-
LexerTokenKind::GreaterThan => Self::GreaterThan,
138-
LexerTokenKind::GreaterEqualThan => Self::GreaterEqualThan,
139-
LexerTokenKind::And => Self::And,
140-
LexerTokenKind::Or => Self::Or,
141-
_ => return Err(ParserErrorKind::ConvertError(value))
142-
})
143-
}
144-
}
145-
146-
147-
#[derive(lang_macro::EnumVariants, Debug, Clone, Copy, PartialEq, Eq)]
148-
pub enum AssignmentOperator {
149-
PlusAssign,
150-
MinusAssign,
151-
MultiplyAssign,
152-
DivideAssign,
153-
Assign
154-
}
155-
156-
impl TryFrom<LexerTokenKind> for AssignmentOperator {
157-
type Error = crate::parser::ParserErrorKind;
158-
159-
fn try_from(value: LexerTokenKind) -> Result<Self, Self::Error> {
160-
Ok(match value {
161-
LexerTokenKind::PlusEqual => Self::PlusAssign,
162-
LexerTokenKind::MinusEqual => Self::MinusAssign,
163-
LexerTokenKind::MultiplyEqual => Self::MultiplyAssign,
164-
LexerTokenKind::DivideEqual => Self::DivideAssign,
165-
LexerTokenKind::Equal => Self::Assign,
166-
_ => return Err(ParserErrorKind::ConvertError(value))
167-
})
168-
}
169-
}
170-
171-
172-
173-
#[derive(Debug, Clone, PartialEq, Eq)]
174-
pub struct Function {
175-
pub name: String,
176-
pub parameters: Option<Vec<Variable>>,
177-
pub strict_type: Option<String>,
178-
pub body: WithCursor<Block>,
179-
}
180-
181-
#[derive(Debug, Clone, PartialEq, Eq)]
182-
pub struct Variable {
183-
pub name: String,
184-
pub strict_type: Option<String>,
185-
pub value: Option<WithCursor<Expression>>,
186-
}
5+
pub trait Parse<T> {
6+
fn parse(parser: &mut Parser) -> ParserResult<T>;
7+
}

packages/engine/src/parser/error.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,9 @@ pub struct ParserError {
3030
pub end: Cursor,
3131

3232
#[cfg(feature = "cli")]
33-
pub source_file: SourceFile,
33+
pub source_file: crate::error::SourceFile,
3434
}
3535

36-
#[cfg(feature = "cli")]
37-
pub(super) type SourceFile = Box<(Option<std::path::PathBuf>, String)>;
3836
pub(super) type ParserResult<T> = std::result::Result<T, ParserErrorKind>;
3937

4038
#[cfg(feature = "cli")]
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
use crate::{lexer::tokens::LexerTokenKind, parser::ParserErrorKind};
2+
3+
use super::Expression;
4+
5+
#[derive(Debug, Clone, PartialEq, Eq)]
6+
pub struct Arithmetic(pub Expression, pub ArithmeticOperator, pub Expression);
7+
8+
#[derive(lang_macro::EnumVariants, Debug, Clone, Copy, PartialEq, Eq)]
9+
pub enum ArithmeticOperator {
10+
Add,
11+
Subtract,
12+
Multiply,
13+
Divide,
14+
}
15+
16+
impl TryFrom<LexerTokenKind> for ArithmeticOperator {
17+
type Error = ParserErrorKind;
18+
19+
fn try_from(value: LexerTokenKind) -> Result<Self, Self::Error> {
20+
Ok(match value {
21+
LexerTokenKind::Plus => Self::Add,
22+
LexerTokenKind::Minus => Self::Subtract,
23+
LexerTokenKind::Multiply => Self::Multiply,
24+
LexerTokenKind::Divide => Self::Divide,
25+
_ => return Err(ParserErrorKind::ConvertError(value))
26+
})
27+
}
28+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
use crate::{lexer::tokens::LexerTokenKind, parser::ParserErrorKind};
2+
3+
use super::Expression;
4+
5+
#[derive(Debug, Clone, PartialEq, Eq)]
6+
pub struct Assignment(pub Expression, pub AssignmentOperator, pub Expression);
7+
8+
#[derive(lang_macro::EnumVariants, Debug, Clone, Copy, PartialEq, Eq)]
9+
pub enum AssignmentOperator {
10+
PlusAssign,
11+
MinusAssign,
12+
MultiplyAssign,
13+
DivideAssign,
14+
Assign
15+
}
16+
17+
impl TryFrom<LexerTokenKind> for AssignmentOperator {
18+
type Error = ParserErrorKind;
19+
20+
fn try_from(value: LexerTokenKind) -> Result<Self, Self::Error> {
21+
Ok(match value {
22+
LexerTokenKind::PlusEqual => Self::PlusAssign,
23+
LexerTokenKind::MinusEqual => Self::MinusAssign,
24+
LexerTokenKind::MultiplyEqual => Self::MultiplyAssign,
25+
LexerTokenKind::DivideEqual => Self::DivideAssign,
26+
LexerTokenKind::Equal => Self::Assign,
27+
_ => return Err(ParserErrorKind::ConvertError(value))
28+
})
29+
}
30+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
use crate::parser::stmt::Statement;
2+
3+
#[derive(Debug, Clone, PartialEq, Eq)]
4+
pub struct Block(pub Vec<Statement>);
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
use super::{Block, Expression};
2+
3+
#[derive(Debug, Clone, PartialEq, Eq)]
4+
pub struct ElseExpr(pub Option<Expression>, pub Block, pub Option<Box<ElseExpr>>);

0 commit comments

Comments
 (0)