zexus 1.6.2

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 (227) hide show
  1. package/LICENSE +0 -0
  2. package/README.md +2513 -0
  3. package/bin/zexus +2 -0
  4. package/bin/zpics +2 -0
  5. package/bin/zpm +2 -0
  6. package/bin/zx +2 -0
  7. package/bin/zx-deploy +2 -0
  8. package/bin/zx-dev +2 -0
  9. package/bin/zx-run +2 -0
  10. package/package.json +66 -0
  11. package/scripts/README.md +24 -0
  12. package/scripts/postinstall.js +44 -0
  13. package/shared_config.json +24 -0
  14. package/src/README.md +1525 -0
  15. package/src/tests/run_zexus_tests.py +117 -0
  16. package/src/tests/test_all_phases.zx +346 -0
  17. package/src/tests/test_blockchain_features.zx +306 -0
  18. package/src/tests/test_complexity_features.zx +321 -0
  19. package/src/tests/test_core_integration.py +185 -0
  20. package/src/tests/test_phase10_ecosystem.zx +177 -0
  21. package/src/tests/test_phase1_modifiers.zx +87 -0
  22. package/src/tests/test_phase2_plugins.zx +80 -0
  23. package/src/tests/test_phase3_security.zx +97 -0
  24. package/src/tests/test_phase4_vfs.zx +116 -0
  25. package/src/tests/test_phase5_types.zx +117 -0
  26. package/src/tests/test_phase6_metaprogramming.zx +125 -0
  27. package/src/tests/test_phase7_optimization.zx +132 -0
  28. package/src/tests/test_phase9_advanced_types.zx +157 -0
  29. package/src/tests/test_security_features.py +419 -0
  30. package/src/tests/test_security_features.zx +276 -0
  31. package/src/tests/test_simple_zx.zx +1 -0
  32. package/src/tests/test_verification_simple.zx +69 -0
  33. package/src/zexus/__init__.py +28 -0
  34. package/src/zexus/__main__.py +5 -0
  35. package/src/zexus/__pycache__/__init__.cpython-312.pyc +0 -0
  36. package/src/zexus/__pycache__/advanced_types.cpython-312.pyc +0 -0
  37. package/src/zexus/__pycache__/builtin_modules.cpython-312.pyc +0 -0
  38. package/src/zexus/__pycache__/capability_system.cpython-312.pyc +0 -0
  39. package/src/zexus/__pycache__/complexity_system.cpython-312.pyc +0 -0
  40. package/src/zexus/__pycache__/concurrency_system.cpython-312.pyc +0 -0
  41. package/src/zexus/__pycache__/config.cpython-312.pyc +0 -0
  42. package/src/zexus/__pycache__/dependency_injection.cpython-312.pyc +0 -0
  43. package/src/zexus/__pycache__/ecosystem.cpython-312.pyc +0 -0
  44. package/src/zexus/__pycache__/environment.cpython-312.pyc +0 -0
  45. package/src/zexus/__pycache__/error_reporter.cpython-312.pyc +0 -0
  46. package/src/zexus/__pycache__/hybrid_orchestrator.cpython-312.pyc +0 -0
  47. package/src/zexus/__pycache__/lexer.cpython-312.pyc +0 -0
  48. package/src/zexus/__pycache__/metaprogramming.cpython-312.pyc +0 -0
  49. package/src/zexus/__pycache__/module_cache.cpython-312.pyc +0 -0
  50. package/src/zexus/__pycache__/object.cpython-312.pyc +0 -0
  51. package/src/zexus/__pycache__/optimization.cpython-312.pyc +0 -0
  52. package/src/zexus/__pycache__/plugin_system.cpython-312.pyc +0 -0
  53. package/src/zexus/__pycache__/policy_engine.cpython-312.pyc +0 -0
  54. package/src/zexus/__pycache__/security.cpython-312.pyc +0 -0
  55. package/src/zexus/__pycache__/stdlib_integration.cpython-312.pyc +0 -0
  56. package/src/zexus/__pycache__/strategy_recovery.cpython-312.pyc +0 -0
  57. package/src/zexus/__pycache__/syntax_validator.cpython-312.pyc +0 -0
  58. package/src/zexus/__pycache__/type_system.cpython-312.pyc +0 -0
  59. package/src/zexus/__pycache__/virtual_filesystem.cpython-312.pyc +0 -0
  60. package/src/zexus/__pycache__/zexus_ast.cpython-312.pyc +0 -0
  61. package/src/zexus/__pycache__/zexus_token.cpython-312.pyc +0 -0
  62. package/src/zexus/advanced_types.py +401 -0
  63. package/src/zexus/blockchain/__init__.py +40 -0
  64. package/src/zexus/blockchain/__pycache__/__init__.cpython-312.pyc +0 -0
  65. package/src/zexus/blockchain/__pycache__/crypto.cpython-312.pyc +0 -0
  66. package/src/zexus/blockchain/__pycache__/ledger.cpython-312.pyc +0 -0
  67. package/src/zexus/blockchain/__pycache__/transaction.cpython-312.pyc +0 -0
  68. package/src/zexus/blockchain/crypto.py +463 -0
  69. package/src/zexus/blockchain/ledger.py +255 -0
  70. package/src/zexus/blockchain/transaction.py +267 -0
  71. package/src/zexus/builtin_modules.py +284 -0
  72. package/src/zexus/builtin_plugins.py +317 -0
  73. package/src/zexus/capability_system.py +372 -0
  74. package/src/zexus/cli/__init__.py +2 -0
  75. package/src/zexus/cli/__pycache__/__init__.cpython-312.pyc +0 -0
  76. package/src/zexus/cli/__pycache__/main.cpython-312.pyc +0 -0
  77. package/src/zexus/cli/main.py +707 -0
  78. package/src/zexus/cli/zpm.py +203 -0
  79. package/src/zexus/compare_interpreter_compiler.py +146 -0
  80. package/src/zexus/compiler/__init__.py +169 -0
  81. package/src/zexus/compiler/__pycache__/__init__.cpython-312.pyc +0 -0
  82. package/src/zexus/compiler/__pycache__/lexer.cpython-312.pyc +0 -0
  83. package/src/zexus/compiler/__pycache__/parser.cpython-312.pyc +0 -0
  84. package/src/zexus/compiler/__pycache__/zexus_ast.cpython-312.pyc +0 -0
  85. package/src/zexus/compiler/bytecode.py +266 -0
  86. package/src/zexus/compiler/compat_runtime.py +277 -0
  87. package/src/zexus/compiler/lexer.py +257 -0
  88. package/src/zexus/compiler/parser.py +779 -0
  89. package/src/zexus/compiler/semantic.py +118 -0
  90. package/src/zexus/compiler/zexus_ast.py +454 -0
  91. package/src/zexus/complexity_system.py +575 -0
  92. package/src/zexus/concurrency_system.py +493 -0
  93. package/src/zexus/config.py +201 -0
  94. package/src/zexus/crypto_bridge.py +19 -0
  95. package/src/zexus/dependency_injection.py +423 -0
  96. package/src/zexus/ecosystem.py +434 -0
  97. package/src/zexus/environment.py +101 -0
  98. package/src/zexus/environment_manager.py +119 -0
  99. package/src/zexus/error_reporter.py +314 -0
  100. package/src/zexus/evaluator/__init__.py +12 -0
  101. package/src/zexus/evaluator/__pycache__/__init__.cpython-312.pyc +0 -0
  102. package/src/zexus/evaluator/__pycache__/bytecode_compiler.cpython-312.pyc +0 -0
  103. package/src/zexus/evaluator/__pycache__/core.cpython-312.pyc +0 -0
  104. package/src/zexus/evaluator/__pycache__/expressions.cpython-312.pyc +0 -0
  105. package/src/zexus/evaluator/__pycache__/functions.cpython-312.pyc +0 -0
  106. package/src/zexus/evaluator/__pycache__/integration.cpython-312.pyc +0 -0
  107. package/src/zexus/evaluator/__pycache__/statements.cpython-312.pyc +0 -0
  108. package/src/zexus/evaluator/__pycache__/utils.cpython-312.pyc +0 -0
  109. package/src/zexus/evaluator/bytecode_compiler.py +700 -0
  110. package/src/zexus/evaluator/core.py +891 -0
  111. package/src/zexus/evaluator/expressions.py +827 -0
  112. package/src/zexus/evaluator/functions.py +3989 -0
  113. package/src/zexus/evaluator/integration.py +396 -0
  114. package/src/zexus/evaluator/statements.py +4303 -0
  115. package/src/zexus/evaluator/utils.py +126 -0
  116. package/src/zexus/evaluator_original.py +2041 -0
  117. package/src/zexus/external_bridge.py +16 -0
  118. package/src/zexus/find_affected_imports.sh +155 -0
  119. package/src/zexus/hybrid_orchestrator.py +152 -0
  120. package/src/zexus/input_validation.py +259 -0
  121. package/src/zexus/lexer.py +571 -0
  122. package/src/zexus/logging.py +89 -0
  123. package/src/zexus/lsp/__init__.py +9 -0
  124. package/src/zexus/lsp/completion_provider.py +207 -0
  125. package/src/zexus/lsp/definition_provider.py +22 -0
  126. package/src/zexus/lsp/hover_provider.py +71 -0
  127. package/src/zexus/lsp/server.py +269 -0
  128. package/src/zexus/lsp/symbol_provider.py +31 -0
  129. package/src/zexus/metaprogramming.py +321 -0
  130. package/src/zexus/module_cache.py +89 -0
  131. package/src/zexus/module_manager.py +107 -0
  132. package/src/zexus/object.py +973 -0
  133. package/src/zexus/optimization.py +424 -0
  134. package/src/zexus/parser/__init__.py +31 -0
  135. package/src/zexus/parser/__pycache__/__init__.cpython-312.pyc +0 -0
  136. package/src/zexus/parser/__pycache__/parser.cpython-312.pyc +0 -0
  137. package/src/zexus/parser/__pycache__/strategy_context.cpython-312.pyc +0 -0
  138. package/src/zexus/parser/__pycache__/strategy_structural.cpython-312.pyc +0 -0
  139. package/src/zexus/parser/integration.py +86 -0
  140. package/src/zexus/parser/parser.py +3977 -0
  141. package/src/zexus/parser/strategy_context.py +7254 -0
  142. package/src/zexus/parser/strategy_structural.py +1033 -0
  143. package/src/zexus/persistence.py +391 -0
  144. package/src/zexus/plugin_system.py +290 -0
  145. package/src/zexus/policy_engine.py +365 -0
  146. package/src/zexus/profiler/__init__.py +5 -0
  147. package/src/zexus/profiler/profiler.py +233 -0
  148. package/src/zexus/purity_system.py +398 -0
  149. package/src/zexus/runtime/__init__.py +20 -0
  150. package/src/zexus/runtime/async_runtime.py +324 -0
  151. package/src/zexus/search_old_imports.sh +65 -0
  152. package/src/zexus/security.py +1407 -0
  153. package/src/zexus/stack_trace.py +233 -0
  154. package/src/zexus/stdlib/__init__.py +27 -0
  155. package/src/zexus/stdlib/blockchain.py +341 -0
  156. package/src/zexus/stdlib/compression.py +167 -0
  157. package/src/zexus/stdlib/crypto.py +124 -0
  158. package/src/zexus/stdlib/datetime.py +163 -0
  159. package/src/zexus/stdlib/db_mongo.py +199 -0
  160. package/src/zexus/stdlib/db_mysql.py +162 -0
  161. package/src/zexus/stdlib/db_postgres.py +163 -0
  162. package/src/zexus/stdlib/db_sqlite.py +133 -0
  163. package/src/zexus/stdlib/encoding.py +230 -0
  164. package/src/zexus/stdlib/fs.py +195 -0
  165. package/src/zexus/stdlib/http.py +219 -0
  166. package/src/zexus/stdlib/http_server.py +248 -0
  167. package/src/zexus/stdlib/json_module.py +61 -0
  168. package/src/zexus/stdlib/math.py +360 -0
  169. package/src/zexus/stdlib/os_module.py +265 -0
  170. package/src/zexus/stdlib/regex.py +148 -0
  171. package/src/zexus/stdlib/sockets.py +253 -0
  172. package/src/zexus/stdlib/test_framework.zx +208 -0
  173. package/src/zexus/stdlib/test_runner.zx +119 -0
  174. package/src/zexus/stdlib_integration.py +341 -0
  175. package/src/zexus/strategy_recovery.py +256 -0
  176. package/src/zexus/syntax_validator.py +356 -0
  177. package/src/zexus/testing/zpics.py +407 -0
  178. package/src/zexus/testing/zpics_runtime.py +369 -0
  179. package/src/zexus/type_system.py +374 -0
  180. package/src/zexus/validation_system.py +569 -0
  181. package/src/zexus/virtual_filesystem.py +355 -0
  182. package/src/zexus/vm/__init__.py +8 -0
  183. package/src/zexus/vm/__pycache__/__init__.cpython-312.pyc +0 -0
  184. package/src/zexus/vm/__pycache__/async_optimizer.cpython-312.pyc +0 -0
  185. package/src/zexus/vm/__pycache__/bytecode.cpython-312.pyc +0 -0
  186. package/src/zexus/vm/__pycache__/cache.cpython-312.pyc +0 -0
  187. package/src/zexus/vm/__pycache__/jit.cpython-312.pyc +0 -0
  188. package/src/zexus/vm/__pycache__/memory_manager.cpython-312.pyc +0 -0
  189. package/src/zexus/vm/__pycache__/memory_pool.cpython-312.pyc +0 -0
  190. package/src/zexus/vm/__pycache__/optimizer.cpython-312.pyc +0 -0
  191. package/src/zexus/vm/__pycache__/parallel_vm.cpython-312.pyc +0 -0
  192. package/src/zexus/vm/__pycache__/peephole_optimizer.cpython-312.pyc +0 -0
  193. package/src/zexus/vm/__pycache__/profiler.cpython-312.pyc +0 -0
  194. package/src/zexus/vm/__pycache__/register_allocator.cpython-312.pyc +0 -0
  195. package/src/zexus/vm/__pycache__/register_vm.cpython-312.pyc +0 -0
  196. package/src/zexus/vm/__pycache__/ssa_converter.cpython-312.pyc +0 -0
  197. package/src/zexus/vm/__pycache__/vm.cpython-312.pyc +0 -0
  198. package/src/zexus/vm/async_optimizer.py +420 -0
  199. package/src/zexus/vm/bytecode.py +428 -0
  200. package/src/zexus/vm/bytecode_converter.py +297 -0
  201. package/src/zexus/vm/cache.py +532 -0
  202. package/src/zexus/vm/jit.py +720 -0
  203. package/src/zexus/vm/memory_manager.py +520 -0
  204. package/src/zexus/vm/memory_pool.py +511 -0
  205. package/src/zexus/vm/optimizer.py +478 -0
  206. package/src/zexus/vm/parallel_vm.py +899 -0
  207. package/src/zexus/vm/peephole_optimizer.py +452 -0
  208. package/src/zexus/vm/profiler.py +527 -0
  209. package/src/zexus/vm/register_allocator.py +462 -0
  210. package/src/zexus/vm/register_vm.py +520 -0
  211. package/src/zexus/vm/ssa_converter.py +757 -0
  212. package/src/zexus/vm/vm.py +1392 -0
  213. package/src/zexus/zexus_ast.py +1782 -0
  214. package/src/zexus/zexus_token.py +253 -0
  215. package/src/zexus/zpm/__init__.py +15 -0
  216. package/src/zexus/zpm/installer.py +116 -0
  217. package/src/zexus/zpm/package_manager.py +208 -0
  218. package/src/zexus/zpm/publisher.py +98 -0
  219. package/src/zexus/zpm/registry.py +110 -0
  220. package/src/zexus.egg-info/PKG-INFO +2235 -0
  221. package/src/zexus.egg-info/SOURCES.txt +876 -0
  222. package/src/zexus.egg-info/dependency_links.txt +1 -0
  223. package/src/zexus.egg-info/entry_points.txt +3 -0
  224. package/src/zexus.egg-info/not-zip-safe +1 -0
  225. package/src/zexus.egg-info/requires.txt +14 -0
  226. package/src/zexus.egg-info/top_level.txt +2 -0
  227. package/zexus.json +14 -0
