Commits

Mark Shannon  committed e4ad68a

Add the files

  • Participants
  • Parent commits fa0ef7b

Comments (0)

Files changed (112)

File HotPy/parser/AndExpr.java

+package parser;
+
+import antlr.RecognitionException;
+import antlr.Token;
+
+class AndExpr extends Expr {
+
+    Expr left;
+    Expr right;
+
+    AndExpr(Expr left, Expr right) {
+        this.left = left;
+        this.right = right;
+    }
+
+    void write(AssemblerWriter out) throws RecognitionException {
+        left.write(out);
+        Label patch = new Label();
+        out.branch("JUMP_IF_FALSE_OR_POP", patch);
+        right.write(out);
+        patch.write(out);
+    }
+
+    void delete(AssemblerWriter out) throws RecognitionException {
+        throw new RecognitionException("Cannot delete rvalue");
+    }
+
+    void writeLHS(AssemblerWriter out) throws RecognitionException {
+        throw new RecognitionException("Cannot assign to binary expression");
+    }
+
+    void branch(boolean onTrue, AssemblerWriter out, Label l) throws RecognitionException {
+        if (onTrue) {
+            super.branch(true, out, l);
+        } else {
+            left.branch(false, out, l);
+            right.branch(false, out, l);
+        }
+    }
+
+
+
+}

File HotPy/parser/AppendStmt.java

+package parser;
+
+import antlr.RecognitionException;
+
+class AppendStmt extends Stmt {
+
+    Expr list;
+    Expr val;
+
+    AppendStmt(Expr list, Expr val) {
+        this.list = list;
+        this.val = val;
+    }
+
+    void write(AssemblerWriter out) throws RecognitionException {
+        //list.write(out);
+        //val.write(out);
+        //out.writeln("LIST_APPEND 2");
+        throw new RecognitionException("Not implemented");
+    }
+
+}
+

File HotPy/parser/AsgnExpr.java

+package parser;
+import antlr.RecognitionException;
+import antlr.Token;
+
+class AsgnExpr extends Expr {
+
+    Expr left;
+    Expr right;
+
+    AsgnExpr(Expr left, Expr right) throws RecognitionException {
+        left.lhs();
+        this.left = left;
+        this.right = right;
+    }
+
+    void write(AssemblerWriter out) throws RecognitionException {
+        right.write(out);
+        out.writeln("DUP_TOP");
+        left.writeLHS(out);
+    }
+
+    void delete(AssemblerWriter out) throws RecognitionException {
+        throw new RecognitionException("Cannot delete rvalue");
+    }
+
+    void writeLHS(AssemblerWriter out) throws RecognitionException {
+        throw new RecognitionException("Cannot assign to an assignment");
+    }
+
+}

File HotPy/parser/AsgnStmt.java

+package parser;
+import antlr.RecognitionException;
+import antlr.Token;
+
+class AsgnStmt extends Stmt {
+
+    Expr left;
+    Expr right;
+
+    AsgnStmt(Expr left, Expr right) throws RecognitionException {
+        this.left = left;
+        this.right = right;
+    }
+
+    void write(AssemblerWriter out) throws RecognitionException {
+        out.line(line);
+        right.write(out);
+        left.writeLHS(out);
+    }
+
+}

File HotPy/parser/AssemblerFile.java

+package parser;
+
+import antlr.RecognitionException;
+import java.util.*;
+import java.io.*;
+
+public class AssemblerFile {
+
+    AssemblerFile(String filename, List<Function> functions, List<ClassDefn> classes, Stmt script) {
+        this.functions = functions;
+        this.classes =   classes;
+        this.script =    script.returns();
+        this.file = filename;
+    }
+
+    List<Function> functions;
+    List<ClassDefn> classes;
+    Stmt script;
+    String file;
+
+    void write(AssemblerWriter out) throws RecognitionException {
+        out.write(".filename \"" + file + "\"\n");
+        for (Function f: functions)
+            f.write(out);
+        for (ClassDefn c: classes)
+            c.write(out);
+        out.write(".init\n");
+        out.clearLineNumbers();
+        script.write(out);
+        out.write(".end\n");
+    }
+
+}

File HotPy/parser/AssemblerWriter.java

+package parser;
+import java.io.*;
+import java.util.*;
+
+class AssemblerWriter {
+
+    private PrintWriter out;
+
+    public AssemblerWriter(OutputStream out) {
+        this.out = new PrintWriter(out);
+    }
+
+    int pending_line;
+
+    void branch(String op, Label l) {
+        write_line();
+       // Assert the opcode is valid branch?
+        out.write(op);
+        out.write(" ");
+        out.write(l.toString());
+        out.write("\n");
+    }
+
+    void branch_back(String op, Label l) {
+        write_line();
+        out.write(op);
+        out.write(" ");
+        out.write(l.toString());
+        out.write("\n");
+    }
+
+    void clearLineNumbers() {
+        pending_line = 0;
+    }
+
+    void writeLabel(String s) {
+        out.write(s + ":\n");
+    }
+
+    public void writeln(String s) {
+        write_line();
+        out.write(s);
+        out.write("\n");
+    }
+
+    void write(String s) {
+        out.write(s);
+    }
+
+    void write_line() {
+        if (pending_line != 0) {
+            out.write(".line " + pending_line + "\n");
+            pending_line = 0;
+        }
+    }
+
+    void line (int line) {
+        pending_line = line;
+    }
+
+    public void close() {
+        out.close();
+    }
+
+}
+

File HotPy/parser/AssertStmt.java

