diff options
author | Adrian Thurston <thurston@colm.net> | 2019-12-27 16:04:09 +0200 |
---|---|---|
committer | Adrian Thurston <thurston@colm.net> | 2019-12-27 16:08:58 +0200 |
commit | 2589d8bc96cced6519a09dba1996a021cd2cf34d (patch) | |
tree | 866256898394789768d50d79b04988f04f7473da | |
parent | 7c474afdd7d5ef23f1a6ea99a54db72ae86f331c (diff) | |
download | colm-2589d8bc96cced6519a09dba1996a021cd2cf34d.tar.gz |
colm rewriting: newly generated prods should go above where they are created
Redid the sublist implementation in colm such that the new produciton is placed
immediately above the place where it it was first referenced. Works better in
the face of of namesapce usage
Also removed the repeat implementations. These are already correct in the C++
program so no sense in redoing them. This is in line with the notion that we
can use colm transformations for new language features, before they are
eventually moved into the main program.
-rw-r--r-- | colm/prog.lm | 88 |
1 files changed, 48 insertions, 40 deletions
diff --git a/colm/prog.lm b/colm/prog.lm index 635f0d92..f024228f 100644 --- a/colm/prog.lm +++ b/colm/prog.lm @@ -7,10 +7,6 @@ A: str = argv->pop() F: stream = open( A, 'r' ) parse P: start [ F ] -def new_roots - [new_roots root_item] -| [] - prod_list cons_prod( SLA: prod_sublist ) { if match SLA [Left: prod_sublist BAR prod_el_list] @@ -19,58 +15,70 @@ prod_list cons_prod( SLA: prod_sublist ) return cons prod_list[ '[ ' SLA.prod_el_list ' ]' ] } -void rewrite( P: ref<start> ) -{ - Modified: bool = false - cons Additional: new_roots [] +alias prod_map map<prod_list, id> +alias unique_prod map_el<prod_list, id> - for PE: prod_el in P { - if match PE - [OptName: opt_prod_el_name POPEN PS: prod_sublist PCLOSE OptRep: opt_repeat] +global PM: prod_map = new prod_map() +global NextId: int = 1 +global Modified: bool = false + +cfl_def rewrite_cfl_def( CflDef: ref<cfl_def> ) +{ + NewDef: cfl_def + for PE: prod_el in CflDef { + if match PE [ + OptName: opt_prod_el_name POPEN PS: prod_sublist PCLOSE OptRep: opt_repeat] { - PE = cons prod_el - [OptName "_sublist_" OptRep] + PL: prod_list = cons_prod(PS) - cons Def: cfl_def - "def _sublist_ [cons_prod(PS)]" + Name: id = PM->find( PL ) + if ( !Name ) { + Name = parse id + "_sublist_[sprintf("%d", NextId)]" + NextId = NextId + 1 - Additional = cons new_roots - [Additional Def] - } -# else if match PE -# [OptName: opt_prod_el_name Qual: region_qual Id: id "<*"] -# { -# parse LeftRepId: id "_lrep_[Id]" -# PE = cons prod_el [OptName Qual LeftRepId] -# -# cons Def: cfl_def "def [LeftRepId] \[[LeftRepId] [Id]\] | \[\] -# -# Additional = cons new_roots [Additional Def] -# } - else if match PE - [OptName: opt_prod_el_name Qual: region_qual Id: id "<+"] - { - parse LeftRepId: id "_lrep_[Id]" + PM->insert( PL, Name ) - PE = cons prod_el [OptName Qual LeftRepId] + NewDef = cons cfl_def + "def [Name] [PL]" + } - cons Def: cfl_def "def [LeftRepId] \[[LeftRepId] [Id]\] | \[[Id]\] + PE = cons prod_el + [OptName Name OptRep " "] - Additional = cons new_roots [Additional Def] + Modified = true } } + return NewDef +} + +void rewrite( P: ref<start> ) +{ + Modified = false + + for RIL: root_item<* in P { + require RIL [Head: root_item<* CflDef: cfl_def] + + NewDef: cfl_def + + NewDef = rewrite_cfl_def( CflDef ) + + if NewDef { + RIL = cons root_item<* [Head NewDef "\n\n" CflDef] + Modified = true + } + else { + RIL = cons root_item<* [Head CflDef] + } - while ( Additional.root_item ) { - P = cons start [P.RootItemList Additional.root_item] - Additional = Additional._new_roots - Modified = true } + return Modified } if P { while ( rewrite( P ) ) {} - #rewrite( P ) + #print "final:\n[P] } ColmTree = P |