Skip to content
Snippets Groups Projects
Verified Commit 6368aeb0 authored by Emily Rowlands's avatar Emily Rowlands
Browse files

Added more statement code and docs.

parent e19fd818
No related branches found
No related tags found
No related merge requests found
......@@ -10,6 +10,7 @@ import jrr1g18.boring.nodes.UnaryOperationNode;
import jrr1g18.boring.nodes.ValueNode;
import jrr1g18.boring.nodes.statements.AssignmentStatementNode;
import jrr1g18.boring.nodes.statements.CompoundStatementNode;
import jrr1g18.boring.nodes.statements.EmptyStatementNode;
import jrr1g18.boring.nodes.statements.StatementNode;
import jrr1g18.boring.tokens.IdentifierToken;
import jrr1g18.boring.tokens.Token;
......@@ -168,16 +169,20 @@ public class Parser {
return result;
}
public StatementNode empty_statement() {
public StatementNode empty_statement() throws IllegalTokenException {
eat(TokenType.SEMICOLON);
return new EmptyStatementNode();
}
public IdentifierNode variable() {
public IdentifierNode variable() throws IllegalTokenException {
eat(TokenType.ID);
String name = ((IdentifierToken) m_currentToken).getName();
IdentifierNode node = new IdentifierNode(m_currentToken.getType(), name);
return new IdentifierNode(name);
}
public AssignmentStatementNode assignment_statement() throws IllegalTokenException {
public AssignmentStatementNode assignment_statement()
throws IllegalTokenException {
IdentifierNode left = variable();
eat(TokenType.ASSIGN);
ASTNode right = expr();
......@@ -195,7 +200,7 @@ public class Parser {
}
}
public CompoundStatementNode statement_list() {
public CompoundStatementNode statement_list() throws IllegalTokenException {
CompoundStatementNode node = new CompoundStatementNode();
do {
node.addChild(statement());
......
package jrr1g18.boring.nodes;
/**
* An abstract class that represents a node on the abstract syntax tree (AST).
*
* @author jrr1g18
*
*/
public abstract class ASTNode {
/**
* An enum denoting all types of node
*
* @author jrr1g18
*
*/
public enum NodeType {
INTEGER, ADD, SUB, MUL, DIV, UNARYPLUS, UNARYMINUS,
// ValueNode
INTEGER,
// BinaryOperationNode
ADD, SUB, MUL, DIV,
// UnaryOperationNode
UNARYPLUS, UNARYMINUS,
STATEMENT, COMPOUND_STATEMENT, ASSIGN_STATEMENT,
// StatementNode
STATEMENT,
// CompundStatementNode
COMPOUND_STATEMENT,
// AssignmentStatementNode
ASSIGN_STATEMENT,
// EmptyStatementNode
EMPTY_STATEMENT,
// IdentifierNode
IDENTIFIER,
}
private final NodeType m_type;
/**
* Checks if a node type is valid for this Node class.
*
* @param type The node type to check
*/
public abstract boolean isValidType(NodeType type);
/**
* Create a new ASTNode
*
* @param type The type of node to create
*/
public ASTNode(NodeType type) {
m_type = type;
}
/**
* @return The node type
*/
public NodeType getType() {
return m_type;
}
......
package jrr1g18.boring.nodes;
/**
* A class representing a binary operation on the AST.
*
* @author jrr1g18
*
*/
public class BinaryOperationNode extends ASTNode {
private ASTNode m_lhs;
private ASTNode m_rhs;
/**
* Create a new BinaryOperationNode
*
* @param lhs The left hand side
* @param type The operation type
* @param rhs The right hand side
*/
public BinaryOperationNode(ASTNode lhs, NodeType type, ASTNode rhs) {
super(type);
m_lhs = lhs;
m_rhs = rhs;
}
......@@ -17,4 +31,9 @@ public class BinaryOperationNode extends ASTNode {
public ASTNode getRHS() {
return m_rhs;
}
@Override
public boolean isValidType(NodeType type) {
}
}
......@@ -4,8 +4,8 @@ public class IdentifierNode extends ASTNode {
private String m_name;
public IdentifierNode(NodeType type, String name) {
super(type);
public IdentifierNode(String name) {
super(NodeType.IDENTIFIER);
m_name = name;
}
......
......@@ -3,6 +3,12 @@ package jrr1g18.boring.nodes.statements;
import jrr1g18.boring.nodes.ASTNode;
import jrr1g18.boring.nodes.IdentifierNode;
/**
* A statement class that represents an assignment.
*
* @author jrr1g18
*
*/
public class AssignmentStatementNode extends StatementNode {
private IdentifierNode m_lhs;
......@@ -22,7 +28,7 @@ public class AssignmentStatementNode extends StatementNode {
}
/**
* @param lhs the lhs to set
* @param lhs The new lhs
*/
public void setLhs(IdentifierNode lhs) {
this.m_lhs = lhs;
......@@ -36,7 +42,7 @@ public class AssignmentStatementNode extends StatementNode {
}
/**
* @param rhs the rhs to set
* @param rhs the new rhs
*/
public void setRhs(ASTNode rhs) {
this.m_rhs = rhs;
......
/**
*
*/
package jrr1g18.boring.nodes.statements;
/**
* Empty statement
*
* @author jrr1g18
*
*/
public class EmptyStatementNode extends StatementNode {
/**
* Create a new empty statement.
*/
public EmptyStatementNode() {
super(NodeType.EMPTY_STATEMENT);
}
@Override
public boolean isValidType(NodeType type) {
return super.isValidType(type);
}
}
package jrr1g18.boring.nodes.statements;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import jrr1g18.boring.nodes.ASTNode;
/**
* An abstract class for statement nodes to inherit from.
*
* @author jrr1g18
*
*/
public abstract class StatementNode extends ASTNode {
private static final List<NodeType> VALID_NODES;
static {
List<NodeType> nodes = new ArrayList<>();
nodes.add(NodeType.ASSIGN_STATEMENT);
nodes.add(NodeType.COMPOUND_STATEMENT);
nodes.add(NodeType.EMPTY_STATEMENT);
nodes.add(NodeType.STATEMENT);
VALID_NODES = Collections.unmodifiableList(nodes);
}
public StatementNode(NodeType type) {
super(type);
if(!(isValidType(type))) {
throw new IllegalArgumentException(
"Invalid node type: " + type.toString());
}
}
@Override
public boolean isValidType(NodeType type) {
return VALID_NODES.contains(type);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment