package com.drawbridge.jsengine.ast; import java.util.LinkedList; import com.drawbridge.jsengine.Scope; import com.drawbridge.jsengine.jsobjects.JSNumber; import com.drawbridge.jsengine.jsobjects.JSType; import com.drawbridge.text.TextPanel; import com.drawbridge.utils.Utils; import com.drawbridge.vl.blocks.BlockPrimitive; import com.google.caja.parser.js.IntegerLiteral; public class IntegerLiteralEvaluator extends NumberLiteralEvaluator<Integer> { private IntegerLiteral mIntegerLiteral; private int max = Integer.MAX_VALUE; private int min = Integer.MIN_VALUE; public IntegerLiteralEvaluator(Evaluator parent, Scope scope, IntegerLiteral integerLiteral) { super(parent, scope, integerLiteral.getFilePosition()); mIntegerLiteral = integerLiteral; } @Override public JSType evaluate() throws EvaluatorException { return new JSNumber(mIntegerLiteral.getValue().intValue()); } @Override public LinkedList<com.drawbridge.vl.blocks.Block> getBlocks() { LinkedList<com.drawbridge.vl.blocks.Block> results = new LinkedList<com.drawbridge.vl.blocks.Block>(); BlockPrimitive pb = new BlockPrimitive(getFilePosition(), this, "" + mIntegerLiteral.getValue().intValue()); results.add(pb); return results; } /** * * @param listener * @param newVal * @return boolean returns the change in the file position */ @Override public void modifyValue(ParserListener listener, Integer newVal, boolean updateSnapshot) { int newValidValue = (int) getValidValue((newVal * 1.0)); // Apply a valid value mIntegerLiteral = new IntegerLiteral(getFilePosition(), newValidValue); // Update TextPanel based on modifiedValue if (TextPanel.hasInstance() && updateSnapshot) { String before = ""; String after = ""; try { int line = getFilePosition().startLineNo()-1; String text = DBParser.getInstance().getSnapshot(); String [] lines = text.split("\n"); if(line < lines.length){ before = lines[line].substring(0, getFilePosition().startCharInLine()-1); after = lines[line].substring(getFilePosition().startCharInLine()-1 + getFilePosition().length(), lines[line].length()); } String finalText = ""; for(int i = 0; i < lines.length;i++){ if(i != line){ finalText += lines[i]; } else{ finalText += before + newValidValue + after; } if(i < lines.length -1) finalText += "\n"; } DBParser.getInstance().setSnapshot(finalText); } catch (StringIndexOutOfBoundsException e) { Utils.out.println("String index exception"); e.printStackTrace(); } } // Make the parser aware DBParser.getInstance().notifyOfEvalChange(listener); // if (oldValue != newValidValue && !DBParser.getInstance().mRecentlyChangedEvaluators.contains(this)) DBParser.getInstance().mRecentlyChangedEvaluators.add(this); } public int getFPDiff(Integer newVal) { int integerLength = ("" + mIntegerLiteral.getValue().intValue()).length(); int newValLength = ("" + newVal).length(); Utils.out.println("Length of " + mIntegerLiteral.getValue().intValue() + " is " + integerLength + ", Length of new value " + newVal + " is " + newValLength + " diff:" + (newValLength - integerLength)); return newValLength - integerLength; } @Override public Integer getValidValue(Double newVal) { if (newVal > max) return max; else if (newVal < min) return min; else return (int) Math.round(newVal); } @Override public boolean isValidValue(Double newVal) { return !(newVal > max || newVal < min); } public void setMaximumInt(int max) { this.max = max; } public void setMinimumInt(int min) { this.min = min; } @Override public boolean compare(Evaluator e) { if (!e.getClass().equals(getClass())) return false; IntegerLiteralEvaluator ile = (IntegerLiteralEvaluator) e; if (mIntegerLiteral.getValue().intValue() == ile.mIntegerLiteral.getValue().intValue() && e.getFilePosition().equals(getFilePosition())) return true; else return false; } @Override public String toString() { return "IntegerLiteralEvaluator (" + mIntegerLiteral.getValue().intValue() + ")"; } public Integer getMaxValue() { return max; } public Integer getMinValue() { return min; } public boolean hasCustomMax(){ return (max != Integer.MAX_VALUE); } public boolean hasCustomMin(){ return (min != Integer.MIN_VALUE); } @Override public void setMaximum(Integer max) { this.max = max; } @Override public void setMinimum(Integer min) { this.min = min; } }