end of practical 2

java11
simonkellet 2 years ago
parent 32bd99d78b
commit 4341d02572
  1. 21
      Triangle.Compiler/src/main/java/triangle/abstractSyntaxTrees/commands/RepeatCommand.java
  2. 4
      Triangle.Compiler/src/main/java/triangle/abstractSyntaxTrees/visitors/CommandVisitor.java
  3. 6
      Triangle.Compiler/src/main/java/triangle/codeGenerator/Encoder.java
  4. 6
      Triangle.Compiler/src/main/java/triangle/contextualAnalyzer/Checker.java
  5. 13
      Triangle.Compiler/src/main/java/triangle/syntacticAnalyzer/Parser.java
  6. 11
      Triangle.Compiler/src/main/java/triangle/syntacticAnalyzer/Scanner.java
  7. 12
      Triangle.Compiler/src/main/java/triangle/syntacticAnalyzer/Token.java
  8. 6
      Triangle.Compiler/src/main/java/triangle/treeDrawer/LayoutVisitor.java
  9. 4
      Triangle.Compiler/src/test/java/triangle/syntacticAnalyser/TestScanner.java
  10. BIN
      build/libs/Triangle-Tools.jar

@ -0,0 +1,21 @@
package triangle.abstractSyntaxTrees.commands;
import triangle.abstractSyntaxTrees.expressions.Expression;
import triangle.abstractSyntaxTrees.visitors.CommandVisitor;
import triangle.syntacticAnalyzer.SourcePosition;
public class RepeatCommand extends Command {
public RepeatCommand(Expression eAST, Command cAST, SourcePosition position) {
super(position);
E = eAST;
C = cAST;
}
public <TArg, TResult> TResult visit(CommandVisitor<TArg, TResult> v, TArg arg) {
return v.visitRepeatCommand(this, arg);
}
public final Expression E;
public final Command C;
}

@ -5,11 +5,11 @@ import triangle.abstractSyntaxTrees.commands.CallCommand;
import triangle.abstractSyntaxTrees.commands.EmptyCommand; import triangle.abstractSyntaxTrees.commands.EmptyCommand;
import triangle.abstractSyntaxTrees.commands.IfCommand; import triangle.abstractSyntaxTrees.commands.IfCommand;
import triangle.abstractSyntaxTrees.commands.LetCommand; import triangle.abstractSyntaxTrees.commands.LetCommand;
import triangle.abstractSyntaxTrees.commands.RepeatCommand;
import triangle.abstractSyntaxTrees.commands.SequentialCommand; import triangle.abstractSyntaxTrees.commands.SequentialCommand;
import triangle.abstractSyntaxTrees.commands.WhileCommand; import triangle.abstractSyntaxTrees.commands.WhileCommand;
public interface CommandVisitor<TArg, TResult> { public interface CommandVisitor<TArg, TResult> {
TResult visitAssignCommand(AssignCommand ast, TArg arg); TResult visitAssignCommand(AssignCommand ast, TArg arg);
TResult visitCallCommand(CallCommand ast, TArg arg); TResult visitCallCommand(CallCommand ast, TArg arg);
@ -24,4 +24,6 @@ public interface CommandVisitor<TArg, TResult> {
TResult visitWhileCommand(WhileCommand ast, TArg arg); TResult visitWhileCommand(WhileCommand ast, TArg arg);
TResult visitRepeatCommand(RepeatCommand ast, TArg arg);
} }

@ -38,6 +38,7 @@ import triangle.abstractSyntaxTrees.commands.CallCommand;
import triangle.abstractSyntaxTrees.commands.EmptyCommand; import triangle.abstractSyntaxTrees.commands.EmptyCommand;
import triangle.abstractSyntaxTrees.commands.IfCommand; import triangle.abstractSyntaxTrees.commands.IfCommand;
import triangle.abstractSyntaxTrees.commands.LetCommand; import triangle.abstractSyntaxTrees.commands.LetCommand;
import triangle.abstractSyntaxTrees.commands.RepeatCommand;
import triangle.abstractSyntaxTrees.commands.SequentialCommand; import triangle.abstractSyntaxTrees.commands.SequentialCommand;
import triangle.abstractSyntaxTrees.commands.WhileCommand; import triangle.abstractSyntaxTrees.commands.WhileCommand;
import triangle.abstractSyntaxTrees.declarations.BinaryOperatorDeclaration; import triangle.abstractSyntaxTrees.declarations.BinaryOperatorDeclaration;
@ -181,6 +182,11 @@ public final class Encoder implements ActualParameterVisitor<Frame, Integer>,
return null; return null;
} }
@Override
public Void visitRepeatCommand(RepeatCommand ast, Frame frame) {
return null;
}
// Expressions // Expressions
@Override @Override
public Integer visitArrayExpression(ArrayExpression ast, Frame frame) { public Integer visitArrayExpression(ArrayExpression ast, Frame frame) {

@ -34,6 +34,7 @@ import triangle.abstractSyntaxTrees.commands.CallCommand;
import triangle.abstractSyntaxTrees.commands.EmptyCommand; import triangle.abstractSyntaxTrees.commands.EmptyCommand;
import triangle.abstractSyntaxTrees.commands.IfCommand; import triangle.abstractSyntaxTrees.commands.IfCommand;
import triangle.abstractSyntaxTrees.commands.LetCommand; import triangle.abstractSyntaxTrees.commands.LetCommand;
import triangle.abstractSyntaxTrees.commands.RepeatCommand;
import triangle.abstractSyntaxTrees.commands.SequentialCommand; import triangle.abstractSyntaxTrees.commands.SequentialCommand;
import triangle.abstractSyntaxTrees.commands.WhileCommand; import triangle.abstractSyntaxTrees.commands.WhileCommand;
import triangle.abstractSyntaxTrees.declarations.BinaryOperatorDeclaration; import triangle.abstractSyntaxTrees.declarations.BinaryOperatorDeclaration;
@ -185,6 +186,11 @@ public final class Checker implements ActualParameterVisitor<FormalParameter, Vo
return null; return null;
} }
@Override
public Void visitRepeatCommand(RepeatCommand ast, Void arg) {
return null;
}
// Expressions // Expressions
// Returns the TypeDenoter denoting the type of the expression. Does // Returns the TypeDenoter denoting the type of the expression. Does

