evaluating divide and minus in correct order
git-svn-id: svn://anubis/gvsu@116 45c1a28c-8058-47b2-ae61-ca45b979098e
This commit is contained in:
parent
d4c0c32b25
commit
6358a63af5
@ -6,7 +6,7 @@ import jholtrop.gvsu.cs675.bottomup.node.*;
|
|||||||
import jholtrop.gvsu.cs675.bottomup.lexer.Lexer;
|
import jholtrop.gvsu.cs675.bottomup.lexer.Lexer;
|
||||||
import jholtrop.gvsu.cs675.bottomup.parser.Parser;
|
import jholtrop.gvsu.cs675.bottomup.parser.Parser;
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
import java.util.HashMap;
|
import java.util.Stack;
|
||||||
|
|
||||||
public class Evaluator extends DepthFirstAdapter
|
public class Evaluator extends DepthFirstAdapter
|
||||||
{
|
{
|
||||||
@ -20,7 +20,9 @@ public class Evaluator extends DepthFirstAdapter
|
|||||||
|
|
||||||
Start tree = p.parse();
|
Start tree = p.parse();
|
||||||
|
|
||||||
tree.apply(new Evaluator());
|
Evaluator e = new Evaluator();
|
||||||
|
tree.apply(e);
|
||||||
|
System.out.println("Result: " + e.getResult());
|
||||||
}
|
}
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
{
|
{
|
||||||
@ -28,38 +30,39 @@ public class Evaluator extends DepthFirstAdapter
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private HashMap<Node, Double> m_exprValues = new HashMap<Node, Double>();
|
private Stack<Double> m_exprValues = new Stack<Double>();
|
||||||
|
|
||||||
private double getNumValue(TNumber num)
|
public double getResult()
|
||||||
{
|
{
|
||||||
return Double.parseDouble(num.getText());
|
return m_exprValues.peek();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void outAAdditionExpr(AAdditionExpr e)
|
public void outAAdditionExpr(AAdditionExpr e)
|
||||||
{
|
{
|
||||||
Double result =
|
m_exprValues.push(m_exprValues.pop() + m_exprValues.pop());
|
||||||
m_exprValues.get(e.getExpr()) + m_exprValues.get(e.getTerm());
|
|
||||||
m_exprValues.put(e, new Double(result));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void outASubtractionExpr(ASubtractionExpr e)
|
public void outASubtractionExpr(ASubtractionExpr e)
|
||||||
{
|
{
|
||||||
Double result =
|
double b = m_exprValues.pop();
|
||||||
m_exprValues.get(e.getExpr()) - m_exprValues.get(e.getTerm());
|
double a = m_exprValues.pop();
|
||||||
m_exprValues.put(e, new Double(result));
|
m_exprValues.push(a - b);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void outAMultiplicationTerm(AMultiplicationTerm e)
|
public void outAMultiplicationTerm(AMultiplicationTerm e)
|
||||||
{
|
{
|
||||||
Double result =
|
m_exprValues.push(m_exprValues.pop() * m_exprValues.pop());
|
||||||
m_exprValues.get(e.getTerm()) * m_exprValues.get(e.getFactor());
|
|
||||||
m_exprValues.put(e, new Double(result));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void outADivisionTerm(ADivisionTerm e)
|
public void outADivisionTerm(ADivisionTerm e)
|
||||||
{
|
{
|
||||||
Double result =
|
double b = m_exprValues.pop();
|
||||||
m_exprValues.get(e.getTerm()) / m_exprValues.get(e.getFactor());
|
double a = m_exprValues.pop();
|
||||||
m_exprValues.put(e, new Double(result));
|
m_exprValues.push(a / b);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void caseTNumber(TNumber n)
|
||||||
|
{
|
||||||
|
m_exprValues.push(Double.parseDouble(n.getText()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user