001: package gnu.kawa.brl;
002:
003: import gnu.mapping.*;
004: import kawa.standard.Scheme;
005: import gnu.lists.*;
006: import gnu.xml.*;
007: import gnu.expr.*;
008: import gnu.kawa.lispexpr.ReadTable;
009:
010: public class BRL extends Scheme {
011: // The following two fields need to be public so that the findLiteral
012: // method in gnu.expr.LitTable can find them.
013: /** Language instance for KRL dialect. */
014: public static final BRL krl_instance;
015: /** Language instance for BRL dialect. */
016: public static final BRL brl_instance;
017: static final Object emptyForm = new FString();
018:
019: protected static final SimpleEnvironment brlEnvironment = Environment
020: .make("brl-environment", Scheme.kawaEnvironment);
021:
022: static BRLReaderString brlReader = new BRLReaderString();
023:
024: static {
025: krl_instance = new BRL(brlEnvironment);
026: brl_instance = new BRL(brlEnvironment);
027: brl_instance.setBrlCompatible(true);
028: CallContext ctx = CallContext.getInstance();
029: Environment saveEnv = ctx.getEnvironmentRaw();
030: try {
031: ctx.setEnvironmentRaw(brlEnvironment);
032: krl_instance.initBRL();
033: } finally {
034: ctx.setEnvironmentRaw(saveEnv);
035: }
036: }
037:
038: protected BRL(Environment env) {
039: super (env);
040: }
041:
042: void initBRL() {
043: ModuleBody.setMainPrintValues(true);
044: try {
045: loadClass("gnu.brl.stringfun");
046: loadClass("gnu.kawa.brl.progfun");
047: loadClass("gnu.kawa.servlet.HTTP");
048: } catch (Throwable ex) {
049: System.err.println("caught " + ex);
050: }
051: }
052:
053: public static Language getInstance(boolean brlCompatible) {
054: return brlCompatible ? getBrlInstance() : getBrlInstance();
055: }
056:
057: public static BRL getKrlInstance() {
058: return krl_instance;
059: }
060:
061: public static BRL getBrlInstance() {
062: return brl_instance;
063: }
064:
065: boolean brlCompatible = false;
066:
067: public boolean isBrlCompatible() {
068: return brlCompatible;
069: }
070:
071: public void setBrlCompatible(boolean compat) {
072: brlCompatible = compat;
073: }
074:
075: public gnu.text.Lexer getLexer(InPort inp,
076: gnu.text.SourceMessages messages) {
077: Compilation.defaultCallConvention = Compilation.CALL_WITH_CONSUMER;
078: BRLRead lexer = new BRLRead(inp, messages);
079: lexer.setBrlCompatible(isBrlCompatible());
080: return lexer;
081: }
082:
083: public Consumer getOutputConsumer(java.io.Writer out) {
084: if (isBrlCompatible())
085: return super .getOutputConsumer(out);
086: return new XMLPrinter(out, false);
087: }
088:
089: /** The compiler insert calls to this method for applications and applets. */
090: public static void registerEnvironment() {
091: Language.setDefaults(getKrlInstance());
092: }
093:
094: public Expression makeBody(Expression[] exps) {
095: if (isBrlCompatible())
096: return super .makeBody(exps);
097: return new ApplyExp(
098: gnu.kawa.functions.AppendValues.appendValues, exps);
099: }
100:
101: public ReadTable createReadTable() {
102: ReadTable rt = super .createReadTable();
103: rt.setBracketMode(1);
104: rt.set(']', brlReader);
105: return rt;
106: }
107:
108: public Procedure getPrompter() {
109: return new Prompter();
110: }
111: }
112:
113: class Prompter extends Procedure1 {
114: public Object apply1(Object arg) {
115: InPort port = (InPort) arg;
116: int line = port.getLineNumber() + 1;
117: char state = port.readState;
118: if (state == ']')
119: return "<!--BRL:" + line + "-->";
120: else {
121: if (state == '\n')
122: state = '-';
123: return "#|--BRL:" + line + state + "|#";
124: }
125: }
126: }
|