From a00cf6122e386660cce4afd7d1cfb6b1f36f37bc Mon Sep 17 00:00:00 2001 From: SaurabhPethani Date: Tue, 9 Mar 2021 13:59:20 +0530 Subject: [PATCH 01/14] version update --- build.gradle | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build.gradle b/build.gradle index 166be09..6f0ed32 100644 --- a/build.gradle +++ b/build.gradle @@ -38,7 +38,7 @@ plugins { } group 'cafe' -version '0.4.1' +version '0.4.2' sourceCompatibility = '1.8' targetCompatibility = '1.8' From c3e26f35d4025dce63cba8c502fbc85496412de8 Mon Sep 17 00:00:00 2001 From: SaurabhPethani Date: Sat, 3 Apr 2021 19:55:20 +0530 Subject: [PATCH 02/14] Expressions Optimized --- src/main/java/compiler/parser/ParseIR.java | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 src/main/java/compiler/parser/ParseIR.java diff --git a/src/main/java/compiler/parser/ParseIR.java b/src/main/java/compiler/parser/ParseIR.java new file mode 100644 index 0000000..8c70c1a --- /dev/null +++ b/src/main/java/compiler/parser/ParseIR.java @@ -0,0 +1,2 @@ +package compiler.parser;public class ParseIR { +} From bb26f6e911cc942ac06631ab7dfb60e8a722a46e Mon Sep 17 00:00:00 2001 From: SaurabhPethani Date: Sat, 3 Apr 2021 19:57:22 +0530 Subject: [PATCH 03/14] Expression optimized with base change --- -15 export issue | 238 ++ "pethani28@gmail.com\357\200\276" | 238 ++ src/main/java/compiler/ir/OperatorType.java | 1 + src/main/java/compiler/ir/PropertyAccess.java | 8 + .../java/compiler/ir/ReferenceLookup.java | 7 + src/main/java/compiler/parser/ParseIR.java | 2250 ++++++++++++++++- .../ann-func-test/anonymous_func_export.class | Bin 0 -> 1514 bytes .../as-closure/ann_func_export.class | Bin 0 -> 1894 bytes 8 files changed, 2741 insertions(+), 1 deletion(-) create mode 100644 -15 export issue create mode 100644 "pethani28@gmail.com\357\200\276" create mode 100644 src/test/resources/ann-func-test/anonymous_func_export.class create mode 100644 src/test/resources/ann-func-test/as-closure/ann_func_export.class diff --git a/-15 export issue b/-15 export issue new file mode 100644 index 0000000..0bbada9 --- /dev/null +++ b/-15 export issue @@ -0,0 +1,238 @@ + + SSUUMMMMAARRYY OOFF LLEESSSS CCOOMMMMAANNDDSS + + Commands marked with * may be preceded by a number, _N. + Notes in parentheses indicate the behavior if _N is given. + A key preceded by a caret indicates the Ctrl key; thus ^K is ctrl-K. + + h H Display this help. + q :q Q :Q ZZ Exit. + --------------------------------------------------------------------------- + + MMOOVVIINNGG + + e ^E j ^N CR * Forward one line (or _N lines). + y ^Y k ^K ^P * Backward one line (or _N lines). + f ^F ^V SPACE * Forward one window (or _N lines). + b ^B ESC-v * Backward one window (or _N lines). + z * Forward one window (and set window to _N). + w * Backward one window (and set window to _N). + ESC-SPACE * Forward one window, but don't stop at end-of-file. + d ^D * Forward one half-window (and set half-window to _N). + u ^U * Backward one half-window (and set half-window to _N). + ESC-) RightArrow * Right one half screen width (or _N positions). + ESC-( LeftArrow * Left one half screen width (or _N positions). + ESC-} ^RightArrow Right to last column displayed. + ESC-{ ^LeftArrow Left to first column. + F Forward forever; like "tail -f". + ESC-F Like F but stop when search pattern is found. + r ^R ^L Repaint screen. + R Repaint screen, discarding buffered input. + --------------------------------------------------- + Default "window" is the screen height. + Default "half-window" is half of the screen height. + --------------------------------------------------------------------------- + + SSEEAARRCCHHIINNGG + + /_p_a_t_t_e_r_n * Search forward for (_N-th) matching line. + ?_p_a_t_t_e_r_n * Search backward for (_N-th) matching line. + n * Repeat previous search (for _N-th occurrence). + N * Repeat previous search in reverse direction. + ESC-n * Repeat previous search, spanning files. + ESC-N * Repeat previous search, reverse dir. & spanning files. + ESC-u Undo (toggle) search highlighting. + &_p_a_t_t_e_r_n * Display only matching lines + --------------------------------------------------- + A search pattern may be preceded by one or more of: + ^N or ! Search for NON-matching lines. + ^E or * Search multiple files (pass thru END OF FILE). + ^F or @ Start search at FIRST file (for /) or last file (for ?). + ^K Highlight matches, but don't move (KEEP position). + ^R Don't use REGULAR EXPRESSIONS. + --------------------------------------------------------------------------- + + JJUUMMPPIINNGG + + g < ESC-< * Go to first line in file (or line _N). + G > ESC-> * Go to last line in file (or line _N). + p % * Go to beginning of file (or _N percent into file). + t * Go to the (_N-th) next tag. + T * Go to the (_N-th) previous tag. + { ( [ * Find close bracket } ) ]. + } ) ] * Find open bracket { ( [. + ESC-^F _<_c_1_> _<_c_2_> * Find close bracket _<_c_2_>. + ESC-^B _<_c_1_> _<_c_2_> * Find open bracket _<_c_1_> + --------------------------------------------------- + Each "find close bracket" command goes forward to the close bracket + matching the (_N-th) open bracket in the top line. + Each "find open bracket" command goes backward to the open bracket + matching the (_N-th) close bracket in the bottom line. + + m_<_l_e_t_t_e_r_> Mark the current top line with . + M_<_l_e_t_t_e_r_> Mark the current bottom line with . + '_<_l_e_t_t_e_r_> Go to a previously marked position. + '' Go to the previous position. + ^X^X Same as '. + ESC-M_<_l_e_t_t_e_r_> Clear a mark. + --------------------------------------------------- + A mark is any upper-case or lower-case letter. + Certain marks are predefined: + ^ means beginning of the file + $ means end of the file + --------------------------------------------------------------------------- + + CCHHAANNGGIINNGG FFIILLEESS + + :e [_f_i_l_e] Examine a new file. + ^X^V Same as :e. + :n * Examine the (_N-th) next file from the command line. + :p * Examine the (_N-th) previous file from the command line. + :x * Examine the first (or _N-th) file from the command line. + :d Delete the current file from the command line list. + = ^G :f Print current file name. + --------------------------------------------------------------------------- + + MMIISSCCEELLLLAANNEEOOUUSS CCOOMMMMAANNDDSS + + -_<_f_l_a_g_> Toggle a command line option [see OPTIONS below]. + --_<_n_a_m_e_> Toggle a command line option, by name. + __<_f_l_a_g_> Display the setting of a command line option. + ___<_n_a_m_e_> Display the setting of an option, by name. + +_c_m_d Execute the less cmd each time a new file is examined. + + !_c_o_m_m_a_n_d Execute the shell command with $SHELL. + |XX_c_o_m_m_a_n_d Pipe file between current pos & mark XX to shell command. + s _f_i_l_e Save input to a file. + v Edit the current file with $VISUAL or $EDITOR. + V Print version number of "less". + --------------------------------------------------------------------------- + + OOPPTTIIOONNSS + + Most options may be changed either on the command line, + or from within less by using the - or -- command. + Options may be given in one of two forms: either a single + character preceded by a -, or a name preceded by --. + + -? ........ --help + Display help (from command line). + -a ........ --search-skip-screen + Search skips current screen. + -A ........ --SEARCH-SKIP-SCREEN + Search starts just after target line. + -b [_N] .... --buffers=[_N] + Number of buffers. + -B ........ --auto-buffers + Don't automatically allocate buffers for pipes. + -c ........ --clear-screen + Repaint by clearing rather than scrolling. + -d ........ --dumb + Dumb terminal. + -D [_x_n_._n] . --color=_x_n_._n + Set screen colors. (MS-DOS only) + -e -E .... --quit-at-eof --QUIT-AT-EOF + Quit at end of file. + -f ........ --force + Force open non-regular files. + -F ........ --quit-if-one-screen + Quit if entire file fits on first screen. + -g ........ --hilite-search + Highlight only last match for searches. + -G ........ --HILITE-SEARCH + Don't highlight any matches for searches. + -h [_N] .... --max-back-scroll=[_N] + Backward scroll limit. + -i ........ --ignore-case + Ignore case in searches that do not contain uppercase. + -I ........ --IGNORE-CASE + Ignore case in all searches. + -j [_N] .... --jump-target=[_N] + Screen position of target lines. + -J ........ --status-column + Display a status column at left edge of screen. + -k [_f_i_l_e] . --lesskey-file=[_f_i_l_e] + Use a lesskey file. + -K --quit-on-intr + Exit less in response to ctrl-C. + -L ........ --no-lessopen + Ignore the LESSOPEN environment variable. + -m -M .... --long-prompt --LONG-PROMPT + Set prompt style. + -n -N .... --line-numbers --LINE-NUMBERS + Don't use line numbers. + -o [_f_i_l_e] . --log-file=[_f_i_l_e] + Copy to log file (standard input only). + -O [_f_i_l_e] . --LOG-FILE=[_f_i_l_e] + Copy to log file (unconditionally overwrite). + -p [_p_a_t_t_e_r_n] --pattern=[_p_a_t_t_e_r_n] + Start at pattern (from command line). + -P [_p_r_o_m_p_t] --prompt=[_p_r_o_m_p_t] + Define new prompt. + -q -Q .... --quiet --QUIET --silent --SILENT + Quiet the terminal bell. + -r -R .... --raw-control-chars --RAW-CONTROL-CHARS + Output "raw" control characters. + -s ........ --squeeze-blank-lines + Squeeze multiple blank lines. + -S ........ --chop-long-lines + Chop (truncate) long lines rather than wrapping. + -t [_t_a_g] .. --tag=[_t_a_g] + Find a tag. + -T [_t_a_g_s_f_i_l_e] --tag-file=[_t_a_g_s_f_i_l_e] + Use an alternate tags file. + -u -U .... --underline-special --UNDERLINE-SPECIAL + Change handling of backspaces. + -V ........ --version + Display the version number of "less". + -w ........ --hilite-unread + Highlight first new line after forward-screen. + -W ........ --HILITE-UNREAD + Highlight first new line after any forward movement. + -x [_N[,...]] --tabs=[_N[,...]] + Set tab stops. + -X ........ --no-init + Don't use termcap init/deinit strings. + -y [_N] .... --max-forw-scroll=[_N] + Forward scroll limit. + -z [_N] .... --window=[_N] + Set size of window. + -" [_c[_c]] . --quotes=[_c[_c]] + Set shell quote characters. + -~ ........ --tilde + Don't display tildes after end of file. + -# [_N] .... --shift=[_N] + Horizontal scroll amount (0 = one half screen width) + ........ --no-keypad + Don't send termcap keypad init/deinit strings. + ........ --follow-name + The F command changes files if the input file is renamed. + ........ --use-backslash + Subsequent options use backslash as escape char. + + + --------------------------------------------------------------------------- + + LLIINNEE EEDDIITTIINNGG + + These keys can be used to edit text being entered + on the "command line" at the bottom of the screen. + + RightArrow ..................... ESC-l ... Move cursor right one character. + LeftArrow ...................... ESC-h ... Move cursor left one character. + ctrl-RightArrow ESC-RightArrow ESC-w ... Move cursor right one word. + ctrl-LeftArrow ESC-LeftArrow ESC-b ... Move cursor left one word. + HOME ........................... ESC-0 ... Move cursor to start of line. + END ............................ ESC-$ ... Move cursor to end of line. + BACKSPACE ................................ Delete char to left of cursor. + DELETE ......................... ESC-x ... Delete char under cursor. + ctrl-BACKSPACE ESC-BACKSPACE ........... Delete word to left of cursor. + ctrl-DELETE .... ESC-DELETE .... ESC-X ... Delete word under cursor. + ctrl-U ......... ESC (MS-DOS only) ....... Delete entire line. + UpArrow ........................ ESC-k ... Retrieve previous command line. + DownArrow ...................... ESC-j ... Retrieve next command line. + TAB ...................................... Complete filename & cycle. + SHIFT-TAB ...................... ESC-TAB Complete filename & reverse cycle. + ctrl-L ................................... Complete filename, list all. + + diff --git "a/pethani28@gmail.com\357\200\276" "b/pethani28@gmail.com\357\200\276" new file mode 100644 index 0000000..0bbada9 --- /dev/null +++ "b/pethani28@gmail.com\357\200\276" @@ -0,0 +1,238 @@ + + SSUUMMMMAARRYY OOFF LLEESSSS CCOOMMMMAANNDDSS + + Commands marked with * may be preceded by a number, _N. + Notes in parentheses indicate the behavior if _N is given. + A key preceded by a caret indicates the Ctrl key; thus ^K is ctrl-K. + + h H Display this help. + q :q Q :Q ZZ Exit. + --------------------------------------------------------------------------- + + MMOOVVIINNGG + + e ^E j ^N CR * Forward one line (or _N lines). + y ^Y k ^K ^P * Backward one line (or _N lines). + f ^F ^V SPACE * Forward one window (or _N lines). + b ^B ESC-v * Backward one window (or _N lines). + z * Forward one window (and set window to _N). + w * Backward one window (and set window to _N). + ESC-SPACE * Forward one window, but don't stop at end-of-file. + d ^D * Forward one half-window (and set half-window to _N). + u ^U * Backward one half-window (and set half-window to _N). + ESC-) RightArrow * Right one half screen width (or _N positions). + ESC-( LeftArrow * Left one half screen width (or _N positions). + ESC-} ^RightArrow Right to last column displayed. + ESC-{ ^LeftArrow Left to first column. + F Forward forever; like "tail -f". + ESC-F Like F but stop when search pattern is found. + r ^R ^L Repaint screen. + R Repaint screen, discarding buffered input. + --------------------------------------------------- + Default "window" is the screen height. + Default "half-window" is half of the screen height. + --------------------------------------------------------------------------- + + SSEEAARRCCHHIINNGG + + /_p_a_t_t_e_r_n * Search forward for (_N-th) matching line. + ?_p_a_t_t_e_r_n * Search backward for (_N-th) matching line. + n * Repeat previous search (for _N-th occurrence). + N * Repeat previous search in reverse direction. + ESC-n * Repeat previous search, spanning files. + ESC-N * Repeat previous search, reverse dir. & spanning files. + ESC-u Undo (toggle) search highlighting. + &_p_a_t_t_e_r_n * Display only matching lines + --------------------------------------------------- + A search pattern may be preceded by one or more of: + ^N or ! Search for NON-matching lines. + ^E or * Search multiple files (pass thru END OF FILE). + ^F or @ Start search at FIRST file (for /) or last file (for ?). + ^K Highlight matches, but don't move (KEEP position). + ^R Don't use REGULAR EXPRESSIONS. + --------------------------------------------------------------------------- + + JJUUMMPPIINNGG + + g < ESC-< * Go to first line in file (or line _N). + G > ESC-> * Go to last line in file (or line _N). + p % * Go to beginning of file (or _N percent into file). + t * Go to the (_N-th) next tag. + T * Go to the (_N-th) previous tag. + { ( [ * Find close bracket } ) ]. + } ) ] * Find open bracket { ( [. + ESC-^F _<_c_1_> _<_c_2_> * Find close bracket _<_c_2_>. + ESC-^B _<_c_1_> _<_c_2_> * Find open bracket _<_c_1_> + --------------------------------------------------- + Each "find close bracket" command goes forward to the close bracket + matching the (_N-th) open bracket in the top line. + Each "find open bracket" command goes backward to the open bracket + matching the (_N-th) close bracket in the bottom line. + + m_<_l_e_t_t_e_r_> Mark the current top line with . + M_<_l_e_t_t_e_r_> Mark the current bottom line with . + '_<_l_e_t_t_e_r_> Go to a previously marked position. + '' Go to the previous position. + ^X^X Same as '. + ESC-M_<_l_e_t_t_e_r_> Clear a mark. + --------------------------------------------------- + A mark is any upper-case or lower-case letter. + Certain marks are predefined: + ^ means beginning of the file + $ means end of the file + --------------------------------------------------------------------------- + + CCHHAANNGGIINNGG FFIILLEESS + + :e [_f_i_l_e] Examine a new file. + ^X^V Same as :e. + :n * Examine the (_N-th) next file from the command line. + :p * Examine the (_N-th) previous file from the command line. + :x * Examine the first (or _N-th) file from the command line. + :d Delete the current file from the command line list. + = ^G :f Print current file name. + --------------------------------------------------------------------------- + + MMIISSCCEELLLLAANNEEOOUUSS CCOOMMMMAANNDDSS + + -_<_f_l_a_g_> Toggle a command line option [see OPTIONS below]. + --_<_n_a_m_e_> Toggle a command line option, by name. + __<_f_l_a_g_> Display the setting of a command line option. + ___<_n_a_m_e_> Display the setting of an option, by name. + +_c_m_d Execute the less cmd each time a new file is examined. + + !_c_o_m_m_a_n_d Execute the shell command with $SHELL. + |XX_c_o_m_m_a_n_d Pipe file between current pos & mark XX to shell command. + s _f_i_l_e Save input to a file. + v Edit the current file with $VISUAL or $EDITOR. + V Print version number of "less". + --------------------------------------------------------------------------- + + OOPPTTIIOONNSS + + Most options may be changed either on the command line, + or from within less by using the - or -- command. + Options may be given in one of two forms: either a single + character preceded by a -, or a name preceded by --. + + -? ........ --help + Display help (from command line). + -a ........ --search-skip-screen + Search skips current screen. + -A ........ --SEARCH-SKIP-SCREEN + Search starts just after target line. + -b [_N] .... --buffers=[_N] + Number of buffers. + -B ........ --auto-buffers + Don't automatically allocate buffers for pipes. + -c ........ --clear-screen + Repaint by clearing rather than scrolling. + -d ........ --dumb + Dumb terminal. + -D [_x_n_._n] . --color=_x_n_._n + Set screen colors. (MS-DOS only) + -e -E .... --quit-at-eof --QUIT-AT-EOF + Quit at end of file. + -f ........ --force + Force open non-regular files. + -F ........ --quit-if-one-screen + Quit if entire file fits on first screen. + -g ........ --hilite-search + Highlight only last match for searches. + -G ........ --HILITE-SEARCH + Don't highlight any matches for searches. + -h [_N] .... --max-back-scroll=[_N] + Backward scroll limit. + -i ........ --ignore-case + Ignore case in searches that do not contain uppercase. + -I ........ --IGNORE-CASE + Ignore case in all searches. + -j [_N] .... --jump-target=[_N] + Screen position of target lines. + -J ........ --status-column + Display a status column at left edge of screen. + -k [_f_i_l_e] . --lesskey-file=[_f_i_l_e] + Use a lesskey file. + -K --quit-on-intr + Exit less in response to ctrl-C. + -L ........ --no-lessopen + Ignore the LESSOPEN environment variable. + -m -M .... --long-prompt --LONG-PROMPT + Set prompt style. + -n -N .... --line-numbers --LINE-NUMBERS + Don't use line numbers. + -o [_f_i_l_e] . --log-file=[_f_i_l_e] + Copy to log file (standard input only). + -O [_f_i_l_e] . --LOG-FILE=[_f_i_l_e] + Copy to log file (unconditionally overwrite). + -p [_p_a_t_t_e_r_n] --pattern=[_p_a_t_t_e_r_n] + Start at pattern (from command line). + -P [_p_r_o_m_p_t] --prompt=[_p_r_o_m_p_t] + Define new prompt. + -q -Q .... --quiet --QUIET --silent --SILENT + Quiet the terminal bell. + -r -R .... --raw-control-chars --RAW-CONTROL-CHARS + Output "raw" control characters. + -s ........ --squeeze-blank-lines + Squeeze multiple blank lines. + -S ........ --chop-long-lines + Chop (truncate) long lines rather than wrapping. + -t [_t_a_g] .. --tag=[_t_a_g] + Find a tag. + -T [_t_a_g_s_f_i_l_e] --tag-file=[_t_a_g_s_f_i_l_e] + Use an alternate tags file. + -u -U .... --underline-special --UNDERLINE-SPECIAL + Change handling of backspaces. + -V ........ --version + Display the version number of "less". + -w ........ --hilite-unread + Highlight first new line after forward-screen. + -W ........ --HILITE-UNREAD + Highlight first new line after any forward movement. + -x [_N[,...]] --tabs=[_N[,...]] + Set tab stops. + -X ........ --no-init + Don't use termcap init/deinit strings. + -y [_N] .... --max-forw-scroll=[_N] + Forward scroll limit. + -z [_N] .... --window=[_N] + Set size of window. + -" [_c[_c]] . --quotes=[_c[_c]] + Set shell quote characters. + -~ ........ --tilde + Don't display tildes after end of file. + -# [_N] .... --shift=[_N] + Horizontal scroll amount (0 = one half screen width) + ........ --no-keypad + Don't send termcap keypad init/deinit strings. + ........ --follow-name + The F command changes files if the input file is renamed. + ........ --use-backslash + Subsequent options use backslash as escape char. + + + --------------------------------------------------------------------------- + + LLIINNEE EEDDIITTIINNGG + + These keys can be used to edit text being entered + on the "command line" at the bottom of the screen. + + RightArrow ..................... ESC-l ... Move cursor right one character. + LeftArrow ...................... ESC-h ... Move cursor left one character. + ctrl-RightArrow ESC-RightArrow ESC-w ... Move cursor right one word. + ctrl-LeftArrow ESC-LeftArrow ESC-b ... Move cursor left one word. + HOME ........................... ESC-0 ... Move cursor to start of line. + END ............................ ESC-$ ... Move cursor to end of line. + BACKSPACE ................................ Delete char to left of cursor. + DELETE ......................... ESC-x ... Delete char under cursor. + ctrl-BACKSPACE ESC-BACKSPACE ........... Delete word to left of cursor. + ctrl-DELETE .... ESC-DELETE .... ESC-X ... Delete word under cursor. + ctrl-U ......... ESC (MS-DOS only) ....... Delete entire line. + UpArrow ........................ ESC-k ... Retrieve previous command line. + DownArrow ...................... ESC-j ... Retrieve next command line. + TAB ...................................... Complete filename & cycle. + SHIFT-TAB ...................... ESC-TAB Complete filename & reverse cycle. + ctrl-L ................................... Complete filename, list all. + + diff --git a/src/main/java/compiler/ir/OperatorType.java b/src/main/java/compiler/ir/OperatorType.java index 3d1cf1a..042a99a 100644 --- a/src/main/java/compiler/ir/OperatorType.java +++ b/src/main/java/compiler/ir/OperatorType.java @@ -38,6 +38,7 @@ public enum OperatorType { TIMES("*"), DIVIDE("/"), MODULO("%"), + TILDE("~"), POW("**"), FLOOR("//"), diff --git a/src/main/java/compiler/ir/PropertyAccess.java b/src/main/java/compiler/ir/PropertyAccess.java index 76526c4..4de3388 100644 --- a/src/main/java/compiler/ir/PropertyAccess.java +++ b/src/main/java/compiler/ir/PropertyAccess.java @@ -29,8 +29,16 @@ package compiler.ir; +import compiler.parser.Tokens; +import compiler.util.Position; + public class PropertyAccess extends ExpressionStatement { private String name; + private Tokens.Token firstToken; + + public void setFirstToken(Tokens.Token firstToken) { + this.firstToken = firstToken; + } private PropertyAccess(String name) { this.name = name; diff --git a/src/main/java/compiler/ir/ReferenceLookup.java b/src/main/java/compiler/ir/ReferenceLookup.java index ab1232c..8d84123 100644 --- a/src/main/java/compiler/ir/ReferenceLookup.java +++ b/src/main/java/compiler/ir/ReferenceLookup.java @@ -29,10 +29,17 @@ package compiler.ir; +import compiler.parser.Tokens; +import compiler.util.Position; + public class ReferenceLookup extends ExpressionStatement { private final String name; + private Tokens.Token firstToken; + public void setFirstToken(Tokens.Token firstToken) { + this.firstToken = firstToken; + } public ReferenceLookup(String name) { this.name = name; } diff --git a/src/main/java/compiler/parser/ParseIR.java b/src/main/java/compiler/parser/ParseIR.java index 8c70c1a..3db00b5 100644 --- a/src/main/java/compiler/parser/ParseIR.java +++ b/src/main/java/compiler/parser/ParseIR.java @@ -1,2 +1,2250 @@ -package compiler.parser;public class ParseIR { +/* + * Copyright (c) 2021. Dhyey Shah, Saurabh Pethani, Romil Nisar + * + * Developed by: + * Dhyey Shah + * https://github.com/dhyey-shah + * + * Contributors: + * Saurabh Pethani + * https://github.com/SaurabhPethani + * + * Romil Nisar + * + * + * This file is part of Cafe. + * + * Cafe is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, version 3 of the License. + * + * Cafe is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Cafe. If not, see . + */ + +package compiler.parser; + +import compiler.ast.Node; +import compiler.ast.Node.*; +import compiler.gen.ASTToCafeIrVisitor; +import compiler.gen.AnnFuncNameGenerator; +import compiler.ir.*; +import compiler.parser.Tokens.Token; +import compiler.parser.Tokens.TokenKind; +import compiler.util.Log; +import compiler.util.Position; + +import java.text.NumberFormat; +import java.text.ParseException; +import java.util.*; + +import static compiler.util.Log.Type.*; +import static compiler.util.Messages.message; + +public class ParseIR extends Parser { + static { + ParserFactory.registerParser(ParserType.MAINPARSER, new ParseIR()); + } + + private static final class Context { + final static ParseIR.Context context = new ParseIR.Context(); + + public CafeModule module; + private final Deque referenceTableStack = new LinkedList<>(); + private final Deque functionStack = new LinkedList<>(); + private final Deque> objectStack = new LinkedList<>(); + private final Deque forLoopStack = new LinkedList<>(); + private final AnnFuncNameGenerator annFuncNameGenerator = new AnnFuncNameGenerator(); + + private boolean isModuleScope = true; + + private boolean isProperty = false; + + // for functions to be exported + private boolean isExport = false; + + public void enterProperty() { + isProperty = true; + } + + public void leaveProperty() { + isProperty = false; + } + + public boolean isProperty() { + return isProperty; + } + + private Context() { + } + + public CafeModule createModule(String moduleName) { + ReferenceTable global = new ReferenceTable(); + referenceTableStack.push(global); + module = CafeModule.create(moduleName, global); + return module; + } + + public Block enterScope() { + ReferenceTable blockReferenceTable = referenceTableStack.peek() + .fork(); + referenceTableStack.push(blockReferenceTable); + return Block.create(blockReferenceTable); + } + + public void leaveScope() { + referenceTableStack.pop(); + } + + public void enterFunc(Node.FuncNode n) { + isModuleScope = false; + functionStack.push(n); + annFuncNameGenerator.enter(); + } + + public void leaveFunc() { + functionStack.pop(); + if (functionStack.size() == 0) + isModuleScope = true; + annFuncNameGenerator.leave(); + } + + public String getNextAnnFuncName() { + String name = annFuncNameGenerator.current(); + annFuncNameGenerator.next(); + return name; + } + + public enum Scope { + GLOBAL, LOCAL, CLOSURE + } + + public ParseIR.Context.Scope currentScope() { + if (functionStack.size() == 0) + return ParseIR.Context.Scope.GLOBAL; + if (functionStack.size() == 1) + return ParseIR.Context.Scope.LOCAL; + return ParseIR.Context.Scope.CLOSURE; + } + + public void newObjectStack() { + objectStack.push(new LinkedList<>()); + } + + public void popObjectStack() { + objectStack.pop(); + } + + public void push(Object object) { + if (objectStack.isEmpty()) { + newObjectStack(); + } + objectStack.peek() + .push(object); + } + + public Object pop() { + return objectStack.peek() + .pop(); + } + + public Object peek() { + return objectStack.peek(); + } + + SymbolReference createSymbolReference(String name, SymbolReference.Kind kind, SymbolReference.Scope scope) { + SymbolReference ref = SymbolReference.of(name, kind, scope); + referenceTableStack.peek() + .add(ref); + return ref; + } + + public SymbolReference createSymbolReference(String name, Node.Tag tag) { + return createSymbolReference(name, getSymbolKind(tag), getSymbolScope()); + } + + public SymbolReference getReference(String name) { + return referenceTableStack.peek() + .get(name); + } + + SymbolReference.Kind getSymbolKind(Node.Tag tag) { + if (tag == Node.Tag.VARDECL) { + return SymbolReference.Kind.VAR; + } else if (tag == Node.Tag.CONSTDECL) { + return SymbolReference.Kind.CONST; + } + throw new AssertionError("Invalid Symbol Kind"); + } + + SymbolReference.Scope getSymbolScope() { + // There can be only 2 visible symbol scopes: GLOBAL & LOCAL. + // A symbol declared inside a closure is LOCAL to that closure & a CLOSURE itself is LOCAL to its parent block. + // So there is no CLOSURE scope for symbols. + ParseIR.Context.Scope scope = currentScope(); + if (scope == ParseIR.Context.Scope.GLOBAL) + return SymbolReference.Scope.GLOBAL; + if (scope == ParseIR.Context.Scope.LOCAL || scope == ParseIR.Context.Scope.CLOSURE) + return SymbolReference.Scope.LOCAL; + throw new AssertionError("Invalid Symbol Scope"); + } + + public void addFunction(CafeFunction function) { + context.module.addFunction(function); + } + } + + public CafeModule parseToIR(String moduleName) { + ParseIR.Context.context.createModule(moduleName); + ParseIR.Context.context.newObjectStack(); + parse(); + return ParseIR.Context.context.module; + } + + + private Lexer lexer; + private Token token; + private Log log; + private boolean breakAllowed = false, innerLoop = false, error = false; + private List debug = new ArrayList<>(); + + private ParseIR() { + } + + private ParseIR(ParserFactory factory, Lexer lexer) { + debug.add("PARSING"); + this.lexer = lexer; + this.log = factory.log; + // TESTING + // nextToken(); + // while(token.kind != TokenKind.END) { + // debug.add(token.kind); + // nextToken(); + // if(token.kind == TokenKind.ERROR) + // break; + // } + } + + @Override + protected ParseIR instance(ParserFactory factory, Lexer lexer) { + return new ParseIR(factory, lexer); + } + + Token token() { + return token; + } + + Token prevToken() { + return lexer.prevToken(); + } + + void nextToken() { + if (!error) { + lexer.nextToken(); + token = lexer.token(); + } + } + + private String errorDescription(Position position, String message) { + return message + ' ' + message(SOURCE_POSITION, position.getStartLine(), position.getStartColumn()); + } + + private boolean accept(TokenKind tokenKindExpected) { + if (error) return false; + if (token.kind == TokenKind.ERROR) { + error = true; + return false; + } + if (tokenKindExpected == token.kind) { + nextToken(); + return true; + } else { + logError(tokenKindExpected); + return false; + } + } + +// private boolean accept(TokenKind tokenKindExpected) { +// return accept(tokenKindExpected); +// } + + private void logError(TokenKind tokenKindExpected) { + error = true; + log.report(SYMBOL_EXPECTED, token.pos, + errorDescription(token.pos, message(SYMBOL_EXPECTED, tokenKindExpected, token.kind))); + } + + private void logError(Log.Type issue, Object... values) { + error = true; + log.report(issue, token.pos, + errorDescription(token.pos, message(issue, values))); + + } + + ExpressionStatement parseLogicalOrExpression() { + /* + * parseLogicalAndExpression() while(TokenType == OR | TokenType == '||'){ + * parseLogicalAndExpression() } + */ + if (error) return null; + ExpressionStatement exp1 = parseLogicalAndExpression(); + while (token.kind == TokenKind.OROP || token.kind == TokenKind.OR) { + if (error) + return null; + String op = token.value(); + accept(token.kind); + ExpressionStatement exp2 = parseLogicalAndExpression(); +// exp1 = new BinaryExprNode(exp1, exp2, op); + exp1 = BinaryExpression.of(OperatorType.OR) + .right(exp2) + .left(exp1); + } + if (error) return null; + return exp1; + + } + + ExpressionStatement parseLogicalAndExpression() { + /* + * parseLogicalNotExpression() while(TokenType == AND | TokenType == '&&'){ + * parseLogicalNotExpression() } + */ + if (error) return null; + ExpressionStatement exp1 = parseLogicalNotExpression(); + while (token.kind == TokenKind.ANDOP || token.kind == TokenKind.AND) { + if (error) + return null; + String op = token.value(); + accept(token.kind); + ExpressionStatement exp2 = parseLogicalNotExpression(); + exp1 = BinaryExpression.of(OperatorType.AND) + .right(exp2) + .left(exp1); +// exp1 = new BinaryExprNode(exp1, exp2, op); + } + if (error) return null; + return exp1; + } + + ExpressionStatement parseLogicalNotExpression() { + /* + * parseNotEqualToExpression() while(TokenType == NOT | TokenType == '!'){ + * parseNotEqualToExpression() } + */ + if (error) return null; + ExpressionStatement exp1 = parseNotEqualToExpression(); + while (token.kind == TokenKind.NOTOP || token.kind == TokenKind.NOT) { + if (error) + return null; + String op = token.value(); + accept(token.kind); + ExpressionStatement exp2 = parseNotEqualToExpression(); + exp1 = BinaryExpression.of(OperatorType.NOT) + .right(exp2) + .left(exp1); +// exp1 = new BinaryExprNode(exp1, exp2, op); + } + if (error) return null; + return exp1; + } + + ExpressionStatement parseNotEqualToExpression() { + /* + * parseEqualEqualExpression() accept(NOT_EQ) while(TokenType == '!='){ + * parseEqualEqualExpression() } + */ + if (error) return null; + ExpressionStatement exp1 = parseEqualEqualExpression(); + while (token.kind == TokenKind.NOTEQU) { + if (error) + return null; + String op = token.value(); + accept(token.kind); + ExpressionStatement exp2 = parseEqualEqualExpression(); + exp1 = BinaryExpression.of(OperatorType.NOTEQUALS) + .right(exp2) + .left(exp1); +// exp1 = new BinaryExprNode(exp1, exp2, op); + } + if (error) return null; + return exp1; + } + + ExpressionStatement parseEqualEqualExpression() { + /* + * parseRealtionalExpression() while(TokenType == '=='){ + * parseRealtionalExpression() } + */ + if (error) return null; + ExpressionStatement exp1 = parseRelationalExpression(); + while (token.kind == TokenKind.EQUEQU) { + if (error) + return null; + String op = token.value(); + accept(token.kind); + ExpressionStatement exp2 = parseRelationalExpression(); + exp1 = BinaryExpression.of(OperatorType.EQUALS) + .right(exp2) + .left(exp1); +// exp1 = new BinaryExprNode(exp1, exp2, op); + } + if (error) return null; + return exp1; + } + + ExpressionStatement parseRelationalExpression() { + /* + * parseBitOrExpression() while(TokenType == <,>,<=,>=,in ,not in, is, is not ){ + * parseBitOrExpression() } + */ + if (error) return null; + Token tk = token; + ExpressionStatement exp1 = parseBitOrExpression(); + if (exp1 == null) { + error = true; + return null; + } + while (token.kind == TokenKind.LT || token.kind == TokenKind.GT || token.kind == TokenKind.LTE + || token.kind == TokenKind.GTE || token.kind == TokenKind.IN || token.kind == TokenKind.IS + || token.kind == TokenKind.NOT) { + if (error) + return null; + OperatorType op = token.kind == TokenKind.LT ? OperatorType.LESS : token.kind == TokenKind.GT ? OperatorType.MORE : token.kind == TokenKind.LTE ? OperatorType.LESSOREQUALS : token.kind == TokenKind.GTE ? OperatorType.MOREOREQUALS : token.kind == TokenKind.IN ? OperatorType.IN : token.kind == TokenKind.IS ? OperatorType.IS : token.kind == TokenKind.NOT ? OperatorType.NOT : null; + if (token.kind == TokenKind.IS) { + accept(token.kind); + op = OperatorType.IS; + if (token.kind == TokenKind.NOT) { + op = OperatorType.ISNOT; + accept(token.kind); + } + } else if (token.kind == TokenKind.NOT) { + accept(token.kind); + if (token.kind == TokenKind.IN) { + op = OperatorType.NOTIN; + accept(TokenKind.IN); + } + } + ExpressionStatement exp2 = parseBitOrExpression(); + exp1 = BinaryExpression.of(op) + .right(exp2) + .left(exp1); + } + if (error) return null; + + return exp1; + } + + ExpressionStatement parseBitOrExpression() { + /* + * parseBitXorExpression() while(TokenType == '|'){ parseBitXorExpression() } + */ + + if (error) return null; + ExpressionStatement exp1 = parseBitXorExpression(); + while (token.kind == TokenKind.BITOR) { + if (error) + return null; + String op = token.value(); + accept(token.kind); + ExpressionStatement exp2 = parseBitXorExpression(); + exp1 = BinaryExpression.of(OperatorType.BITOR) + .right(exp2) + .left(exp1); + + } + if (error) return null; + return exp1; + } + + ExpressionStatement parseBitXorExpression() { + /* + * parseLogicalAndExpression() while(TokenType == '^'){ + * parseLogicalAndExpression() } + */ + if (error) return null; + ExpressionStatement exp1 = parseBitAndExpression(); + while (token.kind == TokenKind.BITAND) { + if (error) + return null; + String op = token.value(); + accept(token.kind); + ExpressionStatement exp2 = parseBitAndExpression(); + exp1 = BinaryExpression.of(op) + .right(exp2) + .left(exp1); +// exp1 = new BinaryExprNode(exp1, exp2, op); + } + if (error) return null; + return exp1; + } + + ExpressionStatement parseBitAndExpression() { + /* + * parseBitRightShiftExpression() while(TokenType == '&'){ + * parseBitRightShiftExpression() } + */ + if (error) return null; + ExpressionStatement exp1 = parseBitRightShiftExpression(); + while (token.kind == TokenKind.ANDOP) { + if (error) + return null; + String op = token.value(); + accept(token.kind); + ExpressionStatement exp2 = parseBitRightShiftExpression(); + exp1 = BinaryExpression.of(OperatorType.AND) + .right(exp2) + .left(exp1); +// exp1 = new BinaryExprNode(exp1, exp2, op); + } + if (error) return null; + return exp1; + } + + ExpressionStatement parseBitRightShiftExpression() { + /* + * parseBitLeftShiftExpression() while(TokenType == '>>' | TokenType == '>>>'){ + * parseBitLeftShiftExpression() } + */ + if (error) return null; + ExpressionStatement exp1 = parseBitLeftShiftExpression(); + while (token.kind == TokenKind.RSHIFT || token.kind == TokenKind.TRSHIFT) { + if (error) + return null; + OperatorType op = token.kind == TokenKind.RSHIFT ? OperatorType.BITRIGHTSHIFT_SIGNED : OperatorType.BITRIGHTSHIFT_UNSIGNED; + accept(token.kind); + ExpressionStatement exp2 = parseBitLeftShiftExpression(); + exp1 = BinaryExpression.of(op) + .right(exp2) + .left(exp1); +// exp1 = new BinaryExprNode(exp1, exp2, op); + } + if (error) return null; + return exp1; + } + + ExpressionStatement parseBitLeftShiftExpression() { + /* + * parseSubtractExpression() while(TokenType == '<<' | TokenType == '<<<'){ + * parseSubtractExpression() } + */ + if (error) return null; + ExpressionStatement exp1 = parseSubtractExpression(); + while (token.kind == TokenKind.LSHIFT) { + if (error) + return null; + String op = token.value(); + accept(token.kind); + ExpressionStatement exp2 = parseSubtractExpression(); + exp1 = BinaryExpression.of(OperatorType.BITLEFTSHIFT) + .right(exp2) + .left(exp1); +// exp1 = new BinaryExprNode(exp1, exp2, op); + } + if (error) return null; + return exp1; + } + + ExpressionStatement parseSubtractExpression() { + /* + * parseAdditionExpression() while(TokenType == '-'){ parseAdditionExpression() + * } + */ + if (error) return null; + ExpressionStatement exp1 = parseAdditionExpression(); + while (token.kind == TokenKind.SUB) { + if (error) + return null; + String op = token.value(); + accept(token.kind); + ExpressionStatement exp2 = parseAdditionExpression(); + exp1 = BinaryExpression.of(OperatorType.MINUS) + .right(exp2) + .left(exp1); +// exp1 = new BinaryExprNode(exp1, exp2, op); + } + if (error) return null; + return exp1; + } + + ExpressionStatement parseAdditionExpression() { + /* + * parseMultiplicationExpression() while(TokenType == '+'){ + * parseMultiplicationExpression() } + */ + if (error) return null; + ExpressionStatement exp1 = parseMultiplicationExpression(); + while (token.kind == TokenKind.ADD) { + if (error) + return null; + String op = token.value(); + accept(token.kind); + ExpressionStatement exp2 = parseMultiplicationExpression(); + exp1 = BinaryExpression.of(OperatorType.PLUS) + .right(exp2) + .left(exp1); +// exp1 = new BinaryExprNode(exp1, exp2, op); + } + if (error) return null; + return exp1; + } + + ExpressionStatement parseMultiplicationExpression() { + /* + * parseDivisionExpression() while(TokenType == '*'){ parseDivisionExpression() + * } + */ + if (error) return null; + ExpressionStatement exp1 = parseDivisionExpression(); + while (token.kind == TokenKind.MUL) { + if (error) + return null; + String op = token.value(); + accept(token.kind); + ExpressionStatement exp2 = parseDivisionExpression(); + exp1 = BinaryExpression.of(OperatorType.TIMES) + .right(exp2) + .left(exp1); +// exp1 = new BinaryExprNode(exp1, exp2, op); + } + if (error) return null; + return exp1; + } + + ExpressionStatement parseDivisionExpression() { + /* + * parseFactorExpression() while(TokenType == /, %, // ){ + * parseFactorExpression() } + */ + if (error) return null; + ExpressionStatement exp1 = parseFactorExpression(); + while (token.kind == TokenKind.DIV || token.kind == TokenKind.MOD || token.kind == TokenKind.FLOORDIV) { + if (error) + return null; + OperatorType op = TokenKind.DIV == token.kind ? OperatorType.DIVIDE : TokenKind.MOD == token.kind ? OperatorType.MODULO : OperatorType.FLOOR; + accept(token.kind); + ExpressionStatement exp2 = parseFactorExpression(); +// exp1 = new BinaryExprNode(exp1, exp2, op); + exp1 = BinaryExpression.of(op) + .right(exp2) + .left(exp1); + } + if (error) return null; + return exp1; + } + + ExpressionStatement parseFactorExpression() { + /* + * + * if( TokenType == -, ~ ) parseFactorExpression() parsePowerExpression() + * + */ + if (error) return null; + ExpressionStatement exp1 = null; + if (token.kind == TokenKind.SUB || token.kind == TokenKind.TILDE + || token.kind == TokenKind.NOT + || token.kind == TokenKind.NOTOP) { + if (prevToken().kind == TokenKind.IDENTIFIER) { + // Handle Case where previous token is identifier and sould not generate + // UnaryOperator + exp1 = parsePowerExpression(); + } else { + OperatorType op = token.kind == TokenKind.SUB ? OperatorType.MINUS : TokenKind.TILDE == token.kind ? OperatorType.TILDE : TokenKind.NOT == token.kind ? OperatorType.NOT : OperatorType.NOTOP; + accept(token.kind); + exp1 = parsePowerExpression(); + exp1 = UnaryExpression.create(exp1, op); + } + } else { + exp1 = parsePowerExpression(); + } + if (error) return null; + return exp1; + } + + ExpressionStatement parsePowerExpression() { + /* + * parseAtomExpression() while(TokenType == '**'){ parseAtomExpression() } + */ + if (error) return null; + ExpressionStatement exp1 = null, exp2; + try { + exp1 = parseAtomExpression(); + while (token.kind == TokenKind.POWER) { + if (error) + return null; + String op = token.value(); + accept(TokenKind.POWER); + exp2 = parseFactorExpression(); + exp1 = BinaryExpression.of(OperatorType.POW) + .right(exp2) + .left(exp1); +// exp1 = new BinaryExprNode(exp1, exp2, op); + } + return exp1; + } catch (ParseException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + if (error) return null; + return exp1; + + } + + ExpressionStatement parseTrailer(ExpressionStatement oExp) throws ParseException { + ExpressionStatement node = null; + if (token.kind == TokenKind.LPAREN || token.kind == TokenKind.DOT || token.kind == TokenKind.LSQU) { + switch (token.kind) { + case LPAREN: + accept(TokenKind.LPAREN); + node = new FuncCallNode(oExp, new ArgsListNode(parseArgList())); + accept(TokenKind.RPAREN); + break; + + case LSQU: + ExprNode exp1, exp2; + debug.add("Atom Expr: " + token.kind); + while (token.kind == TokenKind.LSQU) { + if (error) + return null; + accept(TokenKind.LSQU); + // if (token.kind == TokenKind.IDENTIFIER) + // exp1 = parseIdentifier(); + // else + // exp1 = parseNumberLiteral(); + exp1 = parseAtomExpression(); + if (token.kind == TokenKind.COLON) { + accept(TokenKind.COLON); + exp2 = parseAtomExpression(); + accept(TokenKind.RSQU); + node = new SliceNode(oExp, exp1, exp2); + } else { + accept(TokenKind.RSQU); + node = new SubscriptNode(oExp, exp1); + oExp = node; + } + + } + break; + case DOT: + ExpressionStatement e1; + debug.add("Atom DOT:" + oExp); + accept(TokenKind.DOT); + Context context = Context.context; + context.enterProperty(); + e1 = parseIdentifier(); + if (error) + return null; + ExpressionStatement trail = e1; + while (token.kind != TokenKind.DOT && (token.kind == TokenKind.LSQU || token.kind == TokenKind.LPAREN)) { + trail = parseTrailer(trail); + } + context.leaveProperty(); + if (trail == null) + node = ObjectAccessStatement.create(oExp, e1); + else + node= ObjectAccessStatement.create(oExp, trail); +// node = new ObjectAccessNode(oExp, trail); + debug.add("Token Kind: " + token.kind); + break; + } + } + return node; + } + + ExpressionStatement parseAtomExpression() throws ParseException { + /* + * List of Trailers parseAtom() + * + * trail = parseTrailer() while (trail) trail = parseTrailer() + */ + + if (error) return null; + debug.add("Atom Expr Node Token: " + token.kind); + ExpressionStatement oExp = parseAtom(); + if (oExp == null) { + error = true; + return null; + } + debug.add("Atom Expr Node Token: " + token.kind); + if (oExp instanceof PropertyAccess || oExp instanceof ThisStatement || oExp instanceof ReferenceLookup) { + ExpressionStatement trailer; + while ((trailer = parseTrailer(oExp)) != null) { + oExp = trailer; + } + } + + if (error) return null; + if (oExp == null) { + error = true; + return null; + } + return oExp; + } + + CafeStatement parseExprStmt() { + if (error) return null; + ExpressionStatement exp1 = parseLogicalAndExpression(); + ExpressionStatement exp2; + debug.add("Expr Stmt : " + token.kind); + if (token.kind == TokenKind.OR || token.kind == TokenKind.OROP) { + String op = token.value(); + accept(token.kind); + exp2 = parseLogicalAndExpression(); + exp1 = BinaryExpression.of(op) + .right(exp2) + .left(exp1); +// exp1 = new BinaryExprNode(exp1, exp2, op); + } else if (token.kind == TokenKind.EQU) { + accept(token.kind); + exp2 = parseValue(); + + return AssignmentStatement.create(exp1, exp2); +// return new AsgnStmtNode(exp1, exp2); + } + if (error) return null; + return exp1; + } + + ExpressionStatement parseAtom() { + /* + * switch TokenType case LPAREN: parseExpressionStatement() accept(RPAREN) case + * IDENTIFIER: parseIdentifier() case STRINGLITERAL: parseStringLiteral() case + * NUMLiteral: parseNumberLiteral() case BOOLLiteral: parseBoolLiteral() case + * NULL: parseNull() case THIS: parseThis() + */ + if (error) return null; + debug.add("Atom Node Token: " + token.kind); + CafeStatement exp1 = null; + switch (token.kind) { + case LPAREN: + accept(TokenKind.LPAREN); + exp1 = parseExprStmt(); + accept(TokenKind.RPAREN); + break; + case IDENTIFIER: + exp1 = parseIdentifier(); + break; + case STRLIT: + exp1 = parseStringLiteral(); + break; + case NUMLIT: + try { + exp1 = parseNumberLiteral(); + } catch (ParseException e) { + // TODO Auto-generated catch block + // e.printStackTrace(); + debug.add("Exception Thrown in parseAtom by NUMLIT"); + } + break; + case TRUE: + case FALSE: + exp1 = parseBoolLiteral(); + break; + case NULL: + exp1 = parseNull(); + break; + case THIS: + exp1 = parseThis(); + break; + default: + logError(INVALID_EXPRESSION); + } + if (error) return null; + if (exp1 == null) { + error = true; + return null; + } + return (ExpressionStatement) exp1; + + } + + ExpressionStatement parseNull() { + if (error) return null; +// Token tk = token; +// accept(TokenKind.NULL); +// NullNode nullNode = new NullNode(); +// nullNode.setFirstToken(tk); + return new NullStatement(); + } + + ExpressionStatement parseThis() { + if (error) return null; +// Token tk = token; +// accept(TokenKind.THIS); +// ThisNode thisNode = new ThisNode(); +// thisNode.setFirstToken(tk); + ParseIR.Context context = ParseIR.Context.context; + boolean isGlobal = false; + if (context.isModuleScope) { + isGlobal = true; + } + return ThisStatement.create(isGlobal); + } + + ExpressionStatement parseIdentifier() { + /* + * Create Identifier Node. return IdentNode() + */ + if (error) return null; + Token prev = token; + accept(TokenKind.IDENTIFIER); + IdenNode iden = new IdenNode(prev.value()); + iden.setFirstToken(prev); + ParseIR.Context context = ParseIR.Context.context; + if (context.isProperty()) { + PropertyAccess prop = PropertyAccess.of(prev.value()); + prop.setFirstToken(prev); + return prop; + }else { + ReferenceLookup refs = ReferenceLookup.of(prev.value()); + refs.setFirstToken(prev); + return refs; + } + } + + ExpressionStatement parseStringLiteral() { + /* + * check Quotes accept(STRING LITERAL) + * + */ + if (error) return null; + Token prev = token; + accept(TokenKind.STRLIT); + return new ConstantStatement(prev.value()); + + } + + ExpressionStatement parseNumberLiteral() throws ParseException { + /* + * + */ + if (error) return null; + Token prevToken = token; + accept(TokenKind.NUMLIT); + Number num, num2; + debug.add("Num Literal PrevToken: " + prevToken.kind); + debug.add("Num Literal Token: " + token.kind); + if (token.kind == TokenKind.DOT) { + nextToken(); + if (token.kind == TokenKind.NUMLIT) { + num = NumberFormat.getInstance() + .parse(prevToken.value() + '.' + token.value()); + accept(TokenKind.NUMLIT); + if (error) return null; + return new ConstantStatement(num); + } else { + accept(TokenKind.NUMLIT); + } + } else { + num = NumberFormat.getInstance() + .parse(prevToken.value()); + if (num instanceof Long) { + if ((long) num <= Integer.MAX_VALUE) { + num2 = num.intValue(); + } else { + num2 = num; + } + } else { + num2 = num; + } + if (error) return null; + return new ConstantStatement(num2); + } + + return null; + } + + ExpressionStatement parseBoolLiteral() { + /* + * + */ + if (error) return null; + Token tk = token; + if (token.kind == TokenKind.TRUE) { + accept(TokenKind.TRUE); + if (error) return null; + + ConstantStatement boolLit = new ConstantStatement(true); +// boolLit.setFirstToken(tk); + return boolLit; + } else if (token.kind == TokenKind.FALSE) { + accept(TokenKind.FALSE); + if (error) return null; + + ConstantStatement boolLit = new ConstantStatement(false); +// boolLit.setFirstToken(tk); + return boolLit; + } + return null; + } + + void parseSubscriptList() { + /* + * while(LBRACKET) parseSubscript() + * + */ + } + + void parseSubscript() { + /* + * accept(LBRACKET) parseNumberLiteral() if ( COLON) parseNumberLiteral() + * accept(LBRACKET) + * + * + */ + } + + List parseArgList() { + /* + * parseArg() while(COMMA) parseArg() + */ + if (error) return null; + List args = new ArrayList<>(); + while (token.kind != TokenKind.RPAREN) { + if (error) + return null; + args.add(parseValue()); + if (token.kind != TokenKind.RPAREN) + accept(TokenKind.COMMA); + } + if (error) return null; + return args; + + } + + void parseArg() { + /* + * parseValue() + */ + } + + //void parseTrailer() { + /* + * if (LPAREN) parseArgList() else if (DOT) parseIdentifier() else if (LBRACKET) + * parseSubscriptList() else return false + * + */ + //} + + void parseExpressionStatement() { + /* + * parseLogAndExpression() if (|| | 'or') parseLogAndExpression() else if (EQUAL + * OPERATOR) parseEqualOperator() parseValue() else handle Error + * + */ + } + + /* parseStatements */ + IfStmtNode parseIf() { + if (error) return null; + ExprNode ifCond = null; + BlockNode ifBlock = new BlockNode(); + + Token firstToken = token; + + nextToken(); + accept(TokenKind.LPAREN); + ifCond = parseLogicalOrExpression(); + if (ifCond == null) { + // log.report(Type.ERROR, token.pos, errorDescription(token.pos, "If without condition!")); + logError(INVALID_EXPRESSION); + error = true; + return null; + } + accept(TokenKind.RPAREN); + accept(TokenKind.LCURLY); + if (token.kind != TokenKind.RCURLY) + ifBlock = parseLoopBlock(); + + accept(TokenKind.RCURLY); + + if (error) return null; + + IfStmtNode ifNode = new IfStmtNode(ifCond, ifBlock); + ifNode.setFirstToken(firstToken); + return ifNode; + } + + StmtNode parseIfStatement() { + /* + * Parse If Statement and check if there any 'else' is there, if 'yes' then + * parseIt and Break otherwise parse Else if statement and append to a list + */ + + if (error) return null; + + IfStmtNode ifNode; + if ((ifNode = parseIf()) != null) { + StmtNode elseBlock = null; + if (token.kind == TokenKind.ELSE) { + Token elseFT = token; + nextToken(); + if (token.kind == TokenKind.IF) { + elseBlock = parseIfStatement(); + } else if (accept(TokenKind.LCURLY)) { + BlockNode blockNode = new BlockNode(); + List stmt = new LinkedList<>(); + while(token.kind != TokenKind.RCURLY) + stmt.addAll(parseBlock()); + blockNode.setStmt(stmt); + accept(TokenKind.RCURLY); + elseBlock = new ElseStmtNode(ifNode, blockNode); + elseBlock.setFirstToken(elseFT); + } + if (error) return null; + ifNode.setElsePart(elseBlock); + } + return ifNode; + } + return null; + } + + void parseElseStatement() { + /* + * accept(ELSE) accept(LCURLY) parseBlockStatements() accept(RCURLY) + */ + } + + void parseElseIfStatement() { + /* + * List of ElseIfNodes + * + * while ( !ELSEIF ){ accept(ELSEIF) accept(LPAREN) parseLogORExpression() + * accept(RPAREN) accept(LCURLY) parseBlockStatements() accept(RCURLY) + * ElseIf.add(ElseIFNode(condition, block) + * + * return ElseIfNode + */ + } + + StmtNode parseAssignmentStatement() { + /* + * parseIdentifier() while (DOT) parseIdentifier() parseEqualOperator() + * parseValue() accept(SEMI) + * + * Not used + */ + if (error) return null; + + ExpressionStatement exp1 = parseIdentifier(); + accept(TokenKind.DOT); + ExpressionStatement exp2 = parseIdentifier(); + exp1 = new ObjectAccessNode(exp1, exp2); + debug.add("Obj Access Parse Assign: " + exp1); + while (token.kind == TokenKind.DOT) { + if (error) + return null; + accept(TokenKind.DOT); + exp2 = parseIdentifier(); + exp1 = new ObjectAccessNode(exp1, exp2); + } + accept(TokenKind.EQU); + ExprNode exp = parseValue(); + accept(TokenKind.SEMICOLON); + if (error) return null; + return new AsgnStmtNode(exp1, exp); + } + + /* Parse Loops */ + List parseForInit() { + /* + * + * + */ + if (error) return null; + List init = null; + ExprNode iden, val; + if (token.kind == TokenKind.SEMICOLON) + return init; + init = new ArrayList<>(); + while (token.kind == TokenKind.VAR || token.kind == TokenKind.IDENTIFIER) { + if (error) + return null; + if (token.kind == TokenKind.VAR) { + accept(TokenKind.VAR); + iden = parseIdentifier(); + accept(TokenKind.EQU); + val = parseValue(); + if (token.kind == TokenKind.COMMA) + nextToken(); + init.add(new VarDeclNode((IdenNode) iden, val)); + } else { + iden = parseIdentifier(); + accept(TokenKind.EQU); + val = parseValue(); + if (token.kind == TokenKind.COMMA) + nextToken(); + init.add(new AsgnStmtNode(iden, val)); + } + } + if (error) return null; + if (init.isEmpty()) return null; + return init; + } + + ExprNode parseForCondition() { + /* + * parseLogOrEcpression() + */ + if (error) return null; + ExprNode cond = null; + if (token.kind == TokenKind.SEMICOLON) + return cond; + cond = parseLogicalOrExpression(); + if (error) return null; + return cond; + } + + List parseForIncrement() { + /* + * + * parseAssignmentStatement() + */ + if (error) return null; + ExpressionStatement iden, val, exp2; + List incrNodes = null; + if (token.kind == TokenKind.RPAREN) { + if (error) return null; + return incrNodes; + } + + incrNodes = new ArrayList<>(); + while (token.kind == TokenKind.IDENTIFIER) { + if (error) + return null; + iden = parseIdentifier(); + // accept(TokenKind.DOT); + // ExprNode expn = parseIdentifier(); + // iden = new ObjectAccessNode(iden, expn); + Context ctx= Context.context; + ctx.enterProperty(); + while (token.kind == TokenKind.DOT) { + if (error) + return null; + accept(TokenKind.DOT); + exp2 = parseIdentifier(); + iden = ObjectAccessStatement.create(iden, exp2); +// iden = new ObjectAccessNode(iden, exp2); + } + ctx.leaveProperty(); + accept(TokenKind.EQU); + val = parseValue(); +// incrNodes.add(new AsgnStmtNode(iden, val)); + incrNodes.add(AssignmentStatement.create(iden, val)); + if (token.kind == TokenKind.COMMA) + nextToken(); + } + if (error) return null; + return incrNodes; + } + + StmtNode parseForStatement() { + /* + * accept(FOR) accept(LPAREN) if (SEMI ) parseForCondition() accept(SEMI) + * parseForIncr() else parseForInit() accept(SEMI) parseForCondition() + * accept(SEMI) parseForIncr() + */ + if (error) return null; + List init = null; + ExprNode cond = null; + List incrNode = null; + BlockNode block = null; + accept(TokenKind.FOR); + accept(TokenKind.LPAREN); + init = parseForInit(); + accept(TokenKind.SEMICOLON); + cond = parseForCondition(); + accept(TokenKind.SEMICOLON); + incrNode = parseForIncrement(); + accept(TokenKind.RPAREN); + accept(TokenKind.LCURLY); + block = parseLoopBlock(); + accept(TokenKind.RCURLY); + if (error) return null; + return new ForStmtNode(init, cond, incrNode, block); + } + + StmtNode parseLoopStatement() { + /* + * accept(LOOP) parseLoopIdentifier() accept(IN) parseLoopValue() + * parseLoopBlock() parseCollectionComprehension() + * + */ + if (error) return null; + IdenNode iden1, iden2 = null; + ExprNode exp = null; + BlockNode block = null; + + accept(TokenKind.LOOP); + iden1 = parseIdentifier(); + if (token.kind == TokenKind.COMMA) { + nextToken(); + iden2 = parseIdentifier(); + } + accept(TokenKind.IN); + try { + exp = parseAtomExpression(); + } catch (ParseException e) { + } + + if (exp == null) { + if (token.kind == TokenKind.LCURLY) + exp = parseCollection(); + else + exp = parseObjectCreation(); + + } + accept(TokenKind.LCURLY); + block = parseLoopBlock(); + accept(TokenKind.RCURLY); + if (error) return null; + return new LoopStmtNode(iden1, iden2, exp, block); + } + + StmtNode parseFlowStatement() { + /* + * if(CONTINUE) return ContinueNode if(BREAK) return BreakNode + */ + if (error) return null; + Token tk = token; + if (breakAllowed) + if (token.kind == TokenKind.CONTINUE) { + if (error) return null; + accept(TokenKind.CONTINUE); + accept(TokenKind.SEMICOLON); + ContinueStmtNode continueStmtNode = new ContinueStmtNode(); + continueStmtNode.setFirstToken(tk); + return continueStmtNode; + } else { + if (error) return null; + accept(TokenKind.BREAK); + accept(TokenKind.SEMICOLON); + BreakStmtNode breakStmtNode = new BreakStmtNode(); + breakStmtNode.setFirstToken(tk); + return breakStmtNode; + } + else { + error = true; + accept(TokenKind.IDENTIFIER); + } + return null; + } + /* Parse Loop Done */ + + /* Parse Collection */ + ExprNode parseList() { + /* + * List of Values + * + * accept(LBRACKET) List.add(parseValue()) if(COMMA or DOTDOT) + * List.add(parseValue()) else if(LOOP_KEYWORD) + * List.add(parseForComprehension()) + * + * return List + */ + if (error) return null; + List listNode = new ArrayList<>(); + ExprNode exp1, exp2; + exp1 = parseValue(); + if (token.kind == TokenKind.RANGE) { + accept(TokenKind.RANGE); + exp2 = parseValue(); + accept(TokenKind.RSQU); + return new RangeNode(exp1, exp2, RangeNode.Type.LIST); + } + listNode.add(exp1); + while (token.kind != TokenKind.RSQU) { + if (error) + return null; + accept(TokenKind.COMMA); + if (token.kind == TokenKind.RSQU) accept(TokenKind.IDENTIFIER); + listNode.add(parseValue()); + } + accept(TokenKind.RSQU); + if (error) return null; + return new ListCollNode(listNode); + } + + ExprNode parseListCollection() { + /* + * List of Collection + * + * if (RBRACKET){ return ListNode() } + * + * else { return parseList(); } + * + * + */ + if (error) return null; + accept(TokenKind.LSQU); + if (token.kind == TokenKind.RSQU) { + accept(TokenKind.RSQU); + if (error) return null; + return new ListCollNode(); + } else if (token.kind == TokenKind.LOOP) { + if (error) return null; + return parseComprehension("list"); // Accept Identifier Before + } else { + if (error) return null; + return parseList(); + } + } + + void parseMap() { // NOT Used by MapColl + /* + * List of Map + * + * case RBRACKET: return List.add(map)) case LBRACKET: map.addKey(parseValue()) + * accept(COMMA) map.addValue(parseValue()) List.add(map) + * + * return map + */ + + } + + ExprNode parseMapCollection() { + /* + * List of MapCollection List of Comp + * + * case: LBRACKET mapCollection.add(parseMap()) + * + * if (COMMA) : mapCollection.add(parseMap()) if (LOOP ) + * Comp.add(parseForComprehension()) + * + */ + if (error) return null; + Map pairs = new LinkedHashMap<>(); + nextToken(); + accept(TokenKind.LSQU); + debug.add("Map Collection : " + token.kind); + while (token.kind != TokenKind.RSQU) { + if (error) + return null; + ExprNode exp2 = new MapCollNode(), exp1 = new MapCollNode(); + if (token.kind == TokenKind.COMMA) + nextToken(); + accept(TokenKind.LSQU); + if (token.kind == TokenKind.RSQU) { + pairs.put(exp1, exp2); + accept(TokenKind.RSQU); + accept(TokenKind.COMMA); + continue; + } else if (token.kind == TokenKind.LOOP) { + return parseComprehension("map"); // Accept Identifier Before + } else { + exp1 = parseValue(); + accept(TokenKind.COMMA); + exp2 = parseValue(); + accept(TokenKind.RSQU); + } + + pairs.put(exp1, exp2); + } + accept(TokenKind.RSQU); + if (error) return null; + return new MapCollNode(pairs); + } + + CompTypeNode parseComprehension(String type) { + if (error) return null; + IdenNode iden1, iden2 = null; + ExprNode exp = null; + BlockNode block = null; + ExprNode ifCond; + + CompTypeNode mapComp = type == "map" ? new MapCompNode() + : type == "list" ? new ListCompNode() + : type == "set" ? new SetCompNode() : type == "link" ? new LinkCompNode() : null; + + while (token.kind != TokenKind.RSQU) { + if (error) + return null; + switch (token.kind) { + case LOOP: + accept(TokenKind.LOOP); + iden1 = parseIdentifier(); + if (token.kind == TokenKind.COMMA) { + nextToken(); + iden2 = parseIdentifier(); + } + accept(TokenKind.IN); + exp = parseCollection(); + if (exp == null) { + if (token.kind == TokenKind.LCURLY) + exp = parseObjectCreation(); + else + try { + exp = parseAtomExpression(); + } catch (ParseException e) { + } + } + mapComp.addExpr(new CompLoopNode(iden1, iden2, exp)); + break; + case IF: + accept(TokenKind.IF); + accept(TokenKind.LPAREN); + ifCond = parseLogicalOrExpression(); + accept(TokenKind.RPAREN); + debug.add("Comprehension: " + token.kind); + mapComp.addExpr(new CompIfNode(ifCond)); + break; + default: + error = true; + accept(TokenKind.IDENTIFIER); + } + } + accept(TokenKind.RSQU); + if (error) return null; + return mapComp; + } + + ExprNode parseSet() { + /* + * List of Values + * + * accept(LBRACKET) List.add(parseValue()) if(COMMA or DOTDOT) + * List.add(parseValue()) else if(LOOP_KEYWORD) + * List.add(parseForComprehension()) + * + * return List + */ + if (error) return null; + List setNode = new ArrayList<>(); + ExprNode exp1, exp2; + exp1 = parseValue(); + if (token.kind == TokenKind.RANGE) { + accept(TokenKind.RANGE); + exp2 = parseValue(); + accept(TokenKind.RSQU); + return new RangeNode(exp1, exp2, RangeNode.Type.SET); + } + setNode.add(exp1); + while (token.kind != TokenKind.RSQU) { + if (error) + return null; + accept(TokenKind.COMMA); + if (token.kind == TokenKind.RSQU) accept(TokenKind.IDENTIFIER); + setNode.add(parseValue()); + } + accept(TokenKind.RSQU); + if (error) return null; + return new SetCollNode(setNode); + } + + ExprNode parseSetCollection() { + /* + * List of Collection + * + * if (RBRACKET){ return SetNode() } + * + * else { return parseSet(); } + * + * + */ + if (error) return null; + nextToken(); + accept(TokenKind.LSQU); + if (token.kind == TokenKind.RSQU) { + accept(TokenKind.RSQU); + if (error) return null; + return new SetCollNode(); + } else if (token.kind == TokenKind.LOOP) { + if (error) return null; + return parseComprehension("set"); // Accept Identifier Before + } else { + if (error) return null; + return parseSet(); + } + } + + ExprNode parseLink() { + /* + * List of Values + * + * accept(LBRACKET) List.add(parseValue()) if(COMMA or DOTDOT) + * List.add(parseValue()) else if(LOOP_KEYWORD) + * List.add(parseForComprehension()) + * + * return List + */ + if (error) return null; + List listNode = new ArrayList<>(); + ExprNode exp1, exp2; + exp1 = parseValue(); + if (token.kind == TokenKind.RANGE) { + accept(TokenKind.RANGE); + exp2 = parseValue(); + accept(TokenKind.RSQU); + return new RangeNode(exp1, exp2, RangeNode.Type.LINK); + } + listNode.add(exp1); + while (token.kind != TokenKind.RSQU) { + if (error) + return null; + accept(TokenKind.COMMA); + if (token.kind == TokenKind.RSQU) accept(TokenKind.IDENTIFIER); + listNode.add(parseValue()); + } + accept(TokenKind.RSQU); + if (error) return null; + return new LinkCollNode(listNode); + } + + ExprNode parseLinkCollection() { + /* + * List of Collection + * + * if (RBRACKET){ return ListNode() } + * + * else { return parseList(); } + * + * + */ + if (error) return null; + nextToken(); + accept(TokenKind.LSQU); + if (token.kind == TokenKind.RSQU) { + accept(TokenKind.RSQU); + if (error) return null; + return new LinkCollNode(); + } else if (token.kind == TokenKind.LOOP) { + if (error) return null; + return parseComprehension("link"); // Accept Identifier Before + } else { + if (error) return null; + return parseLink(); + } + } + + ExprNode parseCollection() { + /* + * List of Collection + * + * read TokenType: case LBRACKET: parseListCollection() case LINK: case SET: + * parseCollection() case MAP: parseMapCollection() + * + * return List + */ + if (error) return null; + ExprNode collExpr = null; + + switch (token.kind) { + case LSQU: + collExpr = parseListCollection(); + break; + case LINK: + collExpr = parseLinkCollection(); + break; + case SET: + collExpr = parseSetCollection(); + break; + case MAP: + collExpr = parseMapCollection(); + break; + } + if (error) return null; + return collExpr; + + } + + /* Parse Collection Done */ + + /* Parse Values */ + ExpressionStatement parseObjectCreation() { + /* + * List of ObjectNode + * + * List.add(parseObject()) + * + * if Comma { List.add(parseObject()) } + * + */ + + if (error) return null; + Map object = new LinkedHashMap<>(); + ExpressionStatement idenNode; + ExpressionStatement exprNode; + + accept(TokenKind.LCURLY); + while (token.kind != TokenKind.RCURLY) { + if (error) + return null; + idenNode = parseIdentifier(); + accept(TokenKind.COLON); + exprNode = parseValue(); + object.put(idenNode.getName(), exprNode); + debug.add("Obj Creation Token : " + token.kind); + if (TokenKind.RCURLY != token.kind) + accept(TokenKind.COMMA); + } + debug.add("Object Creation: " + object); + accept(TokenKind.RCURLY); + if (error) return null; + return ObjectCreationStatement.of(object); +// return new ObjCreationNode(object); + + } + + void parseObject() { // Not Used + /* + * accept(LCURLY) parseIdentifier() + * + * check Colon + * + * parseValue() + * + * accept(RCURLY) + * + * + * return ObjectNode + */ + } + + ExprNode parseAnnFunction() { + /* + * accept(FUNC) isAnnFunmc = true parseFunction() + * + */ + if (error) return null; + List stmt = new ArrayList<>(); + accept(TokenKind.FUNC); + accept(TokenKind.LPAREN); + ParameterListNode params = parseParameter(); + accept(TokenKind.RPAREN); + accept(TokenKind.LCURLY); + debug.add("Ann Func Node: " + token.kind); + while (token.kind != TokenKind.RCURLY) { + if (error) + return null; + List stm = parseBlock(); + if (stm == null) return null; + stmt.addAll(stm); + } + debug.add("Ann Func Node: " + token.kind); + accept(TokenKind.RCURLY); + BlockNode block = new BlockNode(); // BlockNode(stmt); + block.setStmt(stmt); + if (error) return null; + return new AnnFuncNode(params, block); + } + + ExpressionStatement parseValue() { + /* + * ValueNode + * + * checks Grammar + * + * Case Object: parseObjectCreation() Case Collection: parseCollection() Case + * AnnFunc: parseAnnFunction() case Operator: parseBitOrOperator() + * + * return ValueNode + */ + if (error) return null; + ExpressionStatement valExpr = null; + debug.add("Parse Value: " + token.kind); + switch (token.kind) { + case LCURLY: + valExpr = parseObjectCreation(); + break; + case LSQU: + case LINK: + case SET: + case MAP: + valExpr = parseCollection(); + break; + + case FUNC: + valExpr = parseAnnFunction(); + break; + + default: + valExpr = parseBitOrExpression(); + break; + + } + if (error) return null; + return valExpr; + } + + /* Parse Values DOne */ + + void parseVariableDeclaration() { + /* + * + * parseIdentifier() parseEqualOperator() parseValue() + * + * return VariableNode + */ + + } + + List parseVariable() { + /* + * List Variables checks grammar for variable Declaration + * + * + * calls parseVariableDeclaration + * + * return List + */ + if (error) return null; + List varDeclNodes = new ArrayList<>(); + accept(TokenKind.VAR); + while (token.kind != TokenKind.SEMICOLON) { + if (error) + return null; + Token tk = token; + IdenNode idenNode = parseIdentifier(); + ExprNode exp = null; + if (error) + return null; + if (token.kind == TokenKind.EQU) { + nextToken(); + exp = parseValue(); + } + if (token.kind != TokenKind.SEMICOLON) { + accept(TokenKind.COMMA); + } + debug.add("Var Decl: " + exp); + VarDeclNode varDecl = new VarDeclNode(idenNode, exp); + varDecl.setFirstToken(tk); + varDeclNodes.add(varDecl); + } + accept(TokenKind.SEMICOLON); + if (error) return null; + return varDeclNodes; + + } + + List parseConstVariable() { + /* + * List Variables checks grammar for variable Declaration + * + * + * calls parseVariableDeclaration + * + * return List + */ + if (error) return null; + List constDeclNodes = new ArrayList<>(); + accept(TokenKind.CONST); + while (token.kind != TokenKind.SEMICOLON) { + if (error) + return null; + Token tk = token; + IdenNode idenNode = parseIdentifier(); + accept(TokenKind.EQU); + ExprNode exp = parseValue(); + if (token.kind != TokenKind.SEMICOLON) + accept(TokenKind.COMMA); + ConstDeclNode constDecl = new ConstDeclNode(idenNode, exp); + constDecl.setFirstToken(tk); + constDeclNodes.add(constDecl); + } + accept(TokenKind.SEMICOLON); + if (error) return null; + return constDeclNodes; + + } + + ParameterListNode parseParameter() { + /* + * List of Arguments + * + * responsibility: Check Grammar and parseIdentifier + * + * calls: arguments.add = parseIdentifier() arguments.add = varArgs() + * + * return List + */ + if (error) return null; + boolean varArg = false; + List idenNodes = new ArrayList<>(); + + while (token.kind != TokenKind.RPAREN) { + if (error) + return null; + debug.add("ARG List: " + token.kind); + debug.add("ARG List: " + token.value()); + if (error) + return null; + if (token.kind == TokenKind.VARARGS) { + accept(TokenKind.VARARGS); + varArg = true; + idenNodes.add(parseIdentifier()); + // accept(TokenKind.RPAREN); + break; + } + idenNodes.add(parseIdentifier()); + if (TokenKind.RPAREN != token.kind) + accept(TokenKind.COMMA); + } + + if (error) return null; + return new ParameterListNode(idenNodes, varArg); + + } + + // ExprNode parseFunctionCall(){ + // IdenNode funcName = parseIdentifier(); + + // } + + DeclNode parseFunctionDeclaration() { + /* + * List of Parameter BlockNode + * + * calls: name = isAnnFunc ? null : parseIdentifier() parameter = + * parseParameter() BlockNode= parseBlockStatement() + * + * FunctionNode(name, parameter, BlockNode); returns FunctionNode + */ + if (error) return null; + accept(TokenKind.FUNC); + Token tk = token; + ExprNode funcName = parseIdentifier(); + accept(TokenKind.LPAREN); + ParameterListNode arg = parseParameter(); + accept(TokenKind.RPAREN); + accept(TokenKind.LCURLY); + List stmt = new ArrayList<>(); + while (token.kind != TokenKind.RCURLY) { + if (error) + return null; + List stm = parseBlock(); + if (stm == null) return null; + stmt.addAll(stm); + } + accept(TokenKind.RCURLY); + BlockNode block = new BlockNode(); + block.setStmt(stmt); + if (error) return null; + + FuncDeclNode funcDecl = new FuncDeclNode((IdenNode) funcName, arg, block); + funcDecl.setFirstToken(tk); + return funcDecl; + } + + void parseDeclarativeStatement() { + /* + * List of Declarative Statement + * + * Checks Type of Statement and calls below methods to parse calls parseFunction + * calls parseVariable + */ + } + + void block() { + /* + * Handles Cases For Each Type of Block Statements like DECL, ASGN, IF etc. and + * calls respective methods + * + */ + } + + void parseBlockStatement() { + /* + * List of Statement + * + * parse Grammar, checks type of Statement and calls block() + */ + + /* + * switch(token.kind){ case VAR } + */ + + } + + StmtNode parseReturnStatement() { + if (error) return null; + accept(TokenKind.RET); + Token tk = token; + ExprNode exp = parseValue(); + debug.add("Return : " + exp); + accept(TokenKind.SEMICOLON); + if (error) return null; + + ReturnStmtNode rtrnNode = new ReturnStmtNode(exp); + rtrnNode.setFirstToken(tk); + return rtrnNode; + } + + BlockNode parseLoopBlock() { + /* + * parseBlockStatement() parseFlowStatement() + */ + if (error) return null; + List blockStats = new ArrayList<>(); + BlockNode blockNode = new BlockNode(); + while (token.kind != TokenKind.RCURLY) { + if (error) + return null; + switch (token.kind) { + case CONTINUE: + case BREAK: + blockStats.add(parseFlowStatement()); + break; + default: + List stm = parseBlock(); + if (stm == null) return null; + blockStats.addAll(stm); + } + } + if (error) return null; + blockNode.setStmt(blockStats); + return blockNode; + } + + // return Block Statement Node + List parseBlock() { + /* + * List of block Statements calls parseBlockStatement + */ + if (error) return null; + List blockStmt = new ArrayList<>(); + debug.add("Token kind " + token.kind); + switch (token.kind) { + case VAR: + List stm = parseVariable(); + if (stm == null) return null; + blockStmt.addAll(stm); + break; + case CONST: + List stm1 = parseConstVariable(); + if (stm1 == null) return null; + blockStmt.addAll(stm1); + break; + case FUNC: + DeclNode decl = parseFunctionDeclaration(); + if (decl == null) return null; + blockStmt.add(decl); + break; + case IF: + StmtNode stm2 = parseIfStatement(); + if (stm2 == null) return null; + blockStmt.add(stm2); + break; + case FOR: + innerLoop = breakAllowed ? true : false; + breakAllowed = true; + StmtNode stm3 = parseForStatement(); + if (stm3 == null) return null; + blockStmt.add(stm3); + breakAllowed = innerLoop ? true : false; + innerLoop = false; + break; + case LOOP: + StmtNode stm4 = parseLoopStatement(); + if (stm4 == null) return null; + blockStmt.add(stm4); + break; + case RET: + StmtNode stm5 = parseReturnStatement(); + if (stm5 == null) return null; + blockStmt.add(stm5); + break; + case IDENTIFIER: + case THIS: + case NULL: + StmtNode stm6 = parseExprStmt(); + if (stm6 == null) return null; + blockStmt.add(stm6); + debug.add("Block Stmt: " + token.kind); + accept(TokenKind.SEMICOLON); + break; + case BREAK: + case CONTINUE: + StmtNode stm7 = parseFlowStatement(); + blockStmt.add(stm7); + debug.add("Block Stmt: " + token.kind); + break; + default: + logError(TokenKind.IDENTIFIER); + } + if (error) return null; + debug.add("Block Stmt: " + blockStmt); + return blockStmt; + } + + List parseExportStatement() { + List exportStmtNode = new ArrayList(); + + accept(TokenKind.EXPORT); + switch (token.kind) { + case IDENTIFIER: + IdenNode id = parseIdentifier(); + if (id == null) return null; + exportStmtNode.add(new ExportStmtNode(id, null)); + while (token.kind == TokenKind.COMMA) { + accept(TokenKind.COMMA); + id = parseIdentifier(); + if (id == null) return null; + exportStmtNode.add(new ExportStmtNode(id, null)); + } + accept(TokenKind.SEMICOLON); + break; + + case VAR: + List stm = parseVariable(); + if (stm == null) return null; + for (VarDeclNode var : stm) { + exportStmtNode.add(new ExportStmtNode(var.getIden(), var)); + } + break; + case CONST: + List stm1 = parseConstVariable(); + if (stm1 == null) return null; + for (ConstDeclNode var : stm1) { + exportStmtNode.add(new ExportStmtNode(var.getIden(), var)); + } + break; + case FUNC: + DeclNode decl = parseFunctionDeclaration(); + if (decl == null) return null; + exportStmtNode.add(new ExportStmtNode(decl.getIden(), decl)); + break; + default: + error = true; + } + if (error) return null; + return exportStmtNode; + } + + // return Import Statement Node + ImportStmtNode parseImportStatement() { + /* List of Imports */ + + // accept('@'); + // boolean valid = checkFilePathRegex(token.value()); + // if(valid) return ImportStatement(token.value()) + // else Throw Error + ImportStmtNode importStmtNode = null; + Map blocks = new HashMap(); + IdenNode id1, id2 = null; + + accept(TokenKind.IMPORT); + if(token.kind == TokenKind.IDENTIFIER){ + id1 = parseIdentifier(); + if (token.kind == TokenKind.AS) { + accept(token.kind); + id2 = parseIdentifier(); + } + blocks.put(id1, id2); + while (token.kind == TokenKind.COMMA) { + accept(TokenKind.COMMA); + id1 = parseIdentifier(); + id2 = null; + if (token.kind == TokenKind.AS) { + accept(token.kind); + id2 = parseIdentifier(); + } + blocks.put(id1, id2); + } + } else { + if(accept(TokenKind.MUL)) { + id1 = new IdenNode("*"); + accept(TokenKind.AS); + id2 = parseIdentifier(); + blocks.put(id1, id2); + } + } + + accept(TokenKind.FROM); +// File file = new File(token.value()+".class"); +// if(!file.exists()){ +// +// logError(INVALID_IMPORT_FILE, token.value()); +// error = true; +// } else { + importStmtNode = new ImportStmtNode(blocks, token.value()); + nextToken(); + accept(TokenKind.SEMICOLON); + + return importStmtNode; + } + + // return Statement Node + + // return List Of Statements + ProgramNode parseStatements() { + /* + * List of Statement stats + * + * calls parseStatement() + */ + + // nextToken(); + // while(token.kind != END){ + // StatementNode stat= parseStatement(); + // stats.add(stat); + // nextToken(); + // } + // return ProgramNode(stats); + if (error) return null; + + nextToken(); + debug.add("Stmt Node Token: " + token.kind); + List tree = new ArrayList<>(); + while (token.kind != TokenKind.END) { + if (error) + return null; + switch (token.kind) { + case IMPORT: + ImportStmtNode importStmtNode = parseImportStatement(); + if (importStmtNode == null) return null; + tree.add(importStmtNode); + break; + case EXPORT: + List exports = parseExportStatement(); + if(exports == null) return null; + tree.addAll(exports); + break; + default: + List stmt = parseBlock(); + if (stmt == null) return null; + tree.addAll(stmt); + break; + } + } + if (error) return null; + debug.add("Block Statements " + tree); + return new ProgramNode(tree); + } + + // void parseStatementAsBlock() { + // /* + // * switch(token.kind){ case IF: case FOR: case } + // */ + // } + + ProgramNode parseProgram() { + // TODO: ProgramNode node; + // node = parseStatements() + // return node + if (error) return null; + ProgramNode node = parseStatements(); + if (error) return null; + return node; + } + + @Override + public Node parse() { + // while(token.kind != TokenKind.END) { + // if(token.kind == TokenKind.ERROR) { + // return; + // } + // debug.add(token.kind+" "+token.value()+" "+token.pos); + // lexer.nextToken(); + // token = lexer.token(); + // } + + // Parser p = parseProgram(); + // return p; + + return parseProgram(); + } } diff --git a/src/test/resources/ann-func-test/anonymous_func_export.class b/src/test/resources/ann-func-test/anonymous_func_export.class new file mode 100644 index 0000000000000000000000000000000000000000..c90dd9cf030885daefd54b9c41664f1935e1aa83 GIT binary patch literal 1514 zcmZuwe^V1j7=D%jmpe}hLC{idZG)P`ACcBpTPUptQ&Iv_Lv514Cp-@e+&36K^#4vaIiGviwmR4gB5lJsM zgQVOMr{uI83T|}hAVZv*_CqktP**3j_{d_$!!*wOIE!;GJj^jn<)n&4XsXKSZKA<& zJzqPfSvR3ndrLz*38%tSiak-n!8~zjiCw9Lk4yN(!>71R9pyqndBlOJgt{G9xpqpG z!e|+icTX4}U*L*O_9er3n@dI3hN|Iv8ZMhH>F za+ccs!N)~A8yJ8uEcW5RmpCY*vGw3vXEUC?bM%(Zxa9K~J&);YuKxyj?=k-T4btnyKWP_mF@?zl zGefKWIX%q&gA0GzcS=VRCV*L_C=CVujSIyaukqOcLQ*A=^GThwg}g>?P-OvE6T~#F zw$4Nkg=hT)M52fr6ljS~CC91k?KpPJVi|7Ixohtgaj>}gJ`>|yOKHdA<(C^JYhh9~`WF6y-U6uCt4 JU*T)q{Rm|4Wi9{! literal 0 HcmV?d00001 diff --git a/src/test/resources/ann-func-test/as-closure/ann_func_export.class b/src/test/resources/ann-func-test/as-closure/ann_func_export.class new file mode 100644 index 0000000000000000000000000000000000000000..71faeb5de790f244536af1d56e5b938404fe1622 GIT binary patch literal 1894 zcmcIkYg5}s6g`UpS&9h8BqU8=q$Y{&v{fJE(LzZR6BidlXiT6qZJo8ez$UWfkz}Uy z2ef~ukD0_JGo60!kLvWUC4++fp{sb_OCPO+WTW!w|1BOfa zLe=lMp==53q1Y9^aBZ>48;)3Dm>GVeA;mDhC|wyYF{JW^EfZrHH!uo~VUoLUeYfM< zb@78rqCscKJmm-6a=5!^J!m`?Htn8aFmvI)3^qLJhQeoWE==mBlgvM4Mf?=BKIUU7B7Sjf%aL&X8&gw8QLpXEN4TK+7+|b)37!23) z)f1St(3kGsLSiSzRM;ZlQzYKJPlP5eV^#%whhenErAtl8KTcT3FcqjVDoBQulql%c(Fvtvt?~APiL1C~P}zBg zv$^`+_4RuB(R!(Vqkfa&N`8Q_p>Rf3ugITRlT9&eJ6_Q7MH=s^KAyy!PeV_U(3tuI z!)#we(rq4EWzt0`J@;5@m1PYdGw2P^3xm+-ZHAwd1WEV6dnT+k5$=1<749}25zJLR z?^&mv?9E`eUUA$!Y>Of0l}fVG*rUW9rzS%|tlhv(gC^$I) zNvO9@v%WakrNmAwrX})ZJdGt2Rut-U6Bm_!4BVrmAJbSi@wU={filCyD*!aCFkBu= zaD{lYxuRMz{3XLr{}Zc8G4>}UQRgsHs|HrFMx?GY%qP^>e0G5|2r=Hs@^FaX)883 zj}(O=r{8d)c>Ouv>YI?-Vv}=mp0qM~j$A*>9InI`Q?#l)V_g)sdjSYU5!cC40iBGG zQ`z?;-zUnJ;axg6Nb!BU={S*gghgOyz*bim7~xehEn3x+%icM{U7(9n_I~zG7hn8^ z3IRRB!`@~WkD@r7j7RVpp3oZQCNfDYF~&KHr%Jf^65B-P*Q-Secb~%jf$vk;j;ZYj Y89}2LPKQlvhGKj|{vY5&e1r$UzdQxeaR2}S literal 0 HcmV?d00001 From d7017bacb46318df5286486f5ee59487bae508d0 Mon Sep 17 00:00:00 2001 From: SaurabhPethani Date: Sat, 3 Apr 2021 23:47:14 +0530 Subject: [PATCH 04/14] For Loop, Collection IR done --- src/main/java/compiler/parser/ParseIR.java | 560 +++++++++++---------- 1 file changed, 301 insertions(+), 259 deletions(-) diff --git a/src/main/java/compiler/parser/ParseIR.java b/src/main/java/compiler/parser/ParseIR.java index 3db00b5..e559ec5 100644 --- a/src/main/java/compiler/parser/ParseIR.java +++ b/src/main/java/compiler/parser/ParseIR.java @@ -38,6 +38,7 @@ import compiler.parser.Tokens.TokenKind; import compiler.util.Log; import compiler.util.Position; +import jdk.nashorn.internal.ir.Assignment; import java.text.NumberFormat; import java.text.ParseException; @@ -1129,56 +1130,62 @@ StmtNode parseAssignmentStatement() { * Not used */ if (error) return null; - - ExpressionStatement exp1 = parseIdentifier(); - accept(TokenKind.DOT); - ExpressionStatement exp2 = parseIdentifier(); - exp1 = new ObjectAccessNode(exp1, exp2); - debug.add("Obj Access Parse Assign: " + exp1); - while (token.kind == TokenKind.DOT) { - if (error) - return null; - accept(TokenKind.DOT); - exp2 = parseIdentifier(); - exp1 = new ObjectAccessNode(exp1, exp2); - } - accept(TokenKind.EQU); - ExprNode exp = parseValue(); - accept(TokenKind.SEMICOLON); - if (error) return null; - return new AsgnStmtNode(exp1, exp); +// +// ExpressionStatement exp1 = parseIdentifier(); +// accept(TokenKind.DOT); +// ExpressionStatement exp2 = parseIdentifier(); +// exp1 = new ObjectAccessNode(exp1, exp2); +// debug.add("Obj Access Parse Assign: " + exp1); +// while (token.kind == TokenKind.DOT) { +// if (error) +// return null; +// accept(TokenKind.DOT); +// exp2 = parseIdentifier(); +// exp1 = new ObjectAccessNode(exp1, exp2); +// } +// accept(TokenKind.EQU); +// ExprNode exp = parseValue(); +// accept(TokenKind.SEMICOLON); +// if (error) return null; +// return new AsgnStmtNode(exp1, exp); } /* Parse Loops */ - List parseForInit() { + List parseForInit() { /* * * */ if (error) return null; - List init = null; - ExprNode iden, val; + List init = null; + ExpressionStatement iden, val; + Context ctx = Context.context; if (token.kind == TokenKind.SEMICOLON) return init; - init = new ArrayList<>(); + init = new LinkedList<>(); while (token.kind == TokenKind.VAR || token.kind == TokenKind.IDENTIFIER) { if (error) return null; if (token.kind == TokenKind.VAR) { accept(TokenKind.VAR); iden = parseIdentifier(); + SymbolReference sym = ctx.createSymbolReference(iden.getName(), Node.Tag.VARDECL); accept(TokenKind.EQU); val = parseValue(); + DeclarativeAssignmentStatement stmt = DeclarativeAssignmentStatement.create(sym, val); if (token.kind == TokenKind.COMMA) nextToken(); - init.add(new VarDeclNode((IdenNode) iden, val)); + init.add(stmt); } else { + // this else is not needed but keeping just for reference.. iden = parseIdentifier(); + SymbolReference sym = ctx.createSymbolReference(iden.getName(), Node.Tag.VARDECL); accept(TokenKind.EQU); val = parseValue(); + DeclarativeAssignmentStatement stmt = DeclarativeAssignmentStatement.create(sym, val); if (token.kind == TokenKind.COMMA) nextToken(); - init.add(new AsgnStmtNode(iden, val)); + init.add(stmt); } } if (error) return null; @@ -1186,12 +1193,12 @@ List parseForInit() { return init; } - ExprNode parseForCondition() { + ExpressionStatement parseForCondition() { /* * parseLogOrEcpression() */ if (error) return null; - ExprNode cond = null; + ExpressionStatement cond = null; if (token.kind == TokenKind.SEMICOLON) return cond; cond = parseLogicalOrExpression(); @@ -1199,14 +1206,14 @@ ExprNode parseForCondition() { return cond; } - List parseForIncrement() { + List> parseForIncrement() { /* * * parseAssignmentStatement() */ if (error) return null; ExpressionStatement iden, val, exp2; - List incrNodes = null; + List> incrNodes = null; if (token.kind == TokenKind.RPAREN) { if (error) return null; return incrNodes; @@ -1242,17 +1249,20 @@ List parseForIncrement() { return incrNodes; } - StmtNode parseForStatement() { + CafeStatement parseForStatement() { /* * accept(FOR) accept(LPAREN) if (SEMI ) parseForCondition() accept(SEMI) * parseForIncr() else parseForInit() accept(SEMI) parseForCondition() * accept(SEMI) parseForIncr() */ if (error) return null; - List init = null; - ExprNode cond = null; - List incrNode = null; - BlockNode block = null; + Context context = Context.context; + ForLoopStatement forLoop = ForLoopStatement.loop(); + context.forLoopStack.push(forLoop); + List init = null; + ExpressionStatement cond = null; + List> incrNode = null; + Block block = null; accept(TokenKind.FOR); accept(TokenKind.LPAREN); init = parseForInit(); @@ -1265,19 +1275,24 @@ StmtNode parseForStatement() { block = parseLoopBlock(); accept(TokenKind.RCURLY); if (error) return null; - return new ForStmtNode(init, cond, incrNode, block); + forLoop.condition(cond) + .init(init) + .postStatement(incrNode) + .block(block); + context.forLoopStack.pop(); + return forLoop; } - StmtNode parseLoopStatement() { + CafeStatement parseLoopStatement() { /* * accept(LOOP) parseLoopIdentifier() accept(IN) parseLoopValue() * parseLoopBlock() parseCollectionComprehension() * */ if (error) return null; - IdenNode iden1, iden2 = null; - ExprNode exp = null; - BlockNode block = null; + ExpressionStatement iden1, iden2 = null; + ExpressionStatement exp = null; + Block block = null; accept(TokenKind.LOOP); iden1 = parseIdentifier(); @@ -1302,15 +1317,18 @@ StmtNode parseLoopStatement() { block = parseLoopBlock(); accept(TokenKind.RCURLY); if (error) return null; - return new LoopStmtNode(iden1, iden2, exp, block); + //TODO: Implementation of LoopStmt IR node is yet to be done. +// return new LoopStmtNode(iden1, iden2, exp, block); + return null; } - StmtNode parseFlowStatement() { + CafeStatement parseFlowStatement() { /* * if(CONTINUE) return ContinueNode if(BREAK) return BreakNode */ if (error) return null; Token tk = token; + Context context = Context.context; if (breakAllowed) if (token.kind == TokenKind.CONTINUE) { if (error) return null; @@ -1318,14 +1336,16 @@ StmtNode parseFlowStatement() { accept(TokenKind.SEMICOLON); ContinueStmtNode continueStmtNode = new ContinueStmtNode(); continueStmtNode.setFirstToken(tk); - return continueStmtNode; + return BreakContinueStatement.newContinue() + .setEnclosingLoop(context.forLoopStack.peek()); } else { if (error) return null; accept(TokenKind.BREAK); accept(TokenKind.SEMICOLON); - BreakStmtNode breakStmtNode = new BreakStmtNode(); - breakStmtNode.setFirstToken(tk); - return breakStmtNode; +// BreakStmtNode breakStmtNode = new BreakStmtNode(); +// breakStmtNode.setFirstToken(tk); + return BreakContinueStatement.newBreak() + .setEnclosingLoop(context.forLoopStack.peek()); } else { error = true; @@ -1336,7 +1356,7 @@ StmtNode parseFlowStatement() { /* Parse Loop Done */ /* Parse Collection */ - ExprNode parseList() { + ExpressionStatement parseList() { /* * List of Values * @@ -1347,14 +1367,16 @@ ExprNode parseList() { * return List */ if (error) return null; - List listNode = new ArrayList<>(); - ExprNode exp1, exp2; +// List listNode = new ArrayList<>(); + ListCollection listNode = ListCollection.list(); + ExpressionStatement exp1, exp2; exp1 = parseValue(); if (token.kind == TokenKind.RANGE) { - accept(TokenKind.RANGE); - exp2 = parseValue(); - accept(TokenKind.RSQU); - return new RangeNode(exp1, exp2, RangeNode.Type.LIST); + // TODO: to implement IR node implementation for RANGE + // accept(TokenKind.RANGE); + // exp2 = parseValue(); + // accept(TokenKind.RSQU); + // return new RangeNode(exp1, exp2, RangeNode.Type.LIST); } listNode.add(exp1); while (token.kind != TokenKind.RSQU) { @@ -1366,10 +1388,10 @@ ExprNode parseList() { } accept(TokenKind.RSQU); if (error) return null; - return new ListCollNode(listNode); + return listNode; } - ExprNode parseListCollection() { + ExpressionStatement parseListCollection() { /* * List of Collection * @@ -1384,10 +1406,12 @@ ExprNode parseListCollection() { if (token.kind == TokenKind.RSQU) { accept(TokenKind.RSQU); if (error) return null; - return new ListCollNode(); + return ListCollection.list(); } else if (token.kind == TokenKind.LOOP) { if (error) return null; - return parseComprehension("list"); // Accept Identifier Before + // TODO: implement IR node for Comprehension node + // return parseComprehension("list"); // Accept Identifier Before + return null; } else { if (error) return null; return parseList(); @@ -1406,7 +1430,7 @@ void parseMap() { // NOT Used by MapColl } - ExprNode parseMapCollection() { + ExpressionStatement parseMapCollection() { /* * List of MapCollection List of Comp * @@ -1416,93 +1440,95 @@ ExprNode parseMapCollection() { * Comp.add(parseForComprehension()) * */ - if (error) return null; - Map pairs = new LinkedHashMap<>(); - nextToken(); - accept(TokenKind.LSQU); - debug.add("Map Collection : " + token.kind); - while (token.kind != TokenKind.RSQU) { - if (error) - return null; - ExprNode exp2 = new MapCollNode(), exp1 = new MapCollNode(); - if (token.kind == TokenKind.COMMA) - nextToken(); - accept(TokenKind.LSQU); - if (token.kind == TokenKind.RSQU) { - pairs.put(exp1, exp2); - accept(TokenKind.RSQU); - accept(TokenKind.COMMA); - continue; - } else if (token.kind == TokenKind.LOOP) { - return parseComprehension("map"); // Accept Identifier Before - } else { - exp1 = parseValue(); - accept(TokenKind.COMMA); - exp2 = parseValue(); - accept(TokenKind.RSQU); - } - - pairs.put(exp1, exp2); - } - accept(TokenKind.RSQU); - if (error) return null; - return new MapCollNode(pairs); - } - - CompTypeNode parseComprehension(String type) { - if (error) return null; - IdenNode iden1, iden2 = null; - ExprNode exp = null; - BlockNode block = null; - ExprNode ifCond; - - CompTypeNode mapComp = type == "map" ? new MapCompNode() - : type == "list" ? new ListCompNode() - : type == "set" ? new SetCompNode() : type == "link" ? new LinkCompNode() : null; - - while (token.kind != TokenKind.RSQU) { - if (error) - return null; - switch (token.kind) { - case LOOP: - accept(TokenKind.LOOP); - iden1 = parseIdentifier(); - if (token.kind == TokenKind.COMMA) { - nextToken(); - iden2 = parseIdentifier(); - } - accept(TokenKind.IN); - exp = parseCollection(); - if (exp == null) { - if (token.kind == TokenKind.LCURLY) - exp = parseObjectCreation(); - else - try { - exp = parseAtomExpression(); - } catch (ParseException e) { - } - } - mapComp.addExpr(new CompLoopNode(iden1, iden2, exp)); - break; - case IF: - accept(TokenKind.IF); - accept(TokenKind.LPAREN); - ifCond = parseLogicalOrExpression(); - accept(TokenKind.RPAREN); - debug.add("Comprehension: " + token.kind); - mapComp.addExpr(new CompIfNode(ifCond)); - break; - default: - error = true; - accept(TokenKind.IDENTIFIER); - } - } - accept(TokenKind.RSQU); - if (error) return null; - return mapComp; - } - - ExprNode parseSet() { + // TODO: to implement Map IR node +// if (error) return null; +// Map pairs = new LinkedHashMap<>(); +// nextToken(); +// accept(TokenKind.LSQU); +// debug.add("Map Collection : " + token.kind); +// while (token.kind != TokenKind.RSQU) { +// if (error) +// return null; +// ExprNode exp2 = new MapCollNode(), exp1 = new MapCollNode(); +// if (token.kind == TokenKind.COMMA) +// nextToken(); +// accept(TokenKind.LSQU); +// if (token.kind == TokenKind.RSQU) { +// pairs.put(exp1, exp2); +// accept(TokenKind.RSQU); +// accept(TokenKind.COMMA); +// continue; +// } else if (token.kind == TokenKind.LOOP) { +// return parseComprehension("map"); // Accept Identifier Before +// } else { +// exp1 = parseValue(); +// accept(TokenKind.COMMA); +// exp2 = parseValue(); +// accept(TokenKind.RSQU); +// } +// +// pairs.put(exp1, exp2); +// } +// accept(TokenKind.RSQU); +// if (error) return null; +// return new MapCollNode(pairs); + } + + ExpressionStatement parseComprehension(String type) { +// if (error) return null; +// IdenNode iden1, iden2 = null; +// ExprNode exp = null; +// BlockNode block = null; +// ExprNode ifCond; +// +// CompTypeNode mapComp = type == "map" ? new MapCompNode() +// : type == "list" ? new ListCompNode() +// : type == "set" ? new SetCompNode() : type == "link" ? new LinkCompNode() : null; +// +// while (token.kind != TokenKind.RSQU) { +// if (error) +// return null; +// switch (token.kind) { +// case LOOP: +// accept(TokenKind.LOOP); +// iden1 = parseIdentifier(); +// if (token.kind == TokenKind.COMMA) { +// nextToken(); +// iden2 = parseIdentifier(); +// } +// accept(TokenKind.IN); +// exp = parseCollection(); +// if (exp == null) { +// if (token.kind == TokenKind.LCURLY) +// exp = parseObjectCreation(); +// else +// try { +// exp = parseAtomExpression(); +// } catch (ParseException e) { +// } +// } +// mapComp.addExpr(new CompLoopNode(iden1, iden2, exp)); +// break; +// case IF: +// accept(TokenKind.IF); +// accept(TokenKind.LPAREN); +// ifCond = parseLogicalOrExpression(); +// accept(TokenKind.RPAREN); +// debug.add("Comprehension: " + token.kind); +// mapComp.addExpr(new CompIfNode(ifCond)); +// break; +// default: +// error = true; +// accept(TokenKind.IDENTIFIER); +// } +// } +// accept(TokenKind.RSQU); +// if (error) return null; +// return mapComp; + return null; + } + + ExpressionStatement parseSet() { /* * List of Values * @@ -1512,30 +1538,31 @@ ExprNode parseSet() { * * return List */ - if (error) return null; - List setNode = new ArrayList<>(); - ExprNode exp1, exp2; - exp1 = parseValue(); - if (token.kind == TokenKind.RANGE) { - accept(TokenKind.RANGE); - exp2 = parseValue(); - accept(TokenKind.RSQU); - return new RangeNode(exp1, exp2, RangeNode.Type.SET); - } - setNode.add(exp1); - while (token.kind != TokenKind.RSQU) { - if (error) - return null; - accept(TokenKind.COMMA); - if (token.kind == TokenKind.RSQU) accept(TokenKind.IDENTIFIER); - setNode.add(parseValue()); - } - accept(TokenKind.RSQU); - if (error) return null; - return new SetCollNode(setNode); - } - - ExprNode parseSetCollection() { + // Not used +// if (error) return null; +// List setNode = new ArrayList<>(); +// ExprNode exp1, exp2; +// exp1 = parseValue(); +// if (token.kind == TokenKind.RANGE) { +// accept(TokenKind.RANGE); +// exp2 = parseValue(); +// accept(TokenKind.RSQU); +// return new RangeNode(exp1, exp2, RangeNode.Type.SET); +// } +// setNode.add(exp1); +// while (token.kind != TokenKind.RSQU) { +// if (error) +// return null; +// accept(TokenKind.COMMA); +// if (token.kind == TokenKind.RSQU) accept(TokenKind.IDENTIFIER); +// setNode.add(parseValue()); +// } +// accept(TokenKind.RSQU); +// if (error) return null; +// return new SetCollNode(setNode); + } + + ExpressionStatement parseSetCollection() { /* * List of Collection * @@ -1545,20 +1572,22 @@ ExprNode parseSetCollection() { * * */ - if (error) return null; - nextToken(); - accept(TokenKind.LSQU); - if (token.kind == TokenKind.RSQU) { - accept(TokenKind.RSQU); - if (error) return null; - return new SetCollNode(); - } else if (token.kind == TokenKind.LOOP) { - if (error) return null; - return parseComprehension("set"); // Accept Identifier Before - } else { - if (error) return null; - return parseSet(); - } + // TODO: to implement SET IR node +// if (error) return null; +// nextToken(); +// accept(TokenKind.LSQU); +// if (token.kind == TokenKind.RSQU) { +// accept(TokenKind.RSQU); +// if (error) return null; +// return new SetCollNode(); +// } else if (token.kind == TokenKind.LOOP) { +// if (error) return null; +// return parseComprehension("set"); // Accept Identifier Before +// } else { +// if (error) return null; +// return parseSet(); +// } + return null; } ExprNode parseLink() { @@ -1571,30 +1600,31 @@ ExprNode parseLink() { * * return List */ - if (error) return null; - List listNode = new ArrayList<>(); - ExprNode exp1, exp2; - exp1 = parseValue(); - if (token.kind == TokenKind.RANGE) { - accept(TokenKind.RANGE); - exp2 = parseValue(); - accept(TokenKind.RSQU); - return new RangeNode(exp1, exp2, RangeNode.Type.LINK); - } - listNode.add(exp1); - while (token.kind != TokenKind.RSQU) { - if (error) - return null; - accept(TokenKind.COMMA); - if (token.kind == TokenKind.RSQU) accept(TokenKind.IDENTIFIER); - listNode.add(parseValue()); - } - accept(TokenKind.RSQU); - if (error) return null; - return new LinkCollNode(listNode); - } - - ExprNode parseLinkCollection() { + // not Used +// if (error) return null; +// List listNode = new ArrayList<>(); +// ExprNode exp1, exp2; +// exp1 = parseValue(); +// if (token.kind == TokenKind.RANGE) { +// accept(TokenKind.RANGE); +// exp2 = parseValue(); +// accept(TokenKind.RSQU); +// return new RangeNode(exp1, exp2, RangeNode.Type.LINK); +// } +// listNode.add(exp1); +// while (token.kind != TokenKind.RSQU) { +// if (error) +// return null; +// accept(TokenKind.COMMA); +// if (token.kind == TokenKind.RSQU) accept(TokenKind.IDENTIFIER); +// listNode.add(parseValue()); +// } +// accept(TokenKind.RSQU); +// if (error) return null; +// return new LinkCollNode(listNode); + } + + ExpressionStatement parseLinkCollection() { /* * List of Collection * @@ -1604,23 +1634,26 @@ ExprNode parseLinkCollection() { * * */ - if (error) return null; - nextToken(); - accept(TokenKind.LSQU); - if (token.kind == TokenKind.RSQU) { - accept(TokenKind.RSQU); - if (error) return null; - return new LinkCollNode(); - } else if (token.kind == TokenKind.LOOP) { - if (error) return null; - return parseComprehension("link"); // Accept Identifier Before - } else { - if (error) return null; - return parseLink(); - } + // TODO: to implement LINK IR node +// if (error) return null; +// nextToken(); +// accept(TokenKind.LSQU); +// if (token.kind == TokenKind.RSQU) { +// accept(TokenKind.RSQU); +// if (error) return null; +// return new LinkCollNode(); +// } else if (token.kind == TokenKind.LOOP) { +// if (error) return null; +// return parseComprehension("link"); // Accept Identifier Before +// } else { +// if (error) return null; +// return parseLink(); +// } + + return null; } - ExprNode parseCollection() { + ExpressionStatement parseCollection() { /* * List of Collection * @@ -1630,7 +1663,7 @@ ExprNode parseCollection() { * return List */ if (error) return null; - ExprNode collExpr = null; + ExpressionStatement collExpr = null; switch (token.kind) { case LSQU: @@ -1798,8 +1831,9 @@ List parseVariable() { if (error) return null; Token tk = token; - IdenNode idenNode = parseIdentifier(); - ExprNode exp = null; + ExpressionStatement iden = parseIdentifier(); + ExpressionStatement exp = null; + Context ctx = Context.context; if (error) return null; if (token.kind == TokenKind.EQU) { @@ -1810,17 +1844,19 @@ List parseVariable() { accept(TokenKind.COMMA); } debug.add("Var Decl: " + exp); - VarDeclNode varDecl = new VarDeclNode(idenNode, exp); - varDecl.setFirstToken(tk); - varDeclNodes.add(varDecl); + SymbolReference sym = ctx.createSymbolReference(iden.getName(), Node.Tag.VARDECL); + DeclarativeAssignmentStatement stmt = DeclarativeAssignmentStatement.create(sym, exp); + varDeclNodes.add(stmt); +// VarDeclNode varDecl = new VarDeclNode(idenNode, exp); +// varDecl.setFirstToken(tk); +// varDeclNodes.add(varDecl); } accept(TokenKind.SEMICOLON); if (error) return null; return varDeclNodes; - } - List parseConstVariable() { + List parseConstVariable() { /* * List Variables checks grammar for variable Declaration * @@ -1830,20 +1866,23 @@ List parseConstVariable() { * return List */ if (error) return null; - List constDeclNodes = new ArrayList<>(); + List constDeclNodes = new ArrayList<>(); accept(TokenKind.CONST); + Context ctx = Context.context; while (token.kind != TokenKind.SEMICOLON) { if (error) return null; Token tk = token; - IdenNode idenNode = parseIdentifier(); + ExpressionStatement iden = parseIdentifier(); accept(TokenKind.EQU); - ExprNode exp = parseValue(); + ExpressionStatement exp = parseValue(); if (token.kind != TokenKind.SEMICOLON) accept(TokenKind.COMMA); - ConstDeclNode constDecl = new ConstDeclNode(idenNode, exp); - constDecl.setFirstToken(tk); - constDeclNodes.add(constDecl); + SymbolReference sym = ctx.createSymbolReference(iden.getName(), Tag.CONSTDECL); + DeclarativeAssignmentStatement stmt = DeclarativeAssignmentStatement.create(sym, exp); +// ConstDeclNode constDecl = new ConstDeclNode(idenNo, exp); +// constDecl.setFirstToken(tk); + constDeclNodes.add(stmt); } accept(TokenKind.SEMICOLON); if (error) return null; @@ -1863,7 +1902,7 @@ ParameterListNode parseParameter() { */ if (error) return null; boolean varArg = false; - List idenNodes = new ArrayList<>(); + List idenNodes = new ArrayList<>(); while (token.kind != TokenKind.RPAREN) { if (error) @@ -1959,27 +1998,28 @@ void parseBlockStatement() { } - StmtNode parseReturnStatement() { + CafeStatement parseReturnStatement() { if (error) return null; accept(TokenKind.RET); Token tk = token; - ExprNode exp = parseValue(); + ExpressionStatement exp = parseValue(); debug.add("Return : " + exp); accept(TokenKind.SEMICOLON); if (error) return null; - - ReturnStmtNode rtrnNode = new ReturnStmtNode(exp); - rtrnNode.setFirstToken(tk); - return rtrnNode; + ReturnStatement returnStatement = ReturnStatement.of(exp); +// rtrnNode.setFirstToken(tk); + return returnStatement; } - BlockNode parseLoopBlock() { + Block parseLoopBlock() { /* * parseBlockStatement() parseFlowStatement() */ if (error) return null; - List blockStats = new ArrayList<>(); - BlockNode blockNode = new BlockNode(); + List blockStats = new ArrayList<>(); + + Context context = Context.context; + Block block = context.enterScope(); while (token.kind != TokenKind.RCURLY) { if (error) return null; @@ -1989,32 +2029,34 @@ BlockNode parseLoopBlock() { blockStats.add(parseFlowStatement()); break; default: - List stm = parseBlock(); + List> stm = parseBlock(); if (stm == null) return null; blockStats.addAll(stm); + for (CafeStatement stmt: blockStats){ + block.add(stmt); + } } } if (error) return null; - blockNode.setStmt(blockStats); - return blockNode; + return block; } // return Block Statement Node - List parseBlock() { + List> parseBlock() { /* * List of block Statements calls parseBlockStatement */ if (error) return null; - List blockStmt = new ArrayList<>(); + List> blockStmt = new ArrayList<>(); debug.add("Token kind " + token.kind); switch (token.kind) { case VAR: - List stm = parseVariable(); + List stm = parseVariable(); if (stm == null) return null; blockStmt.addAll(stm); break; case CONST: - List stm1 = parseConstVariable(); + List stm1 = parseConstVariable(); if (stm1 == null) return null; blockStmt.addAll(stm1); break; @@ -2031,26 +2073,26 @@ List parseBlock() { case FOR: innerLoop = breakAllowed ? true : false; breakAllowed = true; - StmtNode stm3 = parseForStatement(); + CafeStatement stm3 = parseForStatement(); if (stm3 == null) return null; blockStmt.add(stm3); breakAllowed = innerLoop ? true : false; innerLoop = false; break; case LOOP: - StmtNode stm4 = parseLoopStatement(); + CafeStatement stm4 = parseLoopStatement(); if (stm4 == null) return null; blockStmt.add(stm4); break; case RET: - StmtNode stm5 = parseReturnStatement(); + CafeStatement stm5 = parseReturnStatement(); if (stm5 == null) return null; blockStmt.add(stm5); break; case IDENTIFIER: case THIS: case NULL: - StmtNode stm6 = parseExprStmt(); + CafeStatement stm6 = parseExprStmt(); if (stm6 == null) return null; blockStmt.add(stm6); debug.add("Block Stmt: " + token.kind); @@ -2058,7 +2100,7 @@ List parseBlock() { break; case BREAK: case CONTINUE: - StmtNode stm7 = parseFlowStatement(); + CafeStatement stm7 = parseFlowStatement(); blockStmt.add(stm7); debug.add("Block Stmt: " + token.kind); break; @@ -2089,7 +2131,7 @@ List parseExportStatement() { break; case VAR: - List stm = parseVariable(); + List stm = parseVariable(); if (stm == null) return null; for (VarDeclNode var : stm) { exportStmtNode.add(new ExportStmtNode(var.getIden(), var)); @@ -2188,7 +2230,7 @@ ProgramNode parseStatements() { nextToken(); debug.add("Stmt Node Token: " + token.kind); - List tree = new ArrayList<>(); + List> tree = new ArrayList<>(); while (token.kind != TokenKind.END) { if (error) return null; @@ -2204,7 +2246,7 @@ ProgramNode parseStatements() { tree.addAll(exports); break; default: - List stmt = parseBlock(); + List> stmt = parseBlock(); if (stmt == null) return null; tree.addAll(stmt); break; From e14884656bab2729c226c1440c6ee4a2b57997f1 Mon Sep 17 00:00:00 2001 From: SaurabhPethani Date: Sun, 4 Apr 2021 17:13:13 +0530 Subject: [PATCH 05/14] Converted to IR, yet to test --- .../java/compiler/ir/ExpressionStatement.java | 1 + src/main/java/compiler/parser/ParseIR.java | 459 ++++++++++++------ 2 files changed, 301 insertions(+), 159 deletions(-) diff --git a/src/main/java/compiler/ir/ExpressionStatement.java b/src/main/java/compiler/ir/ExpressionStatement.java index 3070e27..ba1f078 100644 --- a/src/main/java/compiler/ir/ExpressionStatement.java +++ b/src/main/java/compiler/ir/ExpressionStatement.java @@ -38,4 +38,5 @@ public static ExpressionStatement of(Object expr) { throw cantConvert("ExpressionStatement", expr); } + public abstract String getName(); } diff --git a/src/main/java/compiler/parser/ParseIR.java b/src/main/java/compiler/parser/ParseIR.java index e559ec5..cd12a87 100644 --- a/src/main/java/compiler/parser/ParseIR.java +++ b/src/main/java/compiler/parser/ParseIR.java @@ -43,6 +43,7 @@ import java.text.NumberFormat; import java.text.ParseException; import java.util.*; +import java.util.concurrent.locks.Condition; import static compiler.util.Log.Type.*; import static compiler.util.Messages.message; @@ -52,12 +53,19 @@ public class ParseIR extends Parser { ParserFactory.registerParser(ParserType.MAINPARSER, new ParseIR()); } + public CafeModule parseToIR(String moduleName) { + ParseIR.Context.context.createModule(moduleName); + ParseIR.Context.context.newObjectStack(); + parseStatements(); + return ParseIR.Context.context.module; + } + private static final class Context { final static ParseIR.Context context = new ParseIR.Context(); public CafeModule module; private final Deque referenceTableStack = new LinkedList<>(); - private final Deque functionStack = new LinkedList<>(); + private final Deque functionStack = new LinkedList<>(); private final Deque> objectStack = new LinkedList<>(); private final Deque forLoopStack = new LinkedList<>(); private final AnnFuncNameGenerator annFuncNameGenerator = new AnnFuncNameGenerator(); @@ -102,7 +110,7 @@ public void leaveScope() { referenceTableStack.pop(); } - public void enterFunc(Node.FuncNode n) { + public void enterFunc(String n) { isModuleScope = false; functionStack.push(n); annFuncNameGenerator.enter(); @@ -200,12 +208,7 @@ public void addFunction(CafeFunction function) { } } - public CafeModule parseToIR(String moduleName) { - ParseIR.Context.context.createModule(moduleName); - ParseIR.Context.context.newObjectStack(); - parse(); - return ParseIR.Context.context.module; - } + private Lexer lexer; @@ -701,12 +704,25 @@ ExpressionStatement parseTrailer(ExpressionStatement oExp) throws ParseException switch (token.kind) { case LPAREN: accept(TokenKind.LPAREN); - node = new FuncCallNode(oExp, new ArgsListNode(parseArgList())); + Context context = Context.context; + +// // eg: a.b() +// // b is a property of a, thus method invocation node is created. + if (context.isProperty()) { +// n.invokedOn.accept(this); + node = MethodInvocation.create(oExp, (List) parseArgList()); + } + else { + // eg: a() + // a() is normal function call, thus function invocation node is created. + node = FunctionInvocation.create(oExp, (List) parseArgList()); + } +// node = new FuncCallNode(oExp, new ArgsListNode(parseArgList())); accept(TokenKind.RPAREN); break; case LSQU: - ExprNode exp1, exp2; + ExpressionStatement exp1, exp2; debug.add("Atom Expr: " + token.kind); while (token.kind == TokenKind.LSQU) { if (error) @@ -721,10 +737,14 @@ ExpressionStatement parseTrailer(ExpressionStatement oExp) throws ParseException accept(TokenKind.COLON); exp2 = parseAtomExpression(); accept(TokenKind.RSQU); - node = new SliceNode(oExp, exp1, exp2); + node = SliceExpression.slice(oExp) + .beginsAt(exp1) + .endsAt(exp2); +// node = new SliceNode(oExp, exp1, exp2); } else { accept(TokenKind.RSQU); - node = new SubscriptNode(oExp, exp1); + node = SubscriptExpression.create(oExp, exp1); +// node = new SubscriptNode(oExp, exp1); oExp = node; } @@ -734,8 +754,8 @@ ExpressionStatement parseTrailer(ExpressionStatement oExp) throws ParseException ExpressionStatement e1; debug.add("Atom DOT:" + oExp); accept(TokenKind.DOT); - Context context = Context.context; - context.enterProperty(); + Context context1 = Context.context; + context1.enterProperty(); e1 = parseIdentifier(); if (error) return null; @@ -743,7 +763,7 @@ ExpressionStatement parseTrailer(ExpressionStatement oExp) throws ParseException while (token.kind != TokenKind.DOT && (token.kind == TokenKind.LSQU || token.kind == TokenKind.LPAREN)) { trail = parseTrailer(trail); } - context.leaveProperty(); + context1.leaveProperty(); if (trail == null) node = ObjectAccessStatement.create(oExp, e1); else @@ -999,12 +1019,12 @@ void parseSubscript() { */ } - List parseArgList() { + List parseArgList() { /* * parseArg() while(COMMA) parseArg() */ if (error) return null; - List args = new ArrayList<>(); + List args = new ArrayList<>(); while (token.kind != TokenKind.RPAREN) { if (error) return null; @@ -1040,10 +1060,10 @@ void parseExpressionStatement() { } /* parseStatements */ - IfStmtNode parseIf() { + ConditionalBranching parseIf() { if (error) return null; - ExprNode ifCond = null; - BlockNode ifBlock = new BlockNode(); + ExpressionStatement ifCond = null; + Block ifBlock = null; Token firstToken = token; @@ -1065,12 +1085,17 @@ IfStmtNode parseIf() { if (error) return null; - IfStmtNode ifNode = new IfStmtNode(ifCond, ifBlock); - ifNode.setFirstToken(firstToken); - return ifNode; + ConditionalBranching conditionalBranching = ConditionalBranching + .branch() + .condition(ifCond) + .whenTrue(ifBlock); + +// IfStmtNode ifNode = new IfStmtNode(ifCond, ifBlock); +// ifNode.setFirstToken(firstToken); + return conditionalBranching; } - StmtNode parseIfStatement() { + CafeStatement parseIfStatement() { /* * Parse If Statement and check if there any 'else' is there, if 'yes' then * parseIt and Break otherwise parse Else if statement and append to a list @@ -1078,26 +1103,24 @@ StmtNode parseIfStatement() { if (error) return null; - IfStmtNode ifNode; + ConditionalBranching ifNode; if ((ifNode = parseIf()) != null) { - StmtNode elseBlock = null; + CafeStatement elseBlock = null; if (token.kind == TokenKind.ELSE) { Token elseFT = token; nextToken(); if (token.kind == TokenKind.IF) { elseBlock = parseIfStatement(); } else if (accept(TokenKind.LCURLY)) { - BlockNode blockNode = new BlockNode(); - List stmt = new LinkedList<>(); + Block blockNode = null; while(token.kind != TokenKind.RCURLY) - stmt.addAll(parseBlock()); - blockNode.setStmt(stmt); + blockNode = parseBlock(); accept(TokenKind.RCURLY); - elseBlock = new ElseStmtNode(ifNode, blockNode); - elseBlock.setFirstToken(elseFT); + elseBlock = ifNode.elseBranch(blockNode); +// elseBlock.setFirstToken(elseFT); } if (error) return null; - ifNode.setElsePart(elseBlock); + ifNode.otherwise(elseBlock); } return ifNode; } @@ -1148,6 +1171,7 @@ StmtNode parseAssignmentStatement() { // accept(TokenKind.SEMICOLON); // if (error) return null; // return new AsgnStmtNode(exp1, exp); + return null; } /* Parse Loops */ @@ -1472,6 +1496,7 @@ ExpressionStatement parseMapCollection() { // accept(TokenKind.RSQU); // if (error) return null; // return new MapCollNode(pairs); + return null; } ExpressionStatement parseComprehension(String type) { @@ -1560,6 +1585,7 @@ ExpressionStatement parseSet() { // accept(TokenKind.RSQU); // if (error) return null; // return new SetCollNode(setNode); + return null; } ExpressionStatement parseSetCollection() { @@ -1622,6 +1648,7 @@ ExprNode parseLink() { // accept(TokenKind.RSQU); // if (error) return null; // return new LinkCollNode(listNode); + return null; } ExpressionStatement parseLinkCollection() { @@ -1698,7 +1725,7 @@ ExpressionStatement parseObjectCreation() { */ if (error) return null; - Map object = new LinkedHashMap<>(); + Map object = new LinkedHashMap<>(); ExpressionStatement idenNode; ExpressionStatement exprNode; @@ -1737,32 +1764,67 @@ void parseObject() { // Not Used */ } - ExprNode parseAnnFunction() { + AnonymousFunction parseAnnFunction() { /* * accept(FUNC) isAnnFunmc = true parseFunction() * */ if (error) return null; - List stmt = new ArrayList<>(); + Context context = Context.context; accept(TokenKind.FUNC); + String funcName = context.getNextAnnFuncName(); + context.enterFunc(funcName); accept(TokenKind.LPAREN); - ParameterListNode params = parseParameter(); + List params = parseParameter(); accept(TokenKind.RPAREN); accept(TokenKind.LCURLY); debug.add("Ann Func Node: " + token.kind); + List> stmt = new ArrayList<>(); while (token.kind != TokenKind.RCURLY) { if (error) return null; - List stm = parseBlock(); + Block stm = parseBlock(); if (stm == null) return null; - stmt.addAll(stm); + stmt.addAll(stm.getStatements()); } - debug.add("Ann Func Node: " + token.kind); accept(TokenKind.RCURLY); - BlockNode block = new BlockNode(); // BlockNode(stmt); - block.setStmt(stmt); + Block block = context.enterScope(); + for (CafeStatement stm: stmt) { + block.add(stm); + } + + if (!block.hasReturn()) + block.add(ReturnStatement.of(null)); + CafeFunction function = CafeFunction.function(funcName) + .block(block) + .withParameters(params); + if (context.isExport) + function = function.asExport(); + + context.addFunction(function); + + ExpressionStatement expression=null; + if (context.currentScope() == ParseIR.Context.Scope.CLOSURE) { + expression = function.asClosure(); + } else { + expression = FunctionWrapper.wrap(function); + } + context.leaveFunc(); +// while (token.kind != TokenKind.RCURLY) { +// if (error) +// return null; +// List stm = parseBlock(); +// if (stm == null) return null; +// stmt.addAll(stm); +// } +// debug.add("Ann Func Node: " + token.kind); +// accept(TokenKind.RCURLY); +// BlockNode block = new BlockNode(); // BlockNode(stmt); +// block.setStmt(stmt); + TargetFuncWrapper targetFuncWrapper = (TargetFuncWrapper) expression; + AnonymousFunction anonymousFunction = AnonymousFunction.func(targetFuncWrapper); if (error) return null; - return new AnnFuncNode(params, block); + return anonymousFunction; } ExpressionStatement parseValue() { @@ -1890,7 +1952,7 @@ List parseConstVariable() { } - ParameterListNode parseParameter() { + List parseParameter() { /* * List of Arguments * @@ -1902,30 +1964,28 @@ ParameterListNode parseParameter() { */ if (error) return null; boolean varArg = false; - List idenNodes = new ArrayList<>(); + List idenNodes = new ArrayList<>(); while (token.kind != TokenKind.RPAREN) { if (error) return null; debug.add("ARG List: " + token.kind); debug.add("ARG List: " + token.value()); - if (error) - return null; + if (token.kind == TokenKind.VARARGS) { accept(TokenKind.VARARGS); varArg = true; - idenNodes.add(parseIdentifier()); + idenNodes.add(parseIdentifier().getName()); // accept(TokenKind.RPAREN); break; } - idenNodes.add(parseIdentifier()); + idenNodes.add(parseIdentifier().getName()); if (TokenKind.RPAREN != token.kind) accept(TokenKind.COMMA); } if (error) return null; - return new ParameterListNode(idenNodes, varArg); - + return idenNodes; } // ExprNode parseFunctionCall(){ @@ -1933,7 +1993,7 @@ ParameterListNode parseParameter() { // } - DeclNode parseFunctionDeclaration() { + DeclarativeAssignmentStatement parseFunctionDeclaration() { /* * List of Parameter BlockNode * @@ -1943,29 +2003,55 @@ DeclNode parseFunctionDeclaration() { * FunctionNode(name, parameter, BlockNode); returns FunctionNode */ if (error) return null; + Context context = Context.context; accept(TokenKind.FUNC); Token tk = token; - ExprNode funcName = parseIdentifier(); + ExpressionStatement funcName = parseIdentifier(); + context.enterFunc(funcName.getName()); accept(TokenKind.LPAREN); - ParameterListNode arg = parseParameter(); + List arg = parseParameter(); accept(TokenKind.RPAREN); accept(TokenKind.LCURLY); - List stmt = new ArrayList<>(); + List> stmt = new ArrayList<>(); while (token.kind != TokenKind.RCURLY) { if (error) return null; - List stm = parseBlock(); + Block stm = parseBlock(); if (stm == null) return null; - stmt.addAll(stm); + stmt.addAll(stm.getStatements()); } accept(TokenKind.RCURLY); - BlockNode block = new BlockNode(); - block.setStmt(stmt); + Block block = context.enterScope(); + for (CafeStatement stm: stmt) { + block.add(stm); + } + + if (!block.hasReturn()) + block.add(ReturnStatement.of(null)); + CafeFunction function = CafeFunction.function(funcName.getName()) + .block(block) + .withParameters(arg); + if (context.isExport) + function = function.asExport(); + + context.addFunction(function); + + ExpressionStatement expression=null; + if (context.currentScope() == ParseIR.Context.Scope.CLOSURE) { + expression = function.asClosure(); + } else { + expression = FunctionWrapper.wrap(function); + } + context.leaveFunc(); + SymbolReference ref = context.createSymbolReference(funcName.getName(), Node.Tag.VARDECL); + DeclarativeAssignmentStatement statement = DeclarativeAssignmentStatement.create(ref, expression); +// BlockNode block = new BlockNode(); +// block.setStmt(stmt); if (error) return null; - FuncDeclNode funcDecl = new FuncDeclNode((IdenNode) funcName, arg, block); - funcDecl.setFirstToken(tk); - return funcDecl; +// FuncDeclNode funcDecl = new FuncDeclNode((IdenNode) funcName, arg, block); +// funcDecl.setFirstToken(tk); + return statement; } void parseDeclarativeStatement() { @@ -2029,12 +2115,13 @@ Block parseLoopBlock() { blockStats.add(parseFlowStatement()); break; default: - List> stm = parseBlock(); - if (stm == null) return null; - blockStats.addAll(stm); - for (CafeStatement stmt: blockStats){ - block.add(stmt); - } + block = parseBlock(); +// List> stm = parseBlock(); +// if (stm == null) return null; +// blockStats.addAll(stm); +// for (CafeStatement stmt: blockStats){ +// block.add(stmt); +// } } } if (error) return null; @@ -2042,90 +2129,120 @@ Block parseLoopBlock() { } // return Block Statement Node - List> parseBlock() { + Block parseBlock() { /* * List of block Statements calls parseBlockStatement */ if (error) return null; - List> blockStmt = new ArrayList<>(); + + Context context = Context.context; + Block block = context.enterScope(); + +// for (Node.StmtNode stmt : n.block) { +// stmt.accept(this); +// CafeStatement statement = (CafeStatement) context.pop(); +// block.add(statement); +// } +// context.push(block); + +// List> blockStmt = new ArrayList<>(); debug.add("Token kind " + token.kind); - switch (token.kind) { - case VAR: - List stm = parseVariable(); - if (stm == null) return null; - blockStmt.addAll(stm); - break; - case CONST: - List stm1 = parseConstVariable(); - if (stm1 == null) return null; - blockStmt.addAll(stm1); - break; - case FUNC: - DeclNode decl = parseFunctionDeclaration(); - if (decl == null) return null; - blockStmt.add(decl); - break; - case IF: - StmtNode stm2 = parseIfStatement(); - if (stm2 == null) return null; - blockStmt.add(stm2); - break; - case FOR: - innerLoop = breakAllowed ? true : false; - breakAllowed = true; - CafeStatement stm3 = parseForStatement(); - if (stm3 == null) return null; - blockStmt.add(stm3); - breakAllowed = innerLoop ? true : false; - innerLoop = false; - break; - case LOOP: - CafeStatement stm4 = parseLoopStatement(); - if (stm4 == null) return null; - blockStmt.add(stm4); - break; - case RET: - CafeStatement stm5 = parseReturnStatement(); - if (stm5 == null) return null; - blockStmt.add(stm5); - break; - case IDENTIFIER: - case THIS: - case NULL: - CafeStatement stm6 = parseExprStmt(); - if (stm6 == null) return null; - blockStmt.add(stm6); - debug.add("Block Stmt: " + token.kind); - accept(TokenKind.SEMICOLON); - break; - case BREAK: - case CONTINUE: - CafeStatement stm7 = parseFlowStatement(); - blockStmt.add(stm7); - debug.add("Block Stmt: " + token.kind); - break; - default: - logError(TokenKind.IDENTIFIER); - } + + switch (token.kind) { + case VAR: + List stm = parseVariable(); + if (stm == null) return null; + for (CafeStatement st : stm) { + block.add(st); + } + break; + case CONST: + List stm1 = parseConstVariable(); + if (stm1 == null) return null; + for (CafeStatement st : stm1) { + block.add(st); + } + break; + case FUNC: + DeclarativeAssignmentStatement decl = parseFunctionDeclaration(); + if (decl == null) return null; + block.add(decl); + break; + case IF: + CafeStatement stm2 = parseIfStatement(); + if (stm2 == null) return null; + block.add(stm2); + break; + case FOR: + innerLoop = breakAllowed ? true : false; + breakAllowed = true; + CafeStatement stm3 = parseForStatement(); + if (stm3 == null) return null; + block.add(stm3); + breakAllowed = innerLoop ? true : false; + innerLoop = false; + break; + case LOOP: + CafeStatement stm4 = parseLoopStatement(); + if (stm4 == null) return null; + block.add(stm4); + break; + case RET: + CafeStatement stm5 = parseReturnStatement(); + if (stm5 == null) return null; + block.add(stm5); + break; + case IDENTIFIER: + case THIS: + case NULL: + CafeStatement stm6 = parseExprStmt(); + if (stm6 == null) return null; + block.add(stm6); + debug.add("Block Stmt: " + token.kind); + accept(TokenKind.SEMICOLON); + break; + case BREAK: + case CONTINUE: + CafeStatement stm7 = parseFlowStatement(); + block.add(stm7); + debug.add("Block Stmt: " + token.kind); + break; + default: + logError(TokenKind.IDENTIFIER); + } + context.leaveScope(); if (error) return null; - debug.add("Block Stmt: " + blockStmt); - return blockStmt; + debug.add("Block Stmt: " + block); + return block; } - List parseExportStatement() { - List exportStmtNode = new ArrayList(); + List parseExportStatement() { + List exportStmtNode = new ArrayList(); + + Context context = Context.context; + context.isExport = true; +// String name = n.iden.name; +// CafeExport export = CafeExport.export(name); +// context.module.addExport(export); +// if (n.node == null) { +// // Just pushing to avoid error +// context.push(export); +// } else { +// n.node.accept(this); +// } + accept(TokenKind.EXPORT); switch (token.kind) { case IDENTIFIER: - IdenNode id = parseIdentifier(); + ExpressionStatement id = parseIdentifier(); if (id == null) return null; - exportStmtNode.add(new ExportStmtNode(id, null)); + exportStmtNode.add(CafeExport.export(id.getName())); while (token.kind == TokenKind.COMMA) { accept(TokenKind.COMMA); id = parseIdentifier(); if (id == null) return null; - exportStmtNode.add(new ExportStmtNode(id, null)); + exportStmtNode.add(CafeExport.export(id.getName())); } accept(TokenKind.SEMICOLON); break; @@ -2133,31 +2250,36 @@ List parseExportStatement() { case VAR: List stm = parseVariable(); if (stm == null) return null; - for (VarDeclNode var : stm) { - exportStmtNode.add(new ExportStmtNode(var.getIden(), var)); + for (DeclarativeAssignmentStatement var : stm) { + exportStmtNode.add(CafeExport.export(var.getSymbolReference().getName())); } break; case CONST: - List stm1 = parseConstVariable(); + List stm1 = parseConstVariable(); if (stm1 == null) return null; - for (ConstDeclNode var : stm1) { - exportStmtNode.add(new ExportStmtNode(var.getIden(), var)); + for (DeclarativeAssignmentStatement var : stm1) { + exportStmtNode.add(CafeExport.export(var.getSymbolReference().getName())); } break; case FUNC: - DeclNode decl = parseFunctionDeclaration(); + DeclarativeAssignmentStatement decl = parseFunctionDeclaration(); if (decl == null) return null; - exportStmtNode.add(new ExportStmtNode(decl.getIden(), decl)); + exportStmtNode.add(CafeExport.export(decl.getName())); break; default: error = true; } + + context.isExport = false; + for(CafeExport expo : exportStmtNode){ + context.module.addExport(expo); + } if (error) return null; return exportStmtNode; } // return Import Statement Node - ImportStmtNode parseImportStatement() { + CafeImport parseImportStatement() { /* List of Imports */ // accept('@'); @@ -2165,8 +2287,18 @@ ImportStmtNode parseImportStatement() { // if(valid) return ImportStatement(token.value()) // else Throw Error ImportStmtNode importStmtNode = null; - Map blocks = new HashMap(); - IdenNode id1, id2 = null; + Map blocks = new HashMap(); + ExpressionStatement id1, id2 = null; + Context context = Context.context; +// +// for (Map.Entry entry : n.importAliasMap.entrySet()) { +// Node.IdenNode value = entry.getValue(); +// String alias = null; +// if (value != null) +// alias = value.name; +// cafeImport.add(entry.getKey().name, alias); +// } + accept(TokenKind.IMPORT); if(token.kind == TokenKind.IDENTIFIER){ @@ -2175,7 +2307,7 @@ ImportStmtNode parseImportStatement() { accept(token.kind); id2 = parseIdentifier(); } - blocks.put(id1, id2); + blocks.put(id1.getName(), id2 != null ? id2.getName() : null); while (token.kind == TokenKind.COMMA) { accept(TokenKind.COMMA); id1 = parseIdentifier(); @@ -2184,14 +2316,14 @@ ImportStmtNode parseImportStatement() { accept(token.kind); id2 = parseIdentifier(); } - blocks.put(id1, id2); + blocks.put(id1.getName(), id2 != null ? id2.getName() : null); } } else { if(accept(TokenKind.MUL)) { - id1 = new IdenNode("*"); +// id1 = new IdenNode("*"); accept(TokenKind.AS); id2 = parseIdentifier(); - blocks.put(id1, id2); + blocks.put("*", id2.getName()); } } @@ -2202,17 +2334,22 @@ ImportStmtNode parseImportStatement() { // logError(INVALID_IMPORT_FILE, token.value()); // error = true; // } else { - importStmtNode = new ImportStmtNode(blocks, token.value()); + + CafeImport cafeImport = CafeImport.of(token.value()); +// importStmtNode = new ImportStmtNode(blocks, token.value()); nextToken(); accept(TokenKind.SEMICOLON); - - return importStmtNode; + for (Map.Entry entry : blocks.entrySet()) { + cafeImport.add(entry.getKey(), entry.getValue()); + } + context.module.addImport(cafeImport); + return cafeImport; } // return Statement Node // return List Of Statements - ProgramNode parseStatements() { + CafeModule parseStatements() { /* * List of Statement stats * @@ -2236,25 +2373,29 @@ ProgramNode parseStatements() { return null; switch (token.kind) { case IMPORT: - ImportStmtNode importStmtNode = parseImportStatement(); + CafeImport importStmtNode = parseImportStatement(); if (importStmtNode == null) return null; tree.add(importStmtNode); break; case EXPORT: - List exports = parseExportStatement(); + List exports = parseExportStatement(); if(exports == null) return null; tree.addAll(exports); break; default: - List> stmt = parseBlock(); + Block stmt = parseBlock(); if (stmt == null) return null; - tree.addAll(stmt); + tree.addAll(stmt.getStatements()); break; } } if (error) return null; debug.add("Block Statements " + tree); - return new ProgramNode(tree); + for( CafeStatement stm: tree){ + Context.context.module.add(stm); + } +// return new ProgramNode(tree); + return Context.context.module; } // void parseStatementAsBlock() { @@ -2268,7 +2409,7 @@ ProgramNode parseProgram() { // node = parseStatements() // return node if (error) return null; - ProgramNode node = parseStatements(); + ProgramNode node =null; if (error) return null; return node; } From cbbfcaaec871ea31a74d972101ceeee4193670ff Mon Sep 17 00:00:00 2001 From: SaurabhPethani Date: Mon, 5 Apr 2021 11:34:32 +0530 Subject: [PATCH 06/14] ParseIR ready, yet to handle 2 errors --- .../java/compiler/ir/ExpressionStatement.java | 4 +- .../java/compiler/ir/FunctionInvocation.java | 2 +- .../java/compiler/ir/MethodInvocation.java | 2 +- src/main/java/compiler/main/CafeCompiler.java | 20 +- src/main/java/compiler/parser/MainParser.java | 7 + src/main/java/compiler/parser/ParseIR.java | 537 +++++++++--------- src/main/java/compiler/parser/Parser.java | 3 +- src/main/java/compiler/parser/ParserType.java | 2 +- .../java/compiler/parser/MainParserTest.java | 13 +- 9 files changed, 291 insertions(+), 299 deletions(-) diff --git a/src/main/java/compiler/ir/ExpressionStatement.java b/src/main/java/compiler/ir/ExpressionStatement.java index ba1f078..16dd727 100644 --- a/src/main/java/compiler/ir/ExpressionStatement.java +++ b/src/main/java/compiler/ir/ExpressionStatement.java @@ -38,5 +38,7 @@ public static ExpressionStatement of(Object expr) { throw cantConvert("ExpressionStatement", expr); } - public abstract String getName(); + public String getName(){ + return null; + } } diff --git a/src/main/java/compiler/ir/FunctionInvocation.java b/src/main/java/compiler/ir/FunctionInvocation.java index da0dfd6..a59d9d4 100644 --- a/src/main/java/compiler/ir/FunctionInvocation.java +++ b/src/main/java/compiler/ir/FunctionInvocation.java @@ -47,7 +47,7 @@ public ExpressionStatement getReference() { return ref; } - public static FunctionInvocation create(Object ref, List args) { + public static FunctionInvocation create(Object ref, List> args) { List> arguments = new LinkedList<>(); for (Object arg : args) { if (arg instanceof CafeElement) { diff --git a/src/main/java/compiler/ir/MethodInvocation.java b/src/main/java/compiler/ir/MethodInvocation.java index 44a5e42..90b802c 100644 --- a/src/main/java/compiler/ir/MethodInvocation.java +++ b/src/main/java/compiler/ir/MethodInvocation.java @@ -41,7 +41,7 @@ private MethodInvocation(ExpressionStatement invokedUpon, List this.arguments = arguments; } - public static MethodInvocation create(Object invokedOn, List args) { + public static MethodInvocation create(Object invokedOn, List> args) { List> arguments = new LinkedList<>(); for (Object arg : args) { if (arg instanceof CafeElement) { diff --git a/src/main/java/compiler/main/CafeCompiler.java b/src/main/java/compiler/main/CafeCompiler.java index 4bff4a1..3aac8d9 100644 --- a/src/main/java/compiler/main/CafeCompiler.java +++ b/src/main/java/compiler/main/CafeCompiler.java @@ -85,9 +85,8 @@ public CafeCompiler(String sourceFile) { enum Phase { INIT, - PARSE, + PARSEIR, ANALYZE, - IR, GEN } @@ -114,20 +113,21 @@ public CompilerResult compile() { case INIT: fileManager.setSourceFile(sourceFile); break; - case PARSE: + case PARSEIR: parser = parserFactory.newParser(ParserType.MAINPARSER, fileManager.asCharList()); - programNode = parser.parse(); + module = parser.parseToIR(moduleName); + System.out.println("Success "+module); break; case ANALYZE: // System.out.println((char) 27 + "[33m" + "\nPrettyPrint"); // new PrettyPrinter().prettyPrint(programNode); - analyzer.visitProgram((ProgramNode) programNode); - break; - case IR: - module = new ASTToCafeIrVisitor().transform((ProgramNode) programNode, moduleName); - module.accept(new ClosureReferenceVisitor()); - module.accept(new SymbolReferenceAssignmentVisitor()); +// analyzer.visitProgram((ProgramNode) programNode); break; +// case IR: +// module = new ASTToCafeIrVisitor().transform((ProgramNode) programNode, moduleName); +// module.accept(new ClosureReferenceVisitor()); +// module.accept(new SymbolReferenceAssignmentVisitor()); +// break; case GEN: byteCode = new JVMByteCodeGenVisitor().generateByteCode(module, moduleName); result.ok(byteCode); diff --git a/src/main/java/compiler/parser/MainParser.java b/src/main/java/compiler/parser/MainParser.java index d0922d3..7435a05 100644 --- a/src/main/java/compiler/parser/MainParser.java +++ b/src/main/java/compiler/parser/MainParser.java @@ -31,6 +31,8 @@ import compiler.ast.Node; import compiler.ast.Node.*; +import compiler.ir.CafeModule; +import compiler.ir.ReferenceTable; import compiler.parser.Tokens.Token; import compiler.parser.Tokens.TokenKind; import compiler.util.Log; @@ -48,6 +50,11 @@ public class MainParser extends Parser { ParserFactory.registerParser(ParserType.MAINPARSER, new MainParser()); } + @Override + public CafeModule parseToIR(String mod){ + return CafeModule.create(mod, new ReferenceTable()); + } + private Lexer lexer; private Token token; private Log log; diff --git a/src/main/java/compiler/parser/ParseIR.java b/src/main/java/compiler/parser/ParseIR.java index cd12a87..118d9e9 100644 --- a/src/main/java/compiler/parser/ParseIR.java +++ b/src/main/java/compiler/parser/ParseIR.java @@ -31,19 +31,16 @@ import compiler.ast.Node; import compiler.ast.Node.*; -import compiler.gen.ASTToCafeIrVisitor; import compiler.gen.AnnFuncNameGenerator; import compiler.ir.*; import compiler.parser.Tokens.Token; import compiler.parser.Tokens.TokenKind; import compiler.util.Log; import compiler.util.Position; -import jdk.nashorn.internal.ir.Assignment; import java.text.NumberFormat; import java.text.ParseException; import java.util.*; -import java.util.concurrent.locks.Condition; import static compiler.util.Log.Type.*; import static compiler.util.Messages.message; @@ -100,6 +97,7 @@ public CafeModule createModule(String moduleName) { } public Block enterScope() { + assert referenceTableStack.peek() != null; ReferenceTable blockReferenceTable = referenceTableStack.peek() .fork(); referenceTableStack.push(blockReferenceTable); @@ -153,11 +151,13 @@ public void push(Object object) { if (objectStack.isEmpty()) { newObjectStack(); } + assert objectStack.peek() != null; objectStack.peek() .push(object); } public Object pop() { + assert objectStack.peek() != null; return objectStack.peek() .pop(); } @@ -168,6 +168,7 @@ public Object peek() { SymbolReference createSymbolReference(String name, SymbolReference.Kind kind, SymbolReference.Scope scope) { SymbolReference ref = SymbolReference.of(name, kind, scope); + assert referenceTableStack.peek() != null; referenceTableStack.peek() .add(ref); return ref; @@ -178,6 +179,7 @@ public SymbolReference createSymbolReference(String name, Node.Tag tag) { } public SymbolReference getReference(String name) { + assert referenceTableStack.peek() != null; return referenceTableStack.peek() .get(name); } @@ -215,7 +217,7 @@ public void addFunction(CafeFunction function) { private Token token; private Log log; private boolean breakAllowed = false, innerLoop = false, error = false; - private List debug = new ArrayList<>(); + private final List debug = new ArrayList<>(); private ParseIR() { } @@ -225,7 +227,7 @@ private ParseIR(ParserFactory factory, Lexer lexer) { this.lexer = lexer; this.log = factory.log; // TESTING - // nextToken(); +// nextToken(); // while(token.kind != TokenKind.END) { // debug.add(token.kind); // nextToken(); @@ -283,26 +285,26 @@ private void logError(TokenKind tokenKindExpected) { errorDescription(token.pos, message(SYMBOL_EXPECTED, tokenKindExpected, token.kind))); } - private void logError(Log.Type issue, Object... values) { + private void logError(Object... values) { error = true; - log.report(issue, token.pos, - errorDescription(token.pos, message(issue, values))); + log.report(Log.Type.INVALID_EXPRESSION, token.pos, + errorDescription(token.pos, message(Log.Type.INVALID_EXPRESSION, values))); } - ExpressionStatement parseLogicalOrExpression() { + ExpressionStatement parseLogicalOrExpression() { /* * parseLogicalAndExpression() while(TokenType == OR | TokenType == '||'){ * parseLogicalAndExpression() } */ if (error) return null; - ExpressionStatement exp1 = parseLogicalAndExpression(); + ExpressionStatement exp1 = parseLogicalAndExpression(); while (token.kind == TokenKind.OROP || token.kind == TokenKind.OR) { if (error) return null; - String op = token.value(); +// String op = token.value(); accept(token.kind); - ExpressionStatement exp2 = parseLogicalAndExpression(); + ExpressionStatement exp2 = parseLogicalAndExpression(); // exp1 = new BinaryExprNode(exp1, exp2, op); exp1 = BinaryExpression.of(OperatorType.OR) .right(exp2) @@ -313,19 +315,19 @@ ExpressionStatement parseLogicalOrExpression() { } - ExpressionStatement parseLogicalAndExpression() { + ExpressionStatement parseLogicalAndExpression() { /* * parseLogicalNotExpression() while(TokenType == AND | TokenType == '&&'){ * parseLogicalNotExpression() } */ if (error) return null; - ExpressionStatement exp1 = parseLogicalNotExpression(); + ExpressionStatement exp1 = parseLogicalNotExpression(); while (token.kind == TokenKind.ANDOP || token.kind == TokenKind.AND) { if (error) return null; - String op = token.value(); +// String op = token.value(); accept(token.kind); - ExpressionStatement exp2 = parseLogicalNotExpression(); + ExpressionStatement exp2 = parseLogicalNotExpression(); exp1 = BinaryExpression.of(OperatorType.AND) .right(exp2) .left(exp1); @@ -335,19 +337,19 @@ ExpressionStatement parseLogicalAndExpression() { return exp1; } - ExpressionStatement parseLogicalNotExpression() { + ExpressionStatement parseLogicalNotExpression() { /* * parseNotEqualToExpression() while(TokenType == NOT | TokenType == '!'){ * parseNotEqualToExpression() } */ if (error) return null; - ExpressionStatement exp1 = parseNotEqualToExpression(); + ExpressionStatement exp1 = parseNotEqualToExpression(); while (token.kind == TokenKind.NOTOP || token.kind == TokenKind.NOT) { if (error) return null; - String op = token.value(); +// String op = token.value(); accept(token.kind); - ExpressionStatement exp2 = parseNotEqualToExpression(); + ExpressionStatement exp2 = parseNotEqualToExpression(); exp1 = BinaryExpression.of(OperatorType.NOT) .right(exp2) .left(exp1); @@ -357,19 +359,19 @@ ExpressionStatement parseLogicalNotExpression() { return exp1; } - ExpressionStatement parseNotEqualToExpression() { + ExpressionStatement parseNotEqualToExpression() { /* * parseEqualEqualExpression() accept(NOT_EQ) while(TokenType == '!='){ * parseEqualEqualExpression() } */ if (error) return null; - ExpressionStatement exp1 = parseEqualEqualExpression(); + ExpressionStatement exp1 = parseEqualEqualExpression(); while (token.kind == TokenKind.NOTEQU) { if (error) return null; - String op = token.value(); +// String op = token.value(); accept(token.kind); - ExpressionStatement exp2 = parseEqualEqualExpression(); + ExpressionStatement exp2 = parseEqualEqualExpression(); exp1 = BinaryExpression.of(OperatorType.NOTEQUALS) .right(exp2) .left(exp1); @@ -379,19 +381,19 @@ ExpressionStatement parseNotEqualToExpression() { return exp1; } - ExpressionStatement parseEqualEqualExpression() { + ExpressionStatement parseEqualEqualExpression() { /* * parseRealtionalExpression() while(TokenType == '=='){ * parseRealtionalExpression() } */ if (error) return null; - ExpressionStatement exp1 = parseRelationalExpression(); + ExpressionStatement exp1 = parseRelationalExpression(); while (token.kind == TokenKind.EQUEQU) { if (error) return null; - String op = token.value(); +// String op = token.value(); accept(token.kind); - ExpressionStatement exp2 = parseRelationalExpression(); + ExpressionStatement exp2 = parseRelationalExpression(); exp1 = BinaryExpression.of(OperatorType.EQUALS) .right(exp2) .left(exp1); @@ -401,14 +403,14 @@ ExpressionStatement parseEqualEqualExpression() { return exp1; } - ExpressionStatement parseRelationalExpression() { + ExpressionStatement parseRelationalExpression() { /* * parseBitOrExpression() while(TokenType == <,>,<=,>=,in ,not in, is, is not ){ * parseBitOrExpression() } */ if (error) return null; - Token tk = token; - ExpressionStatement exp1 = parseBitOrExpression(); +// Token tk = token; + ExpressionStatement exp1 = parseBitOrExpression(); if (exp1 == null) { error = true; return null; @@ -418,7 +420,7 @@ ExpressionStatement parseRelationalExpression() { || token.kind == TokenKind.NOT) { if (error) return null; - OperatorType op = token.kind == TokenKind.LT ? OperatorType.LESS : token.kind == TokenKind.GT ? OperatorType.MORE : token.kind == TokenKind.LTE ? OperatorType.LESSOREQUALS : token.kind == TokenKind.GTE ? OperatorType.MOREOREQUALS : token.kind == TokenKind.IN ? OperatorType.IN : token.kind == TokenKind.IS ? OperatorType.IS : token.kind == TokenKind.NOT ? OperatorType.NOT : null; + OperatorType op = token.kind == TokenKind.LT ? OperatorType.LESS : token.kind == TokenKind.GT ? OperatorType.MORE : token.kind == TokenKind.LTE ? OperatorType.LESSOREQUALS : token.kind == TokenKind.GTE ? OperatorType.MOREOREQUALS : token.kind == TokenKind.IN ? OperatorType.IN : token.kind == TokenKind.IS ? OperatorType.IS : OperatorType.NOT; if (token.kind == TokenKind.IS) { accept(token.kind); op = OperatorType.IS; @@ -433,7 +435,7 @@ ExpressionStatement parseRelationalExpression() { accept(TokenKind.IN); } } - ExpressionStatement exp2 = parseBitOrExpression(); + ExpressionStatement exp2 = parseBitOrExpression(); exp1 = BinaryExpression.of(op) .right(exp2) .left(exp1); @@ -443,19 +445,19 @@ ExpressionStatement parseRelationalExpression() { return exp1; } - ExpressionStatement parseBitOrExpression() { + ExpressionStatement parseBitOrExpression() { /* * parseBitXorExpression() while(TokenType == '|'){ parseBitXorExpression() } */ if (error) return null; - ExpressionStatement exp1 = parseBitXorExpression(); + ExpressionStatement exp1 = parseBitXorExpression(); while (token.kind == TokenKind.BITOR) { if (error) return null; - String op = token.value(); +// String op = token.value(); accept(token.kind); - ExpressionStatement exp2 = parseBitXorExpression(); + ExpressionStatement exp2 = parseBitXorExpression(); exp1 = BinaryExpression.of(OperatorType.BITOR) .right(exp2) .left(exp1); @@ -465,19 +467,19 @@ ExpressionStatement parseBitOrExpression() { return exp1; } - ExpressionStatement parseBitXorExpression() { + ExpressionStatement parseBitXorExpression() { /* * parseLogicalAndExpression() while(TokenType == '^'){ * parseLogicalAndExpression() } */ if (error) return null; - ExpressionStatement exp1 = parseBitAndExpression(); + ExpressionStatement exp1 = parseBitAndExpression(); while (token.kind == TokenKind.BITAND) { if (error) return null; String op = token.value(); accept(token.kind); - ExpressionStatement exp2 = parseBitAndExpression(); + ExpressionStatement exp2 = parseBitAndExpression(); exp1 = BinaryExpression.of(op) .right(exp2) .left(exp1); @@ -487,19 +489,19 @@ ExpressionStatement parseBitXorExpression() { return exp1; } - ExpressionStatement parseBitAndExpression() { + ExpressionStatement parseBitAndExpression() { /* * parseBitRightShiftExpression() while(TokenType == '&'){ * parseBitRightShiftExpression() } */ if (error) return null; - ExpressionStatement exp1 = parseBitRightShiftExpression(); + ExpressionStatement exp1 = parseBitRightShiftExpression(); while (token.kind == TokenKind.ANDOP) { if (error) return null; - String op = token.value(); +// String op = token.value(); accept(token.kind); - ExpressionStatement exp2 = parseBitRightShiftExpression(); + ExpressionStatement exp2 = parseBitRightShiftExpression(); exp1 = BinaryExpression.of(OperatorType.AND) .right(exp2) .left(exp1); @@ -509,19 +511,19 @@ ExpressionStatement parseBitAndExpression() { return exp1; } - ExpressionStatement parseBitRightShiftExpression() { + ExpressionStatement parseBitRightShiftExpression() { /* * parseBitLeftShiftExpression() while(TokenType == '>>' | TokenType == '>>>'){ * parseBitLeftShiftExpression() } */ if (error) return null; - ExpressionStatement exp1 = parseBitLeftShiftExpression(); + ExpressionStatement exp1 = parseBitLeftShiftExpression(); while (token.kind == TokenKind.RSHIFT || token.kind == TokenKind.TRSHIFT) { if (error) return null; OperatorType op = token.kind == TokenKind.RSHIFT ? OperatorType.BITRIGHTSHIFT_SIGNED : OperatorType.BITRIGHTSHIFT_UNSIGNED; accept(token.kind); - ExpressionStatement exp2 = parseBitLeftShiftExpression(); + ExpressionStatement exp2 = parseBitLeftShiftExpression(); exp1 = BinaryExpression.of(op) .right(exp2) .left(exp1); @@ -531,19 +533,19 @@ ExpressionStatement parseBitRightShiftExpression() { return exp1; } - ExpressionStatement parseBitLeftShiftExpression() { + ExpressionStatement parseBitLeftShiftExpression() { /* * parseSubtractExpression() while(TokenType == '<<' | TokenType == '<<<'){ * parseSubtractExpression() } */ if (error) return null; - ExpressionStatement exp1 = parseSubtractExpression(); + ExpressionStatement exp1 = parseSubtractExpression(); while (token.kind == TokenKind.LSHIFT) { if (error) return null; - String op = token.value(); +// String op = token.value(); accept(token.kind); - ExpressionStatement exp2 = parseSubtractExpression(); + ExpressionStatement exp2 = parseSubtractExpression(); exp1 = BinaryExpression.of(OperatorType.BITLEFTSHIFT) .right(exp2) .left(exp1); @@ -553,19 +555,19 @@ ExpressionStatement parseBitLeftShiftExpression() { return exp1; } - ExpressionStatement parseSubtractExpression() { + ExpressionStatement parseSubtractExpression() { /* * parseAdditionExpression() while(TokenType == '-'){ parseAdditionExpression() * } */ if (error) return null; - ExpressionStatement exp1 = parseAdditionExpression(); + ExpressionStatement exp1 = parseAdditionExpression(); while (token.kind == TokenKind.SUB) { if (error) return null; - String op = token.value(); +// String op = token.value(); accept(token.kind); - ExpressionStatement exp2 = parseAdditionExpression(); + ExpressionStatement exp2 = parseAdditionExpression(); exp1 = BinaryExpression.of(OperatorType.MINUS) .right(exp2) .left(exp1); @@ -575,19 +577,19 @@ ExpressionStatement parseSubtractExpression() { return exp1; } - ExpressionStatement parseAdditionExpression() { + ExpressionStatement parseAdditionExpression() { /* * parseMultiplicationExpression() while(TokenType == '+'){ * parseMultiplicationExpression() } */ if (error) return null; - ExpressionStatement exp1 = parseMultiplicationExpression(); + ExpressionStatement exp1 = parseMultiplicationExpression(); while (token.kind == TokenKind.ADD) { if (error) return null; - String op = token.value(); +// String op = token.value(); accept(token.kind); - ExpressionStatement exp2 = parseMultiplicationExpression(); + ExpressionStatement exp2 = parseMultiplicationExpression(); exp1 = BinaryExpression.of(OperatorType.PLUS) .right(exp2) .left(exp1); @@ -597,19 +599,19 @@ ExpressionStatement parseAdditionExpression() { return exp1; } - ExpressionStatement parseMultiplicationExpression() { + ExpressionStatement parseMultiplicationExpression() { /* * parseDivisionExpression() while(TokenType == '*'){ parseDivisionExpression() * } */ if (error) return null; - ExpressionStatement exp1 = parseDivisionExpression(); + ExpressionStatement exp1 = parseDivisionExpression(); while (token.kind == TokenKind.MUL) { if (error) return null; - String op = token.value(); +// String op = token.value(); accept(token.kind); - ExpressionStatement exp2 = parseDivisionExpression(); + ExpressionStatement exp2 = parseDivisionExpression(); exp1 = BinaryExpression.of(OperatorType.TIMES) .right(exp2) .left(exp1); @@ -619,19 +621,19 @@ ExpressionStatement parseMultiplicationExpression() { return exp1; } - ExpressionStatement parseDivisionExpression() { + ExpressionStatement parseDivisionExpression() { /* * parseFactorExpression() while(TokenType == /, %, // ){ * parseFactorExpression() } */ if (error) return null; - ExpressionStatement exp1 = parseFactorExpression(); + ExpressionStatement exp1 = parseFactorExpression(); while (token.kind == TokenKind.DIV || token.kind == TokenKind.MOD || token.kind == TokenKind.FLOORDIV) { if (error) return null; OperatorType op = TokenKind.DIV == token.kind ? OperatorType.DIVIDE : TokenKind.MOD == token.kind ? OperatorType.MODULO : OperatorType.FLOOR; accept(token.kind); - ExpressionStatement exp2 = parseFactorExpression(); + ExpressionStatement exp2 = parseFactorExpression(); // exp1 = new BinaryExprNode(exp1, exp2, op); exp1 = BinaryExpression.of(op) .right(exp2) @@ -641,14 +643,14 @@ ExpressionStatement parseDivisionExpression() { return exp1; } - ExpressionStatement parseFactorExpression() { + ExpressionStatement parseFactorExpression() { /* * * if( TokenType == -, ~ ) parseFactorExpression() parsePowerExpression() * */ if (error) return null; - ExpressionStatement exp1 = null; + ExpressionStatement exp1; if (token.kind == TokenKind.SUB || token.kind == TokenKind.TILDE || token.kind == TokenKind.NOT || token.kind == TokenKind.NOTOP) { @@ -669,18 +671,18 @@ ExpressionStatement parseFactorExpression() { return exp1; } - ExpressionStatement parsePowerExpression() { + ExpressionStatement parsePowerExpression() { /* * parseAtomExpression() while(TokenType == '**'){ parseAtomExpression() } */ if (error) return null; - ExpressionStatement exp1 = null, exp2; + ExpressionStatement exp1, exp2; try { exp1 = parseAtomExpression(); while (token.kind == TokenKind.POWER) { if (error) return null; - String op = token.value(); +// String op = token.value(); accept(TokenKind.POWER); exp2 = parseFactorExpression(); exp1 = BinaryExpression.of(OperatorType.POW) @@ -688,18 +690,17 @@ ExpressionStatement parsePowerExpression() { .left(exp1); // exp1 = new BinaryExprNode(exp1, exp2, op); } + if (error) return null; return exp1; } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } - if (error) return null; - return exp1; - + return null; } - ExpressionStatement parseTrailer(ExpressionStatement oExp) throws ParseException { - ExpressionStatement node = null; + ExpressionStatement parseTrailer(ExpressionStatement oExp) throws ParseException { + ExpressionStatement node = null; if (token.kind == TokenKind.LPAREN || token.kind == TokenKind.DOT || token.kind == TokenKind.LSQU) { switch (token.kind) { case LPAREN: @@ -710,19 +711,19 @@ ExpressionStatement parseTrailer(ExpressionStatement oExp) throws ParseException // // b is a property of a, thus method invocation node is created. if (context.isProperty()) { // n.invokedOn.accept(this); - node = MethodInvocation.create(oExp, (List) parseArgList()); + node = MethodInvocation.create(oExp, parseArgList()); } else { // eg: a() // a() is normal function call, thus function invocation node is created. - node = FunctionInvocation.create(oExp, (List) parseArgList()); + node = FunctionInvocation.create(oExp, parseArgList()); } // node = new FuncCallNode(oExp, new ArgsListNode(parseArgList())); accept(TokenKind.RPAREN); break; case LSQU: - ExpressionStatement exp1, exp2; + ExpressionStatement exp1, exp2; debug.add("Atom Expr: " + token.kind); while (token.kind == TokenKind.LSQU) { if (error) @@ -751,7 +752,7 @@ ExpressionStatement parseTrailer(ExpressionStatement oExp) throws ParseException } break; case DOT: - ExpressionStatement e1; + ExpressionStatement e1; debug.add("Atom DOT:" + oExp); accept(TokenKind.DOT); Context context1 = Context.context; @@ -759,7 +760,7 @@ ExpressionStatement parseTrailer(ExpressionStatement oExp) throws ParseException e1 = parseIdentifier(); if (error) return null; - ExpressionStatement trail = e1; + ExpressionStatement trail = e1; while (token.kind != TokenKind.DOT && (token.kind == TokenKind.LSQU || token.kind == TokenKind.LPAREN)) { trail = parseTrailer(trail); } @@ -776,7 +777,7 @@ ExpressionStatement parseTrailer(ExpressionStatement oExp) throws ParseException return node; } - ExpressionStatement parseAtomExpression() throws ParseException { + ExpressionStatement parseAtomExpression() throws ParseException { /* * List of Trailers parseAtom() * @@ -785,31 +786,27 @@ ExpressionStatement parseAtomExpression() throws ParseException { if (error) return null; debug.add("Atom Expr Node Token: " + token.kind); - ExpressionStatement oExp = parseAtom(); + ExpressionStatement oExp = parseAtom(); if (oExp == null) { error = true; return null; } debug.add("Atom Expr Node Token: " + token.kind); if (oExp instanceof PropertyAccess || oExp instanceof ThisStatement || oExp instanceof ReferenceLookup) { - ExpressionStatement trailer; + ExpressionStatement trailer; while ((trailer = parseTrailer(oExp)) != null) { oExp = trailer; } } if (error) return null; - if (oExp == null) { - error = true; - return null; - } return oExp; } - CafeStatement parseExprStmt() { + CafeStatement parseExprStmt() { if (error) return null; - ExpressionStatement exp1 = parseLogicalAndExpression(); - ExpressionStatement exp2; + ExpressionStatement exp1 = parseLogicalAndExpression(); + ExpressionStatement exp2; debug.add("Expr Stmt : " + token.kind); if (token.kind == TokenKind.OR || token.kind == TokenKind.OROP) { String op = token.value(); @@ -830,16 +827,16 @@ CafeStatement parseExprStmt() { return exp1; } - ExpressionStatement parseAtom() { + ExpressionStatement parseAtom() { /* - * switch TokenType case LPAREN: parseExpressionStatement() accept(RPAREN) case + * switch TokenType case LPAREN: parseExpressionStatement exp1 = null; switch (token.kind) { case LPAREN: accept(TokenKind.LPAREN); @@ -872,18 +869,18 @@ ExpressionStatement parseAtom() { exp1 = parseThis(); break; default: - logError(INVALID_EXPRESSION); + logError(); } if (error) return null; if (exp1 == null) { error = true; return null; } - return (ExpressionStatement) exp1; + return (ExpressionStatement) exp1; } - ExpressionStatement parseNull() { + ExpressionStatement parseNull() { if (error) return null; // Token tk = token; // accept(TokenKind.NULL); @@ -892,7 +889,7 @@ ExpressionStatement parseNull() { return new NullStatement(); } - ExpressionStatement parseThis() { + ExpressionStatement parseThis() { if (error) return null; // Token tk = token; // accept(TokenKind.THIS); @@ -906,7 +903,7 @@ ExpressionStatement parseThis() { return ThisStatement.create(isGlobal); } - ExpressionStatement parseIdentifier() { + ExpressionStatement parseIdentifier() { /* * Create Identifier Node. return IdentNode() */ @@ -927,7 +924,7 @@ ExpressionStatement parseIdentifier() { } } - ExpressionStatement parseStringLiteral() { + ExpressionStatement parseStringLiteral() { /* * check Quotes accept(STRING LITERAL) * @@ -939,7 +936,7 @@ ExpressionStatement parseStringLiteral() { } - ExpressionStatement parseNumberLiteral() throws ParseException { + ExpressionStatement parseNumberLiteral() throws ParseException { /* * */ @@ -979,26 +976,24 @@ ExpressionStatement parseNumberLiteral() throws ParseException { return null; } - ExpressionStatement parseBoolLiteral() { + ExpressionStatement parseBoolLiteral() { /* * */ if (error) return null; - Token tk = token; +// Token tk = token; if (token.kind == TokenKind.TRUE) { accept(TokenKind.TRUE); if (error) return null; - ConstantStatement boolLit = new ConstantStatement(true); -// boolLit.setFirstToken(tk); - return boolLit; + // boolLit.setFirstToken(tk); + return new ConstantStatement(true); } else if (token.kind == TokenKind.FALSE) { accept(TokenKind.FALSE); if (error) return null; - ConstantStatement boolLit = new ConstantStatement(false); -// boolLit.setFirstToken(tk); - return boolLit; + // boolLit.setFirstToken(tk); + return new ConstantStatement(false); } return null; } @@ -1019,12 +1014,12 @@ void parseSubscript() { */ } - List parseArgList() { + List> parseArgList() { /* * parseArg() while(COMMA) parseArg() */ if (error) return null; - List args = new ArrayList<>(); + List> args = new ArrayList<>(); while (token.kind != TokenKind.RPAREN) { if (error) return null; @@ -1062,17 +1057,17 @@ void parseExpressionStatement() { /* parseStatements */ ConditionalBranching parseIf() { if (error) return null; - ExpressionStatement ifCond = null; - Block ifBlock = null; + ExpressionStatement ifCond; + Block ifBlock; - Token firstToken = token; +// Token firstToken = token; nextToken(); accept(TokenKind.LPAREN); ifCond = parseLogicalOrExpression(); if (ifCond == null) { // log.report(Type.ERROR, token.pos, errorDescription(token.pos, "If without condition!")); - logError(INVALID_EXPRESSION); + logError(); error = true; return null; } @@ -1080,19 +1075,19 @@ ConditionalBranching parseIf() { accept(TokenKind.LCURLY); if (token.kind != TokenKind.RCURLY) ifBlock = parseLoopBlock(); + else + ifBlock = parseBlock(); accept(TokenKind.RCURLY); if (error) return null; - ConditionalBranching conditionalBranching = ConditionalBranching + // IfStmtNode ifNode = new IfStmtNode(ifCond, ifBlock); +// ifNode.setFirstToken(firstToken); + return ConditionalBranching .branch() .condition(ifCond) .whenTrue(ifBlock); - -// IfStmtNode ifNode = new IfStmtNode(ifCond, ifBlock); -// ifNode.setFirstToken(firstToken); - return conditionalBranching; } CafeStatement parseIfStatement() { @@ -1107,14 +1102,12 @@ CafeStatement parseIfStatement() { if ((ifNode = parseIf()) != null) { CafeStatement elseBlock = null; if (token.kind == TokenKind.ELSE) { - Token elseFT = token; +// Token elseFT = token; nextToken(); if (token.kind == TokenKind.IF) { elseBlock = parseIfStatement(); } else if (accept(TokenKind.LCURLY)) { - Block blockNode = null; - while(token.kind != TokenKind.RCURLY) - blockNode = parseBlock(); + Block blockNode = parseBlock(); accept(TokenKind.RCURLY); elseBlock = ifNode.elseBranch(blockNode); // elseBlock.setFirstToken(elseFT); @@ -1154,9 +1147,9 @@ StmtNode parseAssignmentStatement() { */ if (error) return null; // -// ExpressionStatement exp1 = parseIdentifier(); +// ExpressionStatement parseForInit() { */ if (error) return null; List init = null; - ExpressionStatement iden, val; + ExpressionStatement iden, val; Context ctx = Context.context; if (token.kind == TokenKind.SEMICOLON) return init; @@ -1217,12 +1210,12 @@ List parseForInit() { return init; } - ExpressionStatement parseForCondition() { + ExpressionStatement parseForCondition() { /* * parseLogOrEcpression() */ if (error) return null; - ExpressionStatement cond = null; + ExpressionStatement cond = null; if (token.kind == TokenKind.SEMICOLON) return cond; cond = parseLogicalOrExpression(); @@ -1236,10 +1229,9 @@ List> parseForIncrement() { * parseAssignmentStatement() */ if (error) return null; - ExpressionStatement iden, val, exp2; + ExpressionStatement iden, val, exp2; List> incrNodes = null; if (token.kind == TokenKind.RPAREN) { - if (error) return null; return incrNodes; } @@ -1273,7 +1265,7 @@ List> parseForIncrement() { return incrNodes; } - CafeStatement parseForStatement() { + CafeStatement parseForStatement() { /* * accept(FOR) accept(LPAREN) if (SEMI ) parseForCondition() accept(SEMI) * parseForIncr() else parseForInit() accept(SEMI) parseForCondition() @@ -1283,10 +1275,10 @@ CafeStatement parseForStatement() { Context context = Context.context; ForLoopStatement forLoop = ForLoopStatement.loop(); context.forLoopStack.push(forLoop); - List init = null; - ExpressionStatement cond = null; - List> incrNode = null; - Block block = null; + List init; + ExpressionStatement cond; + List> incrNode; + Block block; accept(TokenKind.FOR); accept(TokenKind.LPAREN); init = parseForInit(); @@ -1307,16 +1299,16 @@ CafeStatement parseForStatement() { return forLoop; } - CafeStatement parseLoopStatement() { + CafeStatement parseLoopStatement() { /* * accept(LOOP) parseLoopIdentifier() accept(IN) parseLoopValue() * parseLoopBlock() parseCollectionComprehension() * */ if (error) return null; - ExpressionStatement iden1, iden2 = null; - ExpressionStatement exp = null; - Block block = null; + ExpressionStatement iden1=null, iden2=null; + ExpressionStatement exp = null; + Block block=null; accept(TokenKind.LOOP); iden1 = parseIdentifier(); @@ -1327,7 +1319,7 @@ CafeStatement parseLoopStatement() { accept(TokenKind.IN); try { exp = parseAtomExpression(); - } catch (ParseException e) { + } catch (ParseException ignored) { } if (exp == null) { @@ -1346,7 +1338,7 @@ CafeStatement parseLoopStatement() { return null; } - CafeStatement parseFlowStatement() { + CafeStatement parseFlowStatement() { /* * if(CONTINUE) return ContinueNode if(BREAK) return BreakNode */ @@ -1355,7 +1347,6 @@ CafeStatement parseFlowStatement() { Context context = Context.context; if (breakAllowed) if (token.kind == TokenKind.CONTINUE) { - if (error) return null; accept(TokenKind.CONTINUE); accept(TokenKind.SEMICOLON); ContinueStmtNode continueStmtNode = new ContinueStmtNode(); @@ -1363,7 +1354,6 @@ CafeStatement parseFlowStatement() { return BreakContinueStatement.newContinue() .setEnclosingLoop(context.forLoopStack.peek()); } else { - if (error) return null; accept(TokenKind.BREAK); accept(TokenKind.SEMICOLON); // BreakStmtNode breakStmtNode = new BreakStmtNode(); @@ -1380,7 +1370,7 @@ CafeStatement parseFlowStatement() { /* Parse Loop Done */ /* Parse Collection */ - ExpressionStatement parseList() { + ExpressionStatement parseList() { /* * List of Values * @@ -1391,9 +1381,9 @@ ExpressionStatement parseList() { * return List */ if (error) return null; -// List listNode = new ArrayList<>(); +// List listNode = new ArrayList<>(); ListCollection listNode = ListCollection.list(); - ExpressionStatement exp1, exp2; + ExpressionStatement exp1; exp1 = parseValue(); if (token.kind == TokenKind.RANGE) { // TODO: to implement IR node implementation for RANGE @@ -1415,7 +1405,7 @@ ExpressionStatement parseList() { return listNode; } - ExpressionStatement parseListCollection() { + ExpressionStatement parseListCollection() { /* * List of Collection * @@ -1454,7 +1444,7 @@ void parseMap() { // NOT Used by MapColl } - ExpressionStatement parseMapCollection() { + ExpressionStatement parseMapCollection() { /* * List of MapCollection List of Comp * @@ -1499,7 +1489,7 @@ ExpressionStatement parseMapCollection() { return null; } - ExpressionStatement parseComprehension(String type) { + ExpressionStatement parseComprehension(String type) { // if (error) return null; // IdenNode iden1, iden2 = null; // ExprNode exp = null; @@ -1553,7 +1543,7 @@ ExpressionStatement parseComprehension(String type) { return null; } - ExpressionStatement parseSet() { + ExpressionStatement parseSet() { /* * List of Values * @@ -1588,7 +1578,7 @@ ExpressionStatement parseSet() { return null; } - ExpressionStatement parseSetCollection() { + ExpressionStatement parseSetCollection() { /* * List of Collection * @@ -1616,7 +1606,7 @@ ExpressionStatement parseSetCollection() { return null; } - ExprNode parseLink() { + ExpressionStatement parseLink() { /* * List of Values * @@ -1651,7 +1641,7 @@ ExprNode parseLink() { return null; } - ExpressionStatement parseLinkCollection() { + ExpressionStatement parseLinkCollection() { /* * List of Collection * @@ -1680,7 +1670,7 @@ ExpressionStatement parseLinkCollection() { return null; } - ExpressionStatement parseCollection() { + ExpressionStatement parseCollection() { /* * List of Collection * @@ -1690,7 +1680,7 @@ ExpressionStatement parseCollection() { * return List */ if (error) return null; - ExpressionStatement collExpr = null; + ExpressionStatement collExpr = null; switch (token.kind) { case LSQU: @@ -1714,7 +1704,7 @@ ExpressionStatement parseCollection() { /* Parse Collection Done */ /* Parse Values */ - ExpressionStatement parseObjectCreation() { + ExpressionStatement parseObjectCreation() { /* * List of ObjectNode * @@ -1725,9 +1715,9 @@ ExpressionStatement parseObjectCreation() { */ if (error) return null; - Map object = new LinkedHashMap<>(); - ExpressionStatement idenNode; - ExpressionStatement exprNode; + Map> object = new LinkedHashMap<>(); + ExpressionStatement idenNode; + ExpressionStatement exprNode; accept(TokenKind.LCURLY); while (token.kind != TokenKind.RCURLY) { @@ -1779,19 +1769,8 @@ AnonymousFunction parseAnnFunction() { accept(TokenKind.RPAREN); accept(TokenKind.LCURLY); debug.add("Ann Func Node: " + token.kind); - List> stmt = new ArrayList<>(); - while (token.kind != TokenKind.RCURLY) { - if (error) - return null; - Block stm = parseBlock(); - if (stm == null) return null; - stmt.addAll(stm.getStatements()); - } + Block block = parseBlock(); accept(TokenKind.RCURLY); - Block block = context.enterScope(); - for (CafeStatement stm: stmt) { - block.add(stm); - } if (!block.hasReturn()) block.add(ReturnStatement.of(null)); @@ -1803,7 +1782,7 @@ AnonymousFunction parseAnnFunction() { context.addFunction(function); - ExpressionStatement expression=null; + ExpressionStatement expression; if (context.currentScope() == ParseIR.Context.Scope.CLOSURE) { expression = function.asClosure(); } else { @@ -1827,7 +1806,7 @@ AnonymousFunction parseAnnFunction() { return anonymousFunction; } - ExpressionStatement parseValue() { + ExpressionStatement parseValue() { /* * ValueNode * @@ -1839,7 +1818,7 @@ ExpressionStatement parseValue() { * return ValueNode */ if (error) return null; - ExpressionStatement valExpr = null; + ExpressionStatement valExpr ; debug.add("Parse Value: " + token.kind); switch (token.kind) { case LCURLY: @@ -1892,9 +1871,9 @@ List parseVariable() { while (token.kind != TokenKind.SEMICOLON) { if (error) return null; - Token tk = token; - ExpressionStatement iden = parseIdentifier(); - ExpressionStatement exp = null; +// Token tk = token; + ExpressionStatement iden = parseIdentifier(); + ExpressionStatement exp = null; Context ctx = Context.context; if (error) return null; @@ -1934,10 +1913,10 @@ List parseConstVariable() { while (token.kind != TokenKind.SEMICOLON) { if (error) return null; - Token tk = token; - ExpressionStatement iden = parseIdentifier(); +// Token tk = token; + ExpressionStatement iden = parseIdentifier(); accept(TokenKind.EQU); - ExpressionStatement exp = parseValue(); + ExpressionStatement exp = parseValue(); if (token.kind != TokenKind.SEMICOLON) accept(TokenKind.COMMA); SymbolReference sym = ctx.createSymbolReference(iden.getName(), Tag.CONSTDECL); @@ -2006,26 +1985,14 @@ DeclarativeAssignmentStatement parseFunctionDeclaration() { Context context = Context.context; accept(TokenKind.FUNC); Token tk = token; - ExpressionStatement funcName = parseIdentifier(); + ExpressionStatement funcName = parseIdentifier(); context.enterFunc(funcName.getName()); accept(TokenKind.LPAREN); List arg = parseParameter(); accept(TokenKind.RPAREN); accept(TokenKind.LCURLY); - List> stmt = new ArrayList<>(); - while (token.kind != TokenKind.RCURLY) { - if (error) - return null; - Block stm = parseBlock(); - if (stm == null) return null; - stmt.addAll(stm.getStatements()); - } + Block block = parseBlock(); accept(TokenKind.RCURLY); - Block block = context.enterScope(); - for (CafeStatement stm: stmt) { - block.add(stm); - } - if (!block.hasReturn()) block.add(ReturnStatement.of(null)); CafeFunction function = CafeFunction.function(funcName.getName()) @@ -2088,7 +2055,7 @@ CafeStatement parseReturnStatement() { if (error) return null; accept(TokenKind.RET); Token tk = token; - ExpressionStatement exp = parseValue(); + ExpressionStatement exp = parseValue(); debug.add("Return : " + exp); accept(TokenKind.SEMICOLON); if (error) return null; @@ -2102,32 +2069,94 @@ Block parseLoopBlock() { * parseBlockStatement() parseFlowStatement() */ if (error) return null; - List blockStats = new ArrayList<>(); - - Context context = Context.context; - Block block = context.enterScope(); - while (token.kind != TokenKind.RCURLY) { - if (error) - return null; - switch (token.kind) { - case CONTINUE: - case BREAK: - blockStats.add(parseFlowStatement()); - break; - default: - block = parseBlock(); +// List> blockStats = new ArrayList<>(); +// +// Context context = Context.context; +// Block block = context.enterScope(); +// while (token.kind != TokenKind.RCURLY) { +// if (error) +// return null; +// switch (token.kind) { +// case CONTINUE: +// case BREAK: +// blockStats.add(parseFlowStatement()); +// break; +// default: + Block block = parseBlock(); // List> stm = parseBlock(); // if (stm == null) return null; // blockStats.addAll(stm); // for (CafeStatement stmt: blockStats){ // block.add(stmt); // } - } - } +// } +// } if (error) return null; return block; } - + List> parseStatement() { + List> block = new ArrayList<>(); + switch (token.kind) { + case VAR: + List stm = parseVariable(); + if (stm == null) return null; + block.addAll(stm); + break; + case CONST: + List stm1 = parseConstVariable(); + if (stm1 == null) return null; + block.addAll(stm1); + break; + case FUNC: + DeclarativeAssignmentStatement decl = parseFunctionDeclaration(); + if (decl == null) return null; + block.add(decl); + break; + case IF: + CafeStatement stm2 = parseIfStatement(); + if (stm2 == null) return null; + block.add(stm2); + break; + case FOR: + innerLoop = breakAllowed; + breakAllowed = true; + CafeStatement stm3 = parseForStatement(); + if (stm3 == null) return null; + block.add(stm3); + breakAllowed = innerLoop; + innerLoop = false; + break; + case LOOP: + CafeStatement stm4 = parseLoopStatement(); + if (stm4 == null) return null; + block.add(stm4); + break; + case RET: + CafeStatement stm5 = parseReturnStatement(); + if (stm5 == null) return null; + block.add(stm5); + break; + case IDENTIFIER: + case THIS: + case NULL: + CafeStatement stm6 = parseExprStmt(); + if (stm6 == null) return null; + block.add(stm6); + debug.add("Block Stmt: " + token.kind); + accept(TokenKind.SEMICOLON); + break; + case BREAK: + case CONTINUE: + CafeStatement stm7 = parseFlowStatement(); + block.add(stm7); + debug.add("Block Stmt: " + token.kind); + break; + default: + logError(TokenKind.IDENTIFIER); + } + if(error || block.isEmpty()) return null; + return block; + } // return Block Statement Node Block parseBlock() { /* @@ -2145,71 +2174,19 @@ Block parseBlock() { // } // context.push(block); -// List> blockStmt = new ArrayList<>(); + List> blockStmt = new ArrayList<>(); debug.add("Token kind " + token.kind); - switch (token.kind) { - case VAR: - List stm = parseVariable(); - if (stm == null) return null; - for (CafeStatement st : stm) { - block.add(st); - } - break; - case CONST: - List stm1 = parseConstVariable(); - if (stm1 == null) return null; - for (CafeStatement st : stm1) { - block.add(st); - } - break; - case FUNC: - DeclarativeAssignmentStatement decl = parseFunctionDeclaration(); - if (decl == null) return null; - block.add(decl); - break; - case IF: - CafeStatement stm2 = parseIfStatement(); - if (stm2 == null) return null; - block.add(stm2); - break; - case FOR: - innerLoop = breakAllowed ? true : false; - breakAllowed = true; - CafeStatement stm3 = parseForStatement(); - if (stm3 == null) return null; - block.add(stm3); - breakAllowed = innerLoop ? true : false; - innerLoop = false; - break; - case LOOP: - CafeStatement stm4 = parseLoopStatement(); - if (stm4 == null) return null; - block.add(stm4); - break; - case RET: - CafeStatement stm5 = parseReturnStatement(); - if (stm5 == null) return null; - block.add(stm5); - break; - case IDENTIFIER: - case THIS: - case NULL: - CafeStatement stm6 = parseExprStmt(); - if (stm6 == null) return null; - block.add(stm6); - debug.add("Block Stmt: " + token.kind); - accept(TokenKind.SEMICOLON); - break; - case BREAK: - case CONTINUE: - CafeStatement stm7 = parseFlowStatement(); - block.add(stm7); - debug.add("Block Stmt: " + token.kind); - break; - default: - logError(TokenKind.IDENTIFIER); - } + while(token.kind != TokenKind.RCURLY) { + if (error) return null; + List> stm = parseStatement(); + if (stm == null) return null; + blockStmt.addAll(stm); + } + for(CafeStatement stmt: blockStmt){ + block.add(stmt); + } + context.leaveScope(); if (error) return null; debug.add("Block Stmt: " + block); @@ -2235,7 +2212,7 @@ List parseExportStatement() { accept(TokenKind.EXPORT); switch (token.kind) { case IDENTIFIER: - ExpressionStatement id = parseIdentifier(); + ExpressionStatement id = parseIdentifier(); if (id == null) return null; exportStmtNode.add(CafeExport.export(id.getName())); while (token.kind == TokenKind.COMMA) { @@ -2288,7 +2265,7 @@ CafeImport parseImportStatement() { // else Throw Error ImportStmtNode importStmtNode = null; Map blocks = new HashMap(); - ExpressionStatement id1, id2 = null; + ExpressionStatement id1, id2 = null; Context context = Context.context; // // for (Map.Entry entry : n.importAliasMap.entrySet()) { @@ -2383,9 +2360,9 @@ CafeModule parseStatements() { tree.addAll(exports); break; default: - Block stmt = parseBlock(); + List> stmt = parseStatement(); if (stmt == null) return null; - tree.addAll(stmt.getStatements()); + tree.addAll(stmt); break; } } diff --git a/src/main/java/compiler/parser/Parser.java b/src/main/java/compiler/parser/Parser.java index 9eb5bec..e8fd4c5 100644 --- a/src/main/java/compiler/parser/Parser.java +++ b/src/main/java/compiler/parser/Parser.java @@ -30,9 +30,10 @@ package compiler.parser; import compiler.ast.Node; +import compiler.ir.CafeModule; public abstract class Parser { abstract Parser instance(ParserFactory factory, Lexer lexer); - + public abstract CafeModule parseToIR(String moduleName); public abstract Node parse(); } \ No newline at end of file diff --git a/src/main/java/compiler/parser/ParserType.java b/src/main/java/compiler/parser/ParserType.java index 98af939..5cd953a 100644 --- a/src/main/java/compiler/parser/ParserType.java +++ b/src/main/java/compiler/parser/ParserType.java @@ -35,7 +35,7 @@ * @author Dhyey */ public enum ParserType { - MAINPARSER("MainParser"); + MAINPARSER("ParseIR"); String parserClassName; diff --git a/src/test/java/compiler/parser/MainParserTest.java b/src/test/java/compiler/parser/MainParserTest.java index d0ba0d6..030eb37 100644 --- a/src/test/java/compiler/parser/MainParserTest.java +++ b/src/test/java/compiler/parser/MainParserTest.java @@ -30,6 +30,7 @@ package compiler.parser; import compiler.ast.Node; +import compiler.ir.CafeModule; import compiler.main.SourceFileManager; import compiler.util.Context; import org.testng.annotations.BeforeMethod; @@ -71,15 +72,19 @@ public void init() { public void test_no_error(File file) { fm.setSourceFile(file); Parser parser = factory.newParser(ParserType.MAINPARSER, fm.asCharList()); - Node n = parser.parse(); - assertNotNull(n); + CafeModule module = parser.parseToIR(file.getName()); + System.out.println("Success "+module); +// Node n = parser.parse(); + assertNotNull(module); } @Test(dataProvider = "cafe-error-files") public void test_error(File file) { fm.setSourceFile(file); Parser parser = factory.newParser(ParserType.MAINPARSER, fm.asCharList()); - Node n = parser.parse(); - assertNull(n); + CafeModule module = parser.parseToIR(file.getName()); + System.out.println("Success "+module); +// Node n = parser.parse(); + assertNull(module); } } From 683362694fa6b56bf0cdf087a3ce31a82343a53c Mon Sep 17 00:00:00 2001 From: SaurabhPethani Date: Mon, 5 Apr 2021 18:59:26 +0530 Subject: [PATCH 07/14] Refactored Parse IR --- "pethani28@gmail.com\357\200\276" | 238 ----------- src/main/java/compiler/main/CafeCompiler.java | 2 +- .../parser/{ParseIR.java => IRParser.java} | 387 +++++++++--------- src/main/java/compiler/parser/MainParser.java | 2 +- src/main/java/compiler/parser/ParserType.java | 4 +- .../java/compiler/parser/MainParserTest.java | 4 +- 6 files changed, 198 insertions(+), 439 deletions(-) delete mode 100644 "pethani28@gmail.com\357\200\276" rename src/main/java/compiler/parser/{ParseIR.java => IRParser.java} (92%) diff --git "a/pethani28@gmail.com\357\200\276" "b/pethani28@gmail.com\357\200\276" deleted file mode 100644 index 0bbada9..0000000 --- "a/pethani28@gmail.com\357\200\276" +++ /dev/null @@ -1,238 +0,0 @@ - - SSUUMMMMAARRYY OOFF LLEESSSS CCOOMMMMAANNDDSS - - Commands marked with * may be preceded by a number, _N. - Notes in parentheses indicate the behavior if _N is given. - A key preceded by a caret indicates the Ctrl key; thus ^K is ctrl-K. - - h H Display this help. - q :q Q :Q ZZ Exit. - --------------------------------------------------------------------------- - - MMOOVVIINNGG - - e ^E j ^N CR * Forward one line (or _N lines). - y ^Y k ^K ^P * Backward one line (or _N lines). - f ^F ^V SPACE * Forward one window (or _N lines). - b ^B ESC-v * Backward one window (or _N lines). - z * Forward one window (and set window to _N). - w * Backward one window (and set window to _N). - ESC-SPACE * Forward one window, but don't stop at end-of-file. - d ^D * Forward one half-window (and set half-window to _N). - u ^U * Backward one half-window (and set half-window to _N). - ESC-) RightArrow * Right one half screen width (or _N positions). - ESC-( LeftArrow * Left one half screen width (or _N positions). - ESC-} ^RightArrow Right to last column displayed. - ESC-{ ^LeftArrow Left to first column. - F Forward forever; like "tail -f". - ESC-F Like F but stop when search pattern is found. - r ^R ^L Repaint screen. - R Repaint screen, discarding buffered input. - --------------------------------------------------- - Default "window" is the screen height. - Default "half-window" is half of the screen height. - --------------------------------------------------------------------------- - - SSEEAARRCCHHIINNGG - - /_p_a_t_t_e_r_n * Search forward for (_N-th) matching line. - ?_p_a_t_t_e_r_n * Search backward for (_N-th) matching line. - n * Repeat previous search (for _N-th occurrence). - N * Repeat previous search in reverse direction. - ESC-n * Repeat previous search, spanning files. - ESC-N * Repeat previous search, reverse dir. & spanning files. - ESC-u Undo (toggle) search highlighting. - &_p_a_t_t_e_r_n * Display only matching lines - --------------------------------------------------- - A search pattern may be preceded by one or more of: - ^N or ! Search for NON-matching lines. - ^E or * Search multiple files (pass thru END OF FILE). - ^F or @ Start search at FIRST file (for /) or last file (for ?). - ^K Highlight matches, but don't move (KEEP position). - ^R Don't use REGULAR EXPRESSIONS. - --------------------------------------------------------------------------- - - JJUUMMPPIINNGG - - g < ESC-< * Go to first line in file (or line _N). - G > ESC-> * Go to last line in file (or line _N). - p % * Go to beginning of file (or _N percent into file). - t * Go to the (_N-th) next tag. - T * Go to the (_N-th) previous tag. - { ( [ * Find close bracket } ) ]. - } ) ] * Find open bracket { ( [. - ESC-^F _<_c_1_> _<_c_2_> * Find close bracket _<_c_2_>. - ESC-^B _<_c_1_> _<_c_2_> * Find open bracket _<_c_1_> - --------------------------------------------------- - Each "find close bracket" command goes forward to the close bracket - matching the (_N-th) open bracket in the top line. - Each "find open bracket" command goes backward to the open bracket - matching the (_N-th) close bracket in the bottom line. - - m_<_l_e_t_t_e_r_> Mark the current top line with . - M_<_l_e_t_t_e_r_> Mark the current bottom line with . - '_<_l_e_t_t_e_r_> Go to a previously marked position. - '' Go to the previous position. - ^X^X Same as '. - ESC-M_<_l_e_t_t_e_r_> Clear a mark. - --------------------------------------------------- - A mark is any upper-case or lower-case letter. - Certain marks are predefined: - ^ means beginning of the file - $ means end of the file - --------------------------------------------------------------------------- - - CCHHAANNGGIINNGG FFIILLEESS - - :e [_f_i_l_e] Examine a new file. - ^X^V Same as :e. - :n * Examine the (_N-th) next file from the command line. - :p * Examine the (_N-th) previous file from the command line. - :x * Examine the first (or _N-th) file from the command line. - :d Delete the current file from the command line list. - = ^G :f Print current file name. - --------------------------------------------------------------------------- - - MMIISSCCEELLLLAANNEEOOUUSS CCOOMMMMAANNDDSS - - -_<_f_l_a_g_> Toggle a command line option [see OPTIONS below]. - --_<_n_a_m_e_> Toggle a command line option, by name. - __<_f_l_a_g_> Display the setting of a command line option. - ___<_n_a_m_e_> Display the setting of an option, by name. - +_c_m_d Execute the less cmd each time a new file is examined. - - !_c_o_m_m_a_n_d Execute the shell command with $SHELL. - |XX_c_o_m_m_a_n_d Pipe file between current pos & mark XX to shell command. - s _f_i_l_e Save input to a file. - v Edit the current file with $VISUAL or $EDITOR. - V Print version number of "less". - --------------------------------------------------------------------------- - - OOPPTTIIOONNSS - - Most options may be changed either on the command line, - or from within less by using the - or -- command. - Options may be given in one of two forms: either a single - character preceded by a -, or a name preceded by --. - - -? ........ --help - Display help (from command line). - -a ........ --search-skip-screen - Search skips current screen. - -A ........ --SEARCH-SKIP-SCREEN - Search starts just after target line. - -b [_N] .... --buffers=[_N] - Number of buffers. - -B ........ --auto-buffers - Don't automatically allocate buffers for pipes. - -c ........ --clear-screen - Repaint by clearing rather than scrolling. - -d ........ --dumb - Dumb terminal. - -D [_x_n_._n] . --color=_x_n_._n - Set screen colors. (MS-DOS only) - -e -E .... --quit-at-eof --QUIT-AT-EOF - Quit at end of file. - -f ........ --force - Force open non-regular files. - -F ........ --quit-if-one-screen - Quit if entire file fits on first screen. - -g ........ --hilite-search - Highlight only last match for searches. - -G ........ --HILITE-SEARCH - Don't highlight any matches for searches. - -h [_N] .... --max-back-scroll=[_N] - Backward scroll limit. - -i ........ --ignore-case - Ignore case in searches that do not contain uppercase. - -I ........ --IGNORE-CASE - Ignore case in all searches. - -j [_N] .... --jump-target=[_N] - Screen position of target lines. - -J ........ --status-column - Display a status column at left edge of screen. - -k [_f_i_l_e] . --lesskey-file=[_f_i_l_e] - Use a lesskey file. - -K --quit-on-intr - Exit less in response to ctrl-C. - -L ........ --no-lessopen - Ignore the LESSOPEN environment variable. - -m -M .... --long-prompt --LONG-PROMPT - Set prompt style. - -n -N .... --line-numbers --LINE-NUMBERS - Don't use line numbers. - -o [_f_i_l_e] . --log-file=[_f_i_l_e] - Copy to log file (standard input only). - -O [_f_i_l_e] . --LOG-FILE=[_f_i_l_e] - Copy to log file (unconditionally overwrite). - -p [_p_a_t_t_e_r_n] --pattern=[_p_a_t_t_e_r_n] - Start at pattern (from command line). - -P [_p_r_o_m_p_t] --prompt=[_p_r_o_m_p_t] - Define new prompt. - -q -Q .... --quiet --QUIET --silent --SILENT - Quiet the terminal bell. - -r -R .... --raw-control-chars --RAW-CONTROL-CHARS - Output "raw" control characters. - -s ........ --squeeze-blank-lines - Squeeze multiple blank lines. - -S ........ --chop-long-lines - Chop (truncate) long lines rather than wrapping. - -t [_t_a_g] .. --tag=[_t_a_g] - Find a tag. - -T [_t_a_g_s_f_i_l_e] --tag-file=[_t_a_g_s_f_i_l_e] - Use an alternate tags file. - -u -U .... --underline-special --UNDERLINE-SPECIAL - Change handling of backspaces. - -V ........ --version - Display the version number of "less". - -w ........ --hilite-unread - Highlight first new line after forward-screen. - -W ........ --HILITE-UNREAD - Highlight first new line after any forward movement. - -x [_N[,...]] --tabs=[_N[,...]] - Set tab stops. - -X ........ --no-init - Don't use termcap init/deinit strings. - -y [_N] .... --max-forw-scroll=[_N] - Forward scroll limit. - -z [_N] .... --window=[_N] - Set size of window. - -" [_c[_c]] . --quotes=[_c[_c]] - Set shell quote characters. - -~ ........ --tilde - Don't display tildes after end of file. - -# [_N] .... --shift=[_N] - Horizontal scroll amount (0 = one half screen width) - ........ --no-keypad - Don't send termcap keypad init/deinit strings. - ........ --follow-name - The F command changes files if the input file is renamed. - ........ --use-backslash - Subsequent options use backslash as escape char. - - - --------------------------------------------------------------------------- - - LLIINNEE EEDDIITTIINNGG - - These keys can be used to edit text being entered - on the "command line" at the bottom of the screen. - - RightArrow ..................... ESC-l ... Move cursor right one character. - LeftArrow ...................... ESC-h ... Move cursor left one character. - ctrl-RightArrow ESC-RightArrow ESC-w ... Move cursor right one word. - ctrl-LeftArrow ESC-LeftArrow ESC-b ... Move cursor left one word. - HOME ........................... ESC-0 ... Move cursor to start of line. - END ............................ ESC-$ ... Move cursor to end of line. - BACKSPACE ................................ Delete char to left of cursor. - DELETE ......................... ESC-x ... Delete char under cursor. - ctrl-BACKSPACE ESC-BACKSPACE ........... Delete word to left of cursor. - ctrl-DELETE .... ESC-DELETE .... ESC-X ... Delete word under cursor. - ctrl-U ......... ESC (MS-DOS only) ....... Delete entire line. - UpArrow ........................ ESC-k ... Retrieve previous command line. - DownArrow ...................... ESC-j ... Retrieve next command line. - TAB ...................................... Complete filename & cycle. - SHIFT-TAB ...................... ESC-TAB Complete filename & reverse cycle. - ctrl-L ................................... Complete filename, list all. - - diff --git a/src/main/java/compiler/main/CafeCompiler.java b/src/main/java/compiler/main/CafeCompiler.java index 3aac8d9..5ab9dc5 100644 --- a/src/main/java/compiler/main/CafeCompiler.java +++ b/src/main/java/compiler/main/CafeCompiler.java @@ -114,7 +114,7 @@ public CompilerResult compile() { fileManager.setSourceFile(sourceFile); break; case PARSEIR: - parser = parserFactory.newParser(ParserType.MAINPARSER, fileManager.asCharList()); + parser = parserFactory.newParser(ParserType.IRParser, fileManager.asCharList()); module = parser.parseToIR(moduleName); System.out.println("Success "+module); break; diff --git a/src/main/java/compiler/parser/ParseIR.java b/src/main/java/compiler/parser/IRParser.java similarity index 92% rename from src/main/java/compiler/parser/ParseIR.java rename to src/main/java/compiler/parser/IRParser.java index 118d9e9..2e85f95 100644 --- a/src/main/java/compiler/parser/ParseIR.java +++ b/src/main/java/compiler/parser/IRParser.java @@ -45,25 +45,24 @@ import static compiler.util.Log.Type.*; import static compiler.util.Messages.message; -public class ParseIR extends Parser { +public class IRParser extends Parser { static { - ParserFactory.registerParser(ParserType.MAINPARSER, new ParseIR()); + ParserFactory.registerParser(ParserType.IRParser, new IRParser()); } public CafeModule parseToIR(String moduleName) { - ParseIR.Context.context.createModule(moduleName); - ParseIR.Context.context.newObjectStack(); - parseStatements(); - return ParseIR.Context.context.module; + IRParser.Context.context.createModule(moduleName); +// IRParser.Context.context.newObjectStack(); + return parseStatements(); } private static final class Context { - final static ParseIR.Context context = new ParseIR.Context(); + final static IRParser.Context context = new IRParser.Context(); public CafeModule module; private final Deque referenceTableStack = new LinkedList<>(); private final Deque functionStack = new LinkedList<>(); - private final Deque> objectStack = new LinkedList<>(); +// private final Deque> objectStack = new LinkedList<>(); private final Deque forLoopStack = new LinkedList<>(); private final AnnFuncNameGenerator annFuncNameGenerator = new AnnFuncNameGenerator(); @@ -131,22 +130,21 @@ public enum Scope { GLOBAL, LOCAL, CLOSURE } - public ParseIR.Context.Scope currentScope() { + public IRParser.Context.Scope currentScope() { if (functionStack.size() == 0) - return ParseIR.Context.Scope.GLOBAL; + return IRParser.Context.Scope.GLOBAL; if (functionStack.size() == 1) - return ParseIR.Context.Scope.LOCAL; - return ParseIR.Context.Scope.CLOSURE; + return IRParser.Context.Scope.LOCAL; + return IRParser.Context.Scope.CLOSURE; } +/* public void newObjectStack() { objectStack.push(new LinkedList<>()); } - public void popObjectStack() { objectStack.pop(); } - public void push(Object object) { if (objectStack.isEmpty()) { newObjectStack(); @@ -165,6 +163,7 @@ public Object pop() { public Object peek() { return objectStack.peek(); } +*/ SymbolReference createSymbolReference(String name, SymbolReference.Kind kind, SymbolReference.Scope scope) { SymbolReference ref = SymbolReference.of(name, kind, scope); @@ -178,11 +177,13 @@ public SymbolReference createSymbolReference(String name, Node.Tag tag) { return createSymbolReference(name, getSymbolKind(tag), getSymbolScope()); } +/* public SymbolReference getReference(String name) { assert referenceTableStack.peek() != null; return referenceTableStack.peek() .get(name); } +*/ SymbolReference.Kind getSymbolKind(Node.Tag tag) { if (tag == Node.Tag.VARDECL) { @@ -197,10 +198,10 @@ SymbolReference.Scope getSymbolScope() { // There can be only 2 visible symbol scopes: GLOBAL & LOCAL. // A symbol declared inside a closure is LOCAL to that closure & a CLOSURE itself is LOCAL to its parent block. // So there is no CLOSURE scope for symbols. - ParseIR.Context.Scope scope = currentScope(); - if (scope == ParseIR.Context.Scope.GLOBAL) + IRParser.Context.Scope scope = currentScope(); + if (scope == IRParser.Context.Scope.GLOBAL) return SymbolReference.Scope.GLOBAL; - if (scope == ParseIR.Context.Scope.LOCAL || scope == ParseIR.Context.Scope.CLOSURE) + if (scope == IRParser.Context.Scope.LOCAL || scope == IRParser.Context.Scope.CLOSURE) return SymbolReference.Scope.LOCAL; throw new AssertionError("Invalid Symbol Scope"); } @@ -216,18 +217,19 @@ public void addFunction(CafeFunction function) { private Lexer lexer; private Token token; private Log log; - private boolean breakAllowed = false, innerLoop = false, error = false; - private final List debug = new ArrayList<>(); + private boolean breakAllowed = false; + private boolean error = false; + private List debug = new ArrayList<>(); - private ParseIR() { + private IRParser() { } - private ParseIR(ParserFactory factory, Lexer lexer) { + private IRParser(ParserFactory factory, Lexer lexer) { debug.add("PARSING"); this.lexer = lexer; this.log = factory.log; // TESTING -// nextToken(); + // nextToken(); // while(token.kind != TokenKind.END) { // debug.add(token.kind); // nextToken(); @@ -237,13 +239,13 @@ private ParseIR(ParserFactory factory, Lexer lexer) { } @Override - protected ParseIR instance(ParserFactory factory, Lexer lexer) { - return new ParseIR(factory, lexer); + protected IRParser instance(ParserFactory factory, Lexer lexer) { + return new IRParser(factory, lexer); } - Token token() { - return token; - } +// Token token() { +// return token; +// } Token prevToken() { return lexer.prevToken(); @@ -895,7 +897,7 @@ ExpressionStatement parseThis() { // accept(TokenKind.THIS); // ThisNode thisNode = new ThisNode(); // thisNode.setFirstToken(tk); - ParseIR.Context context = ParseIR.Context.context; + IRParser.Context context = IRParser.Context.context; boolean isGlobal = false; if (context.isModuleScope) { isGlobal = true; @@ -912,7 +914,7 @@ ExpressionStatement parseIdentifier() { accept(TokenKind.IDENTIFIER); IdenNode iden = new IdenNode(prev.value()); iden.setFirstToken(prev); - ParseIR.Context context = ParseIR.Context.context; + IRParser.Context context = IRParser.Context.context; if (context.isProperty()) { PropertyAccess prop = PropertyAccess.of(prev.value()); prop.setFirstToken(prev); @@ -998,21 +1000,21 @@ ExpressionStatement parseBoolLiteral() { return null; } - void parseSubscriptList() { - /* - * while(LBRACKET) parseSubscript() - * - */ - } +// void parseSubscriptList() { +// /* +// * while(LBRACKET) parseSubscript() +// * +// */ +// } - void parseSubscript() { - /* - * accept(LBRACKET) parseNumberLiteral() if ( COLON) parseNumberLiteral() - * accept(LBRACKET) - * - * - */ - } +// void parseSubscript() { +// /* +// * accept(LBRACKET) parseNumberLiteral() if ( COLON) parseNumberLiteral() +// * accept(LBRACKET) +// * +// * +// */ +// } List> parseArgList() { /* @@ -1032,11 +1034,11 @@ List> parseArgList() { } - void parseArg() { - /* - * parseValue() - */ - } +// void parseArg() { +// /* +// * parseValue() +// */ +// } //void parseTrailer() { /* @@ -1046,13 +1048,13 @@ void parseArg() { */ //} - void parseExpressionStatement() { - /* - * parseLogAndExpression() if (|| | 'or') parseLogAndExpression() else if (EQUAL - * OPERATOR) parseEqualOperator() parseValue() else handle Error - * - */ - } +// void parseExpressionStatement() { +// /* +// * parseLogAndExpression() if (|| | 'or') parseLogAndExpression() else if (EQUAL +// * OPERATOR) parseEqualOperator() parseValue() else handle Error +// * +// */ +// } /* parseStatements */ ConditionalBranching parseIf() { @@ -1120,32 +1122,32 @@ CafeStatement parseIfStatement() { return null; } - void parseElseStatement() { - /* - * accept(ELSE) accept(LCURLY) parseBlockStatements() accept(RCURLY) - */ - } +// void parseElseStatement() { +// /* +// * accept(ELSE) accept(LCURLY) parseBlockStatements() accept(RCURLY) +// */ +// } - void parseElseIfStatement() { - /* - * List of ElseIfNodes - * - * while ( !ELSEIF ){ accept(ELSEIF) accept(LPAREN) parseLogORExpression() - * accept(RPAREN) accept(LCURLY) parseBlockStatements() accept(RCURLY) - * ElseIf.add(ElseIFNode(condition, block) - * - * return ElseIfNode - */ - } +// void parseElseIfStatement() { +// /* +// * List of ElseIfNodes +// * +// * while ( !ELSEIF ){ accept(ELSEIF) accept(LPAREN) parseLogORExpression() +// * accept(RPAREN) accept(LCURLY) parseBlockStatements() accept(RCURLY) +// * ElseIf.add(ElseIFNode(condition, block) +// * +// * return ElseIfNode +// */ +// } - StmtNode parseAssignmentStatement() { +// StmtNode parseAssignmentStatement() { /* * parseIdentifier() while (DOT) parseIdentifier() parseEqualOperator() * parseValue() accept(SEMI) * * Not used */ - if (error) return null; +// if (error) return null; // // ExpressionStatement parseForInit() { @@ -1299,44 +1301,44 @@ CafeStatement parseForStatement() { return forLoop; } - CafeStatement parseLoopStatement() { - /* - * accept(LOOP) parseLoopIdentifier() accept(IN) parseLoopValue() - * parseLoopBlock() parseCollectionComprehension() - * - */ - if (error) return null; - ExpressionStatement iden1=null, iden2=null; - ExpressionStatement exp = null; - Block block=null; - - accept(TokenKind.LOOP); - iden1 = parseIdentifier(); - if (token.kind == TokenKind.COMMA) { - nextToken(); - iden2 = parseIdentifier(); - } - accept(TokenKind.IN); - try { - exp = parseAtomExpression(); - } catch (ParseException ignored) { - } - - if (exp == null) { - if (token.kind == TokenKind.LCURLY) - exp = parseCollection(); - else - exp = parseObjectCreation(); - - } - accept(TokenKind.LCURLY); - block = parseLoopBlock(); - accept(TokenKind.RCURLY); - if (error) return null; - //TODO: Implementation of LoopStmt IR node is yet to be done. +// CafeStatement parseLoopStatement() { +// /* +// * accept(LOOP) parseLoopIdentifier() accept(IN) parseLoopValue() +// * parseLoopBlock() parseCollectionComprehension() +// * +// */ +// if (error) return null; +// ExpressionStatement iden1 = null, iden2 = null; +// ExpressionStatement exp = null; +// Block block=null; +// +// accept(TokenKind.LOOP); +// iden1 = parseIdentifier(); +// if (token.kind == TokenKind.COMMA) { +// nextToken(); +// iden2 = parseIdentifier(); +// } +// accept(TokenKind.IN); +// try { +// exp = parseAtomExpression(); +// } catch (ParseException ignored) { +// } +// +// if (exp == null) { +// if (token.kind == TokenKind.LCURLY) +// exp = parseCollection(); +// else +// exp = parseObjectCreation(); +// +// } +// accept(TokenKind.LCURLY); +// block = parseLoopBlock(); +// accept(TokenKind.RCURLY); +// if (error) return null; +// TODO: Implementation of LoopStmt IR node is yet to be done. // return new LoopStmtNode(iden1, iden2, exp, block); - return null; - } +// return null; +// } CafeStatement parseFlowStatement() { /* @@ -1385,13 +1387,13 @@ ExpressionStatement parseList() { ListCollection listNode = ListCollection.list(); ExpressionStatement exp1; exp1 = parseValue(); - if (token.kind == TokenKind.RANGE) { +// if (token.kind == TokenKind.RANGE) { // TODO: to implement IR node implementation for RANGE // accept(TokenKind.RANGE); // exp2 = parseValue(); // accept(TokenKind.RSQU); // return new RangeNode(exp1, exp2, RangeNode.Type.LIST); - } +// } listNode.add(exp1); while (token.kind != TokenKind.RSQU) { if (error) @@ -1432,17 +1434,16 @@ ExpressionStatement parseListCollection() { } } - void parseMap() { // NOT Used by MapColl - /* - * List of Map - * - * case RBRACKET: return List.add(map)) case LBRACKET: map.addKey(parseValue()) - * accept(COMMA) map.addValue(parseValue()) List.add(map) - * - * return map - */ - - } +// void parseMap() { // NOT Used by MapColl +// /* +// * List of Map +// * +// * case RBRACKET: return List.add(map)) case LBRACKET: map.addKey(parseValue()) +// * accept(COMMA) map.addValue(parseValue()) List.add(map) +// * +// * return map +// */ +// } ExpressionStatement parseMapCollection() { /* @@ -1489,7 +1490,7 @@ ExpressionStatement parseMapCollection() { return null; } - ExpressionStatement parseComprehension(String type) { +// ExpressionStatement parseComprehension(String type) { // if (error) return null; // IdenNode iden1, iden2 = null; // ExprNode exp = null; @@ -1540,10 +1541,10 @@ ExpressionStatement parseComprehension(String type) { // accept(TokenKind.RSQU); // if (error) return null; // return mapComp; - return null; - } +// return null; +// } - ExpressionStatement parseSet() { +// ExpressionStatement parseSet() { // Not Used /* * List of Values * @@ -1575,8 +1576,8 @@ ExpressionStatement parseSet() { // accept(TokenKind.RSQU); // if (error) return null; // return new SetCollNode(setNode); - return null; - } +// return null; +// } ExpressionStatement parseSetCollection() { /* @@ -1606,7 +1607,7 @@ ExpressionStatement parseSetCollection() { return null; } - ExpressionStatement parseLink() { +// ExpressionStatement parseLink() { // Not Used /* * List of Values * @@ -1638,8 +1639,8 @@ ExpressionStatement parseLink() { // accept(TokenKind.RSQU); // if (error) return null; // return new LinkCollNode(listNode); - return null; - } +// return null; +// } ExpressionStatement parseLinkCollection() { /* @@ -1739,7 +1740,7 @@ ExpressionStatement parseObjectCreation() { } - void parseObject() { // Not Used +// void parseObject() { // Not Used /* * accept(LCURLY) parseIdentifier() * @@ -1752,7 +1753,7 @@ void parseObject() { // Not Used * * return ObjectNode */ - } +// } AnonymousFunction parseAnnFunction() { /* @@ -1783,7 +1784,7 @@ AnonymousFunction parseAnnFunction() { context.addFunction(function); ExpressionStatement expression; - if (context.currentScope() == ParseIR.Context.Scope.CLOSURE) { + if (context.currentScope() == IRParser.Context.Scope.CLOSURE) { expression = function.asClosure(); } else { expression = FunctionWrapper.wrap(function); @@ -1846,15 +1847,15 @@ ExpressionStatement parseValue() { /* Parse Values DOne */ - void parseVariableDeclaration() { - /* - * - * parseIdentifier() parseEqualOperator() parseValue() - * - * return VariableNode - */ - - } +// void parseVariableDeclaration() { +// /* +// * +// * parseIdentifier() parseEqualOperator() parseValue() +// * +// * return VariableNode +// */ +// +// } List parseVariable() { /* @@ -1942,7 +1943,7 @@ List parseParameter() { * return List */ if (error) return null; - boolean varArg = false; +// boolean varArg = false; List idenNodes = new ArrayList<>(); while (token.kind != TokenKind.RPAREN) { @@ -1953,7 +1954,7 @@ List parseParameter() { if (token.kind == TokenKind.VARARGS) { accept(TokenKind.VARARGS); - varArg = true; +// varArg = true; idenNodes.add(parseIdentifier().getName()); // accept(TokenKind.RPAREN); break; @@ -1984,7 +1985,7 @@ DeclarativeAssignmentStatement parseFunctionDeclaration() { if (error) return null; Context context = Context.context; accept(TokenKind.FUNC); - Token tk = token; +// Token tk = token; ExpressionStatement funcName = parseIdentifier(); context.enterFunc(funcName.getName()); accept(TokenKind.LPAREN); @@ -2003,8 +2004,8 @@ DeclarativeAssignmentStatement parseFunctionDeclaration() { context.addFunction(function); - ExpressionStatement expression=null; - if (context.currentScope() == ParseIR.Context.Scope.CLOSURE) { + ExpressionStatement expression; + if (context.currentScope() == IRParser.Context.Scope.CLOSURE) { expression = function.asClosure(); } else { expression = FunctionWrapper.wrap(function); @@ -2021,47 +2022,45 @@ DeclarativeAssignmentStatement parseFunctionDeclaration() { return statement; } - void parseDeclarativeStatement() { - /* - * List of Declarative Statement - * - * Checks Type of Statement and calls below methods to parse calls parseFunction - * calls parseVariable - */ - } - - void block() { - /* - * Handles Cases For Each Type of Block Statements like DECL, ASGN, IF etc. and - * calls respective methods - * - */ - } - - void parseBlockStatement() { - /* - * List of Statement - * - * parse Grammar, checks type of Statement and calls block() - */ +// void parseDeclarativeStatement() { +// /* +// * List of Declarative Statement +// * +// * Checks Type of Statement and calls below methods to parse calls parseFunction +// * calls parseVariable +// */ +// } - /* - * switch(token.kind){ case VAR } - */ +// void block() { +// /* +// * Handles Cases For Each Type of Block Statements like DECL, ASGN, IF etc. and +// * calls respective methods +// * +// */ +// } - } +// void parseBlockStatement() { +// /* +// * List of Statement +// * +// * parse Grammar, checks type of Statement and calls block() +// */ +// +// /* +// * switch(token.kind){ case VAR } +// */ +// } CafeStatement parseReturnStatement() { if (error) return null; accept(TokenKind.RET); - Token tk = token; +// Token tk = token; ExpressionStatement exp = parseValue(); debug.add("Return : " + exp); accept(TokenKind.SEMICOLON); if (error) return null; - ReturnStatement returnStatement = ReturnStatement.of(exp); -// rtrnNode.setFirstToken(tk); - return returnStatement; + // rtrnNode.setFirstToken(tk); + return ReturnStatement.of(exp); } Block parseLoopBlock() { @@ -2118,18 +2117,18 @@ List> parseStatement() { block.add(stm2); break; case FOR: - innerLoop = breakAllowed; + boolean innerLoop = breakAllowed; breakAllowed = true; CafeStatement stm3 = parseForStatement(); if (stm3 == null) return null; block.add(stm3); breakAllowed = innerLoop; - innerLoop = false; +// innerLoop = false; break; case LOOP: - CafeStatement stm4 = parseLoopStatement(); - if (stm4 == null) return null; - block.add(stm4); +// CafeStatement stm4 = parseLoopStatement(); +// if (stm4 == null) return null; +// block.add(stm4); break; case RET: CafeStatement stm5 = parseReturnStatement(); @@ -2194,7 +2193,7 @@ Block parseBlock() { } List parseExportStatement() { - List exportStmtNode = new ArrayList(); + List exportStmtNode = new ArrayList<>(); Context context = Context.context; context.isExport = true; @@ -2263,11 +2262,10 @@ CafeImport parseImportStatement() { // boolean valid = checkFilePathRegex(token.value()); // if(valid) return ImportStatement(token.value()) // else Throw Error - ImportStmtNode importStmtNode = null; - Map blocks = new HashMap(); +// ImportStmtNode importStmtNode = null; + Map blocks = new HashMap<>(); ExpressionStatement id1, id2 = null; Context context = Context.context; -// // for (Map.Entry entry : n.importAliasMap.entrySet()) { // Node.IdenNode value = entry.getValue(); // String alias = null; @@ -2385,10 +2383,9 @@ ProgramNode parseProgram() { // TODO: ProgramNode node; // node = parseStatements() // return node - if (error) return null; - ProgramNode node =null; - if (error) return null; - return node; +// if (error) return null; +// ProgramNode node =null; + return null; } @Override diff --git a/src/main/java/compiler/parser/MainParser.java b/src/main/java/compiler/parser/MainParser.java index 7435a05..e6efb1c 100644 --- a/src/main/java/compiler/parser/MainParser.java +++ b/src/main/java/compiler/parser/MainParser.java @@ -47,7 +47,7 @@ public class MainParser extends Parser { static { - ParserFactory.registerParser(ParserType.MAINPARSER, new MainParser()); +// ParserFactory.registerParser(ParserType.IRParser, new IRParser()); } @Override diff --git a/src/main/java/compiler/parser/ParserType.java b/src/main/java/compiler/parser/ParserType.java index 5cd953a..9e47669 100644 --- a/src/main/java/compiler/parser/ParserType.java +++ b/src/main/java/compiler/parser/ParserType.java @@ -35,8 +35,8 @@ * @author Dhyey */ public enum ParserType { - MAINPARSER("ParseIR"); - +// MAINPARSER("MainParser"); + IRParser("IRParser"); String parserClassName; ParserType(String className) { diff --git a/src/test/java/compiler/parser/MainParserTest.java b/src/test/java/compiler/parser/MainParserTest.java index 030eb37..133c3a9 100644 --- a/src/test/java/compiler/parser/MainParserTest.java +++ b/src/test/java/compiler/parser/MainParserTest.java @@ -71,7 +71,7 @@ public void init() { @Test(dataProvider = "cafe-files") public void test_no_error(File file) { fm.setSourceFile(file); - Parser parser = factory.newParser(ParserType.MAINPARSER, fm.asCharList()); + Parser parser = factory.newParser(ParserType.IRParser, fm.asCharList()); CafeModule module = parser.parseToIR(file.getName()); System.out.println("Success "+module); // Node n = parser.parse(); @@ -81,7 +81,7 @@ public void test_no_error(File file) { @Test(dataProvider = "cafe-error-files") public void test_error(File file) { fm.setSourceFile(file); - Parser parser = factory.newParser(ParserType.MAINPARSER, fm.asCharList()); + Parser parser = factory.newParser(ParserType.IRParser, fm.asCharList()); CafeModule module = parser.parseToIR(file.getName()); System.out.println("Success "+module); // Node n = parser.parse(); From 6d772ce96762e890c802f7f33722b4f08d8b0a93 Mon Sep 17 00:00:00 2001 From: Dhyey Date: Mon, 5 Apr 2021 19:08:15 +0530 Subject: [PATCH 08/14] renamed MainParser to ASTParser --- .../parser/{MainParser.java => ASTParser.java} | 14 +++++++------- src/main/java/compiler/parser/ParserType.java | 2 +- .../{MainParserTest.java => ASTParserTest.java} | 3 +-- 3 files changed, 9 insertions(+), 10 deletions(-) rename src/main/java/compiler/parser/{MainParser.java => ASTParser.java} (99%) rename src/test/java/compiler/parser/{MainParserTest.java => ASTParserTest.java} (98%) diff --git a/src/main/java/compiler/parser/MainParser.java b/src/main/java/compiler/parser/ASTParser.java similarity index 99% rename from src/main/java/compiler/parser/MainParser.java rename to src/main/java/compiler/parser/ASTParser.java index e6efb1c..0642d96 100644 --- a/src/main/java/compiler/parser/MainParser.java +++ b/src/main/java/compiler/parser/ASTParser.java @@ -45,13 +45,13 @@ import static compiler.util.Log.Type.*; import static compiler.util.Messages.message; -public class MainParser extends Parser { +public class ASTParser extends Parser { static { -// ParserFactory.registerParser(ParserType.IRParser, new IRParser()); + ParserFactory.registerParser(ParserType.ASTParser, new ASTParser()); } @Override - public CafeModule parseToIR(String mod){ + public CafeModule parseToIR(String mod) { return CafeModule.create(mod, new ReferenceTable()); } @@ -61,10 +61,10 @@ public CafeModule parseToIR(String mod){ private boolean breakAllowed = false, innerLoop = false, error = false; private List debug = new ArrayList<>(); - private MainParser() { + private ASTParser() { } - private MainParser(ParserFactory factory, Lexer lexer) { + private ASTParser(ParserFactory factory, Lexer lexer) { debug.add("PARSING"); this.lexer = lexer; this.log = factory.log; @@ -79,8 +79,8 @@ private MainParser(ParserFactory factory, Lexer lexer) { } @Override - protected MainParser instance(ParserFactory factory, Lexer lexer) { - return new MainParser(factory, lexer); + protected ASTParser instance(ParserFactory factory, Lexer lexer) { + return new ASTParser(factory, lexer); } Token token() { diff --git a/src/main/java/compiler/parser/ParserType.java b/src/main/java/compiler/parser/ParserType.java index 9e47669..8d72259 100644 --- a/src/main/java/compiler/parser/ParserType.java +++ b/src/main/java/compiler/parser/ParserType.java @@ -35,7 +35,7 @@ * @author Dhyey */ public enum ParserType { -// MAINPARSER("MainParser"); + ASTParser("ASTParser"), IRParser("IRParser"); String parserClassName; diff --git a/src/test/java/compiler/parser/MainParserTest.java b/src/test/java/compiler/parser/ASTParserTest.java similarity index 98% rename from src/test/java/compiler/parser/MainParserTest.java rename to src/test/java/compiler/parser/ASTParserTest.java index 133c3a9..2c576cd 100644 --- a/src/test/java/compiler/parser/MainParserTest.java +++ b/src/test/java/compiler/parser/ASTParserTest.java @@ -29,7 +29,6 @@ package compiler.parser; -import compiler.ast.Node; import compiler.ir.CafeModule; import compiler.main.SourceFileManager; import compiler.util.Context; @@ -44,7 +43,7 @@ import static org.testng.Assert.assertNotNull; import static org.testng.Assert.assertNull; -public class MainParserTest { +public class ASTParserTest { public static final String SRC = "src/test/resources/parse-test/"; public static final String ERR = "src/test/resources/parse-test/error_test"; From fc97166fb0a3ec3877e0031641199229c1287ab8 Mon Sep 17 00:00:00 2001 From: SaurabhPethani Date: Mon, 5 Apr 2021 22:00:12 +0530 Subject: [PATCH 09/14] IRParser test passed, except index errors --- .../ir/DeclarativeAssignmentStatement.java | 4 ++ src/main/java/compiler/parser/IRParser.java | 36 +++++++++++++----- .../as-closure/ann_func_export.class | Bin 1894 -> 1598 bytes 3 files changed, 30 insertions(+), 10 deletions(-) diff --git a/src/main/java/compiler/ir/DeclarativeAssignmentStatement.java b/src/main/java/compiler/ir/DeclarativeAssignmentStatement.java index 5db217f..a6809a1 100644 --- a/src/main/java/compiler/ir/DeclarativeAssignmentStatement.java +++ b/src/main/java/compiler/ir/DeclarativeAssignmentStatement.java @@ -64,6 +64,10 @@ public SymbolReference getSymbolReference() { return symbolReference; } + @Override + public String getName(){ + return symbolReference.getName(); + } @Override public List> children() { return Collections.singletonList(expressionStatement); diff --git a/src/main/java/compiler/parser/IRParser.java b/src/main/java/compiler/parser/IRParser.java index 2e85f95..616ec4d 100644 --- a/src/main/java/compiler/parser/IRParser.java +++ b/src/main/java/compiler/parser/IRParser.java @@ -436,7 +436,11 @@ ExpressionStatement parseRelationalExpression() { op = OperatorType.NOTIN; accept(TokenKind.IN); } + }else{ + accept(token.kind); } + System.out.println("Relational Operator type: "+op); + ExpressionStatement exp2 = parseBitOrExpression(); exp1 = BinaryExpression.of(op) .right(exp2) @@ -664,7 +668,7 @@ ExpressionStatement parseFactorExpression() { OperatorType op = token.kind == TokenKind.SUB ? OperatorType.MINUS : TokenKind.TILDE == token.kind ? OperatorType.TILDE : TokenKind.NOT == token.kind ? OperatorType.NOT : OperatorType.NOTOP; accept(token.kind); exp1 = parsePowerExpression(); - exp1 = UnaryExpression.create(exp1, op); + exp1 = UnaryExpression.create(op, exp1); } } else { exp1 = parsePowerExpression(); @@ -894,7 +898,7 @@ ExpressionStatement parseNull() { ExpressionStatement parseThis() { if (error) return null; // Token tk = token; -// accept(TokenKind.THIS); + accept(TokenKind.THIS); // ThisNode thisNode = new ThisNode(); // thisNode.setFirstToken(tk); IRParser.Context context = IRParser.Context.context; @@ -1111,7 +1115,7 @@ CafeStatement parseIfStatement() { } else if (accept(TokenKind.LCURLY)) { Block blockNode = parseBlock(); accept(TokenKind.RCURLY); - elseBlock = ifNode.elseBranch(blockNode); + elseBlock = ifNode.whenFalse(blockNode); // elseBlock.setFirstToken(elseFT); } if (error) return null; @@ -1762,10 +1766,11 @@ AnonymousFunction parseAnnFunction() { */ if (error) return null; Context context = Context.context; + String funcName = "#_ANN_Hello"; accept(TokenKind.FUNC); - String funcName = context.getNextAnnFuncName(); - context.enterFunc(funcName); accept(TokenKind.LPAREN); + context.enterFunc(funcName); + funcName = context.getNextAnnFuncName(); List params = parseParameter(); accept(TokenKind.RPAREN); accept(TokenKind.LCURLY); @@ -2051,7 +2056,7 @@ DeclarativeAssignmentStatement parseFunctionDeclaration() { // */ // } - CafeStatement parseReturnStatement() { + ReturnStatement parseReturnStatement() { if (error) return null; accept(TokenKind.RET); // Token tk = token; @@ -2098,21 +2103,25 @@ List> parseStatement() { switch (token.kind) { case VAR: List stm = parseVariable(); + System.out.println("Var Statement: "+stm); if (stm == null) return null; block.addAll(stm); break; case CONST: List stm1 = parseConstVariable(); + System.out.println("const Statement: "+stm1); if (stm1 == null) return null; block.addAll(stm1); break; case FUNC: DeclarativeAssignmentStatement decl = parseFunctionDeclaration(); + System.out.println("For Statement: "+decl); if (decl == null) return null; block.add(decl); break; case IF: CafeStatement stm2 = parseIfStatement(); + System.out.println("If Statement: "+stm2); if (stm2 == null) return null; block.add(stm2); break; @@ -2120,12 +2129,14 @@ List> parseStatement() { boolean innerLoop = breakAllowed; breakAllowed = true; CafeStatement stm3 = parseForStatement(); + System.out.println("for Statement: "+stm3); if (stm3 == null) return null; block.add(stm3); breakAllowed = innerLoop; // innerLoop = false; break; case LOOP: + System.out.println("Entered to Loop:"); // CafeStatement stm4 = parseLoopStatement(); // if (stm4 == null) return null; // block.add(stm4); @@ -2141,19 +2152,22 @@ List> parseStatement() { CafeStatement stm6 = parseExprStmt(); if (stm6 == null) return null; block.add(stm6); - debug.add("Block Stmt: " + token.kind); + System.out.println("Iden This Block Stmt: " + stm6+error); + System.out.println("Iden This BlockToken: " + token.value()); accept(TokenKind.SEMICOLON); break; case BREAK: case CONTINUE: CafeStatement stm7 = parseFlowStatement(); block.add(stm7); - debug.add("Block Stmt: " + token.kind); + System.out.println("Break Continue Block Stmt: " + stm7); + debug.add("Break Continue Block Stmt: " + token.kind); break; default: logError(TokenKind.IDENTIFIER); } - if(error || block.isEmpty()) return null; + if(error) return null; + System.out.println("Block Statement: "+block); return block; } // return Block Statement Node @@ -2175,10 +2189,11 @@ Block parseBlock() { List> blockStmt = new ArrayList<>(); debug.add("Token kind " + token.kind); - + System.out.println("Block Entered"); while(token.kind != TokenKind.RCURLY) { if (error) return null; List> stm = parseStatement(); + System.out.println("Block Stmt: "+stm); if (stm == null) return null; blockStmt.addAll(stm); } @@ -2189,6 +2204,7 @@ Block parseBlock() { context.leaveScope(); if (error) return null; debug.add("Block Stmt: " + block); + System.out.println("Block: "+block); return block; } diff --git a/src/test/resources/ann-func-test/as-closure/ann_func_export.class b/src/test/resources/ann-func-test/as-closure/ann_func_export.class index 71faeb5de790f244536af1d56e5b938404fe1622..49b9e01a1ae1391fc04d2a01cbdc0fb2f58758a1 100644 GIT binary patch delta 437 zcmZWlT}#4H7=F%V&aKUKL4-G1Mv_HqQ5QiGVlB&DA8O{+7$H~$Nm@h%{fPaMq>Gk; z_w_@%>n}*}S&>EO;(5<`pO5$DyhnSg^49P604$-_QTi+vOF3EFcC3n>bIfJO^hO0s z(|$J}8$ywlBB}aK^Ww7IQn7)w0tK54@iX_@HLlvH=SJRbHHvOi!j^(OBnEaH!H$Y? zB)NE(K^uqzq3tEaTx1^yL)gQiR~JuM*?SdJgdbv(FdCjDG{Y13Wm-56%A_j#n5lN? z0?5c<76Ldu{7r}S`Gm3=+$qj*N|nH{AIJ z`aO(5qO&A~Yd!>*ehlP2Wx;VS&U^Aa&-uOYIp@mXsC;?tp92`h#<6_OqEXG7HViAX zYNV_&%j*#`iqa&VZVNeJw>%T=fB&bbHWk{z7V65cX=_kIW_D|!j>krxkj^=4(vBiH@_2E*t;_8xcA>lI># zg4bA3!LY~>);7QHl`GYK1T2Lo43nqn8&;%<_Cuqy0+kqy3-w^vI(B>4HElf$#~slfR(-q%#mf3~|zVqLV65 zwA3+Z9&?xMB!=iBx7!)42nA4` zDqos9 Date: Tue, 6 Apr 2021 15:10:16 +0530 Subject: [PATCH 10/14] export issue resolved --- src/main/java/compiler/parser/IRParser.java | 28 ++++++++++-------- .../as-closure/ann_func_export.class | Bin 1598 -> 1897 bytes 2 files changed, 15 insertions(+), 13 deletions(-) diff --git a/src/main/java/compiler/parser/IRParser.java b/src/main/java/compiler/parser/IRParser.java index 616ec4d..631c650 100644 --- a/src/main/java/compiler/parser/IRParser.java +++ b/src/main/java/compiler/parser/IRParser.java @@ -2208,7 +2208,7 @@ Block parseBlock() { return block; } - List parseExportStatement() { + List parseExportStatement() { List exportStmtNode = new ArrayList<>(); Context context = Context.context; @@ -2222,7 +2222,7 @@ List parseExportStatement() { // } else { // n.node.accept(this); // } - + List decl = new ArrayList<>(); accept(TokenKind.EXPORT); switch (token.kind) { @@ -2240,23 +2240,25 @@ List parseExportStatement() { break; case VAR: - List stm = parseVariable(); - if (stm == null) return null; - for (DeclarativeAssignmentStatement var : stm) { +// List stm = parseVariable(); + decl = parseVariable(); + if (decl == null) return null; + for (DeclarativeAssignmentStatement var : decl) { exportStmtNode.add(CafeExport.export(var.getSymbolReference().getName())); } break; case CONST: - List stm1 = parseConstVariable(); - if (stm1 == null) return null; - for (DeclarativeAssignmentStatement var : stm1) { + decl = parseConstVariable(); + if (decl == null) return null; + for (DeclarativeAssignmentStatement var : decl) { exportStmtNode.add(CafeExport.export(var.getSymbolReference().getName())); } break; case FUNC: - DeclarativeAssignmentStatement decl = parseFunctionDeclaration(); - if (decl == null) return null; - exportStmtNode.add(CafeExport.export(decl.getName())); + DeclarativeAssignmentStatement funcDecl = parseFunctionDeclaration(); + if (funcDecl == null) return null; + exportStmtNode.add(CafeExport.export(funcDecl.getName())); + decl.add(funcDecl); break; default: error = true; @@ -2267,7 +2269,7 @@ List parseExportStatement() { context.module.addExport(expo); } if (error) return null; - return exportStmtNode; + return decl; } // return Import Statement Node @@ -2369,7 +2371,7 @@ CafeModule parseStatements() { tree.add(importStmtNode); break; case EXPORT: - List exports = parseExportStatement(); + List exports = parseExportStatement(); if(exports == null) return null; tree.addAll(exports); break; diff --git a/src/test/resources/ann-func-test/as-closure/ann_func_export.class b/src/test/resources/ann-func-test/as-closure/ann_func_export.class index 49b9e01a1ae1391fc04d2a01cbdc0fb2f58758a1..7cbf3f5c564a8e97b3ca8808d73968e63a6b4c25 100644 GIT binary patch delta 571 zcmY+AO-~b16o#KWGdS%~1x6CxplFN5Y8*8zG=306L6CxKppHR;o8il*K=jMIi`SKm-jW>Rsk(TbYk`xP&YmNz{uH^X`~6>ZdOL3%3NJWza!w+&w{ z#6{~&JY@C{R2-(6={d%%Vq~~-gnKIJ7qJX7?& z3pN6;)eKj?$zc8MOt5A%@8Dz0p#Qy8ug2ZhSZ0Bj-MrvcJa5(1Vw_lc*?(Gtvj4Hp zZ!OBx`!S>?WOg}K+9Us2&XA?Rkmx$mCsil9+FbN^v@3RrOL9@$JssP0uN>TcBVw20 zviJ_(zud`ABRk=3#JQQ0HRcVsM@Ojzvt%or}SP zft`_ov5JGii-$p;K>;M^!^ps!l~|UjUs{rxqwkwoz|P<|S(eqABY+{0o57zUXmSQ? zGGoZ(Gpx!$@&l^^kQ8K70+J?dia;`qO-?feXgfax&}1OZETp#qNHH*k0x>HigAM}| z&=!3LZ6F;oc`lnoJ(2_`gBpW612Y5204bmX5a1KC-^Jj_z_5eCc?W|V#H=u&I1_^{ zl9>XWAkTr!1WKs@jR4s#v75moayx^kx0aB%koOJ--<=G>%nadLD2B)YHHCxC<79{c Q(mX(|mOujx7z`PV0KvjE%K!iX From b5af797fea13b358ff45d380609d6ebe5a29a949 Mon Sep 17 00:00:00 2001 From: Dhyey Date: Tue, 6 Apr 2021 15:30:08 +0530 Subject: [PATCH 11/14] generalized parse() method for all parser implementations. --- src/main/java/compiler/main/CafeCompiler.java | 31 ++++++++------- src/main/java/compiler/parser/ASTParser.java | 9 +---- src/main/java/compiler/parser/IRParser.java | 36 ++++-------------- src/main/java/compiler/parser/Parser.java | 7 +--- .../java/compiler/parser/ASTParserTest.java | 4 +- .../ann-func-test/anonymous_func_export.class | Bin 1514 -> 1515 bytes 6 files changed, 27 insertions(+), 60 deletions(-) diff --git a/src/main/java/compiler/main/CafeCompiler.java b/src/main/java/compiler/main/CafeCompiler.java index 5ab9dc5..c289778 100644 --- a/src/main/java/compiler/main/CafeCompiler.java +++ b/src/main/java/compiler/main/CafeCompiler.java @@ -31,8 +31,6 @@ import compiler.analyzer.SemanticsChecker; import compiler.ast.Node; -import compiler.ast.Node.ProgramNode; -import compiler.gen.ASTToCafeIrVisitor; import compiler.gen.ClosureReferenceVisitor; import compiler.gen.JVMByteCodeGenVisitor; import compiler.gen.SymbolReferenceAssignmentVisitor; @@ -85,8 +83,9 @@ public CafeCompiler(String sourceFile) { enum Phase { INIT, - PARSEIR, - ANALYZE, + PARSE, + IR, + // ANALYZE, GEN } @@ -113,21 +112,21 @@ public CompilerResult compile() { case INIT: fileManager.setSourceFile(sourceFile); break; - case PARSEIR: + case PARSE: parser = parserFactory.newParser(ParserType.IRParser, fileManager.asCharList()); - module = parser.parseToIR(moduleName); - System.out.println("Success "+module); + module = parser.parse(moduleName); + System.out.println("Success " + module); break; - case ANALYZE: -// System.out.println((char) 27 + "[33m" + "\nPrettyPrint"); -// new PrettyPrinter().prettyPrint(programNode); -// analyzer.visitProgram((ProgramNode) programNode); - break; -// case IR: -// module = new ASTToCafeIrVisitor().transform((ProgramNode) programNode, moduleName); -// module.accept(new ClosureReferenceVisitor()); -// module.accept(new SymbolReferenceAssignmentVisitor()); +// case ANALYZE: +//// System.out.println((char) 27 + "[33m" + "\nPrettyPrint"); +//// new PrettyPrinter().prettyPrint(programNode); +//// analyzer.visitProgram((ProgramNode) programNode); // break; + case IR: +// module = new ASTToCafeIrVisitor().transform((ProgramNode) programNode, moduleName); + module.accept(new ClosureReferenceVisitor()); + module.accept(new SymbolReferenceAssignmentVisitor()); + break; case GEN: byteCode = new JVMByteCodeGenVisitor().generateByteCode(module, moduleName); result.ok(byteCode); diff --git a/src/main/java/compiler/parser/ASTParser.java b/src/main/java/compiler/parser/ASTParser.java index 0642d96..abea66a 100644 --- a/src/main/java/compiler/parser/ASTParser.java +++ b/src/main/java/compiler/parser/ASTParser.java @@ -31,8 +31,6 @@ import compiler.ast.Node; import compiler.ast.Node.*; -import compiler.ir.CafeModule; -import compiler.ir.ReferenceTable; import compiler.parser.Tokens.Token; import compiler.parser.Tokens.TokenKind; import compiler.util.Log; @@ -50,11 +48,6 @@ public class ASTParser extends Parser { ParserFactory.registerParser(ParserType.ASTParser, new ASTParser()); } - @Override - public CafeModule parseToIR(String mod) { - return CafeModule.create(mod, new ReferenceTable()); - } - private Lexer lexer; private Token token; private Log log; @@ -2009,7 +2002,7 @@ ProgramNode parseProgram() { } @Override - public Node parse() { + public Node parse(String moduleName) { // while(token.kind != TokenKind.END) { // if(token.kind == TokenKind.ERROR) { // return; diff --git a/src/main/java/compiler/parser/IRParser.java b/src/main/java/compiler/parser/IRParser.java index 631c650..ff26265 100644 --- a/src/main/java/compiler/parser/IRParser.java +++ b/src/main/java/compiler/parser/IRParser.java @@ -30,7 +30,9 @@ package compiler.parser; import compiler.ast.Node; -import compiler.ast.Node.*; +import compiler.ast.Node.ContinueStmtNode; +import compiler.ast.Node.IdenNode; +import compiler.ast.Node.Tag; import compiler.gen.AnnFuncNameGenerator; import compiler.ir.*; import compiler.parser.Tokens.Token; @@ -42,7 +44,8 @@ import java.text.ParseException; import java.util.*; -import static compiler.util.Log.Type.*; +import static compiler.util.Log.Type.SOURCE_POSITION; +import static compiler.util.Log.Type.SYMBOL_EXPECTED; import static compiler.util.Messages.message; public class IRParser extends Parser { @@ -50,7 +53,8 @@ public class IRParser extends Parser { ParserFactory.registerParser(ParserType.IRParser, new IRParser()); } - public CafeModule parseToIR(String moduleName) { + @Override + public CafeModule parse(String moduleName) { IRParser.Context.context.createModule(moduleName); // IRParser.Context.context.newObjectStack(); return parseStatements(); @@ -2396,30 +2400,4 @@ CafeModule parseStatements() { // * switch(token.kind){ case IF: case FOR: case } // */ // } - - ProgramNode parseProgram() { - // TODO: ProgramNode node; - // node = parseStatements() - // return node -// if (error) return null; -// ProgramNode node =null; - return null; - } - - @Override - public Node parse() { - // while(token.kind != TokenKind.END) { - // if(token.kind == TokenKind.ERROR) { - // return; - // } - // debug.add(token.kind+" "+token.value()+" "+token.pos); - // lexer.nextToken(); - // token = lexer.token(); - // } - - // Parser p = parseProgram(); - // return p; - - return parseProgram(); - } } diff --git a/src/main/java/compiler/parser/Parser.java b/src/main/java/compiler/parser/Parser.java index e8fd4c5..e6ff087 100644 --- a/src/main/java/compiler/parser/Parser.java +++ b/src/main/java/compiler/parser/Parser.java @@ -29,11 +29,8 @@ package compiler.parser; -import compiler.ast.Node; -import compiler.ir.CafeModule; - public abstract class Parser { abstract Parser instance(ParserFactory factory, Lexer lexer); - public abstract CafeModule parseToIR(String moduleName); - public abstract Node parse(); + + public abstract T parse(String moduleName); } \ No newline at end of file diff --git a/src/test/java/compiler/parser/ASTParserTest.java b/src/test/java/compiler/parser/ASTParserTest.java index 2c576cd..0dd4ea9 100644 --- a/src/test/java/compiler/parser/ASTParserTest.java +++ b/src/test/java/compiler/parser/ASTParserTest.java @@ -71,7 +71,7 @@ public void init() { public void test_no_error(File file) { fm.setSourceFile(file); Parser parser = factory.newParser(ParserType.IRParser, fm.asCharList()); - CafeModule module = parser.parseToIR(file.getName()); + CafeModule module = parser.parse(file.getName()); System.out.println("Success "+module); // Node n = parser.parse(); assertNotNull(module); @@ -81,7 +81,7 @@ public void test_no_error(File file) { public void test_error(File file) { fm.setSourceFile(file); Parser parser = factory.newParser(ParserType.IRParser, fm.asCharList()); - CafeModule module = parser.parseToIR(file.getName()); + CafeModule module = parser.parse(file.getName()); System.out.println("Success "+module); // Node n = parser.parse(); assertNull(module); diff --git a/src/test/resources/ann-func-test/anonymous_func_export.class b/src/test/resources/ann-func-test/anonymous_func_export.class index c90dd9cf030885daefd54b9c41664f1935e1aa83..d7a6aed9aef00a68ca55966a3f43d5ed4f0cb598 100644 GIT binary patch delta 26 hcmaFG{hE6N9}_RHa=fFTU%XqWpL4w7W+|o%%m8nG2jTz# delta 25 gcmaFO{fc`79}^Fca=fFTU%XqWpL6_XX{HOz0B9@+umAu6 From 1b37d79b9c6148b09a52642a446d700d507bdab5 Mon Sep 17 00:00:00 2001 From: Dhyey Date: Tue, 6 Apr 2021 17:34:38 +0530 Subject: [PATCH 12/14] wrong propertyAccess bug fixed while parsing arguments --- src/main/java/compiler/parser/IRParser.java | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/main/java/compiler/parser/IRParser.java b/src/main/java/compiler/parser/IRParser.java index ff26265..d03a612 100644 --- a/src/main/java/compiler/parser/IRParser.java +++ b/src/main/java/compiler/parser/IRParser.java @@ -711,30 +711,35 @@ ExpressionStatement parsePowerExpression() { ExpressionStatement parseTrailer(ExpressionStatement oExp) throws ParseException { ExpressionStatement node = null; + Context context = Context.context; + boolean isProperty = context.isProperty(); if (token.kind == TokenKind.LPAREN || token.kind == TokenKind.DOT || token.kind == TokenKind.LSQU) { switch (token.kind) { case LPAREN: accept(TokenKind.LPAREN); - Context context = Context.context; // // eg: a.b() // // b is a property of a, thus method invocation node is created. if (context.isProperty()) { // n.invokedOn.accept(this); + context.leaveProperty(); node = MethodInvocation.create(oExp, parseArgList()); } else { // eg: a() // a() is normal function call, thus function invocation node is created. + context.leaveProperty(); node = FunctionInvocation.create(oExp, parseArgList()); } // node = new FuncCallNode(oExp, new ArgsListNode(parseArgList())); + if (isProperty) context.enterProperty(); accept(TokenKind.RPAREN); break; case LSQU: ExpressionStatement exp1, exp2; debug.add("Atom Expr: " + token.kind); + context.leaveProperty(); while (token.kind == TokenKind.LSQU) { if (error) return null; @@ -760,13 +765,13 @@ ExpressionStatement parseTrailer(ExpressionStatement oExp) throws ParseExc } } + if (isProperty) context.enterProperty(); break; case DOT: ExpressionStatement e1; debug.add("Atom DOT:" + oExp); accept(TokenKind.DOT); - Context context1 = Context.context; - context1.enterProperty(); + context.enterProperty(); e1 = parseIdentifier(); if (error) return null; @@ -774,7 +779,7 @@ ExpressionStatement parseTrailer(ExpressionStatement oExp) throws ParseExc while (token.kind != TokenKind.DOT && (token.kind == TokenKind.LSQU || token.kind == TokenKind.LPAREN)) { trail = parseTrailer(trail); } - context1.leaveProperty(); + context.leaveProperty(); if (trail == null) node = ObjectAccessStatement.create(oExp, e1); else From 7ac31b8ba432350fda688df6a0d7fd5f0907b454 Mon Sep 17 00:00:00 2001 From: Dhyey Date: Wed, 7 Apr 2021 15:51:52 +0530 Subject: [PATCH 13/14] if-else bug fix --- src/main/java/compiler/parser/IRParser.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/java/compiler/parser/IRParser.java b/src/main/java/compiler/parser/IRParser.java index d03a612..33b6357 100644 --- a/src/main/java/compiler/parser/IRParser.java +++ b/src/main/java/compiler/parser/IRParser.java @@ -1124,7 +1124,6 @@ CafeStatement parseIfStatement() { } else if (accept(TokenKind.LCURLY)) { Block blockNode = parseBlock(); accept(TokenKind.RCURLY); - elseBlock = ifNode.whenFalse(blockNode); // elseBlock.setFirstToken(elseFT); } if (error) return null; From 6b491733052256822e5f9e49253be293486cc638 Mon Sep 17 00:00:00 2001 From: SaurabhPethani Date: Wed, 7 Apr 2021 17:10:41 +0530 Subject: [PATCH 14/14] IRParser Refactored --- src/main/java/compiler/parser/IRParser.java | 403 +------------------- 1 file changed, 2 insertions(+), 401 deletions(-) diff --git a/src/main/java/compiler/parser/IRParser.java b/src/main/java/compiler/parser/IRParser.java index 33b6357..1e507aa 100644 --- a/src/main/java/compiler/parser/IRParser.java +++ b/src/main/java/compiler/parser/IRParser.java @@ -56,7 +56,6 @@ public class IRParser extends Parser { @Override public CafeModule parse(String moduleName) { IRParser.Context.context.createModule(moduleName); -// IRParser.Context.context.newObjectStack(); return parseStatements(); } @@ -66,7 +65,6 @@ private static final class Context { public CafeModule module; private final Deque referenceTableStack = new LinkedList<>(); private final Deque functionStack = new LinkedList<>(); -// private final Deque> objectStack = new LinkedList<>(); private final Deque forLoopStack = new LinkedList<>(); private final AnnFuncNameGenerator annFuncNameGenerator = new AnnFuncNameGenerator(); @@ -142,33 +140,6 @@ public IRParser.Context.Scope currentScope() { return IRParser.Context.Scope.CLOSURE; } -/* - public void newObjectStack() { - objectStack.push(new LinkedList<>()); - } - public void popObjectStack() { - objectStack.pop(); - } - public void push(Object object) { - if (objectStack.isEmpty()) { - newObjectStack(); - } - assert objectStack.peek() != null; - objectStack.peek() - .push(object); - } - - public Object pop() { - assert objectStack.peek() != null; - return objectStack.peek() - .pop(); - } - - public Object peek() { - return objectStack.peek(); - } -*/ - SymbolReference createSymbolReference(String name, SymbolReference.Kind kind, SymbolReference.Scope scope) { SymbolReference ref = SymbolReference.of(name, kind, scope); assert referenceTableStack.peek() != null; @@ -181,13 +152,6 @@ public SymbolReference createSymbolReference(String name, Node.Tag tag) { return createSymbolReference(name, getSymbolKind(tag), getSymbolScope()); } -/* - public SymbolReference getReference(String name) { - assert referenceTableStack.peek() != null; - return referenceTableStack.peek() - .get(name); - } -*/ SymbolReference.Kind getSymbolKind(Node.Tag tag) { if (tag == Node.Tag.VARDECL) { @@ -247,10 +211,6 @@ protected IRParser instance(ParserFactory factory, Lexer lexer) { return new IRParser(factory, lexer); } -// Token token() { -// return token; -// } - Token prevToken() { return lexer.prevToken(); } @@ -281,10 +241,6 @@ private boolean accept(TokenKind tokenKindExpected) { } } -// private boolean accept(TokenKind tokenKindExpected) { -// return accept(tokenKindExpected); -// } - private void logError(TokenKind tokenKindExpected) { error = true; log.report(SYMBOL_EXPECTED, token.pos, @@ -443,7 +399,6 @@ ExpressionStatement parseRelationalExpression() { }else{ accept(token.kind); } - System.out.println("Relational Operator type: "+op); ExpressionStatement exp2 = parseBitOrExpression(); exp1 = BinaryExpression.of(op) @@ -509,13 +464,11 @@ ExpressionStatement parseBitAndExpression() { while (token.kind == TokenKind.ANDOP) { if (error) return null; -// String op = token.value(); accept(token.kind); ExpressionStatement exp2 = parseBitRightShiftExpression(); exp1 = BinaryExpression.of(OperatorType.AND) .right(exp2) .left(exp1); -// exp1 = new BinaryExprNode(exp1, exp2, op); } if (error) return null; return exp1; @@ -537,7 +490,6 @@ ExpressionStatement parseBitRightShiftExpression() { exp1 = BinaryExpression.of(op) .right(exp2) .left(exp1); -// exp1 = new BinaryExprNode(exp1, exp2, op); } if (error) return null; return exp1; @@ -553,13 +505,11 @@ ExpressionStatement parseBitLeftShiftExpression() { while (token.kind == TokenKind.LSHIFT) { if (error) return null; -// String op = token.value(); accept(token.kind); ExpressionStatement exp2 = parseSubtractExpression(); exp1 = BinaryExpression.of(OperatorType.BITLEFTSHIFT) .right(exp2) .left(exp1); -// exp1 = new BinaryExprNode(exp1, exp2, op); } if (error) return null; return exp1; @@ -575,13 +525,11 @@ ExpressionStatement parseSubtractExpression() { while (token.kind == TokenKind.SUB) { if (error) return null; -// String op = token.value(); accept(token.kind); ExpressionStatement exp2 = parseAdditionExpression(); exp1 = BinaryExpression.of(OperatorType.MINUS) .right(exp2) .left(exp1); -// exp1 = new BinaryExprNode(exp1, exp2, op); } if (error) return null; return exp1; @@ -597,13 +545,11 @@ ExpressionStatement parseAdditionExpression() { while (token.kind == TokenKind.ADD) { if (error) return null; -// String op = token.value(); accept(token.kind); ExpressionStatement exp2 = parseMultiplicationExpression(); exp1 = BinaryExpression.of(OperatorType.PLUS) .right(exp2) .left(exp1); -// exp1 = new BinaryExprNode(exp1, exp2, op); } if (error) return null; return exp1; @@ -619,13 +565,11 @@ ExpressionStatement parseMultiplicationExpression() { while (token.kind == TokenKind.MUL) { if (error) return null; -// String op = token.value(); accept(token.kind); ExpressionStatement exp2 = parseDivisionExpression(); exp1 = BinaryExpression.of(OperatorType.TIMES) .right(exp2) .left(exp1); -// exp1 = new BinaryExprNode(exp1, exp2, op); } if (error) return null; return exp1; @@ -644,7 +588,6 @@ ExpressionStatement parseDivisionExpression() { OperatorType op = TokenKind.DIV == token.kind ? OperatorType.DIVIDE : TokenKind.MOD == token.kind ? OperatorType.MODULO : OperatorType.FLOOR; accept(token.kind); ExpressionStatement exp2 = parseFactorExpression(); -// exp1 = new BinaryExprNode(exp1, exp2, op); exp1 = BinaryExpression.of(op) .right(exp2) .left(exp1); @@ -692,13 +635,11 @@ ExpressionStatement parsePowerExpression() { while (token.kind == TokenKind.POWER) { if (error) return null; -// String op = token.value(); accept(TokenKind.POWER); exp2 = parseFactorExpression(); exp1 = BinaryExpression.of(OperatorType.POW) .right(exp2) .left(exp1); -// exp1 = new BinaryExprNode(exp1, exp2, op); } if (error) return null; return exp1; @@ -721,7 +662,6 @@ ExpressionStatement parseTrailer(ExpressionStatement oExp) throws ParseExc // // eg: a.b() // // b is a property of a, thus method invocation node is created. if (context.isProperty()) { -// n.invokedOn.accept(this); context.leaveProperty(); node = MethodInvocation.create(oExp, parseArgList()); } @@ -731,7 +671,6 @@ ExpressionStatement parseTrailer(ExpressionStatement oExp) throws ParseExc context.leaveProperty(); node = FunctionInvocation.create(oExp, parseArgList()); } -// node = new FuncCallNode(oExp, new ArgsListNode(parseArgList())); if (isProperty) context.enterProperty(); accept(TokenKind.RPAREN); break; @@ -756,11 +695,9 @@ ExpressionStatement parseTrailer(ExpressionStatement oExp) throws ParseExc node = SliceExpression.slice(oExp) .beginsAt(exp1) .endsAt(exp2); -// node = new SliceNode(oExp, exp1, exp2); } else { accept(TokenKind.RSQU); node = SubscriptExpression.create(oExp, exp1); -// node = new SubscriptNode(oExp, exp1); oExp = node; } @@ -784,7 +721,6 @@ ExpressionStatement parseTrailer(ExpressionStatement oExp) throws ParseExc node = ObjectAccessStatement.create(oExp, e1); else node= ObjectAccessStatement.create(oExp, trail); -// node = new ObjectAccessNode(oExp, trail); debug.add("Token Kind: " + token.kind); break; } @@ -830,13 +766,11 @@ CafeStatement parseExprStmt() { exp1 = BinaryExpression.of(op) .right(exp2) .left(exp1); -// exp1 = new BinaryExprNode(exp1, exp2, op); } else if (token.kind == TokenKind.EQU) { accept(token.kind); exp2 = parseValue(); return AssignmentStatement.create(exp1, exp2); -// return new AsgnStmtNode(exp1, exp2); } if (error) return null; return exp1; @@ -897,19 +831,12 @@ ExpressionStatement parseAtom() { ExpressionStatement parseNull() { if (error) return null; -// Token tk = token; -// accept(TokenKind.NULL); -// NullNode nullNode = new NullNode(); -// nullNode.setFirstToken(tk); return new NullStatement(); } ExpressionStatement parseThis() { if (error) return null; -// Token tk = token; accept(TokenKind.THIS); -// ThisNode thisNode = new ThisNode(); -// thisNode.setFirstToken(tk); IRParser.Context context = IRParser.Context.context; boolean isGlobal = false; if (context.isModuleScope) { @@ -960,7 +887,6 @@ ExpressionStatement parseNumberLiteral() throws ParseException { accept(TokenKind.NUMLIT); Number num, num2; debug.add("Num Literal PrevToken: " + prevToken.kind); - debug.add("Num Literal Token: " + token.kind); if (token.kind == TokenKind.DOT) { nextToken(); if (token.kind == TokenKind.NUMLIT) { @@ -1000,35 +926,15 @@ ExpressionStatement parseBoolLiteral() { if (token.kind == TokenKind.TRUE) { accept(TokenKind.TRUE); if (error) return null; - - // boolLit.setFirstToken(tk); return new ConstantStatement(true); } else if (token.kind == TokenKind.FALSE) { accept(TokenKind.FALSE); if (error) return null; - - // boolLit.setFirstToken(tk); return new ConstantStatement(false); } return null; } -// void parseSubscriptList() { -// /* -// * while(LBRACKET) parseSubscript() -// * -// */ -// } - -// void parseSubscript() { -// /* -// * accept(LBRACKET) parseNumberLiteral() if ( COLON) parseNumberLiteral() -// * accept(LBRACKET) -// * -// * -// */ -// } - List> parseArgList() { /* * parseArg() while(COMMA) parseArg() @@ -1044,39 +950,13 @@ List> parseArgList() { } if (error) return null; return args; - } -// void parseArg() { -// /* -// * parseValue() -// */ -// } - - //void parseTrailer() { - /* - * if (LPAREN) parseArgList() else if (DOT) parseIdentifier() else if (LBRACKET) - * parseSubscriptList() else return false - * - */ - //} - -// void parseExpressionStatement() { -// /* -// * parseLogAndExpression() if (|| | 'or') parseLogAndExpression() else if (EQUAL -// * OPERATOR) parseEqualOperator() parseValue() else handle Error -// * -// */ -// } - /* parseStatements */ ConditionalBranching parseIf() { if (error) return null; ExpressionStatement ifCond; Block ifBlock; - -// Token firstToken = token; - nextToken(); accept(TokenKind.LPAREN); ifCond = parseLogicalOrExpression(); @@ -1096,9 +976,6 @@ ConditionalBranching parseIf() { accept(TokenKind.RCURLY); if (error) return null; - - // IfStmtNode ifNode = new IfStmtNode(ifCond, ifBlock); -// ifNode.setFirstToken(firstToken); return ConditionalBranching .branch() .condition(ifCond) @@ -1115,16 +992,15 @@ CafeStatement parseIfStatement() { ConditionalBranching ifNode; if ((ifNode = parseIf()) != null) { - CafeStatement elseBlock = null; + CafeStatement elseBlock = null; if (token.kind == TokenKind.ELSE) { // Token elseFT = token; nextToken(); if (token.kind == TokenKind.IF) { elseBlock = parseIfStatement(); } else if (accept(TokenKind.LCURLY)) { - Block blockNode = parseBlock(); + elseBlock = parseBlock(); accept(TokenKind.RCURLY); -// elseBlock.setFirstToken(elseFT); } if (error) return null; ifNode.otherwise(elseBlock); @@ -1134,53 +1010,6 @@ CafeStatement parseIfStatement() { return null; } -// void parseElseStatement() { -// /* -// * accept(ELSE) accept(LCURLY) parseBlockStatements() accept(RCURLY) -// */ -// } - -// void parseElseIfStatement() { -// /* -// * List of ElseIfNodes -// * -// * while ( !ELSEIF ){ accept(ELSEIF) accept(LPAREN) parseLogORExpression() -// * accept(RPAREN) accept(LCURLY) parseBlockStatements() accept(RCURLY) -// * ElseIf.add(ElseIFNode(condition, block) -// * -// * return ElseIfNode -// */ -// } - -// StmtNode parseAssignmentStatement() { - /* - * parseIdentifier() while (DOT) parseIdentifier() parseEqualOperator() - * parseValue() accept(SEMI) - * - * Not used - */ -// if (error) return null; -// -// ExpressionStatement parseForInit() { /* @@ -1254,9 +1083,6 @@ List> parseForIncrement() { if (error) return null; iden = parseIdentifier(); - // accept(TokenKind.DOT); - // ExprNode expn = parseIdentifier(); - // iden = new ObjectAccessNode(iden, expn); Context ctx= Context.context; ctx.enterProperty(); while (token.kind == TokenKind.DOT) { @@ -1265,12 +1091,10 @@ List> parseForIncrement() { accept(TokenKind.DOT); exp2 = parseIdentifier(); iden = ObjectAccessStatement.create(iden, exp2); -// iden = new ObjectAccessNode(iden, exp2); } ctx.leaveProperty(); accept(TokenKind.EQU); val = parseValue(); -// incrNodes.add(new AsgnStmtNode(iden, val)); incrNodes.add(AssignmentStatement.create(iden, val)); if (token.kind == TokenKind.COMMA) nextToken(); @@ -1370,8 +1194,6 @@ CafeStatement parseFlowStatement() { } else { accept(TokenKind.BREAK); accept(TokenKind.SEMICOLON); -// BreakStmtNode breakStmtNode = new BreakStmtNode(); -// breakStmtNode.setFirstToken(tk); return BreakContinueStatement.newBreak() .setEnclosingLoop(context.forLoopStack.peek()); } @@ -1395,7 +1217,6 @@ ExpressionStatement parseList() { * return List */ if (error) return null; -// List listNode = new ArrayList<>(); ListCollection listNode = ListCollection.list(); ExpressionStatement exp1; exp1 = parseValue(); @@ -1426,8 +1247,6 @@ ExpressionStatement parseListCollection() { * if (RBRACKET){ return ListNode() } * * else { return parseList(); } - * - * */ if (error) return null; accept(TokenKind.LSQU); @@ -1446,17 +1265,6 @@ ExpressionStatement parseListCollection() { } } -// void parseMap() { // NOT Used by MapColl -// /* -// * List of Map -// * -// * case RBRACKET: return List.add(map)) case LBRACKET: map.addKey(parseValue()) -// * accept(COMMA) map.addValue(parseValue()) List.add(map) -// * -// * return map -// */ -// } - ExpressionStatement parseMapCollection() { /* * List of MapCollection List of Comp @@ -1554,41 +1362,6 @@ ExpressionStatement parseMapCollection() { // if (error) return null; // return mapComp; // return null; -// } - -// ExpressionStatement parseSet() { // Not Used - /* - * List of Values - * - * accept(LBRACKET) List.add(parseValue()) if(COMMA or DOTDOT) - * List.add(parseValue()) else if(LOOP_KEYWORD) - * List.add(parseForComprehension()) - * - * return List - */ - // Not used -// if (error) return null; -// List setNode = new ArrayList<>(); -// ExprNode exp1, exp2; -// exp1 = parseValue(); -// if (token.kind == TokenKind.RANGE) { -// accept(TokenKind.RANGE); -// exp2 = parseValue(); -// accept(TokenKind.RSQU); -// return new RangeNode(exp1, exp2, RangeNode.Type.SET); -// } -// setNode.add(exp1); -// while (token.kind != TokenKind.RSQU) { -// if (error) -// return null; -// accept(TokenKind.COMMA); -// if (token.kind == TokenKind.RSQU) accept(TokenKind.IDENTIFIER); -// setNode.add(parseValue()); -// } -// accept(TokenKind.RSQU); -// if (error) return null; -// return new SetCollNode(setNode); -// return null; // } ExpressionStatement parseSetCollection() { @@ -1619,41 +1392,6 @@ ExpressionStatement parseSetCollection() { return null; } -// ExpressionStatement parseLink() { // Not Used - /* - * List of Values - * - * accept(LBRACKET) List.add(parseValue()) if(COMMA or DOTDOT) - * List.add(parseValue()) else if(LOOP_KEYWORD) - * List.add(parseForComprehension()) - * - * return List - */ - // not Used -// if (error) return null; -// List listNode = new ArrayList<>(); -// ExprNode exp1, exp2; -// exp1 = parseValue(); -// if (token.kind == TokenKind.RANGE) { -// accept(TokenKind.RANGE); -// exp2 = parseValue(); -// accept(TokenKind.RSQU); -// return new RangeNode(exp1, exp2, RangeNode.Type.LINK); -// } -// listNode.add(exp1); -// while (token.kind != TokenKind.RSQU) { -// if (error) -// return null; -// accept(TokenKind.COMMA); -// if (token.kind == TokenKind.RSQU) accept(TokenKind.IDENTIFIER); -// listNode.add(parseValue()); -// } -// accept(TokenKind.RSQU); -// if (error) return null; -// return new LinkCollNode(listNode); -// return null; -// } - ExpressionStatement parseLinkCollection() { /* * List of Collection @@ -1748,25 +1486,8 @@ ExpressionStatement parseObjectCreation() { accept(TokenKind.RCURLY); if (error) return null; return ObjectCreationStatement.of(object); -// return new ObjCreationNode(object); - } -// void parseObject() { // Not Used - /* - * accept(LCURLY) parseIdentifier() - * - * check Colon - * - * parseValue() - * - * accept(RCURLY) - * - * - * return ObjectNode - */ -// } - AnonymousFunction parseAnnFunction() { /* * accept(FUNC) isAnnFunmc = true parseFunction() @@ -1803,17 +1524,6 @@ AnonymousFunction parseAnnFunction() { expression = FunctionWrapper.wrap(function); } context.leaveFunc(); -// while (token.kind != TokenKind.RCURLY) { -// if (error) -// return null; -// List stm = parseBlock(); -// if (stm == null) return null; -// stmt.addAll(stm); -// } -// debug.add("Ann Func Node: " + token.kind); -// accept(TokenKind.RCURLY); -// BlockNode block = new BlockNode(); // BlockNode(stmt); -// block.setStmt(stmt); TargetFuncWrapper targetFuncWrapper = (TargetFuncWrapper) expression; AnonymousFunction anonymousFunction = AnonymousFunction.func(targetFuncWrapper); if (error) return null; @@ -1860,16 +1570,6 @@ ExpressionStatement parseValue() { /* Parse Values DOne */ -// void parseVariableDeclaration() { -// /* -// * -// * parseIdentifier() parseEqualOperator() parseValue() -// * -// * return VariableNode -// */ -// -// } - List parseVariable() { /* * List Variables checks grammar for variable Declaration @@ -1885,7 +1585,6 @@ List parseVariable() { while (token.kind != TokenKind.SEMICOLON) { if (error) return null; -// Token tk = token; ExpressionStatement iden = parseIdentifier(); ExpressionStatement exp = null; Context ctx = Context.context; @@ -1902,9 +1601,6 @@ List parseVariable() { SymbolReference sym = ctx.createSymbolReference(iden.getName(), Node.Tag.VARDECL); DeclarativeAssignmentStatement stmt = DeclarativeAssignmentStatement.create(sym, exp); varDeclNodes.add(stmt); -// VarDeclNode varDecl = new VarDeclNode(idenNode, exp); -// varDecl.setFirstToken(tk); -// varDeclNodes.add(varDecl); } accept(TokenKind.SEMICOLON); if (error) return null; @@ -1927,7 +1623,6 @@ List parseConstVariable() { while (token.kind != TokenKind.SEMICOLON) { if (error) return null; -// Token tk = token; ExpressionStatement iden = parseIdentifier(); accept(TokenKind.EQU); ExpressionStatement exp = parseValue(); @@ -1935,14 +1630,11 @@ List parseConstVariable() { accept(TokenKind.COMMA); SymbolReference sym = ctx.createSymbolReference(iden.getName(), Tag.CONSTDECL); DeclarativeAssignmentStatement stmt = DeclarativeAssignmentStatement.create(sym, exp); -// ConstDeclNode constDecl = new ConstDeclNode(idenNo, exp); -// constDecl.setFirstToken(tk); constDeclNodes.add(stmt); } accept(TokenKind.SEMICOLON); if (error) return null; return constDeclNodes; - } List parseParameter() { @@ -1956,7 +1648,6 @@ List parseParameter() { * return List */ if (error) return null; -// boolean varArg = false; List idenNodes = new ArrayList<>(); while (token.kind != TokenKind.RPAREN) { @@ -1967,9 +1658,7 @@ List parseParameter() { if (token.kind == TokenKind.VARARGS) { accept(TokenKind.VARARGS); -// varArg = true; idenNodes.add(parseIdentifier().getName()); - // accept(TokenKind.RPAREN); break; } idenNodes.add(parseIdentifier().getName()); @@ -1981,11 +1670,6 @@ List parseParameter() { return idenNodes; } - // ExprNode parseFunctionCall(){ - // IdenNode funcName = parseIdentifier(); - - // } - DeclarativeAssignmentStatement parseFunctionDeclaration() { /* * List of Parameter BlockNode @@ -2026,53 +1710,18 @@ DeclarativeAssignmentStatement parseFunctionDeclaration() { context.leaveFunc(); SymbolReference ref = context.createSymbolReference(funcName.getName(), Node.Tag.VARDECL); DeclarativeAssignmentStatement statement = DeclarativeAssignmentStatement.create(ref, expression); -// BlockNode block = new BlockNode(); -// block.setStmt(stmt); if (error) return null; -// FuncDeclNode funcDecl = new FuncDeclNode((IdenNode) funcName, arg, block); -// funcDecl.setFirstToken(tk); return statement; } -// void parseDeclarativeStatement() { -// /* -// * List of Declarative Statement -// * -// * Checks Type of Statement and calls below methods to parse calls parseFunction -// * calls parseVariable -// */ -// } - -// void block() { -// /* -// * Handles Cases For Each Type of Block Statements like DECL, ASGN, IF etc. and -// * calls respective methods -// * -// */ -// } - -// void parseBlockStatement() { -// /* -// * List of Statement -// * -// * parse Grammar, checks type of Statement and calls block() -// */ -// -// /* -// * switch(token.kind){ case VAR } -// */ -// } - ReturnStatement parseReturnStatement() { if (error) return null; accept(TokenKind.RET); -// Token tk = token; ExpressionStatement exp = parseValue(); debug.add("Return : " + exp); accept(TokenKind.SEMICOLON); if (error) return null; - // rtrnNode.setFirstToken(tk); return ReturnStatement.of(exp); } @@ -2111,25 +1760,21 @@ List> parseStatement() { switch (token.kind) { case VAR: List stm = parseVariable(); - System.out.println("Var Statement: "+stm); if (stm == null) return null; block.addAll(stm); break; case CONST: List stm1 = parseConstVariable(); - System.out.println("const Statement: "+stm1); if (stm1 == null) return null; block.addAll(stm1); break; case FUNC: DeclarativeAssignmentStatement decl = parseFunctionDeclaration(); - System.out.println("For Statement: "+decl); if (decl == null) return null; block.add(decl); break; case IF: CafeStatement stm2 = parseIfStatement(); - System.out.println("If Statement: "+stm2); if (stm2 == null) return null; block.add(stm2); break; @@ -2137,14 +1782,11 @@ List> parseStatement() { boolean innerLoop = breakAllowed; breakAllowed = true; CafeStatement stm3 = parseForStatement(); - System.out.println("for Statement: "+stm3); if (stm3 == null) return null; block.add(stm3); breakAllowed = innerLoop; -// innerLoop = false; break; case LOOP: - System.out.println("Entered to Loop:"); // CafeStatement stm4 = parseLoopStatement(); // if (stm4 == null) return null; // block.add(stm4); @@ -2160,25 +1802,20 @@ List> parseStatement() { CafeStatement stm6 = parseExprStmt(); if (stm6 == null) return null; block.add(stm6); - System.out.println("Iden This Block Stmt: " + stm6+error); - System.out.println("Iden This BlockToken: " + token.value()); accept(TokenKind.SEMICOLON); break; case BREAK: case CONTINUE: CafeStatement stm7 = parseFlowStatement(); block.add(stm7); - System.out.println("Break Continue Block Stmt: " + stm7); debug.add("Break Continue Block Stmt: " + token.kind); break; default: logError(TokenKind.IDENTIFIER); } if(error) return null; - System.out.println("Block Statement: "+block); return block; } - // return Block Statement Node Block parseBlock() { /* * List of block Statements calls parseBlockStatement @@ -2187,21 +1824,11 @@ Block parseBlock() { Context context = Context.context; Block block = context.enterScope(); - -// for (Node.StmtNode stmt : n.block) { -// stmt.accept(this); -// CafeStatement statement = (CafeStatement) context.pop(); -// block.add(statement); -// } -// context.push(block); - List> blockStmt = new ArrayList<>(); debug.add("Token kind " + token.kind); - System.out.println("Block Entered"); while(token.kind != TokenKind.RCURLY) { if (error) return null; List> stm = parseStatement(); - System.out.println("Block Stmt: "+stm); if (stm == null) return null; blockStmt.addAll(stm); } @@ -2212,7 +1839,6 @@ Block parseBlock() { context.leaveScope(); if (error) return null; debug.add("Block Stmt: " + block); - System.out.println("Block: "+block); return block; } @@ -2221,15 +1847,6 @@ List parseExportStatement() { Context context = Context.context; context.isExport = true; -// String name = n.iden.name; -// CafeExport export = CafeExport.export(name); -// context.module.addExport(export); -// if (n.node == null) { -// // Just pushing to avoid error -// context.push(export); -// } else { -// n.node.accept(this); -// } List decl = new ArrayList<>(); accept(TokenKind.EXPORT); @@ -2292,14 +1909,6 @@ CafeImport parseImportStatement() { Map blocks = new HashMap<>(); ExpressionStatement id1, id2 = null; Context context = Context.context; -// for (Map.Entry entry : n.importAliasMap.entrySet()) { -// Node.IdenNode value = entry.getValue(); -// String alias = null; -// if (value != null) -// alias = value.name; -// cafeImport.add(entry.getKey().name, alias); -// } - accept(TokenKind.IMPORT); if(token.kind == TokenKind.IDENTIFIER){ @@ -2321,7 +1930,6 @@ CafeImport parseImportStatement() { } } else { if(accept(TokenKind.MUL)) { -// id1 = new IdenNode("*"); accept(TokenKind.AS); id2 = parseIdentifier(); blocks.put("*", id2.getName()); @@ -2395,13 +2003,6 @@ CafeModule parseStatements() { for( CafeStatement stm: tree){ Context.context.module.add(stm); } -// return new ProgramNode(tree); return Context.context.module; } - - // void parseStatementAsBlock() { - // /* - // * switch(token.kind){ case IF: case FOR: case } - // */ - // } }