Author: fperrad
Date: Wed May 30 00:10:27 2007
New Revision: 18684

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

Log:
[Lua]
- refactor table constructor (now in POST)
- implements len op as callmethod
- implements comparison op as builtins 

Modified: trunk/languages/lua/lib/luaaux.pir
==============================================================================
--- trunk/languages/lua/lib/luaaux.pir  (original)
+++ trunk/languages/lua/lib/luaaux.pir  Wed May 30 00:10:27 2007
@@ -562,6 +562,124 @@
 
 =back
 
+=head2 Builtins
+
+=over 4
+
+=item C<infix:==>
+
+=cut
+
+.sub 'infix:=='
+    .param pmc a
+    .param pmc b
+    $I0 = iseq a, b
+    new $P0, .LuaBoolean
+    set $P0, $I0
+    .return ($P0)
+.end
+
+
+=item C<infix:!=>
+
+=cut
+
+.sub 'infix:!='
+    .param pmc a
+    .param pmc b
+    $I0 = isne a, b
+    new $P0, .LuaBoolean
+    set $P0, $I0
+    .return ($P0)
+.end
+
+
+=item C<infix:E<lt>=>
+
+=cut
+
+.sub 'infix:E<gt>='
+    .param pmc a
+    .param pmc b
+    $I0 = isle a, b
+    new $P0, .LuaBoolean
+    set $P0, $I0
+    .return ($P0)
+.end
+
+
+=item C<infix:>=>
+
+=cut
+
+.sub 'infix:E<gt>='
+    .param pmc a
+    .param pmc b
+    $I0 = isge a, b
+    new $P0, .LuaBoolean
+    set $P0, $I0
+    .return ($P0)
+.end
+
+
+=item C<infix:E<lt>>
+
+=cut
+
+.sub 'infix:<'
+    .param pmc a
+    .param pmc b
+    $I0 = islt a, b
+    new $P0, .LuaBoolean
+    set $P0, $I0
+    .return ($P0)
+.end
+
+
+=item C<infix:E<gt>>
+
+=cut
+
+.sub 'infix:>'
+    .param pmc a
+    .param pmc b
+    $I0 = isgt a, b
+    new $P0, .LuaBoolean
+    set $P0, $I0
+    .return ($P0)
+.end
+
+
+=item C<infix:and>
+
+=cut
+
+.sub 'infix:and'
+    .param pmc a
+    .param pmc b
+    if a goto L1
+    .return (a)
+L1:
+    .return (b)
+.end
+
+
+=item C<infix:or>
+
+=cut
+
+.sub 'infix:or'
+    .param pmc a
+    .param pmc b
+    unless a goto L1
+    .return (a)
+L1:
+    .return (b)
+.end
+
+
+=back
+
 =head1 AUTHORS
 
 Francois Perrad

Modified: trunk/languages/lua/src/PASTGrammar.tg
==============================================================================
--- trunk/languages/lua/src/PASTGrammar.tg      (original)
+++ trunk/languages/lua/src/PASTGrammar.tg      Wed May 30 00:10:27 2007
@@ -367,10 +367,9 @@
 
 
 transform past (Lua::Grammar::constructor) :language('PIR') {
-    .local pmc past, table
-    new table, 'PAST::Val'
-    table.'init'('node'=>node, 'vtype'=>'.LuaTable')
-    past = table.'new'('PAST::Stmts', table, 'node'=>node)
+    .local pmc past
+    new past, 'PAST::Val'
+    past.'init'('node'=>node, 'vtype'=>'.LuaTable')
     $P0 = node['tablefieldlist']
     if null $P0 goto L1
     $P0 = shift $P0
@@ -383,34 +382,33 @@
     $P0 = shift iter
     $S0 = $P0.'find_key'()
     $P0 = $P0[$S0]
-    $S0 = concat 'Lua::Grammar::', $S0
-    expr = tree.'get'('past', $P0, $S0)
-    key = tree.'get'('key', $P0, $S0)
-    unless null key goto L3
-    $S0 = $I0
-    inc $I0
-    key = past.'new'('PAST::Val', 'node'=>$P0, 'vtype'=>'.LuaNumber', 
'name'=>$S0, 'ctype'=>'n+')
+    unless $S0 == 'expression' goto L3
+    $P1 = tree.'get'('past', $P0, 'Lua::Grammar::expression')
+    past.'push'($P1)
+    goto L2
 L3:
-    var = past.'new'('PAST::Var', table, key, 'node'=>$P0, 'scope'=>'keyed', 
'islvalue'=>1)
-    past.'push_new'('PAST::Op', var, expr, 'node'=>$P0, 'pasttype'=>'assign')
+    $S0 = concat 'Lua::Grammar::', $S0
+    $P1 = tree.'get'('record', $P0, $S0)
+    past.'push'($P1)
     goto L2
 L1:
     .return (past)
 }
 
 
