1 package kawa.standard; 2 import kawa.lang.*; 3 import gnu.lists.*; 4 import gnu.expr.*; 5 import gnu.mapping.Symbol; 6 7 public class define_class extends Syntax 8 { 9 public static final define_class define_class 10 = new define_class("define-class", false); 11 public static final define_class define_simple_class 12 = new define_class("define-simple-class", true); 13 14 boolean isSimple; 15 object objectSyntax; 16 17 define_class (object objectSyntax, boolean isSimple) 18 { 19 this.objectSyntax = objectSyntax; 20 this.isSimple = isSimple; 21 } 22 23 define_class (String name, boolean isSimple) 24 { 25 super(name); 26 this.objectSyntax = object.objectSyntax; 27 this.isSimple = isSimple; 28 } 29 30 public boolean scanForDefinitions (Pair st, java.util.Vector forms, 31 ScopeExp defs, Translator tr) 32 { 33 Object st_cdr = st.cdr; 34 SyntaxForm nameSyntax = null; 35 while (st_cdr instanceof SyntaxForm) 36 { 37 nameSyntax = (SyntaxForm) st_cdr; 38 st_cdr = nameSyntax.form; 39 } 40 if (! (st_cdr instanceof Pair)) 41 return super.scanForDefinitions(st, forms, defs, tr); 42 Pair p = (Pair) st_cdr; 43 Object name = p.car; 44 while (name instanceof SyntaxForm) 45 { 46 nameSyntax = (SyntaxForm) name; 47 name = nameSyntax.form; 48 } 49 if (! (name instanceof String || name instanceof Symbol)) 50 return super.scanForDefinitions(st, forms, defs, tr); 51 Declaration decl = tr.define(name, nameSyntax, defs); 52 if (p instanceof PairWithPosition) 53 decl.setLocation((PairWithPosition) p); 54 ClassExp oexp = new ClassExp(isSimple); 55 decl.noteValue(oexp); 56 decl.setFlag(Declaration.IS_CONSTANT); 57 decl.setType(Compilation.typeClassType); 58 tr.mustCompileHere(); 59 60 String cname = name instanceof Symbol ? ((Symbol) name).getName() 61 : name.toString(); 62 int nlen = cname.length(); 63 if (nlen > 2 && cname.charAt(0) == '<' && cname.charAt(nlen-1) == '>') 64 cname = cname.substring(1, nlen-1); 65 oexp.setName(cname); 66 67 Object members = p.cdr; 68 while (members instanceof SyntaxForm) 69 { 70 nameSyntax = (SyntaxForm) members; 71 members = nameSyntax.form; 72 } 73 if (! (members instanceof Pair)) 74 { 75 tr.error('e', "missing class members"); 76 return false; 77 } 78 p = (Pair) members; 79 ScopeExp save_scope = tr.currentScope(); 80 if (nameSyntax != null) 81 tr.setCurrentScope(nameSyntax.scope); 82 Object [] saved = objectSyntax.scanClassDef(p, oexp, tr); 83 if (nameSyntax != null) 84 tr.setCurrentScope(save_scope); 85 if (saved == null) 86 return false; 87 st = Translator.makePair(st, this, Translator.makePair(p, decl, saved)); 88 forms.addElement (st); 89 return true; 90 } 91 92 public Expression rewriteForm (Pair form, Translator tr) 93 { 94 Declaration decl = null; 96 if (form.cdr instanceof Pair) 97 { 98 form = (Pair) form.cdr; 99 if (! (form.car instanceof Declaration)) 100 return tr.syntaxError(this.getName() + " can only be used in <body>"); 101 decl = (Declaration) form.car; 102 } 103 ClassExp oexp = (ClassExp) decl.getValue(); 104 objectSyntax.rewriteClassDef((Object []) form.cdr, tr); 105 SetExp sexp = new SetExp(decl, oexp); 106 sexp.setDefining (true); 107 return sexp; 108 } 109 } 110 | Popular Tags |