Merge branch 'master' of /Users/ernestoestrada/IdeaProjects/PPL-Fall-2018 with conflicts.
This commit is contained in:
+1
-2
@@ -50,8 +50,7 @@ public class Lexer {
|
|||||||
// System.out.println("current symbol: " + sym + " state = " + state );
|
// System.out.println("current symbol: " + sym + " state = " + state );
|
||||||
|
|
||||||
if ( state == 1 ) {
|
if ( state == 1 ) {
|
||||||
if ( sym == 9 || sym == 10 || sym == 13 ||
|
if ( sym == 9 || sym == 10 || sym == 13 || sym == 32 ) {// whitespace
|
||||||
sym == 32 ) {// whitespace
|
|
||||||
state = 1;
|
state = 1;
|
||||||
}
|
}
|
||||||
else if ( 'a'<=sym && sym<='z' ) {// lowercase
|
else if ( 'a'<=sym && sym<='z' ) {// lowercase
|
||||||
|
|||||||
Regular → Executable
+22
-30
@@ -137,12 +137,7 @@ System.out.println("has " + number + " children");
|
|||||||
public void execute() {
|
public void execute() {
|
||||||
|
|
||||||
if ( kind.equals("stmts") ) {
|
if ( kind.equals("stmts") ) {
|
||||||
if ( first != null ) {
|
// insert code here for Exercise 15
|
||||||
first.execute();
|
|
||||||
if ( second != null ) {
|
|
||||||
second.execute();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
else if ( kind.equals("prtstr") ) {
|
else if ( kind.equals("prtstr") ) {
|
||||||
@@ -150,8 +145,7 @@ System.out.println("has " + number + " children");
|
|||||||
}
|
}
|
||||||
|
|
||||||
else if ( kind.equals("prtexp") ) {
|
else if ( kind.equals("prtexp") ) {
|
||||||
double value = first.evaluate();
|
// insert code here for Exercise 15
|
||||||
System.out.print( value );
|
|
||||||
}
|
}
|
||||||
|
|
||||||
else if ( kind.equals("nl") ) {
|
else if ( kind.equals("nl") ) {
|
||||||
@@ -159,8 +153,7 @@ System.out.println("has " + number + " children");
|
|||||||
}
|
}
|
||||||
|
|
||||||
else if ( kind.equals("sto") ) {
|
else if ( kind.equals("sto") ) {
|
||||||
double value = first.evaluate();
|
// insert code here for Exercise 15
|
||||||
table.store( info, value );
|
|
||||||
}
|
}
|
||||||
|
|
||||||
else {
|
else {
|
||||||
@@ -177,25 +170,25 @@ System.out.println("has " + number + " children");
|
|||||||
}
|
}
|
||||||
|
|
||||||
else if ( kind.equals("var") ) {
|
else if ( kind.equals("var") ) {
|
||||||
return table.retrieve( info );
|
return table.retrieve( info );
|
||||||
}
|
}
|
||||||
|
|
||||||
else if ( kind.equals("+") || kind.equals("-") ) {
|
else if ( kind.equals("+") || kind.equals("-") ) {
|
||||||
double value1 = first.evaluate();
|
double val1 = first.evaluate();
|
||||||
double value2 = second.evaluate();
|
double val2 = second.evaluate();
|
||||||
if ( kind.equals("+") )
|
if (kind.equals("+"))
|
||||||
return value1 + value2;
|
return val1 + val2;
|
||||||
else
|
else
|
||||||
return value1 - value2;
|
return val1 - val2;
|
||||||
}
|
}
|
||||||
|
|
||||||
else if ( kind.equals("*") || kind.equals("/") ) {
|
else if ( kind.equals("*") || kind.equals("/") ) {
|
||||||
double value1 = first.evaluate();
|
double val1 = first.evaluate();
|
||||||
double value2 = second.evaluate();
|
double val2 = second.evaluate();
|
||||||
if ( kind.equals("*") )
|
if (kind.equals("*"))
|
||||||
return value1 * value2;
|
return val1 * val2;
|
||||||
else
|
else
|
||||||
return value1 / value2;
|
return val1 / val2;
|
||||||
}
|
}
|
||||||
|
|
||||||
else if ( kind.equals("input") ) {
|
else if ( kind.equals("input") ) {
|
||||||
@@ -219,25 +212,24 @@ System.out.println("has " + number + " children");
|
|||||||
error("unknown function name [" + kind + "]");
|
error("unknown function name [" + kind + "]");
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
else if ( kind.equals("pow") ) {
|
else if ( kind.equals("pow") ) {
|
||||||
double value1 = first.evaluate();
|
double val1 = first.evaluate();
|
||||||
double value2 = second.evaluate();
|
double val2 = second.evaluate();
|
||||||
return Math.pow( value1, value2 );
|
return Math.pow(val1, val2);
|
||||||
}
|
}
|
||||||
|
|
||||||
else if ( kind.equals("opp") ) {
|
else if ( kind.equals("opp") ) {
|
||||||
double value = first.evaluate();
|
double val = first.evaluate();
|
||||||
return -value;
|
return -val;
|
||||||
}
|
}
|
||||||
|
|
||||||
else {
|
else {
|
||||||
error("Unknown node kind [" + kind + "]");
|
error("Unknown node kind [" + kind + "]");
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
}// evaluate
|
}// evaluate
|
||||||
|
|
||||||
}// Node
|
}// Node
|
||||||
|
|||||||
+7
-8
@@ -179,14 +179,13 @@ public class Parser {
|
|||||||
|
|
||||||
}// <factor>
|
}// <factor>
|
||||||
|
|
||||||
// check whether token is correct kind
|
//private Node parseFuncDefs() {}
|
||||||
private void errorCheck( Token token, String kind ) {
|
|
||||||
if( ! token.isKind( kind ) ) {
|
//private Node parseFuncDef() {}
|
||||||
System.out.println("Error: expected " + token +
|
|
||||||
" to be of kind " + kind );
|
//private Node parseParams() {}
|
||||||
System.exit(1);
|
|
||||||
}
|
//private Node parseArgs() {}
|
||||||
}
|
|
||||||
|
|
||||||
// check whether token is correct kind and details
|
// check whether token is correct kind and details
|
||||||
private void errorCheck( Token token, String kind, String details ) {
|
private void errorCheck( Token token, String kind, String details ) {
|
||||||
|
|||||||
Executable
+87
@@ -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
|
||||||
Reference in New Issue
Block a user