Initial Commit

This commit is contained in:
2018-10-15 12:47:14 -06:00
commit 0c531ffff9
55 changed files with 7285 additions and 0 deletions
+8
View File
@@ -0,0 +1,8 @@
public class IntPair
{
public int first, second;
public IntPair( int a, int b )
{ first = a; second = b; }
}
Executable
+948
View File
@@ -0,0 +1,948 @@
import java.io.File;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Scanner;
public class Jive {
private static Scanner keys = new Scanner( System.in );
private static Scanner input;
private static PrintWriter output;
private static PrintWriter output2;
// "global" info --- applies across entire program
// ------------------------------------------------
// holds the vpl code as it is produced
private static ArrayList<Integer> vpl = new ArrayList<Integer>();
// holds the name and starting index of all functions
private static ArrayList<StringIntPair> funcStart = new ArrayList<StringIntPair>();
// string at given index is name of function called at hole -index
private static ArrayList<String> callHoles = new ArrayList<String>();
// stores for entire program the start label assigned to each
// given function name
private static ArrayList<StringIntPair> callInfo = new ArrayList<StringIntPair>();
// little info for actual globals in VPL:
// ----------------------------------------
// holds all the global variables for entire program
private static ArrayList<String> globsList;
// "local" info --- used separately for each function
// --------------------------------------------------
// holds the local variables and literals temporarily for each function
private static ArrayList<String> locsList = new ArrayList<String>();
// once have a locsList for a function, store it in allLocs for output2
private static ArrayList<String> allLocs = new ArrayList<String>();
// holds the locations of the holes and the corresponding labels in current function
private static ArrayList<StringIntPair> labelHoles = new ArrayList<StringIntPair>();
// holds the label and corresponding index for all labels in current function
private static ArrayList<StringIntPair> labelInfo = new ArrayList<StringIntPair>();
public static void main(String[] args) throws Exception {
if (args.length != 1) {
System.out.println("Usage: java Jive <fileName>");
System.exit(1);
}
String fileName = args[0];
input = new Scanner( new File( fileName ) );
output = new PrintWriter( new File( fileName + ".vpl" ) );
output2 = new PrintWriter( new File( fileName + ".doc" ) );
// scan Jive file one word at a time
// (line breaks mean nothing more than a space)
// start anonymous "main" function
// since human readability is not a big issue, go ahead
// and treat the "main" function like all others, even
// though the label at the top is meaningless, since it
// can't be called
// (all other functions do these things when "Def" is encountered)
vpl.add( 1 );
vpl.add( nextLabel(true) ); // all functions start with a 1 command
// note index where command 4 will be inserted
// (can change if there's a Globs command)
funcStart.add( new StringIntPair( "?", 2 ) );
// note label for start of the function
callInfo.add( new StringIntPair( "?", currentLabel ) );
// make scratch cell (local cell 0) for scratch space for main
locsList.add( "-" );
int scratch = 0;
String rememberWord = "huh?"; // used a little to pass word to next state
callHoles.add("---"); // waste spot 0 so call holes -1, -2, -3, ..
// match nicely to positions in list
int state = 1;
while ( input.hasNext() ) {
String word = input.next();
System.out.println("--------------------->>>" +
"\nIn state " + state + " processing [" + word + "]" );
if ( state == 1 ) {
if ( word.equals("/*") ) {
state = 2;
}
else if ( word.equals("Halt") ) {
vpl.add( 26 );
state = 1;
}
else if ( word.equals("NL") ) {
vpl.add( 29 );
state = 1;
}
else if ( word.equals("Def") ) {// starting a function definition
// stuff to do when see Def, also when see end of input file
finishFunctionDef();
// initialize things for the upcoming function definition
locsList = new ArrayList<String>();
labelHoles = new ArrayList<StringIntPair>();
labelInfo = new ArrayList<StringIntPair>();
// move on to see the function name
state = 3;
}
else if ( isLabel( word ) ) {// process a label
int label = nextLabel(false);
labelInfo.add( new StringIntPair( word, label ) );
vpl.add( 1 );
vpl.add( label );
state = 1;
}
else if ( word.equals("Globs") ) {// global declarations section
// global declarations must be first command in program, if there at all
if ( vpl.size() == 2 ) {// have only put in the command 1 for "main"
// so still at start
vpl.add( 0, 32 );
globsList = new ArrayList<String>();
state = 6;
}
else {
error( "Globals declaration must occur at very beginning of a Jive program");
}
}
else if ( word.equals("Jmp") ) {
vpl.add( 7 );
state = 7;
}
else if ( findBIF2( word ) >= 0 ) {// is built-in function with 2 args
// cheaply get opcode corresponding to word
int op;
if ( word.equals("Get") ) {
op = 24;
}
else {
op = findBIF2(word) + 9;
}
vpl.add( op ); // the binary op
vpl.add( scratch ); // where to put the result
state = 8;
}
else if ( findBIF1( word ) >= 0 ) {// is built-in function with 1 arg
if ( word.equals( "Not" ) )
vpl.add( 20 );
else if ( word.equals( "Opp" ) )
vpl.add( 21 );
else if ( word.equals( "New" ) )
vpl.add( 31 );
else
error("[" + word + "] is not a valid one argument built-in function");
vpl.add( scratch );
state = 11;
}
else if ( word.equals("Keys") ) {// is built-in function with 0 args
vpl.add( 27 );
state = 13;
}
else if ( word.equals("Fet") ) {// have to look for built-in Fet before user-def
state = 16;
}
else if ( isFuncName( word ) ) {
rememberWord = word;
state = 14;
}
else if ( isVar( word ) ) {
// can put in entire part 1 code here
vpl.add( 23 );
vpl.add( scratch );
vpl.add( processVar( word, locsList ) );
state = 15;
}
}// state 1
else if ( state == 2 ) {
if ( word.equals("*/") ) {// reached end of comment
state = 1; // start a new command
}
else {// part of comment
// consume the word and stay in state 2
}
}
else if ( state == 3 ) {
if ( ! isFuncName( word ) ) {
error( "[" + word + "] is not a valid function name");
}
vpl.add( 1 );
vpl.add( nextLabel(true) ); // all functions start with a 1 command
// note index where command 4 will be inserted
funcStart.add( new StringIntPair( word, vpl.size() ) );
// note label for start of the function
callInfo.add( new StringIntPair( word, currentLabel ) );
state = 5;
}
// state 4 lost in change from f ( a b c ) to f a b c .
else if ( state == 5 ) {
if ( word.equals( "." ) ) {
// make scratch cell for this function after params cells
locsList.add( "-" );
scratch = locsList.size()-1;
state = 1; // go on to body of function
}
else if ( isParam( word ) ) {
// note another parameter
locsList.add( word );
// loop back to stay in state 5
}
}
else if ( state == 6 ) {
if ( word.equals( "." ) ) {
// done creating list of globals, generate VPL code for command 32
vpl.add( 1, globsList.size() );
// adjust start index of "main" to be 2 farther
funcStart.get( 0 ).x += 2;
state = 1;
}
else if ( isParam( word ) ) {
// add global
globsList.add( word );
// stay in state 6
}
else {
error("[" + word + "] is not a valid global variable name");
}
}
else if ( state == 7 ) {
if ( isLabel( word ) ) {
// add the hole
vpl.add( -1 );
labelHoles.add( new StringIntPair( word, vpl.size()-1 ) );
state = 1;
}
else {
error( "[" + word + "] is not a valid label");
}
}
else if ( state == 8 ) {
if ( isVar( word ) ) {
vpl.add( processVar( word, locsList ) ); // first arg
state = 9;
}
else {
error( "[" + word + "] is not a valid variable or literal");
}
}
else if ( state == 9 ) {
if ( isVar( word ) ) {
vpl.add( processVar( word, locsList ) ); // second arg
state = 10;
}
else {
error( "[" + word + "] is not a valid variable or literal");
}
}
else if ( state == 10 ) {
if ( word.equals("->") ) {
state = 100;
}
else {
error("a part 1 must be followed by ->");
}
}
else if ( state == 11 ) {
if ( isVar( word ) ) {
vpl.add( processVar( word, locsList ) );
state = 12;
}
else {
error( "[" + word + "] is not a valid variable or literal");
}
}
else if ( state == 12 ) {
if ( word.equals("->") ) {
state = 100;
}
else {
error("a part 1 must be followed by ->");
}
}
else if ( state == 13 ) {
if ( word.equals("->") ) {
vpl.add( scratch );
state = 100;
}
else {
error("a part 1 must be followed by ->");
}
}
else if ( state == 14 ) {
if ( isVar( word ) ) {
vpl.add( 3 );
vpl.add( processVar( word, locsList ) );
// state loops to 14
}
else if ( word.equals("->") ) {
// done with function call, send out the vpl code
vpl.add( 2 );
vpl.add( - nextCallHole() ); // leave a hole
callHoles.add( rememberWord );
// followed by command 6 for when return from doing called function
vpl.add( 6 );
vpl.add( scratch );
state = 100;
}
else {
error("a part 1 must be followed by ->");
}
}
else if ( state == 15 ) {
if ( word.equals("->") ) {
state = 100;
}
else {
error("a part 1 must be followed by ->");
}
}
else if ( state == 16 ) {
if ( isParam( word ) ) {
vpl.add( 34 );
vpl.add( scratch );
vpl.add( processVar( word, globsList ) );
state = 17;
}
else {
error("[" + word + "] is not a valid argument for Fet");
}
}
else if ( state == 17 ) {
if ( word.equals("->") ) {
state = 100;
}
else {
error("a part 1 must be followed by ->");
}
}
// end of part 1 states
// begin part 2 states
else if ( state == 100 ) {// start state for part 2
if ( word.equals(".") ) {
// do nothing with the value in scratch cell from part 1
state = 1;
}
else if ( word.equals("Prt") ) {
// generate code to print the value in the scratch cell
vpl.add( 28 );
vpl.add( scratch );
state = 1;
}
else if ( word.equals("Sym") ) {
// generate code to print the value in the scratch cell
vpl.add( 30 );
vpl.add( scratch );
state = 1;
}
else if ( word.equals("Ret") ) {
// generate code to return the value in scratch cell
vpl.add( 5 );
vpl.add( scratch );
state = 1;
}
else if ( isParam( word ) ) {
vpl.add( 23 );
vpl.add( processVar( word, locsList ) );
vpl.add( scratch );
state = 1;
}
else if ( word.equals("Jmp") ) {
state = 101;
}
else if ( word.equals("Put") ) {
state = 102;
}
else if ( word.equals("Sto") ) {
state = 104;
}
else {// unknown first word in a part 2
error("[" + word + "] is invalid as start of a part 2");
}
}// start state for part 2 (100)
else if ( state == 101 ) {
if ( isLabel( word ) ) {
vpl.add( 8 );
vpl.add( -1 ); // hole for target of jump
labelHoles.add( new StringIntPair( word, vpl.size()-1 ) );
vpl.add( scratch );
state = 1;
}
else {
error("[" + word + "] is not a valid label to use after conditional Jmp");
}
}
else if ( state == 102 ) {// processing first <var> for Put
if ( isVar( word ) ) {// have valid first argument
vpl.add( 25 );
vpl.add( processVar( word, locsList ) );
state = 103;
}
else {
error("[" + word + "] is not a valid first argument for Put");
}
}
else if ( state == 103 ) {// processing second <var> for Put
if ( isVar( word ) ) {// have valid second argument
vpl.add( processVar( word, locsList ) );
vpl.add( scratch );
state = 1;
}
else {
error("[" + word + "] is not a valid first argument for Put");
}
}
else if ( state == 104 ) {
if ( isParam( word ) ) {// valid argument for Sto
// generate VPL code 33 n a
vpl.add( 33 );
vpl.add( processVar( word, globsList ) );
vpl.add( scratch );
state = 1;
}
else {
error("[" + word + "] is not a valid argument for Sto");
}
}
}// loop to scan all words in Jive source file
// finish off last function definition
finishFunctionDef();
System.out.println("vpl before filling call holes:");
for ( int k=0; k<vpl.size(); k++) {
System.out.printf("%4d %6d\n", k, vpl.get(k) );
}
System.out.println();
// display the holes and info:
System.out.println("Call holes:");
for (int k=0; k<callHoles.size(); k++) {
System.out.println( callHoles.get(k) );
}
// scan vpl to line-oriented output file
// and to doc file
// and fill call holes
int funcNumber = 0;
int ip = 0;
while ( ip < vpl.size() ) {
int op = vpl.get( ip );
System.out.println("at ip: " + ip + ", sending op: " + op + " to vpl file");
if ( op==26 || op==29 ) {// operations with 0 arguments
output2.printf("[%5d] ", ip );
out( op + "\n" ); ip++;
}
else if ( op==4 ) {
System.out.println("function number is " + funcNumber);
output2.println("\n------------ " + funcStart.get(funcNumber).s + " -------------");
output2.print( allLocs.get( funcNumber ) ); funcNumber++;
output2.println();
output2.printf("[%5d] ", ip );
out( op + " " ); ip++;
out( vpl.get(ip) + "\n" ); ip++;
}
else if ( op== 2 ) {
// before send to files, replace call hole with actual label
String fName = callHoles.get( - vpl.get( ip+1 ) );
// now search callInfo for the label
int label = -1;
for (int k=0; k<callInfo.size() && label < 0; k++) {
if ( callInfo.get(k).s.equals( fName ) ) {
label = callInfo.get(k).x;
}
}
if ( label == -1 )
error("Could not find function [" + fName + "] in callInfo");
output2.printf("[%5d] ", ip );
out( op + " " ); ip++;
out( label + "\n" ); ip++;
}
else if ( op==1 || op==3 || op==5 || op==6 || op==7 ||
op==27 || op==28 || op==30 || op==32 ) {// ops with 1 arg
output2.printf("[%5d] ", ip );
out( op + " " ); ip++;
out( vpl.get(ip) + "\n" ); ip++;
}
else if ( op==8 || op==20 || op==21 || op==22 || op==23 ||
op==31 || op==33 || op==34 ) {// ops with 2 args
output2.printf("[%5d] ", ip );
out( op + " " ); ip++;
out( vpl.get(ip) + " " ); ip++;
out( vpl.get(ip) + "\n" ); ip++;
}
else if ( (9<=op && op<=19) || op==24 || op==25 ) {
output2.printf("[%5d] ", ip );
out( op + " " ); ip++;
out( vpl.get(ip) + " " ); ip++;
out( vpl.get(ip) + " " ); ip++;
out( vpl.get(ip) + "\n" ); ip++;
}
else {
output2.printf("[%5d] ", ip );
System.out.println( op + " is invalid op code, stop translation");
ip = vpl.size()+1; // just to stop it
//error("[" + op + "] is an invalid operation code");
}
}
output.close();
output2.close();
input.close();
}// main
// return whether w starts with lowercase,
// followed by 0 or more letters or digits
private static boolean isParam( String w ) {
if ( w.length() == 0 ) return false;
if ( ! ( 'a' <= w.charAt(0) && w.charAt(0) <= 'z' ) ) return false;
if ( w.length() ==1 ) return true;
for (int k=1; k<w.length(); k++) {
char x = w.charAt(k);
if ( !letter(x) && !digit(x) ) return false;
}
return true;
}
// return whether w starts with uppercase,
// followed by 0 or more letters or digits
private static boolean isFuncName( String w ) {
if ( w.length() == 0 ) return false;
if ( ! ( 'A' <= w.charAt(0) && w.charAt(0) <= 'Z' ) ) return false;
if ( w.length() == 1 ) return true;
for (int k=1; k<w.length(); k++) {
char x = w.charAt(k);
if ( !letter(x) && !digit(x) ) return false;
}
return true;
}
private static boolean isLabel( String w ) {
if ( ! w.endsWith( ":" ) ) {
return false;
}
else if ( w.length() < 2 || ! lowercase( w.charAt(0) ) ) {
return false;
}
else {
for (int k=1; k<w.length()-1; k++) {
char x = w.charAt(k);
if ( !letter(x) && !digit(x) ) {
return false;
}
}
}
return true;
}
private static boolean lowercase( char x ) {
return 'a'<=x && x<='z';
}
private static boolean uppercase( char x ) {
return 'A'<=x && x<='Z';
}
private static boolean letter( char x ) {
return lowercase(x) || uppercase(x);
}
private static boolean digit( char x ) {
return '0'<=x && x<='9';
}
private static String[] bifs2 = { "Add", "Sub", "Mult", "Quot", "Rem",
"Eq", "NotEq", "Less", "LessEq",
"And", "Or",
"Get" };
private static String[] bifs1 = { "Not", "Opp", "New" };
private static int findBIF2( String word ) {
int loc = -1;
for (int k=0; k<bifs2.length && loc < 0; k++) {
if ( word.equals( bifs2[k] ) ) {
loc = k;
}
}
return loc;
}
private static int findBIF1( String word ) {
int loc = -1;
for (int k=0; k<bifs1.length && loc < 0; k++) {
if ( word.equals( bifs1[k] ) ) {
loc = k;
}
}
return loc;
}
// return whether word is an int literal or
// a parameter
private static boolean isVar( String word ) {
return isParam(word) || isInt(word);
}
// given word which is a variable name or an int
// literal, search for it in list (which will be
// either locsList or globsList) and if found, just
// return its location, otherwise append to end,
// and if is an int literal, add to litsList the 22 command to
// create the literal, and return its location
private static int processVar( String word, ArrayList<String> list ) {
for (int k=0; k<list.size(); k++) {
if ( word.equals( list.get(k) ) ) {
// found word in the list
return k;
}
}
// if still here, word was not found, process it further
if ( isInt(word) ) {// is an int literal, not in list
list.add( word );
return list.size()-1;
}
else {// is a newly discovered variable
list.add( word );
return list.size()-1;
}
}// processVar
private static boolean isInt( String s ) {
boolean result;
try {
int x = Integer.parseInt( s );
result = true;
}
catch( Exception e ) {
result = false;
}
return result;
}
private static void showLocals() {
for (int k=0; k<locsList.size(); k++) {
System.out.printf("%4d %s\n", k, locsList.get(k) );
}
}
// find item in list with string matching w, and return
// its matching integer,
// or report error
private static int findString( String w, ArrayList<StringIntPair> list ) {
for (int k=0; k<list.size(); k++) {
if (list.get(k).s.equals(w) ) {
return list.get(k).x;
}
}
// if still here, didn't find
error("could not find info with string [" + w + "]");
return -1;
}
private static void out( String s ) {
output.print( s );
output2.print( s );
}
private static void error( String message ) {
System.out.println( message );
System.exit(1);
}
public static void main2(String[] args) {
String w = "A37";
System.out.println("func?" + isFuncName( w ) );
System.out.println("var?" + isVar( w ) );
System.out.println("param?" + isParam( w ) );
System.out.println("label?" + isLabel( w ) );
}
// do everything that needs to be done when realize
// we've reached the end of a function definition
private static void finishFunctionDef() {
System.out.println("Start finishFunctionDef:");
System.out.println(" vpl at start: " );
showVPL();
showTables();
// get name of function being finished, just for convenience
String funcName = funcStart.get( funcStart.size()-1).s;
// debug output
System.out.println("Local cells for function just finished, " + funcName + ":" );
showLocals();
// for output2, store locsList for this function
String s = "";
for (int k=0; k<locsList.size(); k++) {
s += k + ": " + locsList.get(k) + "\n";
}
allLocs.add( s );
// insert command 4 with correct count
// first find number of params by searching for scratch cell named "-"
int numParams = -1;
for (int k=0; k<locsList.size() && numParams < 0; k++) {
if ( locsList.get(k).equals("-") ) {
numParams = k;
}
}
if (numParams == -1) {
error("Something wrong in finishFunctionDef, no scratch cell found");
}
// get index in vpl for insertion of command 4
// (just 2 more than where the label for the function starts)
int start = funcStart.get( funcStart.size()-1 ).x;
// insert the command 4
vpl.add( start, 4 );
vpl.add( start+1, locsList.size()-numParams );
// insert the command 22's for each literal in locsList
int count = 2; // have to count the two cells used by command 4
int index = start + 2;
for (int k=0; k<locsList.size(); k++) {
if ( isInt( locsList.get(k) ) ) {// is a literal
vpl.add( index, 22 ); index++;
vpl.add( index, k ); index++;
vpl.add( index, Integer.parseInt( locsList.get(k) ) ); index++;
count += 3; // inserting 3 additional values for each int literal
}
}
//System.out.println("finishFunctionDef, vpl after insertions: " );
//showVPL();
// shift hole locations due to insertions of command 4 and command 22's
for (int k=0; k<labelHoles.size(); k++) {
StringIntPair pair = labelHoles.get(k);
pair.x += count;
}
System.out.println("after shifting locations for insertions, tables are: ");
showTables();
// fill in all label holes
for (int k=0; k<labelHoles.size(); k++) {
StringIntPair pair = labelHoles.get(k);
// find location of this label from info:
int loc = -1;
for (int j=0; j<labelInfo.size() && loc < 0; j++) {
if ( labelInfo.get(j).s.equals( pair.s ) ) {// found it
loc = labelInfo.get(j).x;
}
}
// important error check---Jive program might be missing the label
if ( loc == -1 )
error("couldn't find label [" + pair.s + "]");
System.out.println("filling label hole [" + pair.s + "," + pair.x + "] with " + loc );
vpl.set( pair.x, loc );
}
}// finishFunctionDef
private static void showVPL() {
for (int k=0; k<vpl.size(); k++) {
System.out.println(k + ": " + vpl.get(k) );
}
}
private static void showTables() {
System.out.println("Label holes: ");
for (int k=0; k<labelHoles.size(); k++) {
System.out.println(labelHoles.get(k));
}
System.out.println("Label info: ");
for (int k=0; k<labelInfo.size(); k++) {
System.out.println(labelInfo.get(k));
}
System.out.println("Call holes: ");
for (int k=0; k<callHoles.size(); k++) {
System.out.println(callHoles.get(k));
}
System.out.println("Function starts: ");
for (int k=0; k<funcStart.size(); k++) {
System.out.println(funcStart.get(k));
}
}
// label generating scheme:
// functions are numbered while being scanned
private static int functionNumber = 0;
private static int spread = 1000;
private static int currentLabel = 0;
// generate next label, incrementing by 1
// if not starting a function, otherwise
// starting with multiple of spread
private static int nextLabel( boolean startingFunction ) {
if ( startingFunction ) {
functionNumber++;
currentLabel = spread*functionNumber;
return currentLabel;
}
else {
currentLabel++;
return currentLabel;
}
}
// call hole stuff:
private static int currentCallHole = 0;
private static int nextCallHole() {
currentCallHole++;
return currentCallHole;
}
}// Jive
Executable
+67
View File
@@ -0,0 +1,67 @@
Have begin implementation of Jive, working on state 8.
Through Tuesday, 9/11 made good progress, but still
some problems.
JiveVPL.save can be copied to JiveVPL.class to
allow running my VPL interpreter (version that
takes the memory produced by Jive.java) on
a given VPL file produced by Jive.
As of Sunday, 9/16/2018, at noon, Jive is initially
implemented, testing on a few test cases
(may still have errors, of course, but is useable
for demonstrating the desired translation from
Jive to VPL)
All the tests in the folder Tests seem to be working.
To run my VPL simulator on a VPL program produced by
Jive, you need to compile IntPair.java and
then
copy VPL.save to VPL.class
(I'm hiding my VPL source code so Project 1 isn't ruined).
September 17:
in class we wrote a little Jive program, named "sept17",
that was supposed to ask the user for n and
then store in an array n followed by the first n
Fibonacci numbers (we left off the first 1, which was
fine for our purposes), and then
display that array.
When we translated the Jive program to VPL and then
ran the VPL program, the behavior was weird.
After class I found the following errors in the
Jive program (yay---not in Jive.java, my translator):
first, we forgot to put a Halt command in the
"main" at the end, so after doing whatever work
was in "main", it continued into the next function,
or something. At any rate, it was bad, and was
cured by putting in the Halt command.
second, nothing was displaying. I figured out
after a while that we had forgotten to store
the value n at offset 0 in the array. When I
put in the code:
size -> Put array 0,
the program behaved as desired, except at the end
it crashed. After some work, I realized that I
(with no one else to blame), should have done
n -> Put array 0
With that change, sept17 worked perfectly
(up to n==45---for larger n, integer
overflow produced nonsensical results at the
end the sequence).
The punchline is, we didn't find a problem with
Jive.java!
+20
View File
@@ -0,0 +1,20 @@
/* pair up a string and
an int,
used for holes and info
*/
public class StringIntPair {
public String s;
public int x;
public StringIntPair( String sIn, int xIn ) {
s = sIn;
x = xIn;
}
public String toString() {
return "[" + s + "," + x + "]";
}
}
+31
View File
@@ -0,0 +1,31 @@
/* do some function
definitions and calls
in various orders
*/
Keys -> x
F 2 x -> a
a -> Prt
NL
Halt
Def G a b c .
Mult a b -> x
Mult x c -> x
x -> Ret
Def F a b .
Add a 1 -> c
Sub b 1 -> d
G c d 5 -> r
r -> Ret
+21
View File
@@ -0,0 +1,21 @@
/* main function
for computing
factorial of
input value
*/
Keys -> n
Fact n -> f
f -> Prt NL
Halt
Def Fact n .
Less n 2 -> Jmp small:
Sub n 1 -> temp
Fact temp -> f
Mult n f -> Ret
small:
1 -> Ret
+44
View File
@@ -0,0 +1,44 @@
[ 0] 1 1000
------------ ? -------------
0: -
1: n
2: f
[ 2] 4 3
[ 4] 27 0
[ 6] 23 1 0
[ 9] 3 1
[ 11] 2 2000
[ 13] 6 0
[ 15] 23 2 0
[ 18] 23 0 2
[ 21] 28 0
[ 23] 29
[ 24] 26
[ 25] 1 2000
------------ Fact -------------
0: n
1: -
2: 2
3: 1
4: temp
5: f
[ 27] 4 5
[ 29] 22 2 2
[ 32] 22 3 1
[ 35] 16 1 0 2
[ 39] 8 2001 1
[ 42] 10 1 0 3
[ 46] 23 4 1
[ 49] 3 4
[ 51] 2 2000
[ 53] 6 1
[ 55] 23 5 1
[ 58] 11 1 0 5
[ 62] 5 1
[ 64] 1 2001
[ 66] 23 1 3
[ 69] 5 1
+29
View File
@@ -0,0 +1,29 @@
1 1000
4 3
27 0
23 1 0
3 1
2 2000
6 0
23 2 0
23 0 2
28 0
29
26
1 2000
4 5
22 2 2
22 3 1
16 1 0 2
8 2001 1
10 1 0 3
23 4 1
3 4
2 2000
6 1
23 5 1
11 1 0 5
5 1
1 2001
23 1 3
5 1
+27
View File
@@ -0,0 +1,27 @@
Keys -> x
top:
x -> Prt NL
Eq x 1 -> Jmp exit:
Rem x 2 -> r
Eq r 0 -> Jmp even:
Jmp odd:
even:
Quot x 2 -> x
Jmp top:
odd:
Mult 3 x -> x
Add x 1 -> x
Jmp top:
exit:
Halt
+15
View File
@@ -0,0 +1,15 @@
/* unit tests, sort of,
on global stuff
*/
Globs a b c .
17 -> Sto a
23 -> Sto b
41 -> Sto c
Fet a -> Prt NL
Fet b -> Prt NL
Fet c -> Prt NL
Halt
+51
View File
@@ -0,0 +1,51 @@
GetList -> a
PutList a -> .
Halt
/*--------------------------------------*/
Def GetList .
Keys -> size
Add size 1 -> objSize /* 1 extra cell for size */
New objSize -> list
size -> Put list 0 /* store size at index 0 */
1 -> index
top:
Less size index -> Jmp exit:
Keys -> input
input -> Put list index
Add index 1 -> index
Jmp top:
exit:
list -> Ret
/*--------------------------------------*/
Def PutList list .
0 -> index /* get size of list */
Get list index -> size
1 -> index
top:
Less size index -> Jmp exit:
Get list index -> value
value -> Prt 32 -> Sym
Add index 1 -> index
Jmp top:
exit:
0 -> Ret
+10
View File
@@ -0,0 +1,10 @@
ShowSyms -> .
Halt
Def ShowSyms .
65 -> Sym
33 -> Sym
97 -> Sym
NL
0 -> Ret
+22
View File
@@ -0,0 +1,22 @@
Keys -> row
Keys -> col
PasTri row col -> Prt
Halt
Def PasTri row col .
Eq col 0 -> temp1
Eq row col -> temp2
Or temp1 temp2 -> easy
easy -> Jmp easy:
Sub col 1 -> upLeft
Sub row 1 -> rowAbove
PasTri rowAbove upLeft -> res1
PasTri rowAbove col -> res2
Add res1 res2 -> Ret
easy:
1 -> Ret
+71
View File
@@ -0,0 +1,71 @@
Globs p rowSize .
Main -> .
/* ------------------------------------ */
Def Pastri2 row col .
GetArray2d row col -> temp
NotEq temp 0 -> Jmp easy:
Eq col 0 -> Jmp onEdge:
Eq col row -> Jmp onEdge:
Sub col 1 -> col1
Sub row 1 -> row1
Pastri2 row1 col1 -> temp1
Pastri2 row1 col -> temp2
Add temp1 temp2 -> result
PutArray2d row col result -> .
result -> Ret
onEdge:
PutArray2d row col 1 -> .
1 -> Ret
easy:
temp -> Ret
/* ------------------------------------ */
Def Main .
Keys -> row
Keys -> col
Add row 1 -> rowsize
Add col 1 -> colsize
Mult rowsize colsize -> n
New n -> Sto p
colsize -> Sto rowSize
Pastri2 row col -> Prt NL
Halt
/* ------------------------------------ */
Def GetArray2d r c .
Fet p -> array
Fet rowSize -> cols
Mult r cols -> index
Add c index -> index
Get array index -> result
result -> Ret
/* ------------------------------------ */
Def PutArray2d r c value .
Fet p -> array
Fet rowSize -> cols
Mult r cols -> index
Add c index -> index
value -> Put array index
0 -> Ret
+13
View File
@@ -0,0 +1,13 @@
Keys -> x
Keys -> y
Keys -> z
Add3 x y z -> Prt NL
Halt
Def Add3 a b c .
Add a b -> x
Add x c -> x
x -> Ret
+7
View File
@@ -0,0 +1,7 @@
Globs a b c .
17 -> Sto b
Fet b -> Prt
Halt
Executable
BIN
View File
Binary file not shown.
Executable
+77
View File
@@ -0,0 +1,77 @@
/* ask the user for n,
create in an array the
first n fibonacci numbers
(1,1,2,3,5,8,...)
total them up and display
*/
Keys -> n
Fib n -> array
PutList array -> .
/* forgot this on Sept. 17,
caused weird behavior,
running into the next function?!
*/
Halt
/* given n,
create a heap array with n+1 spaces,
store n in space 0,
compute fib numbers and store them in
spaces 1, 2, ... n
Return starting index of the array
*/
Def Fib n .
Add n 1 -> size
New size -> array
/* forgot this on Sept. 17---
have to store n at
offset 0
*/
n -> Put array 0
0 -> temp1
1 -> temp2
1 -> k
top:
Less n k -> Jmp exit:
Add temp1 temp2 -> temp
temp -> Put array k
temp2 -> temp1
Get array k -> temp2
Add k 1 -> k
Jmp top:
exit:
array -> Ret
/*--------------------------------------*/
Def PutList list .
0 -> index /* get size of list */
Get list index -> size
1 -> index
top:
Less size index -> Jmp exit:
Get list index -> value
value -> Prt 32 -> Sym
Add index 1 -> index
Jmp top:
exit:
0 -> Ret
+101
View File
@@ -0,0 +1,101 @@
[ 0] 1 1000
------------ ? -------------
0: -
1: n
2: array
[ 2] 4 3
[ 4] 27 0
[ 6] 23 1 0
[ 9] 3 1
[ 11] 2 2000
[ 13] 6 0
[ 15] 23 2 0
[ 18] 3 2
[ 20] 2 3000
[ 22] 6 0
[ 24] 26
[ 25] 1 2000
------------ Fib -------------
0: n
1: -
2: 1
3: size
4: array
5: 0
6: temp1
7: temp2
8: k
9: temp
[ 27] 4 9
[ 29] 22 2 1
[ 32] 22 5 0
[ 35] 9 1 0 2
[ 39] 23 3 1
[ 42] 31 1 3
[ 45] 23 4 1
[ 48] 23 1 0
[ 51] 25 4 5 1
[ 55] 23 1 5
[ 58] 23 6 1
[ 61] 23 1 2
[ 64] 23 7 1
[ 67] 23 1 2
[ 70] 23 8 1
[ 73] 1 2001
[ 75] 16 1 0 8
[ 79] 8 2002 1
[ 82] 9 1 6 7
[ 86] 23 9 1
[ 89] 23 1 9
[ 92] 25 4 8 1
[ 96] 23 1 7
[ 99] 23 6 1
[ 102] 24 1 4 8
[ 106] 23 7 1
[ 109] 9 1 8 2
[ 113] 23 8 1
[ 116] 7 2001
[ 118] 1 2002
[ 120] 23 1 4
[ 123] 5 1
[ 125] 1 3000
------------ PutList -------------
0: list
1: -
2: 0
3: index
4: size
5: 1
6: value
7: 32
[ 127] 4 7
[ 129] 22 2 0
[ 132] 22 5 1
[ 135] 22 7 32
[ 138] 23 1 2
[ 141] 23 3 1
[ 144] 24 1 0 3
[ 148] 23 4 1
[ 151] 23 1 5
[ 154] 23 3 1
[ 157] 1 3001
[ 159] 16 1 4 3
[ 163] 8 3002 1
[ 166] 24 1 0 3
[ 170] 23 6 1
[ 173] 23 1 6
[ 176] 28 1
[ 178] 23 1 7
[ 181] 30 1
[ 183] 9 1 3 5
[ 187] 23 3 1
[ 190] 7 3001
[ 192] 1 3002
[ 194] 23 1 2
[ 197] 5 1
+71
View File
@@ -0,0 +1,71 @@
1 1000
4 3
27 0
23 1 0
3 1
2 2000
6 0
23 2 0
3 2
2 3000
6 0
26
1 2000
4 9
22 2 1
22 5 0
9 1 0 2
23 3 1
31 1 3
23 4 1
23 1 0
25 4 5 1
23 1 5
23 6 1
23 1 2
23 7 1
23 1 2
23 8 1
1 2001
16 1 0 8
8 2002 1
9 1 6 7
23 9 1
23 1 9
25 4 8 1
23 1 7
23 6 1
24 1 4 8
23 7 1
9 1 8 2
23 8 1
7 2001
1 2002
23 1 4
5 1
1 3000
4 7
22 2 0
22 5 1
22 7 32
23 1 2
23 3 1
24 1 0 3
23 4 1
23 1 5
23 3 1
1 3001
16 1 4 3
8 3002 1
24 1 0 3
23 6 1
23 1 6
28 1
23 1 7
30 1
9 1 3 5
23 3 1
7 3001
1 3002
23 1 2
5 1