comparison core/src/luan/impl/LuanParser.java @ 648:e387e4021afe

start compiler with len operator
author Franklin Schmidt <fschmidt@gmail.com>
date Wed, 30 Mar 2016 19:40:48 -0600
parents 859c0dedc8b6
children 37f0cf43f191
comparison
equal deleted inserted replaced
647:8e8c30b72e9b 648:e387e4021afe
659 659
660 private Expressions UnaryExpr(In in) throws ParseException { 660 private Expressions UnaryExpr(In in) throws ParseException {
661 parser.begin(); 661 parser.begin();
662 if( parser.match('#') ) { 662 if( parser.match('#') ) {
663 Spaces(in); 663 Spaces(in);
664 Expressions exp = UnaryExpr(in); 664 Expressions exp = required(UnaryExpr(in));
665 return parser.success( new LenExpr( required(expr(exp)) ) ); 665 String code = "$Luan.len($luan," + expressionsToExprString(exp) + ")";
666 exp = stringToExpressions(code);
667 return parser.success(exp);
666 } 668 }
667 if( Minus() ) { 669 if( Minus() ) {
668 Spaces(in); 670 Spaces(in);
669 Expressions exp = UnaryExpr(in); 671 Expressions exp = UnaryExpr(in);
670 return parser.success( new UnmExpr( required(expr(exp)) ) ); 672 return parser.success( new UnmExpr( required(expr(exp)) ) );
1345 if( !parser.match(']') ) 1347 if( !parser.match(']') )
1346 return parser.failure(); 1348 return parser.failure();
1347 return parser.success(); 1349 return parser.success();
1348 } 1350 }
1349 1351
1352
1353
1354
1355 private static String expressionsToExprString(Expressions exp) {
1356 int i = $Luan.addExpressions(exp);
1357 String s = "$Luan.getExpressions(" + i + ").eval($luan)";
1358 if( !(exp instanceof Expr) )
1359 s = "$Luan.first(" + s + ")";
1360 return s;
1361 }
1362
1363 private static int classCounter = 0;
1364
1365 private static Expressions stringToExpressions(String code) {
1366 String className = "EXP" + ++classCounter;
1367 String classCode = ""
1368 +"package luan.impl;\n"
1369 // +"import luan.impl.$Luan;\n"
1370 // +"import luan.impl.Expressions;\n"
1371 +"import luan.LuanException;\n"
1372 +"\n"
1373 +"public class " + className +" implements Expressions {\n"
1374 +" @Override public Object eval(LuanStateImpl $luan) throws LuanException {\n"
1375 +" return " + code + ";\n"
1376 +" }\n"
1377 +"}\n"
1378 ;
1379 try {
1380 Class cls = LuanJavaCompiler.compile("luan.impl."+className,code,classCode);
1381 return (Expressions)cls.newInstance();
1382 } catch(ClassNotFoundException e) {
1383 throw new RuntimeException(e);
1384 } catch(InstantiationException e) {
1385 throw new RuntimeException(e);
1386 } catch(IllegalAccessException e) {
1387 throw new RuntimeException(e);
1388 }
1389 }
1390
1350 } 1391 }