import com.veling.io.*; import java.io.*; import java.util.*; public class Lambda { //statics public static final int NOP = 0; public static final int READ = 1; public static final int LOAD = 2; public static final int ADD = 3; public static final int PRINT = 4; public static final int INSTRUCTION = 32; public static final int DATA = 34; public static final int RESULT = 36; public static final int VALUE = 255; public static void main (String[] args) { if (args.length==1) { String compiled = args[0]+".L"; compile(args[0],compiled); load(compiled); go(); } else { System.out.println("usage: Lambda mysourcefile"); } } public static void compile(String fn, String compiledfn) { try { FastInputStream in = new FastInputStream(new FileInputStream(fn)); HashMap labels = new HashMap(100); labels.put("nop",new Integer(NOP)); labels.put("read",new Integer(READ)); labels.put("load",new Integer(LOAD)); labels.put("add",new Integer(ADD)); labels.put("print",new Integer(PRINT)); labels.put("instruction",new Integer(INSTRUCTION)); labels.put("data",new Integer(DATA)); labels.put("result",new Integer(RESULT)); //put all tokens in a list ArrayList tokens = new ArrayList(1000); String line,token; StringTokenizer tokenizer; Integer value; int lastlabel = -1; try { while (!in.eof) { line = in.readLine(); //skip after comment int commentidx = line.indexOf("//"); if (commentidx>=0) { if (commentidx==0) continue; line = line.substring(0,commentidx-1); } if (line.length()==0) continue; System.out.println("tokenizing "+(256+tokens.size())+" ["+line+"]"); tokenizer = new StringTokenizer(line," ()",false); while (tokenizer.hasMoreTokens()) { token = tokenizer.nextToken(); if (token.endsWith(":")) { //is a label token = token.substring(0,token.length()-1); lastlabel = tokens.size(); labels.put(token,new Integer(256+lastlabel)); //put next address } else { //check for integer literals try { value = Integer.decode(token); //see if is the first after a label (a variable init) tokens.add(value); tokens.add(null); //VALUE tag in postfix //if (lastlabel!=tokens.size()) { //} } catch (NumberFormatException e) { //probably a label anyway //check for value if (token.endsWith("!")) { token = token.substring(0,token.length()-1); tokens.add(token); tokens.add(null); } else { tokens.add(token); } } lastlabel = -1; } } } } catch (EOFException e) {} in.close(); System.out.println("compiling"); FastOutputStream out = new FastOutputStream(new FileOutputStream(compiledfn)); Integer address; Object o; for (int i=0; i=memory.length) { int[] newmem = new int[2*memorysize]; System.arraycopy(memory,0,newmem,0,memory.length); memory = newmem; } memory[memorysize++] = in.readInt(); } } catch (EOFException e) {} in.close(); //reset pointers memory[INSTRUCTION] = 256; memory[INSTRUCTION+1] = VALUE; memory[DATA+1] = VALUE; memory[RESULT+1] = VALUE; memory[RESULT+2] = LOAD; memory[RESULT+3] = INSTRUCTION; memory[RESULT+4] = VALUE; memory[RESULT+5] = 0; memory[RESULT+6] = VALUE; //exit System.out.println("read "+(memorysize-256)+" tokens"); } catch (IOException e) { System.out.println("catched "+e+" with message "+e.getMessage()); } } protected static void go() { System.out.println("executing"); StringBuffer buf = new StringBuffer(30); while ((memory[INSTRUCTION]>0) && (memory[INSTRUCTION]=memorysize) { System.out.println("INFINITY; ready."); } else { System.out.println("ready."); } } protected static int get(int idx) { if ((idx>=0) && (idx=0) && (idx"+memory[RESULT]); break; default: if (get(opcode+1)==VALUE) { //reference to variable value; do directly buf.append(" read "+opcode); memory[RESULT] = get(opcode); memory[INSTRUCTION]++; } else { buf.append(" jump "+opcode); //this is a goto memory[DATA] = memory[INSTRUCTION] + 1; //memory[RESULT] = get(opcode); memory[INSTRUCTION] = opcode; buf.append(" ("); interpret(buf); buf.append(")"); } break; } } }