annotate 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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
11
b7d7069fee58 add assignment statement and CmdLine
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
1 package luan;
b7d7069fee58 add assignment statement and CmdLine
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
2
b7d7069fee58 add assignment statement and CmdLine
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
3 import java.util.List;
b7d7069fee58 add assignment statement and CmdLine
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
4 import java.util.Scanner;
b7d7069fee58 add assignment statement and CmdLine
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
5 import org.parboiled.Parboiled;
b7d7069fee58 add assignment statement and CmdLine
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
6 import org.parboiled.errors.ErrorUtils;
b7d7069fee58 add assignment statement and CmdLine
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
7 import org.parboiled.parserunners.ReportingParseRunner;
b7d7069fee58 add assignment statement and CmdLine
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
8 import org.parboiled.support.ParsingResult;
b7d7069fee58 add assignment statement and CmdLine
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
9 import luan.interp.Expressions;
b7d7069fee58 add assignment statement and CmdLine
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
10 import luan.interp.Stmt;
b7d7069fee58 add assignment statement and CmdLine
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
11 import luan.interp.LuaParser;
b7d7069fee58 add assignment statement and CmdLine
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
12 import luan.lib.BasicLib;
b7d7069fee58 add assignment statement and CmdLine
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
13
b7d7069fee58 add assignment statement and CmdLine
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
14
b7d7069fee58 add assignment statement and CmdLine
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
15 public class CmdLine {
b7d7069fee58 add assignment statement and CmdLine
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
16
b7d7069fee58 add assignment statement and CmdLine
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
17 public static void main(String[] args) throws Exception {
b7d7069fee58 add assignment statement and CmdLine
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
18 LuaParser parser = Parboiled.createParser(LuaParser.class);
b7d7069fee58 add assignment statement and CmdLine
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
19 LuaState lua = new LuaState();
b7d7069fee58 add assignment statement and CmdLine
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
20 while( true ) {
b7d7069fee58 add assignment statement and CmdLine
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
21 System.out.print("> ");
b7d7069fee58 add assignment statement and CmdLine
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
22 String input = new Scanner(System.in).nextLine();
b7d7069fee58 add assignment statement and CmdLine
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
23 ParsingResult<?> result = new ReportingParseRunner(parser.Target()).run(input);
b7d7069fee58 add assignment statement and CmdLine
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
24 if( result.hasErrors() ) {
b7d7069fee58 add assignment statement and CmdLine
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
25 System.out.println("Parse Errors:\n" + ErrorUtils.printParseErrors(result));
b7d7069fee58 add assignment statement and CmdLine
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
26 } else {
b7d7069fee58 add assignment statement and CmdLine
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
27 Object resultValue = result.resultValue;
b7d7069fee58 add assignment statement and CmdLine
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
28 if( resultValue instanceof Expressions ) {
b7d7069fee58 add assignment statement and CmdLine
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
29 Expressions expressions = (Expressions)resultValue;
b7d7069fee58 add assignment statement and CmdLine
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
30 List vals = expressions.eval(lua);
b7d7069fee58 add assignment statement and CmdLine
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
31 if( !vals.isEmpty() )
b7d7069fee58 add assignment statement and CmdLine
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
32 BasicLib.print( vals.toArray() );
b7d7069fee58 add assignment statement and CmdLine
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
33 } else {
b7d7069fee58 add assignment statement and CmdLine
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
34 Stmt stmt = (Stmt)resultValue;
b7d7069fee58 add assignment statement and CmdLine
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
35 stmt.eval(lua);
b7d7069fee58 add assignment statement and CmdLine
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
36 }
b7d7069fee58 add assignment statement and CmdLine
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
37 }
b7d7069fee58 add assignment statement and CmdLine
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
38 }
b7d7069fee58 add assignment statement and CmdLine
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
39 }
b7d7069fee58 add assignment statement and CmdLine
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
40 }