Skip to content

Commit

Permalink
No commit message
Browse files Browse the repository at this point in the history
  • Loading branch information
Jazzepi authored and Jazzepi committed Nov 17, 2009
1 parent 08f9801 commit b245e22
Show file tree
Hide file tree
Showing 26 changed files with 555 additions and 129 deletions.
32 changes: 30 additions & 2 deletions decision
Original file line number Diff line number Diff line change
@@ -1,5 +1,33 @@
MAIN
{
VAR x = 5 + 4;
selfDestruct(x);
WHILE(1==1)
{
VAR x = 3;
WHILE(x<5)
{
VAR x = x + 1;
selfDestruct(getMeTwice);
}
selfDestruct(az);

VAR x = x - 1;
VAR x = x - 1;
IF(x<4)
{
VAR x = x+5;
selfDestruct(a);
IF(x==8)
{
VAR x = 0-9;
VAR y = 0-10;
selfDestruct(b);
IF(x<y)
{
VAR x = 5+3;
selfDestruct(c);
}
}
}
selfDestruct(q);
}
}
23 changes: 23 additions & 0 deletions ifelse
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
MAIN
{
WHILE(1==1)
{

}
VAR x = 0;
WHILE(x!=10)
{
VAR x = x +1;
selfDestruct(seeMe10Times);
}

IFELSE(x==10)
{
VAR y = 5;

}
ELSE
{
VAR x = 5;
}
}
24 changes: 16 additions & 8 deletions src/game/DecisionEngine.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

import lexer.TextFile;
import lexer.Token.TokenType;

import java.util.*;

public class DecisionEngine {

private int MAXEXECUTIONCYCLE = 1000;
Expand Down Expand Up @@ -33,6 +33,7 @@ private void collectSubroutines(TextFile executionCode) {

if(temp.equals("SUBROUTINE"))
{
st.nextToken(); //Clear out space
temp = st.nextToken();

if (subroutineLocation.containsKey(temp))
Expand All @@ -51,14 +52,21 @@ else if(temp.equals("MAIN"))
}
}

public GameCommand getNextGameCommand()
public GameOperation getNextGameCommand()
{
int counter = 0;
boolean haveGameCommand = false;
GameCommand extractedGameCommand = null;
GameOperation extractedGameCommand = null;

while(!haveGameCommand && counter < MAXEXECUTIONCYCLE)
{
if(executionCode.isEndOfFile())
{
System.out.println("RUNTIME ERROR: Program halted!");
extractedGameCommand = new GameOperation("halt");
return extractedGameCommand;
}

String current = executionCode.getLine();
StringTokenizer st = new StringTokenizer(current," (),",true);
String token = st.nextToken();
Expand Down Expand Up @@ -117,7 +125,6 @@ else if(operator.equals("%"))
}

current = executionCode.getLine();

if(current.equals("RIGHTEXPRESSION"))
{
haveNotFinishedLeftEXP = false;
Expand Down Expand Up @@ -249,7 +256,7 @@ else if(token.equals("RETURN"))
st.nextToken(); //Gets a space
token = st.nextToken(); //Gets the variable to return
retValue = symbolTable.get(token); //Put the value from the variable into the return register
executionCode.setRow(returnLocation); //Return to the location where execution is suppose to begin
executionCode.setRow(returnLocation-1); //Return to the location where execution is suppose to begin
symbolTable.put(returnValueIntoThisVar, retValue); //Put the value that was calculated during the subroutine into the VAR <variable> following the CALL command
}
else if(token.startsWith("#"))
Expand Down Expand Up @@ -420,7 +427,7 @@ else if(TokenType.matchesToken(TokenType.GAMEFUNCTION,token))
}
else if(TokenType.matchesToken(TokenType.GAMEORDER, token))
{
extractedGameCommand = new GameCommand(token);
extractedGameCommand = new GameOperation(token);
boolean firstTimeThrough = true;
token = st.nextToken(); // (
token = st.nextToken(); // either ) or a parameter
Expand All @@ -443,10 +450,11 @@ else if(TokenType.matchesToken(TokenType.GAMEORDER, token))
}
else if(token.equals("JUMP"))
{
String jumpAmount = st.nextToken();

String jumpAmount = st.nextToken(); // Space
jumpAmount = st.nextToken(); //
executionCode.addToRow(Integer.parseInt(jumpAmount)- 1);
}

counter++;
}

Expand Down
8 changes: 4 additions & 4 deletions src/game/GameCommand.java → src/game/GameOperation.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

import java.util.ArrayList;

