Updated with Shultz's new changes
This commit is contained in:
Executable → Regular
Executable → Regular
Executable → Regular
+2
-2
@@ -15,10 +15,10 @@ public class Corgi {
|
|||||||
Node root = parser.parseProgram();
|
Node root = parser.parseProgram();
|
||||||
|
|
||||||
// display parse tree for debugging/testing:
|
// 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
|
// execute the parse tree
|
||||||
// root.execute();
|
root.execute();
|
||||||
|
|
||||||
}// main
|
}// main
|
||||||
|
|
||||||
|
|||||||
Executable → Regular
Executable → Regular
Executable → Regular
+28
-8
@@ -137,7 +137,12 @@ System.out.println("has " + number + " children");
|
|||||||
public void execute() {
|
public void execute() {
|
||||||
|
|
||||||
if ( kind.equals("stmts") ) {
|
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") ) {
|
else if ( kind.equals("prtstr") ) {
|
||||||
@@ -145,7 +150,8 @@ System.out.println("has " + number + " children");
|
|||||||
}
|
}
|
||||||
|
|
||||||
else if ( kind.equals("prtexp") ) {
|
else if ( kind.equals("prtexp") ) {
|
||||||
// insert code here for Exercise 15
|
double value = first.evaluate();
|
||||||
|
System.out.print( value );
|
||||||
}
|
}
|
||||||
|
|
||||||
else if ( kind.equals("nl") ) {
|
else if ( kind.equals("nl") ) {
|
||||||
@@ -153,7 +159,8 @@ System.out.println("has " + number + " children");
|
|||||||
}
|
}
|
||||||
|
|
||||||
else if ( kind.equals("sto") ) {
|
else if ( kind.equals("sto") ) {
|
||||||
// insert code here for Exercise 15
|
double value = first.evaluate();
|
||||||
|
table.store( info, value );
|
||||||
}
|
}
|
||||||
|
|
||||||
else {
|
else {
|
||||||
@@ -170,15 +177,25 @@ System.out.println("has " + number + " children");
|
|||||||
}
|
}
|
||||||
|
|
||||||
else if ( kind.equals("var") ) {
|
else if ( kind.equals("var") ) {
|
||||||
// insert code here for Exercise 15
|
return table.retrieve( info );
|
||||||
}
|
}
|
||||||
|
|
||||||
else if ( kind.equals("+") || kind.equals("-") ) {
|
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("/") ) {
|
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") ) {
|
else if ( kind.equals("input") ) {
|
||||||
@@ -206,11 +223,14 @@ System.out.println("has " + number + " children");
|
|||||||
}
|
}
|
||||||
|
|
||||||
else if ( kind.equals("pow") ) {
|
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") ) {
|
else if ( kind.equals("opp") ) {
|
||||||
// insert code here for Exercise 15
|
double value = first.evaluate();
|
||||||
|
return -value;
|
||||||
}
|
}
|
||||||
|
|
||||||
else {
|
else {
|
||||||
|
|||||||
Executable → Regular
Executable → Regular
+3
-2
@@ -2,7 +2,7 @@ main()
|
|||||||
|
|
||||||
def fact( n )
|
def fact( n )
|
||||||
|
|
||||||
if lt(n 1)
|
if le(n, 1)
|
||||||
return 1
|
return 1
|
||||||
else
|
else
|
||||||
temp = fact( n-1 )
|
temp = fact( n-1 )
|
||||||
@@ -15,6 +15,7 @@ def main()
|
|||||||
|
|
||||||
"enter n: "
|
"enter n: "
|
||||||
n = input()
|
n = input()
|
||||||
print( fact(n) )
|
print(n) "! = " print(fact(n)) nl()
|
||||||
|
|
||||||
|
return 0
|
||||||
end
|
end
|
||||||
|
|||||||
Executable → Regular
+4
-2
@@ -17,7 +17,9 @@ def main()
|
|||||||
x1 = (-b - sqrt(disc) ) / (2*a)
|
x1 = (-b - sqrt(disc) ) / (2*a)
|
||||||
x2 = (-b + sqrt(disc) ) / (2*a)
|
x2 = (-b + sqrt(disc) ) / (2*a)
|
||||||
|
|
||||||
"x1: " print(x1) "\n"
|
"x1: " print(x1) nl()
|
||||||
"x2: " print(x2 ) "\n"
|
"x2: " print(x2 ) nl()
|
||||||
|
|
||||||
|
|
||||||
|
return 0
|
||||||
end
|
end
|
||||||
|
|||||||
Executable → Regular
Executable → Regular
Executable → Regular
+49
-12
@@ -2,18 +2,16 @@ Note: this is the draft grammar, with notes and some thoughts,
|
|||||||
for the Project 2 version of Corgi
|
for the Project 2 version of Corgi
|
||||||
------------------
|
------------------
|
||||||
|
|
||||||
<program> -> <funcCall> <funcDefs>
|
<program> -> <funcCall> | <funcCall> <funcDefs>
|
||||||
|
|
||||||
<funcDefs> -> <funcDef> | <funcDef> <funcDefs>
|
<funcDefs> -> <funcDef> | <funcDef> <funcDefs>
|
||||||
|
|
||||||
<funcDef> -> def <funcName> ( ) end |
|
<funcDef> -> def <var> ( ) end |
|
||||||
def <funcName> ( <params> ) end |
|
def <var> ( ) <statements> end
|
||||||
def <funcName> ( ) <statements> end
|
def <var> ( <params> ) end |
|
||||||
def <funcName> ( <params> ) <statements> end
|
def <var> ( <params> ) <statements> end
|
||||||
|
|
||||||
<funcName> -> <var>
|
<params> -> <var> | <var> , <params>
|
||||||
|
|
||||||
<params> -> <var> | <var> <params>
|
|
||||||
|
|
||||||
<statements> -> <statement> |
|
<statements> -> <statement> |
|
||||||
<statement> <statements>
|
<statement> <statements>
|
||||||
@@ -21,7 +19,7 @@ Note: this is the draft grammar, with notes and some thoughts,
|
|||||||
<funcCall> -> <var> ( ) |
|
<funcCall> -> <var> ( ) |
|
||||||
<var> ( <args> )
|
<var> ( <args> )
|
||||||
|
|
||||||
<args> -> <expr> | <expr> <args>
|
<args> -> <expr> | <expr> , <args>
|
||||||
|
|
||||||
<statement> -> <string> |
|
<statement> -> <string> |
|
||||||
<var> = <expr> |
|
<var> = <expr> |
|
||||||
@@ -35,7 +33,7 @@ Note: this is the draft grammar, with notes and some thoughts,
|
|||||||
<expr> -> <term> | <term> + <expr> | <term> - <expr>
|
<expr> -> <term> | <term> + <expr> | <term> - <expr>
|
||||||
<term> -> <factor> | <factor> * <term> | <factor> / <term>
|
<term> -> <factor> | <factor> * <term> | <factor> / <term>
|
||||||
|
|
||||||
<factor> -> <number> | <var> |
|
<factor> -> <num> | <var> |
|
||||||
( <expr> ) |
|
( <expr> ) |
|
||||||
- <factor> |
|
- <factor> |
|
||||||
<funcCall>
|
<funcCall>
|
||||||
@@ -59,8 +57,47 @@ Notes:
|
|||||||
change the Lexer to remove /* ..... */ type of comments---no tokens
|
change the Lexer to remove /* ..... */ type of comments---no tokens
|
||||||
produced
|
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( <expr>, <expr> )
|
||||||
|
|
||||||
|
some new ones:
|
||||||
|
|
||||||
|
print( <expr> ) 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( <expr> ) returns the value rounded to nearest integer
|
||||||
|
trunc( <expr> ) returns the value with the decimal part set to 0
|
||||||
|
|||||||
Executable → Regular
@@ -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
|
|
||||||
Executable → Regular
Executable → Regular
Executable → Regular
Executable → Regular
Executable → Regular
Executable → Regular
Executable → Regular
Executable → Regular
Executable → Regular
Executable → Regular
Executable → Regular
Executable → Regular
+2
-3
@@ -1,7 +1,6 @@
|
|||||||
import java.io.File;
|
|
||||||
import java.io.PrintWriter;
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.Scanner;
|
import java.util.Scanner;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.io.*;
|
||||||
|
|
||||||
public class Jive {
|
public class Jive {
|
||||||
|
|
||||||
|
|||||||
Executable → Regular
Executable → Regular
Executable → Regular
Executable → Regular
Executable → Regular
Executable → Regular
Executable → Regular
Executable → Regular
Executable → Regular
Executable → Regular
Executable → Regular
Executable → Regular
Executable → Regular
Executable → Regular
Executable → Regular
Executable → Regular
Executable → Regular
Executable → Regular
Executable → Regular
Executable → Regular
+2
-4
@@ -1,7 +1,5 @@
|
|||||||
import java.io.File;
|
import java.io.*;
|
||||||
import java.util.ArrayList;
|
import java.util.*;
|
||||||
import java.util.Scanner;
|
|
||||||
import java.util.StringTokenizer;
|
|
||||||
|
|
||||||
public class VPL
|
public class VPL
|
||||||
{
|
{
|
||||||
|
|||||||
Executable → Regular
Reference in New Issue
Block a user