@devaloop/devalang 0.0.1-beta.1 → 0.0.1-beta.3

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 (207) hide show
  1. package/.devalang +9 -10
  2. package/Cargo.toml +84 -80
  3. package/README.md +10 -7
  4. package/docs/CHANGELOG.md +83 -0
  5. package/docs/ROADMAP.md +6 -2
  6. package/docs/TODO.md +3 -14
  7. package/examples/bus.deva +10 -0
  8. package/examples/chain.deva +19 -0
  9. package/examples/effect.deva +2 -0
  10. package/examples/filter.deva +11 -0
  11. package/examples/lfo.deva +9 -0
  12. package/examples/plugin.deva +10 -10
  13. package/examples/routing.deva +23 -0
  14. package/examples/synth.deva +11 -1
  15. package/examples/synth_types.deva +17 -0
  16. package/out-tsc/bin/project-version.json +6 -0
  17. package/out-tsc/core/functions/index.d.ts +5 -0
  18. package/out-tsc/core/functions/index.js +11 -0
  19. package/out-tsc/pkg/devalang_core.d.ts +2 -0
  20. package/out-tsc/pkg/devalang_core.js +17 -2
  21. package/out-tsc/pkg/devalang_core_bg.wasm.d.ts +1 -0
  22. package/out-tsc/scripts/version/copy-to-binary.d.ts +1 -0
  23. package/out-tsc/scripts/version/copy-to-binary.js +79 -0
  24. package/package.json +23 -10
  25. package/project-version.json +3 -3
  26. package/rust/bindings/Cargo.toml +9 -0
  27. package/rust/bindings/src/lib.rs +86 -0
  28. package/rust/cli/addon/commands.rs +35 -0
  29. package/rust/cli/addon/download.rs +234 -0
  30. package/rust/cli/addon/install.rs +33 -0
  31. package/rust/cli/addon/list.rs +224 -0
  32. package/rust/cli/addon/metadata.rs +124 -0
  33. package/rust/cli/addon/mod.rs +8 -0
  34. package/rust/cli/addon/remove.rs +271 -0
  35. package/rust/cli/addon/update.rs +305 -0
  36. package/rust/cli/{install/addon.rs → addon/utils.rs} +34 -43
  37. package/rust/cli/build/commands.rs +153 -103
  38. package/rust/cli/build/mod.rs +2 -2
  39. package/rust/cli/build/process.rs +165 -146
  40. package/rust/cli/check/mod.rs +208 -208
  41. package/rust/cli/discover/commands.rs +53 -31
  42. package/rust/cli/discover/config.rs +2 -4
  43. package/rust/cli/discover/install.rs +139 -28
  44. package/rust/cli/discover/metadata.rs +3 -3
  45. package/rust/cli/login/commands.rs +124 -124
  46. package/rust/cli/me/commands.rs +52 -0
  47. package/rust/cli/me/mod.rs +1 -0
  48. package/rust/cli/mod.rs +2 -2
  49. package/rust/cli/parser.rs +76 -70
  50. package/rust/cli/play/commands.rs +375 -324
  51. package/rust/cli/play/mod.rs +5 -5
  52. package/rust/cli/play/process.rs +159 -150
  53. package/rust/cli/play/realtime.rs +91 -91
  54. package/rust/cli/telemetry/commands.rs +22 -22
  55. package/rust/cli/telemetry/event_creator.rs +80 -80
  56. package/rust/cli/telemetry/mod.rs +3 -3
  57. package/rust/cli/telemetry/send.rs +51 -51
  58. package/rust/cli/template/commands.rs +69 -69
  59. package/rust/config/driver.rs +112 -103
  60. package/rust/config/mod.rs +3 -3
  61. package/rust/config/ops.rs +26 -26
  62. package/rust/config/settings.rs +101 -101
  63. package/rust/core/audio/engine/driver.rs +237 -0
  64. package/rust/core/audio/engine/export.rs +169 -0
  65. package/rust/core/audio/engine/helpers.rs +178 -170
  66. package/rust/core/audio/engine/mod.rs +56 -7
  67. package/rust/core/audio/engine/notes/dsp.rs +88 -0
  68. package/rust/core/audio/engine/notes/mod.rs +53 -0
  69. package/rust/core/audio/engine/notes/params.rs +294 -0
  70. package/rust/core/audio/engine/sample/insert.rs +300 -0
  71. package/rust/core/audio/engine/sample/mod.rs +40 -0
  72. package/rust/core/audio/engine/sample/padding.rs +170 -0
  73. package/rust/core/audio/evaluator/condition.rs +61 -0
  74. package/rust/core/audio/evaluator/mod.rs +9 -0
  75. package/rust/core/audio/{evaluator.rs → evaluator/numeric.rs} +152 -310
  76. package/rust/core/audio/evaluator/rhs.rs +16 -0
  77. package/rust/core/audio/evaluator/string_expr.rs +94 -0
  78. package/rust/core/audio/interpreter/driver.rs +574 -542
  79. package/rust/core/audio/interpreter/mod.rs +2 -14
  80. package/rust/core/audio/interpreter/statements/arrow_call/interprete.rs +179 -0
  81. package/rust/core/audio/interpreter/statements/arrow_call/methods/chord.rs +398 -0
  82. package/rust/core/audio/interpreter/statements/arrow_call/methods/effects.rs +323 -0
  83. package/rust/core/audio/interpreter/statements/arrow_call/methods/mod.rs +3 -0
  84. package/rust/core/audio/interpreter/statements/arrow_call/methods/note.rs +371 -0
  85. package/rust/core/audio/interpreter/statements/arrow_call/mod.rs +3 -0
  86. package/rust/core/audio/interpreter/statements/arrow_call/types/arp.rs +192 -0
  87. package/rust/core/audio/interpreter/statements/arrow_call/types/mod.rs +24 -0
  88. package/rust/core/audio/interpreter/statements/arrow_call/types/pad.rs +116 -0
  89. package/rust/core/audio/interpreter/statements/arrow_call/types/pluck.rs +97 -0
  90. package/rust/core/audio/interpreter/statements/arrow_call/types/sub.rs +100 -0
  91. package/rust/core/audio/interpreter/{automate.rs → statements/automate.rs} +2 -4
  92. package/rust/core/audio/interpreter/{call.rs → statements/call.rs} +36 -5
  93. package/rust/core/audio/interpreter/{condition.rs → statements/condition.rs} +72 -71
  94. package/rust/core/audio/interpreter/{function.rs → statements/function.rs} +24 -26
  95. package/rust/core/audio/interpreter/{let_.rs → statements/let_.rs} +36 -38
  96. package/rust/core/audio/interpreter/{load.rs → statements/load.rs} +17 -19
  97. package/rust/core/audio/interpreter/{loop_.rs → statements/loop_.rs} +115 -114
  98. package/rust/core/audio/interpreter/statements/mod.rs +12 -0
  99. package/rust/core/audio/interpreter/{sleep.rs → statements/sleep.rs} +28 -28
  100. package/rust/core/audio/interpreter/{spawn.rs → statements/spawn.rs} +54 -4
  101. package/rust/core/audio/interpreter/{tempo.rs → statements/tempo.rs} +40 -40
  102. package/rust/core/audio/interpreter/{trigger.rs → statements/trigger.rs} +242 -239
  103. package/rust/core/audio/loader/trigger.rs +98 -97
  104. package/rust/core/audio/mod.rs +6 -7
  105. package/rust/core/audio/special/easing.rs +189 -189
  106. package/rust/core/audio/special/env.rs +45 -45
  107. package/rust/core/audio/special/math.rs +134 -134
  108. package/rust/core/audio/special/modulator.rs +143 -143
  109. package/rust/core/builder/mod.rs +129 -86
  110. package/rust/core/debugger/{module.rs → logs.rs} +52 -55
  111. package/rust/core/debugger/mod.rs +30 -30
  112. package/rust/core/debugger/store.rs +38 -40
  113. package/rust/core/error/mod.rs +269 -269
  114. package/rust/core/lexer/driver.rs +2 -4
  115. package/rust/core/mod.rs +9 -10
  116. package/rust/core/parser/driver/block.rs +111 -0
  117. package/rust/core/parser/driver/cursor.rs +82 -0
  118. package/rust/core/parser/driver/driver_impl.rs +159 -0
  119. package/rust/core/parser/driver/mod.rs +6 -0
  120. package/rust/core/parser/driver/parse_array.rs +120 -0
  121. package/rust/core/parser/driver/parse_map.rs +247 -0
  122. package/rust/core/parser/driver/parser.rs +160 -0
  123. package/rust/core/parser/handler/arrow_call.rs +90 -15
  124. package/rust/core/parser/handler/at.rs +279 -279
  125. package/rust/core/parser/handler/bank.rs +104 -104
  126. package/rust/core/parser/handler/condition.rs +83 -83
  127. package/rust/core/parser/handler/dot.rs +148 -148
  128. package/rust/core/parser/handler/identifier/automate.rs +254 -254
  129. package/rust/core/parser/handler/identifier/call.rs +91 -91
  130. package/rust/core/parser/handler/identifier/emit.rs +70 -70
  131. package/rust/core/parser/handler/identifier/function.rs +113 -113
  132. package/rust/core/parser/handler/identifier/group.rs +89 -89
  133. package/rust/core/parser/handler/identifier/let_.rs +173 -173
  134. package/rust/core/parser/handler/identifier/mod.rs +55 -55
  135. package/rust/core/parser/handler/identifier/on.rs +107 -107
  136. package/rust/core/parser/handler/identifier/print.rs +49 -49
  137. package/rust/core/parser/handler/identifier/sleep.rs +96 -43
  138. package/rust/core/parser/handler/identifier/spawn.rs +91 -91
  139. package/rust/core/parser/handler/identifier/synth.rs +39 -3
  140. package/rust/core/parser/handler/loop_.rs +194 -194
  141. package/rust/core/parser/handler/pattern.rs +25 -2
  142. package/rust/core/parser/handler/tempo.rs +105 -57
  143. package/rust/core/parser/statement.rs +10 -11
  144. package/rust/core/plugin/loader.rs +137 -137
  145. package/rust/core/plugin/runner/mod.rs +11 -0
  146. package/rust/core/plugin/{runner.rs → runner/non_wasm.rs} +206 -72
  147. package/rust/core/plugin/runner/wasm32.rs +44 -0
  148. package/rust/core/preprocessor/loader/inject.rs +313 -0
  149. package/rust/core/preprocessor/loader/loader_helpers.rs +110 -0
  150. package/rust/core/preprocessor/loader/mod.rs +235 -0
  151. package/rust/core/preprocessor/module.rs +55 -60
  152. package/rust/core/preprocessor/{processor.rs → processor/handlers.rs} +107 -114
  153. package/rust/core/preprocessor/processor/mod.rs +1 -0
  154. package/rust/core/preprocessor/resolver/function.rs +69 -69
  155. package/rust/core/preprocessor/resolver/group.rs +122 -94
  156. package/rust/core/preprocessor/resolver/pattern.rs +14 -2
  157. package/rust/core/store/global.rs +57 -61
  158. package/rust/core/store/mod.rs +1 -5
  159. package/rust/lib.rs +323 -308
  160. package/rust/macros/Cargo.toml +14 -0
  161. package/rust/macros/src/lib.rs +52 -0
  162. package/rust/main.rs +336 -143
  163. package/rust/types/Cargo.toml +1 -1
  164. package/rust/types/src/addons.rs +57 -55
  165. package/rust/types/src/config.rs +82 -74
  166. package/rust/types/src/lib.rs +15 -12
  167. package/rust/types/src/plugin.rs +20 -0
  168. package/rust/types/src/store.rs +139 -0
  169. package/rust/types/src/telemetry.rs +85 -85
  170. package/rust/utils/Cargo.toml +5 -2
  171. package/rust/utils/src/file.rs +477 -94
  172. package/rust/utils/src/first_usage.rs +97 -97
  173. package/rust/utils/src/lib.rs +9 -9
  174. package/rust/utils/src/logger.rs +200 -200
  175. package/rust/utils/src/path.rs +158 -88
  176. package/rust/utils/src/signature.rs +41 -41
  177. package/rust/utils/src/spinner.rs +20 -20
  178. package/rust/utils/src/version.rs +58 -27
  179. package/rust/utils/src/watcher.rs +46 -46
  180. package/rust/web/api.rs +5 -5
  181. package/rust/web/auth.rs +5 -0
  182. package/rust/web/cdn.rs +34 -34
  183. package/rust/web/forge.rs +5 -0
  184. package/rust/web/mod.rs +2 -0
  185. package/tests/integration.rs +21 -21
  186. package/typescript/core/functions/index.ts +11 -0
  187. package/typescript/pkg/devalang_core.ts +20 -4
  188. package/typescript/scripts/version/copy-to-binary.ts +82 -0
  189. package/rust/cli/bank/api.rs +0 -122
  190. package/rust/cli/bank/commands.rs +0 -275
  191. package/rust/cli/bank/mod.rs +0 -29
  192. package/rust/cli/install/bank.rs +0 -53
  193. package/rust/cli/install/commands.rs +0 -35
  194. package/rust/cli/install/mod.rs +0 -4
  195. package/rust/cli/install/plugin.rs +0 -61
  196. package/rust/core/audio/engine/sample.rs +0 -366
  197. package/rust/core/audio/engine/synth.rs +0 -325
  198. package/rust/core/audio/interpreter/arrow_call.rs +0 -311
  199. package/rust/core/audio/renderer.rs +0 -54
  200. package/rust/core/parser/driver.rs +0 -584
  201. package/rust/core/preprocessor/loader.rs +0 -637
  202. package/rust/core/store/export.rs +0 -28
  203. package/rust/core/store/function.rs +0 -40
  204. package/rust/core/store/import.rs +0 -28
  205. package/rust/core/store/variable.rs +0 -51
  206. package/rust/core/utils/mod.rs +0 -1
  207. package/rust/core/utils/path.rs +0 -37
