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.
- package/LICENSE +0 -0
- package/README.md +2513 -0
- package/bin/zexus +2 -0
- package/bin/zpics +2 -0
- package/bin/zpm +2 -0
- package/bin/zx +2 -0
- package/bin/zx-deploy +2 -0
- package/bin/zx-dev +2 -0
- package/bin/zx-run +2 -0
- package/package.json +66 -0
- package/scripts/README.md +24 -0
- package/scripts/postinstall.js +44 -0
- package/shared_config.json +24 -0
- package/src/README.md +1525 -0
- package/src/tests/run_zexus_tests.py +117 -0
- package/src/tests/test_all_phases.zx +346 -0
- package/src/tests/test_blockchain_features.zx +306 -0
- package/src/tests/test_complexity_features.zx +321 -0
- package/src/tests/test_core_integration.py +185 -0
- package/src/tests/test_phase10_ecosystem.zx +177 -0
- package/src/tests/test_phase1_modifiers.zx +87 -0
- package/src/tests/test_phase2_plugins.zx +80 -0
- package/src/tests/test_phase3_security.zx +97 -0
- package/src/tests/test_phase4_vfs.zx +116 -0
- package/src/tests/test_phase5_types.zx +117 -0
- package/src/tests/test_phase6_metaprogramming.zx +125 -0
- package/src/tests/test_phase7_optimization.zx +132 -0
- package/src/tests/test_phase9_advanced_types.zx +157 -0
- package/src/tests/test_security_features.py +419 -0
- package/src/tests/test_security_features.zx +276 -0
- package/src/tests/test_simple_zx.zx +1 -0
- package/src/tests/test_verification_simple.zx +69 -0
- package/src/zexus/__init__.py +28 -0
- package/src/zexus/__main__.py +5 -0
- package/src/zexus/__pycache__/__init__.cpython-312.pyc +0 -0
- package/src/zexus/__pycache__/advanced_types.cpython-312.pyc +0 -0
- package/src/zexus/__pycache__/builtin_modules.cpython-312.pyc +0 -0
- package/src/zexus/__pycache__/capability_system.cpython-312.pyc +0 -0
- package/src/zexus/__pycache__/complexity_system.cpython-312.pyc +0 -0
- package/src/zexus/__pycache__/concurrency_system.cpython-312.pyc +0 -0
- package/src/zexus/__pycache__/config.cpython-312.pyc +0 -0
- package/src/zexus/__pycache__/dependency_injection.cpython-312.pyc +0 -0
- package/src/zexus/__pycache__/ecosystem.cpython-312.pyc +0 -0
- package/src/zexus/__pycache__/environment.cpython-312.pyc +0 -0
- package/src/zexus/__pycache__/error_reporter.cpython-312.pyc +0 -0
- package/src/zexus/__pycache__/hybrid_orchestrator.cpython-312.pyc +0 -0
- package/src/zexus/__pycache__/lexer.cpython-312.pyc +0 -0
- package/src/zexus/__pycache__/metaprogramming.cpython-312.pyc +0 -0
- package/src/zexus/__pycache__/module_cache.cpython-312.pyc +0 -0
- package/src/zexus/__pycache__/object.cpython-312.pyc +0 -0
- package/src/zexus/__pycache__/optimization.cpython-312.pyc +0 -0
- package/src/zexus/__pycache__/plugin_system.cpython-312.pyc +0 -0
- package/src/zexus/__pycache__/policy_engine.cpython-312.pyc +0 -0
- package/src/zexus/__pycache__/security.cpython-312.pyc +0 -0
- package/src/zexus/__pycache__/stdlib_integration.cpython-312.pyc +0 -0
- package/src/zexus/__pycache__/strategy_recovery.cpython-312.pyc +0 -0
- package/src/zexus/__pycache__/syntax_validator.cpython-312.pyc +0 -0
- package/src/zexus/__pycache__/type_system.cpython-312.pyc +0 -0
- package/src/zexus/__pycache__/virtual_filesystem.cpython-312.pyc +0 -0
- package/src/zexus/__pycache__/zexus_ast.cpython-312.pyc +0 -0
- package/src/zexus/__pycache__/zexus_token.cpython-312.pyc +0 -0
- package/src/zexus/advanced_types.py +401 -0
- package/src/zexus/blockchain/__init__.py +40 -0
- package/src/zexus/blockchain/__pycache__/__init__.cpython-312.pyc +0 -0
- package/src/zexus/blockchain/__pycache__/crypto.cpython-312.pyc +0 -0
- package/src/zexus/blockchain/__pycache__/ledger.cpython-312.pyc +0 -0
- package/src/zexus/blockchain/__pycache__/transaction.cpython-312.pyc +0 -0
- package/src/zexus/blockchain/crypto.py +463 -0
- package/src/zexus/blockchain/ledger.py +255 -0
- package/src/zexus/blockchain/transaction.py +267 -0
- package/src/zexus/builtin_modules.py +284 -0
- package/src/zexus/builtin_plugins.py +317 -0
- package/src/zexus/capability_system.py +372 -0
- package/src/zexus/cli/__init__.py +2 -0
- package/src/zexus/cli/__pycache__/__init__.cpython-312.pyc +0 -0
- package/src/zexus/cli/__pycache__/main.cpython-312.pyc +0 -0
- package/src/zexus/cli/main.py +707 -0
- package/src/zexus/cli/zpm.py +203 -0
- package/src/zexus/compare_interpreter_compiler.py +146 -0
- package/src/zexus/compiler/__init__.py +169 -0
- package/src/zexus/compiler/__pycache__/__init__.cpython-312.pyc +0 -0
- package/src/zexus/compiler/__pycache__/lexer.cpython-312.pyc +0 -0
- package/src/zexus/compiler/__pycache__/parser.cpython-312.pyc +0 -0
- package/src/zexus/compiler/__pycache__/zexus_ast.cpython-312.pyc +0 -0
- package/src/zexus/compiler/bytecode.py +266 -0
- package/src/zexus/compiler/compat_runtime.py +277 -0
- package/src/zexus/compiler/lexer.py +257 -0
- package/src/zexus/compiler/parser.py +779 -0
- package/src/zexus/compiler/semantic.py +118 -0
- package/src/zexus/compiler/zexus_ast.py +454 -0
- package/src/zexus/complexity_system.py +575 -0
- package/src/zexus/concurrency_system.py +493 -0
- package/src/zexus/config.py +201 -0
- package/src/zexus/crypto_bridge.py +19 -0
- package/src/zexus/dependency_injection.py +423 -0
- package/src/zexus/ecosystem.py +434 -0
- package/src/zexus/environment.py +101 -0
- package/src/zexus/environment_manager.py +119 -0
- package/src/zexus/error_reporter.py +314 -0
- package/src/zexus/evaluator/__init__.py +12 -0
- package/src/zexus/evaluator/__pycache__/__init__.cpython-312.pyc +0 -0
- package/src/zexus/evaluator/__pycache__/bytecode_compiler.cpython-312.pyc +0 -0
- package/src/zexus/evaluator/__pycache__/core.cpython-312.pyc +0 -0
- package/src/zexus/evaluator/__pycache__/expressions.cpython-312.pyc +0 -0
- package/src/zexus/evaluator/__pycache__/functions.cpython-312.pyc +0 -0
- package/src/zexus/evaluator/__pycache__/integration.cpython-312.pyc +0 -0
- package/src/zexus/evaluator/__pycache__/statements.cpython-312.pyc +0 -0
- package/src/zexus/evaluator/__pycache__/utils.cpython-312.pyc +0 -0
- package/src/zexus/evaluator/bytecode_compiler.py +700 -0
- package/src/zexus/evaluator/core.py +891 -0
- package/src/zexus/evaluator/expressions.py +827 -0
- package/src/zexus/evaluator/functions.py +3989 -0
- package/src/zexus/evaluator/integration.py +396 -0
- package/src/zexus/evaluator/statements.py +4303 -0
- package/src/zexus/evaluator/utils.py +126 -0
- package/src/zexus/evaluator_original.py +2041 -0
- package/src/zexus/external_bridge.py +16 -0
- package/src/zexus/find_affected_imports.sh +155 -0
- package/src/zexus/hybrid_orchestrator.py +152 -0
- package/src/zexus/input_validation.py +259 -0
- package/src/zexus/lexer.py +571 -0
- package/src/zexus/logging.py +89 -0
- package/src/zexus/lsp/__init__.py +9 -0
- package/src/zexus/lsp/completion_provider.py +207 -0
- package/src/zexus/lsp/definition_provider.py +22 -0
- package/src/zexus/lsp/hover_provider.py +71 -0
- package/src/zexus/lsp/server.py +269 -0
- package/src/zexus/lsp/symbol_provider.py +31 -0
- package/src/zexus/metaprogramming.py +321 -0
- package/src/zexus/module_cache.py +89 -0
- package/src/zexus/module_manager.py +107 -0
- package/src/zexus/object.py +973 -0
- package/src/zexus/optimization.py +424 -0
- package/src/zexus/parser/__init__.py +31 -0
- package/src/zexus/parser/__pycache__/__init__.cpython-312.pyc +0 -0
- package/src/zexus/parser/__pycache__/parser.cpython-312.pyc +0 -0
- package/src/zexus/parser/__pycache__/strategy_context.cpython-312.pyc +0 -0
- package/src/zexus/parser/__pycache__/strategy_structural.cpython-312.pyc +0 -0
- package/src/zexus/parser/integration.py +86 -0
- package/src/zexus/parser/parser.py +3977 -0
- package/src/zexus/parser/strategy_context.py +7254 -0
- package/src/zexus/parser/strategy_structural.py +1033 -0
- package/src/zexus/persistence.py +391 -0
- package/src/zexus/plugin_system.py +290 -0
- package/src/zexus/policy_engine.py +365 -0
- package/src/zexus/profiler/__init__.py +5 -0
- package/src/zexus/profiler/profiler.py +233 -0
- package/src/zexus/purity_system.py +398 -0
- package/src/zexus/runtime/__init__.py +20 -0
- package/src/zexus/runtime/async_runtime.py +324 -0
- package/src/zexus/search_old_imports.sh +65 -0
- package/src/zexus/security.py +1407 -0
- package/src/zexus/stack_trace.py +233 -0
- package/src/zexus/stdlib/__init__.py +27 -0
- package/src/zexus/stdlib/blockchain.py +341 -0
- package/src/zexus/stdlib/compression.py +167 -0
- package/src/zexus/stdlib/crypto.py +124 -0
- package/src/zexus/stdlib/datetime.py +163 -0
- package/src/zexus/stdlib/db_mongo.py +199 -0
- package/src/zexus/stdlib/db_mysql.py +162 -0
- package/src/zexus/stdlib/db_postgres.py +163 -0
- package/src/zexus/stdlib/db_sqlite.py +133 -0
- package/src/zexus/stdlib/encoding.py +230 -0
- package/src/zexus/stdlib/fs.py +195 -0
- package/src/zexus/stdlib/http.py +219 -0
- package/src/zexus/stdlib/http_server.py +248 -0
- package/src/zexus/stdlib/json_module.py +61 -0
- package/src/zexus/stdlib/math.py +360 -0
- package/src/zexus/stdlib/os_module.py +265 -0
- package/src/zexus/stdlib/regex.py +148 -0
- package/src/zexus/stdlib/sockets.py +253 -0
- package/src/zexus/stdlib/test_framework.zx +208 -0
- package/src/zexus/stdlib/test_runner.zx +119 -0
- package/src/zexus/stdlib_integration.py +341 -0
- package/src/zexus/strategy_recovery.py +256 -0
- package/src/zexus/syntax_validator.py +356 -0
- package/src/zexus/testing/zpics.py +407 -0
- package/src/zexus/testing/zpics_runtime.py +369 -0
- package/src/zexus/type_system.py +374 -0
- package/src/zexus/validation_system.py +569 -0
- package/src/zexus/virtual_filesystem.py +355 -0
- package/src/zexus/vm/__init__.py +8 -0
- package/src/zexus/vm/__pycache__/__init__.cpython-312.pyc +0 -0
- package/src/zexus/vm/__pycache__/async_optimizer.cpython-312.pyc +0 -0
- package/src/zexus/vm/__pycache__/bytecode.cpython-312.pyc +0 -0
- package/src/zexus/vm/__pycache__/cache.cpython-312.pyc +0 -0
- package/src/zexus/vm/__pycache__/jit.cpython-312.pyc +0 -0
- package/src/zexus/vm/__pycache__/memory_manager.cpython-312.pyc +0 -0
- package/src/zexus/vm/__pycache__/memory_pool.cpython-312.pyc +0 -0
- package/src/zexus/vm/__pycache__/optimizer.cpython-312.pyc +0 -0
- package/src/zexus/vm/__pycache__/parallel_vm.cpython-312.pyc +0 -0
- package/src/zexus/vm/__pycache__/peephole_optimizer.cpython-312.pyc +0 -0
- package/src/zexus/vm/__pycache__/profiler.cpython-312.pyc +0 -0
- package/src/zexus/vm/__pycache__/register_allocator.cpython-312.pyc +0 -0
- package/src/zexus/vm/__pycache__/register_vm.cpython-312.pyc +0 -0
- package/src/zexus/vm/__pycache__/ssa_converter.cpython-312.pyc +0 -0
- package/src/zexus/vm/__pycache__/vm.cpython-312.pyc +0 -0
- package/src/zexus/vm/async_optimizer.py +420 -0
- package/src/zexus/vm/bytecode.py +428 -0
- package/src/zexus/vm/bytecode_converter.py +297 -0
- package/src/zexus/vm/cache.py +532 -0
- package/src/zexus/vm/jit.py +720 -0
- package/src/zexus/vm/memory_manager.py +520 -0
- package/src/zexus/vm/memory_pool.py +511 -0
- package/src/zexus/vm/optimizer.py +478 -0
- package/src/zexus/vm/parallel_vm.py +899 -0
- package/src/zexus/vm/peephole_optimizer.py +452 -0
- package/src/zexus/vm/profiler.py +527 -0
- package/src/zexus/vm/register_allocator.py +462 -0
- package/src/zexus/vm/register_vm.py +520 -0
- package/src/zexus/vm/ssa_converter.py +757 -0
- package/src/zexus/vm/vm.py +1392 -0
- package/src/zexus/zexus_ast.py +1782 -0
- package/src/zexus/zexus_token.py +253 -0
- package/src/zexus/zpm/__init__.py +15 -0
- package/src/zexus/zpm/installer.py +116 -0
- package/src/zexus/zpm/package_manager.py +208 -0
- package/src/zexus/zpm/publisher.py +98 -0
- package/src/zexus/zpm/registry.py +110 -0
- package/src/zexus.egg-info/PKG-INFO +2235 -0
- package/src/zexus.egg-info/SOURCES.txt +876 -0
- package/src/zexus.egg-info/dependency_links.txt +1 -0
- package/src/zexus.egg-info/entry_points.txt +3 -0
- package/src/zexus.egg-info/not-zip-safe +1 -0
- package/src/zexus.egg-info/requires.txt +14 -0
- package/src/zexus.egg-info/top_level.txt +2 -0
- package/zexus.json +14 -0
|
@@ -0,0 +1,284 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Builtin Module System for Zexus
|
|
3
|
+
|
|
4
|
+
This module provides a registry of builtin modules (crypto, datetime, math)
|
|
5
|
+
that can be imported using `use "module_name" as alias` syntax.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from .object import Map, String, Integer, Float, Boolean, Builtin, Environment, EvaluationError
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
def create_builtin_modules(evaluator):
|
|
12
|
+
"""
|
|
13
|
+
Create and return a dictionary of builtin modules.
|
|
14
|
+
Each module is an Environment with its functions registered.
|
|
15
|
+
|
|
16
|
+
Args:
|
|
17
|
+
evaluator: The evaluator instance (needed for accessing CryptoPlugin, etc.)
|
|
18
|
+
|
|
19
|
+
Returns:
|
|
20
|
+
Dict mapping module names to their Environment objects
|
|
21
|
+
"""
|
|
22
|
+
modules = {}
|
|
23
|
+
|
|
24
|
+
# ===== CRYPTO MODULE =====
|
|
25
|
+
crypto_env = Environment()
|
|
26
|
+
|
|
27
|
+
# Import CryptoPlugin
|
|
28
|
+
try:
|
|
29
|
+
from .blockchain.crypto import CryptoPlugin
|
|
30
|
+
|
|
31
|
+
# keccak256(data)
|
|
32
|
+
def _crypto_keccak256(*args):
|
|
33
|
+
if len(args) != 1:
|
|
34
|
+
return EvaluationError("keccak256() expects 1 argument: data")
|
|
35
|
+
data = args[0].value if hasattr(args[0], 'value') else str(args[0])
|
|
36
|
+
try:
|
|
37
|
+
result = CryptoPlugin.keccak256(data)
|
|
38
|
+
return String(result)
|
|
39
|
+
except Exception as e:
|
|
40
|
+
return EvaluationError(f"Keccak256 error: {str(e)}")
|
|
41
|
+
|
|
42
|
+
# generate_keypair(algorithm?)
|
|
43
|
+
def _crypto_generate_keypair(*args):
|
|
44
|
+
algorithm = args[0].value if len(args) > 0 and hasattr(args[0], 'value') else 'ECDSA'
|
|
45
|
+
try:
|
|
46
|
+
private_key, public_key = CryptoPlugin.generate_keypair(algorithm)
|
|
47
|
+
# Return as a Map with public_key, private_key, and address
|
|
48
|
+
address = CryptoPlugin.derive_address(public_key)
|
|
49
|
+
return Map({
|
|
50
|
+
String("private_key"): String(private_key),
|
|
51
|
+
String("public_key"): String(public_key),
|
|
52
|
+
String("address"): String(address)
|
|
53
|
+
})
|
|
54
|
+
except Exception as e:
|
|
55
|
+
return EvaluationError(f"Keypair generation error: {str(e)}")
|
|
56
|
+
|
|
57
|
+
# secp256k1_sign(data, private_key)
|
|
58
|
+
def _crypto_secp256k1_sign(*args):
|
|
59
|
+
if len(args) != 2:
|
|
60
|
+
return EvaluationError("secp256k1_sign() expects 2 arguments: data, private_key")
|
|
61
|
+
data = args[0].value if hasattr(args[0], 'value') else str(args[0])
|
|
62
|
+
private_key = args[1].value if hasattr(args[1], 'value') else str(args[1])
|
|
63
|
+
try:
|
|
64
|
+
result = CryptoPlugin.sign_data(data, private_key, 'ECDSA')
|
|
65
|
+
return String(result)
|
|
66
|
+
except Exception as e:
|
|
67
|
+
return EvaluationError(f"Signature error: {str(e)}")
|
|
68
|
+
|
|
69
|
+
# verify_signature(data, signature, public_key)
|
|
70
|
+
def _crypto_verify_signature(*args):
|
|
71
|
+
if len(args) != 3:
|
|
72
|
+
return EvaluationError("verify_signature() expects 3 arguments: data, signature, public_key")
|
|
73
|
+
data = args[0].value if hasattr(args[0], 'value') else str(args[0])
|
|
74
|
+
signature = args[1].value if hasattr(args[1], 'value') else str(args[1])
|
|
75
|
+
public_key = args[2].value if hasattr(args[2], 'value') else str(args[2])
|
|
76
|
+
try:
|
|
77
|
+
result = CryptoPlugin.verify_signature(data, signature, public_key, 'ECDSA')
|
|
78
|
+
return Boolean(result)
|
|
79
|
+
except Exception as e:
|
|
80
|
+
return EvaluationError(f"Verification error: {str(e)}")
|
|
81
|
+
|
|
82
|
+
# calculate_merkle_root(hashes)
|
|
83
|
+
def _crypto_calculate_merkle_root(*args):
|
|
84
|
+
if len(args) != 1:
|
|
85
|
+
return EvaluationError("calculate_merkle_root() expects 1 argument: list of hashes")
|
|
86
|
+
|
|
87
|
+
from .object import List as ListObj
|
|
88
|
+
if not isinstance(args[0], ListObj):
|
|
89
|
+
return EvaluationError("calculate_merkle_root() expects a list")
|
|
90
|
+
|
|
91
|
+
hashes = [h.value if hasattr(h, 'value') else str(h) for h in args[0].elements]
|
|
92
|
+
|
|
93
|
+
if len(hashes) == 0:
|
|
94
|
+
return String(CryptoPlugin.keccak256(""))
|
|
95
|
+
|
|
96
|
+
# Simple merkle root calculation
|
|
97
|
+
while len(hashes) > 1:
|
|
98
|
+
new_level = []
|
|
99
|
+
for i in range(0, len(hashes), 2):
|
|
100
|
+
if i + 1 < len(hashes):
|
|
101
|
+
combined = hashes[i] + hashes[i + 1]
|
|
102
|
+
else:
|
|
103
|
+
combined = hashes[i] + hashes[i]
|
|
104
|
+
new_level.append(CryptoPlugin.keccak256(combined))
|
|
105
|
+
hashes = new_level
|
|
106
|
+
|
|
107
|
+
return String(hashes[0])
|
|
108
|
+
|
|
109
|
+
# sha256(data)
|
|
110
|
+
def _crypto_sha256(*args):
|
|
111
|
+
if len(args) != 1:
|
|
112
|
+
return EvaluationError("sha256() expects 1 argument: data")
|
|
113
|
+
data = args[0].value if hasattr(args[0], 'value') else str(args[0])
|
|
114
|
+
try:
|
|
115
|
+
result = CryptoPlugin.hash_data(data, 'SHA256')
|
|
116
|
+
return String(result)
|
|
117
|
+
except Exception as e:
|
|
118
|
+
return EvaluationError(f"SHA256 error: {str(e)}")
|
|
119
|
+
|
|
120
|
+
# aes_encrypt(data, key)
|
|
121
|
+
def _crypto_aes_encrypt(*args):
|
|
122
|
+
if len(args) != 2:
|
|
123
|
+
return EvaluationError("aes_encrypt() expects 2 arguments: data, key")
|
|
124
|
+
# Simplified AES - would need proper implementation
|
|
125
|
+
data = args[0].value if hasattr(args[0], 'value') else str(args[0])
|
|
126
|
+
key = args[1].value if hasattr(args[1], 'value') else str(args[1])
|
|
127
|
+
# For now, return a mock encrypted value (TODO: implement proper AES)
|
|
128
|
+
import base64
|
|
129
|
+
encoded = base64.b64encode(data.encode()).decode()
|
|
130
|
+
return String(f"aes_encrypted:{encoded}")
|
|
131
|
+
|
|
132
|
+
# aes_decrypt(encrypted_data, key)
|
|
133
|
+
def _crypto_aes_decrypt(*args):
|
|
134
|
+
if len(args) != 2:
|
|
135
|
+
return EvaluationError("aes_decrypt() expects 2 arguments: encrypted_data, key")
|
|
136
|
+
# Simplified AES - would need proper implementation
|
|
137
|
+
encrypted = args[0].value if hasattr(args[0], 'value') else str(args[0])
|
|
138
|
+
key = args[1].value if hasattr(args[1], 'value') else str(args[1])
|
|
139
|
+
# For now, decode the mock encryption (TODO: implement proper AES)
|
|
140
|
+
import base64
|
|
141
|
+
if encrypted.startswith("aes_encrypted:"):
|
|
142
|
+
encoded = encrypted.split(":", 1)[1]
|
|
143
|
+
decoded = base64.b64decode(encoded).decode()
|
|
144
|
+
return String(decoded)
|
|
145
|
+
return String(encrypted)
|
|
146
|
+
|
|
147
|
+
# Register all crypto functions
|
|
148
|
+
crypto_env.set("keccak256", Builtin(_crypto_keccak256, "keccak256"))
|
|
149
|
+
crypto_env.set("generate_keypair", Builtin(_crypto_generate_keypair, "generate_keypair"))
|
|
150
|
+
crypto_env.set("secp256k1_sign", Builtin(_crypto_secp256k1_sign, "secp256k1_sign"))
|
|
151
|
+
crypto_env.set("verify_signature", Builtin(_crypto_verify_signature, "verify_signature"))
|
|
152
|
+
crypto_env.set("calculate_merkle_root", Builtin(_crypto_calculate_merkle_root, "calculate_merkle_root"))
|
|
153
|
+
crypto_env.set("sha256", Builtin(_crypto_sha256, "sha256"))
|
|
154
|
+
crypto_env.set("aes_encrypt", Builtin(_crypto_aes_encrypt, "aes_encrypt"))
|
|
155
|
+
crypto_env.set("aes_decrypt", Builtin(_crypto_aes_decrypt, "aes_decrypt"))
|
|
156
|
+
|
|
157
|
+
except ImportError as e:
|
|
158
|
+
# Crypto module not available
|
|
159
|
+
pass
|
|
160
|
+
|
|
161
|
+
modules["crypto"] = crypto_env
|
|
162
|
+
|
|
163
|
+
# ===== DATETIME MODULE =====
|
|
164
|
+
datetime_env = Environment()
|
|
165
|
+
|
|
166
|
+
import time
|
|
167
|
+
import datetime as dt
|
|
168
|
+
|
|
169
|
+
# now() - returns datetime object-like Map
|
|
170
|
+
def _datetime_now(*args):
|
|
171
|
+
now = dt.datetime.now()
|
|
172
|
+
|
|
173
|
+
# timestamp() method
|
|
174
|
+
def _timestamp(*a):
|
|
175
|
+
return Integer(int(now.timestamp()))
|
|
176
|
+
|
|
177
|
+
return Map({
|
|
178
|
+
String("year"): Integer(now.year),
|
|
179
|
+
String("month"): Integer(now.month),
|
|
180
|
+
String("day"): Integer(now.day),
|
|
181
|
+
String("hour"): Integer(now.hour),
|
|
182
|
+
String("minute"): Integer(now.minute),
|
|
183
|
+
String("second"): Integer(now.second),
|
|
184
|
+
String("timestamp"): Builtin(_timestamp, "timestamp")
|
|
185
|
+
})
|
|
186
|
+
|
|
187
|
+
# timestamp() - returns current unix timestamp
|
|
188
|
+
def _datetime_timestamp(*args):
|
|
189
|
+
return Integer(int(time.time()))
|
|
190
|
+
|
|
191
|
+
datetime_env.set("now", Builtin(_datetime_now, "now"))
|
|
192
|
+
datetime_env.set("timestamp", Builtin(_datetime_timestamp, "timestamp"))
|
|
193
|
+
|
|
194
|
+
modules["datetime"] = datetime_env
|
|
195
|
+
|
|
196
|
+
# ===== MATH MODULE =====
|
|
197
|
+
math_env = Environment()
|
|
198
|
+
|
|
199
|
+
import math
|
|
200
|
+
import random
|
|
201
|
+
|
|
202
|
+
# random_int(min, max)
|
|
203
|
+
def _math_random_int(*args):
|
|
204
|
+
if len(args) != 2:
|
|
205
|
+
return EvaluationError("random_int() expects 2 arguments: min, max")
|
|
206
|
+
min_val = args[0].value if hasattr(args[0], 'value') else int(args[0])
|
|
207
|
+
max_val = args[1].value if hasattr(args[1], 'value') else int(args[1])
|
|
208
|
+
return Integer(random.randint(min_val, max_val))
|
|
209
|
+
|
|
210
|
+
# random() - returns float between 0 and 1
|
|
211
|
+
def _math_random(*args):
|
|
212
|
+
return Float(random.random())
|
|
213
|
+
|
|
214
|
+
# min(a, b)
|
|
215
|
+
def _math_min(*args):
|
|
216
|
+
if len(args) != 2:
|
|
217
|
+
return EvaluationError("min() expects 2 arguments")
|
|
218
|
+
a = args[0].value if hasattr(args[0], 'value') else args[0]
|
|
219
|
+
b = args[1].value if hasattr(args[1], 'value') else args[1]
|
|
220
|
+
result = min(a, b)
|
|
221
|
+
return Float(result) if isinstance(result, float) else Integer(result)
|
|
222
|
+
|
|
223
|
+
# max(a, b)
|
|
224
|
+
def _math_max(*args):
|
|
225
|
+
if len(args) != 2:
|
|
226
|
+
return EvaluationError("max() expects 2 arguments")
|
|
227
|
+
a = args[0].value if hasattr(args[0], 'value') else args[0]
|
|
228
|
+
b = args[1].value if hasattr(args[1], 'value') else args[1]
|
|
229
|
+
result = max(a, b)
|
|
230
|
+
return Float(result) if isinstance(result, float) else Integer(result)
|
|
231
|
+
|
|
232
|
+
# sqrt(n)
|
|
233
|
+
def _math_sqrt(*args):
|
|
234
|
+
if len(args) != 1:
|
|
235
|
+
return EvaluationError("sqrt() expects 1 argument")
|
|
236
|
+
n = args[0].value if hasattr(args[0], 'value') else args[0]
|
|
237
|
+
return Float(math.sqrt(n))
|
|
238
|
+
|
|
239
|
+
# abs(n)
|
|
240
|
+
def _math_abs(*args):
|
|
241
|
+
if len(args) != 1:
|
|
242
|
+
return EvaluationError("abs() expects 1 argument")
|
|
243
|
+
n = args[0].value if hasattr(args[0], 'value') else args[0]
|
|
244
|
+
result = abs(n)
|
|
245
|
+
return Float(result) if isinstance(result, float) else Integer(result)
|
|
246
|
+
|
|
247
|
+
math_env.set("random_int", Builtin(_math_random_int, "random_int"))
|
|
248
|
+
math_env.set("random", Builtin(_math_random, "random"))
|
|
249
|
+
math_env.set("min", Builtin(_math_min, "min"))
|
|
250
|
+
math_env.set("max", Builtin(_math_max, "max"))
|
|
251
|
+
math_env.set("sqrt", Builtin(_math_sqrt, "sqrt"))
|
|
252
|
+
math_env.set("abs", Builtin(_math_abs, "abs"))
|
|
253
|
+
|
|
254
|
+
modules["math"] = math_env
|
|
255
|
+
|
|
256
|
+
return modules
|
|
257
|
+
|
|
258
|
+
|
|
259
|
+
# Global registry of builtin modules
|
|
260
|
+
_BUILTIN_MODULES = {}
|
|
261
|
+
|
|
262
|
+
def get_builtin_module(module_name, evaluator=None):
|
|
263
|
+
"""
|
|
264
|
+
Get a builtin module by name.
|
|
265
|
+
|
|
266
|
+
Args:
|
|
267
|
+
module_name: Name of the module ('crypto', 'datetime', 'math')
|
|
268
|
+
evaluator: Optional evaluator instance for context
|
|
269
|
+
|
|
270
|
+
Returns:
|
|
271
|
+
Environment object with module functions, or None if not found
|
|
272
|
+
"""
|
|
273
|
+
global _BUILTIN_MODULES
|
|
274
|
+
|
|
275
|
+
# Initialize on first access
|
|
276
|
+
if not _BUILTIN_MODULES and evaluator:
|
|
277
|
+
_BUILTIN_MODULES = create_builtin_modules(evaluator)
|
|
278
|
+
|
|
279
|
+
return _BUILTIN_MODULES.get(module_name)
|
|
280
|
+
|
|
281
|
+
|
|
282
|
+
def is_builtin_module(module_name):
|
|
283
|
+
"""Check if a module name refers to a builtin module"""
|
|
284
|
+
return module_name in ["crypto", "datetime", "math"]
|
|
@@ -0,0 +1,317 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Builtin plugins for Zexus.
|
|
3
|
+
|
|
4
|
+
These are standard plugins shipped with the interpreter.
|
|
5
|
+
They provide commonly-used functionality as extensible modules.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from src.zexus.plugin_system import PluginMetadata
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
# JSON Plugin
|
|
12
|
+
JSON_PLUGIN = PluginMetadata(
|
|
13
|
+
name="json",
|
|
14
|
+
version="1.0.0",
|
|
15
|
+
author="Zexus Core Team",
|
|
16
|
+
description="JSON serialization and deserialization",
|
|
17
|
+
requires=[],
|
|
18
|
+
provides=["json.parse", "json.stringify"],
|
|
19
|
+
hooks=[],
|
|
20
|
+
config={}
|
|
21
|
+
)
|
|
22
|
+
|
|
23
|
+
JSON_PLUGIN_CODE = """
|
|
24
|
+
@plugin {
|
|
25
|
+
name: "json",
|
|
26
|
+
version: "1.0.0",
|
|
27
|
+
provides: ["json.parse", "json.stringify"],
|
|
28
|
+
description: "JSON serialization support"
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
public action parse(input) {
|
|
32
|
+
# Validate input is string
|
|
33
|
+
if typeof(input) != "string" {
|
|
34
|
+
ret error("JSON input must be string, got " ++ typeof(input));
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
# Call builtin JSON parser
|
|
38
|
+
ret builtin_json_parse(input);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
public action stringify(obj) {
|
|
42
|
+
# Convert any value to JSON representation
|
|
43
|
+
ret builtin_json_stringify(obj);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
public action pretty(obj) {
|
|
47
|
+
# Pretty-print JSON with indentation
|
|
48
|
+
ret builtin_json_stringify(obj, { indent: 2 });
|
|
49
|
+
}
|
|
50
|
+
"""
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
# Logging Plugin
|
|
54
|
+
LOGGING_PLUGIN = PluginMetadata(
|
|
55
|
+
name="logging",
|
|
56
|
+
version="2.0.0",
|
|
57
|
+
author="Zexus Core Team",
|
|
58
|
+
description="Structured logging with configurable output",
|
|
59
|
+
requires=[],
|
|
60
|
+
provides=["logging.debug", "logging.info", "logging.warn", "logging.error"],
|
|
61
|
+
hooks=[],
|
|
62
|
+
config={
|
|
63
|
+
"level": {"type": "string", "default": "info"},
|
|
64
|
+
"format": {"type": "string", "default": "text"},
|
|
65
|
+
"output": {"type": "string", "default": "stderr"}
|
|
66
|
+
}
|
|
67
|
+
)
|
|
68
|
+
|
|
69
|
+
LOGGING_PLUGIN_CODE = """
|
|
70
|
+
@plugin {
|
|
71
|
+
name: "logging",
|
|
72
|
+
version: "2.0.0",
|
|
73
|
+
provides: ["logging.debug", "logging.info", "logging.warn", "logging.error"],
|
|
74
|
+
config: {
|
|
75
|
+
level: { type: "string", default: "info" },
|
|
76
|
+
format: { type: "string", default: "text" },
|
|
77
|
+
output: { type: "string", default: "stderr" }
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
let LEVELS = { debug: 0, info: 1, warn: 2, error: 3 };
|
|
82
|
+
|
|
83
|
+
action should_log(level) {
|
|
84
|
+
let min_level = LEVELS[plugin.config.level] || 1;
|
|
85
|
+
ret LEVELS[level] >= min_level;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
action format_message(level, msg) {
|
|
89
|
+
let ts = builtin_now();
|
|
90
|
+
cond {
|
|
91
|
+
plugin.config.format == "json" ? {
|
|
92
|
+
ret builtin_json_stringify({
|
|
93
|
+
level: level,
|
|
94
|
+
message: msg,
|
|
95
|
+
timestamp: ts
|
|
96
|
+
});
|
|
97
|
+
};
|
|
98
|
+
else ? {
|
|
99
|
+
ret "[" ++ level ++ "] " ++ msg ++ " (" ++ ts ++ ")";
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
public action debug(msg) {
|
|
105
|
+
if should_log("debug") {
|
|
106
|
+
let formatted = format_message("debug", msg);
|
|
107
|
+
builtin_output(formatted, plugin.config.output);
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
public action info(msg) {
|
|
112
|
+
if should_log("info") {
|
|
113
|
+
let formatted = format_message("info", msg);
|
|
114
|
+
builtin_output(formatted, plugin.config.output);
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
public action warn(msg) {
|
|
119
|
+
if should_log("warn") {
|
|
120
|
+
let formatted = format_message("warn", msg);
|
|
121
|
+
builtin_output(formatted, plugin.config.output);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
public action error(msg) {
|
|
126
|
+
if should_log("error") {
|
|
127
|
+
let formatted = format_message("error", msg);
|
|
128
|
+
builtin_output(formatted, plugin.config.output);
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
"""
|
|
132
|
+
|
|
133
|
+
|
|
134
|
+
# Crypto Plugin
|
|
135
|
+
CRYPTO_PLUGIN = PluginMetadata(
|
|
136
|
+
name="crypto",
|
|
137
|
+
version="1.0.0",
|
|
138
|
+
author="Zexus Core Team",
|
|
139
|
+
description="Cryptographic functions (hash, HMAC, etc.)",
|
|
140
|
+
requires=[],
|
|
141
|
+
provides=["crypto.sha256", "crypto.sha512", "crypto.hmac", "crypto.random"],
|
|
142
|
+
hooks=[],
|
|
143
|
+
config={}
|
|
144
|
+
)
|
|
145
|
+
|
|
146
|
+
CRYPTO_PLUGIN_CODE = """
|
|
147
|
+
@plugin {
|
|
148
|
+
name: "crypto",
|
|
149
|
+
version: "1.0.0",
|
|
150
|
+
provides: ["crypto.sha256", "crypto.sha512", "crypto.hmac", "crypto.random"]
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
public action sha256(input) {
|
|
154
|
+
# Compute SHA256 hash of input
|
|
155
|
+
ret builtin_crypto_hash("sha256", input);
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
public action sha512(input) {
|
|
159
|
+
# Compute SHA512 hash of input
|
|
160
|
+
ret builtin_crypto_hash("sha512", input);
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
public action hmac(key, msg, algorithm) {
|
|
164
|
+
# Compute HMAC
|
|
165
|
+
ret builtin_crypto_hmac(algorithm || "sha256", key, msg);
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
public action random(len) {
|
|
169
|
+
# Generate cryptographically secure random bytes
|
|
170
|
+
ret builtin_crypto_random(len || 32);
|
|
171
|
+
}
|
|
172
|
+
"""
|
|
173
|
+
|
|
174
|
+
|
|
175
|
+
# Validation Plugin
|
|
176
|
+
VALIDATION_PLUGIN = PluginMetadata(
|
|
177
|
+
name="validation",
|
|
178
|
+
version="1.0.0",
|
|
179
|
+
author="Zexus Core Team",
|
|
180
|
+
description="Data validation utilities",
|
|
181
|
+
requires=[],
|
|
182
|
+
provides=["validation.email", "validation.url", "validation.phone"],
|
|
183
|
+
hooks=["type_validator"],
|
|
184
|
+
config={}
|
|
185
|
+
)
|
|
186
|
+
|
|
187
|
+
VALIDATION_PLUGIN_CODE = """
|
|
188
|
+
@plugin {
|
|
189
|
+
name: "validation",
|
|
190
|
+
version: "1.0.0",
|
|
191
|
+
provides: ["validation.email", "validation.url", "validation.phone"],
|
|
192
|
+
hooks: ["type_validator"]
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
action register_type_validators() {
|
|
196
|
+
plugin.register_hook("type_validator", action(value, spec) {
|
|
197
|
+
cond {
|
|
198
|
+
spec.type == "email" ? validate_email(value);
|
|
199
|
+
spec.type == "url" ? validate_url(value);
|
|
200
|
+
spec.type == "phone" ? validate_phone(value);
|
|
201
|
+
else ? true;
|
|
202
|
+
}
|
|
203
|
+
});
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
public action validate_email(addr) {
|
|
207
|
+
# Simple email validation regex
|
|
208
|
+
ret typeof(addr) == "string" &&
|
|
209
|
+
addr ~matches /^[^\\s@]+@[^\\s@]+\\.[^\\s@]+$/;
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
public action validate_url(url) {
|
|
213
|
+
# Simple URL validation
|
|
214
|
+
ret typeof(url) == "string" &&
|
|
215
|
+
(url ~matches /^https?:\\/\\// || url ~matches /^www\\./);
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
public action validate_phone(phone) {
|
|
219
|
+
# Simple phone validation (US format)
|
|
220
|
+
ret typeof(phone) == "string" &&
|
|
221
|
+
phone ~matches /^\\+?1?\\d{9,15}$/;
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
# Register validators on plugin load
|
|
225
|
+
register_type_validators();
|
|
226
|
+
"""
|
|
227
|
+
|
|
228
|
+
|
|
229
|
+
# Collections Plugin
|
|
230
|
+
COLLECTIONS_PLUGIN = PluginMetadata(
|
|
231
|
+
name="collections",
|
|
232
|
+
version="1.0.0",
|
|
233
|
+
author="Zexus Core Team",
|
|
234
|
+
description="Advanced collection utilities (map, filter, reduce, etc.)",
|
|
235
|
+
requires=[],
|
|
236
|
+
provides=["collections.map", "collections.filter", "collections.reduce",
|
|
237
|
+
"collections.zip", "collections.group"],
|
|
238
|
+
hooks=[],
|
|
239
|
+
config={}
|
|
240
|
+
)
|
|
241
|
+
|
|
242
|
+
COLLECTIONS_PLUGIN_CODE = """
|
|
243
|
+
@plugin {
|
|
244
|
+
name: "collections",
|
|
245
|
+
version: "1.0.0",
|
|
246
|
+
provides: ["collections.map", "collections.filter", "collections.reduce",
|
|
247
|
+
"collections.zip", "collections.group"]
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
public action map(arr, fn) {
|
|
251
|
+
# Apply function to each array element
|
|
252
|
+
let result = [];
|
|
253
|
+
each item in arr {
|
|
254
|
+
result = result ++ [fn(item)];
|
|
255
|
+
}
|
|
256
|
+
ret result;
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
public action filter(arr, predicate) {
|
|
260
|
+
# Keep elements that satisfy predicate
|
|
261
|
+
let result = [];
|
|
262
|
+
each item in arr {
|
|
263
|
+
if predicate(item) {
|
|
264
|
+
result = result ++ [item];
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
ret result;
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
public action reduce(arr, fn, initial) {
|
|
271
|
+
# Fold array elements
|
|
272
|
+
let acc = initial;
|
|
273
|
+
each item in arr {
|
|
274
|
+
acc = fn(acc, item);
|
|
275
|
+
}
|
|
276
|
+
ret acc;
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
public action zip(arr1, arr2) {
|
|
280
|
+
# Combine two arrays
|
|
281
|
+
let result = [];
|
|
282
|
+
let len = min(len(arr1), len(arr2));
|
|
283
|
+
let i = 0;
|
|
284
|
+
while i < len {
|
|
285
|
+
result = result ++ [[arr1[i], arr2[i]]];
|
|
286
|
+
i = i + 1;
|
|
287
|
+
}
|
|
288
|
+
ret result;
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
public action group(arr, key_fn) {
|
|
292
|
+
# Group elements by key function
|
|
293
|
+
let groups = {};
|
|
294
|
+
each item in arr {
|
|
295
|
+
let key = key_fn(item);
|
|
296
|
+
if !groups[key] {
|
|
297
|
+
groups[key] = [];
|
|
298
|
+
}
|
|
299
|
+
groups[key] = groups[key] ++ [item];
|
|
300
|
+
}
|
|
301
|
+
ret groups;
|
|
302
|
+
}
|
|
303
|
+
"""
|
|
304
|
+
|
|
305
|
+
|
|
306
|
+
BUILTIN_PLUGINS = {
|
|
307
|
+
"json": (JSON_PLUGIN, JSON_PLUGIN_CODE),
|
|
308
|
+
"logging": (LOGGING_PLUGIN, LOGGING_PLUGIN_CODE),
|
|
309
|
+
"crypto": (CRYPTO_PLUGIN, CRYPTO_PLUGIN_CODE),
|
|
310
|
+
"validation": (VALIDATION_PLUGIN, VALIDATION_PLUGIN_CODE),
|
|
311
|
+
"collections": (COLLECTIONS_PLUGIN, COLLECTIONS_PLUGIN_CODE),
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
|
|
315
|
+
def get_builtin_plugin(name: str):
|
|
316
|
+
"""Get a builtin plugin by name."""
|
|
317
|
+
return BUILTIN_PLUGINS.get(name)
|