From: megacz Date: Fri, 30 Jan 2004 07:00:52 +0000 (+0000) Subject: 2003/06/07 12:00:57 X-Git-Tag: RC3~941 X-Git-Url: http://git.megacz.com/?p=org.ibex.core.git;a=commitdiff_plain;h=1bd9bc0c2100ba4a1dc2a5f4ca25ec9e0d1b2265 2003/06/07 12:00:57 darcs-hash:20040130070052-2ba56-0ca24839f224fc99ea30133b9719a63a1d9b6eb9.gz --- diff --git a/Makefile b/Makefile index 5cd2d79..9b58464 100644 --- a/Makefile +++ b/Makefile @@ -25,7 +25,7 @@ cc_objects := $(cc_sources:src/%.cc=bin-$(platform)/%.cc.o) c_objects := $(c_sources:src/%.c=bin-$(platform)/%.c.o) # tools -gcc_path := $(shell pwd)/gcc +gcc_path := $(shell pwd)/gcc/install #gcc_optimizations := -O9 -ffast-math -fomit-frame-pointer -foptimize-sibling-calls -finline-functions #gcc_optimzations += -funroll-loops -ffunction-sections -fdata-sections gcc_optimizations := -O0 @@ -137,7 +137,7 @@ propose-patch: ## GCJ-Derived Platforms ####################################################################### # if the user doesn't change gcc_path, and it's not built, then we'll try to build it -$(shell pwd)/gcc/bin/$(target)-gcj: +$(shell pwd)/gcc/install/bin/$(target)-gcj: make -C gcc # java_classes is here to force compilation of the .class files (they get used via -Ibin/) without diff --git a/src/org/xwt/Trap.java b/src/org/xwt/Trap.java index 8d9404e..c0dd670 100644 --- a/src/org/xwt/Trap.java +++ b/src/org/xwt/Trap.java @@ -123,15 +123,6 @@ public class Trap { public Object _call(JS.Array args) { return _call(args, JS.getCurrentFunction()); } public Object _call(JS.Array args, Function currentFunction) { Trap currentTrap = TrapContext.get().currentTrap; - /* - if (currentTrap == null || (currentFunction != currentTrap.f)) { - if (Log.on) Log.log(this, "attempt to cascade() by a function that was not invoked as a trap at " + - currentFunction.getSourceName()); - if (Log.on) Log.log(this, "currentfunction == " + currentFunction); - if (Log.on) Log.log(this, "currentTrap.f == " + currentTrap.f); - return null; - } - */ if (args.length() != 0) TrapContext.get().putCascadeHappened = true; Trap t = currentTrap.next; // if we've hit the end of the trap stack, just do a put(,,,true) diff --git a/src/org/xwt/js/ForthBlock.java b/src/org/xwt/js/ByteCodeBlock.java similarity index 62% rename from src/org/xwt/js/ForthBlock.java rename to src/org/xwt/js/ByteCodeBlock.java index 4741d76..fb2d1f5 100644 --- a/src/org/xwt/js/ForthBlock.java +++ b/src/org/xwt/js/ByteCodeBlock.java @@ -1,11 +1,15 @@ -// Copyright 2002 Adam Megacz, see the COPYING file for licensing [GPL] +// Copyright 2003 Adam Megacz, see the COPYING file for licensing [GPL] package org.xwt.js; import org.xwt.util.*; import java.io.*; -/** a block of Forth bytecode */ -class ForthBlock implements OpCodes, Tokens { +/** + * A block of JavaScript bytecode. + * + * The JavaScript ByteCode runs on a very simple stack machine. + */ +class ByteCodeBlock implements ByteCodes, Tokens { int line; String sourceName; @@ -13,14 +17,14 @@ class ForthBlock implements OpCodes, Tokens { Object[] arg = new Object[10]; int size = 0; - public ForthBlock(int line, String sourceName) { this.line = line; this.sourceName = sourceName; } - public ForthBlock(int line, String sourceName, int op_, Object arg_) { this(line, sourceName); add(op_, arg_); } + public ByteCodeBlock(int line, String sourceName) { this.line = line; this.sourceName = sourceName; } + public ByteCodeBlock(int line, String sourceName, int op_, Object arg_) { this(line, sourceName); add(op_, arg_); } public int size() { return size; } public void set(int pos, int op_, Object arg_) { op[pos] = op_; arg[pos] = arg_; } - public void paste(ForthBlock other) { for(int i=0; i> JS.toLong(right))); break; - case Lexer.URSH: t.push(new Long(JS.toLong(left) >>> JS.toLong(right))); break; + case LSH: t.push(new Long(JS.toLong(left) << JS.toLong(right))); break; + case RSH: t.push(new Long(JS.toLong(left) >> JS.toLong(right))); break; + case URSH: t.push(new Long(JS.toLong(left) >>> JS.toLong(right))); break; - // FIXME: these need to work on strings - case Lexer.LT: t.push(JS.toDouble(left) < JS.toDouble(right) ? Boolean.TRUE : Boolean.FALSE); break; - case Lexer.LE: t.push(JS.toDouble(left) <= JS.toDouble(right) ? Boolean.TRUE : Boolean.FALSE); break; - case Lexer.GT: t.push(JS.toDouble(left) > JS.toDouble(right) ? Boolean.TRUE : Boolean.FALSE); break; - case Lexer.GE: t.push(JS.toDouble(left) >= JS.toDouble(right) ? Boolean.TRUE : Boolean.FALSE); break; + case LT: t.push(JS.toDouble(left) < JS.toDouble(right) ? Boolean.TRUE : Boolean.FALSE); break; + case LE: t.push(JS.toDouble(left) <= JS.toDouble(right) ? Boolean.TRUE : Boolean.FALSE); break; + case GT: t.push(JS.toDouble(left) > JS.toDouble(right) ? Boolean.TRUE : Boolean.FALSE); break; + case GE: t.push(JS.toDouble(left) >= JS.toDouble(right) ? Boolean.TRUE : Boolean.FALSE); break; - case Lexer.EQ: - case Lexer.NE: { - // FIXME: should use Javascript coercion-equality rules + case EQ: + case NE: { Object l = left; Object r = right; boolean ret; @@ -232,25 +206,21 @@ class ForthBlock implements OpCodes, Tokens { else if (l instanceof Number) ret = JS.toNumber(r).doubleValue() == JS.toNumber(l).doubleValue(); else if (l instanceof String) ret = r != null && l.equals(r.toString()); else ret = l.equals(r); - t.push(new Boolean(op[i] == Lexer.EQ ? ret : !ret)); break; + t.push(new Boolean(op[i] == EQ ? ret : !ret)); break; } default: throw new Error("unknown opcode " + op[i]); } } } - if (t.size() != 1) { - for(int i=0; i= 0 ? codeToString[op[i]] : "" + op[i]) + " [" + arg[i] + "]"); - } - throw new EvaluatorException(line, sourceName, "eval() terminated with " + t.size() + " elements on the stack; one expected"); - } + if (t.size() != 1) + throw new EvaluatorException(line, sourceName, "eval() terminated with " + t.size() + + " elements on the stack; one expected"); return t.pop(); } public Object doGet(final Object o, final Object v) { - if (o == null) { + if (o == null) throw new EvaluatorException(line, sourceName, "tried to get property \"" + v + "\" from the null value"); - } if (o instanceof String) { if (v.equals("length")) return new Integer(((String)o).length()); else if (v.equals("substring")) return new JS.Function() { @@ -296,21 +266,13 @@ class ForthBlock implements OpCodes, Tokens { return null; } - static class Stack { - public Object[] os = new Object[256]; - private int size = 0; - public void push(Object o) { os[size++] = o; } - public Object pop() { return os[--size]; } - public Object peek() { return os[size - 1]; } - public void swap() { Object temp = os[size - 1]; os[size - 1] = os[size - 2]; os[size - 2] = temp; } - public int size() { return size; } - } - static class EvaluatorException extends RuntimeException { - public EvaluatorException(int line, String sourceName, String s) { super(sourceName + ":" + line + " " + s); } + public EvaluatorException(int line, String sourceName, String s) { + super(sourceName + ":" + line + " " + s); + } } - static abstract class ControlTransferException extends Exception { } + static abstract class ControlTransferException extends RuntimeException { } static class BreakException extends ControlTransferException { public String label; BreakException(String label) { this.label = label; } diff --git a/src/org/xwt/js/OpCodes.java b/src/org/xwt/js/ByteCodes.java similarity index 83% rename from src/org/xwt/js/OpCodes.java rename to src/org/xwt/js/ByteCodes.java index 428d06f..8e79a9a 100644 --- a/src/org/xwt/js/OpCodes.java +++ b/src/org/xwt/js/ByteCodes.java @@ -1,8 +1,15 @@ -// Copyright 2002 Adam Megacz, see the COPYING file for licensing [GPL] +// Copyright 2003 Adam Megacz, see the COPYING file for licensing [GPL] package org.xwt.js; -/** each instruction is an opcode and an optional literal literal. */ -public interface OpCodes { +/** + * Constants for the various JavaScript ByteCode operations. + * + * Each instruction is an opcode and an optional literal literal; the + * arithmetic Tokens are also valid. They are: BITOR, BITXOR, BITAND, + * ADD, INC, DEC, SUB, MUL, DIV, MOD, LSH, RSH, URSH, LT, LE, GT, GE, + * EQ, NE + */ +interface ByteCodes { /** push the literal onto the stack */ public static final byte LITERAL = -2; @@ -14,15 +21,13 @@ public interface OpCodes { public static final byte OBJECT = -4; /** create a new instance; literal is a reference to the corresponding ForthBlock */ - public static final byte FUNCTION = -5; + public static final byte NEWFUNCTION = -5; /** pop a string off the stack and declare it in the current scope */ public static final byte DECLARE = -6; /** push a reference to the current scope onto the stack */ - /* - public static final byte THIS = -7; - */ + public static final byte TOPSCOPE = -7; /** pop two elements off the stack; push stack[-1].get(stack[top]) */ public static final byte GET = -8; @@ -51,9 +56,6 @@ public interface OpCodes { /** pop an element; push a JS.Array containing the keys of the popped element */ public static final byte PUSHKEYS = -19; - /** FIXME: execute the ForthBlock pointed to by the literal */ - public static final byte EXPR = -20; - /** swap the top two elements on the stack */ public static final byte SWAP = -23; diff --git a/src/org/xwt/js/JS.java b/src/org/xwt/js/JS.java index 65bb465..c6b4c77 100644 --- a/src/org/xwt/js/JS.java +++ b/src/org/xwt/js/JS.java @@ -5,26 +5,25 @@ import org.xwt.util.*; import java.io.*; import java.util.*; -/** The public API for the JS engine */ -// FEATURE: try using mutable, recycled 'Num' objects +/** + * The public API for the JS engine; JS itself is actually a class + * implementing the minimal amount of functionality for an Object + * which can be manipulated by JavaScript code. + */ public abstract class JS { + // Static Methods ////////////////////////////////////////////////////////////////////// - public static Function getCurrentFunction() { - return (Function)currentFunction.get(Thread.currentThread()); - } - public static String getCurrentFunctionSourceName() { - return getCurrentFunctionSourceName(Thread.currentThread()); - } + private static Hashtable currentFunction = new Hashtable(); + public static Function getCurrentFunction() { return (Function)currentFunction.get(Thread.currentThread()); } + public static String getCurrentFunctionSourceName() { return getCurrentFunctionSourceName(Thread.currentThread()); } + public static String getFileAndLine() { return getCurrentFunctionSourceName() + ":" + getCurrentFunction().getLine(); } public static String getCurrentFunctionSourceName(Thread t) { Function f = (Function)currentFunction.get(t); if (f == null) return "null"; return f.getSourceName(); } - public static String getFileAndLine() { - return "unknown:??"; - } public static boolean toBoolean(Object o) { if (o == null) return false; @@ -32,6 +31,7 @@ public abstract class JS { if (o instanceof Number) return o.equals(new Integer(0)); return true; } + public static long toLong(Object o) { return toNumber(o).longValue(); } public static double toDouble(Object o) { return toNumber(o).doubleValue(); } public static Number toNumber(Object o) { @@ -40,10 +40,10 @@ public abstract class JS { if (o instanceof String) try { return new Double((String)o); } catch (NumberFormatException e) { return new Double(0); } if (o instanceof Boolean) return ((Boolean)o).booleanValue() ? new Long(1) : new Long(0); if (o instanceof JS) return ((JS)o).coerceToNumber(); - // FIXME - throw new Error("toNumber() got object of type " + o.getClass().getName()); + throw new Error("toNumber() got object of type " + o.getClass().getName() + " which we don't know how to handle"); } + // Instance Methods //////////////////////////////////////////////////////////////////// public abstract Object get(Object key) throws JS.Exn; @@ -57,6 +57,7 @@ public abstract class JS { // Subclasses ///////////////////////////////////////////////////////////////////////// + /** A slightly more featureful version of JS */ public static class Obj extends JS { private Hash entries = new Hash(); private boolean sealed = false; @@ -68,6 +69,7 @@ public abstract class JS { public Object[] keys() { return(entries.keys()); } } + /** An exception which can be thrown and caught by JavaScripts */ public static class Exn extends RuntimeException { private Object js = null; public Exn(Object js) { this.js = js; } @@ -76,6 +78,7 @@ public abstract class JS { public Object getObject() { return js; } } + /** A JavaScript Array */ public static class Array extends Obj { private Vec vec = new Vec(); public Array() { } @@ -128,33 +131,35 @@ public abstract class JS { public void setElementAt(Object o, int i) { vec.setElementAt(o, i); } } - public static Hashtable currentFunction = new Hashtable(); + /** Anything that is callable */ public static abstract class Function extends Obj { - public abstract Object _call(JS.Array args) throws JS.Exn; public String getSourceName() throws JS.Exn { return "unknown"; } public int getLine() throws JS.Exn { return -1; } - public final Object call(JS.Array args) throws JS.Exn { return _call(args); } - } - - public static class Script extends Function { - Vector e = null; - private Script(Vector e) { this.e = e; } - public String getSourceName() throws JS.Exn { return ((ForthBlock)e.elementAt(0)).sourceName; } - public Object _call(JS.Array args) throws JS.Exn { - Scope rootScope = (Scope)args.elementAt(0); + public abstract Object _call(JS.Array args) throws JS.Exn, ByteCodeBlock.ControlTransferException; + public final Object call(JS.Array args) throws JS.Exn { Function saved = (Function)currentFunction.get(Thread.currentThread()); currentFunction.put(Thread.currentThread(), this); try { - for(int i=0; iminPrecedence */ - public ForthBlock startExpr() throws IOException { return startExpr(-1); } - public void startExpr(ForthBlock block) throws IOException { startExpr(-1, block); } - public ForthBlock startExpr(int minPrecedence) throws IOException { - ForthBlock ret = new ForthBlock(line, sourceName); + public ByteCodeBlock startExpr() throws IOException { return startExpr(-1); } + public void startExpr(ByteCodeBlock block) throws IOException { startExpr(-1, block); } + public ByteCodeBlock startExpr(int minPrecedence) throws IOException { + ByteCodeBlock ret = new ByteCodeBlock(line, sourceName); startExpr(minPrecedence, ret); return ret.size() == 0 ? null : ret; } - public void startExpr(int minPrecedence, ForthBlock appendTo) throws IOException { + public void startExpr(int minPrecedence, ByteCodeBlock appendTo) throws IOException { int tok = getToken(); int curLine = line; if (tok == -1) return; @@ -82,33 +83,33 @@ public class Parser extends Lexer implements OpCodes { if (precedence[tok] < minPrecedence || (precedence[tok] == minPrecedence && !isRightAssociative[tok])) { pushBackToken(); return; } - ForthBlock b = appendTo; + ByteCodeBlock b = appendTo; switch (tok) { - case NUMBER: continueExpr(b.add(ForthBlock.LITERAL, number), minPrecedence); return; - case STRING: continueExpr(b.add(ForthBlock.LITERAL, string), minPrecedence); return; - case THIS: continueExpr(b.add(THIS, null), minPrecedence); return; - case NULL: continueExpr(b.add(ForthBlock.LITERAL, null), minPrecedence); return; - case TRUE: case FALSE: continueExpr(b.add(ForthBlock.LITERAL, new Boolean(tok == TRUE)), minPrecedence); return; + case NUMBER: continueExpr(b.add(ByteCodeBlock.LITERAL, number), minPrecedence); return; + case STRING: continueExpr(b.add(ByteCodeBlock.LITERAL, string), minPrecedence); return; + case THIS: continueExpr(b.add(TOPSCOPE, null), minPrecedence); return; + case NULL: continueExpr(b.add(ByteCodeBlock.LITERAL, null), minPrecedence); return; + case TRUE: case FALSE: continueExpr(b.add(ByteCodeBlock.LITERAL, new Boolean(tok == TRUE)), minPrecedence); return; case LB: { - b.add(b.ARRAY, new Integer(0)); + b.add(ARRAY, new Integer(0)); int i = 0; while(true) { int size = b.size(); startExpr(b); if (size == b.size()) if (peekToken() == RB) { consume(RB); continueExpr(b, minPrecedence); return; } - b.add(b.LITERAL, new Integer(i++)); - if (size == b.size()) b.add(b.LITERAL, null); - b.add(b.PUT); - b.add(b.POP); + b.add(LITERAL, new Integer(i++)); + if (size == b.size()) b.add(LITERAL, null); + b.add(PUT); + b.add(POP); if (peekToken() == RB) { consume(RB); continueExpr(b, minPrecedence); return; } consume(COMMA); } } case SUB: { consume(NUMBER); - continueExpr(b.add(ForthBlock.LITERAL, new Double(number.doubleValue() * -1)), minPrecedence); + continueExpr(b.add(ByteCodeBlock.LITERAL, new Double(number.doubleValue() * -1)), minPrecedence); return; } case LP: { @@ -131,16 +132,16 @@ public class Parser extends Lexer implements OpCodes { return; } case LC: { - b.add(b.OBJECT, null); + b.add(OBJECT, null); if (peekToken() == RC) { consume(RC); continueExpr(b, minPrecedence); return; } while(true) { if (peekToken() != NAME && peekToken() != STRING) throw new Error("expected NAME or STRING"); getToken(); - b.add(b.LITERAL, string); + b.add(LITERAL, string); consume(COLON); startExpr(b); - b.add(b.PUT); - b.add(b.POP); + b.add(PUT); + b.add(POP); if (peekToken() == RC) { consume(RC); continueExpr(b, minPrecedence); return; } consume(COMMA); if (peekToken() == RC) { consume(RC); continueExpr(b, minPrecedence); return; } @@ -150,61 +151,61 @@ public class Parser extends Lexer implements OpCodes { String name = string; if (peekToken() == ASSIGN) { consume(ASSIGN); - b.add(THIS); - b.add(ForthBlock.LITERAL, name); + b.add(TOPSCOPE); + b.add(ByteCodeBlock.LITERAL, name); startExpr(minPrecedence, b); - b.add(ForthBlock.PUT); - b.add(ForthBlock.SWAP); - b.add(ForthBlock.POP); + b.add(ByteCodeBlock.PUT); + b.add(ByteCodeBlock.SWAP); + b.add(ByteCodeBlock.POP); } else { - b.add(THIS); - b.add(ForthBlock.LITERAL, name); - b.add(ForthBlock.GET); + b.add(TOPSCOPE); + b.add(ByteCodeBlock.LITERAL, name); + b.add(ByteCodeBlock.GET); } continueExpr(b, minPrecedence); return; } - case Tokens.FUNCTION: { + case FUNCTION: { consume(LP); int numArgs = 0; - ForthBlock b2 = new ForthBlock(curLine, sourceName); - b2.add(THIS); - b2.add(b.SWAP); - b2.add(b.LITERAL, "arguments"); - b2.add(b.LITERAL, "arguments"); - b2.add(b.DECLARE); - b2.add(b.SWAP); - b2.add(b.PUT); - b2.add(b.SWAP); - b2.add(b.POP); + ByteCodeBlock b2 = new ByteCodeBlock(curLine, sourceName); + b2.add(TOPSCOPE); + b2.add(SWAP); + b2.add(LITERAL, "arguments"); + b2.add(LITERAL, "arguments"); + b2.add(DECLARE); + b2.add(SWAP); + b2.add(PUT); + b2.add(SWAP); + b2.add(POP); if (peekToken() == RP) consume(RP); else while(true) { if (peekToken() == COMMA) { - // FIXME: pop an item off the stack here? consume(COMMA); + } else { consume(NAME); // declare the name - b2.add(b.LITERAL, string); - b2.add(b.DECLARE); + b2.add(LITERAL, string); + b2.add(DECLARE); // retrieve it from the arguments array - b2.add(b.LITERAL, new Integer(numArgs)); - b2.add(b.GET_PRESERVE); - b2.add(b.SWAP); - b2.add(b.POP); + b2.add(LITERAL, new Integer(numArgs)); + b2.add(GET_PRESERVE); + b2.add(SWAP); + b2.add(POP); // put it to the current scope - b2.add(THIS); - b2.add(b.SWAP); - b2.add(b.LITERAL, string); - b2.add(b.SWAP); - b2.add(b.PUT); + b2.add(TOPSCOPE); + b2.add(SWAP); + b2.add(LITERAL, string); + b2.add(SWAP); + b2.add(PUT); // clean the stack - b2.add(b.POP); - b2.add(b.POP); + b2.add(POP); + b2.add(POP); if (peekToken() == RP) { consume(RP); break; } consume(COMMA); @@ -212,18 +213,18 @@ public class Parser extends Lexer implements OpCodes { numArgs++; } // pop off the arguments array - b2.add(b.POP); + b2.add(POP); parseStatement(true, b2); - b2.add(b.LITERAL, null); + b2.add(LITERAL, null); b2.add(RETURN); - continueExpr(b.add(OpCodes.FUNCTION, b2), minPrecedence); + continueExpr(b.add(NEWFUNCTION, b2), minPrecedence); return; } default: pushBackToken(); return; } } - public void continueExpr(ForthBlock prefix, int minPrecedence) throws IOException { + public void continueExpr(ByteCodeBlock prefix, int minPrecedence) throws IOException { int tok = getToken(); int curLine = line; if (tok == -1) return; @@ -232,7 +233,7 @@ public class Parser extends Lexer implements OpCodes { { pushBackToken(); return; } if (prefix == null) throw new Error("got null prefix"); - ForthBlock b = prefix; + ByteCodeBlock b = prefix; switch (tok) { @@ -241,9 +242,9 @@ public class Parser extends Lexer implements OpCodes { prefix.set(prefix.size() - 1, b.GET_PRESERVE, new Boolean(true)); startExpr(precedence[tok - 1], b); prefix.add(tok - 1); - prefix.add(b.PUT); - prefix.add(b.SWAP); - prefix.add(b.POP); + prefix.add(PUT); + prefix.add(SWAP); + prefix.add(POP); continueExpr(b, minPrecedence); return; } @@ -265,7 +266,7 @@ public class Parser extends Lexer implements OpCodes { consume(COMMA); } consume(RP); - b.add(b.CALL, new Integer(i)); + b.add(CALL, new Integer(i)); continueExpr(b, minPrecedence); return; } @@ -284,8 +285,8 @@ public class Parser extends Lexer implements OpCodes { int size = b.size(); startExpr(precedence[tok], b); b.arg[size - 1] = new Integer(b.size() - size + 2); - b.add(b.JMP, new Integer(2)); - b.add(b.LITERAL, tok == AND ? new Boolean(false) : new Boolean(true)); + b.add(JMP, new Integer(2)); + b.add(LITERAL, tok == AND ? new Boolean(false) : new Boolean(true)); continueExpr(b, minPrecedence); return; } @@ -295,14 +296,14 @@ public class Parser extends Lexer implements OpCodes { String target = string; if (peekToken() == ASSIGN) { consume(ASSIGN); - b.add(b.LITERAL, target); + b.add(LITERAL, target); startExpr(b); - b.add(b.PUT); - b.add(b.SWAP); - b.add(b.POP); + b.add(PUT); + b.add(SWAP); + b.add(POP); } else { - b.add(b.LITERAL, target); - b.add(b.GET); + b.add(LITERAL, target); + b.add(GET); } continueExpr(b, minPrecedence); return; @@ -314,22 +315,22 @@ public class Parser extends Lexer implements OpCodes { if (peekToken() == ASSIGN) { consume(ASSIGN); startExpr(b); - b.add(b.PUT); - b.add(b.SWAP); - b.add(b.POP); + b.add(PUT); + b.add(SWAP); + b.add(POP); } else { - b.add(b.GET); + b.add(GET); } continueExpr(b, minPrecedence); return; } case HOOK: { - b.add(b.JF, new Integer(0)); + b.add(JF, new Integer(0)); int size = b.size(); startExpr(b); b.arg[size - 1] = new Integer(b.size() - size + 2); - b.add(b.JMP, new Integer(0)); + b.add(JMP, new Integer(0)); consume(COLON); size = b.size(); startExpr(b); @@ -343,8 +344,8 @@ public class Parser extends Lexer implements OpCodes { } /** a block is either a single statement or a list of statements surrounded by curly braces; all expressions are also statements */ - public ForthBlock parseStatement() throws IOException { - ForthBlock ret = new ForthBlock(line, sourceName); + public ByteCodeBlock parseStatement() throws IOException { + ByteCodeBlock ret = new ByteCodeBlock(line, sourceName); ret.add(ret.LITERAL, null); parseStatement(false, ret); if (ret.size() == 1) return null; @@ -352,9 +353,9 @@ public class Parser extends Lexer implements OpCodes { } public void parseStatement(boolean requireBraces) throws IOException { - parseStatement(requireBraces, new ForthBlock(line, sourceName)); + parseStatement(requireBraces, new ByteCodeBlock(line, sourceName)); } - public void parseStatement(boolean requireBraces, ForthBlock b) throws IOException { + public void parseStatement(boolean requireBraces, ByteCodeBlock b) throws IOException { int tok = peekToken(); if (tok == -1) return; boolean braced = tok == LC; @@ -366,7 +367,7 @@ public class Parser extends Lexer implements OpCodes { case THROW: case RETURN: case ASSERT: { getToken(); - if (tok == RETURN && peekToken() == SEMI) b.add(b.LITERAL, null); + if (tok == RETURN && peekToken() == SEMI) b.add(LITERAL, null); else startExpr(b); consume(SEMI); b.add(tok); @@ -388,23 +389,23 @@ public class Parser extends Lexer implements OpCodes { case VAR: { consume(VAR); - b.add(THIS); // push the current scope + b.add(TOPSCOPE); // push the current scope while(true) { consume(NAME); String name = string; - b.add(b.LITERAL, name); // push the name to be declared - b.add(b.DECLARE); // declare it + b.add(LITERAL, name); // push the name to be declared + b.add(DECLARE); // declare it if (peekToken() == ASSIGN) { // if there is an '=' after the variable name - b.add(b.LITERAL, name); // put the var name back on the stack + b.add(LITERAL, name); // put the var name back on the stack consume(ASSIGN); startExpr(b); - b.add(b.PUT); - b.add(b.POP); + b.add(PUT); + b.add(POP); } if (peekToken() != COMMA) break; consume(COMMA); } - b.add(b.POP); + b.add(POP); if (peekToken() == SEMI) consume(SEMI); break; } @@ -415,32 +416,31 @@ public class Parser extends Lexer implements OpCodes { startExpr(b); consume(RP); - b.add(b.JF, new Integer(0)); + b.add(JF, new Integer(0)); int size = b.size(); parseStatement(false, b); if (peekToken() == ELSE) { consume(ELSE); b.arg[size - 1] = new Integer(2 + b.size() - size); - b.add(b.JMP, new Integer(3)); - b.add(b.EXPR, parseStatement()); - b.add(b.POP); - } else { - b.arg[size - 1] = new Integer(1 + b.size() - size); + b.add(JMP, new Integer(0)); + size = b.size(); + parseStatement(false, b); } + b.arg[size - 1] = new Integer(1 + b.size() - size); break; } case WHILE: { consume(WHILE); consume(LP); - ForthBlock loop = new ForthBlock(curLine, sourceName); - b.add(loop.LOOP, loop); + ByteCodeBlock loop = new ByteCodeBlock(curLine, sourceName); + b.add(LOOP, loop); - loop.add(loop.POP); + loop.add(POP); startExpr(loop); - loop.add(loop.JT, new Integer(2)); - loop.add(Lexer.BREAK); + loop.add(JT, new Integer(2)); + loop.add(BREAK); consume(RP); parseStatement(false, loop); @@ -452,19 +452,19 @@ public class Parser extends Lexer implements OpCodes { case SWITCH: { consume(SWITCH); consume(LP); - ForthBlock loop = new ForthBlock(curLine, sourceName); - b.add(loop.LOOP, loop); + ByteCodeBlock loop = new ByteCodeBlock(curLine, sourceName); + b.add(LOOP, loop); startExpr(loop); consume(RP); consume(LC); while(true) if (peekToken() == CASE) { consume(CASE); - loop.add(loop.DUP); + loop.add(DUP); startExpr(loop); consume(COLON); loop.add(EQ); - loop.add(loop.JF, new Integer(0)); + loop.add(JF, new Integer(0)); int size = loop.size(); while(peekToken() != CASE && peekToken() != DEFAULT && peekToken() != RC) { int size2 = loop.size(); @@ -492,23 +492,22 @@ public class Parser extends Lexer implements OpCodes { case DO: { consume(DO); - ForthBlock loop = new ForthBlock(curLine, sourceName); - b.add(loop.LOOP, loop); + ByteCodeBlock loop = new ByteCodeBlock(curLine, sourceName); + b.add(LOOP, loop); parseStatement(false, loop); consume(WHILE); consume(LP); startExpr(loop); - loop.add(loop.JT, new Integer(2)); - loop.add(Lexer.BREAK); - loop.add(Lexer.CONTINUE); + loop.add(JT, new Integer(2)); + loop.add(BREAK); + loop.add(CONTINUE); consume(RP); consume(SEMI); break; } case TRY: { - // FIXME: don't just ignore this! // We deliberately allow you to omit braces in catch{}/finally{} if they are single statements... consume(TRY); parseStatement(true, b); @@ -542,53 +541,52 @@ public class Parser extends Lexer implements OpCodes { consume(NAME); consume(IN); startExpr(b); - b.add(b.PUSHKEYS); - b.add(b.LITERAL, "length"); - b.add(b.GET); + b.add(PUSHKEYS); + b.add(LITERAL, "length"); + b.add(GET); consume(RP); - ForthBlock b2 = new ForthBlock(curLine, sourceName); - b.add(b.SCOPE, b2); - b2.add(b.LITERAL, new Integer(1)); + ByteCodeBlock b2 = new ByteCodeBlock(curLine, sourceName); + b.add(SCOPE, b2); + b2.add(LITERAL, new Integer(1)); b2.add(SUB); - b2.add(b.DUP); - b2.add(b.LITERAL, new Integer(0)); + b2.add(DUP); + b2.add(LITERAL, new Integer(0)); b2.add(LT); - b2.add(b.JT, new Integer(7)); - b2.add(b.GET_PRESERVE); - b2.add(b.LITERAL, varName); - b2.add(b.LITERAL, varName); - b2.add(b.DECLARE); - b2.add(b.PUT); - b2.add(b.EXPR, parseStatement()); + b2.add(JT, new Integer(7)); + b2.add(GET_PRESERVE); + b2.add(LITERAL, varName); + b2.add(LITERAL, varName); + b2.add(DECLARE); + b2.add(PUT); + parseStatement(false, b2); break; } else { - ForthBlock b2 = new ForthBlock(curLine, sourceName); - b.add(b.SCOPE, b2); - b.add(b.POP); + ByteCodeBlock b2 = new ByteCodeBlock(curLine, sourceName); + b.add(SCOPE, b2); + b.add(POP); int size = b2.size(); startExpr(b2); - if (b2.size() - size > 0) b2.add(b.POP); + if (b2.size() - size > 0) b2.add(POP); consume(SEMI); - ForthBlock e2 = startExpr(); + ByteCodeBlock e2 = startExpr(); consume(SEMI); + if (e2 == null) e2 = new ByteCodeBlock(curLine, sourceName, b.LITERAL, null); - if (e2 == null) e2 = new ForthBlock(curLine, sourceName, b.LITERAL, null); - - ForthBlock b3 = new ForthBlock(curLine, sourceName); - b2.add(b.LOOP, b3); - b2.add(b.LITERAL, null); + ByteCodeBlock b3 = new ByteCodeBlock(curLine, sourceName); + b2.add(LOOP, b3); + b2.add(LITERAL, null); - b3.add(b.JT, new Integer(0)); + b3.add(JT, new Integer(0)); size = b3.size(); startExpr(b3); consume(RP); - if (b3.size() - size > 0) b3.add(b.POP); + if (b3.size() - size > 0) b3.add(POP); b3.arg[size - 1] = new Integer(b3.size() - size + 1); - b3.add(b.EXPR, e2); - b3.add(b.JT, new Integer(2)); + b3.paste(e2); + b3.add(JT, new Integer(2)); b3.add(BREAK); parseStatement(false, b3); b3.add(BREAK); @@ -601,7 +599,7 @@ public class Parser extends Lexer implements OpCodes { String name = string; if (peekToken() == COLON) { consume(COLON); - b.add(ForthBlock.LABEL, string); + b.add(ByteCodeBlock.LABEL, string); break; } else { pushBackToken(NAME, name); @@ -616,7 +614,7 @@ public class Parser extends Lexer implements OpCodes { int size = b.size(); startExpr(b); if (size == b.size()) return; - b.add(b.POP); + b.add(POP); if (peekToken() == SEMI) consume(SEMI); break; } diff --git a/src/org/xwt/js/Tokens.java b/src/org/xwt/js/Tokens.java index 3f9065c..f2187f4 100644 --- a/src/org/xwt/js/Tokens.java +++ b/src/org/xwt/js/Tokens.java @@ -1,7 +1,7 @@ // Copyright 2002 Adam Megacz, see the COPYING file for licensing [GPL] package org.xwt.js; -public interface Tokens { +interface Tokens { // Token Constants ////////////////////////////////////////////////////////// public final static int @@ -35,9 +35,9 @@ public interface Tokens { DIV = 28, // / ASSIGN_DIV = 29, // /= MOD = 30, // % - ASSIGN_MOD = 31, // %= + ASSIGN_MOD = 31, // %= BITNOT = 32, // ~ - ASSIGN_BITNOT= 33, // ~= + ASSIGN_BITNOT= 33, // ~= DELPROP = 34, // delete TYPEOF = 35, // typeof NAME = 36, // *** identifiers *** @@ -64,32 +64,29 @@ public interface Tokens { ASSIGN = 57, // = HOOK = 58, // ? COLON = 59, // : - OR = 60, // || - AND = 61, // && - INC = 62, // ++ - DEC = 63, // -- - DOT = 64, // . - FUNCTION = 65, // function + OR = 60, // || + AND = 61, // && + INC = 62, // ++ + DEC = 63, // -- + DOT = 64, // . + FUNCTION = 65, // function IF = 66, // if keyword - ELSE = 67, // else keyword - SWITCH = 68, // switch keyword - CASE = 69, // case keyword - DEFAULT = 70, // default keyword - WHILE = 71, // while keyword - DO = 72, // do keyword - FOR = 73, // for keyword - BREAK = 74, // break keyword - CONTINUE = 75, // continue keyword - VAR = 76, // var keyword - WITH = 77, // with keyword - CATCH = 78, // catch keyword - FINALLY = 79, // finally keyword - RESERVED = 80, // reserved keywords - NOP = 81, // NOP - VOID = 82, // void keyword - MOD_ASSIGN = 83, // %= - BANG = 84, // %= - ASSERT = 85; // assert keyword + ELSE = 67, // else keyword + SWITCH = 68, // switch keyword + CASE = 69, // case keyword + DEFAULT = 70, // default keyword + WHILE = 71, // while keyword + DO = 72, // do keyword + FOR = 73, // for keyword + BREAK = 74, // break keyword + CONTINUE = 75, // continue keyword + VAR = 76, // var keyword + WITH = 77, // with keyword + CATCH = 78, // catch keyword + FINALLY = 79, // finally keyword + RESERVED = 80, // reserved keywords + BANG = 84, // ! + ASSERT = 85; // assert keyword public static final int MAX_TOKEN = ASSERT; @@ -106,6 +103,6 @@ public interface Tokens { "HOOK", "COLON", "OR", "AND", "INC", "DEC", "DOT", "FUNCTION", "IF", "ELSE", "SWITCH", "CASE", "DEFAULT", "WHILE", "DO", "FOR", "BREAK", "CONTINUE", "VAR", "WITH", "CATCH", "FINALLY", - "RESERVED", "NOP", "VOID", "MOD_ASSIGN", "BANG", "ASSERT" }; + "RESERVED", "BANG", "ASSERT" }; } diff --git a/src/org/xwt/util/Vec.java b/src/org/xwt/util/Vec.java index 433c06d..f40d345 100644 --- a/src/org/xwt/util/Vec.java +++ b/src/org/xwt/util/Vec.java @@ -46,6 +46,10 @@ public final class Vec implements Serializable { store[size++] = o; } + public Object peek() { + return lastElement(); + } + public Object elementAt(int i) { return store[i]; }