was going to do it with HashMap, but i think i will switch to a Stack

git-svn-id: svn://anubis/gvsu@115 45c1a28c-8058-47b2-ae61-ca45b979098e
This commit is contained in:
josh 2008-06-01 20:02:31 +00:00
parent 7d4e578674
commit d4c0c32b25
2 changed files with 18 additions and 7 deletions

View File

@ -3,7 +3,7 @@ Package jholtrop.gvsu.cs675.bottomup;
Tokens
blank = (' ' | 9 | 10 | 13);
number = ['0' .. '9']+;
number = ['0' .. '9']+ ('.' ['0' .. '9']+)?;
plus = '+';
minus = '-';
multiply = '*';

View File

@ -6,6 +6,7 @@ import jholtrop.gvsu.cs675.bottomup.node.*;
import jholtrop.gvsu.cs675.bottomup.lexer.Lexer;
import jholtrop.gvsu.cs675.bottomup.parser.Parser;
import java.io.*;
import java.util.HashMap;
public class Evaluator extends DepthFirstAdapter
{
@ -27,28 +28,38 @@ public class Evaluator extends DepthFirstAdapter
}
}
public void caseTNumber(TNumber num)
private HashMap<Node, Double> m_exprValues = new HashMap<Node, Double>();
private double getNumValue(TNumber num)
{
System.out.print(num.getText() + " ");
return Double.parseDouble(num.getText());
}
public void outAAdditionExpr(AAdditionExpr e)
{
System.out.print("+ ");
Double result =
m_exprValues.get(e.getExpr()) + m_exprValues.get(e.getTerm());
m_exprValues.put(e, new Double(result));
}
public void outASubtractionExpr(ASubtractionExpr e)
{
System.out.print("- ");
Double result =
m_exprValues.get(e.getExpr()) - m_exprValues.get(e.getTerm());
m_exprValues.put(e, new Double(result));
}
public void outAMultiplicationTerm(AMultiplicationTerm e)
{
System.out.print("* ");
Double result =
m_exprValues.get(e.getTerm()) * m_exprValues.get(e.getFactor());
m_exprValues.put(e, new Double(result));
}
public void outADivisionTerm(ADivisionTerm e)
{
System.out.print("/ ");
Double result =
m_exprValues.get(e.getTerm()) / m_exprValues.get(e.getFactor());
m_exprValues.put(e, new Double(result));
}
}