+package parser;
+
+import antlr.RecognitionException;
+import java.util.*;
+
+class AssertStmt extends Stmt {
+
+    Stmt body;
+
+    AssertStmt(Expr cond, Expr message) {
+        if (message == null)
+            message = Expr.NONE;
+        Stmt raise = new RaiseStmt(new AssertExpr(message));
+        body = new IfStmt(new NotExpr(cond), raise);
+    }
+
+
+
+    void write(AssemblerWriter out) throws RecognitionException {
+        // Just ignore assert statements?
+        // out.line(line);
+        // body.write(out);
+    }
+
+}
+
+class AssertExpr extends Expr {
+
+    AssertExpr(Expr message)  {
+        this.message = message;
+    }
+
+    Expr message;
+
+    void delete(AssemblerWriter out) throws RecognitionException {
+        throw new RecognitionException("Cannot delete rvalue");
+    }
+
+    void write(AssemblerWriter out) throws RecognitionException {
+        out.writeln("LOAD_GLOBAL AssertionError");
+        message.write(out);
+        out.write("CALL_FUNCTION 1");
+    }
+
+    void writeLHS(AssemblerWriter out) throws RecognitionException {
+        throw new IllegalStateException();
+    }
+
+}

File HotPy/parser/AugAsgnStmt.java

+package parser;
+import antlr.RecognitionException;
+import antlr.Token;
+class AugAsgnStmt extends Stmt {
+
+    Expr left;
+    String op;
+    Expr right;
+
+    AugAsgnStmt(Expr left, String op, Expr right) throws RecognitionException {
+        this.left = left;
+        this.op = op;
+        this.right = right;
+        if (left instanceof Tuple) {
+            throw new RecognitionException("May not use augmented assignment on tuples");
+        }
+    }
+
+    void write(AssemblerWriter out) throws RecognitionException {
+        out.line(line);
+        left.writeAugAsgn(op, right, out);
+    }
+
+}

File HotPy/parser/Binary.java

+package parser;
+import antlr.RecognitionException;
+import antlr.Token;
+
+class Binary extends Expr {
+
+    Expr left;
+    Expr right;
+    String op;
+
+    Binary(Expr left, String op, Expr right) {
+        this.left = left;
+        this.op = op;
+        this.right = right;
+    }
+
+    void write(AssemblerWriter out) throws RecognitionException {
+        left.write(out);
+        right.write(out);
+        out.writeln(op);
+    }
+
+    void writeLHS(AssemblerWriter out) throws RecognitionException {
+        throw new RecognitionException("Cannot assign to binary operator");
+    }
+
+    void delete(AssemblerWriter out) throws RecognitionException {
+        throw new RecognitionException("Cannot delete rvalue");
+    }
+
+}

File HotPy/parser/BreakStmt.java

+package parser;
+
+import antlr.RecognitionException;
+
+class BreakStmt extends Stmt {
+
+    BreakStmt() {
+    }
+
+    void write(AssemblerWriter out) throws RecognitionException {
+        out.line(line);
+        out.writeln("BREAK_LOOP");
+    }
+
+}

File HotPy/parser/Call.java

+package parser;
+
+import antlr.RecognitionException;
+import antlr.Token;
+import java.util.*;
+
+class Call extends Expr {
+
+    Expr left;
+    Parameters args;
+
+    Call(Expr left,  Parameters args) {
+        this.left = left;
+        this.args = args;
+    }
+
+    void delete(AssemblerWriter out) throws RecognitionException {
+        throw new RecognitionException("Cannot delete rvalue");
+    }
+
+    static void write_call(Parameters p, AssemblerWriter out) throws RecognitionException {
+        for (Expr e : p.positional) {
+            e.write(out);
+        }
+        for (int i = 0; i < p.keywords.size(); i+=2) {
+            Expr k = p.keywords.get(i);
+            Expr v = p.keywords.get(i+1);
+            k.write(out);
+            v.write(out);
+        }
+        int n = p.positional.size() + p.keywords.size() * 128;
+        if (p.tuple == null) {
+            if (p.dict == null) {
+                out.writeln("CALL_FUNCTION " + n);
+            } else {
+                p.dict.write(out);
+                out.writeln("CALL_FUNCTION_KW " + n);
+            }
+        } else {
+            p.tuple.write(out);
+            if (p.dict == null) {
+                out.writeln("CALL_FUNCTION_VAR " + n);
+            } else {
+                p.dict.write(out);
+                out.writeln("CALL_FUNCTION_VAR_KW " + n);
+            }
+        }
+    }
+
+    void write(AssemblerWriter out) throws RecognitionException {
+        left.write(out);
+        write_call(args, out);
+    }
+
+    void writeLHS(AssemblerWriter out) throws RecognitionException {
+        throw new RecognitionException("Cannot assign to function call");
+    }
+
+    static final int KEYWORDS_INDEX = 1;
+    static final int TUPLE_INDEX = 2;
+    static final int DICT_INDEX = 4;
+
+}

File HotPy/parser/ChainedComparison.java

+package parser;
+import antlr.RecognitionException;
+import antlr.Token;
+
+class ChainedComparison extends Comparison {
+
+    Expr temp;
+
+    ChainedComparison(Expr left, String op, Expr right, Expr temp) throws RecognitionException {
+        super(left, "COMPARE_OP " + op, right);
+        this.temp = temp;
+        temp.lhs();
+    }
+
+    void write(AssemblerWriter out) throws RecognitionException {
+        //left.write(out);
+        //right.write(out);
+        //out.write(Opcodes.COPY);
+        //temp.writeLHS(out);
+        //out.write(Opcodes.BINARY);
+        //out.write(opID);
+        throw new RecognitionException("Not implemented");
+    }
+
+}

File HotPy/parser/ClassDefn.java

+package parser;
+import java.util.*;
+import java.io.*;
+import antlr.RecognitionException;
+import antlr.Token;
+
+public class ClassDefn {
+
+    ClassDefn(String name, List supers) {
+        this.name = name;
+        this.supers = supers;
+    }
+
+    String name;
+    List supers;
+
+    void write(AssemblerWriter out) {
+        out.write("Class name: " + name);
+        out.write(supers.size() + " super classes");
+    }
+
+}

File HotPy/parser/ClassHeader.java

