Mercurial Hosting > luan
diff src/luan/interp/LuaParser.java @ 34:0cdc1da466ee
implement _G and _ENV
git-svn-id: https://luan-java.googlecode.com/svn/trunk@35 21e917c8-12df-6dd8-5cb6-c86387c605b9
author | fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9> |
---|---|
date | Sun, 16 Dec 2012 09:23:56 +0000 |
parents | 8793c71ad47a |
children | e51906de0f11 |
line wrap: on
line diff
--- a/src/luan/interp/LuaParser.java Sun Dec 16 05:07:42 2012 +0000 +++ b/src/luan/interp/LuaParser.java Sun Dec 16 09:23:56 2012 +0000 @@ -23,6 +23,9 @@ class LuaParser extends BaseParser<Object> { + static final String _G = "_G"; + static final String _ENV = "_ENV"; + static final class Frame { final Frame parent; final List<String> symbols = new ArrayList<String>(); @@ -32,6 +35,14 @@ final List<String> upValueSymbols = new ArrayList<String>(); final List<UpValue.Getter> upValueGetters = new ArrayList<UpValue.Getter>(); + Frame() { + this.parent = null; + upValueSymbols.add(_G); + upValueGetters.add(UpValue.globalGetter); + upValueSymbols.add(_ENV); + upValueGetters.add(UpValue.globalGetter); + } + Frame(Frame parent) { this.parent = parent; } @@ -71,7 +82,7 @@ int nEquals; int parens = 0; - Frame frame = new Frame(null); + Frame frame = new Frame(); boolean nEquals(int n) { nEquals = n; @@ -433,7 +444,7 @@ index = upValueIndex(name); if( index != -1 ) return push( new SetUpVar(index) ); - return push( new SetTableEntry( EnvExpr.INSTANCE, new ConstExpr(name) ) ); + return push( new SetTableEntry( env(), new ConstExpr(name) ) ); } Rule Expr() { @@ -537,8 +548,8 @@ Rule Function() { Var<List<String>> names = new Var<List<String>>(new ArrayList<String>()); return Sequence( + '(', incParens(), Spaces(), action( frame = new Frame(frame) ), - '(', incParens(), Spaces(), Optional( FirstOf( Sequence( @@ -646,6 +657,16 @@ ); } + Expr env() { + int index = stackIndex(_ENV); + if( index != -1 ) + return new GetLocalVar(index); + index = upValueIndex(_ENV); + if( index != -1 ) + return new GetUpVar(index); + throw new RuntimeException("_ENV not found"); + } + boolean makeVarExp() { Object obj2 = pop(); if( obj2==null ) @@ -660,7 +681,7 @@ index = upValueIndex(name); if( index != -1 ) return push( new GetUpVar(index) ); - return push( new GetExpr( EnvExpr.INSTANCE, new ConstExpr(name) ) ); + return push( new GetExpr( env(), new ConstExpr(name) ) ); } // function should be on top of the stack