@devaloop/devalang 0.0.1-alpha.7 → 0.0.1-alpha.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (86) hide show
  1. package/Cargo.toml +1 -1
  2. package/README.md +31 -16
  3. package/docs/CHANGELOG.md +49 -2
  4. package/docs/ROADMAP.md +2 -2
  5. package/docs/SYNTAX.md +41 -7
  6. package/docs/TODO.md +3 -3
  7. package/examples/condition.deva +20 -0
  8. package/examples/group.deva +3 -3
  9. package/examples/index.deva +9 -8
  10. package/examples/loop.deva +10 -8
  11. package/examples/synth.deva +14 -0
  12. package/examples/variables.deva +2 -2
  13. package/out-tsc/bin/devalang.exe +0 -0
  14. package/out-tsc/scripts/version/fetch.js +1 -5
  15. package/package.json +1 -1
  16. package/project-version.json +3 -3
  17. package/rust/cli/build.rs +6 -1
  18. package/rust/core/audio/engine.rs +89 -12
  19. package/rust/core/audio/evaluator.rs +31 -0
  20. package/rust/core/audio/interpreter/arrow_call.rs +129 -0
  21. package/rust/core/audio/interpreter/call.rs +64 -0
  22. package/rust/core/audio/interpreter/condition.rs +69 -0
  23. package/rust/core/audio/interpreter/driver.rs +216 -0
  24. package/rust/core/audio/interpreter/let_.rs +19 -0
  25. package/rust/core/audio/interpreter/load.rs +18 -0
  26. package/rust/core/audio/interpreter/loop_.rs +67 -0
  27. package/rust/core/audio/interpreter/mod.rs +12 -0
  28. package/rust/core/audio/interpreter/sleep.rs +36 -0
  29. package/rust/core/audio/interpreter/spawn.rs +66 -0
  30. package/rust/core/audio/interpreter/tempo.rs +16 -0
  31. package/rust/core/audio/interpreter/trigger.rs +69 -0
  32. package/rust/core/audio/loader/mod.rs +1 -0
  33. package/rust/core/audio/{loader.rs → loader/trigger.rs} +3 -1
  34. package/rust/core/audio/mod.rs +2 -1
  35. package/rust/core/audio/renderer.rs +54 -0
  36. package/rust/core/builder/mod.rs +1 -1
  37. package/rust/core/debugger/lexer.rs +1 -1
  38. package/rust/core/debugger/mod.rs +1 -0
  39. package/rust/core/debugger/store.rs +25 -0
  40. package/rust/core/error/mod.rs +1 -1
  41. package/rust/core/lexer/handler/arrow.rs +31 -0
  42. package/rust/core/lexer/handler/driver.rs +226 -0
  43. package/rust/core/lexer/handler/identifier.rs +3 -0
  44. package/rust/core/lexer/handler/mod.rs +4 -227
  45. package/rust/core/lexer/handler/operator.rs +44 -0
  46. package/rust/core/lexer/mod.rs +25 -4
  47. package/rust/core/lexer/token.rs +40 -9
  48. package/rust/core/parser/driver.rs +331 -0
  49. package/rust/core/parser/handler/arrow_call.rs +126 -0
  50. package/rust/core/parser/handler/at.rs +3 -7
  51. package/rust/core/parser/handler/bank.rs +5 -2
  52. package/rust/core/parser/handler/condition.rs +74 -0
  53. package/rust/core/parser/handler/dot.rs +1 -1
  54. package/rust/core/parser/handler/identifier/call.rs +41 -0
  55. package/rust/core/parser/handler/identifier/group.rs +75 -0
  56. package/rust/core/parser/handler/identifier/let_.rs +133 -0
  57. package/rust/core/parser/handler/identifier/mod.rs +51 -0
  58. package/rust/core/parser/handler/identifier/sleep.rs +33 -0
  59. package/rust/core/parser/handler/identifier/spawn.rs +41 -0
  60. package/rust/core/parser/handler/identifier/synth.rs +65 -0
  61. package/rust/core/parser/handler/loop_.rs +25 -19
  62. package/rust/core/parser/handler/mod.rs +3 -1
  63. package/rust/core/parser/handler/tempo.rs +1 -1
  64. package/rust/core/parser/mod.rs +3 -237
  65. package/rust/core/parser/statement.rs +36 -35
  66. package/rust/core/preprocessor/loader.rs +64 -49
  67. package/rust/core/preprocessor/module.rs +3 -6
  68. package/rust/core/preprocessor/processor.rs +13 -4
  69. package/rust/core/preprocessor/resolver/call.rs +123 -0
  70. package/rust/core/preprocessor/resolver/condition.rs +92 -0
  71. package/rust/core/preprocessor/resolver/driver.rs +227 -0
  72. package/rust/core/preprocessor/resolver/group.rs +35 -87
  73. package/rust/core/preprocessor/resolver/let_.rs +31 -0
  74. package/rust/core/preprocessor/resolver/loop_.rs +62 -116
  75. package/rust/core/preprocessor/resolver/mod.rs +9 -153
  76. package/rust/core/preprocessor/resolver/spawn.rs +58 -0
  77. package/rust/core/preprocessor/resolver/synth.rs +50 -0
  78. package/rust/core/preprocessor/resolver/trigger.rs +51 -50
  79. package/rust/core/preprocessor/resolver/value.rs +78 -0
  80. package/rust/core/utils/path.rs +17 -32
  81. package/rust/core/utils/validation.rs +30 -28
  82. package/typescript/scripts/version/fetch.ts +1 -6
  83. package/rust/core/audio/interpreter.rs +0 -317
  84. package/rust/core/audio/render.rs +0 -53
  85. package/rust/core/lexer/handler/equal.rs +0 -32
  86. package/rust/core/parser/handler/identifier.rs +0 -260
