quantum-framework 0.9.0__py3-none-any.whl
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.
- quantum/__init__.py +1 -0
- quantum/cli/__init__.py +2 -0
- quantum/cli/commands/__init__.py +15 -0
- quantum/cli/commands/build.py +417 -0
- quantum/cli/commands/dev.py +185 -0
- quantum/cli/commands/docs.py +329 -0
- quantum/cli/commands/lint.py +523 -0
- quantum/cli/commands/migrate.py +527 -0
- quantum/cli/commands/new.py +622 -0
- quantum/cli/commands/serve.py +190 -0
- quantum/cli/commands/test.py +193 -0
- quantum/cli/deploy.py +810 -0
- quantum/cli/hot_reload.py +951 -0
- quantum/cli/jobs.py +356 -0
- quantum/cli/mq.py +582 -0
- quantum/cli/pkg.py +390 -0
- quantum/cli/runner.py +547 -0
- quantum/cli/server_process.py +159 -0
- quantum/cli/utils.py +334 -0
- quantum/compiler/__init__.py +30 -0
- quantum/compiler/base_generator.py +367 -0
- quantum/compiler/cli.py +295 -0
- quantum/compiler/expression_transformer.py +444 -0
- quantum/compiler/javascript/__init__.py +10 -0
- quantum/compiler/javascript/generator.py +659 -0
- quantum/compiler/optimizer.py +270 -0
- quantum/compiler/python/__init__.py +10 -0
- quantum/compiler/python/generator.py +883 -0
- quantum/compiler/python/runtime.py +863 -0
- quantum/compiler/transpiler.py +330 -0
- quantum/core/__init__.py +3 -0
- quantum/core/ast_nodes.py +2611 -0
- quantum/core/expression_diagnostics.py +87 -0
- quantum/core/expression_stdlib.py +148 -0
- quantum/core/expressions.py +591 -0
- quantum/core/features/agents/src/__init__.py +30 -0
- quantum/core/features/agents/src/ast_node.py +540 -0
- quantum/core/features/conditionals/src/__init__.py +8 -0
- quantum/core/features/conditionals/src/ast_node.py +68 -0
- quantum/core/features/data_fetching/src/__init__.py +21 -0
- quantum/core/features/data_fetching/src/ast_node.py +312 -0
- quantum/core/features/data_fetching/src/desktop_adapter.py +351 -0
- quantum/core/features/data_fetching/src/html_adapter.py +474 -0
- quantum/core/features/data_fetching/src/parser.py +225 -0
- quantum/core/features/data_import/src/__init__.py +0 -0
- quantum/core/features/data_import/src/ast_node.py +291 -0
- quantum/core/features/data_import/src/runtime.py +538 -0
- quantum/core/features/dump/src/__init__.py +12 -0
- quantum/core/features/dump/src/ast_node.py +106 -0
- quantum/core/features/dump/src/parser.py +61 -0
- quantum/core/features/dump/src/runtime.py +246 -0
- quantum/core/features/functions/src/__init__.py +8 -0
- quantum/core/features/functions/src/ast_node.py +149 -0
- quantum/core/features/game_engine_2d/src/__init__.py +19 -0
- quantum/core/features/game_engine_2d/src/ast_nodes.py +1719 -0
- quantum/core/features/game_engine_2d/src/parser.py +983 -0
- quantum/core/features/invocation/src/__init__.py +0 -0
- quantum/core/features/invocation/src/ast_node.py +146 -0
- quantum/core/features/invocation/src/runtime.py +327 -0
- quantum/core/features/knowledge_base/src/__init__.py +6 -0
- quantum/core/features/knowledge_base/src/ast_node.py +113 -0
- quantum/core/features/knowledge_base/src/parser.py +82 -0
- quantum/core/features/logging/src/__init__.py +12 -0
- quantum/core/features/logging/src/ast_node.py +111 -0
- quantum/core/features/logging/src/parser.py +50 -0
- quantum/core/features/logging/src/runtime.py +190 -0
- quantum/core/features/loops/src/__init__.py +8 -0
- quantum/core/features/loops/src/ast_node.py +60 -0
- quantum/core/features/query/src/__init__.py +0 -0
- quantum/core/features/query/src/database_service.py +322 -0
- quantum/core/features/query/src/query_validators.py +20 -0
- quantum/core/features/state_management/src/__init__.py +11 -0
- quantum/core/features/state_management/src/ast_node.py +228 -0
- quantum/core/features/terminal_engine/src/__init__.py +21 -0
- quantum/core/features/terminal_engine/src/ast_nodes.py +560 -0
- quantum/core/features/terminal_engine/src/parser.py +361 -0
- quantum/core/features/testing_engine/src/__init__.py +41 -0
- quantum/core/features/testing_engine/src/ast_nodes.py +1212 -0
- quantum/core/features/testing_engine/src/parser.py +604 -0
- quantum/core/features/theming/src/__init__.py +48 -0
- quantum/core/features/theming/src/ast_node.py +137 -0
- quantum/core/features/theming/src/presets.py +405 -0
- quantum/core/features/ui_engine/src/__init__.py +20 -0
- quantum/core/features/ui_engine/src/ast_nodes.py +1854 -0
- quantum/core/features/ui_engine/src/parser.py +1106 -0
- quantum/core/features/websocket/src/__init__.py +24 -0
- quantum/core/features/websocket/src/ast_node.py +247 -0
- quantum/core/html_compat.py +299 -0
- quantum/core/parser.py +1235 -0
- quantum/core/parser_registry.py +213 -0
- quantum/core/parsers/__init__.py +76 -0
- quantum/core/parsers/ai/__init__.py +12 -0
- quantum/core/parsers/ai/agent_parser.py +106 -0
- quantum/core/parsers/ai/knowledge_parser.py +86 -0
- quantum/core/parsers/ai/llm_parser.py +78 -0
- quantum/core/parsers/ai/team_parser.py +88 -0
- quantum/core/parsers/base.py +322 -0
- quantum/core/parsers/composition/__init__.py +10 -0
- quantum/core/parsers/composition/import_parser.py +50 -0
- quantum/core/parsers/composition/slot_parser.py +48 -0
- quantum/core/parsers/control_flow/__init__.py +11 -0
- quantum/core/parsers/control_flow/if_parser.py +68 -0
- quantum/core/parsers/control_flow/loop_parser.py +105 -0
- quantum/core/parsers/control_flow/set_parser.py +89 -0
- quantum/core/parsers/data/__init__.py +12 -0
- quantum/core/parsers/data/data_parser.py +217 -0
- quantum/core/parsers/data/invoke_parser.py +116 -0
- quantum/core/parsers/data/query_parser.py +188 -0
- quantum/core/parsers/data/transaction_parser.py +77 -0
- quantum/core/parsers/events/__init__.py +9 -0
- quantum/core/parsers/events/dispatch_event_parser.py +44 -0
- quantum/core/parsers/forms/__init__.py +11 -0
- quantum/core/parsers/forms/action_parser.py +54 -0
- quantum/core/parsers/forms/flash_parser.py +34 -0
- quantum/core/parsers/forms/redirect_parser.py +33 -0
- quantum/core/parsers/functions/__init__.py +11 -0
- quantum/core/parsers/functions/function_parser.py +137 -0
- quantum/core/parsers/functions/param_parser.py +66 -0
- quantum/core/parsers/functions/return_parser.py +31 -0
- quantum/core/parsers/html/__init__.py +10 -0
- quantum/core/parsers/html/component_call_parser.py +130 -0
- quantum/core/parsers/html/html_parser.py +115 -0
- quantum/core/parsers/jobs/__init__.py +11 -0
- quantum/core/parsers/jobs/job_parser.py +71 -0
- quantum/core/parsers/jobs/schedule_parser.py +62 -0
- quantum/core/parsers/jobs/thread_parser.py +57 -0
- quantum/core/parsers/messaging/__init__.py +17 -0
- quantum/core/parsers/messaging/message_ack_parser.py +30 -0
- quantum/core/parsers/messaging/message_nack_parser.py +30 -0
- quantum/core/parsers/messaging/message_parser.py +114 -0
- quantum/core/parsers/messaging/queue_parser.py +61 -0
- quantum/core/parsers/messaging/websocket_parser.py +121 -0
- quantum/core/parsers/persistence/__init__.py +9 -0
- quantum/core/parsers/persistence/persist_parser.py +64 -0
- quantum/core/parsers/routing/__init__.py +9 -0
- quantum/core/parsers/routing/route_parser.py +41 -0
- quantum/core/parsers/scripting/__init__.py +12 -0
- quantum/core/parsers/scripting/pyclass_parser.py +62 -0
- quantum/core/parsers/scripting/pydecorator_parser.py +68 -0
- quantum/core/parsers/scripting/pyimport_parser.py +52 -0
- quantum/core/parsers/scripting/python_parser.py +49 -0
- quantum/core/parsers/services/__init__.py +12 -0
- quantum/core/parsers/services/dump_parser.py +52 -0
- quantum/core/parsers/services/file_parser.py +48 -0
- quantum/core/parsers/services/log_parser.py +46 -0
- quantum/core/parsers/services/mail_parser.py +65 -0
- quantum/core/tiers.py +82 -0
- quantum/packages/__init__.py +28 -0
- quantum/packages/manager.py +413 -0
- quantum/packages/manifest.py +351 -0
- quantum/packages/registry.py +399 -0
- quantum/packages/resolver.py +336 -0
- quantum/plugins/__init__.py +33 -0
- quantum/plugins/hooks.py +329 -0
- quantum/plugins/loader.py +479 -0
- quantum/plugins/manifest.py +336 -0
- quantum/plugins/registry.py +371 -0
- quantum/runtime/__init__.py +28 -0
- quantum/runtime/action_handler.py +443 -0
- quantum/runtime/adapters/__init__.py +88 -0
- quantum/runtime/adapters/memory_adapter.py +690 -0
- quantum/runtime/adapters/rabbitmq_adapter.py +715 -0
- quantum/runtime/adapters/redis_adapter.py +582 -0
- quantum/runtime/adapters/sqlite_adapter.py +414 -0
- quantum/runtime/agent_service.py +1133 -0
- quantum/runtime/api_server.py +86 -0
- quantum/runtime/ast_cache.py +506 -0
- quantum/runtime/auth_service.py +267 -0
- quantum/runtime/component.py +990 -0
- quantum/runtime/component_composer.py +319 -0
- quantum/runtime/component_resolver.py +174 -0
- quantum/runtime/database_service.py +598 -0
- quantum/runtime/email_service.py +162 -0
- quantum/runtime/error_handler.py +295 -0
- quantum/runtime/execution_context.py +286 -0
- quantum/runtime/executor_registry.py +171 -0
- quantum/runtime/executors/__init__.py +71 -0
- quantum/runtime/executors/ai/__init__.py +12 -0
- quantum/runtime/executors/ai/agent_executor.py +217 -0
- quantum/runtime/executors/ai/knowledge_executor.py +114 -0
- quantum/runtime/executors/ai/llm_executor.py +153 -0
- quantum/runtime/executors/ai/team_executor.py +171 -0
- quantum/runtime/executors/base.py +262 -0
- quantum/runtime/executors/control_flow/__init__.py +11 -0
- quantum/runtime/executors/control_flow/if_executor.py +93 -0
- quantum/runtime/executors/control_flow/loop_executor.py +307 -0
- quantum/runtime/executors/control_flow/set_executor.py +412 -0
- quantum/runtime/executors/data/__init__.py +12 -0
- quantum/runtime/executors/data/data_executor.py +145 -0
- quantum/runtime/executors/data/invoke_executor.py +176 -0
- quantum/runtime/executors/data/query_executor.py +256 -0
- quantum/runtime/executors/data/transaction_executor.py +91 -0
- quantum/runtime/executors/jobs/__init__.py +11 -0
- quantum/runtime/executors/jobs/job_executor.py +190 -0
- quantum/runtime/executors/jobs/schedule_executor.py +132 -0
- quantum/runtime/executors/jobs/thread_executor.py +127 -0
- quantum/runtime/executors/messaging/__init__.py +17 -0
- quantum/runtime/executors/messaging/message_ack_executor.py +51 -0
- quantum/runtime/executors/messaging/message_executor.py +174 -0
- quantum/runtime/executors/messaging/queue_executor.py +103 -0
- quantum/runtime/executors/messaging/websocket_executor.py +197 -0
- quantum/runtime/executors/scripting/__init__.py +11 -0
- quantum/runtime/executors/scripting/pyclass_executor.py +90 -0
- quantum/runtime/executors/scripting/pyimport_executor.py +81 -0
- quantum/runtime/executors/scripting/python_executor.py +249 -0
- quantum/runtime/executors/services/__init__.py +12 -0
- quantum/runtime/executors/services/dump_executor.py +72 -0
- quantum/runtime/executors/services/file_executor.py +89 -0
- quantum/runtime/executors/services/log_executor.py +77 -0
- quantum/runtime/executors/services/mail_executor.py +81 -0
- quantum/runtime/expression_cache.py +498 -0
- quantum/runtime/file_upload_service.py +326 -0
- quantum/runtime/function_registry.py +118 -0
- quantum/runtime/game_builder.py +166 -0
- quantum/runtime/game_code_generator.py +2371 -0
- quantum/runtime/game_templates.py +2006 -0
- quantum/runtime/godot_code_generator.py +4681 -0
- quantum/runtime/godot_templates.py +1449 -0
- quantum/runtime/job_executor.py +1599 -0
- quantum/runtime/knowledge_service.py +500 -0
- quantum/runtime/llm_cache.py +100 -0
- quantum/runtime/llm_providers.py +704 -0
- quantum/runtime/llm_service.py +287 -0
- quantum/runtime/logging_setup.py +140 -0
- quantum/runtime/message_broker.py +364 -0
- quantum/runtime/message_queue_service.py +571 -0
- quantum/runtime/param_validation.py +184 -0
- quantum/runtime/pypy_compat.py +315 -0
- quantum/runtime/python_bridge.py +698 -0
- quantum/runtime/query_validators.py +304 -0
- quantum/runtime/renderer.py +733 -0
- quantum/runtime/service_container.py +444 -0
- quantum/runtime/terminal_builder.py +76 -0
- quantum/runtime/terminal_code_generator.py +607 -0
- quantum/runtime/terminal_templates.py +243 -0
- quantum/runtime/testing_builder.py +77 -0
- quantum/runtime/testing_code_generator.py +833 -0
- quantum/runtime/testing_templates.py +85 -0
- quantum/runtime/ui_builder.py +188 -0
- quantum/runtime/ui_desktop_adapter.py +1730 -0
- quantum/runtime/ui_desktop_templates.py +307 -0
- quantum/runtime/ui_html_adapter.py +2691 -0
- quantum/runtime/ui_html_templates.py +2297 -0
- quantum/runtime/ui_mobile_adapter.py +1832 -0
- quantum/runtime/ui_mobile_templates.py +1003 -0
- quantum/runtime/ui_textual_adapter.py +1866 -0
- quantum/runtime/ui_textual_templates.py +45 -0
- quantum/runtime/ui_tokens.py +465 -0
- quantum/runtime/ui_validator.py +365 -0
- quantum/runtime/validators.py +256 -0
- quantum/runtime/web_server.py +1766 -0
- quantum/runtime/websocket_adapter.py +501 -0
- quantum/runtime/websocket_service.py +585 -0
- quantum/runtime/websocket_transport.py +289 -0
- quantum/runtime/wsgi.py +101 -0
- quantum/utils/__init__.py +1 -0
- quantum_framework-0.9.0.dist-info/METADATA +244 -0
- quantum_framework-0.9.0.dist-info/RECORD +262 -0
- quantum_framework-0.9.0.dist-info/WHEEL +5 -0
- quantum_framework-0.9.0.dist-info/entry_points.txt +2 -0
- quantum_framework-0.9.0.dist-info/licenses/LICENSE +21 -0
- quantum_framework-0.9.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,330 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Quantum Transpiler
|
|
3
|
+
==================
|
|
4
|
+
|
|
5
|
+
Main orchestrator for transpiling Quantum code to Python/JavaScript.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
import sys
|
|
9
|
+
from pathlib import Path
|
|
10
|
+
from dataclasses import dataclass
|
|
11
|
+
from typing import Optional, Dict, Any, List
|
|
12
|
+
|
|
13
|
+
# Add parent to path
|
|
14
|
+
|
|
15
|
+
from quantum.core.parser import QuantumParser
|
|
16
|
+
from quantum.core.ast_nodes import QuantumNode
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
@dataclass
|
|
20
|
+
class CompilationResult:
|
|
21
|
+
"""Result of a compilation."""
|
|
22
|
+
success: bool
|
|
23
|
+
code: str
|
|
24
|
+
source_file: Optional[str] = None
|
|
25
|
+
target: str = 'python'
|
|
26
|
+
errors: List[str] = None
|
|
27
|
+
warnings: List[str] = None
|
|
28
|
+
sourcemap: Optional[str] = None
|
|
29
|
+
stats: Dict[str, Any] = None
|
|
30
|
+
|
|
31
|
+
def __post_init__(self):
|
|
32
|
+
if self.errors is None:
|
|
33
|
+
self.errors = []
|
|
34
|
+
if self.warnings is None:
|
|
35
|
+
self.warnings = []
|
|
36
|
+
if self.stats is None:
|
|
37
|
+
self.stats = {}
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
class Transpiler:
|
|
41
|
+
"""
|
|
42
|
+
Main transpiler class.
|
|
43
|
+
|
|
44
|
+
Orchestrates parsing and code generation for Quantum files.
|
|
45
|
+
|
|
46
|
+
Usage:
|
|
47
|
+
transpiler = Transpiler(target='python')
|
|
48
|
+
result = transpiler.compile_file('app.q')
|
|
49
|
+
print(result.code)
|
|
50
|
+
|
|
51
|
+
# Or from string
|
|
52
|
+
result = transpiler.compile_string('<q:component>...</q:component>')
|
|
53
|
+
"""
|
|
54
|
+
|
|
55
|
+
def __init__(
|
|
56
|
+
self,
|
|
57
|
+
target: str = 'python',
|
|
58
|
+
optimize: bool = False,
|
|
59
|
+
sourcemap: bool = False,
|
|
60
|
+
strict: bool = False
|
|
61
|
+
):
|
|
62
|
+
"""
|
|
63
|
+
Initialize the transpiler.
|
|
64
|
+
|
|
65
|
+
Args:
|
|
66
|
+
target: Target language ('python' or 'javascript')
|
|
67
|
+
optimize: Enable optimizations
|
|
68
|
+
sourcemap: Generate source maps
|
|
69
|
+
strict: Strict mode (treat warnings as errors)
|
|
70
|
+
"""
|
|
71
|
+
self.target = target.lower()
|
|
72
|
+
self.optimize = optimize
|
|
73
|
+
self.sourcemap = sourcemap
|
|
74
|
+
self.strict = strict
|
|
75
|
+
self.parser = QuantumParser()
|
|
76
|
+
self._generator = None
|
|
77
|
+
|
|
78
|
+
def _get_generator(self):
|
|
79
|
+
"""Get the code generator for current target."""
|
|
80
|
+
if self._generator is None:
|
|
81
|
+
if self.target == 'python':
|
|
82
|
+
from quantum.compiler.python.generator import PythonGenerator
|
|
83
|
+
self._generator = PythonGenerator()
|
|
84
|
+
elif self.target == 'javascript':
|
|
85
|
+
from quantum.compiler.javascript.generator import JavaScriptGenerator
|
|
86
|
+
self._generator = JavaScriptGenerator()
|
|
87
|
+
else:
|
|
88
|
+
raise ValueError(f"Unknown target: {self.target}")
|
|
89
|
+
return self._generator
|
|
90
|
+
|
|
91
|
+
def compile_file(self, filepath: str) -> CompilationResult:
|
|
92
|
+
"""
|
|
93
|
+
Compile a Quantum file.
|
|
94
|
+
|
|
95
|
+
Args:
|
|
96
|
+
filepath: Path to the .q file
|
|
97
|
+
|
|
98
|
+
Returns:
|
|
99
|
+
CompilationResult with generated code
|
|
100
|
+
"""
|
|
101
|
+
import time
|
|
102
|
+
start_time = time.perf_counter()
|
|
103
|
+
|
|
104
|
+
path = Path(filepath)
|
|
105
|
+
if not path.exists():
|
|
106
|
+
return CompilationResult(
|
|
107
|
+
success=False,
|
|
108
|
+
code='',
|
|
109
|
+
source_file=str(path),
|
|
110
|
+
target=self.target,
|
|
111
|
+
errors=[f"File not found: {filepath}"]
|
|
112
|
+
)
|
|
113
|
+
|
|
114
|
+
try:
|
|
115
|
+
source = path.read_text(encoding='utf-8')
|
|
116
|
+
result = self.compile_string(source, source_file=str(path))
|
|
117
|
+
result.stats['compile_time_ms'] = (time.perf_counter() - start_time) * 1000
|
|
118
|
+
return result
|
|
119
|
+
|
|
120
|
+
except Exception as e:
|
|
121
|
+
return CompilationResult(
|
|
122
|
+
success=False,
|
|
123
|
+
code='',
|
|
124
|
+
source_file=str(path),
|
|
125
|
+
target=self.target,
|
|
126
|
+
errors=[f"Error reading file: {e}"]
|
|
127
|
+
)
|
|
128
|
+
|
|
129
|
+
def compile_string(self, source: str, source_file: str = '<string>') -> CompilationResult:
|
|
130
|
+
"""
|
|
131
|
+
Compile Quantum source code from string.
|
|
132
|
+
|
|
133
|
+
Args:
|
|
134
|
+
source: Quantum source code
|
|
135
|
+
source_file: Optional source file name for error messages
|
|
136
|
+
|
|
137
|
+
Returns:
|
|
138
|
+
CompilationResult with generated code
|
|
139
|
+
"""
|
|
140
|
+
import time
|
|
141
|
+
start_time = time.perf_counter()
|
|
142
|
+
|
|
143
|
+
errors = []
|
|
144
|
+
warnings = []
|
|
145
|
+
|
|
146
|
+
# Parse
|
|
147
|
+
try:
|
|
148
|
+
ast = self.parser.parse(source)
|
|
149
|
+
except Exception as e:
|
|
150
|
+
return CompilationResult(
|
|
151
|
+
success=False,
|
|
152
|
+
code='',
|
|
153
|
+
source_file=source_file,
|
|
154
|
+
target=self.target,
|
|
155
|
+
errors=[f"Parse error: {e}"]
|
|
156
|
+
)
|
|
157
|
+
|
|
158
|
+
if ast is None:
|
|
159
|
+
return CompilationResult(
|
|
160
|
+
success=False,
|
|
161
|
+
code='',
|
|
162
|
+
source_file=source_file,
|
|
163
|
+
target=self.target,
|
|
164
|
+
errors=["Parse returned None"]
|
|
165
|
+
)
|
|
166
|
+
|
|
167
|
+
# Optimize (if enabled)
|
|
168
|
+
if self.optimize:
|
|
169
|
+
try:
|
|
170
|
+
ast = self._optimize(ast)
|
|
171
|
+
except Exception as e:
|
|
172
|
+
warnings.append(f"Optimization warning: {e}")
|
|
173
|
+
|
|
174
|
+
# Generate code
|
|
175
|
+
try:
|
|
176
|
+
generator = self._get_generator()
|
|
177
|
+
code = generator.generate(ast)
|
|
178
|
+
except Exception as e:
|
|
179
|
+
return CompilationResult(
|
|
180
|
+
success=False,
|
|
181
|
+
code='',
|
|
182
|
+
source_file=source_file,
|
|
183
|
+
target=self.target,
|
|
184
|
+
errors=[f"Code generation error: {e}"]
|
|
185
|
+
)
|
|
186
|
+
|
|
187
|
+
# Post-generation optimization (if enabled)
|
|
188
|
+
if self.optimize:
|
|
189
|
+
try:
|
|
190
|
+
from quantum.compiler.optimizer import CodeOptimizer
|
|
191
|
+
optimizer = CodeOptimizer(target=self.target, level=2)
|
|
192
|
+
code = optimizer.optimize(code)
|
|
193
|
+
opt_stats = optimizer.get_stats()
|
|
194
|
+
warnings.append(f"Optimized: {sum(opt_stats.values())} transformations")
|
|
195
|
+
except Exception as e:
|
|
196
|
+
warnings.append(f"Post-optimization warning: {e}")
|
|
197
|
+
|
|
198
|
+
# Generate source map (if enabled)
|
|
199
|
+
sourcemap = None
|
|
200
|
+
if self.sourcemap:
|
|
201
|
+
try:
|
|
202
|
+
sourcemap = self._generate_sourcemap(ast, code, source_file)
|
|
203
|
+
except Exception as e:
|
|
204
|
+
warnings.append(f"Source map warning: {e}")
|
|
205
|
+
|
|
206
|
+
# Build stats
|
|
207
|
+
stats = {
|
|
208
|
+
'parse_time_ms': 0, # TODO: measure separately
|
|
209
|
+
'generate_time_ms': (time.perf_counter() - start_time) * 1000,
|
|
210
|
+
'source_lines': source.count('\n') + 1,
|
|
211
|
+
'output_lines': code.count('\n') + 1,
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
# Check strict mode
|
|
215
|
+
if self.strict and warnings:
|
|
216
|
+
errors.extend(warnings)
|
|
217
|
+
warnings = []
|
|
218
|
+
return CompilationResult(
|
|
219
|
+
success=False,
|
|
220
|
+
code=code,
|
|
221
|
+
source_file=source_file,
|
|
222
|
+
target=self.target,
|
|
223
|
+
errors=errors,
|
|
224
|
+
warnings=[],
|
|
225
|
+
stats=stats
|
|
226
|
+
)
|
|
227
|
+
|
|
228
|
+
return CompilationResult(
|
|
229
|
+
success=True,
|
|
230
|
+
code=code,
|
|
231
|
+
source_file=source_file,
|
|
232
|
+
target=self.target,
|
|
233
|
+
errors=errors,
|
|
234
|
+
warnings=warnings,
|
|
235
|
+
sourcemap=sourcemap,
|
|
236
|
+
stats=stats
|
|
237
|
+
)
|
|
238
|
+
|
|
239
|
+
def _optimize(self, ast: QuantumNode) -> QuantumNode:
|
|
240
|
+
"""Apply optimizations to AST."""
|
|
241
|
+
# TODO: Implement optimizations
|
|
242
|
+
# - Constant folding
|
|
243
|
+
# - Dead code elimination
|
|
244
|
+
# - Expression simplification
|
|
245
|
+
return ast
|
|
246
|
+
|
|
247
|
+
def _generate_sourcemap(self, ast: QuantumNode, code: str, source_file: str) -> str:
|
|
248
|
+
"""Generate source map."""
|
|
249
|
+
# TODO: Implement source map generation
|
|
250
|
+
# For now, return empty
|
|
251
|
+
return ''
|
|
252
|
+
|
|
253
|
+
def compile_directory(
|
|
254
|
+
self,
|
|
255
|
+
source_dir: str,
|
|
256
|
+
output_dir: str,
|
|
257
|
+
recursive: bool = True
|
|
258
|
+
) -> Dict[str, CompilationResult]:
|
|
259
|
+
"""
|
|
260
|
+
Compile all Quantum files in a directory.
|
|
261
|
+
|
|
262
|
+
Args:
|
|
263
|
+
source_dir: Source directory containing .q files
|
|
264
|
+
output_dir: Output directory for generated files
|
|
265
|
+
recursive: Process subdirectories
|
|
266
|
+
|
|
267
|
+
Returns:
|
|
268
|
+
Dictionary mapping source files to compilation results
|
|
269
|
+
"""
|
|
270
|
+
source_path = Path(source_dir)
|
|
271
|
+
output_path = Path(output_dir)
|
|
272
|
+
output_path.mkdir(parents=True, exist_ok=True)
|
|
273
|
+
|
|
274
|
+
results = {}
|
|
275
|
+
pattern = '**/*.q' if recursive else '*.q'
|
|
276
|
+
|
|
277
|
+
for q_file in source_path.glob(pattern):
|
|
278
|
+
rel_path = q_file.relative_to(source_path)
|
|
279
|
+
|
|
280
|
+
# Determine output file
|
|
281
|
+
ext = '.py' if self.target == 'python' else '.js'
|
|
282
|
+
out_file = output_path / rel_path.with_suffix(ext)
|
|
283
|
+
out_file.parent.mkdir(parents=True, exist_ok=True)
|
|
284
|
+
|
|
285
|
+
# Compile
|
|
286
|
+
result = self.compile_file(str(q_file))
|
|
287
|
+
results[str(q_file)] = result
|
|
288
|
+
|
|
289
|
+
# Write output
|
|
290
|
+
if result.success:
|
|
291
|
+
out_file.write_text(result.code, encoding='utf-8')
|
|
292
|
+
|
|
293
|
+
# Write source map
|
|
294
|
+
if result.sourcemap:
|
|
295
|
+
map_file = out_file.with_suffix(ext + '.map')
|
|
296
|
+
map_file.write_text(result.sourcemap, encoding='utf-8')
|
|
297
|
+
|
|
298
|
+
return results
|
|
299
|
+
|
|
300
|
+
|
|
301
|
+
def compile_file(filepath: str, target: str = 'python', **kwargs) -> CompilationResult:
|
|
302
|
+
"""
|
|
303
|
+
Convenience function to compile a single file.
|
|
304
|
+
|
|
305
|
+
Args:
|
|
306
|
+
filepath: Path to .q file
|
|
307
|
+
target: Target language
|
|
308
|
+
**kwargs: Additional transpiler options
|
|
309
|
+
|
|
310
|
+
Returns:
|
|
311
|
+
CompilationResult
|
|
312
|
+
"""
|
|
313
|
+
transpiler = Transpiler(target=target, **kwargs)
|
|
314
|
+
return transpiler.compile_file(filepath)
|
|
315
|
+
|
|
316
|
+
|
|
317
|
+
def compile_string(source: str, target: str = 'python', **kwargs) -> CompilationResult:
|
|
318
|
+
"""
|
|
319
|
+
Convenience function to compile source string.
|
|
320
|
+
|
|
321
|
+
Args:
|
|
322
|
+
source: Quantum source code
|
|
323
|
+
target: Target language
|
|
324
|
+
**kwargs: Additional transpiler options
|
|
325
|
+
|
|
326
|
+
Returns:
|
|
327
|
+
CompilationResult
|
|
328
|
+
"""
|
|
329
|
+
transpiler = Transpiler(target=target, **kwargs)
|
|
330
|
+
return transpiler.compile_string(source)
|
quantum/core/__init__.py
ADDED