-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path02_polyglot.py
More file actions
159 lines (119 loc) · 6.2 KB
/
02_polyglot.py
File metadata and controls
159 lines (119 loc) · 6.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#!/usr/bin/env python3
"""FLUX Polyglot — Compiling mixed C + Python from a FLUX.MD document.
This example shows how FLUX.MD lets you mix C and Python in a single Markdown
document, then compile and run the embedded code blocks through the pipeline.
Run:
PYTHONPATH=src python3 examples/02_polyglot.py
"""
from __future__ import annotations
import os
# ── ANSI helpers ──────────────────────────────────────────────────────────
CYAN = "\033[96m"
GREEN = "\033[92m"
YELLOW = "\033[93m"
MAGENTA = "\033[95m"
BOLD = "\033[1m"
DIM = "\033[2m"
RESET = "\033[0m"
def header(text: str) -> None:
width = 64
print()
print(f"{BOLD}{MAGENTA}{'═' * width}{RESET}")
print(f"{BOLD}{MAGENTA} {text}{RESET}")
print(f"{BOLD}{MAGENTA}{'═' * width}{RESET}")
def info(text: str) -> None:
print(f" {GREEN}✓{RESET} {text}")
def detail(text: str) -> None:
print(f" {DIM}{text}{RESET}")
def section(text: str) -> None:
print()
print(f"{BOLD}{CYAN}── {text} {'─' * (56 - len(text))}{RESET}")
# ══════════════════════════════════════════════════════════════════════════
# Main
# ══════════════════════════════════════════════════════════════════════════
if __name__ == "__main__":
print()
print(f"{BOLD}{YELLOW}{'╔' + '═' * 62 + '╗'}{RESET}")
print(f"{BOLD}{YELLOW}{'║'} FLUX Polyglot — Mixed C + Python Compilation {'║'}{RESET}")
print(f"{BOLD}{YELLOW}{'║'} One Markdown document, multiple languages {'║'}{RESET}")
print(f"{BOLD}{YELLOW}{'╚' + '═' * 62 + '╝'}{RESET}")
# ── Load the FLUX.MD document ──────────────────────────────────────
md_path = os.path.join(os.path.dirname(__file__), "02_polyglot.md")
with open(md_path, "r") as f:
md_source = f.read()
header("FLUX.MD Source Document")
print(f"{DIM}{md_source}{RESET}")
# ── Parse the FLUX.MD document ─────────────────────────────────────
section("Step 1: Parse FLUX.MD")
try:
from flux.parser import FluxMDParser
from flux.parser.nodes import NativeBlock
parser = FluxMDParser()
doc = parser.parse(md_source)
info(f"Parsed document: {len(doc.children)} top-level children")
code_blocks = []
for child in doc.children:
if isinstance(child, NativeBlock):
lang = (child.lang or "").lower().strip()
code_blocks.append((lang, child.content))
info(f" Found {lang.upper()} block ({len(child.content)} chars)")
except ImportError as e:
print(f" {YELLOW}⚠{RESET} Parser import failed: {e}")
code_blocks = []
except Exception as e:
print(f" {YELLOW}⚠{RESET} Parse error: {e}")
code_blocks = []
# ── Compile each code block ────────────────────────────────────────
section("Step 2: Compile Each Code Block")
if code_blocks:
try:
from flux.compiler.pipeline import FluxCompiler
compiler = FluxCompiler()
for lang, content in code_blocks:
info(f"Compiling {lang.upper()} block...")
try:
if lang == "c":
bytecode = compiler.compile_c(content, module_name="polyglot_c")
elif lang == "python":
bytecode = compiler.compile_python(content, module_name="polyglot_py")
else:
detail(f" Skipping unsupported language: {lang}")
continue
info(f" {lang.upper()} → {len(bytecode)} bytes of FLUX bytecode")
detail(f" Magic: {bytecode[:4]}")
except Exception as e:
detail(f" Compilation error: {e}")
except ImportError as e:
print(f" {YELLOW}⚠{RESET} Compiler import failed: {e}")
# ── Run through full pipeline ──────────────────────────────────────
section("Step 3: Run Through Full Pipeline")
try:
from flux.pipeline.e2e import FluxPipeline
# Try the C code block
c_code = code_blocks[0][1] if code_blocks else "int main() { return 42; }"
detail(f"Compiling C source through FLUX.MD pipeline...")
pipeline = FluxPipeline(optimize=True, execute=True)
result = pipeline.run(md_source, lang="md", module_name="polyglot_full")
if result.success:
info("Pipeline completed successfully!")
info(f" Cycles: {result.cycles}")
info(f" Halted: {result.halted}")
else:
info("Pipeline completed with notes:")
for err in result.errors:
detail(f" {err}")
if result.bytecode:
info(f" Bytecode: {len(result.bytecode)} bytes")
except Exception as e:
print(f" {YELLOW}⚠{RESET} Pipeline error: {e}")
# ── Show the beauty of polyglot ────────────────────────────────────
section("Summary: What Makes FLUX.MD Special")
info("FLUX.MD is a literate programming format that:")
detail(" • Embeds C, Python, and more in natural Markdown documents")
detail(" • Compiles each code block through language-specific frontends")
detail(" • Produces unified FLUX bytecode from mixed-language sources")
detail(" • Lets you document AND execute in the same file")
detail(" • Perfect for polyglot signal processing, ML pipelines, etc.")
print()
print(f"{BOLD}{GREEN}── Done! ──{RESET}")
print()