@@ -0,0 +1,331 @@
1
+ use crate::core::{
2
+ lexer::token::{ Token, TokenKind },
3
+ parser::{
4
+ handler::{
5
+ arrow_call::parse_arrow_call,
6
+ at::parse_at_token,
7
+ bank::parse_bank_token,
8
+ condition::parse_condition_token,
9
+ dot::parse_dot_token,
10
+ identifier::parse_identifier_token,
11
+ loop_::parse_loop_token,
12
+ tempo::parse_tempo_token,
13
+ },
14
+ statement::Statement,
15
+ },
16
+ shared::value::Value,
17
+ store::global::GlobalStore,
18
+ };
19
+
20
+ #[derive(Debug, Clone, PartialEq)]
21
+ pub struct Parser {
22
+ pub resolve_modules: bool,
23
+ pub tokens: Vec<Token>,
24
+ pub token_index: usize,
25
+ pub current_module: String,
26
+ pub previous: Option<Token>,
27
+ }
28
+
29
+ impl Parser {
30
+ pub fn new() -> Self {
31
+ Parser {
32
+ resolve_modules: false,
33
+ tokens: Vec::new(),
34
+ token_index: 0,
35
+ current_module: String::new(),
36
+ previous: None,
37
+ }
38
+ }
39
+
40
+ pub fn set_current_module(&mut self, module_path: String) {
41
+ self.current_module = module_path;
42
+ }
43
+
44
+ pub fn advance(&mut self) -> Option<&Token> {
45
+ if self.is_eof() {
46
+ return None;
47
+ }
48
+
49
+ self.previous = self.tokens.get(self.token_index).cloned();
50
+ self.token_index += 1;
51
+
52
+ self.tokens.get(self.token_index - 1)
53
+ }
54
+
55
+ pub fn peek_is(&self, expected: &str) -> bool {
56
+ self.peek().map_or(false, |t| t.lexeme == expected)
57
+ }
58
+
59
+ pub fn peek_nth(&self, n: usize) -> Option<&Token> {
60
+ if self.token_index + n < self.tokens.len() {
61
+ self.tokens.get(self.token_index + n)
62
+ } else {
63
+ None
64
+ }
65
+ }
66
+
67
+ pub fn advance_if(&mut self, kind: TokenKind) -> bool {
68
+ if self.match_token(kind) { true } else { false }
69
+ }
70
+
71
+ pub fn match_token(&mut self, kind: TokenKind) -> bool {
72
+ if let Some(tok) = self.peek() {
73
+ if tok.kind == kind {
74
+ self.advance();
75
+ return true;
76
+ }
77
+ }
78
+ false
79
+ }
80
+
81
+ pub fn previous_clone(&self) -> Option<Token> {
82
+ self.previous.clone()
83
+ }
84
+
85
+ pub fn parse_block(
86
+ &self,
87
+ tokens: Vec<Token>,
88
+ global_store: &mut GlobalStore
89
+ ) -> Vec<Statement> {
90
+ let mut inner_parser = Parser {
91
+ resolve_modules: self.resolve_modules,
92
+ tokens,
93
+ token_index: 0,
94
+ current_module: self.current_module.clone(),
95
+ previous: None,
96
+ };
97
+
98
+ inner_parser.parse_tokens(inner_parser.tokens.clone(), global_store)
99
+ }
100
+
101
+ pub fn parse_tokens(
102
+ &mut self,
103
+ tokens: Vec<Token>,
104
+ global_store: &mut GlobalStore
105
+ ) -> Vec<Statement> {
106
+ self.tokens = tokens;
107
+ self.token_index = 0;
108
+
109
+ let mut statements = Vec::new();
110
+
111
+ while !self.is_eof() {
112
+ let token = match self.peek() {
113
+ Some(t) => t.clone(),
114
+ None => {
115
+ break;
116
+ }
117
+ };
118
+
119
+ let statement = match &token.kind {
120
+ TokenKind::At => parse_at_token(self, global_store),
121
+ TokenKind::Identifier => {
122
+ if let Some(next) = self.peek_nth(1).cloned() {
123
+ if next.kind == TokenKind::Arrow {
124
+ parse_arrow_call(self, global_store)
125
+ } else {
126
+ parse_identifier_token(self, global_store)
127
+ }
128
+ } else {
129
+ parse_identifier_token(self, global_store)
130
+ }
131
+ }
132
+ TokenKind::Dot => parse_dot_token(self, global_store),
133
+ TokenKind::Tempo => parse_tempo_token(self, global_store),
134
+ TokenKind::Bank => parse_bank_token(self, global_store),
135
+ TokenKind::Loop => parse_loop_token(self, global_store),
136
+ TokenKind::If => parse_condition_token(self, global_store),
137
+
138
+ | TokenKind::Else // Ignore else, already handled in `parse_condition_token`
139
+ | TokenKind::Comment
140
+ | TokenKind::Equals
141
+ | TokenKind::Colon
142
+ | TokenKind::Number
143
+ | TokenKind::String
144
+ | TokenKind::LBrace
145
+ | TokenKind::RBrace
146
+ | TokenKind::Comma
147
+ | TokenKind::Newline
148
+ | TokenKind::Dedent
149
+ | TokenKind::Indent => {
150
+ self.advance();
151
+ continue;
152
+ }
153
+
154
+ TokenKind::EOF => {
155
+ break;
156
+ }
157
+
158
+ _ => {
159
+ println!("Unhandled token: {:?}", token);
160
+ self.advance();
161
+ Statement::unknown()
162
+ }
163
+ };
164
+
165
+ statements.push(statement);
166
+ }
167
+
168
+ statements
169
+ }
170
+
171
+ pub fn check_token(&self, kind: TokenKind) -> bool {
172
+ self.peek().map_or(false, |t| t.kind == kind)
173
+ }
174
+
175
+ pub fn parse_map_value(&mut self) -> Option<Value> {
176
+ if !self.match_token(TokenKind::LBrace) {
177
+ return None;
178
+ }
179
+
180
+ let mut map = std::collections::HashMap::new();
181
+
182
+ while !self.check_token(TokenKind::RBrace) && !self.is_eof() {
183
+ let key = if let Some(token) = self.advance() {
184
+ token.lexeme.clone()
185
+ } else {
186
+ break;
187
+ };
188
+
189
+ if !self.match_token(TokenKind::Colon) {
190
+ println!("Expected ':' after map key '{}'", key);
191
+ break;
192
+ }
193
+
194
+ let value = if let Some(token) = self.peek_clone() {
195
+ match token.kind {
196
+ TokenKind::String => {
197
+ self.advance();
198
+ Value::String(token.lexeme.clone())
199
+ }
200
+ TokenKind::Number => {
201
+ self.advance();
202
+ Value::Number(token.lexeme.parse().unwrap_or(0.0))
203
+ }
204
+ TokenKind::Identifier => {
205
+ self.advance();
206
+ Value::Identifier(token.lexeme.clone())
207
+ }
208
+ _ => {
209
+ println!("Unexpected token in map value: {:?}", token);
210
+ Value::Null
211
+ }
212
+ }
213
+ } else {
214
+ Value::Null
215
+ };
216
+
217
+ map.insert(key, value);
218
+ }
219
+
220
+ if !self.match_token(TokenKind::RBrace) {
221
+ println!("Expected '}}' at end of map");
222
+ }
223
+
224
+ Some(Value::Map(map))
225
+ }
226
+
227
+ pub fn peek(&self) -> Option<&Token> {
228
+ self.tokens.get(self.token_index)
229
+ }
230
+
231
+ pub fn peek_clone(&self) -> Option<Token> {
232
+ self.tokens.get(self.token_index).cloned()
233
+ }
234
+
235
+ pub fn expect(&mut self, kind: TokenKind) -> Result<&Token, String> {
236
+ let tok = self.advance().ok_or("Unexpected end of input")?;
237
+ if tok.kind == kind {
238
+ Ok(tok)
239
+ } else {
240
+ Err(format!("Expected {:?}, got {:?}", kind, tok.kind))
241
+ }
242
+ }
243
+
244
+ pub fn collect_block_tokens(&mut self, base_indent: usize) -> Vec<Token> {
245
+ let mut tokens = Vec::new();
246
+
247
+ while let Some(tok) = self.peek() {
248
+ if tok.indent <= base_indent && tok.kind != TokenKind::Newline {
249
+ break;
250
+ }
251
+ tokens.push(self.advance().unwrap().clone());
252
+ }
253
+
254
+ tokens
255
+ }
256
+
257
+ pub fn collect_until<F>(&mut self, condition: F) -> Vec<Token> where F: Fn(&Token) -> bool {
258
+ let mut collected = Vec::new();
259
+ while let Some(token) = self.peek() {
260
+ if token.kind == TokenKind::Newline || token.kind == TokenKind::Indent {
261
+ self.advance(); // Skip newlines and indents
262
+ continue;
263
+ }
264
+ if token.kind == TokenKind::EOF {
265
+ break;
266
+ }
267
+ if condition(token) {
268
+ break;
269
+ }
270
+ collected.push(self.advance().unwrap().clone());
271
+ }
272
+ collected
273
+ }
274
+
275
+ pub fn is_eof(&self) -> bool {
276
+ self.token_index >= self.tokens.len()
277
+ }
278
+
279
+ pub fn parse_block_until_next_else(
280
+ &mut self,
281
+ base_indent: usize,
282
+ global_store: &mut GlobalStore
283
+ ) -> Vec<Statement> {
284
+ let mut block_tokens = Vec::new();
285
+
286
+ while let Some(tok) = self.peek() {
287
+ // Stop if we encounter an 'else' at same indent level
288
+ if tok.lexeme == "else" && tok.indent == base_indent {
289
+ break;
290
+ }
291
+ block_tokens.push(self.advance().unwrap().clone());
292
+ }
293
+
294
+ self.parse_block(block_tokens, global_store)
295
+ }
296
+
297
+ pub fn parse_condition_until_colon(&mut self) -> Option<Value> {
298
+ let tokens = self.collect_until(|t| t.kind == TokenKind::Colon);
299
+ if tokens.is_empty() {
300
+ return None;
301
+ }
302
+
303
+ let condition = tokens
304
+ .iter()
305
+ .map(|t| t.lexeme.clone())
306
+ .collect::<Vec<_>>()
307
+ .join(" ");
308
+
309
+ Some(Value::String(condition))
310
+ }
311
+
312
+ pub fn parse_block_until_else_or_dedent(
313
+ &mut self,
314
+ base_indent: usize,
315
+ global_store: &mut GlobalStore
316
+ ) -> Vec<Statement> {
317
+ let mut tokens = Vec::new();
318
+
319
+ while let Some(tok) = self.peek() {
320
+ if tok.lexeme == "else" && tok.indent == base_indent {
321
+ break;
322
+ }
323
+ if tok.indent < base_indent && tok.kind != TokenKind::Newline {
324
+ break;
325
+ }
326
+ tokens.push(self.advance().unwrap().clone());
327
+ }
328
+
329
+ self.parse_block(tokens, global_store)
330
+ }
331
+ }
@@ -0,0 +1,126 @@
1
+ use crate::core::{
2
+ lexer::token::TokenKind,
3
+ parser::{ driver::Parser, statement::{ Statement, StatementKind } },
4
+ shared::value::Value,
5
+ store::global::GlobalStore,
6
+ };
7
+
8
+ pub fn parse_arrow_call(parser: &mut Parser, global_store: &mut GlobalStore) -> Statement {
9
+ let Some(target_token) = parser.peek_clone() else {
10
+ return Statement::unknown();
11
+ };
12
+
13
+ if target_token.kind != TokenKind::Identifier {
14
+ parser.advance(); // consume target token
15
+ return Statement::unknown();
16
+ }
17
+
18
+ let Some(arrow_token) = parser.peek_nth(1).cloned() else {
19
+ parser.advance(); // consume arrow token
20
+ return Statement {
21
+ kind: StatementKind::Unknown,
22
+ value: Value::String(target_token.lexeme.clone()),
23
+ indent: target_token.indent,
24
+ line: target_token.line,
25
+ column: target_token.column,
26
+ };
27
+ };
28
+
29
+ if arrow_token.kind != TokenKind::Arrow {
30
+ parser.advance(); // consume method token
31
+ return Statement {
32
+ kind: StatementKind::Unknown,
33
+ value: Value::String(target_token.lexeme.clone()),
34
+ indent: target_token.indent,
35
+ line: target_token.line,
36
+ column: target_token.column,
37
+ };
38
+ }
39
+
40
+ // We have a valid arrow call, so we consume the arrow token
41
+ let Some(method_token) = parser.peek_nth(2).cloned() else {
42
+ parser.advance();
43
+ return Statement::unknown();
44
+ };
45
+
46
+ if method_token.kind != TokenKind::Identifier {
47
+ parser.advance();
48
+ return Statement::unknown();
49
+ }
50
+
51
+ // Consume the tokens for target, arrow, and method
52
+ parser.advance(); // target
53
+ parser.advance(); // ->
54
+ parser.advance(); // method
55
+
56
+ let mut args = Vec::new();
57
+
58
+ while let Some(token) = parser.peek_clone() {
59
+ if token.kind == TokenKind::Newline || token.kind == TokenKind::EOF {
60
+ break;
61
+ }
62
+
63
+ parser.advance();
64
+
65
+ let value = match token.kind {
66
+ TokenKind::Identifier => Value::Identifier(token.lexeme.clone()),
67
+ TokenKind::String => Value::String(token.lexeme.clone()),
68
+ TokenKind::Number => Value::Number(token.lexeme.parse::<f32>().unwrap_or(0.0)),
69
+ TokenKind::LBrace => {
70
+ // Handle map literal
71
+ let mut map = std::collections::HashMap::new();
72
+ while let Some(inner_token) = parser.peek_clone() {
73
+ if inner_token.kind == TokenKind::RBrace {
74
+ parser.advance(); // consume RBrace
75
+ break;
76
+ }
77
+ if inner_token.kind == TokenKind::Newline || inner_token.kind == TokenKind::EOF {
78
+ break;
79
+ }
80
+ parser.advance(); // consume key token
81
+ let key = inner_token.lexeme.clone();
82
+
83
+ if let Some(colon_token) = parser.peek_clone() {
84
+ if colon_token.kind == TokenKind::Colon {
85
+ parser.advance(); // consume colon
86
+ if let Some(value_token) = parser.peek_clone() {
87
+ parser.advance(); // consume value token
88
+ let value = match value_token.kind {
89
+ TokenKind::Identifier =>
90
+ Value::Identifier(value_token.lexeme.clone()),
91
+ TokenKind::String => Value::String(value_token.lexeme.clone()),
92
+ TokenKind::Number =>
93
+ Value::Number(
94
+ value_token.lexeme.parse::<f32>().unwrap_or(0.0)
95
+ ),
96
+ TokenKind::Boolean =>
97
+ Value::Boolean(
98
+ value_token.lexeme.parse::<bool>().unwrap_or(false)
99
+ ),
100
+ _ => Value::Unknown,
101
+ };
102
+ map.insert(key, value);
103
+ }
104
+ }
105
+ }
106
+ }
107
+ Value::Map(map)
108
+ }
109
+ _ => Value::Unknown,
110
+ };
111
+
112
+ args.push(value);
113
+ }
114
+
115
+ Statement {
116
+ kind: StatementKind::ArrowCall {
117
+ target: target_token.lexeme.clone(),
118
+ method: method_token.lexeme.clone(),
119
+ args,
120
+ },
121
+ value: Value::Null,
122
+ indent: target_token.indent,
123
+ line: target_token.line,
124
+ column: target_token.column,
125
+ }
126
+ }
@@ -1,14 +1,10 @@
1
1
  use crate::core::{
2
- lexer::token::{ Token, TokenKind },
3
- parser::{
4
- handler::identifier::parse_identifier_token,
5
- statement::{ Statement, StatementKind },
6
- Parser,
7
- },
2
+ lexer::token::TokenKind,
3
+ parser::{ driver::Parser, statement::{ Statement, StatementKind } },
8
4
  shared::value::Value,
9
5
  store::global::GlobalStore,
10
6
  };