-transform key (Lua::Grammar::expression) :language('PIR') {
-    .local pmc key
-    null key
-    .return (key)
-}
-
-
-transform key (Lua::Grammar::record_field) :language('PIR') {
+transform record (Lua::Grammar::record_field) :language('PIR') {
+    .local pmc record
+    record = new .FixedPMCArray
+    set record, 2
     $S0 = node.'find_key'()
     $P0 = node[$S0]
     $S0 = concat 'Lua::Grammar::', $S0
-    .return tree.'get'('key', $P0, $S0)
+    $P1 = tree.'get'('key', $P0, $S0)
+    record[0] = $P1
+    $P0 = node['expression']
+    $P1 = tree.'get'('past', $P0, 'Lua::Grammar::expression')
+    record[1] = $P1
+    .return (record)
 }
 
 
@@ -429,12 +427,6 @@
 }
 
 
-transform past (Lua::Grammar::record_field) :language('PIR') {
-    $P0 = node['expression']
-    .return tree.'get'('past', $P0, 'Lua::Grammar::expression')
-}
-
-
 transform explist (Lua::Grammar::expression_list) :language('PIR') {
     .local pmc explist
     explist = new .ResizablePMCArray

Modified: trunk/languages/lua/src/POSTGrammar.tg
==============================================================================
--- trunk/languages/lua/src/POSTGrammar.tg      (original)
+++ trunk/languages/lua/src/POSTGrammar.tg      Wed May 30 00:10:27 2007
@@ -205,6 +205,13 @@
 }
 
 
+transform len (PAST::Op) :language('PIR') {
+    node.'pirop'('callmethod')
+    node.'name'('len')
+    .return tree.'get'('pirop', node)
+}
+
+
 transform inline (PAST::Op) :language('PIR') {
     node.'pirop'('inline')
     .return tree.'get'('pirop', node)
@@ -223,33 +230,28 @@
     elsepast = node[2]
 
     .local pmc thenlabel, endlabel
-    .local string pasttype, n
-    pasttype = node.'pasttype'()
+    .local string n
     n = node.'unique'('_')
-    $S0 = concat pasttype, n
+    $S0 = concat 'if', n
     thenlabel = ops.'new'('POST::Label', 'result'=>$S0)
     $S0 = concat $S0, '_end'
     endlabel = ops.'new'('POST::Label', 'result'=>$S0)
 
     exprpost = tree.'get'('post', exprpast)
     ops.'push'(exprpost)
-    ops.'push_pirop'(pasttype, exprpost, thenlabel)
-    elsepost = exprpost
+    ops.'push_pirop'('if', exprpost, thenlabel)
     $I0 = defined elsepast
     if $I0 == 0 goto else_done
     elsepost = tree.'get'('post', elsepast)
     ops.'push'(elsepost)
   else_done:
-    ops.'push_pirop'('set', ops, elsepost)
     ops.'push_pirop'('goto', endlabel)
     ops.'push'(thenlabel)
-    thenpost = exprpost
     $I0 = defined thenpast
     if $I0 == 0 goto then_done
     thenpost = tree.'get'('post', thenpast)
     ops.'push'(thenpost)
   then_done:
-    ops.'push_pirop'('set', ops, thenpost)
     ops.'push'(endlabel)
     .return (ops)
 }
@@ -266,26 +268,17 @@
     blockpast = node[1]
 
     .local pmc looplabel, endlabel
-    .local string pasttype, n
-    pasttype = node.'pasttype'()
+    .local string n
     n = node.'unique'('_')
-    $S0 = concat pasttype, n
+    $S0 = concat 'while', n
     looplabel = ops.'new'('POST::Label', 'result'=>$S0)
     $S0 = concat $S0, '_end'
     endlabel = ops.'new'('POST::Label', 'result'=>$S0)
 
-    ##   determine if we need an 'if' or an 'unless' on
-    ##   the conditional (while => if, until => unless)
-    .local string iftype
-    iftype = 'if'
-    if $S0 == 'until' goto done_iftype
-    iftype = 'unless'
-  done_iftype:
-
     ops.'push'(looplabel)
     exprpost = tree.'get'('post', exprpast)
     ops.'push'(exprpost)
-    ops.'push_pirop'(iftype, exprpost, endlabel)
+    ops.'push_pirop'('unless', exprpost, endlabel)
     blockpost = tree.'get'('post', blockpast)
     ops.'push'(blockpost)
     ops.'push_pirop'('goto', looplabel)
@@ -366,15 +359,54 @@
     name = node.'escape'(name)
   skip_escape:
 
-    .local pmc ops
+    .local pmc ops, val
     ops = new 'POST::Ops'
     ops.'init'('node'=>node)