+package parser;
+import java.io.*;
+import java.util.List;
+
+public class ClassHeader {
+
+    String name;
+    int supers;
+
+    ClassHeader(String name, int supers) {
+        this.name = name;
+        this.supers = supers;
+    }
+
+    void writeTo(OutputStream out) throws IOException {
+        out.write(name >> 8);
+        out.write(name & 0xff);
+        out.write(supers >> 8);
+        out.write(supers & 0xff);
+    }
+
+}

File HotPy/parser/ClassScope.java

+package parser;
+import java.util.*;
+import java.io.*;
+import antlr.RecognitionException;
+
+public class ClassScope extends Scope2 {
+
+    ClassScope(String name, Scope2 enclosing) {
+        super();
+        this.name = name;
+        this.enclosing = enclosing;
+    }
+
+    String name;
+    Scope2 enclosing;
+
+    Expr name(String name) {
+        return new Global(name);
+    }
+
+    void global(String name) {
+        // Ignore.
+    }
+
+    void bind(String name) {
+        // Ignore
+    }
+
+
+    Scope2 beginFunction(List parameters) {
+        return new FunctionScope2(parameters, this);
+    }
+
+    Scope2 beginClass(String name) {
+        throw new Error("Nested classes not supported");
+    }
+
+    Scope2 close() {
+        return enclosing;
+    }
+
+}

File HotPy/parser/Comparison.java

+package parser;
+import antlr.RecognitionException;
+import antlr.Token;
+
+class Comparison extends Binary {
+
+    Comparison(Expr left, String op, Expr right) {
+        super(left, "COMPARE_OP " + op, right);
+    }
+
+}

File HotPy/parser/Constant.java

+package parser;
+
+import antlr.RecognitionException;
+import antlr.Token;
+import java.util.*;
+import java.math.*;
+
+class Constant extends Expr {
+
+    static String constant(Object c) {
+        return c.toString();
+    }
+
+    static Expr getString(String s) {
+        return new StringConstant(s);
+    }
+
+    static Expr getInt(Token t) {
+        BigInteger b;
+        String text = t.getText();
+        char c = text.charAt(0);
+        if (c == '0' && text.length() > 1) {
+            c = text.charAt(1);
+            if (c == 'x' || c == 'X') {
+                b = new BigInteger(text.substring(2), 16);
+            } else {
+                b = new BigInteger(text, 8);
+            }
+        } else {
+            b = new BigInteger(text);
+        }
+        return new Constant(constant(b));
+    }
+
+    static Expr getLong(Token t) {
+        String text = t.getText();
+        char l = text.charAt(text.length() -1);
+        BigInteger b;
+        if (l == 'l' || l == 'L') {
+            b = new BigInteger(text.substring(0, text.length() -1));
+        } else {
+            b = new BigInteger(text);
+        }
+        return new Constant(constant(b));
+    }
+
+    static Expr getFloat(Token t) {
+        Double d = Double.valueOf(t.getText());
+        return new Constant(constant(d));
+    }
+
+    static Expr getComplex(Token t) {
+        String text = t.getText();
+        char j = text.charAt(text.length() -1);
+        Double d;
+        if (j == 'j' || j == 'J') {
+            d = Double.valueOf(text.substring(0, text.length() -1));
+        } else {
+            d = Double.valueOf(text);
+        }
+        return new Constant(constant(d));
+    }
+
+    String value;
+
+    Constant(String value) {
+        this.value = value;
+    }
+
+    void write(AssemblerWriter out) {
+        out.writeln("LOAD_CONST " + value);
+    }
+
+    void delete(AssemblerWriter out) throws RecognitionException {
+        throw new RecognitionException("Cannot delete constant");
+    }
+
+    void writeLHS(AssemblerWriter out) throws RecognitionException {
+        throw new RecognitionException("Cannot assign to a constant");
+    }
+
+    public Stmt toStm() {
+        return new Stmt() {
+            void write(AssemblerWriter out) throws RecognitionException {
+                out.line(line);
+            }
+        };
+    }
+}
+
+class StringConstant extends Constant {
+
+    String doc;
+
+    StringConstant(String s) {
+        super(s);
+    }
+
+    void write(AssemblerWriter out) {
+        out.writeln("LOAD_CONST \"" + value + "\"");
+    }
+
+    public Stmt toStm() {
+        return new DocString(doc);
+    }
+
+}

File HotPy/parser/ContinueStmt.java

+package parser;
+
+import antlr.RecognitionException;
+
+class ContinueStmt extends Stmt {
+
+    LoopStmt loop;
+
+    ContinueStmt(LoopStmt loop) {
+        this.loop = loop;
+    }
+
+    void write(AssemblerWriter out) throws RecognitionException {
+        out.line(line);
+        Label l = new Label();
+        loop._continue(l);
+        out.branch("JUMP_ABSOLUTE", l);
+    }
+
+}
+

File HotPy/parser/Decorator.java

+package parser;
+
+import antlr.RecognitionException;
+import java.util.*;
+
+class Decorator {
+
+    Expr var;
+    Parameters params;
+
+    Decorator(Expr var) {
+	    this.var = var;
+    }
+
+    Expr apply(Expr func) {
+        List<Expr> list = new ArrayList<Expr>();
+        list.add(func);
+        Parameters p = new Parameters(list, new ArrayList<Expr>(), null, null);
+        return new Call(var, p);
+    }
+}

File HotPy/parser/DeleteStmt.java

+package parser;
+
+import antlr.RecognitionException;
+import java.util.*;
+
+class DeleteStmt extends Stmt {
+
+    ExprList values;
+
+    DeleteStmt(ExprList values) {
+        this.values = values;
+        // Overly conservative as this is probably del attr or item.
+        deletes = true;
+    }
+
+
+    void write(AssemblerWriter out) throws RecognitionException {
+        out.line(line);
+        for (Expr e : values)
+            e.delete(out);
+    }
+
+}

File HotPy/parser/DictParameter.java

+package parser;
+
+public class DictParameter extends Parameter2 {
+
+    String nameID;
+    Expr type;
+    Expr value;
+
+    DictParameter(String nameID) {
+        super(nameID, null, null);
+    }
+
+}

