Mercurial Hosting > luan
comparison src/luan/impl/LuanParser.java @ 1796:383703b581bc
add backtick
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Wed, 31 Jan 2024 19:13:05 -0700 |
parents | 842a0e0fd8c5 |
children | d1dd61e6dd84 |
comparison
equal
deleted
inserted
replaced
1795:745f756e719b | 1796:383703b581bc |
---|---|
801 } | 801 } |
802 return stmt; | 802 return stmt; |
803 } | 803 } |
804 | 804 |
805 private Stmts ExpressionsStmt() throws ParseException { | 805 private Stmts ExpressionsStmt() throws ParseException { |
806 if( parser.test('`') ) | |
807 return null; | |
806 parser.begin(); | 808 parser.begin(); |
807 Expr exp = Expression(); | 809 Expr exp = Expression(); |
808 if( exp != null && exp.isStmt ) { | 810 if( exp != null && exp.isStmt ) { |
809 Stmts stmt = new Stmts(); | 811 Stmts stmt = new Stmts(); |
810 if( exp.valType==Val.SINGLE ) { | 812 if( exp.valType==Val.SINGLE ) { |
1109 private Expr SingleExpr() throws ParseException { | 1111 private Expr SingleExpr() throws ParseException { |
1110 parser.begin(); | 1112 parser.begin(); |
1111 Expr exp = FunctionExpr(); | 1113 Expr exp = FunctionExpr(); |
1112 if( exp != null ) | 1114 if( exp != null ) |
1113 return parser.success(exp); | 1115 return parser.success(exp); |
1116 exp = BacktickExpr(); | |
1117 if( exp != null ) | |
1118 return parser.success(exp); | |
1114 exp = VarExp(); | 1119 exp = VarExp(); |
1115 if( exp != null ) | 1120 if( exp != null ) |
1116 return parser.success(exp); | 1121 return parser.success(exp); |
1117 exp = VarArgs(); | 1122 exp = VarArgs(); |
1118 if( exp != null ) | 1123 if( exp != null ) |
1119 return parser.success(exp); | 1124 return parser.success(exp); |
1120 return parser.failure(null); | 1125 return parser.failure(null); |
1126 } | |
1127 | |
1128 private Expr BacktickExpr() throws ParseException { | |
1129 if( !parser.match('`') ) | |
1130 return null; | |
1131 Spaces(); | |
1132 frame = new Frame(frame); | |
1133 Stmts stmt = new Stmts(); | |
1134 stmt.add( "LuanTable Io = (LuanTable)luan.require(\"luan:Io.luan\"); " ); | |
1135 stmt.add( "Object stdout = Io.get(luan,\"stdout\"); " ); | |
1136 stmt.add( "LuanStringWriter lsw = new LuanStringWriter(); " ); | |
1137 stmt.add( "Io.put(luan,\"stdout\",lsw); " ); | |
1138 stmt.add( "try { " ); | |
1139 Stmts block = RequiredBlock(); | |
1140 stmt.addAll( block ); | |
1141 RequiredMatch('`'); | |
1142 stmt.add( "} finally { " ); | |
1143 stmt.add( "Io.put(luan,\"stdout\",stdout); " ); | |
1144 stmt.add( "} " ); | |
1145 stmt.add( "return lsw.out().toString(); " ); | |
1146 stmt.hasReturn = true; | |
1147 Expr fnDef = newFnExp(stmt,null); | |
1148 Expr exp = new Expr(Val.SINGLE,true); | |
1149 exp.addAll( fnDef ); | |
1150 exp.add( ".call(luan)" ); | |
1151 frame = frame.parent; | |
1152 return exp; | |
1121 } | 1153 } |
1122 | 1154 |
1123 private Expr FunctionExpr() throws ParseException { | 1155 private Expr FunctionExpr() throws ParseException { |
1124 if( !Keyword("function") ) | 1156 if( !Keyword("function") ) |
1125 return null; | 1157 return null; |
1168 Spaces(); | 1200 Spaces(); |
1169 Stmts block = RequiredBlock(); | 1201 Stmts block = RequiredBlock(); |
1170 stmt.addAll( block ); | 1202 stmt.addAll( block ); |
1171 stmt.hasReturn = block.hasReturn; | 1203 stmt.hasReturn = block.hasReturn; |
1172 Expr fnDef = newFnExp(stmt,name); | 1204 Expr fnDef = newFnExp(stmt,name); |
1173 // RequiredEnd("end_function"); | |
1174 if( !Keyword("end") && !Keyword("end_function") ) { | 1205 if( !Keyword("end") && !Keyword("end_function") ) { |
1175 if( stmt.hasReturn && !parser.endOfInput() ) { | 1206 if( stmt.hasReturn && !parser.endOfInput() ) { |
1176 throw parser.exception("unreachable statement"); | 1207 throw parser.exception("unreachable statement"); |
1177 } else { | 1208 } else { |
1178 throw parser.exception("'end_function' or 'end' expected"); | 1209 throw parser.exception("'end_function' or 'end' expected"); |
1453 return parser.success(); | 1484 return parser.success(); |
1454 } | 1485 } |
1455 | 1486 |
1456 private Expr SubExpr() throws ParseException { | 1487 private Expr SubExpr() throws ParseException { |
1457 parser.begin(); | 1488 parser.begin(); |
1458 if( !parser.match('[') || parser.test("[") || parser.test("=") ) | 1489 if( !parser.match('[') || parser.test('[') || parser.test('=') ) |
1459 return parser.failure(null); | 1490 return parser.failure(null); |
1460 Spaces(); | 1491 Spaces(); |
1461 Expr exp = RequiredExpr().single(); | 1492 Expr exp = RequiredExpr().single(); |
1462 RequiredMatch(']'); | 1493 RequiredMatch(']'); |
1463 Spaces(); | 1494 Spaces(); |
2020 if( !stmts.hasReturn ) | 2051 if( !stmts.hasReturn ) |
2021 stmts.add( "\nreturn LuanFunction.NOTHING;" ); | 2052 stmts.add( "\nreturn LuanFunction.NOTHING;" ); |
2022 StringBuilder sb = new StringBuilder(); | 2053 StringBuilder sb = new StringBuilder(); |
2023 sb.append( "" | 2054 sb.append( "" |
2024 +"package luan.impl; " | 2055 +"package luan.impl; " |
2056 +"import luan.Luan; " | |
2025 +"import luan.LuanClosure; " | 2057 +"import luan.LuanClosure; " |
2026 +"import luan.Luan; " | |
2027 +"import luan.LuanFunction; " | 2058 +"import luan.LuanFunction; " |
2059 +"import luan.LuanTable; " | |
2028 +"import luan.LuanException; " | 2060 +"import luan.LuanException; " |
2029 +"import luan.modules.PackageLuan; " | 2061 +"import luan.modules.PackageLuan; " |
2030 | 2062 |
2031 +"public class " + className +" extends LuanClosure { " | 2063 +"public class " + className +" extends LuanClosure { " |
2032 +"public "+className+"(boolean javaOk,String sourceName) throws LuanException { " | 2064 +"public "+className+"(boolean javaOk,String sourceName) throws LuanException { " |