zexus 1.6.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (227) hide show
  1. package/LICENSE +0 -0
  2. package/README.md +2513 -0
  3. package/bin/zexus +2 -0
  4. package/bin/zpics +2 -0
  5. package/bin/zpm +2 -0
  6. package/bin/zx +2 -0
  7. package/bin/zx-deploy +2 -0
  8. package/bin/zx-dev +2 -0
  9. package/bin/zx-run +2 -0
  10. package/package.json +66 -0
  11. package/scripts/README.md +24 -0
  12. package/scripts/postinstall.js +44 -0
  13. package/shared_config.json +24 -0
  14. package/src/README.md +1525 -0
  15. package/src/tests/run_zexus_tests.py +117 -0
  16. package/src/tests/test_all_phases.zx +346 -0
  17. package/src/tests/test_blockchain_features.zx +306 -0
  18. package/src/tests/test_complexity_features.zx +321 -0
  19. package/src/tests/test_core_integration.py +185 -0
  20. package/src/tests/test_phase10_ecosystem.zx +177 -0
  21. package/src/tests/test_phase1_modifiers.zx +87 -0
  22. package/src/tests/test_phase2_plugins.zx +80 -0
  23. package/src/tests/test_phase3_security.zx +97 -0
  24. package/src/tests/test_phase4_vfs.zx +116 -0
  25. package/src/tests/test_phase5_types.zx +117 -0
  26. package/src/tests/test_phase6_metaprogramming.zx +125 -0
  27. package/src/tests/test_phase7_optimization.zx +132 -0
  28. package/src/tests/test_phase9_advanced_types.zx +157 -0
  29. package/src/tests/test_security_features.py +419 -0
  30. package/src/tests/test_security_features.zx +276 -0
  31. package/src/tests/test_simple_zx.zx +1 -0
  32. package/src/tests/test_verification_simple.zx +69 -0
  33. package/src/zexus/__init__.py +28 -0
  34. package/src/zexus/__main__.py +5 -0
  35. package/src/zexus/__pycache__/__init__.cpython-312.pyc +0 -0
  36. package/src/zexus/__pycache__/advanced_types.cpython-312.pyc +0 -0
  37. package/src/zexus/__pycache__/builtin_modules.cpython-312.pyc +0 -0
  38. package/src/zexus/__pycache__/capability_system.cpython-312.pyc +0 -0
  39. package/src/zexus/__pycache__/complexity_system.cpython-312.pyc +0 -0
  40. package/src/zexus/__pycache__/concurrency_system.cpython-312.pyc +0 -0
  41. package/src/zexus/__pycache__/config.cpython-312.pyc +0 -0
  42. package/src/zexus/__pycache__/dependency_injection.cpython-312.pyc +0 -0
  43. package/src/zexus/__pycache__/ecosystem.cpython-312.pyc +0 -0
  44. package/src/zexus/__pycache__/environment.cpython-312.pyc +0 -0
  45. package/src/zexus/__pycache__/error_reporter.cpython-312.pyc +0 -0
  46. package/src/zexus/__pycache__/hybrid_orchestrator.cpython-312.pyc +0 -0
  47. package/src/zexus/__pycache__/lexer.cpython-312.pyc +0 -0
  48. package/src/zexus/__pycache__/metaprogramming.cpython-312.pyc +0 -0
  49. package/src/zexus/__pycache__/module_cache.cpython-312.pyc +0 -0
  50. package/src/zexus/__pycache__/object.cpython-312.pyc +0 -0
  51. package/src/zexus/__pycache__/optimization.cpython-312.pyc +0 -0
  52. package/src/zexus/__pycache__/plugin_system.cpython-312.pyc +0 -0
  53. package/src/zexus/__pycache__/policy_engine.cpython-312.pyc +0 -0
  54. package/src/zexus/__pycache__/security.cpython-312.pyc +0 -0
  55. package/src/zexus/__pycache__/stdlib_integration.cpython-312.pyc +0 -0
  56. package/src/zexus/__pycache__/strategy_recovery.cpython-312.pyc +0 -0
  57. package/src/zexus/__pycache__/syntax_validator.cpython-312.pyc +0 -0
  58. package/src/zexus/__pycache__/type_system.cpython-312.pyc +0 -0
  59. package/src/zexus/__pycache__/virtual_filesystem.cpython-312.pyc +0 -0
  60. package/src/zexus/__pycache__/zexus_ast.cpython-312.pyc +0 -0
  61. package/src/zexus/__pycache__/zexus_token.cpython-312.pyc +0 -0
  62. package/src/zexus/advanced_types.py +401 -0
  63. package/src/zexus/blockchain/__init__.py +40 -0
  64. package/src/zexus/blockchain/__pycache__/__init__.cpython-312.pyc +0 -0
  65. package/src/zexus/blockchain/__pycache__/crypto.cpython-312.pyc +0 -0
  66. package/src/zexus/blockchain/__pycache__/ledger.cpython-312.pyc +0 -0
  67. package/src/zexus/blockchain/__pycache__/transaction.cpython-312.pyc +0 -0
  68. package/src/zexus/blockchain/crypto.py +463 -0
  69. package/src/zexus/blockchain/ledger.py +255 -0
  70. package/src/zexus/blockchain/transaction.py +267 -0
  71. package/src/zexus/builtin_modules.py +284 -0
  72. package/src/zexus/builtin_plugins.py +317 -0
  73. package/src/zexus/capability_system.py +372 -0
  74. package/src/zexus/cli/__init__.py +2 -0
  75. package/src/zexus/cli/__pycache__/__init__.cpython-312.pyc +0 -0
  76. package/src/zexus/cli/__pycache__/main.cpython-312.pyc +0 -0
  77. package/src/zexus/cli/main.py +707 -0
  78. package/src/zexus/cli/zpm.py +203 -0
  79. package/src/zexus/compare_interpreter_compiler.py +146 -0
  80. package/src/zexus/compiler/__init__.py +169 -0
  81. package/src/zexus/compiler/__pycache__/__init__.cpython-312.pyc +0 -0
  82. package/src/zexus/compiler/__pycache__/lexer.cpython-312.pyc +0 -0
  83. package/src/zexus/compiler/__pycache__/parser.cpython-312.pyc +0 -0
  84. package/src/zexus/compiler/__pycache__/zexus_ast.cpython-312.pyc +0 -0
  85. package/src/zexus/compiler/bytecode.py +266 -0
  86. package/src/zexus/compiler/compat_runtime.py +277 -0
  87. package/src/zexus/compiler/lexer.py +257 -0
  88. package/src/zexus/compiler/parser.py +779 -0
  89. package/src/zexus/compiler/semantic.py +118 -0
  90. package/src/zexus/compiler/zexus_ast.py +454 -0
  91. package/src/zexus/complexity_system.py +575 -0
  92. package/src/zexus/concurrency_system.py +493 -0
  93. package/src/zexus/config.py +201 -0
  94. package/src/zexus/crypto_bridge.py +19 -0
  95. package/src/zexus/dependency_injection.py +423 -0
  96. package/src/zexus/ecosystem.py +434 -0
  97. package/src/zexus/environment.py +101 -0
  98. package/src/zexus/environment_manager.py +119 -0
  99. package/src/zexus/error_reporter.py +314 -0
  100. package/src/zexus/evaluator/__init__.py +12 -0
  101. package/src/zexus/evaluator/__pycache__/__init__.cpython-312.pyc +0 -0
  102. package/src/zexus/evaluator/__pycache__/bytecode_compiler.cpython-312.pyc +0 -0
  103. package/src/zexus/evaluator/__pycache__/core.cpython-312.pyc +0 -0
  104. package/src/zexus/evaluator/__pycache__/expressions.cpython-312.pyc +0 -0
  105. package/src/zexus/evaluator/__pycache__/functions.cpython-312.pyc +0 -0
  106. package/src/zexus/evaluator/__pycache__/integration.cpython-312.pyc +0 -0
  107. package/src/zexus/evaluator/__pycache__/statements.cpython-312.pyc +0 -0
  108. package/src/zexus/evaluator/__pycache__/utils.cpython-312.pyc +0 -0
  109. package/src/zexus/evaluator/bytecode_compiler.py +700 -0
  110. package/src/zexus/evaluator/core.py +891 -0
  111. package/src/zexus/evaluator/expressions.py +827 -0
  112. package/src/zexus/evaluator/functions.py +3989 -0
  113. package/src/zexus/evaluator/integration.py +396 -0
  114. package/src/zexus/evaluator/statements.py +4303 -0
  115. package/src/zexus/evaluator/utils.py +126 -0
  116. package/src/zexus/evaluator_original.py +2041 -0
  117. package/src/zexus/external_bridge.py +16 -0
  118. package/src/zexus/find_affected_imports.sh +155 -0
  119. package/src/zexus/hybrid_orchestrator.py +152 -0
  120. package/src/zexus/input_validation.py +259 -0
  121. package/src/zexus/lexer.py +571 -0
  122. package/src/zexus/logging.py +89 -0
  123. package/src/zexus/lsp/__init__.py +9 -0
  124. package/src/zexus/lsp/completion_provider.py +207 -0
  125. package/src/zexus/lsp/definition_provider.py +22 -0
  126. package/src/zexus/lsp/hover_provider.py +71 -0
  127. package/src/zexus/lsp/server.py +269 -0
  128. package/src/zexus/lsp/symbol_provider.py +31 -0
  129. package/src/zexus/metaprogramming.py +321 -0
  130. package/src/zexus/module_cache.py +89 -0
  131. package/src/zexus/module_manager.py +107 -0
  132. package/src/zexus/object.py +973 -0
  133. package/src/zexus/optimization.py +424 -0
  134. package/src/zexus/parser/__init__.py +31 -0
  135. package/src/zexus/parser/__pycache__/__init__.cpython-312.pyc +0 -0
  136. package/src/zexus/parser/__pycache__/parser.cpython-312.pyc +0 -0
  137. package/src/zexus/parser/__pycache__/strategy_context.cpython-312.pyc +0 -0
  138. package/src/zexus/parser/__pycache__/strategy_structural.cpython-312.pyc +0 -0
  139. package/src/zexus/parser/integration.py +86 -0
  140. package/src/zexus/parser/parser.py +3977 -0
  141. package/src/zexus/parser/strategy_context.py +7254 -0
  142. package/src/zexus/parser/strategy_structural.py +1033 -0
  143. package/src/zexus/persistence.py +391 -0
  144. package/src/zexus/plugin_system.py +290 -0
  145. package/src/zexus/policy_engine.py +365 -0
  146. package/src/zexus/profiler/__init__.py +5 -0
  147. package/src/zexus/profiler/profiler.py +233 -0
  148. package/src/zexus/purity_system.py +398 -0
  149. package/src/zexus/runtime/__init__.py +20 -0
  150. package/src/zexus/runtime/async_runtime.py +324 -0
  151. package/src/zexus/search_old_imports.sh +65 -0
  152. package/src/zexus/security.py +1407 -0
  153. package/src/zexus/stack_trace.py +233 -0
  154. package/src/zexus/stdlib/__init__.py +27 -0
  155. package/src/zexus/stdlib/blockchain.py +341 -0
  156. package/src/zexus/stdlib/compression.py +167 -0
  157. package/src/zexus/stdlib/crypto.py +124 -0
  158. package/src/zexus/stdlib/datetime.py +163 -0
  159. package/src/zexus/stdlib/db_mongo.py +199 -0
  160. package/src/zexus/stdlib/db_mysql.py +162 -0
  161. package/src/zexus/stdlib/db_postgres.py +163 -0
  162. package/src/zexus/stdlib/db_sqlite.py +133 -0
  163. package/src/zexus/stdlib/encoding.py +230 -0
  164. package/src/zexus/stdlib/fs.py +195 -0
  165. package/src/zexus/stdlib/http.py +219 -0
  166. package/src/zexus/stdlib/http_server.py +248 -0
  167. package/src/zexus/stdlib/json_module.py +61 -0
  168. package/src/zexus/stdlib/math.py +360 -0
  169. package/src/zexus/stdlib/os_module.py +265 -0
  170. package/src/zexus/stdlib/regex.py +148 -0
  171. package/src/zexus/stdlib/sockets.py +253 -0
  172. package/src/zexus/stdlib/test_framework.zx +208 -0
  173. package/src/zexus/stdlib/test_runner.zx +119 -0
  174. package/src/zexus/stdlib_integration.py +341 -0
  175. package/src/zexus/strategy_recovery.py +256 -0
  176. package/src/zexus/syntax_validator.py +356 -0
  177. package/src/zexus/testing/zpics.py +407 -0
  178. package/src/zexus/testing/zpics_runtime.py +369 -0
  179. package/src/zexus/type_system.py +374 -0
  180. package/src/zexus/validation_system.py +569 -0
  181. package/src/zexus/virtual_filesystem.py +355 -0
  182. package/src/zexus/vm/__init__.py +8 -0
  183. package/src/zexus/vm/__pycache__/__init__.cpython-312.pyc +0 -0
  184. package/src/zexus/vm/__pycache__/async_optimizer.cpython-312.pyc +0 -0
  185. package/src/zexus/vm/__pycache__/bytecode.cpython-312.pyc +0 -0
  186. package/src/zexus/vm/__pycache__/cache.cpython-312.pyc +0 -0
  187. package/src/zexus/vm/__pycache__/jit.cpython-312.pyc +0 -0
  188. package/src/zexus/vm/__pycache__/memory_manager.cpython-312.pyc +0 -0
  189. package/src/zexus/vm/__pycache__/memory_pool.cpython-312.pyc +0 -0
  190. package/src/zexus/vm/__pycache__/optimizer.cpython-312.pyc +0 -0
  191. package/src/zexus/vm/__pycache__/parallel_vm.cpython-312.pyc +0 -0
  192. package/src/zexus/vm/__pycache__/peephole_optimizer.cpython-312.pyc +0 -0
  193. package/src/zexus/vm/__pycache__/profiler.cpython-312.pyc +0 -0
  194. package/src/zexus/vm/__pycache__/register_allocator.cpython-312.pyc +0 -0
  195. package/src/zexus/vm/__pycache__/register_vm.cpython-312.pyc +0 -0
  196. package/src/zexus/vm/__pycache__/ssa_converter.cpython-312.pyc +0 -0
  197. package/src/zexus/vm/__pycache__/vm.cpython-312.pyc +0 -0
  198. package/src/zexus/vm/async_optimizer.py +420 -0
  199. package/src/zexus/vm/bytecode.py +428 -0
  200. package/src/zexus/vm/bytecode_converter.py +297 -0
  201. package/src/zexus/vm/cache.py +532 -0
  202. package/src/zexus/vm/jit.py +720 -0
  203. package/src/zexus/vm/memory_manager.py +520 -0
  204. package/src/zexus/vm/memory_pool.py +511 -0
  205. package/src/zexus/vm/optimizer.py +478 -0
  206. package/src/zexus/vm/parallel_vm.py +899 -0
  207. package/src/zexus/vm/peephole_optimizer.py +452 -0
  208. package/src/zexus/vm/profiler.py +527 -0
  209. package/src/zexus/vm/register_allocator.py +462 -0
  210. package/src/zexus/vm/register_vm.py +520 -0
  211. package/src/zexus/vm/ssa_converter.py +757 -0
  212. package/src/zexus/vm/vm.py +1392 -0
  213. package/src/zexus/zexus_ast.py +1782 -0
  214. package/src/zexus/zexus_token.py +253 -0
  215. package/src/zexus/zpm/__init__.py +15 -0
  216. package/src/zexus/zpm/installer.py +116 -0
  217. package/src/zexus/zpm/package_manager.py +208 -0
  218. package/src/zexus/zpm/publisher.py +98 -0
  219. package/src/zexus/zpm/registry.py +110 -0
  220. package/src/zexus.egg-info/PKG-INFO +2235 -0
  221. package/src/zexus.egg-info/SOURCES.txt +876 -0
  222. package/src/zexus.egg-info/dependency_links.txt +1 -0
  223. package/src/zexus.egg-info/entry_points.txt +3 -0
  224. package/src/zexus.egg-info/not-zip-safe +1 -0
  225. package/src/zexus.egg-info/requires.txt +14 -0
  226. package/src/zexus.egg-info/top_level.txt +2 -0
  227. package/zexus.json +14 -0
