Compare commits

...

5 Commits

Author SHA1 Message Date
simonkellet 4341d02572 end of practical 2 2 years ago
simonkellet 32bd99d78b new lines to check if it is still working 2 years ago
simonkellet fab24be627 added success test for multi-line 2 years ago
simonkellet 6b7fdd3bb0 added multi-line comment support 2 years ago
simonkellet f69942533f works with error code 2 years ago
  1. 21
      Triangle.Compiler/src/main/java/triangle/abstractSyntaxTrees/commands/RepeatCommand.java
  2. 6
      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. 25
      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. 6
      Triangle.Compiler/src/test/java/triangle/syntacticAnalyser/TestScanner.java
  10. BIN
      build/libs/Triangle-Tools.jar
  11. 3
      programs/hi-newcomment2.tri
  12. 19
      tam

@ -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.IfCommand;
import triangle.abstractSyntaxTrees.commands.LetCommand;
import triangle.abstractSyntaxTrees.commands.RepeatCommand;
import triangle.abstractSyntaxTrees.commands.SequentialCommand;
import triangle.abstractSyntaxTrees.commands.WhileCommand;
public interface CommandVisitor<TArg, TResult> {
TResult visitAssignCommand(AssignCommand ast, TArg arg);
TResult visitCallCommand(CallCommand ast, TArg arg);
@ -23,5 +23,7 @@ public interface CommandVisitor<TArg, TResult> {
TResult visitSequentialCommand(SequentialCommand 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.IfCommand;
import triangle.abstractSyntaxTrees.commands.LetCommand;
import triangle.abstractSyntaxTrees.commands.RepeatCommand;
import triangle.abstractSyntaxTrees.commands.SequentialCommand;
import triangle.abstractSyntaxTrees.commands.WhileCommand;
import triangle.abstractSyntaxTrees.declarations.BinaryOperatorDeclaration;
@ -180,6 +181,11 @@ public final class Encoder implements ActualParameterVisitor<Frame, Integer>,
emitter.emit(OpCode.JUMPIF, Machine.trueRep, Register.CB, loopAddr);
return null;
}
@Override
public Void visitRepeatCommand(RepeatCommand ast, Frame frame) {
return null;
}
// Expressions
@Override

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

@ -37,6 +37,7 @@ import triangle.abstractSyntaxTrees.commands.Command;
import triangle.abstractSyntaxTrees.commands.EmptyCommand;
import triangle.abstractSyntaxTrees.commands.IfCommand;
import triangle.abstractSyntaxTrees.commands.LetCommand;
import triangle.abstractSyntaxTrees.commands.RepeatCommand;
import triangle.abstractSyntaxTrees.commands.SequentialCommand;
import triangle.abstractSyntaxTrees.commands.WhileCommand;
import triangle.abstractSyntaxTrees.declarations.ConstDeclaration;
@ -320,7 +321,7 @@ public class Parser {
}
break;
case Token.WHILE: {
case Token.WHILE: { //while expr. do command
acceptIt();
Expression eAST = parseExpression();
accept(Token.DO);
@ -330,6 +331,16 @@ public class Parser {
}
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.END:
case Token.ELSE:

@ -35,7 +35,7 @@ public final class Scanner {
private boolean isOperator(char 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() {
switch (currentChar) {
// comment
// ! comment
case '!': {
takeIt();
while ((currentChar != SourceFile.EOL) && (currentChar != SourceFile.EOT))
@ -84,6 +84,18 @@ public final class Scanner {
}
break;
// new type of comment, the multi-line $ comment
case '$':{
takeIt();
//check if it is not the comment char or EOT, skipping new lines (no EOL)
while((currentChar != '$') && (currentChar != SourceFile.EOT))
takeIt();
//if we reach another $, takeIt and move on, we've reached the end of the multi-line comment
if(currentChar == '$')
takeIt();
}
break;
// whitespace
case ' ':
case '\n':
@ -187,6 +199,13 @@ public final class Scanner {
while (isOperator(currentChar))
takeIt();
return Token.OPERATOR;
// TODO: Bar operator, week 3
case '|':
takeIt();
while(isOperator(currentChar))
takeIt();
return Token.OPERATOR;
case '\'':
takeIt();
@ -262,7 +281,7 @@ public final class Scanner {
currentlyScanningToken = false;
// skip any whitespace or comments
while (currentChar == '!' || currentChar == ' ' || currentChar == '\n' || currentChar == '\r'
|| currentChar == '\t' || currentChar == '#')
|| currentChar == '\t' || currentChar == '#' || currentChar == '$')
scanSeparator();
currentlyScanningToken = true;

@ -64,20 +64,20 @@ final class Token extends Object {
// 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,
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...
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...
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...
EOT = 33, ERROR = 34;
EOT = 35, ERROR = 36;
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",
"var", "while", ".", ":", ";", ",", ":=", "~", "(", ")", "[", "]", "{", "}", "", "<error>" };
"begin", "const", "do", "else", "end", "func", "if", "in", "let", "of", "proc", "record", "repeat", "then", "type",
"until", "var", "while", ".", ":", ";", ",", ":=", "~", "(", ")", "[", "]", "{", "}", "", "<error>" };
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.IfCommand;
import triangle.abstractSyntaxTrees.commands.LetCommand;
import triangle.abstractSyntaxTrees.commands.RepeatCommand;
import triangle.abstractSyntaxTrees.commands.SequentialCommand;
import triangle.abstractSyntaxTrees.commands.WhileCommand;
import triangle.abstractSyntaxTrees.declarations.BinaryOperatorDeclaration;
@ -158,6 +159,11 @@ public class LayoutVisitor implements ActualParameterVisitor<Void, DrawingTree>,
var d2 = ast.C.visit(this);
return layoutBinary("WhileCom.", d1, d2);
}
@Override
public DrawingTree visitRepeatCommand(RepeatCommand ast, Void obj) {
return null;
}
// Expressions
@Override

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

Binary file not shown.

@ -4,6 +4,9 @@
$
another new comment
another new comment
another new comment
another new comment
$
begin

19
tam

@ -14,11 +14,16 @@ if [ -z "$1" ]
file=$1
fi
printf "${GRN}Compiling file: $1.tri to $1.tam ...${NC}\n"
# Compile to tam
#java -cp build/libs/Triangle-Tools.jar triangle.Compiler programs/$1.tri -o=$1.tam &> /dev/null #Quiet output, not handy!
java -cp build/libs/Triangle-Tools.jar triangle.Compiler programs/$1.tri -o=$1.tam
printf "${GRN}[INFO] Compiling file: $1.tri to $1.tam ...${NC}\n"
printf "${GRN}Running file: $1.tam ...${NC}\n"
# Run the Program
java -cp build/libs/Triangle-Tools.jar triangle.abstractMachine.Interpreter $1.tam
# Compile to tam
if java -cp build/libs/Triangle-Tools.jar triangle.Compiler programs/$1.tri -o=$1.tam &> /dev/null #quiet
then
printf "${GRN}[INFO] Running file: $1.tam ...${NC}\n"
java -cp build/libs/Triangle-Tools.jar triangle.abstractMachine.Interpreter $1.tam
else
printf "${RED}[ERROR] Could not complie $1.tri ...${NC}"
err=$(java -cp build/libs/Triangle-Tools.jar triangle.Compiler programs/$1.tri -o=$1.tam)
printf "${RED}\n$err\n${NC}"
exit 1
fi