comparison src/luan/modules/parsers/LuanToString.java @ 1563:8fbcc4747091

remove LuanFunction.luan
author Franklin Schmidt <fschmidt@gmail.com>
date Mon, 09 Nov 2020 01:37:57 -0700
parents b89212fd04b5
children fa066aaa068c
comparison
equal deleted inserted replaced
1562:b89212fd04b5 1563:8fbcc4747091
17 public boolean numberTypes = false; 17 public boolean numberTypes = false;
18 public boolean compressed = false; 18 public boolean compressed = false;
19 public boolean longStrings = false; 19 public boolean longStrings = false;
20 public boolean inline = false; 20 public boolean inline = false;
21 21
22 void applyOptions(Luan luan,LuanTable options) throws LuanException { 22 void applyOptions(LuanTable options) throws LuanException {
23 Boolean b; 23 Boolean b;
24 b = (Boolean)options.get(luan,"strict"); 24 b = (Boolean)options.rawGet("strict");
25 if( b != null ) 25 if( b != null )
26 strict = b; 26 strict = b;
27 b = (Boolean)options.get(luan,"number_types"); 27 b = (Boolean)options.rawGet("number_types");
28 if( b != null ) 28 if( b != null )
29 numberTypes = b; 29 numberTypes = b;
30 b = (Boolean)options.get(luan,"compressed"); 30 b = (Boolean)options.rawGet("compressed");
31 if( b != null ) 31 if( b != null )
32 compressed = b; 32 compressed = b;
33 b = (Boolean)options.get(luan,"long_strings"); 33 b = (Boolean)options.rawGet("long_strings");
34 if( b != null ) 34 if( b != null )
35 longStrings = b; 35 longStrings = b;
36 b = (Boolean)options.get(luan,"inline"); 36 b = (Boolean)options.rawGet("inline");
37 if( b != null ) 37 if( b != null )
38 inline = b; 38 inline = b;
39 } 39 }
40 40
41 public Settings cloneSettings() { 41 public Settings cloneSettings() {
51 static { 51 static {
52 Collections.addAll(settingsKeys,"strict","number_types","compressed","long_strings","inline"); 52 Collections.addAll(settingsKeys,"strict","number_types","compressed","long_strings","inline");
53 } 53 }
54 54
55 private static void checkOptions(LuanTable options) throws LuanException { 55 private static void checkOptions(LuanTable options) throws LuanException {
56 for( Map.Entry entry : options.iterable() ) { 56 for( Map.Entry entry : options.rawIterable() ) {
57 if( !settingsKeys.contains(entry.getKey()) ) 57 if( !settingsKeys.contains(entry.getKey()) )
58 throw new LuanException("invalid option: "+entry.getKey()); 58 throw new LuanException("invalid option: "+entry.getKey());
59 if( !(entry.getValue() instanceof Boolean) ) 59 if( !(entry.getValue() instanceof Boolean) )
60 throw new LuanException("options values must be boolean"); 60 throw new LuanException("options values must be boolean");
61 } 61 }
62 } 62 }
63 63
64 public final Settings settingsInit = new Settings(); 64 public final Settings settingsInit = new Settings();
65 private final Luan luan;
66 private final LuanTable subOptions; 65 private final LuanTable subOptions;
67 66
68 public LuanToString(Luan luan,LuanTable options,LuanTable subOptions) throws LuanException { 67 public LuanToString(LuanTable options,LuanTable subOptions) throws LuanException {
69 this.luan = luan;
70 this.subOptions = subOptions; 68 this.subOptions = subOptions;
71 if( options != null ) { 69 if( options != null ) {
72 checkOptions(options); 70 checkOptions(options);
73 settingsInit.applyOptions(luan,options); 71 settingsInit.applyOptions(options);
74 } 72 }
75 if( subOptions != null ) { 73 if( subOptions != null ) {
76 for( Map.Entry entry : subOptions.iterable() ) { 74 for( Map.Entry entry : subOptions.rawIterable() ) {
77 /* 75 /*
78 if( !(entry.getKey() instanceof String) ) 76 if( !(entry.getKey() instanceof String) )
79 throw new LuanException("sub_options keys must be strings"); 77 throw new LuanException("sub_options keys must be strings");
80 */ 78 */
81 if( !(entry.getValue() instanceof LuanTable) ) 79 if( !(entry.getValue() instanceof LuanTable) )
181 toString( key, sb, indented, keySettings ); 179 toString( key, sb, indented, keySettings );
182 sb.append( ']' ); 180 sb.append( ']' );
183 } 181 }
184 sb.append( settings.compressed ? "=" : " = " ); 182 sb.append( settings.compressed ? "=" : " = " );
185 if( subOptions != null ) { 183 if( subOptions != null ) {
186 LuanTable options = (LuanTable)subOptions.get(luan,key); 184 LuanTable options = (LuanTable)subOptions.rawGet(key);
187 if( options != null ) { 185 if( options != null ) {
188 settings = settings.cloneSettings(); 186 settings = settings.cloneSettings();
189 settings.applyOptions(luan,options); 187 settings.applyOptions(options);
190 } 188 }
191 } 189 }
192 toString( entry.getValue(), sb, indented, settings ); 190 toString( entry.getValue(), sb, indented, settings );
193 } 191 }
194 192
210 } 208 }
211 209
212 public static void addNumberTypes(Luan luan,LuanTable env) { 210 public static void addNumberTypes(Luan luan,LuanTable env) {
213 try { 211 try {
214 LuanTable module = (LuanTable)luan.require("luan:Number.luan"); 212 LuanTable module = (LuanTable)luan.require("luan:Number.luan");
215 env.put( luan, "double", module.fn(luan,"double") ); 213 env.rawPut( "double", module.fn("double") );
216 env.put( luan, "float", module.fn(luan,"float") ); 214 env.rawPut( "float", module.fn("float") );
217 env.put( luan, "integer", module.fn(luan,"integer") ); 215 env.rawPut( "integer", module.fn("integer") );
218 env.put( luan, "long", module.fn(luan,"long") ); 216 env.rawPut( "long", module.fn("long") );
219 } catch(LuanException e) { 217 } catch(LuanException e) {
220 throw new LuanRuntimeException(e); 218 throw new LuanRuntimeException(e);
221 } 219 }
222 } 220 }
223 221