-    $P0 = ops.'push_pirop'('new')
-    $P0.'arglist'($P0, vtype)
-    if name == '' goto end
-    $P0 = ops.'push_pirop'('set', $P0, name, 'result'=>$P0)
+    val = ops.'push_pirop'('new')
+    val.'arglist'(val, vtype)
+    if name == '' goto init
+    val = ops.'push_pirop'('set', val, name, 'result'=>val)
+  init:
+    ops.'result'(val)
+
+    unless vtype == '.LuaTable' goto end
+    .local pmc iter, kpost, vpost
+    .local string key
+    .local int i
+    i = 1
+    iter = node.'iterator'()
+  iter_loop:
+    unless iter goto iter_end
+    $P0 = shift iter
+    $I0 = does $P0, 'array'
+    if $I0 goto rec_field
+    vpost = tree.'get'('post', $P0)
+    kpost = ops.'new'('POST::Ops', 'node'=>node)
+    $P0 = kpost.'push_pirop'('new')
+    $P0.'arglist'($P0, '.LuaNumber')
+    $S0 = i
+    inc i
+    kpost.'push_pirop'('set', $P0, $S0)
+    kpost.'result'($P0)
+    goto end_rec_field
+  rec_field:
+    $P1 = $P0[0]
+    kpost = tree.'get'('post', $P1)
+    $P1 = $P0[1]
+    vpost = tree.'get'('post', $P1)
+  end_rec_field:
+    ops.'push'(kpost)
+    ops.'push'(vpost)
+    key = val.'result'()
+    key = concat key, '['
+    $S0 = kpost.'result'()
+    key .= $S0
+    key .= ']'
+    ops.'push_pirop'('set', key, vpost)
+    goto iter_loop
+  iter_end:
+
   end:
-    ops.'result'($P0)
     .return (ops)
 }
 

Modified: trunk/languages/lua/src/lua51.pg
==============================================================================
--- trunk/languages/lua/src/lua51.pg    (original)
+++ trunk/languages/lua/src/lua51.pg    Wed May 30 00:10:27 2007
@@ -130,11 +130,7 @@
 }
 
 rule tablefieldlist {
-  <tablefield> [ <fieldsep> <tablefield> ]* <fieldsep>?
-}
-
-rule fieldsep {
-  <','> | <';'>
+  <tablefield> [ <[,;]> <tablefield> ]* <[,;]>?
 }
 
 rule tablefield {
@@ -289,7 +285,7 @@
 
 proto 'prefix:not'  is looser('infix:^')    is pirop('not')
                     is returns('.LuaBoolean')                   { ... }
-proto 'prefix:#'    is equiv('prefix:not')  is pirop('len')
+proto 'prefix:#'    is equiv('prefix:not')  is past('len')
                     is returns('.LuaNumber')                    { ... }
 proto 'prefix:-'    is equiv('prefix:not')  is pirop('neg')
                     is returns('.LuaNumber')                    { ... }
@@ -309,22 +305,16 @@
 proto 'infix:..'    is looser('infix:+')    is pirop('concat')  is 
assoc('right')
                     is returns('.LusString')                    { ... }
 
-proto 'infix:<'     is looser('infix:..')   is pirop('islt')
-                    is returns('.LuaBoolean')                   { ... }
-proto 'infix:>'     is equiv('infix:<')     is pirop('isgt')
-                    is returns('.LuaBoolean')                   { ... }
-proto 'infix:<='    is equiv('infix:<')     is pirop('isle')
-                    is returns('.LuaBoolean')                   { ... }
-proto 'infix:>='    is equiv('infix:<')     is pirop('isge')
-                    is returns('.LuaBoolean')                   { ... }
-proto 'infix:~='    is equiv('infix:<')     is pirop('isne')
-                    is returns('.LuaBoolean')                   { ... }
-proto 'infix:=='    is equiv('infix:<')     is pirop('iseq')
-                    is returns('.LuaBoolean')                   { ... }
+proto 'infix:<'     is looser('infix:..')                       { ... }
+proto 'infix:>'     is equiv('infix:<')                         { ... }
+proto 'infix:<='    is equiv('infix:<')                         { ... }
+proto 'infix:>='    is equiv('infix:<')                         { ... }
+proto 'infix:~='    is equiv('infix:<')                         { ... }
+proto 'infix:=='    is equiv('infix:<')                         { ... }
 
-proto 'infix:and'   is looser('infix:<')    is past('and')      { ... }
+proto 'infix:and'   is looser('infix:<')                        { ... }
 
-proto 'infix:or'    is looser('infix:and')  is past('or')       { ... }
+proto 'infix:or'    is looser('infix:and')                      { ... }
 
 
 =head1 AUTHORS

Reply via email to