@@ -0,0 +1,119 @@
1
+ #!/usr/bin/env zexus
2
+ # Zexus Test Runner
3
+ # Discovers and runs test files
4
+
5
+ # Import test framework (when import is available)
6
+ # For now, we'll eval the file
7
+
8
+ action run_test_file(filepath) {
9
+ print("\n🧪 Running: " + filepath)
10
+ print(string_repeat("-", 60))
11
+
12
+ try {
13
+ # Execute the test file
14
+ eval_file(filepath, "zexus")
15
+ return true
16
+ } catch (e) {
17
+ print("❌ Error loading test file: " + string(e))
18
+ return false
19
+ }
20
+ }
21
+
22
+ action string_repeat(str, count) {
23
+ let result = ""
24
+ let i = 0
25
+ while i < count {
26
+ result = result + str
27
+ i = i + 1
28
+ }
29
+ return result
30
+ }
31
+
32
+ action ends_with(str, suffix) {
33
+ let str_len = len(str)
34
+ let suffix_len = len(suffix)
35
+
36
+ if suffix_len > str_len {
37
+ return false
38
+ }
39
+
40
+ # Simple substring check - would need proper string methods
41
+ # For now, just check if it contains .test.zx
42
+ return true # Simplified for now
43
+ }
44
+
45
+ action discover_tests(directory) {
46
+ print("🔍 Discovering tests in: " + directory)
47
+
48
+ let all_files = file_list_dir(directory)
49
+ let test_files = []
50
+
51
+ let i = 0
52
+ while i < len(all_files) {
53
+ let file = all_files[i]
54
+
55
+ # Check if file ends with .test.zx
56
+ # Simple check: contains "test" and ends with ".zx"
57
+ if file_exists(directory + "/" + file) {
58
+ if fs_is_file(directory + "/" + file) {
59
+ # Add to test files if it looks like a test
60
+ let file_lower = file # Would need tolower()
61
+ # For now, just check if filename contains "test"
62
+ test_files = push(test_files, directory + "/" + file)
63
+ }
64
+ }
65
+
66
+ i = i + 1
67
+ }
68
+
69
+ return test_files
70
+ }
71
+
72
+ action run_all_tests(directory) {
73
+ print("╔" + string_repeat("═", 58) + "╗")
74
+ print("║" + " ZEXUS TEST RUNNER " + "║")
75
+ print("╚" + string_repeat("═", 58) + "╝")
76
+
77
+ let test_files = discover_tests(directory)
78
+
79
+ if len(test_files) == 0 {
80
+ print("\n⚠️ No test files found in: " + directory)
81
+ return 1
82
+ }
83
+
84
+ print("\nFound " + string(len(test_files)) + " test file(s)")
85
+
86
+ let i = 0
87
+ let passed = 0
88
+ let failed = 0
89
+
90
+ while i < len(test_files) {
91
+ let test_file = test_files[i]
92
+
93
+ if run_test_file(test_file) {
94
+ passed = passed + 1
95
+ } else {
96
+ failed = failed + 1
97
+ }
98
+
99
+ i = i + 1
100
+ }
101
+
102
+ # Print overall summary
103
+ print("\n" + string_repeat("═", 60))
104
+ print("OVERALL RESULTS")
105
+ print(string_repeat("═", 60))
106
+ print("Test files run: " + string(len(test_files)))
107
+ print("Passed: " + string(passed) + " ✓")
108
+ print("Failed: " + string(failed) + " ✗")
109
+ print(string_repeat("═", 60))
110
+
111
+ if failed == 0 {
112
+ print("🎉 ALL TEST FILES PASSED!")
113
+ return 0
114
+ } else {
115
+ print("❌ SOME TEST FILES FAILED")
116
+ return 1
117
+ }
118
+ }
119
+
@@ -0,0 +1,341 @@
1
+ """
2
+ Standard Library Integration for Zexus
3
+ Provides integration between Python stdlib modules and Zexus evaluator.
4
+ """
5
+
6
+ from .object import Environment, Builtin, String, Integer, Float, Boolean, Map, List as ListObj, EvaluationError
7
+
8
+
9
+ def create_stdlib_module(module_name, evaluator=None):
10
+ """
11
+ Create a Zexus environment for a stdlib module.
12
+
13
+ Args:
14
+ module_name: Name of the stdlib module (fs, http, json, datetime, crypto, blockchain)
15
+ evaluator: Optional evaluator instance
16
+
17
+ Returns:
18
+ Environment object with stdlib functions registered
19
+ """
20
+ env = Environment()
21
+
22
+ if module_name == "fs" or module_name == "stdlib/fs":
23
+ from .stdlib.fs import FileSystemModule
24
+
25
+ # Register all fs functions
26
+ def _fs_read_file(*args):
27
+ if len(args) < 1:
28
+ return EvaluationError("read_file() requires at least 1 argument: path")
29
+ path = args[0].value if hasattr(args[0], 'value') else str(args[0])
30
+ encoding = args[1].value if len(args) > 1 and hasattr(args[1], 'value') else 'utf-8'
31
+ try:
32
+ result = FileSystemModule.read_file(path, encoding)
33
+ return String(result)
34
+ except Exception as e:
35
+ return EvaluationError(f"read_file error: {str(e)}")
36
+
37
+ def _fs_write_file(*args):
38
+ if len(args) < 2:
39
+ return EvaluationError("write_file() requires 2 arguments: path, content")
40
+ path = args[0].value if hasattr(args[0], 'value') else str(args[0])
41
+ content = args[1].value if hasattr(args[1], 'value') else str(args[1])
42
+ encoding = args[2].value if len(args) > 2 and hasattr(args[2], 'value') else 'utf-8'
43
+ try:
44
+ FileSystemModule.write_file(path, content, encoding)
45
+ return Boolean(True)
46
+ except Exception as e:
47
+ return EvaluationError(f"write_file error: {str(e)}")
48
+
49
+ def _fs_exists(*args):
50
+ if len(args) < 1:
51
+ return EvaluationError("exists() requires 1 argument: path")
52
+ path = args[0].value if hasattr(args[0], 'value') else str(args[0])
53
+ result = FileSystemModule.exists(path)
54
+ return Boolean(result)
55
+
56
+ def _fs_mkdir(*args):
57
+ if len(args) < 1:
58
+ return EvaluationError("mkdir() requires 1 argument: path")
59
+ path = args[0].value if hasattr(args[0], 'value') else str(args[0])
60
+ try:
61
+ FileSystemModule.mkdir(path)
62
+ return Boolean(True)
63
+ except Exception as e:
64
+ return EvaluationError(f"mkdir error: {str(e)}")
65
+
66
+ def _fs_list_dir(*args):
67
+ path = args[0].value if len(args) > 0 and hasattr(args[0], 'value') else '.'
68
+ try:
69
+ result = FileSystemModule.list_dir(path)
70
+ return ListObj([String(f) for f in result])
71
+ except Exception as e:
72
+ return EvaluationError(f"list_dir error: {str(e)}")
73
+
74
+ env.set("read_file", Builtin(_fs_read_file))
75
+ env.set("write_file", Builtin(_fs_write_file))
76
+ env.set("exists", Builtin(_fs_exists))
77
+ env.set("mkdir", Builtin(_fs_mkdir))
78
+ env.set("list_dir", Builtin(_fs_list_dir))
79
+
80
+ elif module_name == "http" or module_name == "stdlib/http":
81
+ from .stdlib.http import HttpModule
82
+
83
+ def _http_get(*args):
84
+ if len(args) < 1:
85
+ return EvaluationError("get() requires 1 argument: url")
86
+ url = args[0].value if hasattr(args[0], 'value') else str(args[0])
87
+ try:
88
+ result = HttpModule.get(url)
89
+ return Map({
90
+ String("status"): Integer(result['status']),
91
+ String("body"): String(result['body']),
92
+ String("headers"): Map({String(k): String(v) for k, v in result['headers'].items()})
93
+ })
94
+ except Exception as e:
95
+ return EvaluationError(f"get error: {str(e)}")
96
+
97
+ def _http_post(*args):
98
+ if len(args) < 1:
99
+ return EvaluationError("post() requires at least 1 argument: url")
100
+ url = args[0].value if hasattr(args[0], 'value') else str(args[0])
101
+ data = args[1].value if len(args) > 1 and hasattr(args[1], 'value') else None
102
+ try:
103
+ result = HttpModule.post(url, data)
104
+ return Map({
105
+ String("status"): Integer(result['status']),
106
+ String("body"): String(result['body']),
107
+ String("headers"): Map({String(k): String(v) for k, v in result['headers'].items()})
108
+ })
109
+ except Exception as e:
110
+ return EvaluationError(f"post error: {str(e)}")
111
+
112
+ env.set("get", Builtin(_http_get))
113
+ env.set("post", Builtin(_http_post))
114
+
115
+ elif module_name == "json" or module_name == "stdlib/json":
116
+ from .stdlib.json_module import JsonModule
117
+ import json as json_lib
118
+
119
+ def _json_parse(*args):
120
+ if len(args) < 1:
121
+ return EvaluationError("parse() requires 1 argument: text")
122
+ text = args[0].value if hasattr(args[0], 'value') else str(args[0])
123
+ try:
124
+ result = JsonModule.parse(text)
125
+ return _python_to_zexus(result)
126
+ except Exception as e:
127
+ return EvaluationError(f"parse error: {str(e)}")
128
+
129
+ def _json_stringify(*args):
130
+ if len(args) < 1:
131
+ return EvaluationError("stringify() requires 1 argument: obj")
132
+ obj = _zexus_to_python(args[0])
133
+ try:
134
+ result = JsonModule.stringify(obj)
135
+ return String(result)
136
+ except Exception as e:
137
+ return EvaluationError(f"stringify error: {str(e)}")
138
+
139
+ env.set("parse", Builtin(_json_parse))
140
+ env.set("stringify", Builtin(_json_stringify))
141
+
142
+ elif module_name == "datetime" or module_name == "stdlib/datetime":
143
+ from .stdlib.datetime import DateTimeModule
144
+ from datetime import datetime
145
+
146
+ def _datetime_now(*args):
147
+ try:
148
+ result = DateTimeModule.now()
149
+ return String(result.isoformat())
150
+ except Exception as e:
151
+ return EvaluationError(f"now error: {str(e)}")
152
+
153
+ def _datetime_timestamp(*args):
154
+ try:
155
+ result = DateTimeModule.timestamp()
156
+ return Float(result)
157
+ except Exception as e:
158
+ return EvaluationError(f"timestamp error: {str(e)}")
159
+
160
+ def _datetime_format(*args):
161
+ if len(args) < 1:
162
+ return EvaluationError("format() requires at least 1 argument")
163
+ # For simplicity, accept ISO string and format string
164
+ dt_str = args[0].value if hasattr(args[0], 'value') else str(args[0])
165
+ fmt = args[1].value if len(args) > 1 and hasattr(args[1], 'value') else '%Y-%m-%d %H:%M:%S'
166
+ try:
167
+ dt = datetime.fromisoformat(dt_str)
168
+ result = DateTimeModule.format(dt, fmt)
169
+ return String(result)
170
+ except Exception as e:
171
+ return EvaluationError(f"format error: {str(e)}")
172
+
173
+ env.set("now", Builtin(_datetime_now))
174
+ env.set("timestamp", Builtin(_datetime_timestamp))
175
+ env.set("format", Builtin(_datetime_format))
176
+
177
+ elif module_name == "crypto" or module_name == "stdlib/crypto":
178
+ from .stdlib.crypto import CryptoModule
179
+
180
+ def _crypto_hash_sha256(*args):
181
+ if len(args) < 1:
182
+ return EvaluationError("hash_sha256() requires 1 argument: data")
183
+ data = args[0].value if hasattr(args[0], 'value') else str(args[0])
184
+ try:
185
+ result = CryptoModule.hash_sha256(data)
186
+ return String(result)
187
+ except Exception as e:
188
+ return EvaluationError(f"hash_sha256 error: {str(e)}")
189
+
190
+ def _crypto_keccak256(*args):
191
+ if len(args) < 1:
192
+ return EvaluationError("keccak256() requires 1 argument: data")
193
+ data = args[0].value if hasattr(args[0], 'value') else str(args[0])
194
+ try:
195
+ result = CryptoModule.keccak256(data)
196
+ return String(result)
197
+ except Exception as e:
198
+ return EvaluationError(f"keccak256 error: {str(e)}")
199
+
200
+ def _crypto_random_bytes(*args):
201
+ size = 32 # default
202
+ if len(args) > 0:
203
+ if hasattr(args[0], 'value') and isinstance(args[0].value, int):
204
+ size = args[0].value
205
+ elif isinstance(args[0], int):
206
+ size = args[0]
207
+ else:
208
+ return EvaluationError("random_bytes() size argument must be an integer")
209
+ try:
210
+ result = CryptoModule.random_bytes(size)
211
+ return String(result)
212
+ except Exception as e:
213
+ return EvaluationError(f"random_bytes error: {str(e)}")
214
+
215
+ def _crypto_pbkdf2(*args):
216
+ if len(args) < 2:
217
+ return EvaluationError("pbkdf2() requires at least 2 arguments: password, salt")
218
+ password = args[0].value if hasattr(args[0], 'value') else str(args[0])
219
+ salt = args[1].value if hasattr(args[1], 'value') else str(args[1])
220
+
221
+ # Validate iterations parameter
222
+ iterations = 100000 # default
223
+ if len(args) > 2:
224
+ if hasattr(args[2], 'value') and isinstance(args[2].value, int):
225
+ iterations = args[2].value
226
+ elif isinstance(args[2], int):
227
+ iterations = args[2]
228
+ else:
229
+ return EvaluationError("pbkdf2() iterations argument must be an integer")
230
+
231
+ try:
232
+ result = CryptoModule.pbkdf2(password, salt, iterations)
233
+ return String(result)
234
+ except Exception as e:
235
+ return EvaluationError(f"pbkdf2 error: {str(e)}")
236
+
237
+ env.set("hash_sha256", Builtin(_crypto_hash_sha256))
238
+ env.set("keccak256", Builtin(_crypto_keccak256))
239
+ env.set("random_bytes", Builtin(_crypto_random_bytes))
240
+ env.set("pbkdf2", Builtin(_crypto_pbkdf2))
241
+
242
+ elif module_name == "blockchain" or module_name == "stdlib/blockchain":
243
+ from .stdlib.blockchain import BlockchainModule
244
+
245
+ def _blockchain_create_address(*args):
246
+ if len(args) < 1:
247
+ return EvaluationError("create_address() requires 1 argument: public_key")
248
+ public_key = args[0].value if hasattr(args[0], 'value') else str(args[0])
249
+ prefix = args[1].value if len(args) > 1 and hasattr(args[1], 'value') else "0x"
250
+ try:
251
+ result = BlockchainModule.create_address(public_key, prefix)
252
+ return String(result)
253
+ except Exception as e:
254
+ return EvaluationError(f"create_address error: {str(e)}")
255
+
256
+ def _blockchain_validate_address(*args):
257
+ if len(args) < 1:
258
+ return EvaluationError("validate_address() requires 1 argument: address")
259
+ address = args[0].value if hasattr(args[0], 'value') else str(args[0])
260
+ prefix = args[1].value if len(args) > 1 and hasattr(args[1], 'value') else "0x"
261
+ try:
262
+ result = BlockchainModule.validate_address(address, prefix)
263
+ return Boolean(result)
264
+ except Exception as e:
265
+ return EvaluationError(f"validate_address error: {str(e)}")
266
+
267
+ def _blockchain_calculate_merkle_root(*args):
268
+ if len(args) < 1:
269
+ return EvaluationError("calculate_merkle_root() requires 1 argument: hashes")
270
+ if not isinstance(args[0], ListObj):
271
+ return EvaluationError("calculate_merkle_root() expects a list")
272
+ hashes = [h.value if hasattr(h, 'value') else str(h) for h in args[0].elements]
273
+ try:
274
+ result = BlockchainModule.calculate_merkle_root(hashes)
275
+ return String(result)
276
+ except Exception as e:
277
+ return EvaluationError(f"calculate_merkle_root error: {str(e)}")
278
+
279
+ def _blockchain_create_genesis_block(*args):
280
+ try:
281
+ result = BlockchainModule.create_genesis_block()
282
+ return _python_to_zexus(result)
283
+ except Exception as e:
284
+ return EvaluationError(f"create_genesis_block error: {str(e)}")
285
+
286
+ env.set("create_address", Builtin(_blockchain_create_address))
287
+ env.set("validate_address", Builtin(_blockchain_validate_address))
288
+ env.set("calculate_merkle_root", Builtin(_blockchain_calculate_merkle_root))
289
+ env.set("create_genesis_block", Builtin(_blockchain_create_genesis_block))
290
+
291
+ return env
292
+
293
+
294
+ def _python_to_zexus(value):
295
+ """Convert Python value to Zexus object."""
296
+ if isinstance(value, bool):
297
+ return Boolean(value)
298
+ elif isinstance(value, int):
299
+ return Integer(value)
300
+ elif isinstance(value, float):
301
+ return Float(value)
302
+ elif isinstance(value, str):
303
+ return String(value)
304
+ elif isinstance(value, list):
305
+ return ListObj([_python_to_zexus(v) for v in value])
306
+ elif isinstance(value, dict):
307
+ return Map({String(k): _python_to_zexus(v) for k, v in value.items()})
308
+ else:
309
+ return String(str(value))
310
+
311
+
312
+ def _zexus_to_python(obj):
313
+ """Convert Zexus object to Python value."""
314
+ if hasattr(obj, 'value'):
315
+ return obj.value
316
+ elif isinstance(obj, ListObj):
317
+ return [_zexus_to_python(e) for e in obj.elements]
318
+ elif isinstance(obj, Map):
319
+ return {_zexus_to_python(k): _zexus_to_python(v) for k, v in obj.pairs.items()}
320
+ else:
321
+ return obj
322
+
323
+
324
+ def is_stdlib_module(module_name):
325
+ """Check if a module name refers to a stdlib module."""
326
+ stdlib_modules = ['fs', 'http', 'json', 'datetime', 'crypto', 'blockchain']
327
+
328
+ # Handle both "fs" and "stdlib/fs" formats
329
+ if module_name in stdlib_modules:
330
+ return True
331
+
332
+ if module_name.startswith('stdlib/'):
333
+ module_base = module_name[7:] # Remove 'stdlib/' prefix
334
+ return module_base in stdlib_modules
335
+
336
+ return False
337
+
338
+
339
+ def get_stdlib_module(module_name, evaluator=None):
340
+ """Get a stdlib module environment."""
341
+ return create_stdlib_module(module_name, evaluator)