From bb3034d34381a6f3d39a7e5e175e976391436bf6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vanja=20Stojanovi=C4=87?= Date: Thu, 23 Mar 2023 23:55:16 +0100 Subject: [PATCH] Ast working 90% --- .DS_Store | Bin 6148 -> 8196 bytes .vscode/launch.json | 2 +- TestsRunner/.DS_Store | Bin 6148 -> 8196 bytes TestsRunner/tests/3-ast-popravljeno-v1.tst | 940 +++++++++++++++++++++ src/compiler/lexer/Lexer.java | 1 - src/compiler/parser/Parser.java | 233 ++--- test3.pns | 78 +- 7 files changed, 1136 insertions(+), 118 deletions(-) create mode 100644 TestsRunner/tests/3-ast-popravljeno-v1.tst diff --git a/.DS_Store b/.DS_Store index 40f0826263db23a4af922642d2736ece76655678..c9ff3e4b150121dba13789795aaf365a744a6d5d 100644 GIT binary patch delta 139 zcmZoMXmOBWU|?W$DortDU;r^WfEYvza8E20o2aMAD6lbLH}hr%jz7$c**Q2SHn1=X zZ02E+WuE+*O?+|yYZIG^j)IY;#pD~T_8=jivf!e;ocz3Wkg=P!c%CqGNpJ(%t{@{f b3vzsCp3E=eIoY3wgM$%b8^h*!o;l0_J+2;V delta 103 zcmZp1XfcprU|?W$DortDU=RQ@Ie-{Mvv5r;6q~50$jG-bU^g=(-)0^GS>}ml_ZG8r pa0oI36##(%H;`}zsohxkoq009j3-DZ0~5p)kiiU_<9X&V0{~^i5DNeR diff --git a/.vscode/launch.json b/.vscode/launch.json index 0b7777e..b64f9c9 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -18,7 +18,7 @@ "request": "launch", "mainClass": "App", "projectName": "PINSCompiler_aaffa8a2", - "args": "test ./ ./TestsRunner/tests/3-ast.tst -v" + "args": "test ./ TestsRunner/tests/3-ast-popravljeno-v1.tst -v" }, { "type": "java", diff --git a/TestsRunner/.DS_Store b/TestsRunner/.DS_Store index 492f0bffc5b73ea44d451659dc078901118fbef9..e4cd962cfecf9a5694cc68254641e48a7860139f 100644 GIT binary patch delta 370 zcmZoMXmOBWU|?W$DortDU;r^WfEYvza8E20o2aKKEDn+f@)`1zlXCKtKvJ6pImB2t zD{%BPFJ|Z9nApIg4pPL-ki(G4kmQ+zPzV%t1Y*7aV8FnznTI8uv7Up$mm!fMn<14U z4X7c9Ar(!_dSop?HYY<6gDcP=7oa{@20eych7?pCjIwAtm>G&0iWrj7RCPmCv4I=| zbV~wj5~CKHlAmY>u`-kZommVdizjEXHnJO==qMOjT1=kDV!wGiTL9z6hA<{UW}qe@ kkl+RquAqw0UqI5O8@`> delta 130 zcmZp1XfcprU|?W$DortDU=RQ@Ie-{Mvv5r;6q~50C@Kh&2Z= blabla & +nekaj <= blablabla | nekaj + nekaj < bla & nekaj - nekaj > nekajbla { where + fun drugaFunkcija(nekaj:integer):integer= + { + { if haha then { + if nekaj then 5+3-1-4+2 * true else { + while nekaj >= nekaj * (-5323223) + -44545 : { + for nekaj = 1, nekaj >= -9999999, nekajnekaj-1-1-1-1:nekaj + } + } + } + } + = celotenprogramtideluje69 { where + fun epicBubblesort(polje:arr[10]integer):void = + n == dolzinaPolja & + temp == 0 & { + for i=0,i polje[j] then + temp == polje[j-1] & + polje[j-1] == polje[j] & + polje[j] == temp & + blablabla #neki komentarji + } + } + } + } + } + } +#lololololol +!expected: +Defs [1:1-38:6] + TypeDef [1:1-1:17]: prvic + Atom [1:11-1:17]: STR + TypeDef [2:1-2:21]: drugic + Atom [2:14-2:21]: INT + TypeDef [3:1-3:23]: tretjic + TypeName [3:15-3:23]: besedilo + TypeDef [4:1-4:35]: cetrtic + Array [4:14-4:35] + [7] + Array [4:21-4:35] + [7] + Atom [4:28-4:35]: LOG + VarDef [5:1-5:31]: petic + Array [5:13-5:31] + [10] + Atom [5:24-5:31]: INT + FunDef [7:1-38:6]: prvaFunkcija + Parameter [7:19-7:31]: prvic + Atom [7:25-7:31]: STR + Parameter [7:33-7:47]: drugic + Atom [7:40-7:47]: INT + Parameter [7:49-7:71]: cetrtic + Array [7:57-7:71] + [7] + Atom [7:64-7:71]: LOG + Atom [7:75-7:82]: INT + Where [8:1-38:6] + Defs [11:5-37:11] + FunDef [11:5-37:11]: drugaFunkcija + Parameter [11:23-11:36]: nekaj + Atom [11:29-11:36]: INT + Atom [11:38-11:45]: INT + Binary [12:9-37:11]: ASSIGN + IfThenElse [13:13-20:15] + Name [13:18-13:22]: haha + IfThenElse [13:28-19:18] + Name [14:24-14:29]: nekaj + Binary [14:35-14:51]: ADD + Binary [14:35-14:42]: SUB + Binary [14:35-14:40]: SUB + Binary [14:35-14:38]: ADD + Literal [14:35-14:36]: INT(5) + Literal [14:37-14:38]: INT(3) + Literal [14:39-14:40]: INT(1) + Literal [14:41-14:42]: INT(4) + Binary [14:43-14:51]: MUL + Literal [14:43-14:44]: INT(2) + Literal [14:47-14:51]: LOG(true) + While [14:57-18:22] + Binary [15:31-15:67]: GEQ + Name [15:31-15:36]: nekaj + Binary [15:40-15:67]: ADD + Binary [15:40-15:58]: MUL + Name [15:40-15:45]: nekaj + Block [15:48-15:58] + Unary [15:49-15:57]: SUB + Literal [15:50-15:57]: INT(5323223) + Unary [15:61-15:67]: SUB + Literal [15:62-15:67]: INT(44545) + For [15:73-17:26] + Name [16:33-16:38]: nekaj + Literal [16:41-16:42]: INT(1) + Binary [16:44-16:61]: GEQ + Name [16:44-16:49]: nekaj + Unary [16:53-16:61]: SUB + Literal [16:54-16:61]: INT(9999999) + Binary [16:63-16:81]: SUB + Binary [16:63-16:79]: SUB + Binary [16:63-16:77]: SUB + Binary [16:63-16:75]: SUB + Name [16:63-16:73]: nekajnekaj + Literal [16:74-16:75]: INT(1) + Literal [16:76-16:77]: INT(1) + Literal [16:78-16:79]: INT(1) + Literal [16:80-16:81]: INT(1) + Name [16:82-16:87]: nekaj + Where [21:11-36:14] + Defs [22:13-35:18] + FunDef [22:13-35:18]: epicBubblesort + Parameter [22:32-22:52]: polje + Array [22:38-22:52] + [10] + Atom [22:45-22:52]: INT + TypeName [22:54-22:58]: void + Binary [23:17-35:18]: AND + Binary [23:17-24:26]: AND + Binary [23:17-23:34]: EQ + Name [23:17-23:18]: n + Name [23:22-23:34]: dolzinaPolja + Binary [24:17-24:26]: EQ + Name [24:17-24:21]: temp + Literal [24:25-24:26]: INT(0) + For [24:29-35:18] + Name [25:21-25:22]: i + Literal [25:23-25:24]: INT(0) + Binary [25:25-25:28]: LT + Name [25:25-25:26]: i + Name [25:27-25:28]: n + Binary [25:29-25:34]: ADD + Name [25:29-25:30]: i + Literal [25:33-25:34]: INT(1) + For [26:21-34:22] + Name [26:27-26:28]: j + Literal [26:29-26:30]: INT(0) + Binary [26:32-26:39]: LT + Name [26:32-26:33]: j + Block [26:34-26:39] + Binary [26:35-26:38]: SUB + Name [26:35-26:36]: n + Name [26:37-26:38]: i + Binary [26:40-26:45]: ADD + Name [26:40-26:41]: j + Literal [26:44-26:45]: INT(1) + IfThenElse [27:25-33:26] + Binary [28:32-28:53]: GT + Binary [28:32-28:42]: ARR + Name [28:32-28:37]: polje + Binary [28:38-28:41]: SUB + Name [28:38-28:39]: j + Literal [28:40-28:41]: INT(1) + Binary [28:45-28:53]: ARR + Name [28:45-28:50]: polje + Name [28:51-28:52]: j + Binary [29:33-32:42]: AND + Binary [29:33-31:49]: AND + Binary [29:33-30:55]: AND + Binary [29:33-29:51]: EQ + Name [29:33-29:37]: temp + Binary [29:41-29:51]: ARR + Name [29:41-29:46]: polje + Binary [29:47-29:50]: SUB + Name [29:47-29:48]: j + Literal [29:49-29:50]: INT(1) + Binary [30:33-30:55]: EQ + Binary [30:33-30:43]: ARR + Name [30:33-30:38]: polje + Binary [30:39-30:42]: SUB + Name [30:39-30:40]: j + Literal [30:41-30:42]: INT(1) + Binary [30:47-30:55]: ARR + Name [30:47-30:52]: polje + Name [30:53-30:54]: j + Binary [31:33-31:49]: EQ + Binary [31:33-31:41]: ARR + Name [31:33-31:38]: polje + Name [31:39-31:40]: j + Name [31:45-31:49]: temp + Name [32:33-32:42]: blablabla + Name [21:11-21:35]: celotenprogramtideluje69 + Binary [8:1-10:68]: OR + Binary [8:1-10:19]: OR + Binary [8:1-9:72]: OR + Binary [8:1-9:49]: OR + Binary [8:1-8:23]: AND + Binary [8:1-8:16]: EQ + Binary [8:1-8:10]: SUB + Binary [8:1-8:6]: ADD + Literal [8:1-8:2]: INT(5) + Literal [8:5-8:6]: INT(3) + Literal [8:9-8:10]: INT(2) + Literal [8:14-8:16]: INT(10) + Literal [8:19-8:23]: LOG(true) + Binary [8:26-9:49]: NEQ + Binary [8:26-8:79]: MOD + Binary [8:26-8:69]: MUL + Binary [8:26-8:46]: DIV + Name [8:26-8:32]: beseda + Name [8:35-8:46]: drugabeseda + Literal [8:49-8:69]: STR(danesjelepdanaaaaa) + Name [8:72-8:79]: cetrtic + Binary [8:83-9:49]: SUB + Binary [8:83-9:41]: ADD + Binary [8:83-9:35]: ADD + Binary [8:83-9:8]: SUB + Binary [8:83-8:97]: SUB + Binary [8:83-8:92]: ADD + Name [8:83-8:88]: petic + Literal [8:91-8:92]: INT(5) + Unary [8:95-8:97]: NOT + Name [8:96-8:97]: e + Block [9:3-9:8] + Unary [9:4-9:7]: SUB + Literal [9:5-9:7]: INT(10) + Binary [9:11-9:35]: DIV + Block [9:11-9:22] + Unary [9:12-9:21]: ADD + Block [9:13-9:21] + Unary [9:14-9:20]: ADD + Block [9:15-9:20] + Unary [9:16-9:19]: SUB + Literal [9:17-9:19]: INT(10) + Block [9:25-9:35] + Unary [9:26-9:34]: NOT + Name [9:27-9:34]: cetrtic + Literal [9:38-9:41]: INT(100) + Literal [9:44-9:49]: LOG(false) + Binary [9:52-9:72]: AND + Name [9:52-9:57]: nekaj + Binary [9:60-9:72]: EQ + Name [9:60-9:65]: nekaj + Name [9:69-9:72]: bla + Binary [9:75-10:19]: AND + Binary [9:75-9:90]: GEQ + Name [9:75-9:80]: nekaj + Name [9:84-9:90]: blabla + Binary [10:1-10:19]: LEQ + Name [10:1-10:6]: nekaj + Name [10:10-10:19]: blablabla + Binary [10:22-10:68]: AND + Binary [10:22-10:41]: LT + Binary [10:22-10:35]: ADD + Name [10:22-10:27]: nekaj + Name [10:30-10:35]: nekaj + Name [10:38-10:41]: bla + Binary [10:44-10:68]: GT + Binary [10:44-10:57]: SUB + Name [10:44-10:49]: nekaj + Name [10:52-10:57]: nekaj + Name [10:60-10:68]: nekajbla +!end + + +!code: +####### ULTIMATE TEST PINS 2k69 ###### + +# Inicializacija +# type_definition +typ x: integer; +typ y: string; +typ z: logical; +typ i: blablabla; +typ j: arr[10] arr[15] arr[20] blablabla; + +# variable_definition +var k: string; var l: integer; var q: logical; var m: blabla; var n: arr[69] arr [420] string; + +# Funkcije +# function_definition +fun firstfunction(x: integer, y: string, z: logical, n: arr[69] arr[420] string): integer = + ( {for i = 1, i <= 100, {i = i + 1}: + { + while j > (-10): { + if j * (-10) + 20 + 30 + 10 - 40 / 50 % 100 -(-(-(-(-(+10))))) != 123 then + break + else + firstfunction(123, -234, +456) + } + # komentar komentar komentar ---------------------------------------------- + } }, + + true & false | 10 | 'danes je lep dan''' & blablabla | firstfunction(blablabla, x + 10) + >= + +10 - -20 * !true - !false / (bla, ha, ne, da) - {x=10} + {y+x-z = 5 * true - false} + | a > b+3 | a < c-2 | a >= d*3 | a <= e/7 | a != f & a & b & a / a == 1 + 10 + 20 + 30 / 40 / 50 / 60 + * 10 * 20 * 30 * 40 - 50 - 60 - 80 - 90 % 4 % 6 % 7 % 8 +++++++10, + + { + if abc > abc - 10 + 10 * 10 / 10 - 10 + 10 + -10 + then + print('danes je lep dan') + }, + + { + for i = + { + while j: (j < 10) + }, + { + if a * 10 >= 100 then {a = a - 10} else break + }, + { for j = 1, j < 10, {j=j*2}: print('blablabla') }: + {blablablab[i+10-20*30][j-10-203] = null} + } + ) +{ where fun secondfunction(x: integer):integer = 10+20|30-40&123==20 { where +fun thirdfunction(y: string):arr[10] string = ({10*20=xyz}) } +}; + +fun partition(stevila: arr[10] integer, begin: integer, end: integer): integer = ( + {pivot = stevila[end]}, + {i = (begin - 1)}, + + { for j = begin, j < end, {j = j + 1}: + { if stevila[j] <= pivot then + ({i = i + 1}, + {swapTemp = stevila[j]}, + {stevila[i] = stevila[j]}, + {stevila[j] = swampTemp}) + } + }, + + ({swapTemp = stevila[i+1]}, + {stevila[i+1] = stevila[end]}, + {stevila[end] = swapTemp}), + + return(i + 1) +); + +fun izpis(besedilo: string): void = + print('Tvoj program je prestal celoten preizkus!') +!expected: +Defs [5:1-77:55] + TypeDef [5:1-5:15]: x + Atom [5:8-5:15]: INT + TypeDef [6:1-6:14]: y + Atom [6:8-6:14]: STR + TypeDef [7:1-7:15]: z + Atom [7:8-7:15]: LOG + TypeDef [8:1-8:17]: i + TypeName [8:8-8:17]: blablabla + TypeDef [9:1-9:41]: j + Array [9:8-9:41] + [10] + Array [9:16-9:41] + [15] + Array [9:24-9:41] + [20] + TypeName [9:32-9:41]: blablabla + VarDef [12:1-12:14]: k + Atom [12:8-12:14]: STR + VarDef [12:16-12:30]: l + Atom [12:23-12:30]: INT + VarDef [12:32-12:46]: q + Atom [12:39-12:46]: LOG + VarDef [12:48-12:61]: m + TypeName [12:55-12:61]: blabla + VarDef [12:63-12:94]: n + Array [12:70-12:94] + [69] + Array [12:78-12:94] + [420] + Atom [12:88-12:94]: STR + FunDef [16:1-54:2]: firstfunction + Parameter [16:19-16:29]: x + Atom [16:22-16:29]: INT + Parameter [16:31-16:40]: y + Atom [16:34-16:40]: STR + Parameter [16:42-16:52]: z + Atom [16:45-16:52]: LOG + Parameter [16:54-16:80]: n + Array [16:57-16:80] + [69] + Array [16:65-16:80] + [420] + Atom [16:74-16:80]: STR + Atom [16:83-16:90]: INT + Where [17:5-54:2] + Defs [52:9-53:62] + FunDef [52:9-53:62]: secondfunction + Parameter [52:28-52:38]: x + Atom [52:31-52:38]: INT + Atom [52:40-52:47]: INT + Where [52:50-53:62] + Defs [53:1-53:60] + FunDef [53:1-53:60]: thirdfunction + Parameter [53:19-53:28]: y + Atom [53:22-53:28]: STR + Array [53:30-53:44] + [10] + Atom [53:38-53:44]: STR + Block [53:47-53:60] + Binary [53:48-53:59]: ASSIGN + Binary [53:49-53:54]: MUL + Literal [53:49-53:51]: INT(10) + Literal [53:52-53:54]: INT(20) + Name [53:55-53:58]: xyz + Binary [52:50-52:69]: OR + Binary [52:50-52:55]: ADD + Literal [52:50-52:52]: INT(10) + Literal [52:53-52:55]: INT(20) + Binary [52:56-52:69]: AND + Binary [52:56-52:61]: SUB + Literal [52:56-52:58]: INT(30) + Literal [52:59-52:61]: INT(40) + Binary [52:62-52:69]: EQ + Literal [52:62-52:65]: INT(123) + Literal [52:67-52:69]: INT(20) + Block [17:5-51:6] + For [17:7-26:15] + Name [17:12-17:13]: i + Literal [17:16-17:17]: INT(1) + Binary [17:19-17:27]: LEQ + Name [17:19-17:20]: i + Literal [17:24-17:27]: INT(100) + Binary [17:29-17:40]: ASSIGN + Name [17:30-17:31]: i + Binary [17:34-17:39]: ADD + Name [17:34-17:35]: i + Literal [17:38-17:39]: INT(1) + While [18:9-26:10] + Binary [19:19-19:28]: GT + Name [19:19-19:20]: j + Block [19:23-19:28] + Unary [19:24-19:27]: SUB + Literal [19:25-19:27]: INT(10) + IfThenElse [19:30-24:14] + Binary [20:20-20:86]: NEQ + Binary [20:20-20:79]: SUB + Binary [20:20-20:60]: SUB + Binary [20:20-20:44]: ADD + Binary [20:20-20:39]: ADD + Binary [20:20-20:34]: ADD + Binary [20:20-20:29]: MUL + Name [20:20-20:21]: j + Block [20:24-20:29] + Unary [20:25-20:28]: SUB + Literal [20:26-20:28]: INT(10) + Literal [20:32-20:34]: INT(20) + Literal [20:37-20:39]: INT(30) + Literal [20:42-20:44]: INT(10) + Binary [20:47-20:60]: MOD + Binary [20:47-20:54]: DIV + Literal [20:47-20:49]: INT(40) + Literal [20:52-20:54]: INT(50) + Literal [20:57-20:60]: INT(100) + Block [20:62-20:79] + Unary [20:63-20:78]: SUB + Block [20:64-20:78] + Unary [20:65-20:77]: SUB + Block [20:66-20:77] + Unary [20:67-20:76]: SUB + Block [20:68-20:76] + Unary [20:69-20:75]: SUB + Block [20:70-20:75] + Unary [20:71-20:74]: ADD + Literal [20:72-20:74]: INT(10) + Literal [20:83-20:86]: INT(123) + Name [21:21-21:26]: break + Call [23:21-23:51]: firstfunction + Literal [23:35-23:38]: INT(123) + Unary [23:40-23:44]: SUB + Literal [23:41-23:44]: INT(234) + Unary [23:46-23:50]: ADD + Literal [23:47-23:50]: INT(456) + Binary [28:9-32:75]: OR + Binary [28:9-31:50]: OR + Binary [28:9-31:39]: OR + Binary [28:9-31:28]: OR + Binary [28:9-31:18]: OR + Binary [28:9-30:93]: OR + Binary [28:9-28:61]: OR + Binary [28:9-28:26]: OR + Binary [28:9-28:21]: AND + Literal [28:9-28:13]: LOG(true) + Literal [28:16-28:21]: LOG(false) + Literal [28:24-28:26]: INT(10) + Binary [28:29-28:61]: AND + Literal [28:29-28:49]: STR(danes je lep dan') + Name [28:52-28:61]: blablabla + Binary [28:64-30:93]: GEQ + Call [28:64-28:96]: firstfunction + Name [28:78-28:87]: blablabla + Binary [28:89-28:95]: ADD + Name [28:89-28:90]: x + Literal [28:93-28:95]: INT(10) + Binary [30:9-30:93]: ADD + Binary [30:9-30:64]: SUB + Binary [30:9-30:55]: SUB + Binary [30:9-30:26]: SUB + Unary [30:9-30:12]: ADD + Literal [30:10-30:12]: INT(10) + Binary [30:15-30:26]: MUL + Unary [30:15-30:18]: SUB + Literal [30:16-30:18]: INT(20) + Unary [30:21-30:26]: NOT + Literal [30:22-30:26]: LOG(true) + Binary [30:29-30:55]: DIV + Unary [30:29-30:35]: NOT + Literal [30:30-30:35]: LOG(false) + Block [30:38-30:55] + Name [30:39-30:42]: bla + Name [30:44-30:46]: ha + Name [30:48-30:50]: ne + Name [30:52-30:54]: da + Binary [30:58-30:64]: ASSIGN + Name [30:59-30:60]: x + Literal [30:61-30:63]: INT(10) + Binary [30:67-30:93]: ASSIGN + Binary [30:68-30:73]: SUB + Binary [30:68-30:71]: ADD + Name [30:68-30:69]: y + Name [30:70-30:71]: x + Name [30:72-30:73]: z + Binary [30:76-30:92]: SUB + Binary [30:76-30:84]: MUL + Literal [30:76-30:77]: INT(5) + Literal [30:80-30:84]: LOG(true) + Literal [30:87-30:92]: LOG(false) + Binary [31:11-31:18]: GT + Name [31:11-31:12]: a + Binary [31:15-31:18]: ADD + Name [31:15-31:16]: b + Literal [31:17-31:18]: INT(3) + Binary [31:21-31:28]: LT + Name [31:21-31:22]: a + Binary [31:25-31:28]: SUB + Name [31:25-31:26]: c + Literal [31:27-31:28]: INT(2) + Binary [31:31-31:39]: GEQ + Name [31:31-31:32]: a + Binary [31:36-31:39]: MUL + Name [31:36-31:37]: d + Literal [31:38-31:39]: INT(3) + Binary [31:42-31:50]: LEQ + Name [31:42-31:43]: a + Binary [31:47-31:50]: DIV + Name [31:47-31:48]: e + Literal [31:49-31:50]: INT(7) + Binary [31:53-32:75]: AND + Binary [31:53-31:67]: AND + Binary [31:53-31:63]: AND + Binary [31:53-31:59]: NEQ + Name [31:53-31:54]: a + Name [31:58-31:59]: f + Name [31:62-31:63]: a + Name [31:66-31:67]: b + Binary [31:70-32:75]: EQ + Binary [31:70-31:75]: DIV + Name [31:70-31:71]: a + Name [31:74-31:75]: a + Binary [31:79-32:75]: ADD + Binary [31:79-32:65]: SUB + Binary [31:79-32:43]: SUB + Binary [31:79-32:38]: SUB + Binary [31:79-32:33]: SUB + Binary [31:79-32:28]: ADD + Binary [31:79-31:90]: ADD + Binary [31:79-31:85]: ADD + Literal [31:79-31:80]: INT(1) + Literal [31:83-31:85]: INT(10) + Literal [31:88-31:90]: INT(20) + Binary [31:93-32:28]: MUL + Binary [31:93-32:23]: MUL + Binary [31:93-32:18]: MUL + Binary [31:93-32:13]: MUL + Binary [31:93-31:110]: DIV + Binary [31:93-31:105]: DIV + Binary [31:93-31:100]: DIV + Literal [31:93-31:95]: INT(30) + Literal [31:98-31:100]: INT(40) + Literal [31:103-31:105]: INT(50) + Literal [31:108-31:110]: INT(60) + Literal [32:11-32:13]: INT(10) + Literal [32:16-32:18]: INT(20) + Literal [32:21-32:23]: INT(30) + Literal [32:26-32:28]: INT(40) + Literal [32:31-32:33]: INT(50) + Literal [32:36-32:38]: INT(60) + Literal [32:41-32:43]: INT(80) + Binary [32:46-32:65]: MOD + Binary [32:46-32:61]: MOD + Binary [32:46-32:57]: MOD + Binary [32:46-32:53]: MOD + Literal [32:46-32:48]: INT(90) + Literal [32:52-32:53]: INT(4) + Literal [32:56-32:57]: INT(6) + Literal [32:60-32:61]: INT(7) + Literal [32:64-32:65]: INT(8) + Unary [32:67-32:75]: ADD + Unary [32:68-32:75]: ADD + Unary [32:69-32:75]: ADD + Unary [32:70-32:75]: ADD + Unary [32:71-32:75]: ADD + Unary [32:72-32:75]: ADD + Literal [32:73-32:75]: INT(10) + IfThenElse [34:9-38:10] + Binary [35:16-35:61]: GT + Name [35:16-35:19]: abc + Binary [35:22-35:61]: ADD + Binary [35:22-35:55]: ADD + Binary [35:22-35:50]: SUB + Binary [35:22-35:45]: ADD + Binary [35:22-35:30]: SUB + Name [35:22-35:25]: abc + Literal [35:28-35:30]: INT(10) + Binary [35:33-35:45]: DIV + Binary [35:33-35:40]: MUL + Literal [35:33-35:35]: INT(10) + Literal [35:38-35:40]: INT(10) + Literal [35:43-35:45]: INT(10) + Literal [35:48-35:50]: INT(10) + Literal [35:53-35:55]: INT(10) + Unary [35:58-35:61]: SUB + Literal [35:59-35:61]: INT(10) + Call [37:21-37:46]: print + Literal [37:27-37:45]: STR(danes je lep dan) + For [40:9-50:10] + Name [41:13-41:14]: i + While [42:13-44:14] + Name [43:23-43:24]: j + Block [43:26-43:34] + Binary [43:27-43:33]: LT + Name [43:27-43:28]: j + Literal [43:31-43:33]: INT(10) + IfThenElse [45:17-47:18] + Binary [46:22-46:35]: GEQ + Binary [46:22-46:28]: MUL + Name [46:22-46:23]: a + Literal [46:26-46:28]: INT(10) + Literal [46:32-46:35]: INT(100) + Binary [46:41-46:53]: ASSIGN + Name [46:42-46:43]: a + Binary [46:46-46:52]: SUB + Name [46:46-46:47]: a + Literal [46:50-46:52]: INT(10) + Name [46:59-46:64]: break + For [48:21-48:71] + Name [48:27-48:28]: j + Literal [48:31-48:32]: INT(1) + Binary [48:34-48:40]: LT + Name [48:34-48:35]: j + Literal [48:38-48:40]: INT(10) + Binary [48:42-48:49]: ASSIGN + Name [48:43-48:44]: j + Binary [48:45-48:48]: MUL + Name [48:45-48:46]: j + Literal [48:47-48:48]: INT(2) + Call [48:51-48:69]: print + Literal [48:57-48:68]: STR(blablabla) + Binary [49:25-49:66]: ASSIGN + Binary [49:26-49:58]: ARR + Binary [49:26-49:48]: ARR + Name [49:26-49:36]: blablablab + Binary [49:37-49:47]: SUB + Binary [49:37-49:41]: ADD + Name [49:37-49:38]: i + Literal [49:39-49:41]: INT(10) + Binary [49:42-49:47]: MUL + Literal [49:42-49:44]: INT(20) + Literal [49:45-49:47]: INT(30) + Binary [49:49-49:57]: SUB + Binary [49:49-49:53]: SUB + Name [49:49-49:50]: j + Literal [49:51-49:53]: INT(10) + Literal [49:54-49:57]: INT(203) + Name [49:61-49:65]: null + FunDef [56:1-74:2]: partition + Parameter [56:15-56:39]: stevila + Array [56:24-56:39] + [10] + Atom [56:32-56:39]: INT + Parameter [56:41-56:55]: begin + Atom [56:48-56:55]: INT + Parameter [56:57-56:69]: end + Atom [56:62-56:69]: INT + Atom [56:72-56:79]: INT + Block [56:82-74:2] + Binary [57:5-57:27]: ASSIGN + Name [57:6-57:11]: pivot + Binary [57:14-57:26]: ARR + Name [57:14-57:21]: stevila + Name [57:22-57:25]: end + Binary [58:5-58:22]: ASSIGN + Name [58:6-58:7]: i + Block [58:10-58:21] + Binary [58:11-58:20]: SUB + Name [58:11-58:16]: begin + Literal [58:19-58:20]: INT(1) + For [60:5-67:6] + Name [60:11-60:12]: j + Name [60:15-60:20]: begin + Binary [60:22-60:29]: LT + Name [60:22-60:23]: j + Name [60:26-60:29]: end + Binary [60:31-60:42]: ASSIGN + Name [60:32-60:33]: j + Binary [60:36-60:41]: ADD + Name [60:36-60:37]: j + Literal [60:40-60:41]: INT(1) + IfThenElse [61:9-66:10] + Binary [61:14-61:33]: LEQ + Binary [61:14-61:24]: ARR + Name [61:14-61:21]: stevila + Name [61:22-61:23]: j + Name [61:28-61:33]: pivot + Block [62:13-65:38] + Binary [62:14-62:25]: ASSIGN + Name [62:15-62:16]: i + Binary [62:19-62:24]: ADD + Name [62:19-62:20]: i + Literal [62:23-62:24]: INT(1) + Binary [63:13-63:36]: ASSIGN + Name [63:14-63:22]: swapTemp + Binary [63:25-63:35]: ARR + Name [63:25-63:32]: stevila + Name [63:33-63:34]: j + Binary [64:13-64:38]: ASSIGN + Binary [64:14-64:24]: ARR + Name [64:14-64:21]: stevila + Name [64:22-64:23]: i + Binary [64:27-64:37]: ARR + Name [64:27-64:34]: stevila + Name [64:35-64:36]: j + Binary [65:13-65:37]: ASSIGN + Binary [65:14-65:24]: ARR + Name [65:14-65:21]: stevila + Name [65:22-65:23]: j + Name [65:27-65:36]: swampTemp + Block [69:5-71:31] + Binary [69:6-69:31]: ASSIGN + Name [69:7-69:15]: swapTemp + Binary [69:18-69:30]: ARR + Name [69:18-69:25]: stevila + Binary [69:26-69:29]: ADD + Name [69:26-69:27]: i + Literal [69:28-69:29]: INT(1) + Binary [70:5-70:34]: ASSIGN + Binary [70:6-70:18]: ARR + Name [70:6-70:13]: stevila + Binary [70:14-70:17]: ADD + Name [70:14-70:15]: i + Literal [70:16-70:17]: INT(1) + Binary [70:21-70:33]: ARR + Name [70:21-70:28]: stevila + Name [70:29-70:32]: end + Binary [71:5-71:30]: ASSIGN + Binary [71:6-71:18]: ARR + Name [71:6-71:13]: stevila + Name [71:14-71:17]: end + Name [71:21-71:29]: swapTemp + Call [73:5-73:18]: return + Binary [73:12-73:17]: ADD + Name [73:12-73:13]: i + Literal [73:16-73:17]: INT(1) + FunDef [76:1-77:55]: izpis + Parameter [76:11-76:27]: besedilo + Atom [76:21-76:27]: STR + TypeName [76:30-76:34]: void + Call [77:5-77:55]: print + Literal [77:11-77:54]: STR(Tvoj program je prestal celoten preizkus!) +!end \ No newline at end of file diff --git a/src/compiler/lexer/Lexer.java b/src/compiler/lexer/Lexer.java index 82ea4e5..cf0a7b9 100644 --- a/src/compiler/lexer/Lexer.java +++ b/src/compiler/lexer/Lexer.java @@ -120,7 +120,6 @@ public List scan() { boolean comment = false; for (; i < source.length(); i++, inline_start_index++, inline_stop_index++) { - // CR char spregledamo if ((int) source.charAt(i) == 0xd) { continue; diff --git a/src/compiler/parser/Parser.java b/src/compiler/parser/Parser.java index 5d10380..71d3eaf 100644 --- a/src/compiler/parser/Parser.java +++ b/src/compiler/parser/Parser.java @@ -55,8 +55,7 @@ public Ast parse() { private Ast parseSource() { dump("source -> definitions"); ListIterator symbol_iterator = symbols.listIterator(); - String string = ""; - var tempDefs = parseDefs(symbol_iterator, string); + var tempDefs = parseDefs(symbol_iterator); Symbol curr_sym = symbol_iterator.next(); if (symbol_iterator.hasNext() && curr_sym.tokenType != TokenType.EOF) { Report.error(curr_sym.position, "There is no EOF at the end of file"); @@ -65,22 +64,21 @@ private Ast parseSource() { return tempDefs; } - private Defs parseDefs(ListIterator lexicalSymbol, String string) { + private Defs parseDefs(ListIterator lexicalSymbol) { Location start = lexicalSymbol.next().position.start; lexicalSymbol.previous(); List defsList = new ArrayList(); dump("definitions -> definition definitions_1"); - defsList.add(parseDef(lexicalSymbol, string)); - return parseDefs_1(lexicalSymbol, start, defsList, string); + defsList.add(parseDef(lexicalSymbol)); + return parseDefs_1(lexicalSymbol, start, defsList); } - private Defs parseDefs_1(ListIterator lexicalSymbol, Location start, List defsList, - String string) { + private Defs parseDefs_1(ListIterator lexicalSymbol, Location start, List defsList) { Symbol currentLexicalSym = lexicalSymbol.next(); if (currentLexicalSym.tokenType == TokenType.OP_SEMICOLON) { dump("definitions_1 -> ; definition definitions_1"); - defsList.add(parseDef(lexicalSymbol, string)); - return parseDefs_1(lexicalSymbol, start, defsList, string); + defsList.add(parseDef(lexicalSymbol)); + return parseDefs_1(lexicalSymbol, start, defsList); } else { dump("definitions_1 -> ε"); var tempDefs = new Defs(new Position(start, defsList.get(defsList.size() - 1).position.end), defsList); @@ -89,24 +87,24 @@ private Defs parseDefs_1(ListIterator lexicalSymbol, Location start, Lis } } - private Def parseDef(ListIterator lexicalSymbol, String string) { + private Def parseDef(ListIterator lexicalSymbol) { Symbol currentLexicalSym = lexicalSymbol.next(); if (currentLexicalSym.tokenType == TokenType.KW_TYP) { dump("definition -> type_definition"); - return parseTypeDef(lexicalSymbol, currentLexicalSym.position.start, string); + return parseTypeDef(lexicalSymbol, currentLexicalSym.position.start); } else if (currentLexicalSym.tokenType == TokenType.KW_FUN) { dump("definition -> function_definition"); - return parseFunDef(lexicalSymbol, currentLexicalSym.position.start, string); + return parseFunDef(lexicalSymbol, currentLexicalSym.position.start); } else if (currentLexicalSym.tokenType == TokenType.KW_VAR) { dump("definition -> variable_definition"); - return parseVarDef(lexicalSymbol, currentLexicalSym.position.start, string); + return parseVarDef(lexicalSymbol, currentLexicalSym.position.start); } else { Report.error(currentLexicalSym.position, "Wrong definition statment"); return null; } } - private TypeDef parseTypeDef(ListIterator lexicalSymbol, Location start, String string) { + private TypeDef parseTypeDef(ListIterator lexicalSymbol, Location start) { String tmp = ""; dump("type_definition -> typ id : type"); Symbol currentLexicalSym = lexicalSymbol.next(); @@ -116,15 +114,13 @@ private TypeDef parseTypeDef(ListIterator lexicalSymbol, Location start, currentLexicalSym = lexicalSymbol.next(); if (currentLexicalSym.tokenType != TokenType.OP_COLON) Report.error(currentLexicalSym.position, "After type definition identifier a colon \':\' must follow"); - string += tmp + " "; - Type tempType = parseType(lexicalSymbol, string); + Type tempType = parseType(lexicalSymbol); return new TypeDef(new Position(start, tempType.position.end), tmp.toString(), tempType); } - private Type parseType(ListIterator lexicalSymbol, String string) { + private Type parseType(ListIterator lexicalSymbol) { Symbol currentLexicalSym = lexicalSymbol.next(); Location start = currentLexicalSym.position.start; - string += currentLexicalSym.lexeme; if (currentLexicalSym.tokenType == TokenType.AT_LOGICAL) { dump("type -> logical"); return Atom.LOG(currentLexicalSym.position); @@ -139,26 +135,22 @@ private Type parseType(ListIterator lexicalSymbol, String string) { currentLexicalSym = lexicalSymbol.next(); if (currentLexicalSym.tokenType != TokenType.OP_LBRACKET) Report.error(currentLexicalSym.position, "Specifying arr lenght must be enclosed in square brackets"); - string += currentLexicalSym.lexeme + " "; currentLexicalSym = lexicalSymbol.next(); if (currentLexicalSym.tokenType != TokenType.C_INTEGER) Report.error(currentLexicalSym.position, "Array arr lenght must be an integer"); int size = Integer.parseInt(currentLexicalSym.lexeme); - string += currentLexicalSym.lexeme + " "; currentLexicalSym = lexicalSymbol.next(); if (currentLexicalSym.tokenType != TokenType.OP_RBRACKET) Report.error(currentLexicalSym.position, "Specifying arr lenght must be enclosed in square brackets"); - string += currentLexicalSym.lexeme + " "; - var tempType = parseType(lexicalSymbol, string); + var tempType = parseType(lexicalSymbol); return new Array(new Position(start, tempType.position.end), size, tempType); } else { dump("type -> id"); - string += currentLexicalSym.lexeme + " "; return new TypeName(currentLexicalSym.position, currentLexicalSym.lexeme); } } - private FunDef parseFunDef(ListIterator lexicalSymbol, Location start, String string) { + private FunDef parseFunDef(ListIterator lexicalSymbol, Location start) { dump("function_definition -> fun id \'(\' parameters \')\' \':\' type \'=\' expression"); String tmp = ""; Symbol currentLexicalSym = lexicalSymbol.next(); @@ -170,7 +162,7 @@ private FunDef parseFunDef(ListIterator lexicalSymbol, Location start, S if (currentLexicalSym.tokenType != TokenType.OP_LPARENT) Report.error(currentLexicalSym.position, "Function parameters should be enclosed in paranthesis, the left one is missing or misplaced"); - var tempParams = parseParams(lexicalSymbol, tmp); + var tempParams = parseParams(lexicalSymbol); currentLexicalSym = lexicalSymbol.next(); if (currentLexicalSym.tokenType != TokenType.OP_RPARENT) @@ -182,24 +174,23 @@ private FunDef parseFunDef(ListIterator lexicalSymbol, Location start, S Report.error(currentLexicalSym.position, "Following function declaration a colon is required to denote the body"); - string += tmp + " "; - var tempType = parseType(lexicalSymbol, string); + var tempType = parseType(lexicalSymbol); currentLexicalSym = lexicalSymbol.next(); if (currentLexicalSym.tokenType != TokenType.OP_ASSIGN) Report.error(currentLexicalSym.position, "Following the type in a function declaration an assignment operator is required"); - var tempExpr = parseExpression(lexicalSymbol, string); + var tempExpr = parseExpression(lexicalSymbol); return new FunDef(new Position(start, tempExpr.position.end), tmp, tempParams, tempType, tempExpr); } - private Expr parseExpression(ListIterator lexicalSymbol, String string) { + private Expr parseExpression(ListIterator lexicalSymbol) { dump("expression -> logical_ior_expression expression_1"); var logicalExpr = parseLogicalOrExpression(lexicalSymbol); Symbol currentLexicalSym = lexicalSymbol.next(); + Location start = currentLexicalSym.position.start; if (currentLexicalSym.tokenType == TokenType.OP_LBRACE) { dump("expression_1 -> { WHERE definitions }"); - Location start = currentLexicalSym.position.start; - return parseExpression_1(lexicalSymbol, start, logicalExpr, string); + return parseExpression_1(lexicalSymbol, start, logicalExpr); } else { dump("expression_1 -> ε"); lexicalSymbol.previous(); @@ -207,19 +198,18 @@ private Expr parseExpression(ListIterator lexicalSymbol, String string) } } - private Expr parseExpression_1(ListIterator lexicalSymbol, Location start, Expr logicalExpr, - String string) { + private Expr parseExpression_1(ListIterator lexicalSymbol, Location start, Expr logicalExpr) { Symbol currentLexicalSym = lexicalSymbol.next(); if (currentLexicalSym.tokenType != TokenType.KW_WHERE) Report.error(currentLexicalSym.position, "The keyword where is required after left brace in expression following a logical or expression"); - var tempDefs = parseDefs(lexicalSymbol, string); + var tempDefs = parseDefs(lexicalSymbol); currentLexicalSym = lexicalSymbol.next(); Location end = currentLexicalSym.position.end; if (currentLexicalSym.tokenType != TokenType.OP_RBRACE) Report.error(currentLexicalSym.position, "After definitions in a where definitions statemetn a right brace should close the block"); - return new Where(new Position(start, end), logicalExpr, tempDefs); + return new Where(new Position(logicalExpr.position.start, end), logicalExpr, tempDefs); } private Expr parseLogicalOrExpression(ListIterator lexicalSymbol) { @@ -239,14 +229,16 @@ private Expr parseLogicalOrExpression(ListIterator lexicalSymbol) { private Binary parseLogicalOrExpression_1(ListIterator lexicalSymbol, Location start, Expr left) { var right = parseLogicalAndExpression(lexicalSymbol); + var tempBinary = new Binary(new Position(left.position.start, right.position.end), left, Binary.Operator.OR, + right); Symbol currentLexicalSym = lexicalSymbol.next(); if (currentLexicalSym.tokenType == TokenType.OP_OR) { dump("logical_ior_expression_1 -> \'|\' logical_and_expression logical_ior_expression_1"); - return parseLogicalOrExpression_1(lexicalSymbol, start, right); + return parseLogicalOrExpression_1(lexicalSymbol, start, tempBinary); } else { dump("logical_ior_expression_1 -> ε"); currentLexicalSym = lexicalSymbol.previous(); - return new Binary(new Position(start, currentLexicalSym.position.end), left, Binary.Operator.OR, right); + return tempBinary; } } @@ -254,7 +246,7 @@ private Expr parseLogicalAndExpression(ListIterator lexicalSymbol) { dump("logical_and_expression -> compare_expression logical_and_expression_1"); var tempComp = parseCompareExpression(lexicalSymbol); Symbol currentLexicalSym = lexicalSymbol.next(); - Location start = currentLexicalSym.position.start; + Location start = tempComp.position.start; if (currentLexicalSym.tokenType == TokenType.OP_AND) { dump("logical_and_expression_1 -> \'&\' compare_expression logical_and_expression_1"); return parseLogicalAndExpression_1(lexicalSymbol, start, tempComp); @@ -267,14 +259,16 @@ private Expr parseLogicalAndExpression(ListIterator lexicalSymbol) { private Binary parseLogicalAndExpression_1(ListIterator lexicalSymbol, Location start, Expr left) { var right = parseCompareExpression(lexicalSymbol); + var tempBinary = new Binary(new Position(left.position.start, right.position.end), left, Binary.Operator.AND, + right); Symbol currentLexicalSym = lexicalSymbol.next(); if (currentLexicalSym.tokenType == TokenType.OP_AND) { dump("logical_and_expression_1 -> \'&\' compare_expression logical_and_expression_1"); - return parseLogicalAndExpression_1(lexicalSymbol, start, right); + return parseLogicalAndExpression_1(lexicalSymbol, start, tempBinary); } else { dump("logical_and_expression_1 -> ε"); currentLexicalSym = lexicalSymbol.previous(); - return new Binary(new Position(start, currentLexicalSym.position.end), left, Binary.Operator.AND, right); + return tempBinary; } } @@ -282,29 +276,34 @@ private Expr parseCompareExpression(ListIterator lexicalSymbol) { dump("compare_expression -> additive_expr compare_expression_1"); var tempAdditive = parseAdditiveExpression(lexicalSymbol); Symbol currentLexicalSym = lexicalSymbol.next(); - Location start = currentLexicalSym.position.start; switch (currentLexicalSym.tokenType) { case OP_EQ: dump("compare_expression_1 -> \'==\' additive_expr"); - return parseCompareExpression_1(lexicalSymbol, start, tempAdditive, Binary.Operator.EQ); + return parseCompareExpression_1(lexicalSymbol, tempAdditive.position.start, tempAdditive, + Binary.Operator.EQ); case OP_LEQ: dump("compare_expression_1 -> \'<=\' additive_expr"); - return parseCompareExpression_1(lexicalSymbol, start, tempAdditive, Binary.Operator.LEQ); + return parseCompareExpression_1(lexicalSymbol, tempAdditive.position.start, tempAdditive, + Binary.Operator.LEQ); case OP_GEQ: dump("compare_expression_1 -> \'>=\' additive_expr"); - return parseCompareExpression_1(lexicalSymbol, start, tempAdditive, Binary.Operator.GEQ); + return parseCompareExpression_1(lexicalSymbol, tempAdditive.position.start, tempAdditive, + Binary.Operator.GEQ); case OP_NEQ: dump("compare_expression_1 -> \'!=\' additive_expr"); - return parseCompareExpression_1(lexicalSymbol, start, tempAdditive, Binary.Operator.NEQ); + return parseCompareExpression_1(lexicalSymbol, tempAdditive.position.start, tempAdditive, + Binary.Operator.NEQ); case OP_GT: dump("compare_expression_1 -> \'>\' additive_expr"); - return parseCompareExpression_1(lexicalSymbol, start, tempAdditive, Binary.Operator.GT); + return parseCompareExpression_1(lexicalSymbol, tempAdditive.position.start, tempAdditive, + Binary.Operator.GT); case OP_LT: dump("compare_expression_1 -> \'<\' additive_expr"); - return parseCompareExpression_1(lexicalSymbol, start, tempAdditive, Binary.Operator.LT); + return parseCompareExpression_1(lexicalSymbol, tempAdditive.position.start, tempAdditive, + Binary.Operator.LT); default: dump("compare_expression_1 -> ε"); - currentLexicalSym = lexicalSymbol.previous(); + lexicalSymbol.previous(); return tempAdditive; } } @@ -319,13 +318,12 @@ private Expr parseAdditiveExpression(ListIterator lexicalSymbol) { dump("additive_expr -> multiplicative_expression additive_expression_1"); var tempMult = parseMultiplicativeExpression(lexicalSymbol); Symbol currentLexicalSym = lexicalSymbol.next(); - Location start = currentLexicalSym.position.start; if (currentLexicalSym.tokenType == TokenType.OP_ADD) { dump("additive_expression_1 -> \'+\' multiplicative_expression additive_expression_1"); - return parseAdditiveExpression_1(lexicalSymbol, start, tempMult, Binary.Operator.ADD); + return parseAdditiveExpression_1(lexicalSymbol, tempMult.position.start, tempMult, Binary.Operator.ADD); } else if (currentLexicalSym.tokenType == TokenType.OP_SUB) { dump("additive_expression_1 -> \'-\' multiplicative_expression additive_expression_1"); - return parseAdditiveExpression_1(lexicalSymbol, start, tempMult, Binary.Operator.SUB); + return parseAdditiveExpression_1(lexicalSymbol, tempMult.position.start, tempMult, Binary.Operator.SUB); } else { dump("additive_expression_1 -> ε"); currentLexicalSym = lexicalSymbol.previous(); @@ -336,19 +334,20 @@ private Expr parseAdditiveExpression(ListIterator lexicalSymbol) { private Binary parseAdditiveExpression_1(ListIterator lexicalSymbol, Location start, Expr left, Binary.Operator op) { var right = parseMultiplicativeExpression(lexicalSymbol); + var tempBinary = new Binary(new Position(left.position.start, right.position.end), left, op, right); Symbol currentLexicalSym = lexicalSymbol.next(); if (currentLexicalSym.tokenType == TokenType.OP_ADD) { dump("additive_expression_1 -> \'+\' multiplicative_expression additive_expression_1"); op = Binary.Operator.ADD; - return parseAdditiveExpression_1(lexicalSymbol, start, right, op); + return parseAdditiveExpression_1(lexicalSymbol, start, tempBinary, op); } else if (currentLexicalSym.tokenType == TokenType.OP_SUB) { dump("additive_expression_1 -> \'-\' multiplicative_expression additive_expression_1"); op = Binary.Operator.SUB; - return parseAdditiveExpression_1(lexicalSymbol, start, right, op); + return parseAdditiveExpression_1(lexicalSymbol, start, tempBinary, op); } else { dump("additive_expression_1 -> ε"); - currentLexicalSym = lexicalSymbol.previous(); - return new Binary(new Position(start, currentLexicalSym.position.end), left, op, right); + lexicalSymbol.previous(); + return tempBinary; } } @@ -356,19 +355,21 @@ private Expr parseMultiplicativeExpression(ListIterator lexicalSymbol) { dump("multiplicative_expression -> prefix_expression multiplicative_expression_1"); var tempPrefix = parsePrefixExpression(lexicalSymbol); Symbol currentLexicalSym = lexicalSymbol.next(); - Location start = currentLexicalSym.position.start; if (currentLexicalSym.tokenType == TokenType.OP_MUL) { dump("multiplicative_expression_1 -> \'*\' prefix_expression multiplicative_expression_1"); - return parseMultiplicativeExpression_1(lexicalSymbol, start, tempPrefix, Binary.Operator.MUL); + return parseMultiplicativeExpression_1(lexicalSymbol, tempPrefix.position.start, tempPrefix, + Binary.Operator.MUL); } else if (currentLexicalSym.tokenType == TokenType.OP_DIV) { dump("multiplicative_expression_1 -> \'/\' prefix_expression multiplicative_expression_1"); - return parseMultiplicativeExpression_1(lexicalSymbol, start, tempPrefix, Binary.Operator.DIV); + return parseMultiplicativeExpression_1(lexicalSymbol, tempPrefix.position.start, tempPrefix, + Binary.Operator.DIV); } else if (currentLexicalSym.tokenType == TokenType.OP_MOD) { dump("multiplicative_expression_1 -> \'%\' prefix_expression multiplicative_expression_1"); - return parseMultiplicativeExpression_1(lexicalSymbol, start, tempPrefix, Binary.Operator.MOD); + return parseMultiplicativeExpression_1(lexicalSymbol, tempPrefix.position.start, tempPrefix, + Binary.Operator.MOD); } else { dump("multiplicative_expression_1 -> ε"); - currentLexicalSym = lexicalSymbol.previous(); + lexicalSymbol.previous(); return tempPrefix; } } @@ -376,20 +377,21 @@ private Expr parseMultiplicativeExpression(ListIterator lexicalSymbol) { private Binary parseMultiplicativeExpression_1(ListIterator lexicalSymbol, Location start, Expr left, Binary.Operator op) { var right = parsePrefixExpression(lexicalSymbol); + var tempBinary = new Binary(new Position(left.position.start, right.position.end), left, op, right); Symbol currentLexicalSym = lexicalSymbol.next(); if (currentLexicalSym.tokenType == TokenType.OP_MUL) { dump("multiplicative_expression_1 -> \'*\' prefix_expression multiplicative_expression_1"); - return parseMultiplicativeExpression_1(lexicalSymbol, start, right, Binary.Operator.MUL); + return parseMultiplicativeExpression_1(lexicalSymbol, start, tempBinary, Binary.Operator.MUL); } else if (currentLexicalSym.tokenType == TokenType.OP_DIV) { dump("multiplicative_expression_1 -> \'/\' prefix_expression multiplicative_expression_1"); - return parseMultiplicativeExpression_1(lexicalSymbol, start, right, Binary.Operator.DIV); + return parseMultiplicativeExpression_1(lexicalSymbol, start, tempBinary, Binary.Operator.DIV); } else if (currentLexicalSym.tokenType == TokenType.OP_MOD) { dump("multiplicative_expression_1 -> \'%\' prefix_expression multiplicative_expression_1"); - return parseMultiplicativeExpression_1(lexicalSymbol, start, right, Binary.Operator.MOD); + return parseMultiplicativeExpression_1(lexicalSymbol, start, tempBinary, Binary.Operator.MOD); } else { dump("multiplicative_expression_1 -> ε"); - currentLexicalSym = lexicalSymbol.previous(); - return new Binary(new Position(start, currentLexicalSym.position.end), left, op, right); + lexicalSymbol.previous(); + return tempBinary; } } @@ -416,38 +418,43 @@ private Expr parsePrefixExpression(ListIterator lexicalSymbol) { tempPrefix = parsePostfixExpression(lexicalSymbol); return tempPrefix; } - return new Unary(new Position(start, currentLexicalSym.position.end), tempPrefix, op); + return new Unary(new Position(start, tempPrefix.position.end), tempPrefix, op); } private Expr parsePostfixExpression(ListIterator lexicalSymbol) { dump("postfix_expression -> atom_expression postfix_expression_1"); Expr tempAtom = parseAtomExpression(lexicalSymbol); Symbol currentLexicalSym = lexicalSymbol.next(); - Location start = currentLexicalSym.position.start; - Expr tempPosfix = null; if (currentLexicalSym.tokenType == TokenType.OP_LBRACKET) { dump("postfix_expression_1 -> \'[\' expression \']\' postfix_expression_1"); lexicalSymbol.previous(); - tempPosfix = parsePostfixExpression_1(lexicalSymbol); - return new Binary(new Position(start, currentLexicalSym.position.end), tempAtom, Binary.Operator.ARR, - tempPosfix); + Location[] end = new Location[1]; + var tempPosfix = parsePostfixExpression_1(lexicalSymbol, end); + return new Binary(new Position(tempAtom.position.start, end[0]), tempAtom, Binary.Operator.ARR, tempPosfix); } else { dump("postfix_expression_1 -> ε"); - currentLexicalSym = lexicalSymbol.previous(); + lexicalSymbol.previous(); return tempAtom; } } - private Expr parsePostfixExpression_1(ListIterator lexicalSymbol) { + private Expr parsePostfixExpression_1(ListIterator lexicalSymbol, Location[] end) { Symbol currentLexicalSym = lexicalSymbol.next(); if (currentLexicalSym.tokenType != TokenType.OP_LBRACKET) Report.error(currentLexicalSym.position, "Before expressions a left opening square bracket is required"); - var tempExpr = parseExpression(lexicalSymbol, ""); + var tempExpr = parseExpression(lexicalSymbol); currentLexicalSym = lexicalSymbol.next(); if (currentLexicalSym.tokenType != TokenType.OP_RBRACKET) Report.error(currentLexicalSym.position, "Following expressions a right closing square bracket is required"); - return parsePostfixExpression(lexicalSymbol); + end[0] = currentLexicalSym.position.end; + currentLexicalSym = lexicalSymbol.next(); + if (currentLexicalSym.tokenType == TokenType.OP_LBRACKET) { + return parsePostfixExpression_1(lexicalSymbol, end); + } else { + lexicalSymbol.previous(); + return tempExpr; + } } private Expr parseAtomExpression(ListIterator lexicalSymbol) { @@ -469,16 +476,16 @@ private Expr parseAtomExpression(ListIterator lexicalSymbol) { case IDENTIFIER: dump("atom_expression -> id atom_expression_id"); currentLexicalSym = lexicalSymbol.next(); - Location start = currentLexicalSym.position.start; if (currentLexicalSym.tokenType == TokenType.OP_LPARENT) { dump("atom_expression_id -> \'(\' expressions \')\'"); - var tempExprs = parseExpressions(lexicalSymbol); + var tempExprs = parseExpressions(lexicalSymbol, currentLexicalSym.position.start); currentLexicalSym = lexicalSymbol.next(); if (currentLexicalSym.tokenType != TokenType.OP_RPARENT) Report.error(currentLexicalSym.position, "Expressions should be closed with a closing right paranthesis"); - return new Call(new Position(start, currentLexicalSym.position.end), tempExprs.expressions, - currentLexicalSym.lexeme); + return new Call(new Position(id.position.start, currentLexicalSym.position.end), + tempExprs.expressions, + id.lexeme); } else { dump("atom_expression_id -> ε"); lexicalSymbol.previous(); @@ -486,34 +493,33 @@ private Expr parseAtomExpression(ListIterator lexicalSymbol) { } case OP_LPARENT: dump("atom_expression -> \'(\' expressions \')\'"); - var tempExprs = parseExpressions(lexicalSymbol); + var tempExprs = parseExpressions(lexicalSymbol, currentLexicalSym.position.start); currentLexicalSym = lexicalSymbol.next(); if (currentLexicalSym.tokenType != TokenType.OP_RPARENT) Report.error(currentLexicalSym.position, "Expressions should be closed with a closing right paranthesis"); - return new Block(currentLexicalSym.position, tempExprs.expressions); + return tempExprs; case OP_LBRACE: dump("atom_expression -> \'{\' atom_expression_lbrace_1"); - return parseAtomExpressionLBrace(lexicalSymbol); + return parseAtomExpressionLBrace(lexicalSymbol, currentLexicalSym.position.start); default: lexicalSymbol.previous(); return null; } } - private Expr parseAtomExpressionLBrace(ListIterator lexicalSymbol) { + private Expr parseAtomExpressionLBrace(ListIterator lexicalSymbol, Location start) { Symbol currentLexicalSym = lexicalSymbol.next(); - Location start = currentLexicalSym.position.start; switch (currentLexicalSym.tokenType) { case KW_IF: dump("atom_expression_lbrace_1 -> if expression then expression if_else \'}\'"); - var tempExpr1 = parseExpression(lexicalSymbol, ""); + var tempExpr1 = parseExpression(lexicalSymbol); currentLexicalSym = lexicalSymbol.next(); Optional _else = null; if (currentLexicalSym.tokenType != TokenType.KW_THEN) Report.error(currentLexicalSym.position, "In the if statement after condition a then keyword should follow"); - var tempExpr2 = parseExpression(lexicalSymbol, ""); + var tempExpr2 = parseExpression(lexicalSymbol); currentLexicalSym = lexicalSymbol.next(); if (currentLexicalSym.tokenType != TokenType.OP_RBRACE) { dump("if_else -> else expression"); @@ -529,11 +535,11 @@ private Expr parseAtomExpressionLBrace(ListIterator lexicalSymbol) { return new IfThenElse(new Position(start, currentLexicalSym.position.end), tempExpr1, tempExpr2, _else); case KW_WHILE: dump("atom_expression_lbrace_1 -> while expression \':\' expression \'}\' ."); - var tempExprWhileCond = parseExpression(lexicalSymbol, ""); + var tempExprWhileCond = parseExpression(lexicalSymbol); currentLexicalSym = lexicalSymbol.next(); if (currentLexicalSym.tokenType != TokenType.OP_COLON) Report.error(currentLexicalSym.position, "After while condition a colon should follow"); - var tempExprWhileExpr = parseExpression(lexicalSymbol, ""); + var tempExprWhileExpr = parseExpression(lexicalSymbol); currentLexicalSym = lexicalSymbol.next(); if (currentLexicalSym.tokenType != TokenType.OP_RBRACE) Report.error(currentLexicalSym.position, @@ -550,22 +556,22 @@ private Expr parseAtomExpressionLBrace(ListIterator lexicalSymbol) { if (currentLexicalSym.tokenType != TokenType.OP_ASSIGN) Report.error(currentLexicalSym.position, "Following a for identifier an assignment operator is required"); - var tempExprFor1 = parseExpression(lexicalSymbol, ""); + var tempExprFor1 = parseExpression(lexicalSymbol); currentLexicalSym = lexicalSymbol.next(); if (currentLexicalSym.tokenType != TokenType.OP_COMMA) Report.error(currentLexicalSym.position, "After the first expression in for loop statement a comma is required"); - var tempExprFor2 = parseExpression(lexicalSymbol, ""); + var tempExprFor2 = parseExpression(lexicalSymbol); currentLexicalSym = lexicalSymbol.next(); if (currentLexicalSym.tokenType != TokenType.OP_COMMA) Report.error(currentLexicalSym.position, "After the second expression in for loop statement a comma is required"); - var tempExprFor3 = parseExpression(lexicalSymbol, ""); + var tempExprFor3 = parseExpression(lexicalSymbol); currentLexicalSym = lexicalSymbol.next(); if (currentLexicalSym.tokenType != TokenType.OP_COLON) Report.error(currentLexicalSym.position, "After the third expression in for loop statement a colon is required"); - var tempForBody = parseExpression(lexicalSymbol, ""); + var tempForBody = parseExpression(lexicalSymbol); currentLexicalSym = lexicalSymbol.next(); if (currentLexicalSym.tokenType != TokenType.OP_RBRACE) Report.error(currentLexicalSym.position, @@ -576,12 +582,12 @@ private Expr parseAtomExpressionLBrace(ListIterator lexicalSymbol) { default: dump("atom_expression_lbrace_1 -> expression \'=\' expression \'}\'"); lexicalSymbol.previous(); - var tempExprAss = parseExpression(lexicalSymbol, ""); + var tempExprAss = parseExpression(lexicalSymbol); currentLexicalSym = lexicalSymbol.next(); if (currentLexicalSym.tokenType != TokenType.OP_ASSIGN) Report.error(currentLexicalSym.position, "Following an expression an assignment operator is required in this statment"); - var tempExprAss2 = parseExpression(lexicalSymbol, ""); + var tempExprAss2 = parseExpression(lexicalSymbol); currentLexicalSym = lexicalSymbol.next(); if (currentLexicalSym.tokenType != TokenType.OP_RBRACE) Report.error(currentLexicalSym.position, @@ -595,18 +601,17 @@ private Optional parseIfElse(ListIterator lexicalSymbol) { Symbol currentLexicalSym = lexicalSymbol.next(); if (currentLexicalSym.tokenType != TokenType.KW_ELSE) Report.error(currentLexicalSym.position, "Either else statement or closing right brace expected"); - return Optional.of(parseExpression(lexicalSymbol, "")); + return Optional.of(parseExpression(lexicalSymbol)); } - private Block parseExpressions(ListIterator lexicalSymbol) { + private Block parseExpressions(ListIterator lexicalSymbol, Location start) { dump("expressions -> expression expressions_1"); List expressions = new ArrayList(); - expressions.add(parseExpression(lexicalSymbol, "")); + expressions.add(parseExpression(lexicalSymbol)); Symbol currentLexicalSym = lexicalSymbol.next(); - Location start = currentLexicalSym.position.start; if (currentLexicalSym.tokenType == TokenType.OP_COMMA) { dump("expressions_1 -> \',\' expression expressions_1"); - return parseExpressions_1(lexicalSymbol, start, expressions); + return parseExpressions_1(lexicalSymbol, expressions, start); } else { dump("expressions_1 -> ε"); currentLexicalSym = lexicalSymbol.previous(); @@ -614,12 +619,12 @@ private Block parseExpressions(ListIterator lexicalSymbol) { } } - private Block parseExpressions_1(ListIterator lexicalSymbol, Location start, List expressions) { - expressions.add(parseExpression(lexicalSymbol, "")); + private Block parseExpressions_1(ListIterator lexicalSymbol, List expressions, Location start) { + expressions.add(parseExpression(lexicalSymbol)); Symbol currentLexicalSym = lexicalSymbol.next(); if (currentLexicalSym.tokenType == TokenType.OP_COMMA) { dump("expressions_1 -> \',\' expression expressions_1"); - return parseExpressions_1(lexicalSymbol, start, expressions); + return parseExpressions_1(lexicalSymbol, expressions, start); } else { dump("expressions_1 -> ε"); currentLexicalSym = lexicalSymbol.previous(); @@ -627,20 +632,20 @@ private Block parseExpressions_1(ListIterator lexicalSymbol, Location st } } - private List parseParams(ListIterator lexicalSymbol, String string) { + private List parseParams(ListIterator lexicalSymbol) { List paramsList = new ArrayList(); dump("parameters -> parameter parameters_1"); - paramsList.add(parseParam(lexicalSymbol, string)); - return parseParams_1(lexicalSymbol, paramsList, string); + paramsList.add(parseParam(lexicalSymbol)); + return parseParams_1(lexicalSymbol, paramsList); } - private List parseParams_1(ListIterator lexicalSymbol, List paramsList, - String string) { + private List parseParams_1(ListIterator lexicalSymbol, + List paramsList) { Symbol currentLexicalSym = lexicalSymbol.next(); if (currentLexicalSym.tokenType == TokenType.OP_COMMA) { dump("parameters_1 -> , parameter parameters_1"); - paramsList.add(parseParam(lexicalSymbol, string)); - return parseParams_1(lexicalSymbol, paramsList, string); + paramsList.add(parseParam(lexicalSymbol)); + return parseParams_1(lexicalSymbol, paramsList); } else { dump("parameters_1 -> ε"); lexicalSymbol.previous(); @@ -648,7 +653,7 @@ private List parseParams_1(ListIterator lexicalSymbol, } } - private FunDef.Parameter parseParam(ListIterator lexicalSymbol, String string) { + private FunDef.Parameter parseParam(ListIterator lexicalSymbol) { dump("parameter -> id : type"); Symbol currentLexicalSym = lexicalSymbol.next(); Location start = currentLexicalSym.position.start; @@ -658,12 +663,11 @@ private FunDef.Parameter parseParam(ListIterator lexicalSymbol, String s currentLexicalSym = lexicalSymbol.next(); if (currentLexicalSym.tokenType != TokenType.OP_COLON) Report.error(currentLexicalSym.position, "After parameter identificator a colon is required"); - var tempType = parseType(lexicalSymbol, tmp); - string += tmp; + var tempType = parseType(lexicalSymbol); return new FunDef.Parameter(new Position(start, tempType.position.end), tmp.toString(), tempType); } - private VarDef parseVarDef(ListIterator lexicalSymbol, Location start, String string) { + private VarDef parseVarDef(ListIterator lexicalSymbol, Location start) { String tmp = ""; Symbol currentLexicalSym = lexicalSymbol.next(); tmp += currentLexicalSym.lexeme; @@ -673,8 +677,7 @@ private VarDef parseVarDef(ListIterator lexicalSymbol, Location start, S currentLexicalSym = lexicalSymbol.next(); if (currentLexicalSym.tokenType != TokenType.OP_COLON) Report.error(currentLexicalSym.position, "After variable definition identifier a colon \':\' must follow"); - string += tmp + " "; - var tempType = parseType(lexicalSymbol, string); + var tempType = parseType(lexicalSymbol); return new VarDef(new Position(start, tempType.position.end), tmp, tempType); } diff --git a/test3.pns b/test3.pns index a0733ed..a9898f9 100644 --- a/test3.pns +++ b/test3.pns @@ -1 +1,77 @@ -fun f ( x : logical ) : integer = x + y \ No newline at end of file +####### ULTIMATE TEST PINS 2k69 ###### + +# Inicializacija +# type_definition +typ x: integer; +typ y: string; +typ z: logical; +typ i: blablabla; +typ j: arr[10] arr[15] arr[20] blablabla; + +# variable_definition +var k: string; var l: integer; var q: logical; var m: blabla; var n: arr[69] arr [420] string; + +# Funkcije +# function_definition +fun firstfunction(x: integer, y: string, z: logical, n: arr[69] arr[420] string): integer = + ( {for i = 1, i <= 100, {i = i + 1}: + { + while j > (-10): { + if j * (-10) + 20 + 30 + 10 - 40 / 50 % 100 -(-(-(-(-(+10))))) != 123 then + break + else + firstfunction(123, -234, +456) + } + # komentar komentar komentar ---------------------------------------------- + } }, + + true & false | 10 | 'danes je lep dan''' & blablabla | firstfunction(blablabla, x + 10) + >= + +10 - -20 * !true - !false / (bla, ha, ne, da) - {x=10} + {y+x-z = 5 * true - false} + | a > b+3 | a < c-2 | a >= d*3 | a <= e/7 | a != f & a & b & a / a == 1 + 10 + 20 + 30 / 40 / 50 / 60 + * 10 * 20 * 30 * 40 - 50 - 60 - 80 - 90 % 4 % 6 % 7 % 8 +++++++10, + + { + if abc > abc - 10 + 10 * 10 / 10 - 10 + 10 + -10 + then + print('danes je lep dan') + }, + + { + for i = + { + while j: (j < 10) + }, + { + if a * 10 >= 100 then {a = a - 10} else break + }, + { for j = 1, j < 10, {j=j*2}: print('blablabla') }: + {blablablab[i+10-20*30][j-10-203] = null} + } + ) +{ where fun secondfunction(x: integer):integer = 10+20|30-40&123==20 { where +fun thirdfunction(y: string):arr[10] string = ({10*20=xyz}) } +}; + +fun partition(stevila: arr[10] integer, begin: integer, end: integer): integer = ( + {pivot = stevila[end]}, + {i = (begin - 1)}, + + { for j = begin, j < end, {j = j + 1}: + { if stevila[j] <= pivot then + ({i = i + 1}, + {swapTemp = stevila[j]}, + {stevila[i] = stevila[j]}, + {stevila[j] = swampTemp}) + } + }, + + ({swapTemp = stevila[i+1]}, + {stevila[i+1] = stevila[end]}, + {stevila[end] = swapTemp}), + + return(i + 1) +); + +fun izpis(besedilo: string): void = + print('Tvoj program je prestal celoten preizkus!') \ No newline at end of file