comparison src/luan/impl/LuanParser.java @ 1563:8fbcc4747091

remove LuanFunction.luan
author Franklin Schmidt <fschmidt@gmail.com>
date Mon, 09 Nov 2020 01:37:57 -0700
parents b89212fd04b5
children 2975c932864d
comparison
equal deleted inserted replaced
1562:b89212fd04b5 1563:8fbcc4747091
374 374
375 private Expr callExpStr(Expr fn,Expr args) { 375 private Expr callExpStr(Expr fn,Expr args) {
376 Expr exp = new Expr(null,true); 376 Expr exp = new Expr(null,true);
377 exp.add( "Luan.checkFunction(" ); 377 exp.add( "Luan.checkFunction(" );
378 exp.addAll( fn.single() ); 378 exp.addAll( fn.single() );
379 exp.add( ").call(" ); 379 exp.add( ").call(luan," );
380 exp.addAll( args.array() ); 380 exp.addAll( args.array() );
381 exp.add( ")" ); 381 exp.add( ")" );
382 return exp; 382 return exp;
383 } 383 }
384 384
385 private Stmts TemplateStmt() throws ParseException { 385 private Stmts TemplateStmt() throws ParseException {
386 Expr exprs = TemplateExpressions(); 386 Expr exprs = TemplateExpressions();
387 if( exprs == null ) 387 if( exprs == null )
388 return null; 388 return null;
389 Stmts stmt = new Stmts(); 389 Stmts stmt = new Stmts();
390 stmt.add( "Luan.checkFunction(luan.index(PackageLuan.require(luan,\"luan:Io.luan\"),\"template_write\")).call(" ); 390 stmt.add( "Luan.checkFunction(luan.index(PackageLuan.require(luan,\"luan:Io.luan\"),\"template_write\")).call(luan," );
391 stmt.addAll( exprs.array() ); 391 stmt.addAll( exprs.array() );
392 stmt.add( "); " ); 392 stmt.add( "); " );
393 return stmt; 393 return stmt;
394 } 394 }
395 395
513 Expr expr = RequiredExpr().single(); 513 Expr expr = RequiredExpr().single();
514 RequiredKeyword("do"); 514 RequiredKeyword("do");
515 515
516 String fnVar = "fn" + ++forCounter; 516 String fnVar = "fn" + ++forCounter;
517 Expr fnExp = new Expr(null,false); 517 Expr fnExp = new Expr(null,false);
518 fnExp.add( fnVar + ".call()" ); 518 fnExp.add( fnVar + ".call(luan)" );
519 Stmts stmt = new Stmts(); 519 Stmts stmt = new Stmts();
520 stmt.add( "" 520 stmt.add( ""
521 +"LuanFunction "+fnVar+" = Luan.checkFunction(" 521 +"LuanFunction "+fnVar+" = Luan.checkFunction("
522 ); 522 );
523 stmt.addAll( expr ); 523 stmt.addAll( expr );
869 if( parser.match("==") ) { 869 if( parser.match("==") ) {
870 Spaces(); 870 Spaces();
871 exp = exp.single(); 871 exp = exp.single();
872 Expr exp2 = required(ConcatExpr()).single(); 872 Expr exp2 = required(ConcatExpr()).single();
873 Expr newExp = new Expr(Val.SINGLE,false); 873 Expr newExp = new Expr(Val.SINGLE,false);
874 newExp.add( "LuanImpl.eq(" ); 874 newExp.add( "LuanImpl.eq(luan," );
875 newExp.addAll( exp ); 875 newExp.addAll( exp );
876 newExp.add( "," ); 876 newExp.add( "," );
877 newExp.addAll( exp2 ); 877 newExp.addAll( exp2 );
878 newExp.add( ")" ); 878 newExp.add( ")" );
879 exp = newExp; 879 exp = newExp;
880 } else if( parser.match("~=") ) { 880 } else if( parser.match("~=") ) {
881 Spaces(); 881 Spaces();
882 exp = exp.single(); 882 exp = exp.single();
883 Expr exp2 = required(ConcatExpr()).single(); 883 Expr exp2 = required(ConcatExpr()).single();
884 Expr newExp = new Expr(Val.SINGLE,false); 884 Expr newExp = new Expr(Val.SINGLE,false);
885 newExp.add( "!LuanImpl.eq(" ); 885 newExp.add( "!LuanImpl.eq(luan," );
886 newExp.addAll( exp ); 886 newExp.addAll( exp );
887 newExp.add( "," ); 887 newExp.add( "," );
888 newExp.addAll( exp2 ); 888 newExp.addAll( exp2 );
889 newExp.add( ")" ); 889 newExp.add( ")" );
890 exp = newExp; 890 exp = newExp;
891 } else if( parser.match("<=") ) { 891 } else if( parser.match("<=") ) {
892 Spaces(); 892 Spaces();
893 exp = exp.single(); 893 exp = exp.single();
894 Expr exp2 = required(ConcatExpr()).single(); 894 Expr exp2 = required(ConcatExpr()).single();
895 Expr newExp = new Expr(Val.SINGLE,false); 895 Expr newExp = new Expr(Val.SINGLE,false);
896 newExp.add( "LuanImpl.le(" ); 896 newExp.add( "LuanImpl.le(luan," );
897 newExp.addAll( exp ); 897 newExp.addAll( exp );
898 newExp.add( "," ); 898 newExp.add( "," );
899 newExp.addAll( exp2 ); 899 newExp.addAll( exp2 );
900 newExp.add( ")" ); 900 newExp.add( ")" );
901 exp = newExp; 901 exp = newExp;
902 } else if( parser.match(">=") ) { 902 } else if( parser.match(">=") ) {
903 Spaces(); 903 Spaces();
904 exp = exp.single(); 904 exp = exp.single();
905 Expr exp2 = required(ConcatExpr()).single(); 905 Expr exp2 = required(ConcatExpr()).single();
906 Expr newExp = new Expr(Val.SINGLE,false); 906 Expr newExp = new Expr(Val.SINGLE,false);
907 newExp.add( "LuanImpl.le(" ); 907 newExp.add( "LuanImpl.le(luan," );
908 newExp.addAll( exp2 ); 908 newExp.addAll( exp2 );
909 newExp.add( "," ); 909 newExp.add( "," );
910 newExp.addAll( exp ); 910 newExp.addAll( exp );
911 newExp.add( ")" ); 911 newExp.add( ")" );
912 exp = newExp; 912 exp = newExp;
913 } else if( parser.match("<") ) { 913 } else if( parser.match("<") ) {
914 Spaces(); 914 Spaces();
915 exp = exp.single(); 915 exp = exp.single();
916 Expr exp2 = required(ConcatExpr()).single(); 916 Expr exp2 = required(ConcatExpr()).single();
917 Expr newExp = new Expr(Val.SINGLE,false); 917 Expr newExp = new Expr(Val.SINGLE,false);
918 newExp.add( "LuanImpl.lt(" ); 918 newExp.add( "LuanImpl.lt(luan," );
919 newExp.addAll( exp ); 919 newExp.addAll( exp );
920 newExp.add( "," ); 920 newExp.add( "," );
921 newExp.addAll( exp2 ); 921 newExp.addAll( exp2 );
922 newExp.add( ")" ); 922 newExp.add( ")" );
923 exp = newExp; 923 exp = newExp;
924 } else if( parser.match(">") ) { 924 } else if( parser.match(">") ) {
925 Spaces(); 925 Spaces();
926 exp = exp.single(); 926 exp = exp.single();
927 Expr exp2 = required(ConcatExpr()).single(); 927 Expr exp2 = required(ConcatExpr()).single();
928 Expr newExp = new Expr(Val.SINGLE,false); 928 Expr newExp = new Expr(Val.SINGLE,false);
929 newExp.add( "LuanImpl.lt(" ); 929 newExp.add( "LuanImpl.lt(luan," );
930 newExp.addAll( exp2 ); 930 newExp.addAll( exp2 );
931 newExp.add( "," ); 931 newExp.add( "," );
932 newExp.addAll( exp ); 932 newExp.addAll( exp );
933 newExp.add( ")" ); 933 newExp.add( ")" );
934 exp = newExp; 934 exp = newExp;
946 if( parser.match("..") ) { 946 if( parser.match("..") ) {
947 Spaces(); 947 Spaces();
948 exp = exp.single(); 948 exp = exp.single();
949 Expr exp2 = required(ConcatExpr()).single(); 949 Expr exp2 = required(ConcatExpr()).single();
950 Expr newExp = new Expr(Val.SINGLE,false); 950 Expr newExp = new Expr(Val.SINGLE,false);
951 newExp.add( "LuanImpl.concat(" ); 951 newExp.add( "LuanImpl.concat(luan," );
952 newExp.addAll( exp ); 952 newExp.addAll( exp );
953 newExp.add( "," ); 953 newExp.add( "," );
954 newExp.addAll( exp2 ); 954 newExp.addAll( exp2 );
955 newExp.add( ")" ); 955 newExp.add( ")" );
956 exp = newExp; 956 exp = newExp;
967 if( parser.match('+') ) { 967 if( parser.match('+') ) {
968 Spaces(); 968 Spaces();
969 exp = exp.single(); 969 exp = exp.single();
970 Expr exp2 = required(TermExpr()).single(); 970 Expr exp2 = required(TermExpr()).single();
971 Expr newExp = new Expr(Val.SINGLE,false); 971 Expr newExp = new Expr(Val.SINGLE,false);
972 newExp.add( "LuanImpl.add(" ); 972 newExp.add( "LuanImpl.add(luan," );
973 newExp.addAll( exp ); 973 newExp.addAll( exp );
974 newExp.add( "," ); 974 newExp.add( "," );
975 newExp.addAll( exp2 ); 975 newExp.addAll( exp2 );
976 newExp.add( ")" ); 976 newExp.add( ")" );
977 exp = newExp; 977 exp = newExp;
978 } else if( Minus() ) { 978 } else if( Minus() ) {
979 Spaces(); 979 Spaces();
980 exp = exp.single(); 980 exp = exp.single();
981 Expr exp2 = required(TermExpr()).single(); 981 Expr exp2 = required(TermExpr()).single();
982 Expr newExp = new Expr(Val.SINGLE,false); 982 Expr newExp = new Expr(Val.SINGLE,false);
983 newExp.add( "LuanImpl.sub(" ); 983 newExp.add( "LuanImpl.sub(luan," );
984 newExp.addAll( exp ); 984 newExp.addAll( exp );
985 newExp.add( "," ); 985 newExp.add( "," );
986 newExp.addAll( exp2 ); 986 newExp.addAll( exp2 );
987 newExp.add( ")" ); 987 newExp.add( ")" );
988 exp = newExp; 988 exp = newExp;
1006 if( parser.match('*') ) { 1006 if( parser.match('*') ) {
1007 Spaces(); 1007 Spaces();
1008 exp = exp.single(); 1008 exp = exp.single();
1009 Expr exp2 = required(UnaryExpr()).single(); 1009 Expr exp2 = required(UnaryExpr()).single();
1010 Expr newExp = new Expr(Val.SINGLE,false); 1010 Expr newExp = new Expr(Val.SINGLE,false);
1011 newExp.add( "LuanImpl.mul(" ); 1011 newExp.add( "LuanImpl.mul(luan," );
1012 newExp.addAll( exp ); 1012 newExp.addAll( exp );
1013 newExp.add( "," ); 1013 newExp.add( "," );
1014 newExp.addAll( exp2 ); 1014 newExp.addAll( exp2 );
1015 newExp.add( ")" ); 1015 newExp.add( ")" );
1016 exp = newExp; 1016 exp = newExp;
1017 } else if( parser.match('/') ) { 1017 } else if( parser.match('/') ) {
1018 Spaces(); 1018 Spaces();
1019 exp = exp.single(); 1019 exp = exp.single();
1020 Expr exp2 = required(UnaryExpr()).single(); 1020 Expr exp2 = required(UnaryExpr()).single();
1021 Expr newExp = new Expr(Val.SINGLE,false); 1021 Expr newExp = new Expr(Val.SINGLE,false);
1022 newExp.add( "LuanImpl.div(" ); 1022 newExp.add( "LuanImpl.div(luan," );
1023 newExp.addAll( exp ); 1023 newExp.addAll( exp );
1024 newExp.add( "," ); 1024 newExp.add( "," );
1025 newExp.addAll( exp2 ); 1025 newExp.addAll( exp2 );
1026 newExp.add( ")" ); 1026 newExp.add( ")" );
1027 exp = newExp; 1027 exp = newExp;
1028 } else if( Mod() ) { 1028 } else if( Mod() ) {
1029 Spaces(); 1029 Spaces();
1030 exp = exp.single(); 1030 exp = exp.single();
1031 Expr exp2 = required(UnaryExpr()).single(); 1031 Expr exp2 = required(UnaryExpr()).single();
1032 Expr newExp = new Expr(Val.SINGLE,false); 1032 Expr newExp = new Expr(Val.SINGLE,false);
1033 newExp.add( "LuanImpl.mod(" ); 1033 newExp.add( "LuanImpl.mod(luan," );
1034 newExp.addAll( exp ); 1034 newExp.addAll( exp );
1035 newExp.add( "," ); 1035 newExp.add( "," );
1036 newExp.addAll( exp2 ); 1036 newExp.addAll( exp2 );
1037 newExp.add( ")" ); 1037 newExp.add( ")" );
1038 exp = newExp; 1038 exp = newExp;
1051 parser.begin(); 1051 parser.begin();
1052 if( parser.match('#') ) { 1052 if( parser.match('#') ) {
1053 Spaces(); 1053 Spaces();
1054 Expr exp = required(UnaryExpr()).single(); 1054 Expr exp = required(UnaryExpr()).single();
1055 Expr newExp = new Expr(Val.SINGLE,false); 1055 Expr newExp = new Expr(Val.SINGLE,false);
1056 newExp.add( "LuanImpl.len(" ); 1056 newExp.add( "LuanImpl.len(luan," );
1057 newExp.addAll( exp ); 1057 newExp.addAll( exp );
1058 newExp.add( ")" ); 1058 newExp.add( ")" );
1059 return parser.success(newExp); 1059 return parser.success(newExp);
1060 } 1060 }
1061 if( Minus() ) { 1061 if( Minus() ) {
1062 Spaces(); 1062 Spaces();
1063 Expr exp = required(UnaryExpr()).single(); 1063 Expr exp = required(UnaryExpr()).single();
1064 Expr newExp = new Expr(Val.SINGLE,false); 1064 Expr newExp = new Expr(Val.SINGLE,false);
1065 newExp.add( "LuanImpl.unm(" ); 1065 newExp.add( "LuanImpl.unm(luan," );
1066 newExp.addAll( exp ); 1066 newExp.addAll( exp );
1067 newExp.add( ")" ); 1067 newExp.add( ")" );
1068 return parser.success(newExp); 1068 return parser.success(newExp);
1069 } 1069 }
1070 if( Keyword("not") ) { 1070 if( Keyword("not") ) {
1089 return parser.failure(null); 1089 return parser.failure(null);
1090 if( parser.match('^') ) { 1090 if( parser.match('^') ) {
1091 Spaces(); 1091 Spaces();
1092 Expr exp2 = required(UnaryExpr()); 1092 Expr exp2 = required(UnaryExpr());
1093 Expr newExp = new Expr(Val.SINGLE,false); 1093 Expr newExp = new Expr(Val.SINGLE,false);
1094 newExp.add( "LuanImpl.pow(" ); 1094 newExp.add( "LuanImpl.pow(luan," );
1095 newExp.addAll( exp1.single() ); 1095 newExp.addAll( exp1.single() );
1096 newExp.add( "," ); 1096 newExp.add( "," );
1097 newExp.addAll( exp2.single() ); 1097 newExp.addAll( exp2.single() );
1098 newExp.add( ")" ); 1098 newExp.add( ")" );
1099 exp1 = newExp; 1099 exp1 = newExp;
2021 +"import luan.LuanFunction; " 2021 +"import luan.LuanFunction; "
2022 +"import luan.LuanException; " 2022 +"import luan.LuanException; "
2023 +"import luan.modules.PackageLuan; " 2023 +"import luan.modules.PackageLuan; "
2024 2024
2025 +"public class " + className +" extends LuanClosure { " 2025 +"public class " + className +" extends LuanClosure { "
2026 +"public "+className+"(Luan luan,boolean javaOk,String sourceName) throws LuanException { " 2026 +"public "+className+"(boolean javaOk,String sourceName) throws LuanException { "
2027 +"super(luan,"+toUpValues(upValueSymbols)+",javaOk,sourceName); " 2027 +"super("+toUpValues(upValueSymbols)+",javaOk,sourceName); "
2028 +"} " 2028 +"} "
2029 2029
2030 +"@Override public Object doCall(Luan luan,Object[] args) throws LuanException { " 2030 +"@Override public Object doCall(Luan luan,Object[] args) throws LuanException { "
2031 +"final Pointer[] parentUpValues = upValues; " 2031 +"final Pointer[] parentUpValues = upValues; "
2032 +"Object t; " 2032 +"Object t; "
2064 } 2064 }
2065 2065
2066 Expr toInnerFnExp(List<UpSym> upValueSymbols) { 2066 Expr toInnerFnExp(List<UpSym> upValueSymbols) {
2067 StringBuilder sb = new StringBuilder(); 2067 StringBuilder sb = new StringBuilder();
2068 sb.append( 2068 sb.append(
2069 "new "+className+"(luan(),"+toUpValues(upValueSymbols)+",javaOk,sourceName)" 2069 "new "+className+"("+toUpValues(upValueSymbols)+",javaOk,sourceName)"
2070 ); 2070 );
2071 for( int i=0; i<lines; i++ ) { 2071 for( int i=0; i<lines; i++ ) {
2072 sb.append('\n'); 2072 sb.append('\n');
2073 } 2073 }
2074 2074
2083 name += "$" + diff; 2083 name += "$" + diff;
2084 //name += "_" + lines + "_" + endLine + "_" + line; 2084 //name += "_" + lines + "_" + endLine + "_" + line;
2085 StringBuilder sb = new StringBuilder(); 2085 StringBuilder sb = new StringBuilder();
2086 sb.append( "" 2086 sb.append( ""
2087 +"private static class " + className +" extends LuanClosure { " 2087 +"private static class " + className +" extends LuanClosure { "
2088 +className+"(Luan luan,Pointer[] upValues,boolean javaOk,String sourceName) throws LuanException { " 2088 +className+"(Pointer[] upValues,boolean javaOk,String sourceName) throws LuanException { "
2089 +"super(luan,upValues,javaOk,sourceName); " 2089 +"super(upValues,javaOk,sourceName); "
2090 +"} " 2090 +"} "
2091 +"@Override public Object doCall(Luan luan,Object[] args) throws LuanException { " 2091 +"@Override public Object doCall(Luan luan,Object[] args) throws LuanException { "
2092 +"return _" + name + "(luan,args); " 2092 +"return _" + name + "(luan,args); "
2093 +"} " 2093 +"} "
2094 +"private Object _" + name + "(Luan luan,Object[] args) throws LuanException { " 2094 +"private Object _" + name + "(Luan luan,Object[] args) throws LuanException { "