File HotPy/parser/Dictionary.java

+package parser;
+
+import antlr.RecognitionException;
+import antlr.Token;
+import java.util.*;
+
+class Dictionary extends Expr {
+
+    List<Expr> args;
+
+    Dictionary(List<Expr> args) {
+        this.args = args;
+    }
+
+    void write(AssemblerWriter out) throws RecognitionException {
+        int size = args.size();
+        out.writeln("BUILD_MAP " + size);
+        for(int i = 0; i < size; i += 2) {
+            args.get(i+1).write(out);  // d v
+            args.get(i).write(out);  // d v k
+            out.writeln("STORE_MAP"); // d ( d[k] = v )
+        }
+    }
+
+    void writeLHS(AssemblerWriter out) throws RecognitionException {
+        throw new Error("Not implemented");
+    }
+
+    void delete(AssemblerWriter out) throws RecognitionException {
+        throw new RecognitionException("Cannot delete rvalue");
+    }
+
+}

File HotPy/parser/DisassemblerWriter.java

+package parser;
+import java.io.*;
+
+public class Disassembler extends Saver {
+
+    Disassembler(String name) {
+        super(name);
+        out = System.out;
+    }
+
+    Disassembler(String name, PrintStream output) {
+        super(name);
+        this.out = output;
+    }
+
+    PrintStream out;
+
+    String[] names;
+
+    public void close() throws IOException {
+        super.close();
+        disassemble();
+    }
+
+    int pointer;
+
+    void writeNameTable() {
+        int count = buf[pointer++];
+        names = new String[count];
+        out.println("" + count + " names");
+        for (int i = 0; i < count; i++) {
+            names[i] = readString();
+            out.println("" + i + ": " + names[i]);
+        }
+    }
+
+    String readString() {
+        int count = buf[pointer++];
+        char[] c = new char[count];
+        for (int i = 0; i < count/2; i++) {
+            c[i*2] = (char)(buf[pointer] >>> 8);
+            c[i*2 + 1] = (char)(buf[pointer++] & 0xff);
+        }
+        if ((count & 1) != 0) {
+            c[count -1] = (char)(buf[pointer++] >>> 8);
+        }
+        return new String(c);
+    }
+
+    void writeConstantTable() {
+        int count = buf[pointer++];
+        out.println("" + count + " constants");
+        for (int i = 0; i < count; i++) {
+            int size =  buf[pointer++];
+            out.print("size: " + size);
+            for (int j = 0; j < (size + 1)/2; j++) {
+                out.print(" " + (int)buf[pointer++]);
+            }
+            out.println();
+        }
+    }
+
+    void writeName(String prefix) {
+        out.flush();
+        int index = (buf[pointer++] << 16) >> 16;
+        if (index < 256) {
+            out.println(prefix + index + " \"" + StandardNames.names[index] + "\"" );
+        } else if (index < names.length + 256) {
+            out.println(prefix + index + " \"" + names[index - 256] + "\"" );
+        } else {
+            out.println(prefix + index + " Illegal index");
+        }
+
+    }
+
+    void writeFunction() {
+        writeName("Function name: ");
+        int params = buf[pointer++];
+        out.println("" + params + " parameters");
+        for (int i = 0; i < params; i++) {
+            writeName("Type: ");
+        }
+        int locals = buf[pointer++];
+        out.println("" + locals + " locals");
+        out.println("" + (int)buf[pointer++] + " stack");
+        writeCode();
+        for (int i = 0; i < locals; i++) {
+            writeName("Local " + i + ": ");
+        }
+    }
+
+    void writeCode() {
+        int count = buf[pointer++];
+        for (int i = 0; i < count; i++) {
+            int code = buf[pointer++];
+            out.println(Opcodes.names[code >> 11] + " " + (((code & 0x7ff) << 21) >> 21));
+        }
+    }
+
+    void writeClass() {
+        throw new Error("Not implemented");
+    }
+}
+
+

File HotPy/parser/DocString.java

+package parser;
+
+import antlr.Token;
+import antlr.RecognitionException;
+
+class DocString extends Stmt {
+
+    String text;
+
+    DocString(String doc) {
+        this.text = text;
+    }
+
+    void write(AssemblerWriter out) {
+        out.line(line);
+    }
+
+    String docString() {
+        return text;
+    }
+
+}

File HotPy/parser/Driver.java

+package parser;
+import java.io.*;
+
+public class Driver {
+/*
+    public static void main(String[] args) throws Exception {
+        if (args.length != 1) {
+            System.out.println("Usage: Driver filename");
+            return;
+        }
+        StartUp.load();
+        InputStream in = Parser.get(args[0]);
+        new Module(Names.getIndex("__main__"), in).dump(System.out);
+    }
+*/    
+}

File HotPy/parser/Except.java

+package parser;
+
+import antlr.RecognitionException;
+import antlr.Token;
+import java.util.*;
+
+class Except {
+
+    List<Expr> types;
+    Expr var;
+
+    Except(List<Expr> types, Expr var) {
+        this.types = types;
+        this.var = var;
+    }
+
+    void write(AssemblerWriter out) throws RecognitionException {
+        if (types == null) {
+            out.writeln("POP_TOP");
+            out.writeln("POP_TOP");
+            out.writeln("POP_TOP");
+        } else {
+            Label end = new Label();
+            for (Expr t : types) {
+                out.writeln("DUP_TOP");
+                t.write(out);
+                out.writeln("COMPARE_OP exception match");
+                out.writeln("POP_JUMP_IF_FALSE " + end);
+            }
+            if (var == null)
+                out.writeln("POP_TOP");
+            else
+                var.writeLHS(out);
+            out.writeln("POP_TOP");
+            end.write(out);
+
+        }
+    }
+}

File HotPy/parser/Expr.java