@@ -5,7 +5,7 @@ use devalang_types::Value;
5
5
  use crate::core::{
6
6
  lexer::token::Token,
7
7
  parser::{
8
- driver::Parser,
8
+ driver::parser::Parser,
9
9
  handler::dot::parse_dot_token,
10
10
  statement::{Statement, StatementKind},
11
11
  },
@@ -23,8 +23,9 @@ pub fn parse_synth_token(
23
23
  return Statement::unknown();
24
24
  };
25
25
 
26
- // Expect a provider/waveform identifier (can be dotted: alias.synth)
26
+ // Expect a provider or waveform identifier (can be dotted: alias.synth)
27
27
  // Also accept a dot-led entity by delegating to the dot parser (e.g. .module.export)
28
+ // Support optional explicit waveform after a dotted provider: `synth alias.synth saw {}`
28
29
  let synth_waveform = if let Some(first_token) = parser.peek_clone() {
29
30
  use crate::core::lexer::token::TokenKind;
30
31
 
@@ -81,7 +82,42 @@ pub fn parse_synth_token(
81
82
  }
82
83
  }
83
84
 
84
- parts.join(".")
85
+ // The joined parts form either:
86
+ // - a simple waveform name (e.g. "sine")
87
+ // - a provider entity (e.g. "author.synth")
88
+ let first = parts.join(".");
89
+
90
+ // If this looks like a dotted provider, try to consume an optional explicit
91
+ // waveform identifier on the same line (e.g. `synth author.synth saw`). If none
92
+ // is provided, fall back to the original behaviour (use the joined string as
93
+ // the waveform identifier) to remain backward-compatible.
94
+ if first.contains('.') {
95
+ // peek next token on same line
96
+ if let Some(next_tok) = parser.peek_clone() {
97
+ if next_tok.line == current_line
98
+ && matches!(
99
+ next_tok.kind,
100
+ crate::core::lexer::token::TokenKind::Identifier
101
+ | crate::core::lexer::token::TokenKind::Number
102
+ | crate::core::lexer::token::TokenKind::Synth
103
+ )
104
+ {
105
+ // consume waveform token
106
+ let waveform = next_tok.lexeme.clone();
107
+ parser.advance();
108
+ // store as "provider.waveform" style? keep only waveform for compatibility
109
+ waveform
110
+ } else {
111
+ // no explicit waveform -> use the provider string (old behaviour)
112
+ first
113
+ }
114
+ } else {
115
+ first
116
+ }
117
+ } else {
118
+ // simple waveform name
119
+ first
120
+ }
85
121
  }
