Skip to content

Commit

Permalink
Add converstions toTree in ASTs
Browse files Browse the repository at this point in the history
  • Loading branch information
romildo committed Oct 10, 2016
1 parent 08d2091 commit 49e6533
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 4 deletions.
3 changes: 2 additions & 1 deletion src/main/java/absyn/AST.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package absyn;

import javaslang.render.ToTree;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;

public abstract class AST {
public abstract class AST implements ToTree<String> {

@Override
public String toString() {
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/absyn/ExpBinOp.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package absyn;

import javaslang.collection.Tree;

public class ExpBinOp extends Exp {

public enum Op {PLUS, MINUS, TIMES, DIV}
Expand All @@ -14,4 +16,10 @@ public ExpBinOp(Op op, Exp left, Exp right) {
this.right = right;
}

@Override
public Tree.Node<String> toTree() {
return Tree.of("ExpBinOp: " + op,
left.toTree(),
right.toTree());
}
}
6 changes: 6 additions & 0 deletions src/main/java/absyn/ExpNum.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package absyn;

import javaslang.collection.Tree;

public class ExpNum extends Exp {

public final Double value;
Expand All @@ -8,4 +10,8 @@ public ExpNum(Double value) {
this.value = value;
}

@Override
public Tree.Node<String> toTree() {
return Tree.of("ExpNum: " + value);
}
}
19 changes: 16 additions & 3 deletions src/main/java/main/Driver.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,14 @@
import java.util.ArrayList;
import java.util.List;

import absyn.Exp;
import com.beust.jcommander.JCommander;
import com.beust.jcommander.Parameter;
import com.beust.jcommander.ParameterException;
import java_cup.runtime.Symbol;
import javaslang.render.dot.DotFile;
import javaslang.render.text.Boxes;
import javaslang.render.text.PrettyPrinter;
import parse.SymbolConstants;
import parse.Lexer;
import parse.parser;
Expand Down Expand Up @@ -107,10 +111,19 @@ public static void lexicalAnalysis(Reader input) throws IOException {
}

public static void syntaxAnalysis(Reader input) throws Exception {
Lexer lexer = new Lexer(input);
parser parser = new parser(lexer);
Symbol result = parser.parse();
final Lexer lexer = new Lexer(input);
final parser parser = new parser(lexer);
final Symbol result = parser.parse();
final Exp parseTree = (Exp) result.value;
System.out.println(result.value);
System.out.println();
System.out.println("===Abstract syntax tree:===========");
System.out.println();
System.out.println(PrettyPrinter.pp(parseTree.toTree()));
System.out.println();
System.out.println(Boxes.box(parseTree.toTree()));
DotFile.write(parseTree.toTree(), "test1.dot");
System.out.println();
}

}

0 comments on commit 49e6533

Please sign in to comment.