@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
@@ -0,0 +1,235 @@
1
+ #[cfg(feature = "cli")]
2
+ use crate::core::preprocessor::resolver::driver::{
3
+ resolve_all_modules, resolve_and_flatten_all_modules,
4
+ };
5
+ // resolve_relative_path moved to loader_helpers
6
+ use crate::core::{
7
+ error::ErrorHandler,
8
+ lexer::{driver::Lexer, token::Token},
9
+ parser::{driver::parser::Parser, statement::Statement},
10
+ preprocessor::{module::Module, processor::handlers::process_modules},
11
+ store::global::GlobalStore,
12
+ };
13
+ use devalang_utils::path::normalize_path;
14
+ use std::{collections::HashMap, path::Path};
15
+
16
+ mod inject;
17
+ mod loader_helpers;
18
+
19
+ pub struct ModuleLoader {
20
+ pub entry: String,
21
+ pub output: String,
22
+ pub base_dir: String,
23
+ }
24
+
25
+ impl ModuleLoader {
26
+ pub fn new(entry: &str, output: &str) -> Self {
27
+ let base_dir = Path::new(entry)
28
+ .parent()
29
+ .unwrap_or(Path::new(""))
30
+ .to_string_lossy()
31
+ .replace('\\', "/");
32
+
33
+ Self {
34
+ entry: entry.to_string(),
35
+ output: output.to_string(),
36
+ base_dir,
37
+ }
38
+ }
39
+
40
+ pub fn from_raw_source(
41
+ entry_path: &str,
42
+ output_path: &str,
43
+ content: &str,
44
+ global_store: &mut GlobalStore,
45
+ ) -> Self {
46
+ let normalized_entry_path = normalize_path(entry_path);
47
+
48
+ let mut module = Module::new(entry_path);
49
+ module.content = content.to_string();
50
+
51
+ // Insert a module stub containing the provided content into the
52
+ // global store. This is used by the WASM APIs and tests which
53
+ // operate on in-memory sources instead of files on disk.
54
+ global_store.insert_module(normalized_entry_path.to_string(), module);
55
+
56
+ Self {
57
+ entry: normalized_entry_path.to_string(),
58
+ output: output_path.to_string(),
59
+ base_dir: "".to_string(),
60
+ }
61
+ }
62
+
63
+ pub fn extract_statements_map(
64
+ &self,
65
+ global_store: &GlobalStore,
66
+ ) -> HashMap<String, Vec<Statement>> {
67
+ global_store
68
+ .modules
69
+ .iter()
70
+ .map(|(path, module)| (path.clone(), module.statements.clone()))
71
+ .collect()
72
+ }
73
+
74
+ pub fn load_single_module(&self, global_store: &mut GlobalStore) -> Result<Module, String> {
75
+ let mut module = global_store
76
+ .modules
77
+ .remove(&self.entry)
78
+ .ok_or_else(|| format!("Module not found in store for path: {}", self.entry))?;
79
+
80
+ // SECTION Lexing the module content
81
+ let lexer = Lexer::new();
82
+ let tokens = lexer
83
+ .lex_from_source(&module.content)
84
+ .map_err(|e| format!("Lexer failed: {}", e))?;
85
+
86
+ module.tokens = tokens.clone();
87
+
88
+ // SECTION Parsing tokens into statements
89
+ let mut parser = Parser::new();
90
+ parser.set_current_module(self.entry.clone());
91
+ let statements = parser.parse_tokens(tokens, global_store);
92
+ module.statements = statements;
93
+
94
+ // SECTION Injecting bank triggers if any (legacy default for single-module run)
95
+ if let Err(e) = inject::inject_bank_triggers(&mut module, "808", None) {
96
+ return Err(format!("Failed to inject bank triggers: {}", e));
97
+ }
98
+
99
+ for (plugin_name, alias) in inject::extract_plugin_uses(&module.statements) {
100
+ inject::load_plugin_and_register(&mut module, &plugin_name, &alias, global_store);
101
+ }
102
+
103
+ global_store
104
+ .modules
105
+ .insert(self.entry.clone(), module.clone());
106
+
107
+ // SECTION Error handling
108
+ let mut error_handler = ErrorHandler::new();
109
+ error_handler.detect_from_statements(&mut parser, &module.statements);
110
+
111
+ Ok(module)
112
+ }
113
+
114
+ pub fn load_wasm_module(&self, global_store: &mut GlobalStore) -> Result<(), String> {
115
+ // Step one : Load the module from the global store
116
+ let module = {
117
+ let module_ref = global_store
118
+ .modules
119
+ .get(&self.entry)
120
+ .ok_or_else(|| format!("❌ Module not found for path: {}", self.entry))?;
121
+
122
+ Module::from_existing(&self.entry, module_ref.content.clone())
123
+ };
124
+
125
+ // Step two : lexing
126
+ let lexer = Lexer::new();
127
+ let tokens = lexer
128
+ .lex_from_source(&module.content)
129
+ .map_err(|e| format!("Lexer failed: {}", e))?;
130
+
131
+ // Step three : parsing
132
+ let mut parser = Parser::new();
133
+ parser.set_current_module(self.entry.clone());
134
+
135
+ let statements = parser.parse_tokens(tokens.clone(), global_store);
136
+
137
+ let mut updated_module = module;
138
+ updated_module.tokens = tokens;
139
+ updated_module.statements = statements;
140
+
141
+ // Step four : Injecting bank triggers if any
142
+ if let Err(e) = inject::inject_bank_triggers(&mut updated_module, "808", None) {
143
+ return Err(format!("Failed to inject bank triggers: {}", e));
144
+ }
145
+
146
+ // Insert the updated module into the global store before processing so
147
+ // process_modules can operate on it and populate variable_table, imports,
148
+ // and other derived structures.
149
+ global_store
150
+ .modules
151
+ .insert(self.entry.clone(), updated_module.clone());
152
+
153
+ // Process modules to populate module.variable_table, import/export tables,
154
+ // and other derived structures so runtime execution can resolve groups/synths.
155
+ process_modules(self, global_store);
156
+
157
+ for (plugin_name, alias) in inject::extract_plugin_uses(&updated_module.statements) {
158
+ inject::load_plugin_and_register(
159
+ &mut updated_module,
160
+ &plugin_name,
161
+ &alias,
162
+ global_store,
163
+ );
164
+ }
165
+
166
+ // Step four : error handling
167
+ let mut error_handler = ErrorHandler::new();
168
+ error_handler.detect_from_statements(&mut parser, &updated_module.statements);
169
+
170
+ // Final step : also expose module-level variables and functions into the global store
171
+ // so runtime evaluation (render_audio) can find group/synth definitions.
172
+ // Use the module instance that was actually processed by `process_modules`
173
+ // (it lives in `global_store.modules`) because `updated_module` is a local
174
+ // clone and won't contain the mutations applied by `process_modules`.
175
+ if let Some(stored_module) = global_store.modules.get(&self.entry) {
176
+ global_store
177
+ .variables
178
+ .variables
179
+ .extend(stored_module.variable_table.variables.clone());
180
+ global_store
181
+ .functions
182
+ .functions
183
+ .extend(stored_module.function_table.functions.clone());
184
+ } else {
185
+ // Fallback to the local updated_module if for any reason the module
186
+ // wasn't inserted into the store (defensive programming).
187
+ global_store
188
+ .variables
189
+ .variables
190
+ .extend(updated_module.variable_table.variables.clone());
191
+ global_store
192
+ .functions
193
+ .functions
194
+ .extend(updated_module.function_table.functions.clone());
195
+ }
196
+
197
+ Ok(())
198
+ }
199
+
200
+ #[cfg(feature = "cli")]
201
+ pub fn load_all_modules(
202
+ &self,
203
+ global_store: &mut GlobalStore,
204
+ ) -> (HashMap<String, Vec<Token>>, HashMap<String, Vec<Statement>>) {
205
+ // SECTION Load the entry module and its dependencies
206
+ let tokens_by_module = self.load_module_recursively(&self.entry, global_store);
207
+
208
+ // SECTION Process and resolve modules
209
+ process_modules(self, global_store);
210
+ resolve_all_modules(self, global_store);
211
+
212
+ // SECTION Flatten all modules to get statements (+ injects)
213
+ let statements_by_module = resolve_and_flatten_all_modules(global_store);
214
+
215
+ (tokens_by_module, statements_by_module)
216
+ }
217
+
218
+ #[cfg(feature = "cli")]
219
+ fn load_module_recursively(
220
+ &self,
221
+ raw_path: &str,
222
+ global_store: &mut GlobalStore,
223
+ ) -> HashMap<String, Vec<Token>> {
224
+ crate::core::preprocessor::loader::loader_helpers::load_module_recursively(
225
+ raw_path,
226
+ global_store,
227
+ )
228
+ }
229
+
230
+ #[cfg(feature = "cli")]
231
+ #[allow(dead_code)]
232
+ fn load_module_imports(&self, path: &String, global_store: &mut GlobalStore) {
233
+ crate::core::preprocessor::loader::loader_helpers::load_module_imports(path, global_store)
234
+ }
235
+ }
@@ -1,60 +1,55 @@
1
- use crate::core::{
2
- lexer::token::Token,
3
- parser::statement::Statement,
4
- store::{
5
- export::ExportTable, function::FunctionTable, import::ImportTable, variable::VariableTable,
6
- },
7
- };
8
-
9
- #[derive(Debug, Clone)]
10
- pub struct Module {
11
- pub path: String,
12
- pub resolved: bool,
13
- pub tokens: Vec<Token>,
14
- pub statements: Vec<Statement>,
15
- pub variable_table: VariableTable,
16
- pub function_table: FunctionTable,
17
- pub export_table: ExportTable,
18
- pub import_table: ImportTable,
19
- pub content: String,
20
- pub current_dir: String,
21
- }
22
-
23
- impl Module {
24
- pub fn new(path: &str) -> Self {
25
- Module {
26
- path: path.to_string(),
27
- tokens: Vec::new(),
28
- statements: Vec::new(),
29
- variable_table: VariableTable::new(),
30
- function_table: FunctionTable::new(),
31
- export_table: ExportTable::new(),
32
- import_table: ImportTable::new(),
33
- resolved: false,
34
- content: String::new(),
35
- current_dir: String::new(),
36
- }
37
- }
38
-
39
- pub fn is_resolved(&self) -> bool {
40
- self.resolved
41
- }
42
-
43
- pub fn set_resolved(&mut self, resolved: bool) {
44
- self.resolved = resolved;
45
- }
46
-
47
- pub fn add_token(&mut self, token: Token) {
48
- self.tokens.push(token);
49
- }
50
-
51
- pub fn add_statement(&mut self, statement: Statement) {
52
- self.statements.push(statement);
53
- }
54
-
55
- pub fn from_existing(path: &str, content: String) -> Self {
56
- let mut module = Module::new(path);
57
- module.content = content;
58
- module
59
- }
60
- }
1
+ use crate::core::{lexer::token::Token, parser::statement::Statement};
2
+ use devalang_types::{ExportTable, FunctionTable, ImportTable, VariableTable};
3
+
4
+ #[derive(Debug, Clone)]
5
+ pub struct Module {
6
+ pub path: String,
7
+ pub resolved: bool,
8
+ pub tokens: Vec<Token>,
9
+ pub statements: Vec<Statement>,
10
+ pub variable_table: VariableTable,
11
+ pub function_table: FunctionTable,
12
+ pub export_table: ExportTable,
13
+ pub import_table: ImportTable,
14
+ pub content: String,
15
+ pub current_dir: String,
16
+ }
17
+
18
+ impl Module {
19
+ pub fn new(path: &str) -> Self {
20
+ Module {
21
+ path: path.to_string(),
22
+ tokens: Vec::new(),
23
+ statements: Vec::new(),
24
+ variable_table: VariableTable::new(),
25
+ function_table: FunctionTable::new(),
26
+ export_table: ExportTable::new(),
27
+ import_table: ImportTable::new(),
28
+ resolved: false,
29
+ content: String::new(),
30
+ current_dir: String::new(),
31
+ }
32
+ }
33
+
34
+ pub fn is_resolved(&self) -> bool {
35
+ self.resolved
36
+ }
37
+
38
+ pub fn set_resolved(&mut self, resolved: bool) {
39
+ self.resolved = resolved;
40
+ }
41
+
42
+ pub fn add_token(&mut self, token: Token) {
43
+ self.tokens.push(token);
44
+ }
45
+
46
+ pub fn add_statement(&mut self, statement: Statement) {
47
+ self.statements.push(statement);
48
+ }
49
+
50
+ pub fn from_existing(path: &str, content: String) -> Self {
51
+ let mut module = Module::new(path);
52
+ module.content = content;
53
+ module
54
+ }
55
+ }
@@ -1,114 +1,107 @@
1
- use std::{collections::HashMap, path::Path};
2
-
3
- use crate::core::{
4
- parser::statement::StatementKind,
5
- preprocessor::loader::ModuleLoader,
6
- store::global::GlobalStore,
7
- utils::path::{normalize_path, resolve_relative_path},
8
- };
9
- use devalang_types::Value;
10
-
11
- pub fn process_modules(_module_loader: &ModuleLoader, global_store: &mut GlobalStore) {
12
- for module in global_store.modules.values_mut() {
13
- let logger = devalang_utils::logger::Logger::new();
14
- use devalang_utils::logger::LogLevel;
15
-
16
- for stmt in &module.statements {
17
- match &stmt.kind {
18
- StatementKind::Let { name } => {
19
- if let Value::Null = stmt.value {
20
- logger.log_message(
21
- LogLevel::Error,
22
- &format!("Variable '{}' is declared but not initialized.", name),
23
- );
24
-
25
- module.variable_table.variables.insert(
26
- name.clone(),
27
- Value::StatementKind(Box::new(stmt.kind.clone())),
28
- );
29
-
30
- continue;
31
- }
32
-
33
- if module.variable_table.get(name).is_some() {
34
- logger.log_message(
35
- LogLevel::Error,
36
- &format!("Variable '{}' is already defined in this scope.", name),
37
- );
38
- continue;
39
- }
40
-
41
- if let Some(module_variable) = module.variable_table.variables.get(name) {
42
- logger.log_message(
43
- LogLevel::Error,
44
- &format!(
45
- "Variable '{}' is already defined globally with value: {:?}",
46
- name, module_variable
47
- ),
48
- );
49
- continue;
50
- }
51
-
52
- module
53
- .variable_table
54
- .variables
55
- .insert(name.clone(), stmt.value.clone());
56
- }
57
-
58
- StatementKind::Load { source, alias } => {
59
- let module_dir = Path::new(&module.path).parent().unwrap_or(Path::new(""));
60
-
61
- let resolved_path = normalize_path(module_dir.join(source));
62
-
63
- module
64
- .variable_table
65
- .variables
66
- .insert(alias.clone(), Value::Sample(resolved_path));
67
- }
68
-
69
- StatementKind::Export { names, source: _ } => {
70
- for name in names {
71
- if let Some(val) = module.variable_table.get(name) {
72
- module.export_table.add_export(name.clone(), val.clone());
73
- }
74
- }
75
- }
76
-
77
- StatementKind::Import { names, source } => {
78
- let resolved = resolve_relative_path(&module.path, source);
79
- for name in names {
80
- module
81
- .import_table
82
- .add_import(name.clone(), Value::String(resolved.clone()));
83
- }
84
- }
85
-
86
- StatementKind::Group => {
87
- if let Value::Map(map) = &stmt.value {
88
- if let (Some(Value::String(name)), Some(Value::Block(body))) =
89
- (map.get("identifier"), map.get("body"))
90
- {
91
- let mut stored_map = HashMap::new();
92
-
93
- stored_map
94
- .insert("identifier".to_string(), Value::String(name.clone()));
95
-
96
- stored_map.insert("body".to_string(), Value::Block(body.clone()));
97
-
98
- module
99
- .variable_table
100
- .set(name.to_string(), Value::Map(stored_map));
101
- } else {
102
- logger.log_message(
103
- LogLevel::Error,
104
- &format!("Invalid group definition: {:?}", stmt.value),
105
- );
106
- }
107
- }
108
- }
109
-
110
- _ => {}
111
- }
112
- }
113
- }
114
- }
1
+ use crate::core::{
2
+ parser::statement::StatementKind, preprocessor::loader::ModuleLoader,
3
+ store::global::GlobalStore,
4
+ };
5
+ use devalang_types::Value;
6
+ use devalang_utils::path::{normalize_path, resolve_relative_path};
7
+ use std::collections::HashMap;
8
+
9
+ pub fn process_modules(_module_loader: &ModuleLoader, global_store: &mut GlobalStore) {
10
+ for module in global_store.modules.values_mut() {
11
+ let logger = devalang_utils::logger::Logger::new();
12
+ use devalang_utils::logger::LogLevel;
13
+
14
+ for stmt in &module.statements {
15
+ match &stmt.kind {
16
+ StatementKind::Let { name } => {
17
+ if let Value::Null = stmt.value {
18
+ logger.log_message(
19
+ LogLevel::Error,
20
+ &format!("Variable '{}' is declared but not initialized.", name),
21
+ );
22
+ module.variable_table.variables.insert(
23
+ name.clone(),
24
+ Value::StatementKind(Box::new(stmt.kind.clone())),
25
+ );
26
+ continue;
27
+ }
28
+
29
+ if module.variable_table.get(name).is_some() {
30
+ logger.log_message(
31
+ LogLevel::Error,
32
+ &format!("Variable '{}' is already defined in this scope.", name),
33
+ );
34
+ continue;
35
+ }
36
+
37
+ if let Some(module_variable) = module.variable_table.variables.get(name) {
38
+ logger.log_message(
39
+ LogLevel::Error,
40
+ &format!(
41
+ "Variable '{}' is already defined globally with value: {:?}",
42
+ name, module_variable
43
+ ),
44
+ );
45
+ continue;
46
+ }
47
+
48
+ module
49
+ .variable_table
50
+ .variables
51
+ .insert(name.clone(), stmt.value.clone());
52
+ }
53
+
54
+ StatementKind::Load { source, alias } => {
55
+ let module_dir = std::path::Path::new(&module.path)
56
+ .parent()
57
+ .unwrap_or(std::path::Path::new(""));
58
+ let resolved_path = normalize_path(module_dir.join(source));
59
+ module
60
+ .variable_table
61
+ .variables
62
+ .insert(alias.clone(), Value::Sample(resolved_path));
63
+ }
64
+
65
+ StatementKind::Export { names, source: _ } => {
66
+ for name in names {
67
+ if let Some(val) = module.variable_table.get(name) {
68
+ module.export_table.add_export(name.clone(), val.clone());
69
+ }
70
+ }
71
+ }
72
+
73
+ StatementKind::Import { names, source } => {
74
+ let resolved = resolve_relative_path(&module.path, source);
75
+ for name in names {
76
+ module
77
+ .import_table
78
+ .add_import(name.clone(), Value::String(resolved.clone()));
79
+ }
80
+ }
81
+
82
+ StatementKind::Group => {
83
+ if let Value::Map(map) = &stmt.value {
84
+ if let (Some(Value::String(name)), Some(Value::Block(body))) =
85
+ (map.get("identifier"), map.get("body"))
86
+ {
87
+ let mut stored_map = HashMap::new();
88
+ stored_map
89
+ .insert("identifier".to_string(), Value::String(name.clone()));
90
+ stored_map.insert("body".to_string(), Value::Block(body.clone()));
91
+ module
92
+ .variable_table
93
+ .set(name.to_string(), Value::Map(stored_map));
94
+ } else {
95
+ logger.log_message(
96
+ LogLevel::Error,
97
+ &format!("Invalid group definition: {:?}", stmt.value),
98
+ );
99
+ }
100
+ }
101
+ }
102
+
103
+ _ => {}
104
+ }
105
+ }
106
+ }
107
+ }
@@ -0,0 +1 @@
1
+ pub mod handlers;