Thread: [Bprocessor-commit] bscript/src/net/sourceforge/bprocessor/model/evaluator ArityError.java, NONE,
Status: Pre-Alpha
Brought to you by:
henryml
From: Michael L. <he...@us...> - 2006-10-11 11:23:36
|
Update of /cvsroot/bprocessor/bscript/src/net/sourceforge/bprocessor/model/evaluator In directory sc8-pr-cvs3.sourceforge.net:/tmp/cvs-serv26191/src/net/sourceforge/bprocessor/model/evaluator Modified Files: Primitive.java Operation.java Invokable.java Function.java Call.java Added Files: ArityError.java PrimitiveTypeError.java SemanticError.java Log Message: Improved errorhandling in scripting --- NEW FILE: ArityError.java --- package net.sourceforge.bprocessor.model.evaluator; import java.util.List; public class ArityError extends SemanticError { private List arguments; private Function function; public ArityError(List arguments, Function function) { super(); this.arguments = arguments; this.function = function; } public String toString() { return function.name() + " requires " + function.formals().size() + " arguments but got " + arguments.size() + " : " + arguments; } } Index: Call.java =================================================================== RCS file: /cvsroot/bprocessor/bscript/src/net/sourceforge/bprocessor/model/evaluator/Call.java,v retrieving revision 1.7 retrieving revision 1.8 diff -C2 -d -r1.7 -r1.8 *** Call.java 29 Sep 2006 07:13:16 -0000 1.7 --- Call.java 11 Oct 2006 11:23:26 -0000 1.8 *************** *** 35,39 **** * @param stack Stack */ ! public int 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) throws SemanticError { int length = stack.search(Function.mark) - 1; List arguments = new ArrayList(length); *************** *** 52,56 **** if (invokable instanceof Function) { Function function = (Function) invokable; ! Iterator names = function.formals().iterator(); Iterator values = arguments.iterator(); while (names.hasNext()) { --- 52,63 ---- if (invokable instanceof Function) { Function function = (Function) invokable; ! List formals = function.formals(); ! ! ! if (formals.size() != arguments.size()) { ! throw new ArityError(arguments, function); ! } ! ! Iterator names = formals.iterator(); Iterator values = arguments.iterator(); while (names.hasNext()) { Index: Invokable.java =================================================================== RCS file: /cvsroot/bprocessor/bscript/src/net/sourceforge/bprocessor/model/evaluator/Invokable.java,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** Invokable.java 14 Sep 2006 09:21:46 -0000 1.2 --- Invokable.java 11 Oct 2006 11:23:26 -0000 1.3 *************** *** 17,21 **** * @param env Environment * @param stack Stack */ ! public void evaluate(Environment env, Stack stack); } --- 17,22 ---- * @param env Environment * @param stack Stack + * @throws SemanticError */ ! public void evaluate(Environment env, Stack stack) throws SemanticError; } Index: Primitive.java =================================================================== RCS file: /cvsroot/bprocessor/bscript/src/net/sourceforge/bprocessor/model/evaluator/Primitive.java,v retrieving revision 1.6 retrieving revision 1.7 diff -C2 -d -r1.6 -r1.7 *** Primitive.java 10 Oct 2006 14:41:36 -0000 1.6 --- Primitive.java 11 Oct 2006 11:23:26 -0000 1.7 *************** *** 53,128 **** * @return offset */ ! public int evaluate(Environment env, Stack stack) { switch(opcode) { case ADD: { ! Double op1 = (Double) stack.pop(); ! Double op2 = (Double) stack.pop(); ! stack.push(new Double(op1.doubleValue() + op2.doubleValue())); ! break; } case SUB: { ! Double op1 = (Double) stack.pop(); ! Double op2 = (Double) stack.pop(); ! stack.push(new Double(op2.doubleValue() - op1.doubleValue())); ! break; } case MUL: { ! Double op1 = (Double) stack.pop(); ! Double op2 = (Double) stack.pop(); ! stack.push(new Double(op1.doubleValue() * op2.doubleValue())); ! break; } case DIV: { ! Double op1 = (Double) stack.pop(); ! Double op2 = (Double) stack.pop(); ! stack.push(new Double(op2.doubleValue() / op1.doubleValue())); ! break; } case NEG: { ! Double op1 = (Double) stack.pop(); ! stack.push(new Double(-op1.doubleValue())); ! 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; } case GET: { ! Double op1 = (Double) stack.pop(); ! List op2 = (List) stack.pop(); ! stack.push(op2.get(op1.intValue())); ! break; } case LST: { ! int length = stack.search(Function.mark) - 1; ! List arguments = new ArrayList(length); ! for (int i = 0; i < length; i++) { ! arguments.add(null); ! } ! while (length > 0) { ! Object current = stack.pop(); ! arguments.set(length - 1, current); ! length--; ! } ! stack.pop(); ! stack.push(arguments); ! break; } } - return 1; } } --- 53,172 ---- * @return offset */ ! public int evaluate(Environment env, Stack stack) throws SemanticError { ! try { ! switch(opcode) { ! case ADD: { ! Double op1 = (Double) stack.pop(); ! Double op2 = (Double) stack.pop(); ! stack.push(new Double(op1.doubleValue() + op2.doubleValue())); ! break; ! } ! case SUB: { ! Double op1 = (Double) stack.pop(); ! Double op2 = (Double) stack.pop(); ! stack.push(new Double(op2.doubleValue() - op1.doubleValue())); ! break; ! } ! case MUL: { ! Double op1 = (Double) stack.pop(); ! Double op2 = (Double) stack.pop(); ! stack.push(new Double(op1.doubleValue() * op2.doubleValue())); ! break; ! } ! case DIV: { ! Double op1 = (Double) stack.pop(); ! Double op2 = (Double) stack.pop(); ! stack.push(new Double(op2.doubleValue() / op1.doubleValue())); ! break; ! } ! case NEG: { ! Double op1 = (Double) stack.pop(); ! stack.push(new Double(-op1.doubleValue())); ! 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; ! } ! case GET: { ! Double op1 = (Double) stack.pop(); ! List op2 = (List) stack.pop(); ! stack.push(op2.get(op1.intValue())); ! break; ! } ! case LST: { ! int length = stack.search(Function.mark) - 1; ! List arguments = new ArrayList(length); ! for (int i = 0; i < length; i++) { ! arguments.add(null); ! } ! while (length > 0) { ! Object current = stack.pop(); ! arguments.set(length - 1, current); ! length--; ! } ! stack.pop(); ! stack.push(arguments); ! break; ! } ! } ! } catch (Exception e) { ! if (e instanceof ClassCastException) { ! throw new PrimitiveTypeError(this); ! } ! System.out.println("caught: " + e); ! } ! return 1; ! } ! ! public String toString() { switch(opcode) { case ADD: { ! return "+"; } case SUB: { ! return "-"; } case MUL: { ! return "*"; } case DIV: { ! return "/"; } case NEG: { ! return "-"; } case EQ: { ! return "="; } case LT: { ! return "<"; } case GT: { ! return ">"; } case GET: { ! return "array access"; } case LST: { ! return "list"; } + default: + return ""; } } } --- NEW FILE: SemanticError.java --- package net.sourceforge.bprocessor.model.evaluator; public class SemanticError extends Exception { public SemanticError() { super(); } } Index: Operation.java =================================================================== RCS file: /cvsroot/bprocessor/bscript/src/net/sourceforge/bprocessor/model/evaluator/Operation.java,v retrieving revision 1.4 retrieving revision 1.5 diff -C2 -d -r1.4 -r1.5 *** Operation.java 17 Sep 2006 17:29:21 -0000 1.4 --- Operation.java 11 Oct 2006 11:23:26 -0000 1.5 *************** *** 28,31 **** * @return offset */ ! public abstract int evaluate(Environment env, Stack stack); } --- 28,31 ---- * @return offset */ ! public abstract int evaluate(Environment env, Stack stack) throws SemanticError; } Index: Function.java =================================================================== RCS file: /cvsroot/bprocessor/bscript/src/net/sourceforge/bprocessor/model/evaluator/Function.java,v retrieving revision 1.6 retrieving revision 1.7 diff -C2 -d -r1.6 -r1.7 *** Function.java 17 Sep 2006 17:29:21 -0000 1.6 --- Function.java 11 Oct 2006 11:23:26 -0000 1.7 *************** *** 14,17 **** --- 14,19 ---- import java.util.Vector; + import antlr.CharScanner; + /** * Function *************** *** 81,89 **** } /** * @param env Environment * @param stack Stack */ ! public void evaluate(Environment env, Stack stack) { int pc = 0; while (pc < operations.size()) { --- 83,95 ---- } + public String name() { + return name; + } + /** * @param env Environment * @param stack Stack */ ! public void evaluate(Environment env, Stack stack) throws SemanticError { int pc = 0; while (pc < operations.size()) { --- NEW FILE: PrimitiveTypeError.java --- package net.sourceforge.bprocessor.model.evaluator; public class PrimitiveTypeError extends SemanticError { private Primitive primitive; public PrimitiveTypeError(Primitive primitive) { super(); this.primitive = primitive; } public String toString() { return "wrong arguments for " + primitive; } } |