changeset 54:f5b27ef14d73

add OutputStmt git-svn-id: https://luan-java.googlecode.com/svn/trunk@55 21e917c8-12df-6dd8-5cb6-c86387c605b9
author fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
date Mon, 31 Dec 2012 19:45:06 +0000
parents 54a61d1dc070
children 9381b23ea9e1
files src/luan/interp/LuanParser.java src/luan/interp/OutputStmt.java
diffstat 2 files changed, 56 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/src/luan/interp/LuanParser.java	Mon Dec 31 07:38:09 2012 +0000
+++ b/src/luan/interp/LuanParser.java	Mon Dec 31 19:45:06 2012 +0000
@@ -221,6 +221,7 @@
 			LocalStmt(stmts),
 			Sequence(
 				FirstOf(
+					OutputStmt(),
 					ReturnStmt(),
 					FunctionStmt(),
 					LocalFunctionStmt(),
@@ -240,6 +241,34 @@
 		);
 	}
 
+	Rule OutputStmt() {
+		Var<Integer> start = new Var<Integer>();
+		Var<ExpList.Builder> builder = new Var<ExpList.Builder>(new ExpList.Builder());
+		return Sequence(
+			start.set(currentIndex()),
+			"%>", Optional( EndOfLine() ),
+			ZeroOrMore(
+				FirstOf(
+					Sequence(
+						OneOrMore(
+							TestNot("<%"),
+							ANY
+						),
+						addToExpList(builder.get(),new ConstExpr(match()))
+					),
+					Sequence(
+						"<%=", Spaces(),
+						Expr(),
+						addToExpList(builder.get()),
+						"%>"
+					)
+				)
+			),
+			"<%", Spaces(),
+			push( new OutputStmt( se(start.get()), builder.get().build() ) )
+		);
+	}
+
 	Rule ReturnStmt() {
 		Var<Integer> start = new Var<Integer>();
 		return Sequence(
@@ -796,6 +825,11 @@
 		return true;
 	}
 
+	boolean addToExpList(ExpList.Builder bld, Expr expr) {
+		bld.add(expr);
+		return true;
+	}
+
 	Rule SubExpr() {
 		return Sequence( '[', incParens(), Spaces(), Expr(), ']', decParens(), Spaces() );
 	}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/luan/interp/OutputStmt.java	Mon Dec 31 19:45:06 2012 +0000
@@ -0,0 +1,22 @@
+package luan.interp;
+
+import luan.Luan;
+import luan.LuanSource;
+import luan.LuanException;
+
+
+final class OutputStmt extends CodeImpl implements Stmt {
+	private final Expressions expressions;
+
+	OutputStmt(LuanSource.Element se,Expressions expressions) {
+		super(se);
+		this.expressions = expressions;
+	}
+
+	@Override public void eval(LuanStateImpl luan) throws LuanException {
+		for( Object obj : expressions.eval(luan) ) {
+			luan.out.print( luan.toString(se,obj) );
+		}
+	}
+
+}