11//! ZynPEG grammar-based compilation format
22//!
3- //! Workflow (No Rust compilation required!):
3+ //! Two runtimes are supported:
4+ //!
5+ //! ## Grammar1 (Legacy)
46//! 1. Parse .zyn grammar with ZynPEG
57//! 2. Compile grammar to zpeg module (pest grammar + JSON commands)
68//! 3. Parse source code with pest_vm (dynamic grammar)
79//! 4. Execute JSON commands via host functions to build TypedAST
810//! 5. Lower TypedAST to HIR and compile
911//!
12+ //! ## Grammar2 (Default)
13+ //! 1. Parse .zyn grammar with ZynPEG to GrammarIR
14+ //! 2. Use GrammarInterpreter to parse source directly to TypedProgram
15+ //! 3. Lower TypedProgram to HIR and compile
16+ //!
1017//! Usage:
1118//! ```bash
12- //! zyntax compile --source my_code.lang --grammar my_lang.zyn --format zyn -o output --run
19+ //! # Default (Grammar2)
20+ //! zyntax compile --source my_code.lang --grammar my_lang.zyn --format zyn -o output --jit
21+ //!
22+ //! # Legacy (Grammar1)
23+ //! zyntax compile --source my_code.lang --grammar my_lang.zyn --format zyn --grammar1 -o output --jit
1324//! ```
1425
1526use colored:: Colorize ;
@@ -19,6 +30,7 @@ use std::sync::{Arc, Mutex};
1930use zyntax_compiler:: hir:: HirModule ;
2031use zyntax_compiler:: lowering:: { AstLowering , LoweringConfig , LoweringContext } ;
2132use zyntax_typed_ast:: { AstArena , InternedString , TypeRegistry , TypedProgram } ;
33+ use zyntax_embed:: Grammar2 ;
2234
2335/// Load and compile source using a ZynPEG grammar
2436pub fn load (
@@ -101,6 +113,84 @@ pub fn load(
101113 Ok ( hir_module)
102114}
103115
116+ /// Load and compile source using Grammar2 runtime (GrammarInterpreter)
117+ ///
118+ /// This is the new default runtime that uses named bindings and direct TypedAST construction.
119+ pub fn load_grammar2 (
120+ grammar_path : & PathBuf ,
121+ source_path : & PathBuf ,
122+ verbose : bool ,
123+ ) -> Result < HirModule , Box < dyn std:: error:: Error > > {
124+ if verbose {
125+ println ! ( "{} Grammar file: {:?}" , "info:" . blue( ) , grammar_path) ;
126+ println ! ( "{} Source file: {:?}" , "info:" . blue( ) , source_path) ;
127+ }
128+
129+ // Verify files exist
130+ if !grammar_path. exists ( ) {
131+ return Err ( format ! ( "Grammar file not found: {:?}" , grammar_path) . into ( ) ) ;
132+ }
133+ if !source_path. exists ( ) {
134+ return Err ( format ! ( "Source file not found: {:?}" , source_path) . into ( ) ) ;
135+ }
136+
137+ // Read grammar
138+ let grammar_code = std:: fs:: read_to_string ( grammar_path) ?;
139+ if verbose {
140+ println ! ( "{} Read {} bytes of grammar" , "info:" . blue( ) , grammar_code. len( ) ) ;
141+ }
142+
143+ // Read source code
144+ let source_code = std:: fs:: read_to_string ( source_path) ?;
145+ if verbose {
146+ println ! ( "{} Read {} bytes of source code" , "info:" . blue( ) , source_code. len( ) ) ;
147+ }
148+
149+ // Step 1: Compile grammar with Grammar2
150+ if verbose {
151+ println ! ( "{} Compiling grammar with Grammar2..." , "info:" . blue( ) ) ;
152+ }
153+ let grammar = Grammar2 :: from_source ( & grammar_code)
154+ . map_err ( |e| format ! ( "Failed to compile grammar: {}" , e) ) ?;
155+
156+ if verbose {
157+ println ! ( "{} Language: {} v{}" , "info:" . blue( ) , grammar. name( ) , grammar. version( ) ) ;
158+ }
159+
160+ // Step 2: Parse source to TypedProgram
161+ if verbose {
162+ println ! ( "{} Parsing source with Grammar2..." , "info:" . blue( ) ) ;
163+ }
164+ let source_file_name = source_path. to_string_lossy ( ) . to_string ( ) ;
165+ let mut typed_program = grammar. parse_with_filename ( & source_code, & source_file_name)
166+ . map_err ( |e| format ! ( "Failed to parse source: {}" , e) ) ?;
167+
168+ // Add the source file to the program for proper diagnostics
169+ use zyntax_typed_ast:: source:: SourceFile ;
170+ typed_program. source_files = vec ! [ SourceFile :: new( source_file_name. clone( ) , source_code. clone( ) ) ] ;
171+
172+ if verbose {
173+ println ! (
174+ "{} Parsed to TypedProgram with {} declarations" ,
175+ "info:" . blue( ) ,
176+ typed_program. declarations. len( )
177+ ) ;
178+ }
179+
180+ // Step 3: Lower TypedProgram to HIR
181+ let hir_module = lower_to_hir ( typed_program, verbose) ?;
182+
183+ if verbose {
184+ println ! (
185+ "{} Lowered to HIR with {} functions" ,
186+ "info:" . blue( ) ,
187+ hir_module. functions. len( )
188+ ) ;
189+ }
190+
191+ Ok ( hir_module)
192+ }
193+
104194/// Compile .zyn grammar to zpeg module
105195fn compile_grammar (
106196 grammar_code : & str ,
0 commit comments