From 42fe2bf27066813b8b33b85cb42208edec65f516 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C5=A1per=20Dobrovoljc?= Date: Wed, 5 Mar 2025 09:14:18 +0100 Subject: [PATCH] LexAn Symbols --- .idea/vcs.xml | 6 ++ prg/test.pins | 3 +- src/pins25/phase/LexAn.java | 130 +++++++++++++++++++++++++++++++++++- 3 files changed, 136 insertions(+), 3 deletions(-) create mode 100644 .idea/vcs.xml diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/prg/test.pins b/prg/test.pins index c8dcdaf..251f915 100644 --- a/prg/test.pins +++ b/prg/test.pins @@ -1,2 +1 @@ -'a' -'\n' \ No newline at end of file += , && || ! == != > < >= <= + - * / % ^ ( ) \ No newline at end of file diff --git a/src/pins25/phase/LexAn.java b/src/pins25/phase/LexAn.java index 297e320..a586c3a 100644 --- a/src/pins25/phase/LexAn.java +++ b/src/pins25/phase/LexAn.java @@ -123,10 +123,11 @@ public class LexAn implements AutoCloseable { * {@link peekToken} in {@link takeToken}. */ private void nextToken() { - while (buffChar == '\n') { + while (buffChar == ' ' || buffChar == '\n' || buffChar == '\t' || buffChar == '\r') { nextChar(); } + Report.Location start = currentLocation(); switch (buffChar) { case -1: // EOF buffToken = new Token(currentLocation(), Token.Symbol.EOF, null); @@ -139,6 +140,130 @@ public class LexAn implements AutoCloseable { case '"': stringConst(); return; + + case '=': + nextChar(); + if (buffChar == '=') { + buffToken = new Token( + new Report.Location(start, currentLocation()), + Token.Symbol.EQU, + null + ); + nextChar(); + return; + } + + buffToken = new Token(start, Token.Symbol.ASSIGN, null); + nextChar(); + return; + + case ',': + buffToken = new Token(currentLocation(), Token.Symbol.COMMA, null); + nextChar(); + return; + + case '&': + nextChar(); + if (buffChar != '&') { + throw new Report.Error(currentLocation(), "Invalid character '" + (char) buffChar + "'"); + } + buffToken = new Token(new Report.Location(start, currentLocation()), Token.Symbol.AND, null); + nextChar(); + return; + + case '|': + nextChar(); + if (buffChar != '|') { + throw new Report.Error(currentLocation(), "Invalid character '" + (char) buffChar + "'"); + } + buffToken = new Token(new Report.Location(start, currentLocation()), Token.Symbol.OR, null); + nextChar(); + return; + + case '!': + nextChar(); + if (buffChar == '=') { + buffToken = new Token( + new Report.Location(start, currentLocation()), + Token.Symbol.NEQ, + null + ); + nextChar(); + return; + } + + buffToken = new Token(start, Token.Symbol.NOT, null); + return; + + case '>': + nextChar(); + if (buffChar == '=') { + buffToken = new Token( + new Report.Location(start, currentLocation()), + Token.Symbol.GEQ, + null + ); + nextChar(); + return; + } + + buffToken = new Token(start, Token.Symbol.GTH, null); + return; + + case '<': + nextChar(); + if (buffChar == '=') { + buffToken = new Token( + new Report.Location(start, currentLocation()), + Token.Symbol.LEQ, + null + ); + nextChar(); + return; + } + + buffToken = new Token(start, Token.Symbol.LTH, null); + return; + + case '+': + buffToken = new Token(currentLocation(), Token.Symbol.ADD, null); + nextChar(); + return; + + case '-': + buffToken = new Token(currentLocation(), Token.Symbol.SUB, null); + nextChar(); + return; + + case '*': + buffToken = new Token(currentLocation(), Token.Symbol.MUL, null); + nextChar(); + return; + + case '/': + buffToken = new Token(currentLocation(), Token.Symbol.DIV, null); + nextChar(); + return; + + case '%': + buffToken = new Token(currentLocation(), Token.Symbol.MOD, null); + nextChar(); + return; + + case '^': + buffToken = new Token(currentLocation(), Token.Symbol.PTR, null); + nextChar(); + return; + + case '(': + buffToken = new Token(currentLocation(), Token.Symbol.LPAREN, null); + nextChar(); + return; + + case ')': + buffToken = new Token(currentLocation(), Token.Symbol.RPAREN, null); + nextChar(); + return; } if (buffChar >= '0' && buffChar <= '9') { @@ -238,6 +363,9 @@ public class LexAn implements AutoCloseable { nextChar(); while (buffChar != '"') { + if (buffChar == -1) { + throw new Report.Error(currentLocation(), "Unterminated string."); + } if (!isChar()) { throw new Report.Error(currentLocation(), "Invalid character '" + (char) buffChar + "'."); }