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:
parent
7d4e578674
commit
d4c0c32b25
@ -3,7 +3,7 @@ Package jholtrop.gvsu.cs675.bottomup;
|
|||||||
|
|
||||||
Tokens
|
Tokens
|
||||||
blank = (' ' | 9 | 10 | 13);
|
blank = (' ' | 9 | 10 | 13);
|
||||||
number = ['0' .. '9']+;
|
number = ['0' .. '9']+ ('.' ['0' .. '9']+)?;
|
||||||
plus = '+';
|
plus = '+';
|
||||||
minus = '-';
|
minus = '-';
|
||||||
multiply = '*';
|
multiply = '*';
|
||||||
|
@ -6,6 +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;
|
||||||
|
|
||||||
public class Evaluator extends DepthFirstAdapter
|
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)
|
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)
|
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)
|
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)
|
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));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user