Mercurial Hosting > luan
changeset 1796:383703b581bc
add backtick
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Wed, 31 Jan 2024 19:13:05 -0700 |
parents | 745f756e719b |
children | ba43135bb98d |
files | src/luan/impl/LuanParser.java src/luan/impl/LuanStringWriter.java |
diffstat | 2 files changed, 60 insertions(+), 3 deletions(-) [+] |
line wrap: on
line diff
--- a/src/luan/impl/LuanParser.java Tue Jan 02 21:49:54 2024 -0700 +++ b/src/luan/impl/LuanParser.java Wed Jan 31 19:13:05 2024 -0700 @@ -803,6 +803,8 @@ } private Stmts ExpressionsStmt() throws ParseException { + if( parser.test('`') ) + return null; parser.begin(); Expr exp = Expression(); if( exp != null && exp.isStmt ) { @@ -1111,6 +1113,9 @@ Expr exp = FunctionExpr(); if( exp != null ) return parser.success(exp); + exp = BacktickExpr(); + if( exp != null ) + return parser.success(exp); exp = VarExp(); if( exp != null ) return parser.success(exp); @@ -1120,6 +1125,33 @@ return parser.failure(null); } + private Expr BacktickExpr() throws ParseException { + if( !parser.match('`') ) + return null; + Spaces(); + frame = new Frame(frame); + Stmts stmt = new Stmts(); + stmt.add( "LuanTable Io = (LuanTable)luan.require(\"luan:Io.luan\"); " ); + stmt.add( "Object stdout = Io.get(luan,\"stdout\"); " ); + stmt.add( "LuanStringWriter lsw = new LuanStringWriter(); " ); + stmt.add( "Io.put(luan,\"stdout\",lsw); " ); + stmt.add( "try { " ); + Stmts block = RequiredBlock(); + stmt.addAll( block ); + RequiredMatch('`'); + stmt.add( "} finally { " ); + stmt.add( "Io.put(luan,\"stdout\",stdout); " ); + stmt.add( "} " ); + stmt.add( "return lsw.out().toString(); " ); + stmt.hasReturn = true; + Expr fnDef = newFnExp(stmt,null); + Expr exp = new Expr(Val.SINGLE,true); + exp.addAll( fnDef ); + exp.add( ".call(luan)" ); + frame = frame.parent; + return exp; + } + private Expr FunctionExpr() throws ParseException { if( !Keyword("function") ) return null; @@ -1170,7 +1202,6 @@ stmt.addAll( block ); stmt.hasReturn = block.hasReturn; Expr fnDef = newFnExp(stmt,name); -// RequiredEnd("end_function"); if( !Keyword("end") && !Keyword("end_function") ) { if( stmt.hasReturn && !parser.endOfInput() ) { throw parser.exception("unreachable statement"); @@ -1455,7 +1486,7 @@ private Expr SubExpr() throws ParseException { parser.begin(); - if( !parser.match('[') || parser.test("[") || parser.test("=") ) + if( !parser.match('[') || parser.test('[') || parser.test('=') ) return parser.failure(null); Spaces(); Expr exp = RequiredExpr().single(); @@ -2022,9 +2053,10 @@ StringBuilder sb = new StringBuilder(); sb.append( "" +"package luan.impl; " + +"import luan.Luan; " +"import luan.LuanClosure; " - +"import luan.Luan; " +"import luan.LuanFunction; " + +"import luan.LuanTable; " +"import luan.LuanException; " +"import luan.modules.PackageLuan; "
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/luan/impl/LuanStringWriter.java Wed Jan 31 19:13:05 2024 -0700 @@ -0,0 +1,25 @@ +package luan.impl; + +import java.io.Writer; +import java.io.StringWriter; +import java.io.IOException; +import luan.Luan; +import luan.LuanException; +import luan.modules.IoLuan; + + +public final class LuanStringWriter implements IoLuan.LuanWriter { + private final Writer out = new StringWriter(); + + public Object out() { + return out; + } + + public void write(Luan luan,Object... args) throws LuanException, IOException { + for( Object obj : args ) { + out.write( luan.luanToString(obj) ); + } + } + + public void close() {} +}