Author: fperrad
Date: Mon Jun  4 22:53:53 2007
New Revision: 18820

Modified:
   trunk/languages/lua/src/PASTGrammar.tg
   trunk/languages/lua/src/POSTGrammar.tg
   trunk/languages/lua/src/lua51.pir

Log:
[Lua]
- add scope to symbol table
- improvement assignment

Modified: trunk/languages/lua/src/PASTGrammar.tg
==============================================================================
--- trunk/languages/lua/src/PASTGrammar.tg      (original)
+++ trunk/languages/lua/src/PASTGrammar.tg      Mon Jun  4 22:53:53 2007
@@ -17,6 +17,7 @@
 transform past (ROOT) :language('PIR') {
     .local pmc past
     $P0 = new 'Lua::Symbtab'
+    $P0.'push_scope'()
     set_hll_global ['Lua::PAST'], '$symbtab', $P0
     $P0 = node['block']
     past = tree.'get'('past', $P0, 'Lua::Grammar::block')
@@ -45,6 +46,9 @@
     $P1 = tree.'get'('past', $P0, 'Lua::Grammar::statement')
     past.'push'($P1)
   L3:
+    .local pmc symbtab
+    symbtab = get_hll_global ['Lua::PAST'], '$symbtab'
+    symbtab.'pop_scope'()
     .return (past)
 }
 
@@ -61,6 +65,9 @@
     .local pmc past, cpast, expr, blk
     $P0 = node['expression']
     expr = tree.'get'('past', $P0, 'Lua::Grammar::expression')
+    .local pmc symbtab
+    symbtab = get_hll_global ['Lua::PAST'], '$symbtab'
+    symbtab.'push_scope'()
     $P0 = node['block']
     blk = tree.'get'('past', $P0, 'Lua::Grammar::block')
     past = expr.'new'('PAST::Op', expr, blk, 'node'=>node, 'pasttype'=>'if')
@@ -79,8 +86,10 @@
   L1:
     $P0 = node['else_block']
     if null $P0 goto L3
+    symbtab.'push_scope'()
     $P0 = shift $P0
-    $P1 = tree.'get'('past', $P0, 'Lua::Grammar::else_block')
+    $P0 = $P0['block']
+    $P1 = tree.'get'('past', $P0, 'Lua::Grammar::block')
     cpast.'push'($P1)
   L3:
     .return (past)
@@ -91,24 +100,22 @@
     .local pmc expr, blk
     $P0 = node['expression']
     expr = tree.'get'('past', $P0, 'Lua::Grammar::expression')
+    .local pmc symbtab
+    symbtab = get_hll_global ['Lua::PAST'], '$symbtab'
+    symbtab.'push_scope'()
     $P0 = node['block']
     blk = tree.'get'('past', $P0, 'Lua::Grammar::block')
     .return expr.'new'('PAST::Op', expr, blk, 'node'=>node, 'pasttype'=>'if')
 }
 
 
-transform past (Lua::Grammar::else_block) :language('PIR') {
-    $S0 = node.'find_key'()
-    $P0 = node[$S0]
-    $S1 = concat 'Lua::Grammar::', $S0
-    .return tree.'get'('past', $P0, $S1)
-}
-
-
 transform past (Lua::Grammar::while_stat) :language('PIR') {
     .local pmc expr, blk
     $P0 = node['expression']
     expr = tree.'get'('past', $P0, 'Lua::Grammar::expression')
+    .local pmc symbtab
+    symbtab = get_hll_global ['Lua::PAST'], '$symbtab'
+    symbtab.'push_scope'()
     $P0 = node['block']
     blk = tree.'get'('past', $P0, 'Lua::Grammar::block')
     .return expr.'new'('PAST::Op', expr, blk, 'node'=>node, 
'pasttype'=>'while')
@@ -117,11 +124,14 @@
 
 transform past (Lua::Grammar::repeat_stat) :language('PIR') {
     .local pmc expr, blk
-    $P0 = node['expression']
-    expr = tree.'get'('past', $P0, 'Lua::Grammar::expression')
+    .local pmc symbtab
+    symbtab = get_hll_global ['Lua::PAST'], '$symbtab'
+    symbtab.'push_scope'()
     $P0 = node['block']
     blk = tree.'get'('past', $P0, 'Lua::Grammar::block')
-    .return expr.'new'('PAST::Op', expr, blk, 'node'=>node, 
'pasttype'=>'repeat')
+    $P0 = node['expression']
+    expr = tree.'get'('past', $P0, 'Lua::Grammar::expression')
+    .return expr.'new'('PAST::Op', blk, expr, 'node'=>node, 
'pasttype'=>'repeat')
 }
 
 
@@ -143,6 +153,7 @@
     .local pmc name, explist
     .local pmc symbtab
     symbtab = get_hll_global ['Lua::PAST'], '$symbtab'
+    symbtab.'push_scope'()
     $P0 = node['name']
     $S0 = $P0.'result_object'()
     symbtab.'insert'($S0)
@@ -167,6 +178,7 @@
     .local pmc past, namelist, explist
     .local pmc symbtab
     symbtab = get_hll_global ['Lua::PAST'], '$symbtab'
+    symbtab.'push_scope'()
     namelist = new .ResizablePMCArray
     $P0 = node['name_list']
     $P0 = $P0['name']
@@ -347,9 +359,6 @@
     $S0 = $P0.'result_object'()
     $I0 = index $S0, ':'
     if $I0 < 0 goto L1
-    .local pmc symbtab
-    symbtab = get_hll_global ['Lua::PAST'], '$symbtab'
-    symbtab.'insert'('self')
     past = new 'PAST::Var'
     past.'init'('node'=>$P0, 'name'=>'self', 'scope'=>'parameter')
     set_hll_global ['Lua::PAST'], '$?self', past
@@ -386,12 +395,14 @@
     .local pmc past, parlist, var
     .local pmc symbtab
     symbtab = get_hll_global ['Lua::PAST'], '$symbtab'
+    symbtab.'push_scope'()
     parlist = new .ResizablePMCArray
     var = get_hll_global ['Lua::PAST'], '$?self'
     if null var goto L1
     push parlist, var
     null var
     set_hll_global ['Lua::PAST'], '$?self', var
+    symbtab.'insert'('self')
   L1:
     $P0 = node['parameter_list']
     if null $P0 goto L2
@@ -420,8 +431,7 @@
   L2:
     $P0 = node['block']
     past = tree.'get'('past', $P0, 'Lua::Grammar::block')
-    $P0 = new 'Lua::Symbtab'
-    .return past.'new'('PAST::Block', parlist :flat, past, 'node'=>node, 
'symtable'=>$P0, 'pragma'=>':anon :lex')
+    .return past.'new'('PAST::Block', parlist :flat, past, 'node'=>node, 
'pragma'=>':anon :lex')
 }
 
 

