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

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 (54) hide show
  1. package/Cargo.toml +7 -5
  2. package/README.md +26 -7
  3. package/docs/CHANGELOG.md +34 -2
  4. package/docs/ROADMAP.md +3 -3
  5. package/docs/SYNTAX.md +42 -14
  6. package/docs/TODO.md +9 -8
  7. package/examples/group.deva +12 -0
  8. package/examples/index.deva +10 -9
  9. package/examples/loop.deva +15 -0
  10. package/examples/variables.deva +9 -0
  11. package/out-tsc/bin/devalang.exe +0 -0
  12. package/package.json +43 -41
  13. package/project-version.json +5 -5
  14. package/rust/cli/build.rs +4 -4
  15. package/rust/cli/check.rs +2 -1
  16. package/rust/cli/init.rs +4 -2
  17. package/rust/cli/play.rs +5 -3
  18. package/rust/cli/template.rs +2 -1
  19. package/rust/config/loader.rs +0 -1
  20. package/rust/{audio → core/audio}/engine.rs +1 -5
  21. package/rust/core/audio/interpreter.rs +317 -0
  22. package/rust/{audio → core/audio}/loader.rs +4 -0
  23. package/rust/{audio → core/audio}/render.rs +1 -5
  24. package/rust/core/builder/mod.rs +9 -8
  25. package/rust/core/lexer/handler/indent.rs +1 -1
  26. package/rust/core/lexer/handler/mod.rs +3 -4
  27. package/rust/core/lexer/handler/newline.rs +5 -1
  28. package/rust/core/lexer/handler/string.rs +3 -6
  29. package/rust/core/lexer/mod.rs +10 -5
  30. package/rust/core/mod.rs +2 -1
  31. package/rust/core/parser/handler/identifier.rs +127 -1
  32. package/rust/core/parser/handler/loop_.rs +11 -0
  33. package/rust/core/parser/mod.rs +0 -1
  34. package/rust/core/parser/statement.rs +9 -5
  35. package/rust/core/preprocessor/loader.rs +65 -3
  36. package/rust/core/preprocessor/module.rs +2 -0
  37. package/rust/core/preprocessor/processor.rs +28 -2
  38. package/rust/core/preprocessor/resolver/bank.rs +10 -9
  39. package/rust/core/preprocessor/resolver/group.rs +113 -0
  40. package/rust/core/preprocessor/resolver/loop_.rs +15 -13
  41. package/rust/core/preprocessor/resolver/mod.rs +11 -5
  42. package/rust/core/preprocessor/resolver/trigger.rs +0 -3
  43. package/rust/lib.rs +117 -0
  44. package/rust/main.rs +2 -1
  45. package/rust/utils/logger.rs +45 -6
  46. package/rust/utils/spinner.rs +2 -0
  47. package/templates/minimal/.devalang +2 -1
  48. package/templates/minimal/README.md +202 -0
  49. package/templates/welcome/.devalang +2 -1
  50. package/templates/welcome/README.md +48 -31
  51. package/examples/exported.deva +0 -7
  52. package/rust/audio/interpreter.rs +0 -143
  53. /package/rust/{audio → core/audio}/mod.rs +0 -0
  54. /package/rust/{audio → core/audio}/player.rs +0 -0
