Mercurial Hosting > luan
diff src/luan/CmdLine.java @ 11:b7d7069fee58
add assignment statement and CmdLine
git-svn-id: https://luan-java.googlecode.com/svn/trunk@12 21e917c8-12df-6dd8-5cb6-c86387c605b9
author | fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9> |
---|---|
date | Thu, 22 Nov 2012 10:51:56 +0000 |
parents | |
children | 9cea1aea5eef |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/luan/CmdLine.java Thu Nov 22 10:51:56 2012 +0000 @@ -0,0 +1,40 @@ +package luan; + +import java.util.List; +import java.util.Scanner; +import org.parboiled.Parboiled; +import org.parboiled.errors.ErrorUtils; +import org.parboiled.parserunners.ReportingParseRunner; +import org.parboiled.support.ParsingResult; +import luan.interp.Expressions; +import luan.interp.Stmt; +import luan.interp.LuaParser; +import luan.lib.BasicLib; + + +public class CmdLine { + + public static void main(String[] args) throws Exception { + LuaParser parser = Parboiled.createParser(LuaParser.class); + LuaState lua = new LuaState(); + while( true ) { + System.out.print("> "); + String input = new Scanner(System.in).nextLine(); + ParsingResult<?> result = new ReportingParseRunner(parser.Target()).run(input); + if( result.hasErrors() ) { + System.out.println("Parse Errors:\n" + ErrorUtils.printParseErrors(result)); + } else { + Object resultValue = result.resultValue; + if( resultValue instanceof Expressions ) { + Expressions expressions = (Expressions)resultValue; + List vals = expressions.eval(lua); + if( !vals.isEmpty() ) + BasicLib.print( vals.toArray() ); + } else { + Stmt stmt = (Stmt)resultValue; + stmt.eval(lua); + } + } + } + } +}