java/ch/wlkl/wsm/Ex.java

package ch.wlkl.wsm;

import ch.wlkl.wsm.FormatFactory.Fmt;
import ch.wlkl.wsm.FormatFactory.Fmt.Fct;

/**
 * an abstract Expression for execution
 */
public abstract class Ex implements Cloneable, Formattable {

    final public static Ex[] a0 = new Ex[0];

    public abstract Flow exe(Frame cf);

    public void format(Formatter f) {
    }

    public CharSequence format() {
        Formatter f = Formatter.make();
        FormatFactory  m = new FormatFactory();
        formatRulesDefault(m);
        f.formatOk(m.atKey(getClass()), this);
        return f.text();
    }    

    public static void syntax(SyntaxFactory f) {
   //     f.work(Ex.class, new String[][] {    {"%%%"}});
        f.rule("ex", f.orI(f.ref("pr"),f.ref("ass"), f.ref("get"), f.ref("seq")));
        f.rule("pr", f.orI(f.rule(ExCF.class, f.lit("cf")), f.rule(ExConst.class, f.lex("const"))));
        f.rule("seq", ExSeq.class, f.seq(f.ref("ex"), f.zero2n(f.seq(f.lit(";"), f.opt(f.ref("ex"))))));
        f.rule("ass", ExAss.class, f.seq(f.ref("get", -5), f.seq(f.lit("="), f.ref("ex"))));
        f.rule("get", ExGet.class, f.seq(f.ref("pr"), f.lit("["), f.ref("ex"), f.lit("]")));
/*        f.add(f.new FormatSimple(ExAss.class, ", $ , [ $ ] , = $ , :,>>$<,o%>$<%, o% >$<") {
            public Format ele(int fx, Formattable e) {
                return fx != 1 || ! (e instanceof ExAss ||e instanceof ExSeq || e instanceof ExReturn || e instanceof ExThrow) ? atFor(e) : atKey("()") ;
            }
        });
        f.ele(ExCF.class, "cf , "); 
        f.ele(ExConst.class, ", $ ,");
        f.copyTo(ExAss.class, ExGet.class);
        f.ele(ExIf.class, "if , $ , then $ , else $ , endif :, 2 >>$i2<<, % 2>n$i2<, 1n% 2>n$i2<, 1n%"); 
        f.ele(ExNew.class, "new ,");  
        f.ele(ExSeq.class, ", | |1 ; | $ , :,||% 1n|$i1");
        f.ele(ExFor.class, "for , $ , in $ , do $, endfor :, 2 >>$i2<<, 2>n% 2>$i2<<, % 1>n$i1<, 1n%");
        f.ele(ExWhile.class, "while , $ , do $ , endwhile :, 1 >>$i1<<, % 1>n$i1<, 1n%");
        f.ele(ExJFun.class, " , $ ( , | |2 ', | $ , ) :,$%,||% 1n|1>$i1<"); 
        f.ele(ExReturn.class, "return , $ , :, >$<");
        f.ele(ExThrow.class, "throw , $ , :, >$<");
  */  }
    public static void formatRulesDefault(FormatFactory f) {
   //     f.work(Ex.class, new String[][] {    {"%%%"}});
        f.fctNst("()", "( , $ , )");
        f.add(new Fct(f, ExAss.class, ", $ , [ $ ] , = $ , :,>>$<,o%>$<%, o% >$<") {
            public int format(Formatter f, int i, Formattable e) {
                if (i == 1 && (e instanceof ExAss || e instanceof ExSeq || e instanceof ExReturn || e instanceof ExThrow))
                    f.format(fact.atKey("()"), e);
                else
                    return super.format(f, i, e);
                return -1;
            }
        });
        f.fct(ExCF.class, "cf , ");
        f.fct(ExConst.class, ", $ ,");
        f.add(new Fct(f, ExGet.class, ", $ , [ $ ] , :,>>$<,o%>$<%,") {
            public int format(Formatter f, int i, Formattable e) {
                if (i == 1 && (e instanceof ExAss || e instanceof ExSeq || e instanceof ExReturn || e instanceof ExThrow))
                    f.format(fact.atKey("()"), e);
                else
                    return super.format(f, i, e);
                return -1;
            }
        });
        f.fct(ExIf.class, "if , $ , then $ , else $ , endif :, 2 >>$i2<<, % 2>n$i2<, 1n% 2>n$i2<, 1n%");
        f.fct(ExNew.class, "new ,");  
        f.fct(ExSeq.class, ", | |1 ; | $ , :,||% 1n|$i1");
        f.fct(ExFor.class, "for , $ , in $ , do $, endfor :, 2 >>$i2<<, 2>n% 2>$i2<<, % 1>n$i1<, 1n%");
        f.fct(ExWhile.class, "while , $ , do $ , endwhile :, 1 >>$i1<<, % 1>n$i1<, 1n%");
        f.fct(ExJFun.class, " , $ ( , | |2 ', | $ , ) :,$%,||% 1n|1>$i1<"); 
        f.fct(ExReturn.class, "return , $ , :, >$<");
        f.fct(ExThrow.class, "throw , $ , :, >$<");
    }        
}