From 13efc3fd81e56057de3a66b26ea6bd97662fce6c Mon Sep 17 00:00:00 2001 From: Ernesto Estrada Date: Mon, 22 Oct 2018 12:40:13 -0600 Subject: [PATCH] Merge branch 'master' of /Users/ernestoestrada/IdeaProjects/PPL-Fall-2018 with conflicts. --- Corgi/Lexer.java | 3 +- Corgi/Node.java | 52 ++++++++---------- Corgi/Parser.java | 15 +++--- Corgi/partialNode.txt | 87 ++++++++++++++++++++++++++++++ Jive/{IntPair.java => IntPair.txt} | 0 Corgi/quadroots => quadroots | 0 6 files changed, 117 insertions(+), 40 deletions(-) mode change 100644 => 100755 Corgi/Node.java create mode 100755 Corgi/partialNode.txt rename Jive/{IntPair.java => IntPair.txt} (100%) rename Corgi/quadroots => quadroots (100%) diff --git a/Corgi/Lexer.java b/Corgi/Lexer.java index 94f4840..8cbd2de 100644 --- a/Corgi/Lexer.java +++ b/Corgi/Lexer.java @@ -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 diff --git a/Corgi/Node.java b/Corgi/Node.java old mode 100644 new mode 100755 index 63578aa..a154ea0 --- a/Corgi/Node.java +++ b/Corgi/Node.java @@ -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 diff --git a/Corgi/Parser.java b/Corgi/Parser.java index 73935d4..0967187 100644 --- a/Corgi/Parser.java +++ b/Corgi/Parser.java @@ -179,14 +179,13 @@ public class Parser { }// - // 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 ) { diff --git a/Corgi/partialNode.txt b/Corgi/partialNode.txt new file mode 100755 index 0000000..e40b782 --- /dev/null +++ b/Corgi/partialNode.txt @@ -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 diff --git a/Jive/IntPair.java b/Jive/IntPair.txt similarity index 100% rename from Jive/IntPair.java rename to Jive/IntPair.txt diff --git a/Corgi/quadroots b/quadroots similarity index 100% rename from Corgi/quadroots rename to quadroots