+package parser;
+
+import antlr.RecognitionException;
+import java.util.List;
+
+abstract class Expr {
+
+    abstract void write(AssemblerWriter out) throws RecognitionException;
+
+    abstract void delete(AssemblerWriter out) throws RecognitionException;
+
+    abstract void writeLHS(AssemblerWriter out) throws RecognitionException;
+
+    public static Expr NONE = new None();
+    public static Expr TRUE = new Bool(true);
+    public static Expr FALSE = new Bool(false);
+
+    Expr negate() {
+        return new Unary("neg", this);
+    }
+
+    Stmt assign(Expr rhs) throws RecognitionException {
+        lhs();
+        return new AsgnStmt(this, rhs);
+    }
+
+    void lhs() throws RecognitionException {
+    }
+
+    void writeAugAsgn(String op, Expr right, AssemblerWriter out) throws RecognitionException {
+        write(out);  // r x
+        right.write(out); // r
+        out.writeln(op);   // r+x
+        writeLHS(out);   //
+    }
+
+    void branch(boolean onTrue, AssemblerWriter out, Label l) throws RecognitionException {
+        write(out);
+        out.branch(onTrue ? "POP_JUMP_IF_TRUE" : "POP_JUMP_IF_FALSE", l);
+    }
+
+    public Stmt toStm() {
+        return new Stmt() {
+            void write(AssemblerWriter out) throws RecognitionException {
+                out.line(line);
+                Expr.this.write(out);
+                out.writeln("POP_TOP");
+            }
+        };
+    }
+
+    private static class None extends Expr {
+
+        public String toString() {
+            return "None";
+        }
+
+        void write(AssemblerWriter out) throws RecognitionException {
+            out.writeln("LOAD_CONST None");
+        }
+
+        void writeLHS(AssemblerWriter out) throws RecognitionException {
+            // Shouldn't get here protected by lhs()?
+            throw new RecognitionException("Cannot assign to None");
+        }
+
+        void delete(AssemblerWriter out) throws RecognitionException {
+            // Shouldn't get here protected by lhs()?
+            throw new RecognitionException("Cannot delete None");
+        }
+
+        void lhs() throws RecognitionException {
+            throw new RecognitionException("Cannot assign to None");
+        }
+
+    }
+
+    private static class Bool extends Expr {
+
+        private boolean value;
+        private String name;
+
+        Bool(boolean b) {
+            this.value = b;
+            this.name = b ? "True" : "False" ;
+        }
+
+        public String toString() {
+            return this.name;
+        }
+
+        void write(AssemblerWriter out) throws RecognitionException {
+            out.writeln("LOAD_CONST " + (value ? "True" : "False"));
+        }
+
+        void writeLHS(AssemblerWriter out) throws RecognitionException {
+            // Shouldn't get here protected by lhs()?
+            throw new RecognitionException("Cannot assign to " + name);
+        }
+
+        void delete(AssemblerWriter out) throws RecognitionException {
+            // Shouldn't get here protected by lhs()?
+            throw new RecognitionException("Cannot delete " + name);
+        }
+
+        void lhs() throws RecognitionException {
+            throw new RecognitionException("Cannot assign to " + name);
+        }
+
+    }
+
+}

File HotPy/parser/ExprList.java

+package parser;
+
+import antlr.RecognitionException;
+import java.util.ArrayList;
+
+class ExprList extends ArrayList<Expr> {
+    
+    boolean trailingComma = false;
+    
+}
+    
+  

File HotPy/parser/FileScope.java

+package parser;
+import java.util.*;
+import java.io.*;
+import antlr.RecognitionException;
+
+public class FileScope extends Scope2 {
+
+    FileScope() {
+        super();
+    }
+
+    Expr name(String name) {
+        return new Global(name);
+    }
+
+    void global(String name) {
+        // Ignore.
+    }
+
+    void bind(String name) {
+        // Ignore
+    }
+
+    Scope2 beginFunction(List parameters) {
+        return new FunctionScope2(parameters, this);
+    }
+
+    Scope2 beginClass(String name) {
+        return new ClassScope(name, this);
+    }
+
+    Scope2 close() {
+        return null;
+    }
+
+    int tempIndex;
+
+}

File HotPy/parser/ForStmt.java

+package parser;
+
+import antlr.RecognitionException;
+import java.util.*;
+
+class ForStmt extends LoopStmt {
+
+    Expr var;
+    Expr collect;
+    Expr temp;
+
+    ForStmt(Expr var, Expr collect, Expr temp) throws RecognitionException {
+        this.var = var;
+        var.lhs();
+        this.collect = collect;
+        this.temp = temp;
+        temp.lhs();
+    }
+
+    void write(AssemblerWriter out) throws RecognitionException {
+        out.line(line);
+        collect.write(out);
+        out.writeln("GET_ITER");
+        Label target = new Label();
+        target.write(out);
+        for (Label c : continues) {
+           c.write(out);
+        }
+        Label patch = new Label();
+        out.branch("FOR_ITER", patch);
+        var.writeLHS(out);
+        suite.write(out);
+        out.branch_back("JUMP_ABSOLUTE", target);
+        patch.write(out);
+        for (Label b : breaks) {
+           b.write(out);
+        }
+    }
+
+}
+

File HotPy/parser/FromStar.java

+package parser;
+
+import antlr.RecognitionException;
+import java.util.*;
+
+public class FromStar extends Stmt {
+
+    Expr module_name;
+
+    public FromStar(Expr module_name) {
+        this.module_name = module_name;
+    }
+
+    void write(AssemblerWriter out) throws RecognitionException {
+        throw new Error("Not implemented");
+    }
+
+}

File HotPy/parser/FromStmt.java

