// // SAtom.java : シンボル Atom クラス定義 // // This is a part of miolisp sourcefile // Copyright (C) 1998,99 Nishiyama, Naoki / Mio software lab. // // This program is free software; you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation; either version 2 of the License, or // (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this program; if not, write to the Free Software // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA // import java.util.* ; // Symbol Atom final class SAtom implements Atom { public static final String NILSTR = "nil" ; public static final String TSTR = "t" ; public static final String COMMASTR = "comma" ; public static final String COMMAATSTR = "atmark" ; public static final String QUOTESTR = "quote" ; public static final String BQSTR = "backquote" ; // 大域値なしを示すためのポインタ。内容はなんでもよい public static final String NOVALUE = "*NOVALUE*" ; // シンボルテーブルの初期値 public static final int GLOBALENTRIES = 4096 ; // 良く使われるで有ろう表現へのキャッシュ public static SAtom nil , t , comma , commaat , quote , bquote ; // 全ての Symbol へのリファレンスを持つテーブル static Hashtable h = new Hashtable(GLOBALENTRIES) ; // インスタンス変数 Object prop ; // 属性リスト(未実装) Object glob ; // 大域値 String s ; // 印字名 static { nil = getSAtom(NILSTR) ; t = getSAtom(TSTR) ; comma = getSAtom(COMMASTR) ; commaat = getSAtom(COMMAATSTR) ; bquote = getSAtom(BQSTR) ; quote = getSAtom(QUOTESTR) ; } // シンボルの単一性保持のため、コンストラクタの代わりに getSAtom を使う。 public static SAtom getSAtom(String x) { x = x.toLowerCase() ; SAtom s = (SAtom)h.get(x) ; if (s == null) { // 登録されていない場合 s = new SAtom(x) ; h.put(x,s) ; } return s ; } // 外からコンストラクタは呼べないようになっている。 private SAtom(String x) { s = x ; prop = nil ; glob = NOVALUE ; } public String getName() { return s ; } public boolean nullp() { return this == nil ; } public boolean eq(Atom x) { return this == x ; } // 大域値を代入する public void setGlobal(Object o) { glob = o ;} // 大域値を得る public Object getGlobal() { return glob ; } // 辞書引き public Object eval(Environment e) throws EvalException { // nil と t は固定 if ((this == nil) || (this == t)) { return this ;} // Environment.lookup() から SAtom.getGlobal() が呼び返される。 Object res = e.lookup(this) ; if (res == NOVALUE) { throw new EvalException("Symbol <"+s+"> not defined") ; } return res ; } // グローバル辞書を連想リストとして得る static Object getGlobalDic() { return getgdicalist(h.elements()) ; } // グローバル辞書(ハッシュテーブル)を lisp の連想リストとして得る private static Object getgdicalist(Enumeration e) { if (e.hasMoreElements()) { SAtom s = (SAtom)e.nextElement() ; if (s.glob != NOVALUE) { // 値の有るもののみ return new Cons(new Cons(s,s.glob),getgdicalist(e)) ; } else { return getgdicalist(e) ; } } return SAtom.nil ; } }