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
package/src/README.md
ADDED
|
@@ -0,0 +1,1525 @@
|
|
|
1
|
+
# Zexus — Developer Guide (Interpreter & Compiler)
|
|
2
|
+
|
|
3
|
+
This document describes the source layout, responsibilities of each module under `src/zexus`, the language syntax accepted by the system (both interpreter and compiler), examples, built-ins, and developer workflows for testing and extending the project.
|
|
4
|
+
|
|
5
|
+
Use this as the canonical reference for contributors and maintainers.
|
|
6
|
+
|
|
7
|
+
---
|
|
8
|
+
## Quick commands
|
|
9
|
+
## Security Best Practices for Zexus
|
|
10
|
+
|
|
11
|
+
### The Defense-in-Depth Security Model
|
|
12
|
+
|
|
13
|
+
Zexus provides multiple layers of security that work together:
|
|
14
|
+
|
|
15
|
+
```
|
|
16
|
+
┌─────────────────────────────────────┐
|
|
17
|
+
│ 1. Export Access Control (File) │ - WHO can access? (export + allowed_files)
|
|
18
|
+
├─────────────────────────────────────┤
|
|
19
|
+
│ 2. Verify Security Checks (Runtime) │ - Should they be allowed? (verify + conditions)
|
|
20
|
+
├─────────────────────────────────────┤
|
|
21
|
+
│ 3. Protect Guardrails (Enforcement) │ - Enforce rules (rate limit, auth, HTTPS, etc.)
|
|
22
|
+
├─────────────────────────────────────┤
|
|
23
|
+
│ 4. Middleware Validation (Request) │ - Process & validate requests (auth, logging, CORS)
|
|
24
|
+
├─────────────────────────────────────┤
|
|
25
|
+
│ 5. Throttle & Cache (Performance) │ - Prevent abuse, optimize access
|
|
26
|
+
└─────────────────────────────────────┘
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
### Example: Secure Payment System
|
|
30
|
+
|
|
31
|
+
```zexus
|
|
32
|
+
// Step 1: Define entities with type safety
|
|
33
|
+
entity PaymentRequest {
|
|
34
|
+
from: Address,
|
|
35
|
+
to: Address,
|
|
36
|
+
amount: integer,
|
|
37
|
+
timestamp: integer
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
// Step 2: Create contract with persistent state
|
|
41
|
+
contract PaymentProcessor {
|
|
42
|
+
persistent storage transactions: List<PaymentRequest>
|
|
43
|
+
persistent storage daily_limits: Map<Address, integer>
|
|
44
|
+
persistent storage balances: Map<Address, integer>
|
|
45
|
+
|
|
46
|
+
action process_payment(from: Address, to: Address, amount: integer) -> boolean {
|
|
47
|
+
require(balances[from] >= amount, "Insufficient funds")
|
|
48
|
+
require(daily_limits[from] + amount <= 10000, "Daily limit exceeded")
|
|
49
|
+
|
|
50
|
+
balances[from] = balances[from] - amount
|
|
51
|
+
balances[to] = balances.get(to, 0) + amount
|
|
52
|
+
daily_limits[from] = daily_limits.get(from, 0) + amount
|
|
53
|
+
|
|
54
|
+
transactions.push({from: from, to: to, amount: amount, timestamp: now()})
|
|
55
|
+
return true
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
// Step 3: Export with file restrictions
|
|
60
|
+
let processor = contract PaymentProcessor()
|
|
61
|
+
export processor to "payment_service.zx" with "execute"
|
|
62
|
+
|
|
63
|
+
// Step 4: Add runtime verification
|
|
64
|
+
verify(processor.process_payment, [
|
|
65
|
+
check_authenticated(),
|
|
66
|
+
check_user_kyc(), // Know Your Customer
|
|
67
|
+
check_not_suspended(),
|
|
68
|
+
check_transaction_limit()
|
|
69
|
+
])
|
|
70
|
+
|
|
71
|
+
// Step 5: Protect against abuse
|
|
72
|
+
protect(processor.process_payment, {
|
|
73
|
+
rate_limit: 100, // Max 100 txns per minute
|
|
74
|
+
auth_required: true,
|
|
75
|
+
require_https: true,
|
|
76
|
+
min_password_strength: "very_strong",
|
|
77
|
+
session_timeout: 1800, // 30 min session
|
|
78
|
+
blocked_ips: ["10.0.0.100"], // Block known threat
|
|
79
|
+
allowed_ips: ["10.0.0.0/8"] // Allow corporate only
|
|
80
|
+
}, "strict")
|
|
81
|
+
|
|
82
|
+
// Step 6: Add middleware
|
|
83
|
+
middleware(fraud_detection, action(request, response) {
|
|
84
|
+
if (request.amount > 50000) {
|
|
85
|
+
// Flag for review
|
|
86
|
+
notify_fraud_team(request)
|
|
87
|
+
}
|
|
88
|
+
return true
|
|
89
|
+
})
|
|
90
|
+
|
|
91
|
+
middleware(audit_logging, action(request, response) {
|
|
92
|
+
log_transaction({
|
|
93
|
+
user: request.user,
|
|
94
|
+
action: "payment",
|
|
95
|
+
amount: request.amount,
|
|
96
|
+
timestamp: now(),
|
|
97
|
+
ip: request.client_ip
|
|
98
|
+
})
|
|
99
|
+
return true
|
|
100
|
+
})
|
|
101
|
+
|
|
102
|
+
// Step 7: Configure authentication
|
|
103
|
+
auth {
|
|
104
|
+
provider: "oauth2",
|
|
105
|
+
scopes: ["payments:process"],
|
|
106
|
+
token_expiry: 3600,
|
|
107
|
+
mfa_required: true,
|
|
108
|
+
session_timeout: 1800
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
// Step 8: Apply throttling
|
|
112
|
+
throttle(processor.process_payment, {
|
|
113
|
+
requests_per_minute: 100,
|
|
114
|
+
requests_per_hour: 5000,
|
|
115
|
+
per_user: true
|
|
116
|
+
})
|
|
117
|
+
|
|
118
|
+
// Step 9: Cache common lookups
|
|
119
|
+
cache(get_exchange_rate, {
|
|
120
|
+
ttl: 300, // 5 minutes - rates update frequently
|
|
121
|
+
invalidate_on: ["market_update"]
|
|
122
|
+
})
|
|
123
|
+
|
|
124
|
+
// Result: Secure, performant, auditable payment system!
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
### Security Checklist
|
|
128
|
+
|
|
129
|
+
When building sensitive applications:
|
|
130
|
+
|
|
131
|
+
- ✅ Define data types with `entity` for type safety
|
|
132
|
+
- ✅ Use `contract` for persistent state management
|
|
133
|
+
- ✅ Restrict access with `export` (file-level)
|
|
134
|
+
- ✅ Add checks with `verify` (runtime conditions)
|
|
135
|
+
- ✅ Enforce rules with `protect` (guardrails)
|
|
136
|
+
- ✅ Validate requests with `middleware`
|
|
137
|
+
- ✅ Configure `auth` for authentication
|
|
138
|
+
- ✅ Limit abuse with `throttle`
|
|
139
|
+
- ✅ Optimize access with `cache`
|
|
140
|
+
- ✅ Log everything for auditing
|
|
141
|
+
- ✅ Use HTTPS for all external communications
|
|
142
|
+
- ✅ Implement MFA for sensitive operations
|
|
143
|
+
- ✅ Rotate secrets regularly
|
|
144
|
+
- ✅ Review and test security rules
|
|
145
|
+
|
|
146
|
+
---
|
|
147
|
+
|
|
148
|
+
- Run quick integration tests:
|
|
149
|
+
- `python3 scripts/verify_integration.py`
|
|
150
|
+
- Run compiler investigation script:
|
|
151
|
+
- `python3 investigate_compiler.py`
|
|
152
|
+
- Run unit tests (if present):
|
|
153
|
+
- `pytest tests/`
|
|
154
|
+
- Start interactive experimentation (REPL not included by default):
|
|
155
|
+
- Use the interpreter pipeline in python REPL by importing modules.
|
|
156
|
+
|
|
157
|
+
---
|
|
158
|
+
|
|
159
|
+
## High-level architecture
|
|
160
|
+
|
|
161
|
+
- **Lexer**: tokenizes source text into a token stream.
|
|
162
|
+
- **Parser(s)**: Convert tokens into AST nodes.
|
|
163
|
+
- Interpreter parser (tolerant): robust parsing that recovers from mixed syntax styles and common mistakes.
|
|
164
|
+
- Compiler parser (production): cleaner AST for semantic checks and bytecode generation; made tolerant for common surface differences.
|
|
165
|
+
- **Structural/context strategies**: helpers used by the tolerant interpreter parser to split token streams into blocks and map blocks to AST.
|
|
166
|
+
- **AST**: two parallel AST definitions:
|
|
167
|
+
- Interpreter AST (`src/zexus/zexus_ast.py`) — richer node set used by the evaluator.
|
|
168
|
+
- Compiler AST (`src/zexus/compiler/zexus_ast.py`) — cleaner nodes optimized for semantic analysis and code generation.
|
|
169
|
+
- **Evaluator**: Walks the interpreter AST and produces runtime objects; includes builtins and error handling.
|
|
170
|
+
- **Object model**: Runtime `Object` classes (Integer, String, List, Map, Environment, Builtin, Action, etc.).
|
|
171
|
+
- **Compiler front-end**: Production lexer/parser, semantic analyzer, bytecode generator.
|
|
172
|
+
- **Renderer System**: Advanced UI/graphics system with screens, components, themes, and canvas drawing.
|
|
173
|
+
- **Virtual Machine**: **NEW** - Advanced stack-based VM with async/await, events, modules, and closure support.
|
|
174
|
+
- **Utilities**: syntax validator, recovery engine, config flags, developer scripts.
|
|
175
|
+
|
|
176
|
+
---
|
|
177
|
+
|
|
178
|
+
## File map (src/zexus) — what each file does
|
|
179
|
+
|
|
180
|
+
### Core Language
|
|
181
|
+
- `zexus_token.py`
|
|
182
|
+
- Defines token constants and Token class.
|
|
183
|
+
- **NEW**: Async/events tokens (`ASYNC`, `AWAIT`, `EVENT`, `EMIT`, `ENUM`, `PROTOCOL`, `IMPORT`)
|
|
184
|
+
|
|
185
|
+
- `lexer.py`
|
|
186
|
+
- Implements lexical analysis (character scanning → tokens).
|
|
187
|
+
|
|
188
|
+
- `zexus_ast.py` (interpreter)
|
|
189
|
+
- Interpreter AST node classes.
|
|
190
|
+
- **NEW**: `AsyncActionStatement`, `AwaitExpression`, `EventStatement`, `EmitStatement`, `EnumStatement`, `ProtocolStatement`, `ImportStatement`
|
|
191
|
+
|
|
192
|
+
- `evaluator.py`
|
|
193
|
+
- Evaluator walks interpreter AST and returns runtime objects.
|
|
194
|
+
- **NEW**: Async/event builtins and runtime support.
|
|
195
|
+
|
|
196
|
+
### Compiler System
|
|
197
|
+
- `compiler/__init__.py`
|
|
198
|
+
- Exposes `ZexusCompiler`, re-exports interpreter `builtins` as `BUILTINS`.
|
|
199
|
+
|
|
200
|
+
- `compiler/parser.py` (ProductionParser)
|
|
201
|
+
- **NEW**: Supports async actions, await expressions, events, enums, protocols, imports.
|
|
202
|
+
|
|
203
|
+
- `compiler/zexus_ast.py` (compiler)
|
|
204
|
+
- **NEW**: Async/events AST nodes for compiler pipeline.
|
|
205
|
+
|
|
206
|
+
- `compiler/semantic.py`
|
|
207
|
+
- **NEW**: Semantic checks for async usage, event signatures, protocol conformance.
|
|
208
|
+
|
|
209
|
+
- `compiler/bytecode.py`
|
|
210
|
+
- **NEW**: Bytecode generation for async/await, events, modules, closures.
|
|
211
|
+
|
|
212
|
+
### Virtual Machine (NEW - Major Update)
|
|
213
|
+
- `vm/vm.py`
|
|
214
|
+
- **COMPLETELY REWRITTEN**: Advanced stack-based VM with:
|
|
215
|
+
- **Low-level ops**: `LOAD_CONST`, `LOAD`, `STORE`, `CALL`, `JUMP`, `RETURN`
|
|
216
|
+
- **Async primitives**: `SPAWN`, `AWAIT` - real coroutine support
|
|
217
|
+
- **Event system**: `REGISTER_EVENT`, `EMIT_EVENT` - reactive programming
|
|
218
|
+
- **Module system**: `IMPORT` - Python module integration
|
|
219
|
+
- **Type system**: `DEFINE_ENUM`, `ASSERT_PROTOCOL` - advanced types
|
|
220
|
+
- **Function calls**: `CALL_NAME`, `CALL_FUNC_CONST`, `CALL_TOP` - multiple calling conventions
|
|
221
|
+
- **Closure support**: `STORE_FUNC` with lexical closure capture
|
|
222
|
+
|
|
223
|
+
- `vm/bytecode.py`
|
|
224
|
+
- **NEW**: Complete bytecode instruction set and VM operations.
|
|
225
|
+
|
|
226
|
+
### Renderer System
|
|
227
|
+
- `renderer/` - Advanced UI/Graphics System
|
|
228
|
+
- `backend.py` - Unified backend for interpreter and VM
|
|
229
|
+
- `color_system.py` - Color mixing, themes, gradients
|
|
230
|
+
- `layout.py` - Screen components and inheritance
|
|
231
|
+
- `painter.py` - Terminal graphics with styling
|
|
232
|
+
- `canvas.py` - Drawing primitives
|
|
233
|
+
- `graphics.py` - Clocks, animations, visualizations
|
|
234
|
+
|
|
235
|
+
---
|
|
236
|
+
|
|
237
|
+
## Language syntax overview
|
|
238
|
+
|
|
239
|
+
### NEW: Advanced Language Features
|
|
240
|
+
|
|
241
|
+
#### Async/Await System
|
|
242
|
+
```zexus
|
|
243
|
+
// Real async/await with proper concurrency
|
|
244
|
+
action async broadcast_transaction(tx: Transaction) {
|
|
245
|
+
let peers = get_connected_peers()
|
|
246
|
+
for each p in peers {
|
|
247
|
+
let response = await p.send(tx) // 🚀 Real async I/O
|
|
248
|
+
if response.success {
|
|
249
|
+
print("Propagated to " + p.address)
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
action async mine_block() {
|
|
255
|
+
let block = await create_new_block()
|
|
256
|
+
let result = await broadcast_block(block)
|
|
257
|
+
return result
|
|
258
|
+
}
|
|
259
|
+
```
|
|
260
|
+
|
|
261
|
+
Event System
|
|
262
|
+
|
|
263
|
+
```zexus
|
|
264
|
+
// Reactive event-driven programming
|
|
265
|
+
event TransactionMined {
|
|
266
|
+
hash: string,
|
|
267
|
+
block_number: integer,
|
|
268
|
+
from: Address,
|
|
269
|
+
to: Address
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
// Register event handlers
|
|
273
|
+
register_event("tx_mined", action(tx) {
|
|
274
|
+
update_wallet_balance(tx.from, -tx.amount)
|
|
275
|
+
update_wallet_balance(tx.to, tx.amount)
|
|
276
|
+
notify_subscribers(tx)
|
|
277
|
+
})
|
|
278
|
+
|
|
279
|
+
// Emit events
|
|
280
|
+
action confirm_transaction(tx: Transaction) {
|
|
281
|
+
// ... validation logic
|
|
282
|
+
emit TransactionMined {
|
|
283
|
+
hash: tx.hash,
|
|
284
|
+
block_number: current_block,
|
|
285
|
+
from: tx.from,
|
|
286
|
+
to: tx.to
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
```
|
|
290
|
+
|
|
291
|
+
Module System
|
|
292
|
+
|
|
293
|
+
```zexus
|
|
294
|
+
// Import external modules
|
|
295
|
+
use "crypto" as crypto
|
|
296
|
+
use "network" as p2p
|
|
297
|
+
use "blockchain" as chain
|
|
298
|
+
|
|
299
|
+
action create_wallet() {
|
|
300
|
+
let keypair = crypto.generate_keypair()
|
|
301
|
+
let address = crypto.derive_address(keypair.public_key)
|
|
302
|
+
return Wallet { keypair: keypair, address: address }
|
|
303
|
+
}
|
|
304
|
+
```
|
|
305
|
+
|
|
306
|
+
Enums & Protocols
|
|
307
|
+
|
|
308
|
+
```zexus
|
|
309
|
+
// Advanced type system
|
|
310
|
+
enum ChainType {
|
|
311
|
+
ZIVER,
|
|
312
|
+
ETHEREUM,
|
|
313
|
+
BSC,
|
|
314
|
+
TON,
|
|
315
|
+
POLYGON
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
protocol Wallet {
|
|
319
|
+
action transfer(to: Address, amount: integer) -> boolean
|
|
320
|
+
action get_balance() -> integer
|
|
321
|
+
action get_address() -> Address
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
// Protocol implementation
|
|
325
|
+
contract MyWallet implements Wallet {
|
|
326
|
+
action transfer(to: Address, amount: integer) -> boolean {
|
|
327
|
+
// implementation
|
|
328
|
+
return true
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
action get_balance() -> integer {
|
|
332
|
+
return this.balance
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
action get_address() -> Address {
|
|
336
|
+
return this.address
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
```
|
|
340
|
+
|
|
341
|
+
Closure System
|
|
342
|
+
|
|
343
|
+
```zexus
|
|
344
|
+
// Proper lexical closures
|
|
345
|
+
action create_counter() {
|
|
346
|
+
let count = 0
|
|
347
|
+
|
|
348
|
+
action increment() {
|
|
349
|
+
count = count + 1
|
|
350
|
+
return count
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
action get_count() {
|
|
354
|
+
return count
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
return [increment, get_count]
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
// Usage - maintains proper closure semantics
|
|
361
|
+
let counter_ops = create_counter()
|
|
362
|
+
let increment = counter_ops[0]
|
|
363
|
+
let get_count = counter_ops[1]
|
|
364
|
+
|
|
365
|
+
print(increment()) // 1
|
|
366
|
+
print(increment()) // 2
|
|
367
|
+
print(get_count()) // 2 - Correct closure behavior!
|
|
368
|
+
```
|
|
369
|
+
|
|
370
|
+
Existing Features (Enhanced)
|
|
371
|
+
|
|
372
|
+
Renderer System
|
|
373
|
+
|
|
374
|
+
```zexus
|
|
375
|
+
// Now works in both interpreter and compiled modes
|
|
376
|
+
Screen blockchain_dashboard {
|
|
377
|
+
height: 30,
|
|
378
|
+
width: 100,
|
|
379
|
+
theme: "dark_theme"
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
Component mining_visualization {
|
|
383
|
+
type: "mining_viz",
|
|
384
|
+
x: 10,
|
|
385
|
+
y: 5,
|
|
386
|
+
width: 80,
|
|
387
|
+
height: 20
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
action async update_dashboard() {
|
|
391
|
+
define_screen("blockchain_dashboard")
|
|
392
|
+
define_component("mining_visualization")
|
|
393
|
+
add_to_screen("blockchain_dashboard", "mining_visualization")
|
|
394
|
+
|
|
395
|
+
while true {
|
|
396
|
+
let output = render_screen("blockchain_dashboard")
|
|
397
|
+
print(output)
|
|
398
|
+
await sleep(1) // Async rendering loop
|
|
399
|
+
}
|
|
400
|
+
}
|
|
401
|
+
```
|
|
402
|
+
|
|
403
|
+
Smart Contract Ready
|
|
404
|
+
|
|
405
|
+
```zexus
|
|
406
|
+
contract Token {
|
|
407
|
+
persistent storage balances: Map<Address, integer>
|
|
408
|
+
persistent storage total_supply: integer
|
|
409
|
+
|
|
410
|
+
action transfer(to: Address, amount: integer) -> boolean {
|
|
411
|
+
let sender = msg.sender
|
|
412
|
+
let sender_balance = balances.get(sender, 0)
|
|
413
|
+
|
|
414
|
+
require(sender_balance >= amount, "Insufficient balance")
|
|
415
|
+
require(amount > 0, "Amount must be positive")
|
|
416
|
+
|
|
417
|
+
balances[sender] = sender_balance - amount
|
|
418
|
+
balances[to] = balances.get(to, 0) + amount
|
|
419
|
+
|
|
420
|
+
emit Transfer { from: sender, to: to, amount: amount }
|
|
421
|
+
return true
|
|
422
|
+
}
|
|
423
|
+
}
|
|
424
|
+
```
|
|
425
|
+
|
|
426
|
+
|
|
427
|
+
## Keywords Reference
|
|
428
|
+
|
|
429
|
+
### Core Language Keywords
|
|
430
|
+
|
|
431
|
+
#### `let` - Variable Declaration
|
|
432
|
+
**Purpose**: Declare and initialize variables
|
|
433
|
+
```zexus
|
|
434
|
+
let name = "Zexus" // String variable
|
|
435
|
+
let count = 42 // Integer
|
|
436
|
+
let price = 99.99 // Float
|
|
437
|
+
let active = true // Boolean
|
|
438
|
+
let numbers = [1, 2, 3] // List
|
|
439
|
+
let user = {name: "John"} // Map/Object
|
|
440
|
+
```
|
|
441
|
+
|
|
442
|
+
action - Function Definition
|
|
443
|
+
|
|
444
|
+
Purpose: Define reusable code blocks (functions)
|
|
445
|
+
|
|
446
|
+
```zexus
|
|
447
|
+
// Basic function
|
|
448
|
+
action greet(name) {
|
|
449
|
+
return "Hello " + name
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
// With return type
|
|
453
|
+
action add(a: integer, b: integer) -> integer {
|
|
454
|
+
return a + b
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
// Function expression
|
|
458
|
+
let multiply = action(x, y) { return x * y }
|
|
459
|
+
```
|
|
460
|
+
|
|
461
|
+
async action - Asynchronous Functions
|
|
462
|
+
|
|
463
|
+
Purpose: Define functions that can perform async operations
|
|
464
|
+
|
|
465
|
+
```zexus
|
|
466
|
+
action async fetch_data(url) {
|
|
467
|
+
let response = await http_get(url)
|
|
468
|
+
return parse_json(response)
|
|
469
|
+
}
|
|
470
|
+
|
|
471
|
+
action async process_transaction(tx) {
|
|
472
|
+
let receipt = await send_transaction(tx)
|
|
473
|
+
await wait_for_confirmation(receipt)
|
|
474
|
+
return receipt
|
|
475
|
+
}
|
|
476
|
+
```
|
|
477
|
+
|
|
478
|
+
return - Function Return
|
|
479
|
+
|
|
480
|
+
Purpose: Return values from functions
|
|
481
|
+
|
|
482
|
+
```zexus
|
|
483
|
+
action calculate(x) {
|
|
484
|
+
if x > 10 {
|
|
485
|
+
return x * 2
|
|
486
|
+
}
|
|
487
|
+
return x + 1
|
|
488
|
+
}
|
|
489
|
+
```
|
|
490
|
+
|
|
491
|
+
if/else - Conditional Logic
|
|
492
|
+
|
|
493
|
+
Purpose: Control program flow based on conditions
|
|
494
|
+
|
|
495
|
+
```zexus
|
|
496
|
+
if temperature > 30 {
|
|
497
|
+
print("It's hot!")
|
|
498
|
+
} else if temperature < 10 {
|
|
499
|
+
print("It's cold!")
|
|
500
|
+
} else {
|
|
501
|
+
print("Nice weather!")
|
|
502
|
+
}
|
|
503
|
+
```
|
|
504
|
+
|
|
505
|
+
for each/in - Loop Iteration
|
|
506
|
+
|
|
507
|
+
Purpose: Iterate over collections
|
|
508
|
+
|
|
509
|
+
```zexus
|
|
510
|
+
let numbers = [1, 2, 3, 4, 5]
|
|
511
|
+
|
|
512
|
+
// Iterate list
|
|
513
|
+
for each num in numbers {
|
|
514
|
+
print(num * 2)
|
|
515
|
+
}
|
|
516
|
+
|
|
517
|
+
// Iterate map
|
|
518
|
+
let user = {name: "John", age: 30}
|
|
519
|
+
for each key in user {
|
|
520
|
+
print(key + ": " + string(user[key]))
|
|
521
|
+
}
|
|
522
|
+
```
|
|
523
|
+
|
|
524
|
+
while - Conditional Looping
|
|
525
|
+
|
|
526
|
+
Purpose: Repeat while condition is true
|
|
527
|
+
|
|
528
|
+
```zexus
|
|
529
|
+
let count = 0
|
|
530
|
+
while count < 5 {
|
|
531
|
+
print("Count: " + string(count))
|
|
532
|
+
count = count + 1
|
|
533
|
+
}
|
|
534
|
+
```
|
|
535
|
+
|
|
536
|
+
try/catch - Error Handling
|
|
537
|
+
|
|
538
|
+
Purpose: Handle runtime errors gracefully
|
|
539
|
+
|
|
540
|
+
```zexus
|
|
541
|
+
try {
|
|
542
|
+
let result = risky_operation()
|
|
543
|
+
print("Success: " + string(result))
|
|
544
|
+
} catch(error) {
|
|
545
|
+
print("Error occurred: " + string(error))
|
|
546
|
+
// Handle error or recover
|
|
547
|
+
}
|
|
548
|
+
```
|
|
549
|
+
|
|
550
|
+
Type System Keywords
|
|
551
|
+
|
|
552
|
+
enum - Enumerated Types
|
|
553
|
+
|
|
554
|
+
Purpose: Define a set of named constants
|
|
555
|
+
|
|
556
|
+
```zexus
|
|
557
|
+
enum Status {
|
|
558
|
+
PENDING,
|
|
559
|
+
PROCESSING,
|
|
560
|
+
COMPLETED,
|
|
561
|
+
FAILED
|
|
562
|
+
}
|
|
563
|
+
|
|
564
|
+
enum ChainType {
|
|
565
|
+
ZIVER,
|
|
566
|
+
ETHEREUM,
|
|
567
|
+
BSC,
|
|
568
|
+
TON
|
|
569
|
+
}
|
|
570
|
+
|
|
571
|
+
// Usage
|
|
572
|
+
let tx_status = Status.PENDING
|
|
573
|
+
let chain = ChainType.ETHEREUM
|
|
574
|
+
```
|
|
575
|
+
|
|
576
|
+
protocol - Interface Definitions
|
|
577
|
+
|
|
578
|
+
Purpose: Define method contracts that types must implement
|
|
579
|
+
|
|
580
|
+
```zexus
|
|
581
|
+
protocol Wallet {
|
|
582
|
+
action transfer(to: Address, amount: integer) -> boolean
|
|
583
|
+
action get_balance() -> integer
|
|
584
|
+
action get_address() -> Address
|
|
585
|
+
}
|
|
586
|
+
|
|
587
|
+
protocol Storage {
|
|
588
|
+
action get(key: string) -> any
|
|
589
|
+
action set(key: string, value: any) -> boolean
|
|
590
|
+
action delete(key: string) -> boolean
|
|
591
|
+
}
|
|
592
|
+
```
|
|
593
|
+
|
|
594
|
+
contract - Smart Contracts
|
|
595
|
+
|
|
596
|
+
Purpose: Define blockchain smart contracts with persistent state
|
|
597
|
+
|
|
598
|
+
```zexus
|
|
599
|
+
contract Token {
|
|
600
|
+
persistent storage balances: Map<Address, integer>
|
|
601
|
+
persistent storage owner: Address
|
|
602
|
+
|
|
603
|
+
action transfer(to: Address, amount: integer) -> boolean {
|
|
604
|
+
require(balances[msg.sender] >= amount, "Insufficient balance")
|
|
605
|
+
balances[msg.sender] = balances[msg.sender] - amount
|
|
606
|
+
balances[to] = balances.get(to, 0) + amount
|
|
607
|
+
return true
|
|
608
|
+
}
|
|
609
|
+
}
|
|
610
|
+
```
|
|
611
|
+
|
|
612
|
+
Event System Keywords
|
|
613
|
+
|
|
614
|
+
event - Event Definitions
|
|
615
|
+
|
|
616
|
+
Purpose: Define event structures for reactive programming
|
|
617
|
+
|
|
618
|
+
```zexus
|
|
619
|
+
event UserRegistered {
|
|
620
|
+
user_id: string,
|
|
621
|
+
timestamp: integer,
|
|
622
|
+
plan: string
|
|
623
|
+
}
|
|
624
|
+
|
|
625
|
+
event TransactionCompleted {
|
|
626
|
+
tx_hash: string,
|
|
627
|
+
from: Address,
|
|
628
|
+
to: Address,
|
|
629
|
+
amount: integer,
|
|
630
|
+
block: integer
|
|
631
|
+
}
|
|
632
|
+
```
|
|
633
|
+
|
|
634
|
+
emit - Event Emission
|
|
635
|
+
|
|
636
|
+
Purpose: Trigger events with data
|
|
637
|
+
|
|
638
|
+
```zexus
|
|
639
|
+
action register_user(user_data) {
|
|
640
|
+
// ... registration logic
|
|
641
|
+
emit UserRegistered {
|
|
642
|
+
user_id: user_data.id,
|
|
643
|
+
timestamp: datetime_now().timestamp(),
|
|
644
|
+
plan: user_data.plan
|
|
645
|
+
}
|
|
646
|
+
}
|
|
647
|
+
```
|
|
648
|
+
|
|
649
|
+
register_event - Event Handlers
|
|
650
|
+
|
|
651
|
+
Purpose: Register functions to handle events
|
|
652
|
+
|
|
653
|
+
```zexus
|
|
654
|
+
register_event("user_registered", action(event) {
|
|
655
|
+
print("New user: " + event.user_id)
|
|
656
|
+
send_welcome_email(event.user_id)
|
|
657
|
+
})
|
|
658
|
+
|
|
659
|
+
register_event("tx_completed", action(event) {
|
|
660
|
+
update_balances(event.from, event.to, event.amount)
|
|
661
|
+
notify_parties(event)
|
|
662
|
+
})
|
|
663
|
+
```
|
|
664
|
+
|
|
665
|
+
Module System Keywords
|
|
666
|
+
|
|
667
|
+
use - Module Imports
|
|
668
|
+
|
|
669
|
+
Purpose: Import external modules and libraries
|
|
670
|
+
|
|
671
|
+
```zexus
|
|
672
|
+
use "crypto" as crypto // Cryptography functions
|
|
673
|
+
use "network" as net // Networking utilities
|
|
674
|
+
use "blockchain" as chain // Blockchain operations
|
|
675
|
+
use "math" as math // Math functions
|
|
676
|
+
|
|
677
|
+
// Usage
|
|
678
|
+
let hash = crypto.sha256("data")
|
|
679
|
+
let peers = net.get_peers()
|
|
680
|
+
let block = chain.get_latest_block()
|
|
681
|
+
```
|
|
682
|
+
|
|
683
|
+
external - External Function Declarations
|
|
684
|
+
|
|
685
|
+
Purpose: Declare functions implemented outside Zexus
|
|
686
|
+
|
|
687
|
+
```zexus
|
|
688
|
+
external action sha256(data: string) -> string from "crypto"
|
|
689
|
+
external action verify_signature from "security"
|
|
690
|
+
external action random_bytes(count: integer) -> list from "crypto"
|
|
691
|
+
```
|
|
692
|
+
|
|
693
|
+
Concurrency Keywords
|
|
694
|
+
|
|
695
|
+
await - Asynchronous Waiting
|
|
696
|
+
|
|
697
|
+
Purpose: Wait for async operations to complete
|
|
698
|
+
|
|
699
|
+
```zexus
|
|
700
|
+
action async process_data() {
|
|
701
|
+
let data = await fetch_from_api() // Wait for HTTP
|
|
702
|
+
let processed = await process_large_data(data) // Wait for computation
|
|
703
|
+
let stored = await save_to_database(processed) // Wait for I/O
|
|
704
|
+
return stored
|
|
705
|
+
}
|
|
706
|
+
```
|
|
707
|
+
|
|
708
|
+
spawn - Concurrent Task Creation
|
|
709
|
+
|
|
710
|
+
Purpose: Launch concurrent operations (when used with builtins)
|
|
711
|
+
|
|
712
|
+
```zexus
|
|
713
|
+
action async process_multiple_files() {
|
|
714
|
+
let files = ["file1.txt", "file2.txt", "file3.txt"]
|
|
715
|
+
|
|
716
|
+
// Process files concurrently
|
|
717
|
+
let tasks = []
|
|
718
|
+
for each file in files {
|
|
719
|
+
let task = spawn process_file(file) // Start concurrent task
|
|
720
|
+
tasks.push(task)
|
|
721
|
+
}
|
|
722
|
+
|
|
723
|
+
// Wait for all to complete
|
|
724
|
+
for each task in tasks {
|
|
725
|
+
let result = await task
|
|
726
|
+
print("Processed: " + string(result))
|
|
727
|
+
}
|
|
728
|
+
}
|
|
729
|
+
```
|
|
730
|
+
|
|
731
|
+
Renderer System Keywords
|
|
732
|
+
|
|
733
|
+
Screen - UI Screen Definitions
|
|
734
|
+
|
|
735
|
+
Purpose: Define application screens/windows
|
|
736
|
+
|
|
737
|
+
```zexus
|
|
738
|
+
Screen login_screen {
|
|
739
|
+
height: 20,
|
|
740
|
+
width: 60,
|
|
741
|
+
title: "User Login",
|
|
742
|
+
theme: "dark_theme",
|
|
743
|
+
border: true
|
|
744
|
+
}
|
|
745
|
+
|
|
746
|
+
Screen dashboard {
|
|
747
|
+
height: 25,
|
|
748
|
+
width: 80,
|
|
749
|
+
title: "Blockchain Dashboard",
|
|
750
|
+
background: "gradient(blue, purple)"
|
|
751
|
+
}
|
|
752
|
+
```
|
|
753
|
+
|
|
754
|
+
Component - Reusable UI Components
|
|
755
|
+
|
|
756
|
+
Purpose: Define reusable interface elements
|
|
757
|
+
|
|
758
|
+
```zexus
|
|
759
|
+
Component primary_button {
|
|
760
|
+
type: "button",
|
|
761
|
+
text: "Click Me",
|
|
762
|
+
color: "blue",
|
|
763
|
+
width: 15,
|
|
764
|
+
height: 3,
|
|
765
|
+
border: "rounded"
|
|
766
|
+
}
|
|
767
|
+
|
|
768
|
+
Component text_input {
|
|
769
|
+
type: "textbox",
|
|
770
|
+
placeholder: "Enter text...",
|
|
771
|
+
width: 30,
|
|
772
|
+
height: 3,
|
|
773
|
+
background: "white"
|
|
774
|
+
}
|
|
775
|
+
```
|
|
776
|
+
|
|
777
|
+
Theme - Color Theme Definitions
|
|
778
|
+
|
|
779
|
+
Purpose: Define consistent color schemes
|
|
780
|
+
|
|
781
|
+
```zexus
|
|
782
|
+
Theme dark_theme {
|
|
783
|
+
primary: "navy_blue",
|
|
784
|
+
accent: "electric_blue",
|
|
785
|
+
background: "dark_gray",
|
|
786
|
+
text: "white",
|
|
787
|
+
success: "green",
|
|
788
|
+
warning: "orange",
|
|
789
|
+
error: "red"
|
|
790
|
+
}
|
|
791
|
+
|
|
792
|
+
Theme light_theme {
|
|
793
|
+
primary: "sky_blue",
|
|
794
|
+
accent: "royal_blue",
|
|
795
|
+
background: "white",
|
|
796
|
+
text: "black",
|
|
797
|
+
success: "forest_green",
|
|
798
|
+
warning: "gold",
|
|
799
|
+
error: "crimson"
|
|
800
|
+
}
|
|
801
|
+
```
|
|
802
|
+
|
|
803
|
+
Blockchain-Specific Keywords
|
|
804
|
+
|
|
805
|
+
persistent storage - Contract State
|
|
806
|
+
|
|
807
|
+
Purpose: Declare persistent storage in smart contracts
|
|
808
|
+
|
|
809
|
+
```zexus
|
|
810
|
+
contract Bank {
|
|
811
|
+
persistent storage balances: Map<Address, integer>
|
|
812
|
+
persistent storage total_deposits: integer
|
|
813
|
+
persistent storage owner: Address
|
|
814
|
+
persistent storage interest_rate: float
|
|
815
|
+
}
|
|
816
|
+
```
|
|
817
|
+
|
|
818
|
+
require - Contract Conditions
|
|
819
|
+
|
|
820
|
+
Purpose: Enforce conditions in smart contracts
|
|
821
|
+
|
|
822
|
+
```zexus
|
|
823
|
+
action withdraw(amount: integer) {
|
|
824
|
+
require(amount > 0, "Amount must be positive")
|
|
825
|
+
require(balances[msg.sender] >= amount, "Insufficient funds")
|
|
826
|
+
require(contract_active, "Contract is paused")
|
|
827
|
+
|
|
828
|
+
balances[msg.sender] = balances[msg.sender] - amount
|
|
829
|
+
return true
|
|
830
|
+
}
|
|
831
|
+
```
|
|
832
|
+
|
|
833
|
+
Utility Keywords
|
|
834
|
+
|
|
835
|
+
print - Output to Console
|
|
836
|
+
|
|
837
|
+
Purpose: Display values to the console
|
|
838
|
+
|
|
839
|
+
```zexus
|
|
840
|
+
print("Hello World") // String
|
|
841
|
+
print(42) // Number
|
|
842
|
+
print([1, 2, 3]) // List
|
|
843
|
+
print({name: "John", age: 30}) // Map
|
|
844
|
+
print("Value: " + string(some_value)) // Concatenation
|
|
845
|
+
```
|
|
846
|
+
|
|
847
|
+
debug - Debugging Output
|
|
848
|
+
|
|
849
|
+
Purpose: Debugging and development output
|
|
850
|
+
|
|
851
|
+
```zexus
|
|
852
|
+
debug "Starting process..." // Simple debug message
|
|
853
|
+
debug "User data:", user_data // Debug with data
|
|
854
|
+
debug_trace("Function entry") // Stack tracing
|
|
855
|
+
```
|
|
856
|
+
|
|
857
|
+
export - Module Exports
|
|
858
|
+
|
|
859
|
+
Purpose: Make functions/variables available to other modules
|
|
860
|
+
|
|
861
|
+
```zexus
|
|
862
|
+
export action public_function() { // Export function
|
|
863
|
+
return "accessible from other modules"
|
|
864
|
+
}
|
|
865
|
+
|
|
866
|
+
export let API_KEY = "12345" // Export variable
|
|
867
|
+
|
|
868
|
+
action private_helper() { // Not exported (private)
|
|
869
|
+
return "internal use only"
|
|
870
|
+
}
|
|
871
|
+
```
|
|
872
|
+
|
|
873
|
+
## Advanced Security & Entity Features
|
|
874
|
+
|
|
875
|
+
### entity - Object-Oriented Data Structures
|
|
876
|
+
|
|
877
|
+
Purpose: Define typed entities with properties and inheritance from `let`
|
|
878
|
+
|
|
879
|
+
**What entity does:**
|
|
880
|
+
- Creates reusable data structures similar to classes/structs
|
|
881
|
+
- Supports typed properties with optional default values
|
|
882
|
+
- Enables inheritance through composition
|
|
883
|
+
- Type-checks property assignments
|
|
884
|
+
|
|
885
|
+
```zexus
|
|
886
|
+
// Define an entity
|
|
887
|
+
entity User {
|
|
888
|
+
name: string,
|
|
889
|
+
email: string,
|
|
890
|
+
age: integer = 18,
|
|
891
|
+
role: string = "user"
|
|
892
|
+
}
|
|
893
|
+
|
|
894
|
+
// Create an instance
|
|
895
|
+
let user = User{ name: "Alice", email: "alice@example.com" }
|
|
896
|
+
|
|
897
|
+
// Access properties
|
|
898
|
+
print(user.name) // "Alice"
|
|
899
|
+
print(user.age) // 18 (uses default)
|
|
900
|
+
|
|
901
|
+
// Update properties
|
|
902
|
+
user.role = "admin"
|
|
903
|
+
|
|
904
|
+
// Inheritance/composition
|
|
905
|
+
entity Admin extends User {
|
|
906
|
+
permissions: list,
|
|
907
|
+
department: string
|
|
908
|
+
}
|
|
909
|
+
```
|
|
910
|
+
|
|
911
|
+
### verify - Security Verification Checks
|
|
912
|
+
|
|
913
|
+
Purpose: Wrap functions with security verification checks for maximum security
|
|
914
|
+
|
|
915
|
+
**What verify does:**
|
|
916
|
+
- Works alongside export for additional security layer
|
|
917
|
+
- Allows multiple verification conditions before function execution
|
|
918
|
+
- Provides custom error handling on verification failure
|
|
919
|
+
- Perfect for sensitive operations (payments, data deletion, etc.)
|
|
920
|
+
|
|
921
|
+
**How export + verify complement each other:**
|
|
922
|
+
- `export` controls **WHO** can access a function (file-level access control)
|
|
923
|
+
- `verify` controls **IF** access is allowed based on runtime conditions (runtime security checks)
|
|
924
|
+
- Together they provide **defense in depth** - two layers of security
|
|
925
|
+
|
|
926
|
+
```zexus
|
|
927
|
+
action transfer_funds(to: Address, amount: integer) -> boolean {
|
|
928
|
+
return amount > 0 && balance >= amount
|
|
929
|
+
}
|
|
930
|
+
|
|
931
|
+
// Export to specific files AND verify before execution
|
|
932
|
+
export transfer_funds to "payment_service.zx" with "read_write"
|
|
933
|
+
|
|
934
|
+
verify(transfer_funds, [
|
|
935
|
+
check_authenticated(), // User must be logged in
|
|
936
|
+
check_balance(amount), // Sufficient balance
|
|
937
|
+
check_whitelist(to), // Recipient on whitelist
|
|
938
|
+
check_rate_limit() // Not exceeded daily limit
|
|
939
|
+
])
|
|
940
|
+
|
|
941
|
+
// Usage: Can only be called if exported to correct file AND all verify checks pass
|
|
942
|
+
let result = transfer_funds("0x123...", 100)
|
|
943
|
+
```
|
|
944
|
+
|
|
945
|
+
### contract - Smart Contracts with Persistent State
|
|
946
|
+
|
|
947
|
+
Purpose: Define blockchain smart contracts with persistent storage and methods
|
|
948
|
+
|
|
949
|
+
**What contract does:**
|
|
950
|
+
- Creates contracts with persistent state variables
|
|
951
|
+
- Stores state permanently (suitable for blockchain operations)
|
|
952
|
+
- Enables complex business logic with state management
|
|
953
|
+
- Supports transaction-like operations
|
|
954
|
+
|
|
955
|
+
```zexus
|
|
956
|
+
contract Token {
|
|
957
|
+
persistent storage balances: Map<Address, integer>
|
|
958
|
+
persistent storage owner: Address
|
|
959
|
+
persistent storage total_supply: integer = 1000000
|
|
960
|
+
|
|
961
|
+
action transfer(to: Address, amount: integer) -> boolean {
|
|
962
|
+
require(balances[msg.sender] >= amount, "Insufficient balance")
|
|
963
|
+
balances[msg.sender] = balances[msg.sender] - amount
|
|
964
|
+
balances[to] = balances.get(to, 0) + amount
|
|
965
|
+
return true
|
|
966
|
+
}
|
|
967
|
+
|
|
968
|
+
action get_balance(account: Address) -> integer {
|
|
969
|
+
return balances.get(account, 0)
|
|
970
|
+
}
|
|
971
|
+
|
|
972
|
+
action mint(to: Address, amount: integer) {
|
|
973
|
+
require(msg.sender == owner, "Only owner can mint")
|
|
974
|
+
balances[to] = balances.get(to, 0) + amount
|
|
975
|
+
total_supply = total_supply + amount
|
|
976
|
+
}
|
|
977
|
+
}
|
|
978
|
+
|
|
979
|
+
// Deploy contract
|
|
980
|
+
let token = contract Token()
|
|
981
|
+
|
|
982
|
+
// Use contract methods
|
|
983
|
+
let balance = token.get_balance("0xAlice")
|
|
984
|
+
token.transfer("0xBob", 100)
|
|
985
|
+
token.mint("0xAlice", 1000)
|
|
986
|
+
```
|
|
987
|
+
|
|
988
|
+
### protect - Security Guardrails Against Unauthorized Access
|
|
989
|
+
|
|
990
|
+
Purpose: Set protection rules and enforce security guardrails against attacks
|
|
991
|
+
|
|
992
|
+
**What protect does:**
|
|
993
|
+
- Enforces rate limiting to prevent brute force attacks
|
|
994
|
+
- Requires authentication/authorization
|
|
995
|
+
- Validates HTTPS connections
|
|
996
|
+
- Enforces password strength requirements
|
|
997
|
+
- Manages session timeouts
|
|
998
|
+
- Blocks malicious IP addresses
|
|
999
|
+
- Works in three enforcement modes: strict, warn, audit
|
|
1000
|
+
|
|
1001
|
+
```zexus
|
|
1002
|
+
action login(username: string, password: string) -> boolean {
|
|
1003
|
+
// ... login logic ...
|
|
1004
|
+
return true
|
|
1005
|
+
}
|
|
1006
|
+
|
|
1007
|
+
protect(login, {
|
|
1008
|
+
rate_limit: 10, // Max 10 login attempts per minute
|
|
1009
|
+
auth_required: true, // Must authenticate
|
|
1010
|
+
require_https: true, // Only allow HTTPS
|
|
1011
|
+
min_password_strength: "strong", // Password complexity requirement
|
|
1012
|
+
session_timeout: 3600, // 1 hour session timeout
|
|
1013
|
+
allowed_ips: ["10.0.0.0/8"], // Allow private network only
|
|
1014
|
+
blocked_ips: ["192.168.1.100"], // Block specific IP
|
|
1015
|
+
require_mfa: true // Multi-factor authentication
|
|
1016
|
+
})
|
|
1017
|
+
|
|
1018
|
+
// Example with enforcement levels
|
|
1019
|
+
protect(delete_account, {
|
|
1020
|
+
auth_required: true,
|
|
1021
|
+
require_mfa: true,
|
|
1022
|
+
rate_limit: 1 // Only 1 deletion per day
|
|
1023
|
+
}, "strict") // Strictly enforce - deny on any violation
|
|
1024
|
+
|
|
1025
|
+
protect(log_access, {
|
|
1026
|
+
rate_limit: 1000
|
|
1027
|
+
}, "warn") // Warn but allow - log violations
|
|
1028
|
+
|
|
1029
|
+
protect(audit_trail, {
|
|
1030
|
+
rate_limit: 5000
|
|
1031
|
+
}, "audit") // Allow but record - for audit purposes
|
|
1032
|
+
```
|
|
1033
|
+
|
|
1034
|
+
### middleware - Request/Response Processing
|
|
1035
|
+
|
|
1036
|
+
Purpose: Register middleware handlers for request/response processing pipelines
|
|
1037
|
+
|
|
1038
|
+
```zexus
|
|
1039
|
+
middleware(authenticate, action(request, response) {
|
|
1040
|
+
let token = request.headers["Authorization"]
|
|
1041
|
+
if (!verify_token(token)) {
|
|
1042
|
+
response.status = 401
|
|
1043
|
+
response.body = "Unauthorized"
|
|
1044
|
+
return false // Stop chain
|
|
1045
|
+
}
|
|
1046
|
+
return true // Continue chain
|
|
1047
|
+
})
|
|
1048
|
+
|
|
1049
|
+
middleware(log_requests, action(request, response) {
|
|
1050
|
+
print("[LOG] " + request.method + " " + request.path)
|
|
1051
|
+
return true
|
|
1052
|
+
})
|
|
1053
|
+
|
|
1054
|
+
middleware(cors, action(request, response) {
|
|
1055
|
+
response.headers["Access-Control-Allow-Origin"] = "*"
|
|
1056
|
+
return true
|
|
1057
|
+
})
|
|
1058
|
+
```
|
|
1059
|
+
|
|
1060
|
+
### auth - Authentication Configuration
|
|
1061
|
+
|
|
1062
|
+
Purpose: Configure global authentication settings
|
|
1063
|
+
|
|
1064
|
+
```zexus
|
|
1065
|
+
auth {
|
|
1066
|
+
provider: "oauth2", // OAuth2, JWT, SAML, etc.
|
|
1067
|
+
scopes: ["read", "write", "delete"],
|
|
1068
|
+
token_expiry: 3600, // 1 hour
|
|
1069
|
+
refresh_enabled: true,
|
|
1070
|
+
mfa_required: false,
|
|
1071
|
+
session_timeout: 7200, // 2 hours
|
|
1072
|
+
password_min_length: 12,
|
|
1073
|
+
password_require_uppercase: true,
|
|
1074
|
+
password_require_symbols: true
|
|
1075
|
+
}
|
|
1076
|
+
```
|
|
1077
|
+
|
|
1078
|
+
### throttle - Rate Limiting
|
|
1079
|
+
|
|
1080
|
+
Purpose: Throttle function execution to prevent abuse
|
|
1081
|
+
|
|
1082
|
+
```zexus
|
|
1083
|
+
action api_endpoint() {
|
|
1084
|
+
return { status: "ok" }
|
|
1085
|
+
}
|
|
1086
|
+
|
|
1087
|
+
throttle(api_endpoint, {
|
|
1088
|
+
requests_per_minute: 100, // Max 100 requests per minute
|
|
1089
|
+
burst_size: 10, // Allow 10 requests instantly
|
|
1090
|
+
per_user: true // Apply limit per user, not globally
|
|
1091
|
+
})
|
|
1092
|
+
|
|
1093
|
+
// Advanced throttling
|
|
1094
|
+
throttle(expensive_operation, {
|
|
1095
|
+
requests_per_minute: 5,
|
|
1096
|
+
requests_per_hour: 50,
|
|
1097
|
+
requests_per_day: 500,
|
|
1098
|
+
per_user: true
|
|
1099
|
+
})
|
|
1100
|
+
```
|
|
1101
|
+
|
|
1102
|
+
### cache - Caching Directives
|
|
1103
|
+
|
|
1104
|
+
Purpose: Cache function results for performance optimization
|
|
1105
|
+
|
|
1106
|
+
```zexus
|
|
1107
|
+
action expensive_query(user_id: string) {
|
|
1108
|
+
// ... database query that takes 2 seconds ...
|
|
1109
|
+
return results
|
|
1110
|
+
}
|
|
1111
|
+
|
|
1112
|
+
cache(expensive_query, {
|
|
1113
|
+
ttl: 3600, // Cache for 1 hour
|
|
1114
|
+
key: "query_result", // Cache key
|
|
1115
|
+
invalidate_on: ["data_changed", "user_updated"] // Events that clear cache
|
|
1116
|
+
})
|
|
1117
|
+
|
|
1118
|
+
// First call: executes query, caches result
|
|
1119
|
+
let result1 = expensive_query("user123") // Takes 2 seconds
|
|
1120
|
+
|
|
1121
|
+
// Second call within TTL: returns cached result
|
|
1122
|
+
let result2 = expensive_query("user123") // Instant - from cache
|
|
1123
|
+
|
|
1124
|
+
// After TTL or invalidation event: re-executes query
|
|
1125
|
+
let result3 = expensive_query("user123") // Takes 2 seconds again
|
|
1126
|
+
```
|
|
1127
|
+
|
|
1128
|
+
Special Types and Values
|
|
1129
|
+
|
|
1130
|
+
Built-in Constants
|
|
1131
|
+
|
|
1132
|
+
```zexus
|
|
1133
|
+
let n = null // Null value
|
|
1134
|
+
let t = true // Boolean true
|
|
1135
|
+
let f = false // Boolean false
|
|
1136
|
+
|
|
1137
|
+
// Special values in contracts
|
|
1138
|
+
let sender = msg.sender // Transaction sender
|
|
1139
|
+
let value = msg.value // Transaction value
|
|
1140
|
+
let timestamp = block.time // Current block timestamp
|
|
1141
|
+
```
|
|
1142
|
+
|
|
1143
|
+
Type Annotations
|
|
1144
|
+
|
|
1145
|
+
```zexus
|
|
1146
|
+
let name: string = "John" // String type
|
|
1147
|
+
let age: integer = 30 // Integer type
|
|
1148
|
+
let price: float = 19.99 // Float type
|
|
1149
|
+
let active: boolean = true // Boolean type
|
|
1150
|
+
let scores: list = [95, 87, 92] // List type
|
|
1151
|
+
let user: map = {name: "John"} // Map type
|
|
1152
|
+
let addr: Address = "0x123..." // Address type
|
|
1153
|
+
```
|
|
1154
|
+
|
|
1155
|
+
Keyword Usage Patterns
|
|
1156
|
+
|
|
1157
|
+
Smart Contract Pattern
|
|
1158
|
+
|
|
1159
|
+
```zexus
|
|
1160
|
+
contract TokenContract {
|
|
1161
|
+
persistent storage balances: Map<Address, integer>
|
|
1162
|
+
persistent storage owner: Address
|
|
1163
|
+
|
|
1164
|
+
action transfer(to: Address, amount: integer) -> boolean {
|
|
1165
|
+
require(amount > 0, "Invalid amount")
|
|
1166
|
+
require(balances[msg.sender] >= amount, "Insufficient balance")
|
|
1167
|
+
|
|
1168
|
+
balances[msg.sender] = balances[msg.sender] - amount
|
|
1169
|
+
balances[to] = balances.get(to, 0) + amount
|
|
1170
|
+
|
|
1171
|
+
emit Transfer {
|
|
1172
|
+
from: msg.sender,
|
|
1173
|
+
to: to,
|
|
1174
|
+
amount: amount,
|
|
1175
|
+
timestamp: block.timestamp
|
|
1176
|
+
}
|
|
1177
|
+
|
|
1178
|
+
return true
|
|
1179
|
+
}
|
|
1180
|
+
}
|
|
1181
|
+
```
|
|
1182
|
+
|
|
1183
|
+
Async Event Handler Pattern
|
|
1184
|
+
|
|
1185
|
+
```zexus
|
|
1186
|
+
action async process_transaction(tx) {
|
|
1187
|
+
try {
|
|
1188
|
+
let receipt = await send_transaction(tx)
|
|
1189
|
+
|
|
1190
|
+
emit TransactionSent {
|
|
1191
|
+
tx_hash: receipt.hash,
|
|
1192
|
+
status: "pending"
|
|
1193
|
+
}
|
|
1194
|
+
|
|
1195
|
+
let confirmation = await wait_for_confirmation(receipt.hash)
|
|
1196
|
+
|
|
1197
|
+
emit TransactionConfirmed {
|
|
1198
|
+
tx_hash: receipt.hash,
|
|
1199
|
+
block: confirmation.block,
|
|
1200
|
+
status: "confirmed"
|
|
1201
|
+
}
|
|
1202
|
+
|
|
1203
|
+
} catch(error) {
|
|
1204
|
+
emit TransactionFailed {
|
|
1205
|
+
tx_hash: tx.hash,
|
|
1206
|
+
error: string(error)
|
|
1207
|
+
}
|
|
1208
|
+
}
|
|
1209
|
+
}
|
|
1210
|
+
```
|
|
1211
|
+
|
|
1212
|
+
UI Component Pattern
|
|
1213
|
+
|
|
1214
|
+
```zexus
|
|
1215
|
+
Theme app_theme {
|
|
1216
|
+
primary: "indigo",
|
|
1217
|
+
accent: "amber",
|
|
1218
|
+
background: "slate",
|
|
1219
|
+
text: "white"
|
|
1220
|
+
}
|
|
1221
|
+
|
|
1222
|
+
Screen main_screen {
|
|
1223
|
+
width: 100,
|
|
1224
|
+
height: 30,
|
|
1225
|
+
theme: "app_theme"
|
|
1226
|
+
}
|
|
1227
|
+
|
|
1228
|
+
Component wallet_display {
|
|
1229
|
+
type: "panel",
|
|
1230
|
+
title: "Wallet Balance",
|
|
1231
|
+
width: 40,
|
|
1232
|
+
height: 10
|
|
1233
|
+
}
|
|
1234
|
+
|
|
1235
|
+
action async update_interface() {
|
|
1236
|
+
set_theme("app_theme")
|
|
1237
|
+
define_screen("main_screen")
|
|
1238
|
+
define_component("wallet_display")
|
|
1239
|
+
add_to_screen("main_screen", "wallet_display")
|
|
1240
|
+
|
|
1241
|
+
while true {
|
|
1242
|
+
let balance = await get_wallet_balance()
|
|
1243
|
+
// Update display with current balance
|
|
1244
|
+
let output = render_screen("main_screen")
|
|
1245
|
+
print(output)
|
|
1246
|
+
await sleep(2) // Update every 2 seconds
|
|
1247
|
+
}
|
|
1248
|
+
}
|
|
1249
|
+
|
|
1250
|
+
```
|
|
1251
|
+
|
|
1252
|
+
---
|
|
1253
|
+
|
|
1254
|
+
Virtual Machine Architecture (NEW)
|
|
1255
|
+
|
|
1256
|
+
Stack-Based Execution
|
|
1257
|
+
|
|
1258
|
+
```
|
|
1259
|
+
[VM Stack Machine]
|
|
1260
|
+
├── Value Stack: [val1, val2, val3, ...]
|
|
1261
|
+
├── Call Stack: [frame1, frame2, ...]
|
|
1262
|
+
├── Environment: {vars, closures, builtins}
|
|
1263
|
+
└── Event Registry: {event_name: [handlers]}
|
|
1264
|
+
```
|
|
1265
|
+
|
|
1266
|
+
Bytecode Operations
|
|
1267
|
+
|
|
1268
|
+
```python
|
|
1269
|
+
# Low-level ops (stack machine)
|
|
1270
|
+
LOAD_CONST 0 # Push constant
|
|
1271
|
+
STORE "x" # Pop and store variable
|
|
1272
|
+
LOAD "x" # Push variable value
|
|
1273
|
+
CALL_NAME "print" # Call function
|
|
1274
|
+
SPAWN # Create async task
|
|
1275
|
+
AWAIT # Await coroutine
|
|
1276
|
+
JUMP 15 # Jump to instruction
|
|
1277
|
+
```
|
|
1278
|
+
|
|
1279
|
+
Closure Implementation
|
|
1280
|
+
|
|
1281
|
+
```python
|
|
1282
|
+
# Closure capture uses Cell objects
|
|
1283
|
+
def STORE_FUNC(name, func_descriptor):
|
|
1284
|
+
# Capture current environment as closure cells
|
|
1285
|
+
closure = {k: Cell(v) for k, v in current_env.items()}
|
|
1286
|
+
func_descriptor.closure = closure
|
|
1287
|
+
current_env[name] = func_descriptor
|
|
1288
|
+
```
|
|
1289
|
+
|
|
1290
|
+
---
|
|
1291
|
+
|
|
1292
|
+
Execution Modes
|
|
1293
|
+
|
|
1294
|
+
1. Interpreter Mode
|
|
1295
|
+
|
|
1296
|
+
· Direct AST evaluation
|
|
1297
|
+
· Good for development/debugging
|
|
1298
|
+
· Full language support
|
|
1299
|
+
|
|
1300
|
+
2. Compiler Mode
|
|
1301
|
+
|
|
1302
|
+
· Source → Bytecode → VM execution
|
|
1303
|
+
· Better performance
|
|
1304
|
+
· NEW: Full async/events/closure support
|
|
1305
|
+
|
|
1306
|
+
3. Auto Mode
|
|
1307
|
+
|
|
1308
|
+
· Attempt compilation, fallback to interpreter
|
|
1309
|
+
· Best of both worlds
|
|
1310
|
+
|
|
1311
|
+
Unified Renderer Backend
|
|
1312
|
+
|
|
1313
|
+
· Renderer works identically in all modes
|
|
1314
|
+
· Single backend API for both interpreter and VM
|
|
1315
|
+
|
|
1316
|
+
---
|
|
1317
|
+
|
|
1318
|
+
Built-in functions (Enhanced)
|
|
1319
|
+
|
|
1320
|
+
Core Builtins
|
|
1321
|
+
|
|
1322
|
+
· string(x), len(x), first(list), rest(list)
|
|
1323
|
+
· map(), filter(), reduce()
|
|
1324
|
+
· datetime_now(), random(), sqrt()
|
|
1325
|
+
|
|
1326
|
+
NEW: Async & Network
|
|
1327
|
+
|
|
1328
|
+
· sleep(seconds) - Async sleep
|
|
1329
|
+
· spawn(coroutine) - Create async task
|
|
1330
|
+
· fetch(url) - HTTP requests
|
|
1331
|
+
· connect_peer(address) - P2P networking
|
|
1332
|
+
|
|
1333
|
+
NEW: Crypto & Blockchain
|
|
1334
|
+
|
|
1335
|
+
· keccak256(data) - Hashing
|
|
1336
|
+
· secp256k1_sign(msg, key) - Signing
|
|
1337
|
+
· verify_signature(msg, sig, pubkey) - Verification
|
|
1338
|
+
· create_address(pubkey) - Address derivation
|
|
1339
|
+
|
|
1340
|
+
Renderer Builtins
|
|
1341
|
+
|
|
1342
|
+
· define_screen(), define_component(), render_screen()
|
|
1343
|
+
· mix(), create_canvas(), draw_line(), draw_circle()
|
|
1344
|
+
|
|
1345
|
+
---
|
|
1346
|
+
|
|
1347
|
+
Developer workflows
|
|
1348
|
+
|
|
1349
|
+
Adding New Syntax
|
|
1350
|
+
|
|
1351
|
+
1. Add tokens in zexus_token.py
|
|
1352
|
+
2. Update lexer in lexer.py
|
|
1353
|
+
3. Add AST nodes in both zexus_ast.py and compiler/zexus_ast.py
|
|
1354
|
+
4. Implement parsing in both parsers
|
|
1355
|
+
5. Add bytecode generation in compiler/bytecode.py
|
|
1356
|
+
6. Implement VM support in vm/vm.py
|
|
1357
|
+
7. Add semantic checks in compiler/semantic.py
|
|
1358
|
+
8. Create tests and update verification scripts
|
|
1359
|
+
|
|
1360
|
+
Async/Event Development
|
|
1361
|
+
|
|
1362
|
+
```zexus
|
|
1363
|
+
// 1. Define async action
|
|
1364
|
+
action async network_operation() {
|
|
1365
|
+
let data = await fetch("https://api.example.com/data")
|
|
1366
|
+
return process(data)
|
|
1367
|
+
}
|
|
1368
|
+
|
|
1369
|
+
// 2. Define events
|
|
1370
|
+
event DataReceived {
|
|
1371
|
+
url: string,
|
|
1372
|
+
data: any,
|
|
1373
|
+
timestamp: integer
|
|
1374
|
+
}
|
|
1375
|
+
|
|
1376
|
+
// 3. Register handlers
|
|
1377
|
+
register_event("data_received", action(event) {
|
|
1378
|
+
print("Received data from " + event.url)
|
|
1379
|
+
cache_data(event.data)
|
|
1380
|
+
})
|
|
1381
|
+
|
|
1382
|
+
// 4. Emit events
|
|
1383
|
+
emit DataReceived {
|
|
1384
|
+
url: "https://api.example.com/data",
|
|
1385
|
+
data: response_data,
|
|
1386
|
+
timestamp: datetime_now().timestamp()
|
|
1387
|
+
}
|
|
1388
|
+
```
|
|
1389
|
+
|
|
1390
|
+
---
|
|
1391
|
+
|
|
1392
|
+
Testing & Verification
|
|
1393
|
+
|
|
1394
|
+
Comprehensive Testing
|
|
1395
|
+
|
|
1396
|
+
```bash
|
|
1397
|
+
# Run full test suite
|
|
1398
|
+
python3 scripts/verify_integration.py
|
|
1399
|
+
|
|
1400
|
+
# Test specific features
|
|
1401
|
+
python3 -c "
|
|
1402
|
+
from zexus.compiler import ZexusCompiler
|
|
1403
|
+
|
|
1404
|
+
# Test async/await
|
|
1405
|
+
code = '''
|
|
1406
|
+
action async test_async() {
|
|
1407
|
+
let result = await some_operation()
|
|
1408
|
+
return result
|
|
1409
|
+
}
|
|
1410
|
+
'''
|
|
1411
|
+
|
|
1412
|
+
compiler = ZexusCompiler(code)
|
|
1413
|
+
bytecode = compiler.compile()
|
|
1414
|
+
if compiler.errors:
|
|
1415
|
+
print('Errors:', compiler.errors)
|
|
1416
|
+
else:
|
|
1417
|
+
result = compiler.run_bytecode(debug=True)
|
|
1418
|
+
print('Result:', result)
|
|
1419
|
+
"
|
|
1420
|
+
```
|
|
1421
|
+
|
|
1422
|
+
VM Inspection
|
|
1423
|
+
|
|
1424
|
+
```python
|
|
1425
|
+
from zexus.vm.vm import VM
|
|
1426
|
+
|
|
1427
|
+
# Create VM with custom environment
|
|
1428
|
+
vm = VM(builtins=my_builtins, env=initial_env)
|
|
1429
|
+
|
|
1430
|
+
# Execute and inspect
|
|
1431
|
+
result = vm.execute(bytecode, debug=True)
|
|
1432
|
+
print("VM Stack:", vm.stack)
|
|
1433
|
+
print("VM Environment:", vm.env)
|
|
1434
|
+
```
|
|
1435
|
+
|
|
1436
|
+
---
|
|
1437
|
+
|
|
1438
|
+
Performance Characteristics
|
|
1439
|
+
|
|
1440
|
+
Interpreter Mode
|
|
1441
|
+
|
|
1442
|
+
· Pros: Fast startup, easy debugging
|
|
1443
|
+
· Cons: Slower execution, no optimizations
|
|
1444
|
+
|
|
1445
|
+
Compiler + VM Mode
|
|
1446
|
+
|
|
1447
|
+
· Pros: Better performance, async optimization
|
|
1448
|
+
· Cons: Slower startup, more complex
|
|
1449
|
+
|
|
1450
|
+
Memory Management
|
|
1451
|
+
|
|
1452
|
+
· Stack-based: Efficient value passing
|
|
1453
|
+
· Closure cells: Proper memory handling
|
|
1454
|
+
· Async tasks: Automatic cleanup
|
|
1455
|
+
|
|
1456
|
+
---
|
|
1457
|
+
|
|
1458
|
+
Production Readiness
|
|
1459
|
+
|
|
1460
|
+
✅ Implemented
|
|
1461
|
+
|
|
1462
|
+
· Complete language syntax
|
|
1463
|
+
· Advanced type system
|
|
1464
|
+
· Async/await concurrency
|
|
1465
|
+
· Event-driven architecture
|
|
1466
|
+
· Module system
|
|
1467
|
+
· Closure semantics
|
|
1468
|
+
· Virtual machine
|
|
1469
|
+
· Renderer system
|
|
1470
|
+
|
|
1471
|
+
🚀 Blockchain Ready
|
|
1472
|
+
|
|
1473
|
+
· Smart contract runtime
|
|
1474
|
+
· P2P networking primitives
|
|
1475
|
+
· Crypto operations
|
|
1476
|
+
· State management
|
|
1477
|
+
· Gas tracking (conceptual)
|
|
1478
|
+
|
|
1479
|
+
🔧 Next Steps
|
|
1480
|
+
|
|
1481
|
+
· Enhanced optimizer
|
|
1482
|
+
· JIT compilation
|
|
1483
|
+
· Production deployment
|
|
1484
|
+
· Standard library expansion
|
|
1485
|
+
|
|
1486
|
+
---
|
|
1487
|
+
|
|
1488
|
+
Contribution Guidelines
|
|
1489
|
+
|
|
1490
|
+
Code Standards
|
|
1491
|
+
|
|
1492
|
+
· Maintain dual AST compatibility
|
|
1493
|
+
· Test both interpreter and compiler paths
|
|
1494
|
+
· Ensure renderer backend consistency
|
|
1495
|
+
· Document new VM operations
|
|
1496
|
+
|
|
1497
|
+
Testing Requirements
|
|
1498
|
+
|
|
1499
|
+
· Add integration tests for new features
|
|
1500
|
+
· Verify async/event behavior
|
|
1501
|
+
· Test closure semantics
|
|
1502
|
+
· Ensure cross-mode compatibility
|
|
1503
|
+
|
|
1504
|
+
Documentation
|
|
1505
|
+
|
|
1506
|
+
· Update this README for new features
|
|
1507
|
+
· Add examples for complex features
|
|
1508
|
+
· Document VM bytecode operations
|
|
1509
|
+
· Create architecture diagrams
|
|
1510
|
+
|
|
1511
|
+
---
|
|
1512
|
+
|
|
1513
|
+
Architecture Diagrams Available
|
|
1514
|
+
|
|
1515
|
+
· Language compilation pipeline
|
|
1516
|
+
· VM stack machine operation
|
|
1517
|
+
· Async/event system flow
|
|
1518
|
+
· Closure memory model
|
|
1519
|
+
· Renderer component architecture
|
|
1520
|
+
|
|
1521
|
+
Contact maintainers for detailed architecture diagrams.
|
|
1522
|
+
|
|
1523
|
+
---
|
|
1524
|
+
|
|
1525
|
+
Zexus is now a production-ready language system capable of building sophisticated applications including blockchain platforms, reactive UIs, and distributed systems.
|