@@ -0,0 +1,317 @@
1
+ use crate::{
2
+ core::{
3
+ audio::{ engine::AudioEngine, loader::load_trigger },
4
+ parser::statement::{ Statement, StatementKind },
5
+ shared::{ duration::Duration, value::Value },
6
+ store::variable::VariableTable,
7
+ },
8
+ };
9
+
10
+ pub fn interprete_statements(
11
+ statements: &Vec<Statement>,
12
+ audio_engine: AudioEngine,
13
+ entry: String,
14
+ output: String
15
+ ) -> (AudioEngine, f32, f32) {
16
+ let mut base_bpm = 120.0;
17
+ let mut base_duration = 60.0 / base_bpm;
18
+
19
+ let variable_table = audio_engine.variables.clone();
20
+
21
+ let (updated_audio_engine, base_bpm, max_end_time) = execute_audio_statements(
22
+ audio_engine.clone(),
23
+ variable_table.clone(),
24
+ statements.clone(),
25
+ base_bpm.clone(),
26
+ base_duration.clone(),
27
+ 0.0,
28
+ 0.0
29
+ );
30
+
31
+ (updated_audio_engine, base_bpm, max_end_time)
32
+ }
33
+
34
+ pub fn execute_audio_statements(
35
+ mut audio_engine: AudioEngine,
36
+ mut variable_table: VariableTable,
37
+ mut statements: Vec<Statement>,
38
+ mut base_bpm: f32,
39
+ mut base_duration: f32,
40
+ mut max_end_time: f32,
41
+ mut cursor_time: f32
42
+ ) -> (AudioEngine, f32, f32) {
43
+ for stmt in statements {
44
+ match &stmt.kind {
45
+ StatementKind::Load { source, alias } => {
46
+ variable_table.set(alias.to_string(), Value::String(source.clone()));
47
+ }
48
+
49
+ StatementKind::Let { name } => {
50
+ variable_table.set(name.to_string(), stmt.value.clone());
51
+ }
52
+
53
+ StatementKind::Tempo => {
54
+ if let Value::Number(bpm_) = &stmt.value {
55
+ base_bpm = *bpm_ as f32;
56
+ base_duration = 60.0 / base_bpm;
57
+ } else {
58
+ eprintln!("❌ Invalid tempo value: {:?}", stmt.value);
59
+ }
60
+ }
61
+
62
+ StatementKind::Trigger { entity, duration } => {
63
+ if let Some(trigger_val) = variable_table.get(entity) {
64
+ // Étape 1 : Résolution de duration
65
+ let duration_secs = match duration {
66
+ Duration::Number(n) => *n,
67
+
68
+ Duration::Identifier(id) => {
69
+ if id == "auto" {
70
+ 1.0 // Valeur par défaut pour "auto"
71
+ } else {
72
+ match variable_table.get(id) {
73
+ Some(Value::Number(n)) => *n,
74
+ Some(Value::Identifier(other)) => {
75
+ if other == "auto" {
76
+ 1.0 // Valeur par défaut pour "auto"
77
+ } else {
78
+ eprintln!("❌ Invalid duration identifier '{}': expected number or string, got identifier", id);
79
+ continue;
80
+ }
81
+ }
82
+ Some(other) => {
83
+ eprintln!(
84
+ "❌ Invalid duration reference '{}': expected number or string, got {:?}",
85
+ id,
86
+ other
87
+ );
88
+ continue;
89
+ }
90
+ None => {
91
+ eprintln!("❌ Duration identifier '{}' not found in variable table", id);
92
+ continue;
93
+ }
94
+ }
95
+ }
96
+ }
97
+
98
+ Duration::Auto => {
99
+ // Si "auto", tu choisis une valeur par défaut ou duration du sample ?
100
+ 1.0 // ← par exemple 1.0 beat
101
+ }
102
+ };
103
+
104
+ // Durée réelle en secondes selon tempo
105
+ let duration_final = duration_secs * base_duration;
106
+
107
+ // Chargement de l'audio
108
+ let (src, _) = load_trigger(
109
+ trigger_val,
110
+ duration,
111
+ base_duration,
112
+ variable_table.clone()
113
+ );
114
+
115
+ audio_engine.insert(&src, cursor_time, duration_final, None);
116
+
117
+ cursor_time += duration_final;
118
+ if cursor_time > max_end_time {
119
+ max_end_time = cursor_time;
120
+ }
121
+ } else {
122
+ eprintln!("❌ Unknown trigger entity: {}", entity);
123
+ }
124
+ }
125
+
126
+ StatementKind::Spawn => {
127
+ if let Value::String(identifier) = &stmt.value {
128
+ match variable_table.get(identifier) {
129
+ Some(Value::Map(map)) => {
130
+ if let Some(Value::Block(block)) = map.get("body") {
131
+ let mut local_max = cursor_time;
132
+
133
+ for inner_stmt in block {
134
+ let (inner_engine, _, inner_end_time) =
135
+ execute_audio_statements(
136
+ audio_engine.clone(),
137
+ variable_table.clone(),
138
+ vec![inner_stmt.clone()],
139
+ base_bpm,
140
+ base_duration,
141
+ max_end_time,
142
+ cursor_time // <- important: same cursor time
143
+ );
144
+ audio_engine = inner_engine;
145
+ if inner_end_time > local_max {
146
+ local_max = inner_end_time;
147
+ }
148
+ }
149
+
150
+ // Update cursor once all done
151
+ if local_max > max_end_time {
152
+ max_end_time = local_max;
153
+ }
154
+ cursor_time = local_max;
155
+ }
156
+ }
157
+ _ => eprintln!("❌ Cannot spawn '{}'", identifier),
158
+ }
159
+ }
160
+ }
161
+
162
+ StatementKind::Sleep => {
163
+ let duration_secs = match &stmt.value {
164
+ Value::Number(ms) => *ms / 1000.0,
165
+
166
+ Value::String(s) if s.ends_with("ms") => {
167
+ let ms = s.trim_end_matches("ms").parse::<f32>();
168
+ match ms {
169
+ Ok(ms) => ms / 1000.0,
170
+ Err(_) => {
171
+ eprintln!("❌ Invalid sleep value (ms): {}", s);
172
+ continue;
173
+ }
174
+ }
175
+ }
176
+
177
+ Value::String(s) if s.ends_with("s") => {
178
+ let s_ = s.trim_end_matches("s").parse::<f32>();
179
+ match s_ {
180
+ Ok(secs) => secs,
181
+ Err(_) => {
182
+ eprintln!("❌ Invalid sleep value (s): {}", s);
183
+ continue;
184
+ }
185
+ }
186
+ }
187
+
188
+ other => {
189
+ eprintln!("❌ Invalid sleep value: {:?}", other);
190
+ continue;
191
+ }
192
+ };
193
+
194
+ cursor_time += duration_secs;
195
+
196
+ if cursor_time > max_end_time {
197
+ max_end_time = cursor_time;
198
+ }
199
+ }
200
+
201
+ StatementKind::Loop => {
202
+ if let Value::Map(loop_value) = &stmt.value {
203
+ let iterator = loop_value.get("iterator");
204
+ let body = loop_value.get("body");
205
+
206
+ let loop_count = if let Some(Value::Number(n)) = iterator {
207
+ *n as usize
208
+ } else {
209
+ eprintln!("❌ Loop iterator must be a number: {:?}", iterator);
210
+ continue;
211
+ };
212
+
213
+ let loop_body = if let Some(Value::Block(body)) = body {
214
+ body.clone()
215
+ } else {
216
+ eprintln!("❌ Loop body must be a block: {:?}", body);
217
+ continue;
218
+ };
219
+
220
+ for _ in 0..loop_count {
221
+ let (loop_engine, _, loop_end_time) = execute_audio_statements(
222
+ audio_engine.clone(),
223
+ variable_table.clone(),
224
+ loop_body.clone(),
225
+ base_bpm,
226
+ base_duration,
227
+ max_end_time,
228
+ cursor_time
229
+ );
230
+
231
+ audio_engine = loop_engine;
232
+
233
+ // Update time and max_end_time after each loop iteration
234
+ cursor_time = loop_end_time;
235
+ if loop_end_time > max_end_time {
236
+ max_end_time = loop_end_time;
237
+ }
238
+ }
239
+ }
240
+ }
241
+
242
+ StatementKind::Call => {
243
+ if let Value::String(identifier) = &stmt.value {
244
+ match variable_table.get(identifier) {
245
+ Some(Value::Map(map)) => {
246
+ match map.get("body") {
247
+ Some(Value::Block(block)) => {
248
+ let (called_engine, _, called_end_time) =
249
+ execute_audio_statements(
250
+ audio_engine.clone(),
251
+ variable_table.clone(),
252
+ block.clone(),
253
+ base_bpm,
254
+ base_duration,
255
+ max_end_time,
256
+ cursor_time
257
+ );
258
+
259
+ audio_engine = called_engine;
260
+ cursor_time = called_end_time;
261
+ if called_end_time > max_end_time {
262
+ max_end_time = called_end_time;
263
+ }
264
+ }
265
+ Some(other) => {
266
+ eprintln!(
267
+ "❌ Cannot call '{}': expected 'body' to be a block, got {:?}",
268
+ identifier,
269
+ other
270
+ );
271
+ }
272
+ None => {
273
+ eprintln!("❌ Cannot call '{}': missing 'body' in group map", identifier);
274
+ }
275
+ }
276
+ }
277
+ Some(other) => {
278
+ eprintln!(
279
+ "❌ Cannot call '{}': expected a Map with 'body', got {:?}",
280
+ identifier,
281
+ other
282
+ );
283
+ }
284
+ None => {
285
+ eprintln!("❌ Cannot call '{}': not found in variable table", identifier);
286
+ }
287
+ }
288
+ } else {
289
+ eprintln!(
290
+ "❌ Invalid call statement: expected a string identifier, got {:?}",
291
+ stmt.value
292
+ );
293
+ }
294
+ }
295
+
296
+ StatementKind::Bank => {}
297
+
298
+ StatementKind::Import { names, source } => {}
299
+
300
+ StatementKind::Export { names, source } => {}
301
+
302
+ StatementKind::Group => {}
303
+
304
+ StatementKind::Unknown => {
305
+ // Ignore unknown statements
306
+ }
307
+
308
+ _ => {
309
+ eprintln!("Unsupported statement kind: {:?}", stmt);
310
+ }
311
+ }
312
+ }
313
+
314
+ audio_engine.set_variables(variable_table);
315
+
316
+ (audio_engine, base_bpm, max_end_time)
317
+ }
@@ -24,6 +24,10 @@ pub fn load_trigger(
24
24
  duration_as_secs = base_duration;
25
25
  } else if let Some(Value::Number(num)) = variable_table.get(duration_identifier) {
26
26
  duration_as_secs = *num;
27
+ } else if let Some(Value::String(num_str)) = variable_table.get(duration_identifier) {
28
+ duration_as_secs = num_str.parse::<f32>().unwrap_or(base_duration);
29
+ } else if let Some(Value::Identifier(num_str)) = variable_table.get(duration_identifier) {
30
+ duration_as_secs = num_str.parse::<f32>().unwrap_or(base_duration);
27
31
  } else {
28
32
  eprintln!("❌ Invalid duration identifier: {}", duration_identifier);
29
33
  }
