package net.mightypork.rcalc; import java.util.HashMap; import java.util.Map.Entry; import java.util.regex.Matcher; import java.util.regex.Pattern; import net.mightypork.rcalc.numbers.Fraction; /** * Rational Calculator Session - interactive RCalc with possibility to set and * use variables. * * @author Ondrej Hruska */ public class RCalcSession implements IDebugable { /** Debug mode flag */ private boolean debug = false; /** Calculator instance */ private RCalc rcalc = new RCalc(); /** Table of user defined variables */ private HashMap symbolTable = new HashMap(); @Override public boolean isDebug() { return debug; } @Override public void setDebug(boolean debug) { rcalc.setDebug(debug); this.debug = debug; } /** * Get variable value * * @param name variable name * @return the value */ public Fraction getVariable(String name) { return symbolTable.get(name.toLowerCase()); } /** * Set a variable value * * @param name variable name * @param value value to set */ public void setVariable(String name, Fraction value) { symbolTable.put(name.trim(), value); } /** * Evaluate an expression * * @param expression expression * @return result */ public Fraction evaluate(String expression) { expression = expression.trim().toLowerCase(); if (expression.length() == 0) return null; // no operation expression = expression.replace(":=", "="); // normalize assignment sign String assignedVariable = null; String assignmentOperation = ""; // parse name=value syntax for variables if (expression.matches("^[a-z]+\\s*[+\\-*/%^]?=\\s*(.+)$")) { Pattern pattern = Pattern.compile("^([a-z]+)\\s*([+\\-*/%^]?=)\\s*(.+)$"); Matcher matcher = pattern.matcher(expression); if (!matcher.find() || matcher.groupCount() != 3) { throw new ParseError("Invalid expression format, check your syntax."); } assignedVariable = matcher.group(1).trim(); assignmentOperation = matcher.group(2).trim(); expression = matcher.group(3).trim(); } // replace variable names with their values for (Entry entry : symbolTable.entrySet()) { String regex = "(?:(? table) { this.symbolTable = table; } /** * Get the current symbol table * * @return symbol table */ public HashMap getSymbolTable() { return symbolTable; } }