+package parser;
+
+import antlr.RecognitionException;
+import java.util.*;
+
+class FromStmt extends Stmt {
+
+    Expr module_name;
+    List<NamePair> pairs = new ArrayList();
+    String name;
+    String as_name;
+
+    public FromStmt(Expr module_name, String name, String as_name) {
+        this.module_name = module_name;
+        this.name = name;
+        this.as_name = as_name;
+    }
+
+    void addNames(String name, String as_name) {
+        NamePair pair = new NamePair();
+        pair.name = name;
+        pair.as_name = as_name;
+        pairs.add(pair);
+    }
+
+    void write(AssemblerWriter out) throws RecognitionException {
+//        module_name.write(out);
+//        out.write(Opcodes.IMPORT);
+//        for (NamePair p : pairs) {
+//            out.write(Opcodes.COPY);
+//            out.write(Opcodes.LOAD_ATTR);
+//            out.writeShort(p.name);
+//            out.write(Opcodes.STORE_GLOBAL);
+//            out.writeShort(p.as_name);
+//        }
+//        out.write(Opcodes.LOAD_ATTR);
+//        out.writeShort(name);
+//        out.write(Opcodes.STORE_GLOBAL);
+//        out.writeShort(as_name);
+        throw new RecognitionException("Not implemented");
+    }
+
+}
+
+
+class NamePair {
+    String name;
+    String as_name;
+}

File HotPy/parser/Function.java

+package parser;
+import java.util.*;
+import java.io.*;
+import antlr.RecognitionException;
+import antlr.Token;
+
+public class Function {
+
+    static final int STANDARD = 0;
+    static final int TUPLE = 4;
+    static final int DICT = 8;
+    static final int GENERATOR = 512;
+    static final int ESCAPES = 1024;
+    static final int NO_DELETES = 8192;
+
+    Function(String name, List<Parameter2> parameters, Stmt suite, boolean is_closure) {
+        this.kind = STANDARD;
+        int last = parameters.size()-1;
+        if (last >= 0 && parameters.get(last) instanceof DictParameter) {
+            this.kind |= DICT;
+            last--;
+        }
+        if (last >= 0 && parameters.get(last) instanceof TupleParameter) {
+            this.kind |= TUPLE;
+        }
+        if (suite.yields)
+            this.kind |= GENERATOR;
+        if (!suite.deletes)
+            this.kind |= NO_DELETES;
+        if (is_closure)
+            this.kind |= ESCAPES;
+        this.name = name;
+        this.parameters = parameters;
+        this.suite = suite;
+    }
+
+    String name;
+    List<Parameter2> parameters;
+    Stmt suite;
+    int kind;
+
+    //void dump(PrintStream out, Disassembler d) throws RecognitionException {
+    //    out.println("Function " + d.name(name));
+    //    out.println(parameters.size() + " parameters");
+    //    out.println("Locals: " + localNames.length);
+    //    out.println("Kind: " + kind);
+    //    if (suite != null) {
+    //        out.println("Byte Codes:");
+    //        d.dumpCode(suite, out);
+    //    } else {
+    //        out.println("Native");
+    //    }
+    //    out.println("Local names:");
+    //    int index = 0;
+    //    for (int n : localNames) {
+    //        out.println("" + (index++) + ": " + d.name(n));
+    //    }
+    //}
+
+    void write(AssemblerWriter out) throws RecognitionException {
+        out.write(".function " + name + "\n");
+        for (Parameter2 p : parameters) {
+            out.write(".param " + p.name + "\n");
+        }
+        out.clearLineNumbers();
+        if (suite != null) {
+            suite.write(out);
+        }
+        out.writeln(".end");
+        out.writeln("");
+    }
+
+}

File HotPy/parser/FunctionScope.java

+package parser;
+import java.util.*;
+import java.io.*;
+import antlr.RecognitionException;
+
+/** Need to rework Local variable logic to work properly in two passes
+ *  First pass identify all used and bound variables
+ *  If variable is used in a nested scope, forces frame onto stack all variables
+ *  become DEREF as frame is heap allocated.  */
+
+public class FunctionScope extends LocalScope {
+
+    static final int ESCAPES = 1024;
+    static final int CLOSURE = 2048;
+
+    FunctionScope(List<Parameter2> parameters, Scope enclosing) throws RecognitionException {
+        super(enclosing instanceof FunctionScope ? enclosing : null);
+        // Is function name in the scope of the function?
+        indices = new HashMap<String, Integer>();
+        Set<String> pNames = new HashSet<String>();
+        for (Parameter2 p : parameters) {
+            if (pNames.contains(p.getName()))
+                throw new RecognitionException("Repeat name in parameters");
+            locals.add(p.getName());
+            pNames.add(p.getName());
+        }
+        this.parameters = parameters;
+    }
+
+    Map<String, Integer> indices;
+    int tempIndex;
+    List<Parameter2> parameters;
+
+    Type variable(String name) {
+        if (!closed)
+            close();
+        Integer i = indices.get(name);
+        if (i == null) {
+            return escape(name, 0);
+        } else {
+            return new FastLocal(name);
+        }
+    }
+
+    Type escape(String name, int depth) {
+        Integer i = indices.get(name);
+        if (i != null) {
+            return new DeRef(i, depth);
+        } else if (enclosing != null) {
+            return enclosing.escape(name, depth + 1);
+        } else {
+            return new GlobalVar(name);
+        }
+    }
+
+    boolean closed;
+
+    void close() {
+        if (closed)
+            return;
+        closed = true;
+        //if (enclosing != null) {
+        //    int name_0 = NameTable.getIndex("0");
+        //    indices.put(name_0, 0);
+        //    locals.set(name_0);
+        //}
+        for (Parameter2 p : parameters) {
+            indices.put(p.getName(), indices.size());
+        }
+        for(String s: locals) {
+            used.contains(s);
+            if (!indices.containsKey(s)) {
+                indices.put(s, indices.size());
+            }
+        }
+    }
+
+    boolean is_closure() {
+        return enclosing != null;
+    }
+
+}

File HotPy/parser/FunctionScope2.java

