Merge branch 'master' of /Users/ernestoestrada/IdeaProjects/PPL-Fall-2018 with conflicts.

This commit is contained in:
Ernesto Estrada
2018-10-22 12:40:13 -06:00
parent 216d31ca59
commit 13efc3fd81
6 changed files with 117 additions and 40 deletions
+1 -2
View File
@@ -50,8 +50,7 @@ public class Lexer {
// System.out.println("current symbol: " + sym + " state = " + state );
if ( state == 1 ) {
if ( sym == 9 || sym == 10 || sym == 13 ||
sym == 32 ) {// whitespace
if ( sym == 9 || sym == 10 || sym == 13 || sym == 32 ) {// whitespace
state = 1;
}
else if ( 'a'<=sym && sym<='z' ) {// lowercase
Regular → Executable
+22 -30
View File
@@ -137,12 +137,7 @@ System.out.println("has " + number + " children");
public void execute() {
if ( kind.equals("stmts") ) {
if ( first != null ) {
first.execute();
if ( second != null ) {
second.execute();
}
}
// insert code here for Exercise 15
}
else if ( kind.equals("prtstr") ) {
@@ -150,8 +145,7 @@ System.out.println("has " + number + " children");
}
else if ( kind.equals("prtexp") ) {
double value = first.evaluate();
System.out.print( value );
// insert code here for Exercise 15
}
else if ( kind.equals("nl") ) {
@@ -159,8 +153,7 @@ System.out.println("has " + number + " children");
}
else if ( kind.equals("sto") ) {
double value = first.evaluate();
table.store( info, value );
// insert code here for Exercise 15
}
else {
@@ -177,25 +170,25 @@ System.out.println("has " + number + " children");
}
else if ( kind.equals("var") ) {
return table.retrieve( info );
return table.retrieve( info );
}
else if ( kind.equals("+") || kind.equals("-") ) {
double value1 = first.evaluate();
double value2 = second.evaluate();
if ( kind.equals("+") )
return value1 + value2;
else
return value1 - value2;
double val1 = first.evaluate();
double val2 = second.evaluate();
if (kind.equals("+"))
return val1 + val2;
else
return val1 - val2;
}
else if ( kind.equals("*") || kind.equals("/") ) {
double value1 = first.evaluate();
double value2 = second.evaluate();
if ( kind.equals("*") )
return value1 * value2;
else
return value1 / value2;
double val1 = first.evaluate();
double val2 = second.evaluate();
if (kind.equals("*"))
return val1 * val2;
else
return val1 / val2;
}
else if ( kind.equals("input") ) {
@@ -219,25 +212,24 @@ System.out.println("has " + number + " children");
error("unknown function name [" + kind + "]");
return 0;
}
}
else if ( kind.equals("pow") ) {
double value1 = first.evaluate();
double value2 = second.evaluate();
return Math.pow( value1, value2 );
double val1 = first.evaluate();
double val2 = second.evaluate();
return Math.pow(val1, val2);
}
else if ( kind.equals("opp") ) {
double value = first.evaluate();
return -value;
double val = first.evaluate();
return -val;
}
else {
error("Unknown node kind [" + kind + "]");
return 0;
}
}// evaluate
}// Node
+7 -8
View File
@@ -179,14 +179,13 @@ public class Parser {
}// <factor>
// check whether token is correct kind
private void errorCheck( Token token, String kind ) {
if( ! token.isKind( kind ) ) {
System.out.println("Error: expected " + token +
" to be of kind " + kind );
System.exit(1);
}
}
//private Node parseFuncDefs() {}
//private Node parseFuncDef() {}
//private Node parseParams() {}
//private Node parseArgs() {}
// check whether token is correct kind and details
private void errorCheck( Token token, String kind, String details ) {
+87
View File
@@ -0,0 +1,87 @@
// ask this node to execute itself
// (for nodes that don't return a value)
public void execute() {
if ( kind.equals("stmts") ) {
// insert code here for Exercise 15
}
else if ( kind.equals("prtstr") ) {
System.out.print( info );
}
else if ( kind.equals("prtexp") ) {
// insert code here for Exercise 15
}
else if ( kind.equals("nl") ) {
System.out.print( "\n" );
}
else if ( kind.equals("sto") ) {
// insert code here for Exercise 15
}
else {
error("Unknown kind of node [" + kind + "]");
}
}// execute
// compute and return value produced by this node
public double evaluate() {
if ( kind.equals("num") ) {
return Double.parseDouble( info );
}
else if ( kind.equals("var") ) {
// insert code here for Exercise 15
}
else if ( kind.equals("+") || kind.equals("-") ) {
// insert code here for Exercise 15
}
else if ( kind.equals("*") || kind.equals("/") ) {
// insert code here for Exercise 15
}
else if ( kind.equals("input") ) {
return keys.nextDouble();
}
else if ( kind.equals("sqrt") || kind.equals("cos") ||
kind.equals("sin") || kind.equals("atan")
) {
double value = first.evaluate();
if ( kind.equals("sqrt") )
return Math.sqrt(value);
else if ( kind.equals("cos") )
return Math.cos( Math.toRadians( value ) );
else if ( kind.equals("sin") )
return Math.sin( Math.toRadians( value ) );
else if ( kind.equals("atan") )
return Math.toDegrees( Math.atan( value ) );
else {
error("unknown function name [" + kind + "]");
return 0;
}
}
else if ( kind.equals("pow") ) {
// insert code here for Exercise 15
}
else if ( kind.equals("opp") ) {
// insert code here for Exercise 15
}
else {
error("Unknown node kind [" + kind + "]");
return 0;
}
}// evaluate
View File