@@ -0,0 +1,69 @@
1
+ =========================
2
+ VERIFICATION: Core Features Working
3
+ =========================
4
+
5
+ print "✅ SECURITY FEATURES:";
6
+ print "";
7
+
8
+ // Test 1: Capability
9
+ capability admin_access;
10
+ print " ✓ capability statement parsed";
11
+
12
+ // Test 2: Grant
13
+ grant service_account admin_access;
14
+ print " ✓ grant statement parsed";
15
+
16
+ // Test 3: Revoke
17
+ revoke service_account admin_access;
18
+ print " ✓ revoke statement parsed";
19
+
20
+ // Test 4: Validate
21
+ let data = "test@example.com";
22
+ validate data using "email";
23
+ print " ✓ validate statement parsed";
24
+
25
+ // Test 5: Sanitize
26
+ let html_content = "<p>Hello</p>";
27
+ sanitize html_content as "html";
28
+ print " ✓ sanitize statement parsed";
29
+
30
+ // Test 6: Immutable
31
+ immutable let secret = "secure-key";
32
+ print " ✓ immutable statement parsed";
33
+
34
+ print "";
35
+ print "✅ COMPLEXITY FEATURES:";
36
+ print "";
37
+
38
+ // Test 7: Interface
39
+ interface Reader {
40
+ read;
41
+ close;
42
+ };
43
+ print " ✓ interface statement parsed";
44
+
45
+ // Test 8: Type Alias
46
+ type_alias UserId = integer;
47
+ print " ✓ type_alias statement parsed";
48
+
49
+ // Test 9: Module
50
+ module database {
51
+ let connection = "db://localhost";
52
+ };
53
+ print " ✓ module statement parsed";
54
+
55
+ // Test 10: Package
56
+ package app.utils {
57
+ let version = "1.0.0";
58
+ };
59
+ print " ✓ package statement parsed";
60
+
61
+ // Test 11: Using (RAII)
62
+ using(file = "test.txt") {
63
+ print " ✓ using statement parsed and executed";
64
+ };
65
+
66
+ print "";
67
+ print "=========================";
68
+ print "✅ All Features Verified!";
69
+ print "=========================";
@@ -0,0 +1,28 @@
1
+ # src/zexus/__init__.py
2
+ """
3
+ Zexus Programming Language
4
+ A declarative, intent-based programming language for modern applications.
5
+ """
6
+
7
+ __version__ = "1.6.2"
8
+ __author__ = "Ziver Labs"
9
+ __email__ = "ziverofficial567@gmail.com"
10
+
11
+ from .lexer import Lexer
12
+ from .parser import Parser
13
+ # UPDATED: Import from new evaluator structure
14
+ from .evaluator import evaluate
15
+ from .object import (
16
+ Environment, Object, Integer, Float, String, Boolean, Null,
17
+ List, Map, Action, Builtin, ReturnValue, EmbeddedCode
18
+ )
19
+
20
+ # For backward compatibility, you can alias eval_node to evaluate if needed
21
+ # but better to update callers to use evaluate
22
+ eval_node = evaluate # Alias for backward compatibility
23
+
24
+ __all__ = [
25
+ "Lexer", "Parser", "evaluate", "eval_node", "Environment", # UPDATED
26
+ "Object", "Integer", "Float", "String", "Boolean",
27
+ "Null", "List", "Map", "Action", "Builtin", "ReturnValue", "EmbeddedCode"
28
+ ]
@@ -0,0 +1,5 @@
1
+ # ~/zexus-interpreter/src/zexus/__main__.py
2
+ from .cli.main import cli
3
+
4
+ if __name__ == "__main__":
5
+ cli()
@@ -0,0 +1,401 @@
1
+ """
2
+ Advanced type system with generics, traits, and union types.
3
+ """
4
+
5
+ from typing import Any, Dict, List, Optional, Set, Union, Tuple
6
+ from dataclasses import dataclass, field
7
+ from enum import Enum
8
+ from abc import ABC, abstractmethod
9
+
10
+
11
+ class TypeVariance(Enum):
12
+ """Type parameter variance."""
13
+ COVARIANT = "covariant" # Producer type
14
+ CONTRAVARIANT = "contravariant" # Consumer type
15
+ INVARIANT = "invariant" # Bidirectional type
16
+
17
+
18
+ @dataclass
19
+ class TypeParameter:
20
+ """Generic type parameter."""
21
+ name: str
22
+ bounds: Optional[List['TypeSpec']] = None # Upper bounds
23
+ variance: TypeVariance = TypeVariance.INVARIANT
24
+ default: Optional['TypeSpec'] = None # Default type
25
+
26
+ def __hash__(self) -> int:
27
+ return hash(self.name)
28
+
29
+ def __eq__(self, other) -> bool:
30
+ if not isinstance(other, TypeParameter):
31
+ return False
32
+ return self.name == other.name
33
+
34
+ def satisfies_bounds(self, type_spec: 'TypeSpec') -> bool:
35
+ """Check if type satisfies bounds."""
36
+ if not self.bounds:
37
+ return True
38
+ # Simplified - would need full subtype checking
39
+ return True
40
+
41
+
42
+ @dataclass
43
+ class GenericType:
44
+ """Generic type with type parameters."""
45
+ base_type: 'TypeSpec'
46
+ type_params: List[TypeParameter] = field(default_factory=list)
47
+ type_args: Dict[str, 'TypeSpec'] = field(default_factory=dict)
48
+
49
+ def is_fully_specified(self) -> bool:
50
+ """Check if all type parameters are bound."""
51
+ return len(self.type_args) == len(self.type_params)
52
+
53
+ def instantiate(self, type_args: Dict[str, 'TypeSpec']) -> 'TypeSpec':
54
+ """Create concrete type from generic."""
55
+ # Return instantiated type
56
+ return self.base_type
57
+
58
+ def __repr__(self) -> str:
59
+ """String representation."""
60
+ params = ", ".join(p.name for p in self.type_params)
61
+ return f"Generic<{params}>"
62
+
63
+
64
+ @dataclass
65
+ class UnionType:
66
+ """Union type (multiple possible types)."""
67
+ types: Set['TypeSpec'] = field(default_factory=set)
68
+ discriminator: Optional[str] = None # Discriminator field for tagged unions
69
+
70
+ def add_type(self, type_spec: 'TypeSpec'):
71
+ """Add type to union."""
72
+ self.types.add(type_spec)
73
+
74
+ def is_member(self, type_spec: 'TypeSpec') -> bool:
75
+ """Check if type is in union."""
76
+ return type_spec in self.types
77
+
78
+ def __repr__(self) -> str:
79
+ """String representation."""
80
+ type_names = " | ".join(str(t) for t in self.types)
81
+ return f"({type_names})"
82
+
83
+
84
+ class Trait(ABC):
85
+ """Base class for type traits."""
86
+
87
+ @abstractmethod
88
+ def get_name(self) -> str:
89
+ """Get trait name."""
90
+ pass
91
+
92
+ @abstractmethod
93
+ def get_required_methods(self) -> Set[str]:
94
+ """Get methods required to implement trait."""
95
+ pass
96
+
97
+ @abstractmethod
98
+ def get_provided_methods(self) -> Dict[str, callable]:
99
+ """Get default method implementations."""
100
+ pass
101
+
102
+ @abstractmethod
103
+ def validate_implementation(self, obj: Any) -> Tuple[bool, List[str]]:
104
+ """Validate that object implements trait."""
105
+ pass
106
+
107
+
108
+ @dataclass
109
+ class StructuralTrait(Trait):
110
+ """Trait based on object structure."""
111
+ name: str
112
+ required_methods: Set[str] = field(default_factory=set)
113
+ required_attributes: Set[str] = field(default_factory=set)
114
+ method_signatures: Dict[str, List['TypeSpec']] = field(default_factory=dict)
115
+ default_impls: Dict[str, callable] = field(default_factory=dict)
116
+
117
+ def get_name(self) -> str:
118
+ """Get trait name."""
119
+ return self.name
120
+
121
+ def get_required_methods(self) -> Set[str]:
122
+ """Get required methods."""
123
+ return self.required_methods.copy()
124
+
125
+ def get_provided_methods(self) -> Dict[str, callable]:
126
+ """Get default implementations."""
127
+ return self.default_impls.copy()
128
+
129
+ def validate_implementation(self, obj: Any) -> Tuple[bool, List[str]]:
130
+ """Check if object implements trait."""
131
+ missing = []
132
+
133
+ # Check methods
134
+ for method in self.required_methods:
135
+ if not hasattr(obj, method) or not callable(getattr(obj, method)):
136
+ missing.append(f"Missing method: {method}")
137
+
138
+ # Check attributes
139
+ for attr in self.required_attributes:
140
+ if not hasattr(obj, attr):
141
+ missing.append(f"Missing attribute: {attr}")
142
+
143
+ return len(missing) == 0, missing
144
+
145
+
146
+ @dataclass
147
+ class TraitImpl:
148
+ """Trait implementation for a type."""
149
+ type_name: str
150
+ trait: Trait
151
+ methods: Dict[str, callable] = field(default_factory=dict)
152
+
153
+ def get_method(self, name: str) -> Optional[callable]:
154
+ """Get method implementation."""
155
+ if name in self.methods:
156
+ return self.methods[name]
157
+ return None
158
+
159
+ def is_valid(self) -> bool:
160
+ """Check if implementation is valid."""
161
+ valid, _ = self.trait.validate_implementation(self)
162
+ return valid
163
+
164
+
165
+ class SimpleIterableTrait(StructuralTrait):
166
+ """Trait for iterable types."""
167
+
168
+ def __init__(self):
169
+ """Initialize iterable trait."""
170
+ super().__init__(
171
+ name="Iterable",
172
+ required_methods={"iter", "next"},
173
+ method_signatures={
174
+ "iter": [],
175
+ "next": []
176
+ }
177
+ )
178
+
179
+
180
+ class SimpleComparableTrait(StructuralTrait):
181
+ """Trait for comparable types."""
182
+
183
+ def __init__(self):
184
+ """Initialize comparable trait."""
185
+ super().__init__(
186
+ name="Comparable",
187
+ required_methods={"compare", "equals"},
188
+ method_signatures={
189
+ "compare": [],
190
+ "equals": []
191
+ }
192
+ )
193
+
194
+
195
+ class SimpleCloneableTrait(StructuralTrait):
196
+ """Trait for cloneable types."""
197
+
198
+ def __init__(self):
199
+ """Initialize cloneable trait."""
200
+ super().__init__(
201
+ name="Cloneable",
202
+ required_methods={"clone"},
203
+ method_signatures={
204
+ "clone": []
205
+ }
206
+ )
207
+
208
+
209
+ class AdvancedTypeSpec:
210
+ """Advanced type specification with generics and traits."""
211
+
212
+ def __init__(self, base_type: str):
213
+ """Initialize advanced type spec."""
214
+ self.base_type = base_type
215
+ self.generic: Optional[GenericType] = None
216
+ self.union: Optional[UnionType] = None
217
+ self.traits: List[Trait] = []
218
+ self.nullable = False
219
+ self.array_of: Optional['AdvancedTypeSpec'] = None
220
+
221
+ def with_generic(self, type_params: List[TypeParameter],
222
+ type_args: Optional[Dict[str, 'AdvancedTypeSpec']] = None) -> 'AdvancedTypeSpec':
223
+ """Add generic parameters."""
224
+ self.generic = GenericType(
225
+ base_type=self,
226
+ type_params=type_params,
227
+ type_args=type_args or {}
228
+ )
229
+ return self
230
+
231
+ def with_union(self, types: List['AdvancedTypeSpec']) -> 'AdvancedTypeSpec':
232
+ """Create union type."""
233
+ self.union = UnionType()
234
+ for t in types:
235
+ self.union.add_type(t)
236
+ return self
237
+
238
+ def with_trait(self, trait: Trait) -> 'AdvancedTypeSpec':
239
+ """Add trait requirement."""
240
+ self.traits.append(trait)
241
+ return self
242
+
243
+ def make_nullable(self) -> 'AdvancedTypeSpec':
244
+ """Make type nullable."""
245
+ self.nullable = True
246
+ return self
247
+
248
+ def make_array(self) -> 'AdvancedTypeSpec':
249
+ """Create array type."""
250
+ arr = AdvancedTypeSpec(f"{self.base_type}[]")
251
+ arr.array_of = self
252
+ return arr
253
+
254
+ def is_assignable_to(self, other: 'AdvancedTypeSpec') -> bool:
255
+ """Check if assignable to another type."""
256
+ # Base type check
257
+ if self.base_type == other.base_type:
258
+ return True
259
+
260
+ # Nullable check
261
+ if other.nullable and self.nullable:
262
+ return True
263
+
264
+ # Union check
265
+ if other.union and self in other.union.types:
266
+ return True
267
+
268
+ return False
269
+
270
+ def satisfies_traits(self, obj: Any) -> bool:
271
+ """Check if object satisfies all traits."""
272
+ for trait in self.traits:
273
+ valid, _ = trait.validate_implementation(obj)
274
+ if not valid:
275
+ return False
276
+ return True
277
+
278
+ def __repr__(self) -> str:
279
+ """String representation."""
280
+ parts = [self.base_type]
281
+
282
+ if self.generic:
283
+ parts.append(str(self.generic))
284
+
285
+ if self.union:
286
+ parts.append(str(self.union))
287
+
288
+ if self.traits:
289
+ trait_names = ", ".join(t.get_name() for t in self.traits)
290
+ parts.append(f"impl({trait_names})")
291
+
292
+ if self.nullable:
293
+ parts.append("?")
294
+
295
+ return "".join(parts)
296
+
297
+
298
+ class TraitRegistry:
299
+ """Registry for managing traits."""
300
+
301
+ def __init__(self):
302
+ """Initialize trait registry."""
303
+ self.traits: Dict[str, Trait] = {}
304
+ self.implementations: Dict[Tuple[str, str], TraitImpl] = {} # (type, trait) -> impl
305
+ self._register_default_traits()
306
+
307
+ def _register_default_traits(self):
308
+ """Register built-in traits."""
309
+ self.register_trait("Iterable", SimpleIterableTrait())
310
+ self.register_trait("Comparable", SimpleComparableTrait())
311
+ self.register_trait("Cloneable", SimpleCloneableTrait())
312
+
313
+ def register_trait(self, name: str, trait: Trait):
314
+ """Register a trait."""
315
+ self.traits[name] = trait
316
+
317
+ def get_trait(self, name: str) -> Optional[Trait]:
318
+ """Get trait by name."""
319
+ return self.traits.get(name)
320
+
321
+ def register_impl(self, type_name: str, trait_name: str, impl: TraitImpl):
322
+ """Register trait implementation for a type."""
323
+ self.implementations[(type_name, trait_name)] = impl
324
+
325
+ def get_impl(self, type_name: str, trait_name: str) -> Optional[TraitImpl]:
326
+ """Get trait implementation."""
327
+ return self.implementations.get((type_name, trait_name))
328
+
329
+ def get_type_traits(self, type_name: str) -> List[Trait]:
330
+ """Get all traits implemented by a type."""
331
+ trait_names = set()
332
+ for (t_type, t_name), _ in self.implementations.items():
333
+ if t_type == type_name:
334
+ trait_names.add(t_name)
335
+
336
+ return [self.traits[name] for name in trait_names if name in self.traits]
337
+
338
+
339
+ class GenericResolver:
340
+ """Resolve generic type applications."""
341
+
342
+ def __init__(self):
343
+ """Initialize generic resolver."""
344
+ self.instantiations: Dict[str, AdvancedTypeSpec] = {}
345
+
346
+ def resolve(self, generic_type: 'AdvancedTypeSpec',
347
+ type_args: Dict[str, 'AdvancedTypeSpec']) -> 'AdvancedTypeSpec':
348
+ """Resolve generic type with type arguments."""
349
+ if not generic_type.generic:
350
+ return generic_type
351
+
352
+ # Create instantiation key
353
+ type_arg_strs = [str(t) for t in type_args.values()]
354
+ key = f"{generic_type.base_type}[{','.join(type_arg_strs)}]"
355
+
356
+ if key in self.instantiations:
357
+ return self.instantiations[key]
358
+
359
+ # Create concrete type
360
+ concrete = AdvancedTypeSpec(key)
361
+ self.instantiations[key] = concrete
362
+
363
+ return concrete
364
+
365
+ def check_type_bounds(self, type_param: TypeParameter,
366
+ type_arg: AdvancedTypeSpec) -> bool:
367
+ """Check if type argument satisfies bounds."""
368
+ if not type_param.bounds:
369
+ return True
370
+
371
+ for bound in type_param.bounds:
372
+ # Simplified check
373
+ if hasattr(bound, 'base_type') and hasattr(type_arg, 'base_type'):
374
+ if bound.base_type == type_arg.base_type:
375
+ return True
376
+
377
+ return len(type_param.bounds) == 0
378
+
379
+
380
+ # Global trait registry
381
+ _global_trait_registry = TraitRegistry()
382
+
383
+
384
+ def get_trait_registry() -> TraitRegistry:
385
+ """Get global trait registry."""
386
+ return _global_trait_registry
387
+
388
+
389
+ def create_generic_type(name: str, type_params: List[str]) -> AdvancedTypeSpec:
390
+ """Create a generic type."""
391
+ spec = AdvancedTypeSpec(name)
392
+ params = [TypeParameter(p) for p in type_params]
393
+ spec.with_generic(params)
394
+ return spec
395
+
396
+
397
+ def create_union_type(types: List[AdvancedTypeSpec]) -> AdvancedTypeSpec:
398
+ """Create a union type."""
399
+ spec = AdvancedTypeSpec("union")
400
+ spec.with_union(types)
401
+ return spec
@@ -0,0 +1,40 @@
1
+ """
2
+ Zexus Blockchain Module
3
+
4
+ Complete blockchain and smart contract support for Zexus.
5
+
6
+ Features:
7
+ - Immutable ledger with versioning
8
+ - Transaction context (TX object)
9
+ - Gas tracking and execution limits
10
+ - Cryptographic primitives (hashing, signatures)
11
+ - Smart contract execution environment
12
+ """
13
+
14
+ from .ledger import Ledger, LedgerManager, get_ledger_manager
15
+ from .transaction import (
16
+ TransactionContext, GasTracker,
17
+ create_tx_context, get_current_tx, end_tx_context,
18
+ consume_gas, check_gas_and_consume
19
+ )
20
+ from .crypto import CryptoPlugin, register_crypto_builtins
21
+
22
+ __all__ = [
23
+ # Ledger
24
+ 'Ledger',
25
+ 'LedgerManager',
26
+ 'get_ledger_manager',
27
+
28
+ # Transaction
29
+ 'TransactionContext',
30
+ 'GasTracker',
31
+ 'create_tx_context',
32
+ 'get_current_tx',
33
+ 'end_tx_context',
34
+ 'consume_gas',
35
+ 'check_gas_and_consume',
36
+
37
+ # Crypto
38
+ 'CryptoPlugin',
39
+ 'register_crypto_builtins',
40
+ ]