+package parser;
+import java.util.*;
+import java.io.*;
+import antlr.RecognitionException;
+
+public class FunctionScope2 extends Scope2 {
+
+    FunctionScope2(List<Parameter2> parameters, Scope2 enclosing) {
+        super();
+        // Is function name in the scope of the function?
+        this.enclosing = enclosing;
+        indices = new HashMap<Integer, Integer>();
+        locals = new BitSet();
+        globals = new BitSet();
+        Set<Integer> pNames = new HashSet<Integer>();
+        for (Parameter2 p : parameters) {
+            if (pNames.contains(p.getName()))
+                throw new Error("Repeat name " + p.getName());
+            indices.put(p.getName(), indices.size());
+            locals.set(p.getName());
+            pNames.add(p.getName());
+        }
+    }
+
+    Scope2 enclosing;
+    BitSet locals;
+    BitSet globals;
+    Map<Integer, Integer> indices;
+    int tempIndex;
+
+    Expr name(String name) {
+        return new Variable(name, this);
+    }
+
+
+    void global(String name) {
+        globals.set(name);
+        locals.clear(name);
+    }
+
+    void bind(String name) {
+        locals.set(name, !globals.get(name));
+    }
+
+    Integer getIndex(String name) {
+        return indices.get(name);
+    }
+
+    Scope2 beginFunction(List<Parameter2> parameters) {
+        throw new Error("Nested functions not supported");
+    }
+
+    Scope2 beginClass(String name) {
+        throw new Error("Nested classes not supported");
+    }
+
+    boolean closed;
+
+    Scope2 close() {
+        closed = true;
+        for(int i= locals.nextSetBit(0); i>=0; i=locals.nextSetBit(i+1)) {
+            if (!indices.containsKey(i)) {
+                indices.put(i, indices.size());
+            }
+        }
+        return enclosing;
+    }
+
+
+
+
+}

File HotPy/parser/Global.java

+package parser;
+
+import antlr.RecognitionException;
+import java.util.*;
+
+class Global extends Expr {
+
+    String name;
+
+    Global(String name) {
+        this.name = name;
+    }
+
+    void write(AssemblerWriter out) {
+        out.writeln("LOAD_GLOBAL " + name);
+    }
+
+    void delete(AssemblerWriter out) throws RecognitionException {
+        out.writeln("DELETE_GLOBAL " + name);
+    }
+
+    void writeLHS(AssemblerWriter out) throws RecognitionException {
+        out.writeln("STORE_GLOBAL " + name);
+    }
+
+}
+

File HotPy/parser/Handler.java

+package parser;
+
+public class Handler {
+    
+    int id;
+    int address;
+    Integer type;
+    
+    Handler(int id) {
+        this.id = id;
+    }
+    
+}

File HotPy/parser/HasSpecialExpr.java

+package parser;
+
+import antlr.RecognitionException;
+import antlr.Token;
+import java.util.List;
+
+class HasSpecialExpr extends Expr {
+
+    Expr expr;
+    String name;
+
+    HasSpecialExpr(Expr expr, String name) {
+        this.name = name;
+        this.expr = expr;
+    }
+
+    void write(AssemblerWriter out) throws RecognitionException {
+        expr.write(out);
+        if (name.equals("__dict__")) {
+            out.writeln("HAS_DICT");
+        } else {
+            out.writeln("HAS_SPECIAL " + name);
+        }
+    }
+
+    void writeLHS(AssemblerWriter out) throws RecognitionException {
+        throw new RecognitionException("Cannot assign to special query");
+    }
+
+    void delete(AssemblerWriter out) throws RecognitionException {
+        throw new RecognitionException("Cannot assign to special query");
+    }
+
+    void writeAugAsgn(String op, Expr value, AssemblerWriter out) throws RecognitionException {
+        throw new RecognitionException("Cannot assign to special query");
+    }
+}

File HotPy/parser/Identity.java

+package parser;
+import antlr.RecognitionException;
+import antlr.Token;
+
+class Identity extends Expr {
+
+    Expr left;
+    Expr right;
+
+    Identity(Expr left, Expr right) {
+        this.left = left;
+        this.right = right;
+    }
+
+    void write(AssemblerWriter out) throws RecognitionException {
+        left.write(out);
+        right.write(out);
+        out.writeln("COMPARE_OP is");
+    }
+
+    void writeLHS(AssemblerWriter out) throws RecognitionException {
+        throw new RecognitionException("Cannot assign to binary operator");
+    }
+
+    void delete(AssemblerWriter out) throws RecognitionException {
+        throw new RecognitionException("Cannot delete rvalue");
+    }
+
+}

File HotPy/parser/IfStmt.java

+package parser;
+
+import antlr.RecognitionException;
+import java.util.*;
+
+class IfStmt extends Stmt {
+
+    Expr cond;
+    Stmt suite;
+    Stmt _else = null;
+    List<IfPart> ifs = new ArrayList<IfPart>();
+
+    IfStmt(Expr cond, Stmt suite) {
+        this.cond = cond;
+        this.suite = suite;
+        yields = suite.yields;
+        deletes = suite.deletes;
+    }
+
+    void addIf(Expr cond, Stmt suite, int line) {
+        ifs.add(new IfPart(cond, suite, line));
+        yields |= suite.yields;
+        deletes |= suite.deletes;
+    }
+
+    void addElse(Stmt suite) {
+        _else = suite;
+        yields |= suite.yields;
+        deletes |= suite.deletes;
+    }
+
+    void write(AssemblerWriter out) throws RecognitionException {
+        out.line(line);
+        List<Label> patches = new ArrayList<Label>();
+//        out.write(Opcodes.IF);
+        Label next = new Label();
+        cond.branch(false, out, next);
+        suite.write(out);
+        for(IfPart ip: ifs) {
+            Label l  = new Label();
+            patches.add(l);
+            out.writeln("JUMP_FORWARD " + l);
+            out.line(ip.line);
+            next.write(out);
+            next = new Label();
+            ip.cond.branch(false, out, next);
+            ip.suite.write(out);
+        }
+
+        if (_else != null) {
+            Label e = new Label();
+            out.branch("JUMP_FORWARD", e);
+            patches.add(e);
+            next.write(out);
+            out.line(_else.line);
+            _else.write(out);
+        } else {
+             next.write(out);
+        }
+        for (Label p: patches) {
+            p.write(out);
+        }
+    }
+
+}
+
+class IfPart {
+
+    Expr cond;
+    Stmt suite;
+    int line;
+
+    IfPart(Expr cond, Stmt suite, int line) {
+        this.cond = cond;
+        this.suite = suite;
+        this.line = line;
+    }
+
+}

