Skip to content

Commit

Permalink
Add toString() method to all classes in the AST.
Browse files Browse the repository at this point in the history
  • Loading branch information
grahamedgecombe committed Apr 12, 2013
1 parent 00e4f9a commit e9111ad
Show file tree
Hide file tree
Showing 19 changed files with 142 additions and 5 deletions.
5 changes: 5 additions & 0 deletions examples/squares.tb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
10 LET X = 1
20 PRINT X * X
30 LET X = X + 1
40 IF X > 10 THEN END
50 GOTO 20
20 changes: 18 additions & 2 deletions src/main/java/com/grahamedgecombe/tinybasic/TinyBasicCompiler.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,25 @@
package com.grahamedgecombe.tinybasic;

public final class TinyBasicCompiler {
import com.grahamedgecombe.tinybasic.ast.Program;
import com.grahamedgecombe.tinybasic.parser.Parser;
import com.grahamedgecombe.tinybasic.tokenizer.Tokenizer;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public static void main(String[] args) {
public final class TinyBasicCompiler {

public static void main(String[] args) throws IOException {
Path path = Paths.get(args[0]);
try (Tokenizer tokenizer = new Tokenizer(Files.newBufferedReader(path, StandardCharsets.UTF_8))) {
try (Parser parser = new Parser(tokenizer)) {
Program program = parser.parse();
System.out.println(program.toString().trim());
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,9 @@ public int hashCode() {
return Objects.hash(operator, leftExpression, rightExpression);
}

@Override
public String toString() {
return "(" + leftExpression + " " + operator + " " + rightExpression + ")";
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
package com.grahamedgecombe.tinybasic.ast;

public enum BinaryOperator {
PLUS, MINUS, MULT, DIV
PLUS('+'), MINUS('-'), MULT('*'), DIV('/');

private final char character;

private BinaryOperator(char character) {
this.character = character;
}

public String toString() {
return String.valueOf(character);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,9 @@ public int hashCode() {
return Objects.hash(type, target);
}

@Override
public String toString() {
return type + " " + target;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,9 @@ public int hashCode() {
return 0;
}

@Override
public String toString() {
return "END";
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,9 @@ public int hashCode() {
return Objects.hash(operator, leftExpression, rightExpression, statement);
}

@Override
public String toString() {
return "IF " + leftExpression + " " + operator + " " + rightExpression + " THEN " + statement;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,9 @@ public int hashCode() {
return value;
}

@Override
public String toString() {
return Integer.toString(value);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,9 @@ public int hashCode() {
return value.hashCode();
}

@Override
public String toString() {
return "\"" + value + "\"";
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,15 @@ public int hashCode() {
return names.hashCode();
}

@Override
public String toString() {
StringBuilder buf = new StringBuilder("INPUT ");
for (int i = 0; i < names.size(); i++) {
buf.append(names.get(i));
if (i != (names.size() - 1))
buf.append(", ");
}
return buf.toString();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,9 @@ public int hashCode() {
return Objects.hash(name, value);
}

@Override
public String toString() {
return "LET " + name + " = " + value;
}

}
5 changes: 5 additions & 0 deletions src/main/java/com/grahamedgecombe/tinybasic/ast/Line.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,9 @@ public int hashCode() {
return Objects.hash(number, statement);
}

@Override
public String toString() {
return number + " " + statement;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,15 @@ public int hashCode() {
return values.hashCode();
}

@Override
public String toString() {
StringBuilder buf = new StringBuilder("PRINT ");
for (int i = 0; i < values.size(); i++) {
buf.append(values.get(i));
if (i != (values.size() - 1))
buf.append(", ");
}
return buf.toString();
}

}
9 changes: 9 additions & 0 deletions src/main/java/com/grahamedgecombe/tinybasic/ast/Program.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,13 @@ public int hashCode() {
return lines.hashCode();
}

@Override
public String toString() {
StringBuilder buf = new StringBuilder();
for (Line line : lines) {
buf.append(line).append("\n");
}
return buf.toString();
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
package com.grahamedgecombe.tinybasic.ast;

public enum RelationalOperator {
EQ, NE, GT, GTE, LT, LTE
EQ("="), NE("<>"), GT(">"), GTE(">="), LT("<"), LTE("<=");

private final String string;

private RelationalOperator(String string) {
this.string = string;
}

public String toString() {
return string;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,9 @@ public int hashCode() {
return 0;
}

@Override
public String toString() {
return "RETURN";
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,9 @@ public int hashCode() {
return Objects.hash(operator, expression);
}

@Override
public String toString() {
return "(" + operator + expression + ")";
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
package com.grahamedgecombe.tinybasic.ast;

public enum UnaryOperator {
PLUS, MINUS
PLUS('+'), MINUS('-');

private final char character;

private UnaryOperator(char character) {
this.character = character;
}

public String toString() {
return String.valueOf(character);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,9 @@ public int hashCode() {
return name.hashCode();
}

@Override
public String toString() {
return name;
}

}

0 comments on commit e9111ad

Please sign in to comment.