public class GameCommand {
public class GameOperation {

private String commandName;
private ArrayList<String> parameters = new ArrayList<String>();

public GameCommand(String commandName, ArrayList<String> parameters)
public GameOperation(String commandName, ArrayList<String> parameters)
{
this.commandName = commandName;
for(String element: parameters)
Expand All @@ -16,12 +16,12 @@ public GameCommand(String commandName, ArrayList<String> parameters)
}
}

public GameCommand(String commandName)
public GameOperation(String commandName)
{
this.commandName = commandName;
}

public GameCommand()
public GameOperation()
{

}
Expand Down
6 changes: 3 additions & 3 deletions src/game/Robot.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package game;

public class Robot {
Item leftHandItem, rightHandItem, backPackItem, armorItem, helmetItem, miscItem, miscItem2;
String name;
int playerDesignation;
private Item leftHandItem, rightHandItem, backPackItem, armorItem, helmetItem, miscItem, miscItem2;
private String name;
private int playerDesignation;

Robot()
{
Expand Down
34 changes: 26 additions & 8 deletions src/lexer/Block.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,22 @@

import lexer.Token.TokenType;

/**
* A block consisting of zero or more statements.
* @author Michael Pinnegar
*
*/
public class Block {

ArrayList<Statement> block = new ArrayList<Statement>();
/**
* An ordered collection of statements in a block.
*/
private ArrayList<Statement> block = new ArrayList<Statement>();

/**
* Adds the VMC representation of this block, statement by statement, to the compiled code.
* @param flag unfinished compiled code
*/
public void compile(TextFile flag) {

for(Statement element : block)
Expand All @@ -17,6 +29,10 @@ public void compile(TextFile flag) {

}

/**
* Prints out the elements of a block as they were taken in as source code.
* Useful for verifying that the intermediate tree structure has been built correctly.
*/
void print()
{
for(Statement element : block)
Expand All @@ -25,6 +41,10 @@ void print()
}
}

/**
* Class constructor that builds itself from the source code found in body.
* @param body source code
*/
Block(TextFile body)
{
Token current = body.getNonWSToken(true);
Expand Down Expand Up @@ -88,16 +108,14 @@ else if(current.getText().equals("RETURN"))
else if(current.getType() == TokenType.GAMEORDER)
{
current = body.getNonWSToken(false);

GameCommand gameCommandStatementToBeAdded = new GameCommand(body, current);
block.add(gameCommandStatementToBeAdded);
current = body.getNonWSToken(false);

if (current.getText().equals("("))
{
GameCommand gameCommandStatementToBeAdded = new GameCommand(body, current);
block.add(gameCommandStatementToBeAdded);
}
else
if(!current.getText().equals(";"))
{
System.out.println("ERROR: ( expected after GAMEORDER on line" + body.getReport() + ". Token " + current.getText() + " of type " + current.getType() + " found instead.");
System.out.println("ERROR: ; symbol expected after GAMEORDER while parsing GAMEORDER on line "+ body.getReport()+ ". Token " + current.getText() + " of type " + current.getType() + " found instead.");
}
}
else
Expand Down
42 changes: 37 additions & 5 deletions src/lexer/Expression.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,59 @@

import java.util.ArrayList;

/**
* An expression consisting of a single term, followed by zero or more ADDOP TERM pairs.
* @author Michael Pinnegar
* @see Term
*
*/
public class Expression {

/**
* List of terms. Goes [ADDOP->TERM] in pairs.
* List of {@link Term}. Goes [ADDOP->TERM] in pairs.
*/
ArrayList<Term> terms = new ArrayList<Term>();
private ArrayList<Term> terms = new ArrayList<Term>();

/**
* List of addops. Goes [ADDOP->TERM] in pairs.
* List of addops (+/-). Goes [ADDOP->TERM] in pairs.
*/
ArrayList<String> addops = new ArrayList<String>();
private ArrayList<String> addops = new ArrayList<String>();

/**
* Very first term. Has no addop pair.
*/
Term first = null;
private Term first = null;

/**
* Counter used to uniquely label the nodes of the tree representation of the source code. Node
* numbers are used in the linearization of the tree's expressions, where it is converted into
* source code.
*/
private static int counter = 0;

/**
* Adds one to the counter.
*/
public static void incrementCounter()
{
counter++;
}

/**
* Retrieves the value of the counter.
* @return counter value
*/
public static int getCounterVal()
{
return counter;
}

/**
* Adds the VMC representation of this expression to the compiled code.
* Returns the integer numbering of the expression's node in the tree structure.
* @param flag unfinished compiled code
* @return expression's node numbering
*/
public int compile(TextFile flag) {

Expression.incrementCounter();
Expand Down Expand Up @@ -60,6 +84,10 @@ public int compile(TextFile flag) {
return thisM;
}

/**
* Prints out the elements of this expression as they were taken in as source code.
* Useful for verifying that the intermediate tree structure has been built correctly.
*/
public void print() {
if(first != null)
{
Expand All @@ -79,6 +107,10 @@ public void print() {
}
}

/**
* This constructor builds an expression from the source code found in body.
* @param body source code
*/
public Expression(TextFile body) {

first = new Term(body); //Process the first term
Expand Down
Loading

0 comments on commit b245e22

Please sign in to comment.