-
- case DO: {
- if (prefix != null) { pushBackToken(); return prefix; }
- Expr firstBlock = parseBlock(false);
- if (getToken() != WHILE) throw new ParserException("expecting WHILE");
- if (getToken() != LP) throw new ParserException("expected left paren");
- Expr whileExpr = parseMaximalExpr();
- if (getToken() != RP) throw new ParserException("expected right paren");
- if (getToken() != SEMI) throw new ParserException("semicolon");
- return new Expr(curLine, DO, firstBlock, whileExpr);
- }
-
- default:
- pushBackToken();
- return prefix;
- }
- }
-
- // Expr //////////////////////////////////////////////////////////////////////
-
- /** sorta like gcc trees */
- class Expr {
- int code = -1;
-
- final Expr left;
- final Expr right;
- Expr next = null; // if this expr is part of a list
- int line = -1;
- String sourceName = "unknown";
-
- String string = null;
- Number number = null;
-
- public String toString() { return toString(0); }
- public String toString(int indent) {
- String ret = "";
- for(int i=0; i<indent; i++) ret += " ";
- ret += Lexer.codeToString[code];
- if (code == Lexer.NUMBER) ret += " " + number;
- else if (string != null) ret += " \"" + string + "\"";
- ret += "\n";
- if (left != null) ret += left.toString(indent + 2);
- if (right != null) ret += right.toString(indent + 2);
- if (next != null) ret += next.toString(indent);
- return ret;
- }
-
- public Expr(int line, String s) { this(line, Lexer.STRING); this.string = s; } // an identifier or label
- public Expr(int line, int code, String s) { this(line, code); this.string = s; }
- public Expr(int line, Number n) { this(line, Lexer.NUMBER); this.number = n; } // an identifier or label
- public Expr(int line, int code) { this(line, code, null, null); }
- public Expr(int line, int code, Expr left) { this(line, code, left, null); }
- public Expr(int line, int code, Expr left, Expr right) {
- this.code = code; this.left = left; this.right = right;
- this.line = line;
- this.sourceName = Parser.this.sourceName;
- }
-
- public double toDouble(Object o) { return toNumber(o).doubleValue(); }
- public long toLong(Object o) { return toNumber(o).longValue(); }
- public boolean toBoolean(Object o) {
- if (o == null) return false;
- if (o instanceof Boolean) return ((Boolean)o).booleanValue();
- if (o instanceof Number) return o.equals(new Integer(0));
- return true;
- }
-
- public Object eval(final JS.Scope s) throws ControlTransferException, JS.Exn {
- switch(code) {
-
- case Lexer.BITOR: return new Long(toLong(left.eval(s)) | toLong(right.eval(s)));
- case Lexer.BITXOR: return new Long(toLong(left.eval(s)) ^ toLong(right.eval(s)));
- case Lexer.BITAND: return new Long(toLong(left.eval(s)) & toLong(right.eval(s)));
- case Lexer.BITNOT: return new Long(~toLong(left.eval(s)));
-
- case Lexer.ADD: {
- Object l = left.eval(s);
- Object r = right.eval(s);
- if (l instanceof String || r instanceof String) {
- if (l == null) l = "null";
- if (r == null) r = "null";
- if (l instanceof Number && ((Number)l).doubleValue() == ((Number)l).longValue())
- l = new Long(((Number)l).longValue());
- if (r instanceof Number && ((Number)r).doubleValue() == ((Number)r).longValue())
- r = new Long(((Number)r).longValue());
- return l.toString() + r.toString();