86
122
  } else {
87
123
  return crate::core::parser::statement::error_from_token(
@@ -1,194 +1,194 @@
1
- use devalang_types::Value;
2
-
3
- use crate::core::{
4
- lexer::token::TokenKind,
5
- parser::{
6
- driver::Parser,
7
- statement::{Statement, StatementKind},
8
- },
9
- store::global::GlobalStore,
10
- };
11
-
12
- pub fn parse_loop_token(parser: &mut Parser, global_store: &mut GlobalStore) -> Statement {
13
- parser.advance(); // consume 'loop' or 'for' (aliased in lexer)
14
- let Some(loop_token) = parser.previous_clone() else {
15
- return Statement::unknown();
16
- };
17
-
18
- // Support two forms:
19
- // 1) loop <count>:
20
- // 2) for <ident> in [a,b,c]:
21
-
22
- // Peek next to decide
23
- let Some(next_token) = parser.peek_clone() else {
24
- return Statement::error_with_pos(
25
- loop_token.indent,
26
- loop_token.line,
27
- loop_token.column,
28
- "Expected iterator after loop/for".to_string(),
29
- );
30
- };
31
-
32
- // Try to detect 'for <ident> in [array]:' form
33
- let mut foreach_ident: Option<String> = None;
34
- if let TokenKind::Identifier = next_token.kind {
35
- // Could be either count identifier (old form) or foreach variable
36
- // Look ahead for 'in'
37
- let name = next_token.lexeme.clone();
38
- // don't consume yet; we'll branch
39
- if let Some(t2) = parser.peek_nth(1) {
40
- if t2.kind == TokenKind::Identifier && t2.lexeme == "in" {
41
- // foreach form
42
- foreach_ident = Some(name);
43
- // consume ident and 'in'
44
- parser.advance();
45
- parser.advance();
46
- }
47
- }
48
- }
49
-
50
- if let Some(var_name) = foreach_ident {
51
- // Expect [array] OR number OR string OR identifier after 'in'
52
- let array_val = if let Some(tok) = parser.peek_clone() {
53
- match tok.kind {
54
- TokenKind::LBracket => {
55
- if let Some(v) = parser.parse_array_value() {
56
- v
57
- } else {
58
- Value::Array(vec![])
59
- }
60
- }
61
- TokenKind::Number => {
62
- parser.advance();
63
- let n = tok.lexeme.parse::<f32>().unwrap_or(0.0);
64
- Value::Number(n)
65
- }
66
- TokenKind::String => {
67
- parser.advance();
68
- Value::String(tok.lexeme.clone())
69
- }
70
- TokenKind::Identifier => {
71
- parser.advance();
72
- Value::Identifier(tok.lexeme.clone())
73
- }
74
- _ => {
75
- return Statement::error_with_pos(
76
- loop_token.indent,
77
- loop_token.line,
78
- loop_token.column,
79
- "Expected array, number, string or identifier after 'in'".to_string(),
80
- );
81
- }
82
- }
83
- } else {
84
- return Statement::error_with_pos(
85
- loop_token.indent,
86
- loop_token.line,
87
- loop_token.column,
88
- "Expected array, number, string or identifier after 'in'".to_string(),
89
- );
90
- };
91
-
92
- // Expect ':'
93
- if !parser.match_token(TokenKind::Colon) {
94
- return Statement::error_with_pos(
95
- loop_token.indent,
96
- loop_token.line,
97
- loop_token.column,
98
- "Expected ':' after foreach header".to_string(),
99
- );
100
- }
101
-
102
- let tokens =
103
- parser.collect_until(|t| t.kind == TokenKind::Dedent || t.kind == TokenKind::EOF);
104
- let loop_body = parser.parse_block(tokens.clone(), global_store);
105
- if let Some(token) = parser.peek() {
106
- if token.kind == TokenKind::Dedent {
107
- parser.advance();
108
- }
109
- }
110
-
111
- let mut value_map = std::collections::HashMap::new();
112
- value_map.insert("foreach".to_string(), Value::Identifier(var_name));
113
- value_map.insert("array".to_string(), array_val);
114
- value_map.insert("body".to_string(), Value::Block(loop_body.clone()));
115
-
116
- return Statement {
117
- kind: StatementKind::Loop,
118
- value: Value::Map(value_map),
119
- indent: loop_token.indent,
120
- line: loop_token.line,
121
- column: loop_token.column,
122
- };
123
- }
124
-
125
- // Fallback to legacy: loop <count>:
126
- let Some(iterator_token) = parser.peek_clone() else {
127
- return Statement::error_with_pos(
128
- loop_token.indent,
129
- loop_token.line,
130
- loop_token.column,
131
- "Expected number or identifier after 'loop'".to_string(),
132
- );
133
- };
134
-
135
- let iterator_value = match iterator_token.kind {
136
- TokenKind::Number => {
137
- let val = iterator_token.lexeme.parse::<f32>().unwrap_or(1.0);
138
- parser.advance();
139
- Value::Number(val)
140
- }
141
- TokenKind::Identifier => {
142
- let val = iterator_token.lexeme.clone();
143
- parser.advance();
144
- Value::Identifier(val)
145
- }
146
- TokenKind::String => {
147
- // strings that are numeric (e.g. "10")
148
- let s = iterator_token.lexeme.clone();
149
- parser.advance();
150
- Value::String(s)
151
- }
152
- _ => {
153
- return Statement::error_with_pos(
154
- iterator_token.clone().indent,
155
- iterator_token.clone().line,
156
- iterator_token.clone().column,
157
- "Expected a number, string or identifier as loop count".to_string(),
158
- );
159
- }
160
- };
161
-
162
- if !parser.match_token(TokenKind::Colon) {
163
- let message = format!(
164
- "Expected ':' after loop count, got {:?}",
165
- parser.peek_kind()
166
- );
167
- return Statement::error_with_pos(
168
- loop_token.clone().indent,
169
- loop_token.clone().line,
170
- loop_token.clone().column,
171
- message,
172
- );
173
- }
174
-
175
- let tokens = parser.collect_until(|t| t.kind == TokenKind::Dedent || t.kind == TokenKind::EOF);
176
- let loop_body = parser.parse_block(tokens.clone(), global_store);
177
- if let Some(token) = parser.peek() {
178
- if token.kind == TokenKind::Dedent {
179
- parser.advance();
180
- }
181
- }
182
-
183
- let mut value_map = std::collections::HashMap::new();
184
- value_map.insert("iterator".to_string(), iterator_value);
185
- value_map.insert("body".to_string(), Value::Block(loop_body.clone()));
186
-
187
- Statement {
188
- kind: StatementKind::Loop,
189
- value: Value::Map(value_map),
190
- indent: loop_token.indent,
191
- line: loop_token.line,
192
- column: loop_token.column,
193
- }
194
- }
1
+ use devalang_types::Value;
2
+
3
+ use crate::core::{
4
+ lexer::token::TokenKind,
5
+ parser::{
6
+ driver::parser::Parser,
7
+ statement::{Statement, StatementKind},
8
+ },
9
+ store::global::GlobalStore,
10
+ };
11
+
12
+ pub fn parse_loop_token(parser: &mut Parser, global_store: &mut GlobalStore) -> Statement {
13
+ parser.advance(); // consume 'loop' or 'for' (aliased in lexer)
14
+ let Some(loop_token) = parser.previous_clone() else {
15
+ return Statement::unknown();
16
+ };
17
+
18
+ // Support two forms:
19
+ // 1) loop <count>:
20
+ // 2) for <ident> in [a,b,c]:
21
+
22
+ // Peek next to decide
23
+ let Some(next_token) = parser.peek_clone() else {
24
+ return Statement::error_with_pos(
25
+ loop_token.indent,
26
+ loop_token.line,
27
+ loop_token.column,
28
+ "Expected iterator after loop/for".to_string(),
29
+ );
30
+ };
31
+
32
+ // Try to detect 'for <ident> in [array]:' form
33
+ let mut foreach_ident: Option<String> = None;
34
+ if let TokenKind::Identifier = next_token.kind {
35
+ // Could be either count identifier (old form) or foreach variable
36
+ // Look ahead for 'in'
37
+ let name = next_token.lexeme.clone();
38
+ // don't consume yet; we'll branch
39
+ if let Some(t2) = parser.peek_nth(1) {
40
+ if t2.kind == TokenKind::Identifier && t2.lexeme == "in" {
41
+ // foreach form
42
+ foreach_ident = Some(name);
43
+ // consume ident and 'in'
44
+ parser.advance();
45
+ parser.advance();
46
+ }
47
+ }
48
+ }
49
+
50
+ if let Some(var_name) = foreach_ident {
51
+ // Expect [array] OR number OR string OR identifier after 'in'
52
+ let array_val = if let Some(tok) = parser.peek_clone() {
53
+ match tok.kind {
54
+ TokenKind::LBracket => {
55
+ if let Some(v) = parser.parse_array_value() {
56
+ v
57
+ } else {
58
+ Value::Array(vec![])
59
+ }
60
+ }
61
+ TokenKind::Number => {
62
+ parser.advance();
63
+ let n = tok.lexeme.parse::<f32>().unwrap_or(0.0);
64
+ Value::Number(n)
65
+ }
66
+ TokenKind::String => {
67
+ parser.advance();
68
+ Value::String(tok.lexeme.clone())
69
+ }
70
+ TokenKind::Identifier => {
71
+ parser.advance();
72
+ Value::Identifier(tok.lexeme.clone())
73
+ }
74
+ _ => {
75
+ return Statement::error_with_pos(
76
+ loop_token.indent,
77
+ loop_token.line,
78
+ loop_token.column,
79
+ "Expected array, number, string or identifier after 'in'".to_string(),
80
+ );
81
+ }
82
+ }
83
+ } else {
84
+ return Statement::error_with_pos(
85
+ loop_token.indent,
86
+ loop_token.line,
87
+ loop_token.column,
88
+ "Expected array, number, string or identifier after 'in'".to_string(),
89
+ );
90
+ };
91
+
92
+ // Expect ':'
93
+ if !parser.match_token(TokenKind::Colon) {
94
+ return Statement::error_with_pos(
95
+ loop_token.indent,
96
+ loop_token.line,
97
+ loop_token.column,
98
+ "Expected ':' after foreach header".to_string(),
99
+ );
100
+ }
101
+
102
+ let tokens =
103
+ parser.collect_until(|t| t.kind == TokenKind::Dedent || t.kind == TokenKind::EOF);
104
+ let loop_body = parser.parse_block(tokens.clone(), global_store);
105
+ if let Some(token) = parser.peek() {
106
+ if token.kind == TokenKind::Dedent {
107
+ parser.advance();
108
+ }
109
+ }
110
+
111
+ let mut value_map = std::collections::HashMap::new();
112
+ value_map.insert("foreach".to_string(), Value::Identifier(var_name));
113
+ value_map.insert("array".to_string(), array_val);
114
+ value_map.insert("body".to_string(), Value::Block(loop_body.clone()));
115
+
116
+ return Statement {
117
+ kind: StatementKind::Loop,
118
+ value: Value::Map(value_map),
119
+ indent: loop_token.indent,
120
+ line: loop_token.line,
121
+ column: loop_token.column,
122
+ };
123
+ }
124
+
125
+ // Fallback to legacy: loop <count>:
126
+ let Some(iterator_token) = parser.peek_clone() else {
127
+ return Statement::error_with_pos(
128
+ loop_token.indent,
129
+ loop_token.line,
130
+ loop_token.column,
131
+ "Expected number or identifier after 'loop'".to_string(),
132
+ );
133
+ };
134
+
135
+ let iterator_value = match iterator_token.kind {
136
+ TokenKind::Number => {
137
+ let val = iterator_token.lexeme.parse::<f32>().unwrap_or(1.0);
138
+ parser.advance();
139
+ Value::Number(val)
140
+ }
141
+ TokenKind::Identifier => {
142
+ let val = iterator_token.lexeme.clone();
143
+ parser.advance();
144
+ Value::Identifier(val)
145
+ }
146
+ TokenKind::String => {
147
+ // strings that are numeric (e.g. "10")
148
+ let s = iterator_token.lexeme.clone();
149
+ parser.advance();
150
+ Value::String(s)
151
+ }
152
+ _ => {
153
+ return Statement::error_with_pos(
154
+ iterator_token.clone().indent,
155
+ iterator_token.clone().line,
156
+ iterator_token.clone().column,
157
+ "Expected a number, string or identifier as loop count".to_string(),
158
+ );
159
+ }
160
+ };
161
+
162
+ if !parser.match_token(TokenKind::Colon) {
163
+ let message = format!(
164
+ "Expected ':' after loop count, got {:?}",
165
+ parser.peek_kind()
166
+ );
167
+ return Statement::error_with_pos(
168
+ loop_token.clone().indent,
169
+ loop_token.clone().line,
170
+ loop_token.clone().column,
171
+ message,
172
+ );
173
+ }
174
+
175
+ let tokens = parser.collect_until(|t| t.kind == TokenKind::Dedent || t.kind == TokenKind::EOF);
176
+ let loop_body = parser.parse_block(tokens.clone(), global_store);
177
+ if let Some(token) = parser.peek() {
178
+ if token.kind == TokenKind::Dedent {
179
+ parser.advance();
180
+ }
181
+ }
182
+
183
+ let mut value_map = std::collections::HashMap::new();
184
+ value_map.insert("iterator".to_string(), iterator_value);
185
+ value_map.insert("body".to_string(), Value::Block(loop_body.clone()));
186
+
187
+ Statement {
188
+ kind: StatementKind::Loop,
189
+ value: Value::Map(value_map),
190
+ indent: loop_token.indent,
191
+ line: loop_token.line,
192
+ column: loop_token.column,
193
+ }
194
+ }
@@ -3,7 +3,7 @@ use devalang_types::Value;
3
3
  use crate::core::{
4
4
  lexer::token::TokenKind,
5
5
  parser::{
6
- driver::Parser,
6
+ driver::parser::Parser,
7
7
  statement::{Statement, StatementKind},
8
8
  },
9
9
  store::global::GlobalStore,
@@ -52,6 +52,12 @@ pub fn parse_pattern_token(parser: &mut Parser, _global_store: &mut GlobalStore)
52
52
  }
53
53
  }
54
54
 
55
+ // optional inline options map like { swing: 0.08 }
56
+ let mut options: Option<Value> = None;
57
+ if parser.check_token(TokenKind::LBrace) {
58
+ options = parser.parse_map_value();
59
+ }
60
+
55
61
  // optional '=' and pattern string
56
62
  let mut value: Value = Value::Null;
57
63
  if parser.peek_is("=") {
@@ -59,9 +65,26 @@ pub fn parse_pattern_token(parser: &mut Parser, _global_store: &mut GlobalStore)
59
65
  if let Some(tok3) = parser.peek_clone() {
60
66
  if tok3.kind == TokenKind::String {
61
67
  parser.advance();
62
- value = Value::String(tok3.lexeme.clone());
68
+ let pat_str = Value::String(tok3.lexeme.clone());
69
+ if let Some(opts) = options {
70
+ // merge options map with pattern key
71
+ if let Value::Map(mut m) = opts {
72
+ m.insert("pattern".to_string(), pat_str);
73
+ value = Value::Map(m);
74
+ } else {
75
+ // unexpected, wrap into a map
76
+ let mut m = std::collections::HashMap::new();
77
+ m.insert("pattern".to_string(), pat_str);
78
+ value = Value::Map(m);
79
+ }
80
+ } else {
81
+ value = Value::String(tok3.lexeme.clone());
82
+ }
63
83
  }
64
84
  }
85
+ } else if let Some(opts) = options {
86
+ // only options were provided, store them as the value
87
+ value = opts;
65
88
  }
66
89
 
67
90
  Statement {
@@ -1,57 +1,105 @@
1
- use crate::core::{
2
- lexer::token::TokenKind,
3
- parser::{
4
- driver::Parser,
5
- statement::{Statement, StatementKind},
6
- },
7
- store::global::GlobalStore,
8
- };
9
- use devalang_types::Value;
10
-
11
- pub fn parse_tempo_token(parser: &mut Parser, _global_store: &mut GlobalStore) -> Statement {
12
- parser.advance(); // consume 'bpm'
13
-
14
- let Some(tempo_token) = parser.previous_clone() else {
15
- return Statement::unknown();
16
- };
17
-
18
- // Expect a number or identifier
19
- let Some(value_token) = parser.peek_clone() else {
20
- return Statement::error_with_pos(
21
- tempo_token.indent,
22
- tempo_token.line,
23
- tempo_token.column,
24
- "Expected a number or identifier after 'bpm'".to_string(),
25
- );
26
- };
27
-
28
- let value = match value_token.kind {
29
- TokenKind::Number => {
30
- parser.advance();
31
- Value::Number(value_token.lexeme.parse().unwrap_or(0.0))
32
- }
33
- TokenKind::Identifier => {
34
- parser.advance();
35
- Value::Identifier(value_token.lexeme.clone())
36
- }
37
- _ => {
38
- return Statement::error_with_pos(
39
- value_token.indent,
40
- value_token.line,
41
- value_token.column,
42
- format!(
43
- "Expected a number or identifier after 'bpm', got {:?}",
44
- value_token.kind
45
- ),
46
- );
47
- }
48
- };
49
-
50
- Statement {
51
- kind: StatementKind::Tempo,
52
- value,
53
- indent: tempo_token.indent,
54
- line: tempo_token.line,
55
- column: tempo_token.column,
56
- }
57
- }
1
+ use crate::core::{
2
+ lexer::token::TokenKind,
3
+ parser::{
4
+ driver::parser::Parser,
5
+ statement::{Statement, StatementKind},
6
+ },
7
+ store::global::GlobalStore,
8
+ };
9
+ use devalang_types::Value;
10
+
11
+ pub fn parse_tempo_token(parser: &mut Parser, _global_store: &mut GlobalStore) -> Statement {
12
+ parser.advance(); // consume 'bpm'
13
+
14
+ let Some(tempo_token) = parser.previous_clone() else {
15
+ return Statement::unknown();
16
+ };
17
+
18
+ // Expect a number or identifier
19
+ let Some(value_token) = parser.peek_clone() else {
20
+ return Statement::error_with_pos(
21
+ tempo_token.indent,
22
+ tempo_token.line,
23
+ tempo_token.column,
24
+ "Expected a number or identifier after 'bpm'".to_string(),
25
+ );
26
+ };
27
+
28
+ let value = match value_token.kind {
29
+ TokenKind::Number => {
30
+ // support decimals and fraction forms
31
+ let mut num = value_token.lexeme.clone();
32
+ parser.advance();
33
+ if let Some(dot) = parser.peek_clone() {
34
+ if dot.kind == TokenKind::Dot {
35
+ if let Some(next) = parser.peek_nth(1).cloned() {
36
+ if next.kind == TokenKind::Number {
37
+ parser.advance();
38
+ parser.advance();
39
+ num.push('.');
40
+ num.push_str(&next.lexeme);
41
+ }
42
+ }
43
+ }
44
+ }
45
+
46
+ if let Some(slash) = parser.peek_clone() {
47
+ if slash.kind == TokenKind::Slash {
48
+ parser.advance();
49
+ if let Some(den) = parser.peek_clone() {
50
+ if den.kind == TokenKind::Number || den.kind == TokenKind::Identifier {
51
+ let frac = format!("{}/{}", num, den.lexeme);
52
+ parser.advance();
53
+ Value::Duration(devalang_types::Duration::Beat(frac))
54
+ } else {
55
+ return Statement::error_with_pos(
56
+ slash.indent,
57
+ slash.line,
58
+ slash.column,
59
+ "Expected denominator after '/' in bpm".to_string(),
60
+ );
61
+ }
62
+ } else {
63
+ return Statement::error_with_pos(
64
+ slash.indent,
65
+ slash.line,
66
+ slash.column,
67
+ "Expected denominator after '/' in bpm".to_string(),
68
+ );
69
+ }
70
+ } else {
71
+ Value::Number(num.parse().unwrap_or(0.0))
72
+ }
73
+ } else {
74
+ Value::Number(num.parse().unwrap_or(0.0))
75
+ }
76
+ }
77
+ TokenKind::Identifier => {
78
+ parser.advance();
79
+ Value::Identifier(value_token.lexeme.clone())
80
+ }
81
+ TokenKind::String => {
82
+ parser.advance();
83
+ Value::String(value_token.lexeme.clone())
84
+ }
85
+ _ => {
86
+ return Statement::error_with_pos(
87
+ value_token.indent,
88
+ value_token.line,
89
+ value_token.column,
90
+ format!(
91
+ "Expected a number, string or identifier after 'bpm', got {:?}",
92
+ value_token.kind
93
+ ),
94
+ );
95
+ }
96
+ };
97
+
98
+ Statement {
99
+ kind: StatementKind::Tempo,
100
+ value,
101
+ indent: tempo_token.indent,
102
+ line: tempo_token.line,
103
+ column: tempo_token.column,
104
+ }
105
+ }