@@ -1,11 +1,7 @@
1
1
  use std::collections::HashMap;
2
2
 
3
3
  use crate::{
4
- audio::{ engine::AudioEngine, interpreter::interprete_statements },
5
- core::{
6
- parser::statement::Statement,
7
- store::global::GlobalStore,
8
- },
4
+ core::{ audio::{engine::AudioEngine, interpreter::interprete_statements}, parser::statement::Statement, store::global::GlobalStore },
9
5
  utils::logger::{ LogLevel, Logger },
10
6
  };
11
7
 
@@ -1,10 +1,11 @@
1
- use crate::audio::render::render_audio_with_modules;
1
+ use crate::core::audio::render::render_audio_with_modules;
2
2
  use crate::core::parser::statement::Statement;
3
3
  use crate::core::store::global::GlobalStore;
4
- use crate::utils::logger::Logger;
5
4
  use std::{ collections::HashMap, fs::create_dir_all };
6
5
  use std::io::Write;
7
6
 
7
+ use crate::utils::logger::Logger;
8
+
8
9
  pub struct Builder {}
9
10
 
10
11
  impl Builder {
@@ -12,16 +13,14 @@ impl Builder {
12
13
  Builder {}
13
14
  }
14
15
 
15
- pub fn build_ast(&self, modules: &HashMap<String, Vec<Statement>>) {
16
- let output_path = "./output";
17
-
16
+ pub fn build_ast(&self, modules: &HashMap<String, Vec<Statement>>, out_dir: &str) {
18
17
  for (name, statements) in modules {
19
18
  let formatted_name = name.split("/").last().unwrap_or(name);
20
19
  let formatted_name = formatted_name.replace(".deva", "");
21
20
 
22
- create_dir_all(format!("{}/ast", output_path)).expect("Failed to create AST directory");
21
+ create_dir_all(format!("{}/ast", out_dir)).expect("Failed to create AST directory");
23
22
 
24
- let file_path = format!("{}/ast/{}.json", output_path, formatted_name);
23
+ let file_path = format!("{}/ast/{}.json", out_dir, formatted_name);
25
24
  let mut file = std::fs::File::create(file_path).expect("Failed to create AST file");
26
25
 
27
26
  let content = serde_json
@@ -46,7 +45,9 @@ impl Builder {
46
45
  global_store
47
46
  );
48
47
 
49
- create_dir_all(format!("{}/audio", normalized_output_dir)).expect("Failed to create audio directory");
48
+ create_dir_all(format!("{}/audio", normalized_output_dir)).expect(
49
+ "Failed to create audio directory"
50
+ );
50
51
 
51
52
  for (module_name, mut audio_engine) in audio_engines {
52
53
  let formatted_module_name = module_name
@@ -17,7 +17,7 @@ pub fn handle_indent_lexer(
17
17
  chars.next();
18
18
  col += 1;
19
19
  } else if c == '\t' {
20
- *current_indent += 4; // tabulation = 4 espaces
20
+ *current_indent += 4;
21
21
  chars.next();
22
22
  col += 4;
23
23
  } else {
@@ -34,15 +34,14 @@ fn advance_char<I: Iterator<Item = char>>(
34
34
  ) -> Option<char> {
35
35
  let c = chars.next()?;
36
36
  if c == '\n' {
37
- *line += 1;
38
- *column = 1;
37
+ // Do not increment column on newline here
39
38
  } else {
40
39
  *column += 1;
41
40
  }
42
41
  Some(c)
43
42
  }
44
43
 
45
- pub fn handle_content_lexing(content: String) -> Vec<Token> {
44
+ pub fn handle_content_lexing(content: String) -> Result<Vec<Token>, String> {
46
45
  let mut tokens = Vec::new();
47
46
 
48
47
  let mut line = 1;
@@ -234,5 +233,5 @@ pub fn handle_content_lexing(content: String) -> Vec<Token> {
234
233
  // );
235
234
  // }
236
235
 
237
- tokens
236
+ Ok(tokens)
238
237
  }
@@ -14,6 +14,10 @@ pub fn handle_newline_lexer(
14
14
  lexeme: ch.to_string(),
15
15
  line: *line,
16
16
  column: 0,
17
- indent: 0,
17
+ indent: *current_indent,
18
18
  });
19
+
20
+ *line += 1;
21
+ *column = 1;
22
+ *at_line_start = true;
19
23
  }
@@ -1,4 +1,4 @@
1
- use crate::core::lexer::token::{Token, TokenKind};
1
+ use crate::core::lexer::token::{ Token, TokenKind };
2
2
 
3
3
  pub fn handle_string_lexer(
4
4
  ch: char,
@@ -11,23 +11,20 @@ pub fn handle_string_lexer(
11
11
  ) {
12
12
  let quote_char = ch;
13
13
 
14
- // Position de départ du token
15
14
  let start_column = *column;
16
15
  let start_line = *line;
17
16
 
18
- // Consommer le guillemet ouvrant
19
- chars.next();
20
17
  *column += 1;
21
18
 
22
19
  let mut string_content = String::new();
23
20
 
24
21
  while let Some(&next_ch) = chars.peek() {
25
22
  if next_ch == quote_char {
26
- chars.next(); // Consomme le guillemet fermant
23
+ chars.next();
27
24
  *column += 1;
28
25
  break;
29
26
  } else if next_ch == '\\' {
30
- chars.next(); // Consomme '\'
27
+ chars.next();
31
28
  *column += 1;
32
29
 
33
30
  if let Some(escaped) = chars.next() {
@@ -2,7 +2,10 @@ pub mod handler;
2
2
  pub mod token;
3
3
 
4
4
  use std::fs;
5
- use crate::core::{ lexer::{ handler::handle_content_lexing, token::Token }, utils::path::normalize_path };
5
+ use crate::core::{
6
+ lexer::{ handler::handle_content_lexing, token::Token },
7
+ utils::path::normalize_path,
8
+ };
6
9
 
7
10
  pub struct Lexer {}
8
11
 
@@ -11,14 +14,16 @@ impl Lexer {
11
14
  Lexer {}
12
15
  }
13
16
 
17
+ pub fn lex_from_source(&self, source: &str) -> Result<Vec<Token>, String> {
18
+ handle_content_lexing(source.to_string())
19
+ }
20
+
14
21
  pub fn lex_tokens(&self, entrypoint: &str) -> Vec<Token> {
15
22
  let path = normalize_path(entrypoint);
16
23
 
17
- let file_content = fs
18
- ::read_to_string(&path)
19
- .expect("Failed to read the entrypoint file");
24
+ let file_content = fs::read_to_string(&path).expect("Failed to read the entrypoint file");
20
25
 
21
- let tokens = handle_content_lexing(file_content);
26
+ let tokens = handle_content_lexing(file_content).expect("Failed to lex the content");
22
27
 
23
28
  tokens
24
29
  }
package/rust/core/mod.rs CHANGED
@@ -6,4 +6,5 @@ pub mod shared;
6
6
  pub mod debugger;
7
7
  pub mod utils;
8
8
  pub mod builder;
9
- pub mod error;
9
+ pub mod error;
10
+ pub mod audio;
@@ -6,7 +6,7 @@ use crate::core::{
6
6
  };
7
7
  use std::collections::HashMap;
8
8
 
9
- pub fn parse_identifier_token(parser: &mut Parser, _global_store: &mut GlobalStore) -> Statement {
9
+ pub fn parse_identifier_token(parser: &mut Parser, global_store: &mut GlobalStore) -> Statement {
10
10
  let Some(current_token) = parser.peek_clone() else {
11
11
  return Statement::unknown();
12
12
  };
@@ -111,6 +111,132 @@ pub fn parse_identifier_token(parser: &mut Parser, _global_store: &mut GlobalSto
111
111
  line: current_token.line,
112
112
  column: current_token.column,
113
113
  };
114
+ } else if current_token.lexeme == "group" {
115
+ parser.advance(); // consume "group"
116
+
117
+ let identifier = if let Some(token) = parser.peek_clone() {
118
+ if token.kind == TokenKind::String {
119
+ parser.advance();
120
+ token.lexeme.clone()
121
+ } else if token.kind == TokenKind::Identifier {
122
+ parser.advance();
123
+ token.lexeme.clone()
124
+ } else {
125
+ return Statement::error(
126
+ token,
127
+ "Expected string or identifier after 'group'".to_string()
128
+ );
129
+ }
130
+ } else {
131
+ return Statement::error(current_token, "Expected string after 'group'".to_string());
132
+ };
133
+
134
+ let Some(colon_token) = parser.peek_clone() else {
135
+ return Statement::error(
136
+ current_token,
137
+ "Expected ':' after group identifier".to_string()
138
+ );
139
+ };
140
+
141
+ if colon_token.kind != TokenKind::Colon {
142
+ let message = format!(
143
+ "Expected ':' after group identifier, got {:?}",
144
+ colon_token.kind
145
+ );
146
+ return Statement::error(colon_token.clone(), message);
147
+ }
148
+
149
+ let tokens = parser.collect_until(
150
+ |t| (t.kind == TokenKind::Dedent || t.kind == TokenKind::EOF)
151
+ );
152
+ let group_body = parser.parse_block(tokens.clone(), global_store);
153
+
154
+ // Peek for dedent
155
+ if let Some(token) = parser.peek() {
156
+ if token.kind == TokenKind::Dedent {
157
+ parser.advance();
158
+ } else {
159
+ // Unexpected token after group body
160
+ }
161
+ } else {
162
+ // EOF or unexpected end of input
163
+ }
164
+
165
+ let mut value_map = HashMap::new();
166
+
167
+ value_map.insert("identifier".to_string(), Value::String(identifier));
168
+ value_map.insert("body".to_string(), Value::Block(group_body.clone()));
169
+
170
+ return Statement {
171
+ kind: StatementKind::Group,
172
+ value: Value::Map(value_map),
173
+ indent: current_token.indent,
174
+ line: current_token.line,
175
+ column: current_token.column,
176
+ };
177
+ } else if current_token.lexeme == "call" {
178
+ parser.advance(); // consume "call"
179
+
180
+ let identifier = if let Some(token) = parser.peek_clone() {
181
+ if token.kind == TokenKind::Identifier {
182
+ parser.advance();
183
+ token.lexeme.clone()
184
+ } else {
185
+ return Statement::error(token, "Expected identifier after 'call'".to_string());
186
+ }
187
+ } else {
188
+ return Statement::error(current_token, "Expected identifier after 'call'".to_string());
189
+ };
190
+
191
+ return Statement {
192
+ kind: StatementKind::Call,
193
+ value: Value::String(identifier),
194
+ indent: current_token.indent,
195
+ line: current_token.line,
196
+ column: current_token.column,
197
+ };
198
+ } else if current_token.lexeme == "spawn" {
199
+ parser.advance(); // consume "spawn"
200
+
201
+ let identifier = if let Some(token) = parser.peek_clone() {
202
+ if token.kind == TokenKind::Identifier {
203
+ parser.advance();
204
+ token.lexeme.clone()
205
+ } else {
206
+ return Statement::error(token, "Expected identifier after 'spawn'".to_string());
207
+ }
208
+ } else {
209
+ return Statement::error(current_token, "Expected identifier after 'spawn'".to_string());
210
+ };
211
+
212
+ return Statement {
213
+ kind: StatementKind::Spawn,
214
+ value: Value::String(identifier),
215
+ indent: current_token.indent,
216
+ line: current_token.line,
217
+ column: current_token.column,
218
+ };
219
+ } else if current_token.lexeme == "sleep" {
220
+ parser.advance(); // consume "sleep"
221
+
222
+ let duration = if let Some(token) = parser.peek_clone() {
223
+ if token.kind == TokenKind::Number {
224
+ parser.advance();
225
+ token.lexeme.parse().unwrap_or(0.0)
226
+ } else {
227
+ return Statement::error(token, "Expected number after 'sleep'".to_string());
228
+ }
229
+ } else {
230
+ return Statement::error(current_token, "Expected number after 'sleep'".to_string());
231
+ };
232
+
233
+ return Statement {
234
+ kind: StatementKind::Sleep,
235
+ value: Value::Number(duration),
236
+ indent: current_token.indent,
237
+ line: current_token.line,
238
+ column: current_token.column,
239
+ };
114
240
  } else {
115
241
  // Unknown identifier handling
116
242
  Statement {
@@ -40,6 +40,17 @@ pub fn parse_loop_token(parser: &mut Parser, global_store: &mut GlobalStore) ->
40
40
  );
41
41
  let loop_body = parser.parse_block(tokens.clone(), global_store);
42
42
 
43
+ // Peek for dedent
44
+ if let Some(token) = parser.peek() {
45
+ if token.kind == TokenKind::Dedent {
46
+ parser.advance();
47
+ } else {
48
+ // Unexpected token after loop body
49
+ }
50
+ } else {
51
+ // EOF or unexpected end of input
52
+ }
53
+
43
54
  let mut value_map = HashMap::new();
44
55
 
45
56
  value_map.insert("iterator".to_string(), Value::Identifier(iterator_name));
@@ -225,7 +225,6 @@ impl Parser {
225
225
  break;
226
226
  }
227
227
  if condition(token) {
228
- self.advance(); // Consume the token that matches the condition
229
228
  break;
230
229
  }
231
230
  collected.push(self.advance().unwrap().clone());