Modified: trunk/languages/lua/src/POSTGrammar.tg
==============================================================================
--- trunk/languages/lua/src/POSTGrammar.tg      (original)
+++ trunk/languages/lua/src/POSTGrammar.tg      Mon Jun  4 22:53:53 2007
@@ -291,10 +291,10 @@
     $S0 = concat $S0, '_end'
     endlabel = ops.'new'('POST::Label', 'result'=>$S0)
     ops.'push'(looplabel)
-    $P0 = node[1]
+    $P0 = node[0]
     blk = tree.'get'('post', $P0)
     ops.'push'(blk)
-    $P0 = node[0]
+    $P0 = node[1]
     expr = tree.'get'('post', $P0)
     ops.'push'(expr)
     ops.'push_pirop'('if', expr, endlabel)
@@ -407,25 +407,35 @@
     ops.'push_pirop'('set', lpost, rpost)
     .return (ops)
   L1:
-    .local pmc iter
-    new iter, .Iterator, namelist
+    .local pmc iter, tmp
+    new tmp, .ResizablePMCArray
+    new iter, .Iterator, explist
   L4:
     unless iter goto L5
     $P0 = shift iter
-    unless explist goto L6
-    $P1 = shift explist
-    rpost = tree.'get'('post', $P1)
+    rpost = tree.'get'('post', $P0)
     ops.'push'(rpost)
-    goto L7
+    $P1 = ops.'push_pirop'('set')
+    $P1.'arglist'($P1, rpost)
+    push tmp, $P1
+    goto L4
+  L5:
+    new iter, .Iterator, namelist
   L6:
+    unless iter goto L7
+    $P0 = shift iter
+    unless tmp goto L8
+    rpost = shift tmp
+    goto L9
+  L8:
     rpost = ops.'push_pirop'('new')
     rpost.'arglist'(rpost, '.LuaNil')
-  L7:
+  L9:
     lpost = tree.'get'('post', $P0)
     ops.'push'(lpost)
     ops.'push_pirop'('set', lpost, rpost)
-    goto L4
-  L5:
+    goto L6
+  L7:
     .return (ops)
 }
 

Modified: trunk/languages/lua/src/lua51.pir
==============================================================================
--- trunk/languages/lua/src/lua51.pir   (original)
+++ trunk/languages/lua/src/lua51.pir   Mon Jun  4 22:53:53 2007
@@ -734,18 +734,36 @@
 .namespace [ "Lua::Symbtab" ]
 
 .sub '__onload' :load :init
-    $P0 = subclass 'Hash', 'Lua::Symbtab'
+    $P0 = subclass 'ResizablePMCArray', 'Lua::Symbtab'
 .end
 
 .sub 'insert' :method
     .param string name
-    self[name] = 1
+    $P0 = self[0]
+    $P0[name] = 1
 .end
 
 .sub 'lookup' :method
     .param string name
-    $I0 = exists self[name]
-    .return ($I0)
+    .local pmc iter
+    new iter, .Iterator, self
+  L1:
+    unless iter goto L2
+    $P0 = shift iter
+    $I0 = exists $P0[name]
+    unless $I0 goto L1
+    .return (1)
+  L2:
+    .return (0)
+.end
+
+.sub 'push_scope' :method
+    new $P0, .Hash
+    unshift self, $P0
+.end
+
+.sub 'pop_scope' :method
+    $P0 = shift self
 .end
 
 .include 'languages/lua/src/lua51_gen.pir'

Reply via email to