Skip to content

Commit

Permalink
Add var declarations parser. Also, Happy 22nd Birthday to me!! #CSisLife
Browse files Browse the repository at this point in the history
  • Loading branch information
ShutoAraki committed Oct 18, 2018
1 parent 938943d commit 546ba45
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 16 deletions.
8 changes: 4 additions & 4 deletions Project3Java/src/csc426/ast/Block.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,22 @@
public class Block {

private ValDecls valDecls;
// private VarDecls varDecls;
private VarDecls varDecls;
// private FunDecls funDecls;
// private Stmt stmt;

// public Block(ValDecls valDecls, VarDecls varDecls, FunDecls funDecls, Stmt stmt) {
public Block(ValDecls valDecls) {
public Block(ValDecls valDecls, VarDecls varDecls) {
this.valDecls = valDecls;
// this.varDecls = varDecls;
this.varDecls = varDecls;
// this.funDecls = funDecls;
// this.stmt = stmt;
}

public void display(String indentation) {
System.out.println(indentation + "Block");
valDecls.display(indentation + " ");
// varDecls.display(indentation + " ");
varDecls.display(indentation + " ");
// funDecls.display(indentation + " ");
// stmt.display(indentation + " ");
}
Expand Down
18 changes: 18 additions & 0 deletions Project3Java/src/csc426/ast/Type.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,23 @@
package csc426.ast;

import csc426.syntax.TokenType;

public class Type {

private TokenType tokenType;

public Type(TokenType tokenType) {
this.tokenType = tokenType;
}

public String toString() {
if (tokenType == TokenType.INT)
return "Int";
else if (tokenType == TokenType.BOOL)
return "Bool";
else if (tokenType == TokenType.VOID)
return "Void";
return "";
}

}
11 changes: 11 additions & 0 deletions Project3Java/src/csc426/ast/VarDecl.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,15 @@

public class VarDecl {

private String id;
private Type type;

public VarDecl(String id, Type type) {
this.id = id;
this.type = type;
}

public void display(String indentation) {
System.out.println(indentation + "Var " + id + " : " + type);
}
}
15 changes: 12 additions & 3 deletions Project3Java/src/csc426/ast/VarDecls.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
package csc426.ast;

import java.util.List;

public class VarDecls {

private List<VarDecl> vars;

public void display(String indentation) {
// TODO Auto-generated method stub

public VarDecls(List<VarDecl> vars) {
this.vars = vars;
}

public void display(String indentation) {
for (VarDecl var : vars) {
var.display(indentation + " ");
}
}

}
61 changes: 52 additions & 9 deletions Project3Java/src/csc426/syntax/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,26 +60,25 @@ public Program parseProgram() {
}

/*
* <Block> --> <ValDecls> FIRST = VAL
* <Block> --> <ValDecls> <VarDecls> <FunDecls> <Stmt> FIRST = VAL, VAR, FUN, FIRST(Stmt)
*/
public Block parseBlock() {
ValDecls vs = parseValDecls();
return new Block(vs);
VarDecls rs = parseVarDecls();

return new Block(vs, rs);
}

/*
* <ValDecls> --> <ValDecl> <ValDecls> FIRST = VAL
* | ε FOLLOW = VAR
*/
public ValDecls parseValDecls() {
if (check(TokenType.VAL)) {
List<ValDecl> vdList = new ArrayList<ValDecl>();
while (check(TokenType.VAL)) {
vdList.add(parseValDecl());
}
return new ValDecls(vdList);
List<ValDecl> vdList = new ArrayList<ValDecl>();
while (check(TokenType.VAL)) {
vdList.add(parseValDecl());
}
return new ValDecls(new ArrayList<ValDecl>());
return new ValDecls(vdList);
}

/*
Expand Down Expand Up @@ -107,6 +106,50 @@ public int parseSign() {
}
return 1;
}

/*
* <VarDecls> --> <VarDecl> <VarDecls> FIRST = VAR
* | ε FOLLOW = FUN, FIRST(Stmt)
*/
public VarDecls parseVarDecls() {
List<VarDecl> vrList = new ArrayList<VarDecl>();
while (check(TokenType.VAR)) {
vrList.add(parseVarDecl());
}
return new VarDecls(vrList);
}

/*
* <VarDecl> --> var id : <Type> ; FIRST = VAR
*/
public VarDecl parseVarDecl() {
match(TokenType.VAR);
Token id = match(TokenType.ID);
match(TokenType.COLON);
Type type = parseType();
match(TokenType.SEMI);
return new VarDecl(id.lexeme, type);
}

// Better implementation? Too much redundant code
public Type parseType() {

if (check(TokenType.INT)) {
match(TokenType.INT);
return new Type(TokenType.INT);
} else if (check(TokenType.BOOL)) {
match(TokenType.BOOL);
return new Type(TokenType.BOOL);
} else if (check(TokenType.VOID)) {
match(TokenType.VOID);
return new Type(TokenType.VOID);
} else {
System.err.println("Invalid var type: Var type has to be INT, BOOL, or VOID");
System.exit(1);
}

return null; // Unreachable
}

private Scanner scanner;
private Token current;
Expand Down

0 comments on commit 546ba45

Please sign in to comment.