@ -37,6 +37,7 @@ import triangle.abstractSyntaxTrees.commands.Command;
import triangle.abstractSyntaxTrees.commands.EmptyCommand; import triangle.abstractSyntaxTrees.commands.EmptyCommand;
import triangle.abstractSyntaxTrees.commands.IfCommand; import triangle.abstractSyntaxTrees.commands.IfCommand;
import triangle.abstractSyntaxTrees.commands.LetCommand; import triangle.abstractSyntaxTrees.commands.LetCommand;
import triangle.abstractSyntaxTrees.commands.RepeatCommand;
import triangle.abstractSyntaxTrees.commands.SequentialCommand; import triangle.abstractSyntaxTrees.commands.SequentialCommand;
import triangle.abstractSyntaxTrees.commands.WhileCommand; import triangle.abstractSyntaxTrees.commands.WhileCommand;
import triangle.abstractSyntaxTrees.declarations.ConstDeclaration; import triangle.abstractSyntaxTrees.declarations.ConstDeclaration;
@ -320,7 +321,7 @@ public class Parser {
} }
break; break;
case Token.WHILE: { case Token.WHILE: { //while expr. do command
acceptIt(); acceptIt();
Expression eAST = parseExpression(); Expression eAST = parseExpression();
accept(Token.DO); accept(Token.DO);
@ -330,6 +331,16 @@ public class Parser {
} }
break; break;
case Token.REPEAT: { //repeat command until expr.
acceptIt();
Command cAST = parseSingleCommand();
accept(Token.UNTIL); //check that there is a "until"
Expression eAST = parseExpression();
finish(commandPos);
commandAST = new RepeatCommand(eAST, cAST, commandPos);
}
break;
case Token.SEMICOLON: case Token.SEMICOLON:
case Token.END: case Token.END:
case Token.ELSE: case Token.ELSE:

@ -35,7 +35,7 @@ public final class Scanner {
private boolean isOperator(char c) { private boolean isOperator(char c) {
return (c == '+' || c == '-' || c == '*' || c == '/' || c == '=' || c == '<' || c == '>' || c == '\\' return (c == '+' || c == '-' || c == '*' || c == '/' || c == '=' || c == '<' || c == '>' || c == '\\'
|| c == '&' || c == '@' || c == '%' || c == '^' || c == '?'); || c == '&' || c == '@' || c == '%' || c == '^' || c == '?' || c == '|');
} }
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
@ -64,7 +64,7 @@ public final class Scanner {
private void scanSeparator() { private void scanSeparator() {
switch (currentChar) { switch (currentChar) {
// comment // ! comment
case '!': { case '!': {
takeIt(); takeIt();
while ((currentChar != SourceFile.EOL) && (currentChar != SourceFile.EOT)) while ((currentChar != SourceFile.EOL) && (currentChar != SourceFile.EOT))
@ -200,6 +200,13 @@ public final class Scanner {
takeIt(); takeIt();
return Token.OPERATOR; return Token.OPERATOR;
// TODO: Bar operator, week 3
case '|':
takeIt();
while(isOperator(currentChar))
takeIt();
return Token.OPERATOR;
case '\'': case '\'':
takeIt(); takeIt();
takeIt(); // the quoted character takeIt(); // the quoted character

@ -64,20 +64,20 @@ final class Token extends Object {
// reserved words - must be in alphabetical order... // reserved words - must be in alphabetical order...
ARRAY = 4, BEGIN = 5, CONST = 6, DO = 7, ELSE = 8, END = 9, FUNC = 10, IF = 11, IN = 12, LET = 13, OF = 14, ARRAY = 4, BEGIN = 5, CONST = 6, DO = 7, ELSE = 8, END = 9, FUNC = 10, IF = 11, IN = 12, LET = 13, OF = 14,
PROC = 15, RECORD = 16, THEN = 17, TYPE = 18, VAR = 19, WHILE = 20, PROC = 15, RECORD = 16, REPEAT = 17, THEN = 18, TYPE = 19, UNTIL = 20, VAR = 21, WHILE = 22,
// punctuation... // punctuation...
DOT = 21, COLON = 22, SEMICOLON = 23, COMMA = 24, BECOMES = 25, IS = 26, DOT = 23, COLON = 24, SEMICOLON = 25, COMMA = 26, BECOMES = 27, IS = 28,
// brackets... // brackets...
LPAREN = 27, RPAREN = 28, LBRACKET = 29, RBRACKET = 30, LCURLY = 31, RCURLY = 32, LPAREN = 29, RPAREN = 30, LBRACKET = 31, RBRACKET = 32, LCURLY = 33, RCURLY = 34,
// special tokens... // special tokens...
EOT = 33, ERROR = 34; EOT = 35, ERROR = 36;
private static String[] tokenTable = new String[] { "<int>", "<char>", "<identifier>", "<operator>", "array", private static String[] tokenTable = new String[] { "<int>", "<char>", "<identifier>", "<operator>", "array",
"begin", "const", "do", "else", "end", "func", "if", "in", "let", "of", "proc", "record", "then", "type", "begin", "const", "do", "else", "end", "func", "if", "in", "let", "of", "proc", "record", "repeat", "then", "type",
"var", "while", ".", ":", ";", ",", ":=", "~", "(", ")", "[", "]", "{", "}", "", "<error>" }; "until", "var", "while", ".", ":", ";", ",", ":=", "~", "(", ")", "[", "]", "{", "}", "", "<error>" };
private final static int firstReservedWord = Token.ARRAY, lastReservedWord = Token.WHILE; private final static int firstReservedWord = Token.ARRAY, lastReservedWord = Token.WHILE;

@ -33,6 +33,7 @@ import triangle.abstractSyntaxTrees.commands.CallCommand;
import triangle.abstractSyntaxTrees.commands.EmptyCommand; import triangle.abstractSyntaxTrees.commands.EmptyCommand;
import triangle.abstractSyntaxTrees.commands.IfCommand; import triangle.abstractSyntaxTrees.commands.IfCommand;
import triangle.abstractSyntaxTrees.commands.LetCommand; import triangle.abstractSyntaxTrees.commands.LetCommand;
import triangle.abstractSyntaxTrees.commands.RepeatCommand;
import triangle.abstractSyntaxTrees.commands.SequentialCommand; import triangle.abstractSyntaxTrees.commands.SequentialCommand;
import triangle.abstractSyntaxTrees.commands.WhileCommand; import triangle.abstractSyntaxTrees.commands.WhileCommand;
import triangle.abstractSyntaxTrees.declarations.BinaryOperatorDeclaration; import triangle.abstractSyntaxTrees.declarations.BinaryOperatorDeclaration;
@ -159,6 +160,11 @@ public class LayoutVisitor implements ActualParameterVisitor<Void, DrawingTree>,
return layoutBinary("WhileCom.", d1, d2); return layoutBinary("WhileCom.", d1, d2);
} }
@Override
public DrawingTree visitRepeatCommand(RepeatCommand ast, Void obj) {
return null;
}
// Expressions // Expressions
@Override @Override
public DrawingTree visitArrayExpression(ArrayExpression ast, Void obj) { public DrawingTree visitArrayExpression(ArrayExpression ast, Void obj) {

@ -34,13 +34,13 @@ public class TestScanner {
@Test @Test
public void testBarDemo() { public void testBarDemo() {
compileExpectFailure("/bardemo.tri"); compileExpectSuccess("/bardemo.tri"); //TODO: Change this to failure
} }
@Test @Test
public void testRepeatUntil() { public void testRepeatUntil() {
compileExpectFailure("/repeatuntil.tri"); compileExpectSuccess("/repeatuntil.tri");
} }
@Test @Test

Binary file not shown.