Author: chromatic
Date: Wed Apr 19 19:08:21 2006
New Revision: 12384
Modified:
trunk/languages/pheme/lib/PhemeCompiler.pir
Log:
Minor refactorings to enable larger ones.
I'd like to share a LanguageCompiler object between many languages, varying
only the name of the language and tree grammar files. This is step one.
Modified: trunk/languages/pheme/lib/PhemeCompiler.pir
==============================================================================
--- trunk/languages/pheme/lib/PhemeCompiler.pir (original)
+++ trunk/languages/pheme/lib/PhemeCompiler.pir Wed Apr 19 19:08:21 2006
@@ -6,97 +6,145 @@
.sub compile_pheme
.param pmc source
- .local pmc match
+ .local pmc parse_tree
+ parse_tree = get_parse_tree( source )
+ unless parse_tree goto err_parse_fail
+
+ # dump_parse_tree( parse_tree )
+
+ .local pmc past_tree
+ past_tree = get_past_tree( parse_tree )
+ unless past_tree goto err_past_fail
+
+ # dump_it( past_tree )
+
+ .local pmc post_tree
+ post_tree = get_post_tree( past_tree )
+ unless post_tree goto err_post_fail
+
+ # dump_it( post )
+
+ .local pmc pir
+ pir = get_pir( post_tree )
+ unless pir goto err_pir_fail
+
+ .local pmc pir_compiled
+ pir_compiled = compile_pir( pir )
+ unless pir_compiled goto err_no_pir_compiled
+
+ pir_compiled()
+ end
+
+ err_parse_fail:
+ print "Parse failed\n"
+
+ err_past_fail:
+ print "Unable to construct AST.\n"
+
+ err_post_fail:
+ print "Unable to construct OST.\n"
+
+ err_pir_fail:
+ print "Unable to construct PIR.\n"
+
+ err_no_pir_compiled:
+ print "Unable to compile PIR.\n"
+
+ cleanup:
+ .return()
+.end
+
+.sub get_parse_tree
+ .param pmc source
+
.local pmc start_rule
.local pmc newfrom
+ .local pmc match
+
start_rule = find_global 'Pheme::Grammar', 'prog'
newfrom = find_global 'PGE::Match', 'newfrom'
source = newfrom(source, 0, 'Pheme::Grammar')
match = start_rule( source )
- # dump_match( match )
+
+ .return( match )
+.end
+
+.sub get_past_tree
+ .param pmc parse_tree
+
+ load_bytecode 'TGE.pbc'
+ load_bytecode 'lib/PAST.pbc'
.local pmc compiled_symbols
compiled_symbols = new .Hash
- store_global 'PhemeCompiler', 'symbols', compiled_symbols
- unless match goto err_match_fail
+ store_global 'PhemeCompiler', 'symbols', compiled_symbols
.local string tg_source
tg_source = _slurp_file( 'lib/pge2past.tg' )
- load_bytecode 'TGE.pbc'
.local pmc tree_grammar
tree_grammar = new 'TGE'
tree_grammar.agcompile( tg_source )
.local pmc ast_builder
- ast_builder = tree_grammar.apply( match )
+ ast_builder = tree_grammar.apply( parse_tree )
- load_bytecode 'lib/PAST.pbc'
- .local pmc ast
- ast = ast_builder.get( 'result' )
- $I0 = defined ast
- unless $I0 goto err_no_ast
+ .local pmc past_tree
+ past_tree = ast_builder.get( 'result' )
+ .return( past_tree )
+.end
- # dump_it( ast )
+.sub get_post_tree
+ .param pmc past_tree
+
+ load_bytecode 'lib/POST.pbc'
+ load_bytecode 'lib/PhemeSymbols.pbc'
+ .local string tg_source
tg_source = _slurp_file( 'lib/past2post.tg' )
+
.local pmc post_grammar
post_grammar = new 'TGE'
post_grammar.agcompile( tg_source )
.local pmc post_builder
- post_builder = post_grammar.apply( ast )
+ post_builder = post_grammar.apply( past_tree )
- load_bytecode 'lib/POST.pbc'
- load_bytecode 'lib/PhemeSymbols.pbc'
- .local pmc post
- post = post_builder.get( 'result' )
- $I0 = defined post
- unless $I0 goto err_no_post
+ .local pmc post_tree
+ post_tree = post_builder.get( 'result' )
+ .return( post_tree )
+.end
- # dump_it( post )
+.sub get_pir
+ .param pmc post_tree
+ .local string tg_source
tg_source = _slurp_file( 'lib/post2pir.tg' )
+
.local pmc pir_grammar
pir_grammar = new 'TGE'
pir_grammar.agcompile( tg_source )
.local pmc pir_builder
- pir_builder = pir_grammar.apply( post )
+ pir_builder = pir_grammar.apply( post_tree )
.local pmc pir
pir = pir_builder.get( 'result' )
- $I0 = defined pir
- unless $I0 goto err_no_pir
+ .return( pir )
+.end
+
+.sub compile_pir
+ .param pmc pir
.local pmc pir_compiler
.local pmc pir_compiled
pir_compiler = compreg "PIR"
pir_compiled = pir_compiler( pir )
-
- pir_compiled()
-
- end
-
-
- err_match_fail:
- print "Parse failed\n"
-
- err_no_ast:
- print "Unable to construct AST.\n"
-
- err_no_post:
- print "Unable to construct OST.\n"
-
- err_no_pir:
- print "Unable to construct PIR.\n"
-
- cleanup:
- .return()
+ .return( pir_compiled )
.end
-.sub dump_match
+.sub dump_parse
.param pmc match
load_bytecode 'dumper.pbc'
load_bytecode 'PGE/Dumper.pbc'