[Bprocessor-commit] bscript/src/net/sourceforge/bprocessor/model/evaluator BranchTrue.java, NONE,
Status: Pre-Alpha
Brought to you by:
henryml
From: Michael L. <he...@us...> - 2006-09-17 17:29:23
|
Update of /cvsroot/bprocessor/bscript/src/net/sourceforge/bprocessor/model/evaluator In directory sc8-pr-cvs3.sourceforge.net:/tmp/cvs-serv9471/src/net/sourceforge/bprocessor/model/evaluator Modified Files: Mark.java Primitive.java Operation.java Environment.java Call.java Literal.java Variable.java Function.java Added Files: BranchTrue.java Branch.java BranchFalse.java Assign.java Log Message: Improved scripting Index: Variable.java =================================================================== RCS file: /cvsroot/bprocessor/bscript/src/net/sourceforge/bprocessor/model/evaluator/Variable.java,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -d -r1.3 -r1.4 *** Variable.java 12 Sep 2006 09:27:31 -0000 1.3 --- Variable.java 17 Sep 2006 17:29:21 -0000 1.4 *************** *** 29,35 **** * @param env Environment * @param stack Stack */ ! public void evaluate(Environment env, Stack stack) { stack.push(env.lookupGlobal(id)); } } --- 29,37 ---- * @param env Environment * @param stack Stack + * @return offset */ ! public int evaluate(Environment env, Stack stack) { stack.push(env.lookupGlobal(id)); + return 1; } } --- NEW FILE: Assign.java --- //--------------------------------------------------------------------------------- // $Id: Assign.java,v 1.1 2006/09/17 17:29:21 henryml Exp $ // // Copyright (c) 2005 The BProcessor Team (http://bprocessor.sourceforge.net) // Released under the Lesser GNU Public License v2.1 //--------------------------------------------------------------------------------- package net.sourceforge.bprocessor.model.evaluator; import java.util.Stack; /** * Variable */ public class Assign extends Operation { /** id */ private String id; /** * Constructor * @param id String */ public Assign(String id) { super(); this.id = id; } /** * @param env Environment * @param stack Stack * @return offset */ public int evaluate(Environment env, Stack stack) { env.assignGlobal(id, stack.pop()); return 1; } } --- NEW FILE: Branch.java --- //--------------------------------------------------------------------------------- // $Id: Branch.java,v 1.1 2006/09/17 17:29:21 henryml Exp $ // // Copyright (c) 2005 The BProcessor Team (http://bprocessor.sourceforge.net) // Released under the Lesser GNU Public License v2.1 //--------------------------------------------------------------------------------- package net.sourceforge.bprocessor.model.evaluator; import java.util.Stack; /** * BranchTrue */ public class Branch extends Operation { /** offset */ private int offset; /** * * @param offset int */ public Branch(int offset) { super(); this.offset = offset; } /** * */ public Branch() { super(); } /** * Set offset * @param offset int */ public void setOffset(int offset) { this.offset = offset; } /** * */ public int evaluate(Environment env, Stack stack) { return offset; } } Index: Call.java =================================================================== RCS file: /cvsroot/bprocessor/bscript/src/net/sourceforge/bprocessor/model/evaluator/Call.java,v retrieving revision 1.4 retrieving revision 1.5 diff -C2 -d -r1.4 -r1.5 *** Call.java 14 Sep 2006 09:21:46 -0000 1.4 --- Call.java 17 Sep 2006 17:29:21 -0000 1.5 *************** *** 35,39 **** * @param stack Stack */ ! public void evaluate(Environment env, Stack stack) { int length = stack.search(Function.mark) - 1; List arguments = new ArrayList(length); --- 35,39 ---- * @param stack Stack */ ! public int evaluate(Environment env, Stack stack) { int length = stack.search(Function.mark) - 1; List arguments = new ArrayList(length); *************** *** 65,68 **** --- 65,69 ---- stack.push(name); } + return 1; } } --- NEW FILE: BranchFalse.java --- //--------------------------------------------------------------------------------- // $Id: BranchFalse.java,v 1.1 2006/09/17 17:29:21 henryml Exp $ // // Copyright (c) 2005 The BProcessor Team (http://bprocessor.sourceforge.net) // Released under the Lesser GNU Public License v2.1 //--------------------------------------------------------------------------------- package net.sourceforge.bprocessor.model.evaluator; import java.util.Stack; /** * BranchTrue */ public class BranchFalse extends Operation { /** offset */ private int offset; /** * * @param offset int */ public BranchFalse(int offset) { super(); this.offset = offset; } /** * */ public BranchFalse() { super(); } /** * Set offset * @param offset int */ public void setOffset(int offset) { this.offset = offset; } /** * */ public int evaluate(Environment env, Stack stack) { Boolean value = (Boolean) stack.pop(); if (!value.booleanValue()) { return offset; } else { return 1; } } } Index: Primitive.java =================================================================== RCS file: /cvsroot/bprocessor/bscript/src/net/sourceforge/bprocessor/model/evaluator/Primitive.java,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -d -r1.3 -r1.4 *** Primitive.java 12 Sep 2006 09:27:30 -0000 1.3 --- Primitive.java 17 Sep 2006 17:29:21 -0000 1.4 *************** *** 24,27 **** --- 24,33 ---- /** neg */ public static final int NEG = 5; + /** eq */ + public static final int EQ = 6; + /** lt */ + public static final int LT = 7; + /** gt */ + public static final int GT = 8; /** opcode */ *************** *** 36,45 **** this.opcode = opcode; } ! /** * @param env Environment * @param stack Stack */ ! public void evaluate(Environment env, Stack stack) { switch(opcode) { case ADD: { --- 42,52 ---- this.opcode = opcode; } ! /** * @param env Environment * @param stack Stack + * @return offset */ ! public int evaluate(Environment env, Stack stack) { switch(opcode) { case ADD: { *************** *** 72,77 **** break; } ! } } } --- 79,102 ---- break; } ! case EQ: { ! Object op1 = stack.pop(); ! Object op2 = stack.pop(); ! stack.push(new Boolean(op1.equals(op2))); ! break; ! } ! case LT: { ! Comparable op1 = (Comparable) stack.pop(); ! Comparable op2 = (Comparable) stack.pop(); ! stack.push(new Boolean(op1.compareTo(op2) > 0)); ! break; } + case GT: { + Comparable op1 = (Comparable) stack.pop(); + Comparable op2 = (Comparable) stack.pop(); + stack.push(new Boolean(op1.compareTo(op2) < 0)); + break; + } + } + return 1; } } Index: Literal.java =================================================================== RCS file: /cvsroot/bprocessor/bscript/src/net/sourceforge/bprocessor/model/evaluator/Literal.java,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -d -r1.3 -r1.4 *** Literal.java 12 Sep 2006 09:27:30 -0000 1.3 --- Literal.java 17 Sep 2006 17:29:21 -0000 1.4 *************** *** 29,35 **** * @param env Environment * @param stack Stack */ ! public void evaluate(Environment env, Stack stack) { stack.push(value); } } --- 29,37 ---- * @param env Environment * @param stack Stack + * @return offset */ ! public int evaluate(Environment env, Stack stack) { stack.push(value); + return 1; } } Index: Mark.java =================================================================== RCS file: /cvsroot/bprocessor/bscript/src/net/sourceforge/bprocessor/model/evaluator/Mark.java,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** Mark.java 12 Sep 2006 09:27:30 -0000 1.1 --- Mark.java 17 Sep 2006 17:29:21 -0000 1.2 *************** *** 26,32 **** * @param env Environment * @param stack Stack */ ! public void evaluate(Environment env, Stack stack) { stack.push(Function.mark); } --- 26,34 ---- * @param env Environment * @param stack Stack + * @return offset */ ! public int evaluate(Environment env, Stack stack) { stack.push(Function.mark); + return 1; } --- NEW FILE: BranchTrue.java --- //--------------------------------------------------------------------------------- // $Id: BranchTrue.java,v 1.1 2006/09/17 17:29:21 henryml Exp $ // // Copyright (c) 2005 The BProcessor Team (http://bprocessor.sourceforge.net) // Released under the Lesser GNU Public License v2.1 //--------------------------------------------------------------------------------- package net.sourceforge.bprocessor.model.evaluator; import java.util.Stack; /** * BranchTrue */ public class BranchTrue extends Operation { /** offset */ private int offset; /** * * @param offset int */ public BranchTrue(int offset) { super(); this.offset = offset; } /** * */ public BranchTrue() { super(); } /** * Set offset * @param offset int */ public void setOffset(int offset) { this.offset = offset; } /** * */ public int evaluate(Environment env, Stack stack) { Boolean value = (Boolean) stack.pop(); if (value.booleanValue()) { return offset; } else { return 1; } } } Index: Operation.java =================================================================== RCS file: /cvsroot/bprocessor/bscript/src/net/sourceforge/bprocessor/model/evaluator/Operation.java,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -d -r1.3 -r1.4 *** Operation.java 12 Sep 2006 09:27:30 -0000 1.3 --- Operation.java 17 Sep 2006 17:29:21 -0000 1.4 *************** *** 26,30 **** * @param env Environment * @param stack Stack */ ! public abstract void evaluate(Environment env, Stack stack); } --- 26,31 ---- * @param env Environment * @param stack Stack + * @return offset */ ! public abstract int evaluate(Environment env, Stack stack); } Index: Function.java =================================================================== RCS file: /cvsroot/bprocessor/bscript/src/net/sourceforge/bprocessor/model/evaluator/Function.java,v retrieving revision 1.5 retrieving revision 1.6 diff -C2 -d -r1.5 -r1.6 *** Function.java 14 Sep 2006 09:21:46 -0000 1.5 --- Function.java 17 Sep 2006 17:29:21 -0000 1.6 *************** *** 12,15 **** --- 12,16 ---- import java.util.List; import java.util.Stack; + import java.util.Vector; /** *************** *** 27,31 **** /** operations */ ! private List operations; /** --- 28,32 ---- /** operations */ ! private Vector operations; /** *************** *** 34,38 **** public Function() { super(); ! operations = new LinkedList(); } --- 35,39 ---- public Function() { super(); ! operations = new Vector(); } *************** *** 43,47 **** super(); this.name = name; ! operations = new LinkedList(); } --- 44,48 ---- super(); this.name = name; ! operations = new Vector(); } *************** *** 53,57 **** this.name = name; this.formals = formals; ! operations = new LinkedList(); } --- 54,58 ---- this.name = name; this.formals = formals; ! operations = new Vector(); } *************** *** 66,69 **** --- 67,78 ---- /** * + * @return + */ + public int length() { + return operations.size(); + } + + /** + * * @return formals */ *************** *** 77,84 **** */ public void evaluate(Environment env, Stack stack) { ! Iterator iter = operations.iterator(); ! while (iter.hasNext()) { ! Operation current = (Operation) iter.next(); ! current.evaluate(env, stack); } } --- 86,94 ---- */ public void evaluate(Environment env, Stack stack) { ! int pc = 0; ! while (pc < operations.size()) { ! Operation current = (Operation) operations.get(pc); ! int offset = current.evaluate(env, stack); ! pc += offset; } } Index: Environment.java =================================================================== RCS file: /cvsroot/bprocessor/bscript/src/net/sourceforge/bprocessor/model/evaluator/Environment.java,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -d -r1.3 -r1.4 *** Environment.java 13 Sep 2006 13:36:32 -0000 1.3 --- Environment.java 17 Sep 2006 17:29:21 -0000 1.4 *************** *** 84,87 **** --- 84,130 ---- /** + * Define new local variable + * @param name String + * @param value Object + */ + public void define(String name, Object value) { + variables.put(name, value); + } + + /** + * Attempt to assign a variable. + * Return false of the variable does not exist. + * @param name String + * @param value Object + * @return true of variable exists + */ + public boolean assign(String name, Object value) { + if (variables.containsKey(name)) { + variables.put(name, value); + return true; + } else { + if (origin != null) { + return origin.assign(name, value); + } else { + return false; + } + } + } + + /** + * Attemt to assign the variable in the + * local scope and any inherited scope. + * If the variable does not exist, then + * define it in the local scope. + * @param name String + * @param value Object + */ + public void assignGlobal(String name, Object value) { + if (!assign(name, value)) { + define(name, value); + } + } + + /** * Return local * @param i Index *************** *** 91,93 **** --- 134,144 ---- return indexables.get(i); } + + /** + * Return number of indexables + * @return number of indexables + */ + public int length() { + return indexables.size(); + } } |