File HotPy/parser/ImportStmt.java

+package parser;
+
+import antlr.RecognitionException;
+import java.util.*;
+
+public class ImportStmt extends Stmt {
+
+    String module_name;
+    String as_name;
+
+    public ImportStmt(String module_name, String as_name) {
+        this.module_name = module_name;
+        this.as_name = as_name;
+    }
+
+    String first_name() {
+        return module_name.split("\\.")[0];
+    }
+
+    void write(AssemblerWriter out) throws RecognitionException {
+        out.line(line);
+        out.writeln("LOAD_CONST 0");
+        out.writeln("LOAD_CONST None");
+        out.writeln("IMPORT_NAME " + module_name);
+        if (this.as_name == null) {
+            out.writeln("STORE_GLOBAL " + first_name());
+        } else {
+            assert("To do" == "");
+            out.writeln("STORE_GLOBAL " + as_name);
+        }
+    }
+
+}

File HotPy/parser/InExpr.java

+package parser;
+import antlr.RecognitionException;
+import antlr.Token;
+
+class InExpr extends Expr {
+
+    Expr left;
+    Expr right;
+
+    InExpr(Expr left, Expr right) {
+        this.left = left;
+        this.right = right;
+    }
+
+    void write_contains(AssemblerWriter out) throws RecognitionException {
+        left.write(out);
+        right.write(out);
+        out.writeln("COMPARE_OP in");
+    }
+
+    void write(AssemblerWriter out) throws RecognitionException {
+        write_contains(out);
+    }
+
+    void writeLHS(AssemblerWriter out) throws RecognitionException {
+        throw new RecognitionException("Cannot assign to binary operator");
+    }
+
+    void delete(AssemblerWriter out) throws RecognitionException {
+        throw new RecognitionException("Cannot delete rvalue");
+    }
+
+}

File HotPy/parser/Index.java

+package parser;
+
+import antlr.RecognitionException;
+import antlr.Token;
+
+class Index extends Expr {
+
+    Expr left;
+    Expr args;
+
+    Index(Expr left,  Expr args) {
+        this.left = left;
+        this.args = args;
+    }
+
+    void write_get(AssemblerWriter out) {
+        //out.write(Opcodes.SWAP_LOAD_SPECIAL);
+        //out.write(0);
+        //out.write(StandardNames.__getitem__);
+        //out.write(Opcodes.SWAP_CALL_SIMPLE);
+        //out.write(1);
+        out.writeln("BINARY_SUBSCR");
+    }
+
+    void write_set(AssemblerWriter out) {
+        //out.write(Opcodes.SWAP_LOAD_SPECIAL);
+        //out.write(0);
+        //out.write(StandardNames.__setitem__);
+        //out.write(Opcodes.FLIP_CALL_SIMPLE);
+        //out.write(2);
+        //out.write(Opcodes.DROP);
+        out.writeln("STORE_SUBSCR");
+    }
+
+    void write(AssemblerWriter out) throws RecognitionException {
+        left.write(out);
+        args.write(out);
+        write_get(out);
+    }
+
+    void writeLHS(AssemblerWriter out) throws RecognitionException {
+        left.write(out);
+        args.write(out);
+        write_set(out);
+    }
+
+    void writeAugAsgn(String op, Expr value, AssemblerWriter out) throws RecognitionException {
+        left.write(out);    // l
+        args.write(out); // l a
+        out.writeln("DUP_TOP_TWO"); // l a l a
+        write_get(out);
+        value.write(out);   // l a l[a] v
+        out.writeln(op);   // l a l[a]+v
+        out.writeln("ROT_THREE");
+        write_set(out);
+    }
+
+    void delete(AssemblerWriter out) throws RecognitionException {
+        left.write(out);
+        args.write(out);
+        //out.write(Opcodes.SWAP_LOAD_SPECIAL);
+        //out.write(0);
+        //out.write(StandardNames.__delitem__);
+        //out.write(Opcodes.SWAP_CALL_SIMPLE);
+        //out.write(1);
+        //out.write(Opcodes.DROP);
+        out.writeln("DELETE_SUBSCR");
+    }
+
+}

File HotPy/parser/InteractiveScope.java

+package parser;
+import java.util.*;
+import java.io.*;
+import antlr.RecognitionException;
+
+public class InteractiveScope extends Scope2 {
+
+    InteractiveScope() {
+        super();
+    }
+
+    Expr name(String name) {
+        return new Global(name);
+    }
+
+    Stmt discard(final Expr e) {
+        return new Stmt() {
+            void write(AssemblerWriter out) throws RecognitionException {
+                e.write(out);
+                // Leave on stack
+            }
+        };
+    }
+
+    void global(String name) {
+        // Ignore.
+    }
+
+    void bind(String name) {
+        // Ignore
+    }
+
+    Scope2 beginFunction(List parameters) {
+        return new FunctionScope2(parameters, this);
+    }
+
+    Scope2 beginClass(String name) {
+        return new ClassScope(name, this);
+    }
+
+    Scope2 close() {
+        return null;
+    }
+
+    int tempIndex;
+
+
+}

File HotPy/parser/Label.java

+package parser;
+
+class Label {
+
+    private static int next_id;
+    private int id;
+
+    public Label() {
+        this.id = next_id++;
+    }
+
+    public String toString() {
+        return "l" + id;
+    }
+
+    void write(AssemblerWriter out) {
+        out.writeLabel("l" + id);
+    }
+
+}

File HotPy/parser/LambdaExpr.java

+package parser;
+import java.util.*;
+import java.io.*;
+import antlr.RecognitionException;
+import antlr.Token;
+
+public final class LambdaExpr extends Expr {
+
+    String name;
+    int defaults;
+    List<Parameter2> parameters;