diff --git a/Corgi/Basic.java b/Corgi/Basic.java old mode 100755 new mode 100644 diff --git a/Corgi/Camera.java b/Corgi/Camera.java old mode 100755 new mode 100644 diff --git a/Corgi/Corgi.java b/Corgi/Corgi.java old mode 100755 new mode 100644 index d1dad41..b3317ec --- a/Corgi/Corgi.java +++ b/Corgi/Corgi.java @@ -15,10 +15,10 @@ public class Corgi { Node root = parser.parseProgram(); // display parse tree for debugging/testing: - TreeViewer viewer = new TreeViewer("Parse Tree", 0, 0, 800, 500, root ); +// TreeViewer viewer = new TreeViewer("Parse Tree", 0, 0, 800, 500, root ); // execute the parse tree -// root.execute(); + root.execute(); }// main diff --git a/Corgi/Lexer.java b/Corgi/Lexer.java old mode 100755 new mode 100644 diff --git a/Corgi/MemTable.java b/Corgi/MemTable.java old mode 100755 new mode 100644 diff --git a/Corgi/Node.java b/Corgi/Node.java old mode 100755 new mode 100644 index debe2be..63578aa --- a/Corgi/Node.java +++ b/Corgi/Node.java @@ -137,7 +137,12 @@ System.out.println("has " + number + " children"); public void execute() { if ( kind.equals("stmts") ) { - // insert code here for Exercise 15 + if ( first != null ) { + first.execute(); + if ( second != null ) { + second.execute(); + } + } } else if ( kind.equals("prtstr") ) { @@ -145,7 +150,8 @@ System.out.println("has " + number + " children"); } else if ( kind.equals("prtexp") ) { - // insert code here for Exercise 15 + double value = first.evaluate(); + System.out.print( value ); } else if ( kind.equals("nl") ) { @@ -153,7 +159,8 @@ System.out.println("has " + number + " children"); } else if ( kind.equals("sto") ) { - // insert code here for Exercise 15 + double value = first.evaluate(); + table.store( info, value ); } else { @@ -170,15 +177,25 @@ System.out.println("has " + number + " children"); } else if ( kind.equals("var") ) { - // insert code here for Exercise 15 + return table.retrieve( info ); } else if ( kind.equals("+") || kind.equals("-") ) { - // insert code here for Exercise 15 + double value1 = first.evaluate(); + double value2 = second.evaluate(); + if ( kind.equals("+") ) + return value1 + value2; + else + return value1 - value2; } else if ( kind.equals("*") || kind.equals("/") ) { - // insert code here for Exercise 15 + double value1 = first.evaluate(); + double value2 = second.evaluate(); + if ( kind.equals("*") ) + return value1 * value2; + else + return value1 / value2; } else if ( kind.equals("input") ) { @@ -206,11 +223,14 @@ System.out.println("has " + number + " children"); } else if ( kind.equals("pow") ) { - // insert code here for Exercise 15 + double value1 = first.evaluate(); + double value2 = second.evaluate(); + return Math.pow( value1, value2 ); } else if ( kind.equals("opp") ) { - // insert code here for Exercise 15 + double value = first.evaluate(); + return -value; } else { diff --git a/Corgi/Parser.java b/Corgi/Parser.java old mode 100755 new mode 100644 diff --git a/Corgi/Tests/factorial b/Corgi/Tests/factorial old mode 100755 new mode 100644 index e9a70bb..89ec686 --- a/Corgi/Tests/factorial +++ b/Corgi/Tests/factorial @@ -2,7 +2,7 @@ main() def fact( n ) - if lt(n 1) + if le(n, 1) return 1 else temp = fact( n-1 ) @@ -15,6 +15,7 @@ def main() "enter n: " n = input() - print( fact(n) ) + print(n) "! = " print(fact(n)) nl() +return 0 end diff --git a/Corgi/Tests/quadroots b/Corgi/Tests/quadroots old mode 100755 new mode 100644 index 66c1d32..c9f6969 --- a/Corgi/Tests/quadroots +++ b/Corgi/Tests/quadroots @@ -17,7 +17,9 @@ def main() x1 = (-b - sqrt(disc) ) / (2*a) x2 = (-b + sqrt(disc) ) / (2*a) - "x1: " print(x1) "\n" - "x2: " print(x2 ) "\n" + "x1: " print(x1) nl() + "x2: " print(x2 ) nl() + + return 0 end diff --git a/Corgi/Token.java b/Corgi/Token.java old mode 100755 new mode 100644 diff --git a/Corgi/TreeViewer.java b/Corgi/TreeViewer.java old mode 100755 new mode 100644 diff --git a/Corgi/corgiCFG.txt b/Corgi/corgiCFG.txt old mode 100755 new mode 100644 index 3fe78b1..1420de2 --- a/Corgi/corgiCFG.txt +++ b/Corgi/corgiCFG.txt @@ -2,18 +2,16 @@ Note: this is the draft grammar, with notes and some thoughts, for the Project 2 version of Corgi ------------------ - -> + -> | -> | - -> def ( ) end | - def ( ) end | - def ( ) end - def ( ) end + -> def ( ) end | + def ( ) end + def ( ) end | + def ( ) end - -> - - -> | + -> | , -> | @@ -21,7 +19,7 @@ Note: this is the draft grammar, with notes and some thoughts, -> ( ) | ( ) - -> | + -> | , -> | = | @@ -35,7 +33,7 @@ Note: this is the draft grammar, with notes and some thoughts, -> | + | - -> | * | / - -> | | + -> | | ( ) | - | @@ -59,8 +57,47 @@ Notes: change the Lexer to remove /* ..... */ type of comments---no tokens produced - change Lexer to allow \n inside a string---put \\ in the Java string +NO! change Lexer to allow \n inside a string---put \\ in the Java string? + This is mildly irritating, worth adding a bif: nl() +While doing Project 2 I noticed that it's easier if we require all +functions to return (saves handling difference between hitting "end" +at the end of function that didn't do a return, and hitting "end" +elsewhere). +So, if you want to, it's okay to assume that all functions return on all +execution paths (just do like [return 0] if the function doesn't really +feel like returning a number. -Need to agree on names for all the many BIF's---see upcoming document +Also, it's okay to require the initial funcCall to not be to a built-in +function (in my implementation, seems to be a problem (could probably be +easily fixed, but is a silly feature, anyway). +Here are the tentative official built-in functions: + + lt( , ) returns 1 if first arg is less than the second + 0 otherwise + le( , ) " less than or equal to " + eq( , ) " equal to + ne( , ) " not equal + + or( , ) returns 1 if either arg is non-zero, 0 otherwise + and( , ) returns 1 if both args are non-zero, 0 otherwise + + not( ) returns 1 if arg is zero, 0 otherwise + + all the original Corgi bifs (input(), sqrt, cos, sin, atan, + pow( , ) + + some new ones: + + print( ) just like in original Corgi, except say that if + the decimal part is 0, then don't display the .0 + (this is the only bif that is called as a statement) + NOTE: print should ideally NOT display the decimal part if + it is 0 --- i.e., want 37, not 37.0 + nl() prints a newline + (NOTE: unlike all other functions, funcCall to print and nl + are statements, don't return a value) + + round( ) returns the value rounded to nearest integer + trunc( ) returns the value with the decimal part set to 0 diff --git a/Corgi/oldCorgi.txt b/Corgi/oldCorgi.txt old mode 100755 new mode 100644 diff --git a/Corgi/partialNode.java b/Corgi/partialNode.java deleted file mode 100755 index e40b782..0000000 --- a/Corgi/partialNode.java +++ /dev/null @@ -1,87 +0,0 @@ - // 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/Corgi/quadroots b/Corgi/quadroots old mode 100755 new mode 100644 diff --git a/Documents/announcements b/Documents/announcements old mode 100755 new mode 100644 diff --git a/Documents/implementation.pdf b/Documents/implementation.pdf old mode 100755 new mode 100644 diff --git a/Documents/implementation.tex b/Documents/implementation.tex old mode 100755 new mode 100644 diff --git a/Documents/intro.pdf b/Documents/intro.pdf old mode 100755 new mode 100644 diff --git a/Documents/intro.tex b/Documents/intro.tex old mode 100755 new mode 100644 diff --git a/Documents/parser b/Documents/parser old mode 100755 new mode 100644 diff --git a/Documents/policies.pdf b/Documents/policies.pdf old mode 100755 new mode 100644 diff --git a/Documents/policies.tex b/Documents/policies.tex old mode 100755 new mode 100644 diff --git a/Documents/schedule.tex b/Documents/schedule.tex old mode 100755 new mode 100644 diff --git a/Jive/IntPair.java b/Jive/IntPair.java old mode 100755 new mode 100644 diff --git a/Jive/Jive.java b/Jive/Jive.java old mode 100755 new mode 100644 index 481e812..91e62df --- a/Jive/Jive.java +++ b/Jive/Jive.java @@ -1,7 +1,6 @@ -import java.io.File; -import java.io.PrintWriter; -import java.util.ArrayList; import java.util.Scanner; +import java.util.ArrayList; +import java.io.*; public class Jive { diff --git a/Jive/README b/Jive/README old mode 100755 new mode 100644 diff --git a/Jive/StringIntPair.java b/Jive/StringIntPair.java old mode 100755 new mode 100644 diff --git a/Jive/Tests/calls b/Jive/Tests/calls old mode 100755 new mode 100644 diff --git a/Jive/Tests/factorial b/Jive/Tests/factorial old mode 100755 new mode 100644 diff --git a/Jive/Tests/factorial.doc b/Jive/Tests/factorial.doc old mode 100755 new mode 100644 diff --git a/Jive/Tests/factorial.vpl b/Jive/Tests/factorial.vpl old mode 100755 new mode 100644 diff --git a/Jive/Tests/fizzbuzz b/Jive/Tests/fizzbuzz old mode 100755 new mode 100644 diff --git a/Jive/Tests/globs b/Jive/Tests/globs old mode 100755 new mode 100644 diff --git a/Jive/Tests/listio b/Jive/Tests/listio old mode 100755 new mode 100644 diff --git a/Jive/Tests/lits b/Jive/Tests/lits old mode 100755 new mode 100644 diff --git a/Jive/Tests/pastri b/Jive/Tests/pastri old mode 100755 new mode 100644 diff --git a/Jive/Tests/pastri2 b/Jive/Tests/pastri2 old mode 100755 new mode 100644 diff --git a/Jive/Tests/sillytest b/Jive/Tests/sillytest old mode 100755 new mode 100644 diff --git a/Jive/Tests/tiny b/Jive/Tests/tiny old mode 100755 new mode 100644 diff --git a/Jive/VPL.save b/Jive/VPL.save old mode 100755 new mode 100644 diff --git a/Jive/sept17 b/Jive/sept17 old mode 100755 new mode 100644 diff --git a/Jive/sept17.doc b/Jive/sept17.doc old mode 100755 new mode 100644 diff --git a/Jive/sept17.vpl b/Jive/sept17.vpl old mode 100755 new mode 100644 diff --git a/Project2/readme b/Project2/readme index 4dc95cc..67a6802 100644 --- a/Project2/readme +++ b/Project2/readme @@ -1,8 +1,8 @@ The starting materials for Project 2 are in the -Corgi folder, and in the Documents folder +Corgi folder, and in the Documents folder (page 51 of the file implementation.pdf). -The file corgiCFG.txt has the grammar, and +The file corgiCFG.txt has the grammar, and important comments, including the fact that Corgi now allows /* ..... */ style comments, and a list of the built-in functions. diff --git a/VPL/IntPair.java b/VPL/IntPair.java old mode 100755 new mode 100644 diff --git a/VPL/VPL.java b/VPL/VPL.java old mode 100755 new mode 100644 index ef985df..33e5c2c --- a/VPL/VPL.java +++ b/VPL/VPL.java @@ -1,7 +1,5 @@ -import java.io.File; -import java.util.ArrayList; -import java.util.Scanner; -import java.util.StringTokenizer; +import java.io.*; +import java.util.*; public class VPL { diff --git a/VPL/ex1a b/VPL/ex1a old mode 100755 new mode 100644 diff --git a/VPL/ex1b b/VPL/ex1b old mode 100755 new mode 100644 diff --git a/VPL/ex1c b/VPL/ex1c old mode 100755 new mode 100644 diff --git a/VPL/ex1d b/VPL/ex1d old mode 100755 new mode 100644 diff --git a/VPL/ex2 b/VPL/ex2 old mode 100755 new mode 100644 diff --git a/VPL/ex3 b/VPL/ex3 old mode 100755 new mode 100644 diff --git a/VPL/ex4 b/VPL/ex4 old mode 100755 new mode 100644 diff --git a/VPL/someUnitTests b/VPL/someUnitTests old mode 100755 new mode 100644