From e9111ad0bd2690a06f76071902c36d5ba1364f03 Mon Sep 17 00:00:00 2001 From: Graham Edgecombe Date: Fri, 12 Apr 2013 15:55:03 +0100 Subject: [PATCH] Add toString() method to all classes in the AST. --- examples/squares.tb | 5 +++++ .../tinybasic/TinyBasicCompiler.java | 20 +++++++++++++++++-- .../tinybasic/ast/BinaryExpression.java | 5 +++++ .../tinybasic/ast/BinaryOperator.java | 12 ++++++++++- .../tinybasic/ast/BranchStatement.java | 5 +++++ .../tinybasic/ast/EndStatement.java | 5 +++++ .../tinybasic/ast/IfStatement.java | 5 +++++ .../tinybasic/ast/ImmediateExpression.java | 5 +++++ .../tinybasic/ast/ImmediateString.java | 5 +++++ .../tinybasic/ast/InputStatement.java | 11 ++++++++++ .../tinybasic/ast/LetStatement.java | 5 +++++ .../grahamedgecombe/tinybasic/ast/Line.java | 5 +++++ .../tinybasic/ast/PrintStatement.java | 11 ++++++++++ .../tinybasic/ast/Program.java | 9 +++++++++ .../tinybasic/ast/RelationalOperator.java | 12 ++++++++++- .../tinybasic/ast/ReturnStatement.java | 5 +++++ .../tinybasic/ast/UnaryExpression.java | 5 +++++ .../tinybasic/ast/UnaryOperator.java | 12 ++++++++++- .../tinybasic/ast/VariableExpression.java | 5 +++++ 19 files changed, 142 insertions(+), 5 deletions(-) create mode 100644 examples/squares.tb diff --git a/examples/squares.tb b/examples/squares.tb new file mode 100644 index 0000000..7286d60 --- /dev/null +++ b/examples/squares.tb @@ -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 diff --git a/src/main/java/com/grahamedgecombe/tinybasic/TinyBasicCompiler.java b/src/main/java/com/grahamedgecombe/tinybasic/TinyBasicCompiler.java index 9cadf2a..5329e44 100644 --- a/src/main/java/com/grahamedgecombe/tinybasic/TinyBasicCompiler.java +++ b/src/main/java/com/grahamedgecombe/tinybasic/TinyBasicCompiler.java @@ -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()); + } + } } } diff --git a/src/main/java/com/grahamedgecombe/tinybasic/ast/BinaryExpression.java b/src/main/java/com/grahamedgecombe/tinybasic/ast/BinaryExpression.java index 3150040..393f619 100644 --- a/src/main/java/com/grahamedgecombe/tinybasic/ast/BinaryExpression.java +++ b/src/main/java/com/grahamedgecombe/tinybasic/ast/BinaryExpression.java @@ -44,4 +44,9 @@ public int hashCode() { return Objects.hash(operator, leftExpression, rightExpression); } + @Override + public String toString() { + return "(" + leftExpression + " " + operator + " " + rightExpression + ")"; + } + } diff --git a/src/main/java/com/grahamedgecombe/tinybasic/ast/BinaryOperator.java b/src/main/java/com/grahamedgecombe/tinybasic/ast/BinaryOperator.java index 6804ba8..005d9ae 100644 --- a/src/main/java/com/grahamedgecombe/tinybasic/ast/BinaryOperator.java +++ b/src/main/java/com/grahamedgecombe/tinybasic/ast/BinaryOperator.java @@ -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); + } } diff --git a/src/main/java/com/grahamedgecombe/tinybasic/ast/BranchStatement.java b/src/main/java/com/grahamedgecombe/tinybasic/ast/BranchStatement.java index edbea82..6ecedad 100644 --- a/src/main/java/com/grahamedgecombe/tinybasic/ast/BranchStatement.java +++ b/src/main/java/com/grahamedgecombe/tinybasic/ast/BranchStatement.java @@ -38,4 +38,9 @@ public int hashCode() { return Objects.hash(type, target); } + @Override + public String toString() { + return type + " " + target; + } + } diff --git a/src/main/java/com/grahamedgecombe/tinybasic/ast/EndStatement.java b/src/main/java/com/grahamedgecombe/tinybasic/ast/EndStatement.java index c88d80d..8d1be17 100644 --- a/src/main/java/com/grahamedgecombe/tinybasic/ast/EndStatement.java +++ b/src/main/java/com/grahamedgecombe/tinybasic/ast/EndStatement.java @@ -15,4 +15,9 @@ public int hashCode() { return 0; } + @Override + public String toString() { + return "END"; + } + } diff --git a/src/main/java/com/grahamedgecombe/tinybasic/ast/IfStatement.java b/src/main/java/com/grahamedgecombe/tinybasic/ast/IfStatement.java index 1873ae2..241ddb7 100644 --- a/src/main/java/com/grahamedgecombe/tinybasic/ast/IfStatement.java +++ b/src/main/java/com/grahamedgecombe/tinybasic/ast/IfStatement.java @@ -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; + } + } diff --git a/src/main/java/com/grahamedgecombe/tinybasic/ast/ImmediateExpression.java b/src/main/java/com/grahamedgecombe/tinybasic/ast/ImmediateExpression.java index 4b351fb..a55c9cb 100644 --- a/src/main/java/com/grahamedgecombe/tinybasic/ast/ImmediateExpression.java +++ b/src/main/java/com/grahamedgecombe/tinybasic/ast/ImmediateExpression.java @@ -29,4 +29,9 @@ public int hashCode() { return value; } + @Override + public String toString() { + return Integer.toString(value); + } + } diff --git a/src/main/java/com/grahamedgecombe/tinybasic/ast/ImmediateString.java b/src/main/java/com/grahamedgecombe/tinybasic/ast/ImmediateString.java index cf39d36..1e28e5a 100644 --- a/src/main/java/com/grahamedgecombe/tinybasic/ast/ImmediateString.java +++ b/src/main/java/com/grahamedgecombe/tinybasic/ast/ImmediateString.java @@ -29,4 +29,9 @@ public int hashCode() { return value.hashCode(); } + @Override + public String toString() { + return "\"" + value + "\""; + } + } diff --git a/src/main/java/com/grahamedgecombe/tinybasic/ast/InputStatement.java b/src/main/java/com/grahamedgecombe/tinybasic/ast/InputStatement.java index 5b962db..164ac3f 100644 --- a/src/main/java/com/grahamedgecombe/tinybasic/ast/InputStatement.java +++ b/src/main/java/com/grahamedgecombe/tinybasic/ast/InputStatement.java @@ -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(); + } + } diff --git a/src/main/java/com/grahamedgecombe/tinybasic/ast/LetStatement.java b/src/main/java/com/grahamedgecombe/tinybasic/ast/LetStatement.java index 8586844..b368277 100644 --- a/src/main/java/com/grahamedgecombe/tinybasic/ast/LetStatement.java +++ b/src/main/java/com/grahamedgecombe/tinybasic/ast/LetStatement.java @@ -38,4 +38,9 @@ public int hashCode() { return Objects.hash(name, value); } + @Override + public String toString() { + return "LET " + name + " = " + value; + } + } diff --git a/src/main/java/com/grahamedgecombe/tinybasic/ast/Line.java b/src/main/java/com/grahamedgecombe/tinybasic/ast/Line.java index 319530a..5683dd4 100644 --- a/src/main/java/com/grahamedgecombe/tinybasic/ast/Line.java +++ b/src/main/java/com/grahamedgecombe/tinybasic/ast/Line.java @@ -38,4 +38,9 @@ public int hashCode() { return Objects.hash(number, statement); } + @Override + public String toString() { + return number + " " + statement; + } + } diff --git a/src/main/java/com/grahamedgecombe/tinybasic/ast/PrintStatement.java b/src/main/java/com/grahamedgecombe/tinybasic/ast/PrintStatement.java index 114f01e..a7ee786 100644 --- a/src/main/java/com/grahamedgecombe/tinybasic/ast/PrintStatement.java +++ b/src/main/java/com/grahamedgecombe/tinybasic/ast/PrintStatement.java @@ -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(); + } + } diff --git a/src/main/java/com/grahamedgecombe/tinybasic/ast/Program.java b/src/main/java/com/grahamedgecombe/tinybasic/ast/Program.java index 1a50504..8a2a87e 100644 --- a/src/main/java/com/grahamedgecombe/tinybasic/ast/Program.java +++ b/src/main/java/com/grahamedgecombe/tinybasic/ast/Program.java @@ -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(); + } + } diff --git a/src/main/java/com/grahamedgecombe/tinybasic/ast/RelationalOperator.java b/src/main/java/com/grahamedgecombe/tinybasic/ast/RelationalOperator.java index 1b63dfb..7d39e77 100644 --- a/src/main/java/com/grahamedgecombe/tinybasic/ast/RelationalOperator.java +++ b/src/main/java/com/grahamedgecombe/tinybasic/ast/RelationalOperator.java @@ -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; + } } diff --git a/src/main/java/com/grahamedgecombe/tinybasic/ast/ReturnStatement.java b/src/main/java/com/grahamedgecombe/tinybasic/ast/ReturnStatement.java index 3779a89..b87cf0b 100644 --- a/src/main/java/com/grahamedgecombe/tinybasic/ast/ReturnStatement.java +++ b/src/main/java/com/grahamedgecombe/tinybasic/ast/ReturnStatement.java @@ -15,4 +15,9 @@ public int hashCode() { return 0; } + @Override + public String toString() { + return "RETURN"; + } + } diff --git a/src/main/java/com/grahamedgecombe/tinybasic/ast/UnaryExpression.java b/src/main/java/com/grahamedgecombe/tinybasic/ast/UnaryExpression.java index 0182595..e65e715 100644 --- a/src/main/java/com/grahamedgecombe/tinybasic/ast/UnaryExpression.java +++ b/src/main/java/com/grahamedgecombe/tinybasic/ast/UnaryExpression.java @@ -38,4 +38,9 @@ public int hashCode() { return Objects.hash(operator, expression); } + @Override + public String toString() { + return "(" + operator + expression + ")"; + } + } diff --git a/src/main/java/com/grahamedgecombe/tinybasic/ast/UnaryOperator.java b/src/main/java/com/grahamedgecombe/tinybasic/ast/UnaryOperator.java index 5fb370d..4753e8e 100644 --- a/src/main/java/com/grahamedgecombe/tinybasic/ast/UnaryOperator.java +++ b/src/main/java/com/grahamedgecombe/tinybasic/ast/UnaryOperator.java @@ -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); + } } diff --git a/src/main/java/com/grahamedgecombe/tinybasic/ast/VariableExpression.java b/src/main/java/com/grahamedgecombe/tinybasic/ast/VariableExpression.java index 9f03de0..9df1a4a 100644 --- a/src/main/java/com/grahamedgecombe/tinybasic/ast/VariableExpression.java +++ b/src/main/java/com/grahamedgecombe/tinybasic/ast/VariableExpression.java @@ -29,4 +29,9 @@ public int hashCode() { return name.hashCode(); } + @Override + public String toString() { + return name; + } + }