1 package kawa.standard; 2 import kawa.lang.*; 3 import gnu.expr.*; 4 import gnu.lists.*; 5 import java.util.Stack ; 6 7 public class with_compile_options extends Syntax 8 { 9 public static final with_compile_options with_compile_options 10 = new with_compile_options(); 11 static { with_compile_options.setName("with-compile-options"); } 12 13 public void scanForm (Pair form, ScopeExp defs, Translator tr) 14 { 15 Stack stack = new Stack (); 16 Object rest = getOptions(form.cdr, stack, this, tr); 17 if (rest == LList.Empty) 18 return; 19 if (rest == form.cdr) 20 { 21 tr.scanBody(rest, defs, false); 22 return; 23 } 24 rest = tr.scanBody(rest, defs, true); 25 rest = new Pair(stack, rest); 26 tr.currentOptions.popOptionValues(stack); 27 tr.formStack.add(Translator.makePair(form, form.car, rest)); 28 } 29 30 public static Object getOptions (Object form, Stack stack, 31 Syntax command, Translator tr) 32 { 33 boolean seenKey = false; 34 gnu.text.Options options = tr.currentOptions; 35 SyntaxForm syntax = null; 36 for (;;) 37 { 38 while (form instanceof SyntaxForm) 39 { 40 syntax = (SyntaxForm) form; 41 form = syntax.form; 42 } 43 if (! (form instanceof Pair)) 44 break; 45 Pair pair = (Pair) form; 46 Object pair_car = Translator.stripSyntax(pair.car); 47 if (! (pair_car instanceof Keyword)) 48 break; 49 String key = ((Keyword) pair_car).getName(); 50 seenKey = true; 51 Object savePos = tr.pushPositionOf(pair); 52 try 53 { 54 form = pair.cdr; 55 while (form instanceof SyntaxForm) 56 { 57 syntax = (SyntaxForm) form; 58 form = syntax.form; 59 } 60 if (! (form instanceof Pair)) 61 { 62 tr.error('e', "keyword " + key + " not followed by value"); 63 return LList.Empty; 64 } 65 pair = (Pair) form; 66 Object value = Translator.stripSyntax(pair.car); 67 form = pair.cdr; 68 Object oldValue = options.getLocal(key); 69 if (options.getInfo(key) == null) 70 { 71 tr.error('w', "unknown compile option: "+key); 72 continue; 73 } 74 if (value instanceof FString) 75 value = value.toString(); 76 else if (value instanceof Boolean 77 || value instanceof Number ) 78 ; 79 else 80 { 81 value = null; 82 tr.error('e', "invalid literal value for key "+key); 83 } 84 options.set(key, value, tr.getMessages()); 85 if (stack != null) 86 { 87 stack.push(key); 88 stack.push(oldValue); 89 stack.push(value); 90 } 91 } 92 finally 93 { 94 tr.popPositionOf(savePos); 95 } 96 } 97 if (! seenKey) 98 tr.error('e', "no option keyword in "+command.getName()); 99 return Translator.wrapSyntax(form, syntax); 100 } 101 102 public Expression rewriteForm (Pair form, Translator tr) 103 { 104 Object rest; 105 Stack stack; 106 Object obj = form.cdr; 107 Pair p; 108 if (obj instanceof Pair 109 && (p = (Pair) obj).car instanceof Stack ) 110 { 111 stack = (Stack ) p.car; 112 rest = p.cdr; 113 tr.currentOptions.pushOptionValues(stack); 114 } 115 else 116 { 117 stack = new Stack (); 118 rest = getOptions(obj, stack, this, tr); 119 } 120 121 try 122 { 123 Expression result = tr.rewrite_body(rest); 124 BeginExp bresult; 125 if (result instanceof BeginExp) 126 bresult = (BeginExp) result; 127 else 128 bresult = new BeginExp (new Expression[] { result }); 129 bresult.setCompileOptions(stack); 130 return bresult; 131 } 132 finally 133 { 134 tr.currentOptions.popOptionValues(stack); 135 } 136 } 137 } 138 139 | Popular Tags |