11
- pub fn parse_at_token(parser: &mut Parser, _global_store: &mut GlobalStore) -> Statement {
7
+ pub fn parse_at_token(parser: &mut Parser, global_store: &mut GlobalStore) -> Statement {
12
8
  parser.advance(); // consume '@'
13
9
 
14
10
  let Some(token) = parser.peek_clone() else {
@@ -1,6 +1,6 @@
1
1
  use crate::core::{
2
2
  lexer::token::TokenKind,
3
- parser::{ statement::{ Statement, StatementKind }, Parser },
3
+ parser::{ statement::{ Statement, StatementKind }, driver::Parser },
4
4
  shared::value::Value,
5
5
  store::global::GlobalStore,
6
6
  };
@@ -25,7 +25,10 @@ pub fn parse_bank_token(parser: &mut Parser, _global_store: &mut GlobalStore) ->
25
25
  _ => Value::Unknown,
26
26
  }
27
27
  } else {
28
- return Statement::error(bank_token, "Expected identifier or number after 'bank'".to_string());
28
+ return Statement::error(
29
+ bank_token,
30
+ "Expected identifier or number after 'bank'".to_string()
31
+ );
29
32
  };
30
33
 
31
34
  Statement {
@@ -0,0 +1,74 @@
1
+ use std::collections::HashMap;
2
+ use crate::core::{
3
+ lexer::token::TokenKind,
4
+ parser::{statement::{Statement, StatementKind}, driver::Parser},
5
+ shared::value::Value,
6
+ store::global::GlobalStore,
7
+ };
8
+
9
+ pub fn parse_condition_token(parser: &mut Parser, global_store: &mut GlobalStore) -> Statement {
10
+ parser.advance(); // consume 'if'
11
+ let Some(if_token) = parser.previous_clone() else {
12
+ return Statement::unknown();
13
+ };
14
+
15
+ let Some(condition) = parser.parse_condition_until_colon() else {
16
+ return Statement::error(if_token, "Expected condition after 'if'".to_string());
17
+ };
18
+
19
+ parser.advance_if(TokenKind::Colon);
20
+ let base_indent = if_token.indent;
21
+
22
+ let if_body = parser.parse_block_until_else_or_dedent(base_indent, global_store);
23
+
24
+ let mut root_map = HashMap::new();
25
+ root_map.insert("condition".to_string(), condition);
26
+ root_map.insert("body".to_string(), Value::Block(if_body));
27
+
28
+ let mut current = &mut root_map;
29
+
30
+ // Loop for else / else if
31
+ while let Some(tok) = parser.peek_clone() {
32
+ // Only continue if we see `else` at same indent level
33
+ if tok.lexeme != "else" || tok.indent != base_indent {
34
+ break;
35
+ }
36
+
37
+ parser.advance(); // consume 'else'
38
+
39
+ // Check if it's an 'else if'
40
+ let next_condition = if parser.peek_is("if") {
41
+ parser.advance(); // consume 'if'
42
+ let Some(cond) = parser.parse_condition_until_colon() else {
43
+ return Statement::error(tok.clone(), "Expected condition after 'else if'".to_string());
44
+ };
45
+ parser.advance_if(TokenKind::Colon);
46
+ Some(cond)
47
+ } else {
48
+ parser.advance_if(TokenKind::Colon);
49
+ None
50
+ };
51
+
52
+ let body = parser.parse_block_until_else_or_dedent(base_indent, global_store);
53
+
54
+ let mut next_map = HashMap::new();
55
+ if let Some(cond) = next_condition {
56
+ next_map.insert("condition".to_string(), cond);
57
+ }
58
+ next_map.insert("body".to_string(), Value::Block(body));
59
+
60
+ current.insert("next".to_string(), Value::Map(next_map));
61
+ current = match current.get_mut("next") {
62
+ Some(Value::Map(map)) => map,
63
+ _ => break,
64
+ };
65
+ }
66
+
67
+ Statement {
68
+ kind: StatementKind::If,
69
+ value: Value::Map(root_map),
70
+ indent: if_token.indent,
71
+ line: if_token.line,
72
+ column: if_token.column,
73
+ }
74
+ }
@@ -1,6 +1,6 @@
1
1
  use crate::core::{
2
2
  lexer::token::TokenKind,
3
- parser::{ statement::{ Statement, StatementKind }, Parser },
3
+ parser::{ statement::{ Statement, StatementKind }, driver::Parser },
4
4
  shared::{ duration::Duration, value::Value },
5
5
  store::global::GlobalStore,
6
6
  };
@@ -0,0 +1,41 @@
1
+ use crate::core::{
2
+ lexer::token::{ Token, TokenKind },
3
+ parser::{ statement::{ Statement, StatementKind }, driver::Parser },
4
+ shared::value::Value,
5
+ store::global::GlobalStore,
6
+ };
7
+
8
+ pub fn parse_call_token(
9
+ parser: &mut Parser,
10
+ current_token: Token,
11
+ global_store: &mut GlobalStore
12
+ ) -> Statement {
13
+ parser.advance(); // consume "call"
14
+
15
+ let value = if let Some(token) = parser.peek_clone() {
16
+ parser.advance();
17
+ match token.kind {
18
+ TokenKind::Identifier => Value::Identifier(token.lexeme.clone()),
19
+ TokenKind::String => Value::String(token.lexeme.clone()),
20
+ _ => {
21
+ return Statement::error(
22
+ token,
23
+ "Expected identifier or string after 'call'".to_string()
24
+ );
25
+ }
26
+ }
27
+ } else {
28
+ return Statement::error(
29
+ current_token,
30
+ "Expected identifier or string after 'call'".to_string()
31
+ );
32
+ };
33
+
34
+ return Statement {
35
+ kind: StatementKind::Call,
36
+ value,
37
+ indent: current_token.indent,
38
+ line: current_token.line,
39
+ column: current_token.column,
40
+ };
41
+ }
@@ -0,0 +1,75 @@
1
+ use crate::core::{
2
+ lexer::token::{ Token, TokenKind },
3
+ parser::{ statement::{ Statement, StatementKind }, driver::Parser },
4
+ shared::value::Value,
5
+ store::global::GlobalStore,
6
+ };
7
+ use std::collections::HashMap;
8
+
9
+ pub fn parse_group_token(
10
+ parser: &mut Parser,
11
+ current_token: Token,
12
+ global_store: &mut GlobalStore
13
+ ) -> Statement {
14
+ parser.advance(); // consume "group"
15
+
16
+ let Some(identifier_token) = parser.peek_clone() else {
17
+ return Statement::error(current_token, "Expected identifier after 'group'".to_string());
18
+ };
19
+
20
+ if identifier_token.kind != TokenKind::Identifier && identifier_token.kind != TokenKind::String {
21
+ return Statement::error(identifier_token, "Expected valid identifier".to_string());
22
+ }
23
+
24
+ parser.advance(); // consume identifier
25
+
26
+ let Some(colon_token) = parser.peek_clone() else {
27
+ return Statement::error(
28
+ identifier_token,
29
+ "Expected ':' after group identifier".to_string()
30
+ );
31
+ };
32
+
33
+ if colon_token.kind != TokenKind::Colon {
34
+ return Statement::error(
35
+ colon_token.clone(),
36
+ "Expected ':' after group identifier".to_string()
37
+ );
38
+ }
39
+
40
+ parser.advance(); // consume ':'
41
+
42
+ let base_indent = current_token.indent;
43
+
44
+ // Clone without consuming tokens
45
+ let mut index = parser.token_index;
46
+ let mut tokens_inside_group = Vec::new();
47
+
48
+ while index < parser.tokens.len() {
49
+ let token = parser.tokens[index].clone();
50
+
51
+ if token.indent <= base_indent && token.kind != TokenKind::Newline {
52
+ break;
53
+ }
54
+
55
+ tokens_inside_group.push(token);
56
+ index += 1;
57
+ }
58
+
59
+ // Advance index once to skip the processed tokens
60
+ parser.token_index = index;
61
+
62
+ let body = parser.parse_block(tokens_inside_group, global_store);
63
+
64
+ let mut value_map = HashMap::new();
65
+ value_map.insert("identifier".to_string(), Value::String(identifier_token.lexeme.clone()));
66
+ value_map.insert("body".to_string(), Value::Block(body));
67
+
68
+ return Statement {
69
+ kind: StatementKind::Group,
70
+ value: Value::Map(value_map),
71
+ indent: current_token.indent,
72
+ line: current_token.line,
73
+ column: current_token.column,
74
+ };
75
+ }