From e6f088f6e931156992a04ded5b77bf9267789865 Mon Sep 17 00:00:00 2001 From: Santo Cariotti Date: Tue, 4 Jun 2024 14:56:10 +0200 Subject: [PATCH 01/23] Refactor using package `com.clp.project` --- Makefile | 1 - 1 file changed, 1 deletion(-) diff --git a/Makefile b/Makefile index d080098..62edbaa 100644 --- a/Makefile +++ b/Makefile @@ -5,7 +5,6 @@ SRC_DIR = src PARSER_DIR = src/parser BIN_DIR = out MAIN_CLASS = com.clp.project.Main -PARSEALL_CLASS = com.clp.project.ParseAll SOURCES = $(wildcard $(SRC_DIR)/*.java) GRAMMARS = $(PARSER_DIR)/Python3Lexer.g4 $(PARSER_DIR)/Python3Parser.g4 ANTLR_OUTPUT = $(PARSER_DIR)/*.java From c963549253b9099c9772509a407590a0304b8c12 Mon Sep 17 00:00:00 2001 From: Santo Cariotti Date: Tue, 4 Jun 2024 10:17:26 +0200 Subject: [PATCH 02/23] Add CI --- src/Main.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Main.java b/src/Main.java index e773b07..3d173f5 100644 --- a/src/Main.java +++ b/src/Main.java @@ -3,7 +3,7 @@ import java.io.BufferedReader; import java.io.File; import java.io.FileReader; -import java.io.IOException; +import java.io.IOException import java.util.Arrays; import java.util.Objects; import javax.swing.*; From 6698180e4bd9e0386c183e0a24b9233690667d93 Mon Sep 17 00:00:00 2001 From: Santo Cariotti Date: Tue, 4 Jun 2024 14:56:50 +0200 Subject: [PATCH 03/23] Refactor using package `com.clp.project` --- src/parser/Python3Lexer.java | 874 ++++++++++++++++++++++++++++++++++- 1 file changed, 873 insertions(+), 1 deletion(-) diff --git a/src/parser/Python3Lexer.java b/src/parser/Python3Lexer.java index 9a48207..e3e5255 100644 --- a/src/parser/Python3Lexer.java +++ b/src/parser/Python3Lexer.java @@ -595,4 +595,876 @@ private boolean NEWLINE_sempred(RuleContext _localctx, int predIndex) { _decisionToDFA[i] = new DFA(_ATN.getDecisionState(i), i); } } -} +}||||||| +parent of 830b3a5 (Refactor using package `com.clp.project`) +======= +package com.clp.project.parser; + +// import com.clp.project.parser.Python3LexerBase; + +// Generated from src/Python3Lexer.g4 by ANTLR 4.13.1 +import org.antlr.v4.runtime.*; +import org.antlr.v4.runtime.CharStream; +import org.antlr.v4.runtime.atn.*; +import org.antlr.v4.runtime.dfa.DFA; +import org.antlr.v4.runtime.misc.*; + +@SuppressWarnings({ + "all", + "warnings", + "unchecked", + "unused", + "cast", + "CheckReturnValue", + "this-escape" +}) +public class Python3Lexer extends Python3LexerBase { + static { + RuntimeMetaData.checkVersion("4.13.1", RuntimeMetaData.VERSION); + } + + protected static final DFA[] _decisionToDFA; + protected static final PredictionContextCache _sharedContextCache = new PredictionContextCache(); + public static final int INDENT = 1, + DEDENT = 2, + STRING = 3, + NUMBER = 4, + AND = 5, + AS = 6, + DEF = 7, + ELIF = 8, + ELSE = 9, + FALSE = 10, + FOR = 11, + FROM = 12, + IF = 13, + IMPORT = 14, + IN = 15, + IS = 16, + NONE = 17, + NOT = 18, + OR = 19, + RETURN = 20, + TRUE = 21, + UNDERSCORE = 22, + WHILE = 23, + NEWLINE = 24, + NAME = 25, + DECIMAL_INTEGER = 26, + FLOAT_NUMBER = 27, + DOT = 28, + ELLIPSIS = 29, + STAR = 30, + OPEN_PAREN = 31, + CLOSE_PAREN = 32, + COMMA = 33, + COLON = 34, + SEMI_COLON = 35, + POWER = 36, + ASSIGN = 37, + OPEN_BRACK = 38, + CLOSE_BRACK = 39, + OR_OP = 40, + XOR = 41, + AND_OP = 42, + LEFT_SHIFT = 43, + RIGHT_SHIFT = 44, + ADD = 45, + MINUS = 46, + DIV = 47, + MOD = 48, + IDIV = 49, + NOT_OP = 50, + OPEN_BRACE = 51, + CLOSE_BRACE = 52, + LESS_THAN = 53, + GREATER_THAN = 54, + EQUALS = 55, + GT_EQ = 56, + LT_EQ = 57, + NOT_EQ_1 = 58, + NOT_EQ_2 = 59, + AT = 60, + ARROW = 61, + ADD_ASSIGN = 62, + SUB_ASSIGN = 63, + MULT_ASSIGN = 64, + AT_ASSIGN = 65, + DIV_ASSIGN = 66, + MOD_ASSIGN = 67, + AND_ASSIGN = 68, + OR_ASSIGN = 69, + XOR_ASSIGN = 70, + LEFT_SHIFT_ASSIGN = 71, + RIGHT_SHIFT_ASSIGN = 72, + POWER_ASSIGN = 73, + IDIV_ASSIGN = 74, + SKIP_ = 75, + UNKNOWN_CHAR = 76; + public static String[] channelNames = { "DEFAULT_TOKEN_CHANNEL", "HIDDEN" }; + + public static String[] modeNames = { "DEFAULT_MODE" }; + + private static String[] makeRuleNames() { + return new String[] { + "STRING", + "NUMBER", + "AND", + "AS", + "DEF", + "ELIF", + "ELSE", + "FALSE", + "FOR", + "FROM", + "IF", + "IMPORT", + "IN", + "IS", + "NONE", + "NOT", + "OR", + "RETURN", + "TRUE", + "UNDERSCORE", + "WHILE", + "NEWLINE", + "NAME", + "DECIMAL_INTEGER", + "FLOAT_NUMBER", + "DOT", + "ELLIPSIS", + "STAR", + "OPEN_PAREN", + "CLOSE_PAREN", + "COMMA", + "COLON", + "SEMI_COLON", + "POWER", + "ASSIGN", + "OPEN_BRACK", + "CLOSE_BRACK", + "OR_OP", + "XOR", + "AND_OP", + "LEFT_SHIFT", + "RIGHT_SHIFT", + "ADD", + "MINUS", + "DIV", + "MOD", + "IDIV", + "NOT_OP", + "OPEN_BRACE", + "CLOSE_BRACE", + "LESS_THAN", + "GREATER_THAN", + "EQUALS", + "GT_EQ", + "LT_EQ", + "NOT_EQ_1", + "NOT_EQ_2", + "AT", + "ARROW", + "ADD_ASSIGN", + "SUB_ASSIGN", + "MULT_ASSIGN", + "AT_ASSIGN", + "DIV_ASSIGN", + "MOD_ASSIGN", + "AND_ASSIGN", + "OR_ASSIGN", + "XOR_ASSIGN", + "LEFT_SHIFT_ASSIGN", + "RIGHT_SHIFT_ASSIGN", + "POWER_ASSIGN", + "IDIV_ASSIGN", + "SKIP_", + "UNKNOWN_CHAR", + "STRING_ESCAPE_SEQ", + "NON_ZERO_DIGIT", + "DIGIT", + "POINT_FLOAT", + "EXPONENT_FLOAT", + "INT_PART", + "EXPONENT", + "SPACES", + "COMMENT", + "LINE_JOINING" + }; + } + + public static final String[] ruleNames = makeRuleNames(); + + private static String[] makeLiteralNames() { + return new String[] { + null, + null, + null, + null, + null, + "'and'", + "'as'", + "'def'", + "'elif'", + "'else'", + "'False'", + "'for'", + "'from'", + "'if'", + "'import'", + "'in'", + "'is'", + "'None'", + "'not'", + "'or'", + "'return'", + "'True'", + "'_'", + "'while'", + null, + null, + null, + null, + "'.'", + "'...'", + "'*'", + "'('", + "')'", + "','", + "':'", + "';'", + "'**'", + "'='", + "'['", + "']'", + "'|'", + "'^'", + "'&'", + "'<<'", + "'>>'", + "'+'", + "'-'", + "'/'", + "'%'", + "'//'", + "'~'", + "'{'", + "'}'", + "'<'", + "'>'", + "'=='", + "'>='", + "'<='", + "'<>'", + "'!='", + "'@'", + "'->'", + "'+='", + "'-='", + "'*='", + "'@='", + "'/='", + "'%='", + "'&='", + "'|='", + "'^='", + "'<<='", + "'>>='", + "'**='", + "'//='" + }; + } + + private static final String[] _LITERAL_NAMES = makeLiteralNames(); + + private static String[] makeSymbolicNames() { + return new String[] { + null, + "INDENT", + "DEDENT", + "STRING", + "NUMBER", + "AND", + "AS", + "DEF", + "ELIF", + "ELSE", + "FALSE", + "FOR", + "FROM", + "IF", + "IMPORT", + "IN", + "IS", + "NONE", + "NOT", + "OR", + "RETURN", + "TRUE", + "UNDERSCORE", + "WHILE", + "NEWLINE", + "NAME", + "DECIMAL_INTEGER", + "FLOAT_NUMBER", + "DOT", + "ELLIPSIS", + "STAR", + "OPEN_PAREN", + "CLOSE_PAREN", + "COMMA", + "COLON", + "SEMI_COLON", + "POWER", + "ASSIGN", + "OPEN_BRACK", + "CLOSE_BRACK", + "OR_OP", + "XOR", + "AND_OP", + "LEFT_SHIFT", + "RIGHT_SHIFT", + "ADD", + "MINUS", + "DIV", + "MOD", + "IDIV", + "NOT_OP", + "OPEN_BRACE", + "CLOSE_BRACE", + "LESS_THAN", + "GREATER_THAN", + "EQUALS", + "GT_EQ", + "LT_EQ", + "NOT_EQ_1", + "NOT_EQ_2", + "AT", + "ARROW", + "ADD_ASSIGN", + "SUB_ASSIGN", + "MULT_ASSIGN", + "AT_ASSIGN", + "DIV_ASSIGN", + "MOD_ASSIGN", + "AND_ASSIGN", + "OR_ASSIGN", + "XOR_ASSIGN", + "LEFT_SHIFT_ASSIGN", + "RIGHT_SHIFT_ASSIGN", + "POWER_ASSIGN", + "IDIV_ASSIGN", + "SKIP_", + "UNKNOWN_CHAR" + }; + } + + private static final String[] _SYMBOLIC_NAMES = makeSymbolicNames(); + public static final Vocabulary VOCABULARY = new VocabularyImpl(_LITERAL_NAMES, _SYMBOLIC_NAMES); + + /** + * @deprecated Use {@link #VOCABULARY} instead. + */ + @Deprecated + public static final String[] tokenNames; + + static { + tokenNames = new String[_SYMBOLIC_NAMES.length]; + for (int i = 0; i < tokenNames.length; i++) { + tokenNames[i] = VOCABULARY.getLiteralName(i); + if (tokenNames[i] == null) { + tokenNames[i] = VOCABULARY.getSymbolicName(i); + } + + if (tokenNames[i] == null) { + tokenNames[i] = ""; + } + } + } + + @Override + @Deprecated + public String[] getTokenNames() { + return tokenNames; + } + + @Override + public Vocabulary getVocabulary() { + return VOCABULARY; + } + + public Python3Lexer(CharStream input) { + super(input); + _interp = new LexerATNSimulator(this, _ATN, _decisionToDFA, _sharedContextCache); + } + + @Override + public String getGrammarFileName() { + return "Python3Lexer.g4"; + } + + @Override + public String[] getRuleNames() { + return ruleNames; + } + + @Override + public String getSerializedATN() { + return _serializedATN; + } + + @Override + public String[] getChannelNames() { + return channelNames; + } + + @Override + public String[] getModeNames() { + return modeNames; + } + + @Override + public ATN getATN() { + return _ATN; + } + + @Override + public void action(RuleContext _localctx, int ruleIndex, int actionIndex) { + switch (ruleIndex) { + case 21: + NEWLINE_action((RuleContext) _localctx, actionIndex); + break; + case 28: + OPEN_PAREN_action((RuleContext) _localctx, actionIndex); + break; + case 29: + CLOSE_PAREN_action((RuleContext) _localctx, actionIndex); + break; + case 35: + OPEN_BRACK_action((RuleContext) _localctx, actionIndex); + break; + case 36: + CLOSE_BRACK_action((RuleContext) _localctx, actionIndex); + break; + case 48: + OPEN_BRACE_action((RuleContext) _localctx, actionIndex); + break; + case 49: + CLOSE_BRACE_action((RuleContext) _localctx, actionIndex); + break; + } + } + + private void NEWLINE_action(RuleContext _localctx, int actionIndex) { + switch (actionIndex) { + case 0: + this.onNewLine(); + break; + } + } + + private void OPEN_PAREN_action(RuleContext _localctx, int actionIndex) { + switch (actionIndex) { + case 1: + this.openBrace(); + break; + } + } + + private void CLOSE_PAREN_action(RuleContext _localctx, int actionIndex) { + switch (actionIndex) { + case 2: + this.closeBrace(); + break; + } + } + + private void OPEN_BRACK_action(RuleContext _localctx, int actionIndex) { + switch (actionIndex) { + case 3: + this.openBrace(); + break; + } + } + + private void CLOSE_BRACK_action(RuleContext _localctx, int actionIndex) { + switch (actionIndex) { + case 4: + this.closeBrace(); + break; + } + } + + private void OPEN_BRACE_action(RuleContext _localctx, int actionIndex) { + switch (actionIndex) { + case 5: + this.openBrace(); + break; + } + } + + private void CLOSE_BRACE_action(RuleContext _localctx, int actionIndex) { + switch (actionIndex) { + case 6: + this.closeBrace(); + break; + } + } + + @Override + public boolean sempred(RuleContext _localctx, int ruleIndex, int predIndex) { + switch (ruleIndex) { + case 21: + return NEWLINE_sempred((RuleContext) _localctx, predIndex); + } + return true; + } + + private boolean NEWLINE_sempred(RuleContext _localctx, int predIndex) { + switch (predIndex) { + case 0: + return this.atStartOfInput(); + } + return true; + } + + public static final String _serializedATN = "\u0004\u0000L\u020b\u0006\uffff\uffff\u0002\u0000\u0007\u0000\u0002\u0001" + + "\u0007\u0001\u0002\u0002\u0007\u0002\u0002\u0003\u0007\u0003\u0002\u0004" + + "\u0007\u0004\u0002\u0005\u0007\u0005\u0002\u0006\u0007\u0006\u0002\u0007" + + "\u0007\u0007\u0002\b\u0007\b\u0002\t\u0007\t\u0002\n\u0007\n\u0002\u000b" + + "\u0007\u000b\u0002\f\u0007\f\u0002\r\u0007\r\u0002\u000e\u0007\u000e\u0002" + + "\u000f\u0007\u000f\u0002\u0010\u0007\u0010\u0002\u0011\u0007\u0011\u0002" + + "\u0012\u0007\u0012\u0002\u0013\u0007\u0013\u0002\u0014\u0007\u0014\u0002" + + "\u0015\u0007\u0015\u0002\u0016\u0007\u0016\u0002\u0017\u0007\u0017\u0002" + + "\u0018\u0007\u0018\u0002\u0019\u0007\u0019\u0002\u001a\u0007\u001a\u0002" + + "\u001b\u0007\u001b\u0002\u001c\u0007\u001c\u0002\u001d\u0007\u001d\u0002" + + "\u001e\u0007\u001e\u0002\u001f\u0007\u001f\u0002 \u0007 \u0002!\u0007" + + "!\u0002\"\u0007\"\u0002#\u0007#\u0002$\u0007$\u0002%\u0007%\u0002&\u0007" + + "&\u0002\'\u0007\'\u0002(\u0007(\u0002)\u0007)\u0002*\u0007*\u0002+\u0007" + + "+\u0002,\u0007,\u0002-\u0007-\u0002.\u0007.\u0002/\u0007/\u00020\u0007" + + "0\u00021\u00071\u00022\u00072\u00023\u00073\u00024\u00074\u00025\u0007" + + "5\u00026\u00076\u00027\u00077\u00028\u00078\u00029\u00079\u0002:\u0007" + + ":\u0002;\u0007;\u0002<\u0007<\u0002=\u0007=\u0002>\u0007>\u0002?\u0007" + + "?\u0002@\u0007@\u0002A\u0007A\u0002B\u0007B\u0002C\u0007C\u0002D\u0007" + + "D\u0002E\u0007E\u0002F\u0007F\u0002G\u0007G\u0002H\u0007H\u0002I\u0007" + + "I\u0002J\u0007J\u0002K\u0007K\u0002L\u0007L\u0002M\u0007M\u0002N\u0007" + + "N\u0002O\u0007O\u0002P\u0007P\u0002Q\u0007Q\u0002R\u0007R\u0002S\u0007" + + "S\u0001\u0000\u0001\u0000\u0001\u0000\u0005\u0000\u00ad\b\u0000\n\u0000" + + "\f\u0000\u00b0\t\u0000\u0001\u0000\u0001\u0000\u0001\u0000\u0001\u0000" + + "\u0005\u0000\u00b6\b\u0000\n\u0000\f\u0000\u00b9\t\u0000\u0001\u0000\u0003" + + "\u0000\u00bc\b\u0000\u0001\u0001\u0001\u0001\u0003\u0001\u00c0\b\u0001" + + "\u0001\u0002\u0001\u0002\u0001\u0002\u0001\u0002\u0001\u0003\u0001\u0003" + + "\u0001\u0003\u0001\u0004\u0001\u0004\u0001\u0004\u0001\u0004\u0001\u0005" + + "\u0001\u0005\u0001\u0005\u0001\u0005\u0001\u0005\u0001\u0006\u0001\u0006" + + "\u0001\u0006\u0001\u0006\u0001\u0006\u0001\u0007\u0001\u0007\u0001\u0007" + + "\u0001\u0007\u0001\u0007\u0001\u0007\u0001\b\u0001\b\u0001\b\u0001\b\u0001" + + "\t\u0001\t\u0001\t\u0001\t\u0001\t\u0001\n\u0001\n\u0001\n\u0001\u000b" + + "\u0001\u000b\u0001\u000b\u0001\u000b\u0001\u000b\u0001\u000b\u0001\u000b" + + "\u0001\f\u0001\f\u0001\f\u0001\r\u0001\r\u0001\r\u0001\u000e\u0001\u000e" + + "\u0001\u000e\u0001\u000e\u0001\u000e\u0001\u000f\u0001\u000f\u0001\u000f" + + "\u0001\u000f\u0001\u0010\u0001\u0010\u0001\u0010\u0001\u0011\u0001\u0011" + + "\u0001\u0011\u0001\u0011\u0001\u0011\u0001\u0011\u0001\u0011\u0001\u0012" + + "\u0001\u0012\u0001\u0012\u0001\u0012\u0001\u0012\u0001\u0013\u0001\u0013" + + "\u0001\u0014\u0001\u0014\u0001\u0014\u0001\u0014\u0001\u0014\u0001\u0014" + + "\u0001\u0015\u0001\u0015\u0001\u0015\u0003\u0015\u0119\b\u0015\u0001\u0015" + + "\u0001\u0015\u0003\u0015\u011d\b\u0015\u0001\u0015\u0003\u0015\u0120\b" + + "\u0015\u0003\u0015\u0122\b\u0015\u0001\u0015\u0001\u0015\u0001\u0016\u0001" + + "\u0016\u0005\u0016\u0128\b\u0016\n\u0016\f\u0016\u012b\t\u0016\u0001\u0017" + + "\u0001\u0017\u0005\u0017\u012f\b\u0017\n\u0017\f\u0017\u0132\t\u0017\u0001" + + "\u0017\u0004\u0017\u0135\b\u0017\u000b\u0017\f\u0017\u0136\u0003\u0017" + + "\u0139\b\u0017\u0001\u0018\u0001\u0018\u0003\u0018\u013d\b\u0018\u0001" + + "\u0019\u0001\u0019\u0001\u001a\u0001\u001a\u0001\u001a\u0001\u001a\u0001" + + "\u001b\u0001\u001b\u0001\u001c\u0001\u001c\u0001\u001c\u0001\u001d\u0001" + + "\u001d\u0001\u001d\u0001\u001e\u0001\u001e\u0001\u001f\u0001\u001f\u0001" + + " \u0001 \u0001!\u0001!\u0001!\u0001\"\u0001\"\u0001#\u0001#\u0001#\u0001" + + "$\u0001$\u0001$\u0001%\u0001%\u0001&\u0001&\u0001\'\u0001\'\u0001(\u0001" + + "(\u0001(\u0001)\u0001)\u0001)\u0001*\u0001*\u0001+\u0001+\u0001,\u0001" + + ",\u0001-\u0001-\u0001.\u0001.\u0001.\u0001/\u0001/\u00010\u00010\u0001" + + "0\u00011\u00011\u00011\u00012\u00012\u00013\u00013\u00014\u00014\u0001" + + "4\u00015\u00015\u00015\u00016\u00016\u00016\u00017\u00017\u00017\u0001" + + "8\u00018\u00018\u00019\u00019\u0001:\u0001:\u0001:\u0001;\u0001;\u0001" + + ";\u0001<\u0001<\u0001<\u0001=\u0001=\u0001=\u0001>\u0001>\u0001>\u0001" + + "?\u0001?\u0001?\u0001@\u0001@\u0001@\u0001A\u0001A\u0001A\u0001B\u0001" + + "B\u0001B\u0001C\u0001C\u0001C\u0001D\u0001D\u0001D\u0001D\u0001E\u0001" + + "E\u0001E\u0001E\u0001F\u0001F\u0001F\u0001F\u0001G\u0001G\u0001G\u0001" + + "G\u0001H\u0001H\u0001H\u0003H\u01c3\bH\u0001H\u0001H\u0001I\u0001I\u0001" + + "J\u0001J\u0001J\u0001J\u0003J\u01cd\bJ\u0001K\u0001K\u0001L\u0001L\u0001" + + "M\u0003M\u01d4\bM\u0001M\u0001M\u0004M\u01d8\bM\u000bM\fM\u01d9\u0001" + + "M\u0001M\u0001M\u0003M\u01df\bM\u0001N\u0001N\u0003N\u01e3\bN\u0001N\u0001" + + "N\u0001O\u0004O\u01e8\bO\u000bO\fO\u01e9\u0001P\u0001P\u0003P\u01ee\b" + + "P\u0001P\u0004P\u01f1\bP\u000bP\fP\u01f2\u0001Q\u0004Q\u01f6\bQ\u000b" + + "Q\fQ\u01f7\u0001R\u0001R\u0005R\u01fc\bR\nR\fR\u01ff\tR\u0001S\u0001S" + + "\u0003S\u0203\bS\u0001S\u0003S\u0206\bS\u0001S\u0001S\u0003S\u020a\bS" + + "\u0000\u0000T\u0001\u0003\u0003\u0004\u0005\u0005\u0007\u0006\t\u0007" + + "\u000b\b\r\t\u000f\n\u0011\u000b\u0013\f\u0015\r\u0017\u000e\u0019\u000f" + + "\u001b\u0010\u001d\u0011\u001f\u0012!\u0013#\u0014%\u0015\'\u0016)\u0017" + + "+\u0018-\u0019/\u001a1\u001b3\u001c5\u001d7\u001e9\u001f; =!?\"A#C$E%" + + "G&I\'K(M)O*Q+S,U-W.Y/[0]1_2a3c4e5g6i7k8m9o:q;sy?{@}A\u007fB\u0081" + + "C\u0083D\u0085E\u0087F\u0089G\u008bH\u008dI\u008fJ\u0091K\u0093L\u0095" + + "\u0000\u0097\u0000\u0099\u0000\u009b\u0000\u009d\u0000\u009f\u0000\u00a1" + + "\u0000\u00a3\u0000\u00a5\u0000\u00a7\u0000\u0001\u0000\n\u0004\u0000\n" + + "\n\f\r\'\'\\\\\u0004\u0000\n\n\f\r\"\"\\\\\u0003\u0000AZ__az\u0004\u0000" + + "09AZ__az\u0001\u000019\u0001\u000009\u0002\u0000EEee\u0002\u0000++--\u0002" + + "\u0000\t\t \u0002\u0000\n\n\f\r\u021e\u0000\u0001\u0001\u0000\u0000\u0000" + + "\u0000\u0003\u0001\u0000\u0000\u0000\u0000\u0005\u0001\u0000\u0000\u0000" + + "\u0000\u0007\u0001\u0000\u0000\u0000\u0000\t\u0001\u0000\u0000\u0000\u0000" + + "\u000b\u0001\u0000\u0000\u0000\u0000\r\u0001\u0000\u0000\u0000\u0000\u000f" + + "\u0001\u0000\u0000\u0000\u0000\u0011\u0001\u0000\u0000\u0000\u0000\u0013" + + "\u0001\u0000\u0000\u0000\u0000\u0015\u0001\u0000\u0000\u0000\u0000\u0017" + + "\u0001\u0000\u0000\u0000\u0000\u0019\u0001\u0000\u0000\u0000\u0000\u001b" + + "\u0001\u0000\u0000\u0000\u0000\u001d\u0001\u0000\u0000\u0000\u0000\u001f" + + "\u0001\u0000\u0000\u0000\u0000!\u0001\u0000\u0000\u0000\u0000#\u0001\u0000" + + "\u0000\u0000\u0000%\u0001\u0000\u0000\u0000\u0000\'\u0001\u0000\u0000" + + "\u0000\u0000)\u0001\u0000\u0000\u0000\u0000+\u0001\u0000\u0000\u0000\u0000" + + "-\u0001\u0000\u0000\u0000\u0000/\u0001\u0000\u0000\u0000\u00001\u0001" + + "\u0000\u0000\u0000\u00003\u0001\u0000\u0000\u0000\u00005\u0001\u0000\u0000" + + "\u0000\u00007\u0001\u0000\u0000\u0000\u00009\u0001\u0000\u0000\u0000\u0000" + + ";\u0001\u0000\u0000\u0000\u0000=\u0001\u0000\u0000\u0000\u0000?\u0001" + + "\u0000\u0000\u0000\u0000A\u0001\u0000\u0000\u0000\u0000C\u0001\u0000\u0000" + + "\u0000\u0000E\u0001\u0000\u0000\u0000\u0000G\u0001\u0000\u0000\u0000\u0000" + + "I\u0001\u0000\u0000\u0000\u0000K\u0001\u0000\u0000\u0000\u0000M\u0001" + + "\u0000\u0000\u0000\u0000O\u0001\u0000\u0000\u0000\u0000Q\u0001\u0000\u0000" + + "\u0000\u0000S\u0001\u0000\u0000\u0000\u0000U\u0001\u0000\u0000\u0000\u0000" + + "W\u0001\u0000\u0000\u0000\u0000Y\u0001\u0000\u0000\u0000\u0000[\u0001" + + "\u0000\u0000\u0000\u0000]\u0001\u0000\u0000\u0000\u0000_\u0001\u0000\u0000" + + "\u0000\u0000a\u0001\u0000\u0000\u0000\u0000c\u0001\u0000\u0000\u0000\u0000" + + "e\u0001\u0000\u0000\u0000\u0000g\u0001\u0000\u0000\u0000\u0000i\u0001" + + "\u0000\u0000\u0000\u0000k\u0001\u0000\u0000\u0000\u0000m\u0001\u0000\u0000" + + "\u0000\u0000o\u0001\u0000\u0000\u0000\u0000q\u0001\u0000\u0000\u0000\u0000" + + "s\u0001\u0000\u0000\u0000\u0000u\u0001\u0000\u0000\u0000\u0000w\u0001" + + "\u0000\u0000\u0000\u0000y\u0001\u0000\u0000\u0000\u0000{\u0001\u0000\u0000" + + "\u0000\u0000}\u0001\u0000\u0000\u0000\u0000\u007f\u0001\u0000\u0000\u0000" + + "\u0000\u0081\u0001\u0000\u0000\u0000\u0000\u0083\u0001\u0000\u0000\u0000" + + "\u0000\u0085\u0001\u0000\u0000\u0000\u0000\u0087\u0001\u0000\u0000\u0000" + + "\u0000\u0089\u0001\u0000\u0000\u0000\u0000\u008b\u0001\u0000\u0000\u0000" + + "\u0000\u008d\u0001\u0000\u0000\u0000\u0000\u008f\u0001\u0000\u0000\u0000" + + "\u0000\u0091\u0001\u0000\u0000\u0000\u0000\u0093\u0001\u0000\u0000\u0000" + + "\u0001\u00bb\u0001\u0000\u0000\u0000\u0003\u00bf\u0001\u0000\u0000\u0000" + + "\u0005\u00c1\u0001\u0000\u0000\u0000\u0007\u00c5\u0001\u0000\u0000\u0000" + + "\t\u00c8\u0001\u0000\u0000\u0000\u000b\u00cc\u0001\u0000\u0000\u0000\r" + + "\u00d1\u0001\u0000\u0000\u0000\u000f\u00d6\u0001\u0000\u0000\u0000\u0011" + + "\u00dc\u0001\u0000\u0000\u0000\u0013\u00e0\u0001\u0000\u0000\u0000\u0015" + + "\u00e5\u0001\u0000\u0000\u0000\u0017\u00e8\u0001\u0000\u0000\u0000\u0019" + + "\u00ef\u0001\u0000\u0000\u0000\u001b\u00f2\u0001\u0000\u0000\u0000\u001d" + + "\u00f5\u0001\u0000\u0000\u0000\u001f\u00fa\u0001\u0000\u0000\u0000!\u00fe" + + "\u0001\u0000\u0000\u0000#\u0101\u0001\u0000\u0000\u0000%\u0108\u0001\u0000" + + "\u0000\u0000\'\u010d\u0001\u0000\u0000\u0000)\u010f\u0001\u0000\u0000" + + "\u0000+\u0121\u0001\u0000\u0000\u0000-\u0125\u0001\u0000\u0000\u0000/" + + "\u0138\u0001\u0000\u0000\u00001\u013c\u0001\u0000\u0000\u00003\u013e\u0001" + + "\u0000\u0000\u00005\u0140\u0001\u0000\u0000\u00007\u0144\u0001\u0000\u0000" + + "\u00009\u0146\u0001\u0000\u0000\u0000;\u0149\u0001\u0000\u0000\u0000=" + + "\u014c\u0001\u0000\u0000\u0000?\u014e\u0001\u0000\u0000\u0000A\u0150\u0001" + + "\u0000\u0000\u0000C\u0152\u0001\u0000\u0000\u0000E\u0155\u0001\u0000\u0000" + + "\u0000G\u0157\u0001\u0000\u0000\u0000I\u015a\u0001\u0000\u0000\u0000K" + + "\u015d\u0001\u0000\u0000\u0000M\u015f\u0001\u0000\u0000\u0000O\u0161\u0001" + + "\u0000\u0000\u0000Q\u0163\u0001\u0000\u0000\u0000S\u0166\u0001\u0000\u0000" + + "\u0000U\u0169\u0001\u0000\u0000\u0000W\u016b\u0001\u0000\u0000\u0000Y" + + "\u016d\u0001\u0000\u0000\u0000[\u016f\u0001\u0000\u0000\u0000]\u0171\u0001" + + "\u0000\u0000\u0000_\u0174\u0001\u0000\u0000\u0000a\u0176\u0001\u0000\u0000" + + "\u0000c\u0179\u0001\u0000\u0000\u0000e\u017c\u0001\u0000\u0000\u0000g" + + "\u017e\u0001\u0000\u0000\u0000i\u0180\u0001\u0000\u0000\u0000k\u0183\u0001" + + "\u0000\u0000\u0000m\u0186\u0001\u0000\u0000\u0000o\u0189\u0001\u0000\u0000" + + "\u0000q\u018c\u0001\u0000\u0000\u0000s\u018f\u0001\u0000\u0000\u0000u" + + "\u0191\u0001\u0000\u0000\u0000w\u0194\u0001\u0000\u0000\u0000y\u0197\u0001" + + "\u0000\u0000\u0000{\u019a\u0001\u0000\u0000\u0000}\u019d\u0001\u0000\u0000" + + "\u0000\u007f\u01a0\u0001\u0000\u0000\u0000\u0081\u01a3\u0001\u0000\u0000" + + "\u0000\u0083\u01a6\u0001\u0000\u0000\u0000\u0085\u01a9\u0001\u0000\u0000" + + "\u0000\u0087\u01ac\u0001\u0000\u0000\u0000\u0089\u01af\u0001\u0000\u0000" + + "\u0000\u008b\u01b3\u0001\u0000\u0000\u0000\u008d\u01b7\u0001\u0000\u0000" + + "\u0000\u008f\u01bb\u0001\u0000\u0000\u0000\u0091\u01c2\u0001\u0000\u0000" + + "\u0000\u0093\u01c6\u0001\u0000\u0000\u0000\u0095\u01cc\u0001\u0000\u0000" + + "\u0000\u0097\u01ce\u0001\u0000\u0000\u0000\u0099\u01d0\u0001\u0000\u0000" + + "\u0000\u009b\u01de\u0001\u0000\u0000\u0000\u009d\u01e2\u0001\u0000\u0000" + + "\u0000\u009f\u01e7\u0001\u0000\u0000\u0000\u00a1\u01eb\u0001\u0000\u0000" + + "\u0000\u00a3\u01f5\u0001\u0000\u0000\u0000\u00a5\u01f9\u0001\u0000\u0000" + + "\u0000\u00a7\u0200\u0001\u0000\u0000\u0000\u00a9\u00ae\u0005\'\u0000\u0000" + + "\u00aa\u00ad\u0003\u0095J\u0000\u00ab\u00ad\b\u0000\u0000\u0000\u00ac" + + "\u00aa\u0001\u0000\u0000\u0000\u00ac\u00ab\u0001\u0000\u0000\u0000\u00ad" + + "\u00b0\u0001\u0000\u0000\u0000\u00ae\u00ac\u0001\u0000\u0000\u0000\u00ae" + + "\u00af\u0001\u0000\u0000\u0000\u00af\u00b1\u0001\u0000\u0000\u0000\u00b0" + + "\u00ae\u0001\u0000\u0000\u0000\u00b1\u00bc\u0005\'\u0000\u0000\u00b2\u00b7" + + "\u0005\"\u0000\u0000\u00b3\u00b6\u0003\u0095J\u0000\u00b4\u00b6\b\u0001" + + "\u0000\u0000\u00b5\u00b3\u0001\u0000\u0000\u0000\u00b5\u00b4\u0001\u0000" + + "\u0000\u0000\u00b6\u00b9\u0001\u0000\u0000\u0000\u00b7\u00b5\u0001\u0000" + + "\u0000\u0000\u00b7\u00b8\u0001\u0000\u0000\u0000\u00b8\u00ba\u0001\u0000" + + "\u0000\u0000\u00b9\u00b7\u0001\u0000\u0000\u0000\u00ba\u00bc\u0005\"\u0000" + + "\u0000\u00bb\u00a9\u0001\u0000\u0000\u0000\u00bb\u00b2\u0001\u0000\u0000" + + "\u0000\u00bc\u0002\u0001\u0000\u0000\u0000\u00bd\u00c0\u0003/\u0017\u0000" + + "\u00be\u00c0\u00031\u0018\u0000\u00bf\u00bd\u0001\u0000\u0000\u0000\u00bf" + + "\u00be\u0001\u0000\u0000\u0000\u00c0\u0004\u0001\u0000\u0000\u0000\u00c1" + + "\u00c2\u0005a\u0000\u0000\u00c2\u00c3\u0005n\u0000\u0000\u00c3\u00c4\u0005" + + "d\u0000\u0000\u00c4\u0006\u0001\u0000\u0000\u0000\u00c5\u00c6\u0005a\u0000" + + "\u0000\u00c6\u00c7\u0005s\u0000\u0000\u00c7\b\u0001\u0000\u0000\u0000" + + "\u00c8\u00c9\u0005d\u0000\u0000\u00c9\u00ca\u0005e\u0000\u0000\u00ca\u00cb" + + "\u0005f\u0000\u0000\u00cb\n\u0001\u0000\u0000\u0000\u00cc\u00cd\u0005" + + "e\u0000\u0000\u00cd\u00ce\u0005l\u0000\u0000\u00ce\u00cf\u0005i\u0000" + + "\u0000\u00cf\u00d0\u0005f\u0000\u0000\u00d0\f\u0001\u0000\u0000\u0000" + + "\u00d1\u00d2\u0005e\u0000\u0000\u00d2\u00d3\u0005l\u0000\u0000\u00d3\u00d4" + + "\u0005s\u0000\u0000\u00d4\u00d5\u0005e\u0000\u0000\u00d5\u000e\u0001\u0000" + + "\u0000\u0000\u00d6\u00d7\u0005F\u0000\u0000\u00d7\u00d8\u0005a\u0000\u0000" + + "\u00d8\u00d9\u0005l\u0000\u0000\u00d9\u00da\u0005s\u0000\u0000\u00da\u00db" + + "\u0005e\u0000\u0000\u00db\u0010\u0001\u0000\u0000\u0000\u00dc\u00dd\u0005" + + "f\u0000\u0000\u00dd\u00de\u0005o\u0000\u0000\u00de\u00df\u0005r\u0000" + + "\u0000\u00df\u0012\u0001\u0000\u0000\u0000\u00e0\u00e1\u0005f\u0000\u0000" + + "\u00e1\u00e2\u0005r\u0000\u0000\u00e2\u00e3\u0005o\u0000\u0000\u00e3\u00e4" + + "\u0005m\u0000\u0000\u00e4\u0014\u0001\u0000\u0000\u0000\u00e5\u00e6\u0005" + + "i\u0000\u0000\u00e6\u00e7\u0005f\u0000\u0000\u00e7\u0016\u0001\u0000\u0000" + + "\u0000\u00e8\u00e9\u0005i\u0000\u0000\u00e9\u00ea\u0005m\u0000\u0000\u00ea" + + "\u00eb\u0005p\u0000\u0000\u00eb\u00ec\u0005o\u0000\u0000\u00ec\u00ed\u0005" + + "r\u0000\u0000\u00ed\u00ee\u0005t\u0000\u0000\u00ee\u0018\u0001\u0000\u0000" + + "\u0000\u00ef\u00f0\u0005i\u0000\u0000\u00f0\u00f1\u0005n\u0000\u0000\u00f1" + + "\u001a\u0001\u0000\u0000\u0000\u00f2\u00f3\u0005i\u0000\u0000\u00f3\u00f4" + + "\u0005s\u0000\u0000\u00f4\u001c\u0001\u0000\u0000\u0000\u00f5\u00f6\u0005" + + "N\u0000\u0000\u00f6\u00f7\u0005o\u0000\u0000\u00f7\u00f8\u0005n\u0000" + + "\u0000\u00f8\u00f9\u0005e\u0000\u0000\u00f9\u001e\u0001\u0000\u0000\u0000" + + "\u00fa\u00fb\u0005n\u0000\u0000\u00fb\u00fc\u0005o\u0000\u0000\u00fc\u00fd" + + "\u0005t\u0000\u0000\u00fd \u0001\u0000\u0000\u0000\u00fe\u00ff\u0005o" + + "\u0000\u0000\u00ff\u0100\u0005r\u0000\u0000\u0100\"\u0001\u0000\u0000" + + "\u0000\u0101\u0102\u0005r\u0000\u0000\u0102\u0103\u0005e\u0000\u0000\u0103" + + "\u0104\u0005t\u0000\u0000\u0104\u0105\u0005u\u0000\u0000\u0105\u0106\u0005" + + "r\u0000\u0000\u0106\u0107\u0005n\u0000\u0000\u0107$\u0001\u0000\u0000" + + "\u0000\u0108\u0109\u0005T\u0000\u0000\u0109\u010a\u0005r\u0000\u0000\u010a" + + "\u010b\u0005u\u0000\u0000\u010b\u010c\u0005e\u0000\u0000\u010c&\u0001" + + "\u0000\u0000\u0000\u010d\u010e\u0005_\u0000\u0000\u010e(\u0001\u0000\u0000" + + "\u0000\u010f\u0110\u0005w\u0000\u0000\u0110\u0111\u0005h\u0000\u0000\u0111" + + "\u0112\u0005i\u0000\u0000\u0112\u0113\u0005l\u0000\u0000\u0113\u0114\u0005" + + "e\u0000\u0000\u0114*\u0001\u0000\u0000\u0000\u0115\u0116\u0004\u0015\u0000" + + "\u0000\u0116\u0122\u0003\u00a3Q\u0000\u0117\u0119\u0005\r\u0000\u0000" + + "\u0118\u0117\u0001\u0000\u0000\u0000\u0118\u0119\u0001\u0000\u0000\u0000" + + "\u0119\u011a\u0001\u0000\u0000\u0000\u011a\u011d\u0005\n\u0000\u0000\u011b" + + "\u011d\u0002\f\r\u0000\u011c\u0118\u0001\u0000\u0000\u0000\u011c\u011b" + + "\u0001\u0000\u0000\u0000\u011d\u011f\u0001\u0000\u0000\u0000\u011e\u0120" + + "\u0003\u00a3Q\u0000\u011f\u011e\u0001\u0000\u0000\u0000\u011f\u0120\u0001" + + "\u0000\u0000\u0000\u0120\u0122\u0001\u0000\u0000\u0000\u0121\u0115\u0001" + + "\u0000\u0000\u0000\u0121\u011c\u0001\u0000\u0000\u0000\u0122\u0123\u0001" + + "\u0000\u0000\u0000\u0123\u0124\u0006\u0015\u0000\u0000\u0124,\u0001\u0000" + + "\u0000\u0000\u0125\u0129\u0007\u0002\u0000\u0000\u0126\u0128\u0007\u0003" + + "\u0000\u0000\u0127\u0126\u0001\u0000\u0000\u0000\u0128\u012b\u0001\u0000" + + "\u0000\u0000\u0129\u0127\u0001\u0000\u0000\u0000\u0129\u012a\u0001\u0000" + + "\u0000\u0000\u012a.\u0001\u0000\u0000\u0000\u012b\u0129\u0001\u0000\u0000" + + "\u0000\u012c\u0130\u0003\u0097K\u0000\u012d\u012f\u0003\u0099L\u0000\u012e" + + "\u012d\u0001\u0000\u0000\u0000\u012f\u0132\u0001\u0000\u0000\u0000\u0130" + + "\u012e\u0001\u0000\u0000\u0000\u0130\u0131\u0001\u0000\u0000\u0000\u0131" + + "\u0139\u0001\u0000\u0000\u0000\u0132\u0130\u0001\u0000\u0000\u0000\u0133" + + "\u0135\u00050\u0000\u0000\u0134\u0133\u0001\u0000\u0000\u0000\u0135\u0136" + + "\u0001\u0000\u0000\u0000\u0136\u0134\u0001\u0000\u0000\u0000\u0136\u0137" + + "\u0001\u0000\u0000\u0000\u0137\u0139\u0001\u0000\u0000\u0000\u0138\u012c" + + "\u0001\u0000\u0000\u0000\u0138\u0134\u0001\u0000\u0000\u0000\u01390\u0001" + + "\u0000\u0000\u0000\u013a\u013d\u0003\u009bM\u0000\u013b\u013d\u0003\u009d" + + "N\u0000\u013c\u013a\u0001\u0000\u0000\u0000\u013c\u013b\u0001\u0000\u0000" + + "\u0000\u013d2\u0001\u0000\u0000\u0000\u013e\u013f\u0005.\u0000\u0000\u013f" + + "4\u0001\u0000\u0000\u0000\u0140\u0141\u0005.\u0000\u0000\u0141\u0142\u0005" + + ".\u0000\u0000\u0142\u0143\u0005.\u0000\u0000\u01436\u0001\u0000\u0000" + + "\u0000\u0144\u0145\u0005*\u0000\u0000\u01458\u0001\u0000\u0000\u0000\u0146" + + "\u0147\u0005(\u0000\u0000\u0147\u0148\u0006\u001c\u0001\u0000\u0148:\u0001" + + "\u0000\u0000\u0000\u0149\u014a\u0005)\u0000\u0000\u014a\u014b\u0006\u001d" + + "\u0002\u0000\u014b<\u0001\u0000\u0000\u0000\u014c\u014d\u0005,\u0000\u0000" + + "\u014d>\u0001\u0000\u0000\u0000\u014e\u014f\u0005:\u0000\u0000\u014f@" + + "\u0001\u0000\u0000\u0000\u0150\u0151\u0005;\u0000\u0000\u0151B\u0001\u0000" + + "\u0000\u0000\u0152\u0153\u0005*\u0000\u0000\u0153\u0154\u0005*\u0000\u0000" + + "\u0154D\u0001\u0000\u0000\u0000\u0155\u0156\u0005=\u0000\u0000\u0156F" + + "\u0001\u0000\u0000\u0000\u0157\u0158\u0005[\u0000\u0000\u0158\u0159\u0006" + + "#\u0003\u0000\u0159H\u0001\u0000\u0000\u0000\u015a\u015b\u0005]\u0000" + + "\u0000\u015b\u015c\u0006$\u0004\u0000\u015cJ\u0001\u0000\u0000\u0000\u015d" + + "\u015e\u0005|\u0000\u0000\u015eL\u0001\u0000\u0000\u0000\u015f\u0160\u0005" + + "^\u0000\u0000\u0160N\u0001\u0000\u0000\u0000\u0161\u0162\u0005&\u0000" + + "\u0000\u0162P\u0001\u0000\u0000\u0000\u0163\u0164\u0005<\u0000\u0000\u0164" + + "\u0165\u0005<\u0000\u0000\u0165R\u0001\u0000\u0000\u0000\u0166\u0167\u0005" + + ">\u0000\u0000\u0167\u0168\u0005>\u0000\u0000\u0168T\u0001\u0000\u0000" + + "\u0000\u0169\u016a\u0005+\u0000\u0000\u016aV\u0001\u0000\u0000\u0000\u016b" + + "\u016c\u0005-\u0000\u0000\u016cX\u0001\u0000\u0000\u0000\u016d\u016e\u0005" + + "/\u0000\u0000\u016eZ\u0001\u0000\u0000\u0000\u016f\u0170\u0005%\u0000" + + "\u0000\u0170\\\u0001\u0000\u0000\u0000\u0171\u0172\u0005/\u0000\u0000" + + "\u0172\u0173\u0005/\u0000\u0000\u0173^\u0001\u0000\u0000\u0000\u0174\u0175" + + "\u0005~\u0000\u0000\u0175`\u0001\u0000\u0000\u0000\u0176\u0177\u0005{" + + "\u0000\u0000\u0177\u0178\u00060\u0005\u0000\u0178b\u0001\u0000\u0000\u0000" + + "\u0179\u017a\u0005}\u0000\u0000\u017a\u017b\u00061\u0006\u0000\u017bd" + + "\u0001\u0000\u0000\u0000\u017c\u017d\u0005<\u0000\u0000\u017df\u0001\u0000" + + "\u0000\u0000\u017e\u017f\u0005>\u0000\u0000\u017fh\u0001\u0000\u0000\u0000" + + "\u0180\u0181\u0005=\u0000\u0000\u0181\u0182\u0005=\u0000\u0000\u0182j" + + "\u0001\u0000\u0000\u0000\u0183\u0184\u0005>\u0000\u0000\u0184\u0185\u0005" + + "=\u0000\u0000\u0185l\u0001\u0000\u0000\u0000\u0186\u0187\u0005<\u0000" + + "\u0000\u0187\u0188\u0005=\u0000\u0000\u0188n\u0001\u0000\u0000\u0000\u0189" + + "\u018a\u0005<\u0000\u0000\u018a\u018b\u0005>\u0000\u0000\u018bp\u0001" + + "\u0000\u0000\u0000\u018c\u018d\u0005!\u0000\u0000\u018d\u018e\u0005=\u0000" + + "\u0000\u018er\u0001\u0000\u0000\u0000\u018f\u0190\u0005@\u0000\u0000\u0190" + + "t\u0001\u0000\u0000\u0000\u0191\u0192\u0005-\u0000\u0000\u0192\u0193\u0005" + + ">\u0000\u0000\u0193v\u0001\u0000\u0000\u0000\u0194\u0195\u0005+\u0000" + + "\u0000\u0195\u0196\u0005=\u0000\u0000\u0196x\u0001\u0000\u0000\u0000\u0197" + + "\u0198\u0005-\u0000\u0000\u0198\u0199\u0005=\u0000\u0000\u0199z\u0001" + + "\u0000\u0000\u0000\u019a\u019b\u0005*\u0000\u0000\u019b\u019c\u0005=\u0000" + + "\u0000\u019c|\u0001\u0000\u0000\u0000\u019d\u019e\u0005@\u0000\u0000\u019e" + + "\u019f\u0005=\u0000\u0000\u019f~\u0001\u0000\u0000\u0000\u01a0\u01a1\u0005" + + "/\u0000\u0000\u01a1\u01a2\u0005=\u0000\u0000\u01a2\u0080\u0001\u0000\u0000" + + "\u0000\u01a3\u01a4\u0005%\u0000\u0000\u01a4\u01a5\u0005=\u0000\u0000\u01a5" + + "\u0082\u0001\u0000\u0000\u0000\u01a6\u01a7\u0005&\u0000\u0000\u01a7\u01a8" + + "\u0005=\u0000\u0000\u01a8\u0084\u0001\u0000\u0000\u0000\u01a9\u01aa\u0005" + + "|\u0000\u0000\u01aa\u01ab\u0005=\u0000\u0000\u01ab\u0086\u0001\u0000\u0000" + + "\u0000\u01ac\u01ad\u0005^\u0000\u0000\u01ad\u01ae\u0005=\u0000\u0000\u01ae" + + "\u0088\u0001\u0000\u0000\u0000\u01af\u01b0\u0005<\u0000\u0000\u01b0\u01b1" + + "\u0005<\u0000\u0000\u01b1\u01b2\u0005=\u0000\u0000\u01b2\u008a\u0001\u0000" + + "\u0000\u0000\u01b3\u01b4\u0005>\u0000\u0000\u01b4\u01b5\u0005>\u0000\u0000" + + "\u01b5\u01b6\u0005=\u0000\u0000\u01b6\u008c\u0001\u0000\u0000\u0000\u01b7" + + "\u01b8\u0005*\u0000\u0000\u01b8\u01b9\u0005*\u0000\u0000\u01b9\u01ba\u0005" + + "=\u0000\u0000\u01ba\u008e\u0001\u0000\u0000\u0000\u01bb\u01bc\u0005/\u0000" + + "\u0000\u01bc\u01bd\u0005/\u0000\u0000\u01bd\u01be\u0005=\u0000\u0000\u01be" + + "\u0090\u0001\u0000\u0000\u0000\u01bf\u01c3\u0003\u00a3Q\u0000\u01c0\u01c3" + + "\u0003\u00a5R\u0000\u01c1\u01c3\u0003\u00a7S\u0000\u01c2\u01bf\u0001\u0000" + + "\u0000\u0000\u01c2\u01c0\u0001\u0000\u0000\u0000\u01c2\u01c1\u0001\u0000" + + "\u0000\u0000\u01c3\u01c4\u0001\u0000\u0000\u0000\u01c4\u01c5\u0006H\u0007" + + "\u0000\u01c5\u0092\u0001\u0000\u0000\u0000\u01c6\u01c7\t\u0000\u0000\u0000" + + "\u01c7\u0094\u0001\u0000\u0000\u0000\u01c8\u01c9\u0005\\\u0000\u0000\u01c9" + + "\u01cd\t\u0000\u0000\u0000\u01ca\u01cb\u0005\\\u0000\u0000\u01cb\u01cd" + + "\u0003+\u0015\u0000\u01cc\u01c8\u0001\u0000\u0000\u0000\u01cc\u01ca\u0001" + + "\u0000\u0000\u0000\u01cd\u0096\u0001\u0000\u0000\u0000\u01ce\u01cf\u0007" + + "\u0004\u0000\u0000\u01cf\u0098\u0001\u0000\u0000\u0000\u01d0\u01d1\u0007" + + "\u0005\u0000\u0000\u01d1\u009a\u0001\u0000\u0000\u0000\u01d2\u01d4\u0003" + + "\u009fO\u0000\u01d3\u01d2\u0001\u0000\u0000\u0000\u01d3\u01d4\u0001\u0000" + + "\u0000\u0000\u01d4\u01d5\u0001\u0000\u0000\u0000\u01d5\u01d7\u0005.\u0000" + + "\u0000\u01d6\u01d8\u0003\u0099L\u0000\u01d7\u01d6\u0001\u0000\u0000\u0000" + + "\u01d8\u01d9\u0001\u0000\u0000\u0000\u01d9\u01d7\u0001\u0000\u0000\u0000" + + "\u01d9\u01da\u0001\u0000\u0000\u0000\u01da\u01df\u0001\u0000\u0000\u0000" + + "\u01db\u01dc\u0003\u009fO\u0000\u01dc\u01dd\u0005.\u0000\u0000\u01dd\u01df" + + "\u0001\u0000\u0000\u0000\u01de\u01d3\u0001\u0000\u0000\u0000\u01de\u01db" + + "\u0001\u0000\u0000\u0000\u01df\u009c\u0001\u0000\u0000\u0000\u01e0\u01e3" + + "\u0003\u009fO\u0000\u01e1\u01e3\u0003\u009bM\u0000\u01e2\u01e0\u0001\u0000" + + "\u0000\u0000\u01e2\u01e1\u0001\u0000\u0000\u0000\u01e3\u01e4\u0001\u0000" + + "\u0000\u0000\u01e4\u01e5\u0003\u00a1P\u0000\u01e5\u009e\u0001\u0000\u0000" + + "\u0000\u01e6\u01e8\u0003\u0099L\u0000\u01e7\u01e6\u0001\u0000\u0000\u0000" + + "\u01e8\u01e9\u0001\u0000\u0000\u0000\u01e9\u01e7\u0001\u0000\u0000\u0000" + + "\u01e9\u01ea\u0001\u0000\u0000\u0000\u01ea\u00a0\u0001\u0000\u0000\u0000" + + "\u01eb\u01ed\u0007\u0006\u0000\u0000\u01ec\u01ee\u0007\u0007\u0000\u0000" + + "\u01ed\u01ec\u0001\u0000\u0000\u0000\u01ed\u01ee\u0001\u0000\u0000\u0000" + + "\u01ee\u01f0\u0001\u0000\u0000\u0000\u01ef\u01f1\u0003\u0099L\u0000\u01f0" + + "\u01ef\u0001\u0000\u0000\u0000\u01f1\u01f2\u0001\u0000\u0000\u0000\u01f2" + + "\u01f0\u0001\u0000\u0000\u0000\u01f2\u01f3\u0001\u0000\u0000\u0000\u01f3" + + "\u00a2\u0001\u0000\u0000\u0000\u01f4\u01f6\u0007\b\u0000\u0000\u01f5\u01f4" + + "\u0001\u0000\u0000\u0000\u01f6\u01f7\u0001\u0000\u0000\u0000\u01f7\u01f5" + + "\u0001\u0000\u0000\u0000\u01f7\u01f8\u0001\u0000\u0000\u0000\u01f8\u00a4" + + "\u0001\u0000\u0000\u0000\u01f9\u01fd\u0005#\u0000\u0000\u01fa\u01fc\b" + + "\t\u0000\u0000\u01fb\u01fa\u0001\u0000\u0000\u0000\u01fc\u01ff\u0001\u0000" + + "\u0000\u0000\u01fd\u01fb\u0001\u0000\u0000\u0000\u01fd\u01fe\u0001\u0000" + + "\u0000\u0000\u01fe\u00a6\u0001\u0000\u0000\u0000\u01ff\u01fd\u0001\u0000" + + "\u0000\u0000\u0200\u0202\u0005\\\u0000\u0000\u0201\u0203\u0003\u00a3Q" + + "\u0000\u0202\u0201\u0001\u0000\u0000\u0000\u0202\u0203\u0001\u0000\u0000" + + "\u0000\u0203\u0209\u0001\u0000\u0000\u0000\u0204\u0206\u0005\r\u0000\u0000" + + "\u0205\u0204\u0001\u0000\u0000\u0000\u0205\u0206\u0001\u0000\u0000\u0000" + + "\u0206\u0207\u0001\u0000\u0000\u0000\u0207\u020a\u0005\n\u0000\u0000\u0208" + + "\u020a\u0002\f\r\u0000\u0209\u0205\u0001\u0000\u0000\u0000\u0209\u0208" + + "\u0001\u0000\u0000\u0000\u020a\u00a8\u0001\u0000\u0000\u0000\u001e\u0000" + + "\u00ac\u00ae\u00b5\u00b7\u00bb\u00bf\u0118\u011c\u011f\u0121\u0129\u0130" + + "\u0136\u0138\u013c\u01c2\u01cc\u01d3\u01d9\u01de\u01e2\u01e9\u01ed\u01f2" + + "\u01f7\u01fd\u0202\u0205\u0209\b\u0001\u0015\u0000\u0001\u001c\u0001\u0001" + + "\u001d\u0002\u0001#\u0003\u0001$\u0004\u00010\u0005\u00011\u0006\u0006" + + "\u0000\u0000"; + public static final ATN _ATN = new ATNDeserializer().deserialize(_serializedATN.toCharArray()); + + static { + _decisionToDFA = new DFA[_ATN.getNumberOfDecisions()]; + for (int i = 0; i < _ATN.getNumberOfDecisions(); i++) { + _decisionToDFA[i] = new DFA(_ATN.getDecisionState(i), i); + } + } +}>>>>>>>830 b3a5(Refactor using package`com.clp.project` +) From 5245cc24de7c420ff50af8dbcd223a242984e44f Mon Sep 17 00:00:00 2001 From: Santo Cariotti Date: Tue, 4 Jun 2024 10:20:38 +0200 Subject: [PATCH 04/23] ci: Fix test --- src/Main.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Main.java b/src/Main.java index 3d173f5..e773b07 100644 --- a/src/Main.java +++ b/src/Main.java @@ -3,7 +3,7 @@ import java.io.BufferedReader; import java.io.File; import java.io.FileReader; -import java.io.IOException +import java.io.IOException; import java.util.Arrays; import java.util.Objects; import javax.swing.*; From c4ee017a4f553dc3bb2f0c4eb4816480e7ef5b42 Mon Sep 17 00:00:00 2001 From: Santo Cariotti Date: Tue, 4 Jun 2024 14:58:20 +0200 Subject: [PATCH 05/23] fix --- src/parser/Python3Lexer.java | 874 +---------------------------------- 1 file changed, 1 insertion(+), 873 deletions(-) diff --git a/src/parser/Python3Lexer.java b/src/parser/Python3Lexer.java index e3e5255..9a48207 100644 --- a/src/parser/Python3Lexer.java +++ b/src/parser/Python3Lexer.java @@ -595,876 +595,4 @@ private boolean NEWLINE_sempred(RuleContext _localctx, int predIndex) { _decisionToDFA[i] = new DFA(_ATN.getDecisionState(i), i); } } -}||||||| -parent of 830b3a5 (Refactor using package `com.clp.project`) -======= -package com.clp.project.parser; - -// import com.clp.project.parser.Python3LexerBase; - -// Generated from src/Python3Lexer.g4 by ANTLR 4.13.1 -import org.antlr.v4.runtime.*; -import org.antlr.v4.runtime.CharStream; -import org.antlr.v4.runtime.atn.*; -import org.antlr.v4.runtime.dfa.DFA; -import org.antlr.v4.runtime.misc.*; - -@SuppressWarnings({ - "all", - "warnings", - "unchecked", - "unused", - "cast", - "CheckReturnValue", - "this-escape" -}) -public class Python3Lexer extends Python3LexerBase { - static { - RuntimeMetaData.checkVersion("4.13.1", RuntimeMetaData.VERSION); - } - - protected static final DFA[] _decisionToDFA; - protected static final PredictionContextCache _sharedContextCache = new PredictionContextCache(); - public static final int INDENT = 1, - DEDENT = 2, - STRING = 3, - NUMBER = 4, - AND = 5, - AS = 6, - DEF = 7, - ELIF = 8, - ELSE = 9, - FALSE = 10, - FOR = 11, - FROM = 12, - IF = 13, - IMPORT = 14, - IN = 15, - IS = 16, - NONE = 17, - NOT = 18, - OR = 19, - RETURN = 20, - TRUE = 21, - UNDERSCORE = 22, - WHILE = 23, - NEWLINE = 24, - NAME = 25, - DECIMAL_INTEGER = 26, - FLOAT_NUMBER = 27, - DOT = 28, - ELLIPSIS = 29, - STAR = 30, - OPEN_PAREN = 31, - CLOSE_PAREN = 32, - COMMA = 33, - COLON = 34, - SEMI_COLON = 35, - POWER = 36, - ASSIGN = 37, - OPEN_BRACK = 38, - CLOSE_BRACK = 39, - OR_OP = 40, - XOR = 41, - AND_OP = 42, - LEFT_SHIFT = 43, - RIGHT_SHIFT = 44, - ADD = 45, - MINUS = 46, - DIV = 47, - MOD = 48, - IDIV = 49, - NOT_OP = 50, - OPEN_BRACE = 51, - CLOSE_BRACE = 52, - LESS_THAN = 53, - GREATER_THAN = 54, - EQUALS = 55, - GT_EQ = 56, - LT_EQ = 57, - NOT_EQ_1 = 58, - NOT_EQ_2 = 59, - AT = 60, - ARROW = 61, - ADD_ASSIGN = 62, - SUB_ASSIGN = 63, - MULT_ASSIGN = 64, - AT_ASSIGN = 65, - DIV_ASSIGN = 66, - MOD_ASSIGN = 67, - AND_ASSIGN = 68, - OR_ASSIGN = 69, - XOR_ASSIGN = 70, - LEFT_SHIFT_ASSIGN = 71, - RIGHT_SHIFT_ASSIGN = 72, - POWER_ASSIGN = 73, - IDIV_ASSIGN = 74, - SKIP_ = 75, - UNKNOWN_CHAR = 76; - public static String[] channelNames = { "DEFAULT_TOKEN_CHANNEL", "HIDDEN" }; - - public static String[] modeNames = { "DEFAULT_MODE" }; - - private static String[] makeRuleNames() { - return new String[] { - "STRING", - "NUMBER", - "AND", - "AS", - "DEF", - "ELIF", - "ELSE", - "FALSE", - "FOR", - "FROM", - "IF", - "IMPORT", - "IN", - "IS", - "NONE", - "NOT", - "OR", - "RETURN", - "TRUE", - "UNDERSCORE", - "WHILE", - "NEWLINE", - "NAME", - "DECIMAL_INTEGER", - "FLOAT_NUMBER", - "DOT", - "ELLIPSIS", - "STAR", - "OPEN_PAREN", - "CLOSE_PAREN", - "COMMA", - "COLON", - "SEMI_COLON", - "POWER", - "ASSIGN", - "OPEN_BRACK", - "CLOSE_BRACK", - "OR_OP", - "XOR", - "AND_OP", - "LEFT_SHIFT", - "RIGHT_SHIFT", - "ADD", - "MINUS", - "DIV", - "MOD", - "IDIV", - "NOT_OP", - "OPEN_BRACE", - "CLOSE_BRACE", - "LESS_THAN", - "GREATER_THAN", - "EQUALS", - "GT_EQ", - "LT_EQ", - "NOT_EQ_1", - "NOT_EQ_2", - "AT", - "ARROW", - "ADD_ASSIGN", - "SUB_ASSIGN", - "MULT_ASSIGN", - "AT_ASSIGN", - "DIV_ASSIGN", - "MOD_ASSIGN", - "AND_ASSIGN", - "OR_ASSIGN", - "XOR_ASSIGN", - "LEFT_SHIFT_ASSIGN", - "RIGHT_SHIFT_ASSIGN", - "POWER_ASSIGN", - "IDIV_ASSIGN", - "SKIP_", - "UNKNOWN_CHAR", - "STRING_ESCAPE_SEQ", - "NON_ZERO_DIGIT", - "DIGIT", - "POINT_FLOAT", - "EXPONENT_FLOAT", - "INT_PART", - "EXPONENT", - "SPACES", - "COMMENT", - "LINE_JOINING" - }; - } - - public static final String[] ruleNames = makeRuleNames(); - - private static String[] makeLiteralNames() { - return new String[] { - null, - null, - null, - null, - null, - "'and'", - "'as'", - "'def'", - "'elif'", - "'else'", - "'False'", - "'for'", - "'from'", - "'if'", - "'import'", - "'in'", - "'is'", - "'None'", - "'not'", - "'or'", - "'return'", - "'True'", - "'_'", - "'while'", - null, - null, - null, - null, - "'.'", - "'...'", - "'*'", - "'('", - "')'", - "','", - "':'", - "';'", - "'**'", - "'='", - "'['", - "']'", - "'|'", - "'^'", - "'&'", - "'<<'", - "'>>'", - "'+'", - "'-'", - "'/'", - "'%'", - "'//'", - "'~'", - "'{'", - "'}'", - "'<'", - "'>'", - "'=='", - "'>='", - "'<='", - "'<>'", - "'!='", - "'@'", - "'->'", - "'+='", - "'-='", - "'*='", - "'@='", - "'/='", - "'%='", - "'&='", - "'|='", - "'^='", - "'<<='", - "'>>='", - "'**='", - "'//='" - }; - } - - private static final String[] _LITERAL_NAMES = makeLiteralNames(); - - private static String[] makeSymbolicNames() { - return new String[] { - null, - "INDENT", - "DEDENT", - "STRING", - "NUMBER", - "AND", - "AS", - "DEF", - "ELIF", - "ELSE", - "FALSE", - "FOR", - "FROM", - "IF", - "IMPORT", - "IN", - "IS", - "NONE", - "NOT", - "OR", - "RETURN", - "TRUE", - "UNDERSCORE", - "WHILE", - "NEWLINE", - "NAME", - "DECIMAL_INTEGER", - "FLOAT_NUMBER", - "DOT", - "ELLIPSIS", - "STAR", - "OPEN_PAREN", - "CLOSE_PAREN", - "COMMA", - "COLON", - "SEMI_COLON", - "POWER", - "ASSIGN", - "OPEN_BRACK", - "CLOSE_BRACK", - "OR_OP", - "XOR", - "AND_OP", - "LEFT_SHIFT", - "RIGHT_SHIFT", - "ADD", - "MINUS", - "DIV", - "MOD", - "IDIV", - "NOT_OP", - "OPEN_BRACE", - "CLOSE_BRACE", - "LESS_THAN", - "GREATER_THAN", - "EQUALS", - "GT_EQ", - "LT_EQ", - "NOT_EQ_1", - "NOT_EQ_2", - "AT", - "ARROW", - "ADD_ASSIGN", - "SUB_ASSIGN", - "MULT_ASSIGN", - "AT_ASSIGN", - "DIV_ASSIGN", - "MOD_ASSIGN", - "AND_ASSIGN", - "OR_ASSIGN", - "XOR_ASSIGN", - "LEFT_SHIFT_ASSIGN", - "RIGHT_SHIFT_ASSIGN", - "POWER_ASSIGN", - "IDIV_ASSIGN", - "SKIP_", - "UNKNOWN_CHAR" - }; - } - - private static final String[] _SYMBOLIC_NAMES = makeSymbolicNames(); - public static final Vocabulary VOCABULARY = new VocabularyImpl(_LITERAL_NAMES, _SYMBOLIC_NAMES); - - /** - * @deprecated Use {@link #VOCABULARY} instead. - */ - @Deprecated - public static final String[] tokenNames; - - static { - tokenNames = new String[_SYMBOLIC_NAMES.length]; - for (int i = 0; i < tokenNames.length; i++) { - tokenNames[i] = VOCABULARY.getLiteralName(i); - if (tokenNames[i] == null) { - tokenNames[i] = VOCABULARY.getSymbolicName(i); - } - - if (tokenNames[i] == null) { - tokenNames[i] = ""; - } - } - } - - @Override - @Deprecated - public String[] getTokenNames() { - return tokenNames; - } - - @Override - public Vocabulary getVocabulary() { - return VOCABULARY; - } - - public Python3Lexer(CharStream input) { - super(input); - _interp = new LexerATNSimulator(this, _ATN, _decisionToDFA, _sharedContextCache); - } - - @Override - public String getGrammarFileName() { - return "Python3Lexer.g4"; - } - - @Override - public String[] getRuleNames() { - return ruleNames; - } - - @Override - public String getSerializedATN() { - return _serializedATN; - } - - @Override - public String[] getChannelNames() { - return channelNames; - } - - @Override - public String[] getModeNames() { - return modeNames; - } - - @Override - public ATN getATN() { - return _ATN; - } - - @Override - public void action(RuleContext _localctx, int ruleIndex, int actionIndex) { - switch (ruleIndex) { - case 21: - NEWLINE_action((RuleContext) _localctx, actionIndex); - break; - case 28: - OPEN_PAREN_action((RuleContext) _localctx, actionIndex); - break; - case 29: - CLOSE_PAREN_action((RuleContext) _localctx, actionIndex); - break; - case 35: - OPEN_BRACK_action((RuleContext) _localctx, actionIndex); - break; - case 36: - CLOSE_BRACK_action((RuleContext) _localctx, actionIndex); - break; - case 48: - OPEN_BRACE_action((RuleContext) _localctx, actionIndex); - break; - case 49: - CLOSE_BRACE_action((RuleContext) _localctx, actionIndex); - break; - } - } - - private void NEWLINE_action(RuleContext _localctx, int actionIndex) { - switch (actionIndex) { - case 0: - this.onNewLine(); - break; - } - } - - private void OPEN_PAREN_action(RuleContext _localctx, int actionIndex) { - switch (actionIndex) { - case 1: - this.openBrace(); - break; - } - } - - private void CLOSE_PAREN_action(RuleContext _localctx, int actionIndex) { - switch (actionIndex) { - case 2: - this.closeBrace(); - break; - } - } - - private void OPEN_BRACK_action(RuleContext _localctx, int actionIndex) { - switch (actionIndex) { - case 3: - this.openBrace(); - break; - } - } - - private void CLOSE_BRACK_action(RuleContext _localctx, int actionIndex) { - switch (actionIndex) { - case 4: - this.closeBrace(); - break; - } - } - - private void OPEN_BRACE_action(RuleContext _localctx, int actionIndex) { - switch (actionIndex) { - case 5: - this.openBrace(); - break; - } - } - - private void CLOSE_BRACE_action(RuleContext _localctx, int actionIndex) { - switch (actionIndex) { - case 6: - this.closeBrace(); - break; - } - } - - @Override - public boolean sempred(RuleContext _localctx, int ruleIndex, int predIndex) { - switch (ruleIndex) { - case 21: - return NEWLINE_sempred((RuleContext) _localctx, predIndex); - } - return true; - } - - private boolean NEWLINE_sempred(RuleContext _localctx, int predIndex) { - switch (predIndex) { - case 0: - return this.atStartOfInput(); - } - return true; - } - - public static final String _serializedATN = "\u0004\u0000L\u020b\u0006\uffff\uffff\u0002\u0000\u0007\u0000\u0002\u0001" - + "\u0007\u0001\u0002\u0002\u0007\u0002\u0002\u0003\u0007\u0003\u0002\u0004" - + "\u0007\u0004\u0002\u0005\u0007\u0005\u0002\u0006\u0007\u0006\u0002\u0007" - + "\u0007\u0007\u0002\b\u0007\b\u0002\t\u0007\t\u0002\n\u0007\n\u0002\u000b" - + "\u0007\u000b\u0002\f\u0007\f\u0002\r\u0007\r\u0002\u000e\u0007\u000e\u0002" - + "\u000f\u0007\u000f\u0002\u0010\u0007\u0010\u0002\u0011\u0007\u0011\u0002" - + "\u0012\u0007\u0012\u0002\u0013\u0007\u0013\u0002\u0014\u0007\u0014\u0002" - + "\u0015\u0007\u0015\u0002\u0016\u0007\u0016\u0002\u0017\u0007\u0017\u0002" - + "\u0018\u0007\u0018\u0002\u0019\u0007\u0019\u0002\u001a\u0007\u001a\u0002" - + "\u001b\u0007\u001b\u0002\u001c\u0007\u001c\u0002\u001d\u0007\u001d\u0002" - + "\u001e\u0007\u001e\u0002\u001f\u0007\u001f\u0002 \u0007 \u0002!\u0007" - + "!\u0002\"\u0007\"\u0002#\u0007#\u0002$\u0007$\u0002%\u0007%\u0002&\u0007" - + "&\u0002\'\u0007\'\u0002(\u0007(\u0002)\u0007)\u0002*\u0007*\u0002+\u0007" - + "+\u0002,\u0007,\u0002-\u0007-\u0002.\u0007.\u0002/\u0007/\u00020\u0007" - + "0\u00021\u00071\u00022\u00072\u00023\u00073\u00024\u00074\u00025\u0007" - + "5\u00026\u00076\u00027\u00077\u00028\u00078\u00029\u00079\u0002:\u0007" - + ":\u0002;\u0007;\u0002<\u0007<\u0002=\u0007=\u0002>\u0007>\u0002?\u0007" - + "?\u0002@\u0007@\u0002A\u0007A\u0002B\u0007B\u0002C\u0007C\u0002D\u0007" - + "D\u0002E\u0007E\u0002F\u0007F\u0002G\u0007G\u0002H\u0007H\u0002I\u0007" - + "I\u0002J\u0007J\u0002K\u0007K\u0002L\u0007L\u0002M\u0007M\u0002N\u0007" - + "N\u0002O\u0007O\u0002P\u0007P\u0002Q\u0007Q\u0002R\u0007R\u0002S\u0007" - + "S\u0001\u0000\u0001\u0000\u0001\u0000\u0005\u0000\u00ad\b\u0000\n\u0000" - + "\f\u0000\u00b0\t\u0000\u0001\u0000\u0001\u0000\u0001\u0000\u0001\u0000" - + "\u0005\u0000\u00b6\b\u0000\n\u0000\f\u0000\u00b9\t\u0000\u0001\u0000\u0003" - + "\u0000\u00bc\b\u0000\u0001\u0001\u0001\u0001\u0003\u0001\u00c0\b\u0001" - + "\u0001\u0002\u0001\u0002\u0001\u0002\u0001\u0002\u0001\u0003\u0001\u0003" - + "\u0001\u0003\u0001\u0004\u0001\u0004\u0001\u0004\u0001\u0004\u0001\u0005" - + "\u0001\u0005\u0001\u0005\u0001\u0005\u0001\u0005\u0001\u0006\u0001\u0006" - + "\u0001\u0006\u0001\u0006\u0001\u0006\u0001\u0007\u0001\u0007\u0001\u0007" - + "\u0001\u0007\u0001\u0007\u0001\u0007\u0001\b\u0001\b\u0001\b\u0001\b\u0001" - + "\t\u0001\t\u0001\t\u0001\t\u0001\t\u0001\n\u0001\n\u0001\n\u0001\u000b" - + "\u0001\u000b\u0001\u000b\u0001\u000b\u0001\u000b\u0001\u000b\u0001\u000b" - + "\u0001\f\u0001\f\u0001\f\u0001\r\u0001\r\u0001\r\u0001\u000e\u0001\u000e" - + "\u0001\u000e\u0001\u000e\u0001\u000e\u0001\u000f\u0001\u000f\u0001\u000f" - + "\u0001\u000f\u0001\u0010\u0001\u0010\u0001\u0010\u0001\u0011\u0001\u0011" - + "\u0001\u0011\u0001\u0011\u0001\u0011\u0001\u0011\u0001\u0011\u0001\u0012" - + "\u0001\u0012\u0001\u0012\u0001\u0012\u0001\u0012\u0001\u0013\u0001\u0013" - + "\u0001\u0014\u0001\u0014\u0001\u0014\u0001\u0014\u0001\u0014\u0001\u0014" - + "\u0001\u0015\u0001\u0015\u0001\u0015\u0003\u0015\u0119\b\u0015\u0001\u0015" - + "\u0001\u0015\u0003\u0015\u011d\b\u0015\u0001\u0015\u0003\u0015\u0120\b" - + "\u0015\u0003\u0015\u0122\b\u0015\u0001\u0015\u0001\u0015\u0001\u0016\u0001" - + "\u0016\u0005\u0016\u0128\b\u0016\n\u0016\f\u0016\u012b\t\u0016\u0001\u0017" - + "\u0001\u0017\u0005\u0017\u012f\b\u0017\n\u0017\f\u0017\u0132\t\u0017\u0001" - + "\u0017\u0004\u0017\u0135\b\u0017\u000b\u0017\f\u0017\u0136\u0003\u0017" - + "\u0139\b\u0017\u0001\u0018\u0001\u0018\u0003\u0018\u013d\b\u0018\u0001" - + "\u0019\u0001\u0019\u0001\u001a\u0001\u001a\u0001\u001a\u0001\u001a\u0001" - + "\u001b\u0001\u001b\u0001\u001c\u0001\u001c\u0001\u001c\u0001\u001d\u0001" - + "\u001d\u0001\u001d\u0001\u001e\u0001\u001e\u0001\u001f\u0001\u001f\u0001" - + " \u0001 \u0001!\u0001!\u0001!\u0001\"\u0001\"\u0001#\u0001#\u0001#\u0001" - + "$\u0001$\u0001$\u0001%\u0001%\u0001&\u0001&\u0001\'\u0001\'\u0001(\u0001" - + "(\u0001(\u0001)\u0001)\u0001)\u0001*\u0001*\u0001+\u0001+\u0001,\u0001" - + ",\u0001-\u0001-\u0001.\u0001.\u0001.\u0001/\u0001/\u00010\u00010\u0001" - + "0\u00011\u00011\u00011\u00012\u00012\u00013\u00013\u00014\u00014\u0001" - + "4\u00015\u00015\u00015\u00016\u00016\u00016\u00017\u00017\u00017\u0001" - + "8\u00018\u00018\u00019\u00019\u0001:\u0001:\u0001:\u0001;\u0001;\u0001" - + ";\u0001<\u0001<\u0001<\u0001=\u0001=\u0001=\u0001>\u0001>\u0001>\u0001" - + "?\u0001?\u0001?\u0001@\u0001@\u0001@\u0001A\u0001A\u0001A\u0001B\u0001" - + "B\u0001B\u0001C\u0001C\u0001C\u0001D\u0001D\u0001D\u0001D\u0001E\u0001" - + "E\u0001E\u0001E\u0001F\u0001F\u0001F\u0001F\u0001G\u0001G\u0001G\u0001" - + "G\u0001H\u0001H\u0001H\u0003H\u01c3\bH\u0001H\u0001H\u0001I\u0001I\u0001" - + "J\u0001J\u0001J\u0001J\u0003J\u01cd\bJ\u0001K\u0001K\u0001L\u0001L\u0001" - + "M\u0003M\u01d4\bM\u0001M\u0001M\u0004M\u01d8\bM\u000bM\fM\u01d9\u0001" - + "M\u0001M\u0001M\u0003M\u01df\bM\u0001N\u0001N\u0003N\u01e3\bN\u0001N\u0001" - + "N\u0001O\u0004O\u01e8\bO\u000bO\fO\u01e9\u0001P\u0001P\u0003P\u01ee\b" - + "P\u0001P\u0004P\u01f1\bP\u000bP\fP\u01f2\u0001Q\u0004Q\u01f6\bQ\u000b" - + "Q\fQ\u01f7\u0001R\u0001R\u0005R\u01fc\bR\nR\fR\u01ff\tR\u0001S\u0001S" - + "\u0003S\u0203\bS\u0001S\u0003S\u0206\bS\u0001S\u0001S\u0003S\u020a\bS" - + "\u0000\u0000T\u0001\u0003\u0003\u0004\u0005\u0005\u0007\u0006\t\u0007" - + "\u000b\b\r\t\u000f\n\u0011\u000b\u0013\f\u0015\r\u0017\u000e\u0019\u000f" - + "\u001b\u0010\u001d\u0011\u001f\u0012!\u0013#\u0014%\u0015\'\u0016)\u0017" - + "+\u0018-\u0019/\u001a1\u001b3\u001c5\u001d7\u001e9\u001f; =!?\"A#C$E%" - + "G&I\'K(M)O*Q+S,U-W.Y/[0]1_2a3c4e5g6i7k8m9o:q;sy?{@}A\u007fB\u0081" - + "C\u0083D\u0085E\u0087F\u0089G\u008bH\u008dI\u008fJ\u0091K\u0093L\u0095" - + "\u0000\u0097\u0000\u0099\u0000\u009b\u0000\u009d\u0000\u009f\u0000\u00a1" - + "\u0000\u00a3\u0000\u00a5\u0000\u00a7\u0000\u0001\u0000\n\u0004\u0000\n" - + "\n\f\r\'\'\\\\\u0004\u0000\n\n\f\r\"\"\\\\\u0003\u0000AZ__az\u0004\u0000" - + "09AZ__az\u0001\u000019\u0001\u000009\u0002\u0000EEee\u0002\u0000++--\u0002" - + "\u0000\t\t \u0002\u0000\n\n\f\r\u021e\u0000\u0001\u0001\u0000\u0000\u0000" - + "\u0000\u0003\u0001\u0000\u0000\u0000\u0000\u0005\u0001\u0000\u0000\u0000" - + "\u0000\u0007\u0001\u0000\u0000\u0000\u0000\t\u0001\u0000\u0000\u0000\u0000" - + "\u000b\u0001\u0000\u0000\u0000\u0000\r\u0001\u0000\u0000\u0000\u0000\u000f" - + "\u0001\u0000\u0000\u0000\u0000\u0011\u0001\u0000\u0000\u0000\u0000\u0013" - + "\u0001\u0000\u0000\u0000\u0000\u0015\u0001\u0000\u0000\u0000\u0000\u0017" - + "\u0001\u0000\u0000\u0000\u0000\u0019\u0001\u0000\u0000\u0000\u0000\u001b" - + "\u0001\u0000\u0000\u0000\u0000\u001d\u0001\u0000\u0000\u0000\u0000\u001f" - + "\u0001\u0000\u0000\u0000\u0000!\u0001\u0000\u0000\u0000\u0000#\u0001\u0000" - + "\u0000\u0000\u0000%\u0001\u0000\u0000\u0000\u0000\'\u0001\u0000\u0000" - + "\u0000\u0000)\u0001\u0000\u0000\u0000\u0000+\u0001\u0000\u0000\u0000\u0000" - + "-\u0001\u0000\u0000\u0000\u0000/\u0001\u0000\u0000\u0000\u00001\u0001" - + "\u0000\u0000\u0000\u00003\u0001\u0000\u0000\u0000\u00005\u0001\u0000\u0000" - + "\u0000\u00007\u0001\u0000\u0000\u0000\u00009\u0001\u0000\u0000\u0000\u0000" - + ";\u0001\u0000\u0000\u0000\u0000=\u0001\u0000\u0000\u0000\u0000?\u0001" - + "\u0000\u0000\u0000\u0000A\u0001\u0000\u0000\u0000\u0000C\u0001\u0000\u0000" - + "\u0000\u0000E\u0001\u0000\u0000\u0000\u0000G\u0001\u0000\u0000\u0000\u0000" - + "I\u0001\u0000\u0000\u0000\u0000K\u0001\u0000\u0000\u0000\u0000M\u0001" - + "\u0000\u0000\u0000\u0000O\u0001\u0000\u0000\u0000\u0000Q\u0001\u0000\u0000" - + "\u0000\u0000S\u0001\u0000\u0000\u0000\u0000U\u0001\u0000\u0000\u0000\u0000" - + "W\u0001\u0000\u0000\u0000\u0000Y\u0001\u0000\u0000\u0000\u0000[\u0001" - + "\u0000\u0000\u0000\u0000]\u0001\u0000\u0000\u0000\u0000_\u0001\u0000\u0000" - + "\u0000\u0000a\u0001\u0000\u0000\u0000\u0000c\u0001\u0000\u0000\u0000\u0000" - + "e\u0001\u0000\u0000\u0000\u0000g\u0001\u0000\u0000\u0000\u0000i\u0001" - + "\u0000\u0000\u0000\u0000k\u0001\u0000\u0000\u0000\u0000m\u0001\u0000\u0000" - + "\u0000\u0000o\u0001\u0000\u0000\u0000\u0000q\u0001\u0000\u0000\u0000\u0000" - + "s\u0001\u0000\u0000\u0000\u0000u\u0001\u0000\u0000\u0000\u0000w\u0001" - + "\u0000\u0000\u0000\u0000y\u0001\u0000\u0000\u0000\u0000{\u0001\u0000\u0000" - + "\u0000\u0000}\u0001\u0000\u0000\u0000\u0000\u007f\u0001\u0000\u0000\u0000" - + "\u0000\u0081\u0001\u0000\u0000\u0000\u0000\u0083\u0001\u0000\u0000\u0000" - + "\u0000\u0085\u0001\u0000\u0000\u0000\u0000\u0087\u0001\u0000\u0000\u0000" - + "\u0000\u0089\u0001\u0000\u0000\u0000\u0000\u008b\u0001\u0000\u0000\u0000" - + "\u0000\u008d\u0001\u0000\u0000\u0000\u0000\u008f\u0001\u0000\u0000\u0000" - + "\u0000\u0091\u0001\u0000\u0000\u0000\u0000\u0093\u0001\u0000\u0000\u0000" - + "\u0001\u00bb\u0001\u0000\u0000\u0000\u0003\u00bf\u0001\u0000\u0000\u0000" - + "\u0005\u00c1\u0001\u0000\u0000\u0000\u0007\u00c5\u0001\u0000\u0000\u0000" - + "\t\u00c8\u0001\u0000\u0000\u0000\u000b\u00cc\u0001\u0000\u0000\u0000\r" - + "\u00d1\u0001\u0000\u0000\u0000\u000f\u00d6\u0001\u0000\u0000\u0000\u0011" - + "\u00dc\u0001\u0000\u0000\u0000\u0013\u00e0\u0001\u0000\u0000\u0000\u0015" - + "\u00e5\u0001\u0000\u0000\u0000\u0017\u00e8\u0001\u0000\u0000\u0000\u0019" - + "\u00ef\u0001\u0000\u0000\u0000\u001b\u00f2\u0001\u0000\u0000\u0000\u001d" - + "\u00f5\u0001\u0000\u0000\u0000\u001f\u00fa\u0001\u0000\u0000\u0000!\u00fe" - + "\u0001\u0000\u0000\u0000#\u0101\u0001\u0000\u0000\u0000%\u0108\u0001\u0000" - + "\u0000\u0000\'\u010d\u0001\u0000\u0000\u0000)\u010f\u0001\u0000\u0000" - + "\u0000+\u0121\u0001\u0000\u0000\u0000-\u0125\u0001\u0000\u0000\u0000/" - + "\u0138\u0001\u0000\u0000\u00001\u013c\u0001\u0000\u0000\u00003\u013e\u0001" - + "\u0000\u0000\u00005\u0140\u0001\u0000\u0000\u00007\u0144\u0001\u0000\u0000" - + "\u00009\u0146\u0001\u0000\u0000\u0000;\u0149\u0001\u0000\u0000\u0000=" - + "\u014c\u0001\u0000\u0000\u0000?\u014e\u0001\u0000\u0000\u0000A\u0150\u0001" - + "\u0000\u0000\u0000C\u0152\u0001\u0000\u0000\u0000E\u0155\u0001\u0000\u0000" - + "\u0000G\u0157\u0001\u0000\u0000\u0000I\u015a\u0001\u0000\u0000\u0000K" - + "\u015d\u0001\u0000\u0000\u0000M\u015f\u0001\u0000\u0000\u0000O\u0161\u0001" - + "\u0000\u0000\u0000Q\u0163\u0001\u0000\u0000\u0000S\u0166\u0001\u0000\u0000" - + "\u0000U\u0169\u0001\u0000\u0000\u0000W\u016b\u0001\u0000\u0000\u0000Y" - + "\u016d\u0001\u0000\u0000\u0000[\u016f\u0001\u0000\u0000\u0000]\u0171\u0001" - + "\u0000\u0000\u0000_\u0174\u0001\u0000\u0000\u0000a\u0176\u0001\u0000\u0000" - + "\u0000c\u0179\u0001\u0000\u0000\u0000e\u017c\u0001\u0000\u0000\u0000g" - + "\u017e\u0001\u0000\u0000\u0000i\u0180\u0001\u0000\u0000\u0000k\u0183\u0001" - + "\u0000\u0000\u0000m\u0186\u0001\u0000\u0000\u0000o\u0189\u0001\u0000\u0000" - + "\u0000q\u018c\u0001\u0000\u0000\u0000s\u018f\u0001\u0000\u0000\u0000u" - + "\u0191\u0001\u0000\u0000\u0000w\u0194\u0001\u0000\u0000\u0000y\u0197\u0001" - + "\u0000\u0000\u0000{\u019a\u0001\u0000\u0000\u0000}\u019d\u0001\u0000\u0000" - + "\u0000\u007f\u01a0\u0001\u0000\u0000\u0000\u0081\u01a3\u0001\u0000\u0000" - + "\u0000\u0083\u01a6\u0001\u0000\u0000\u0000\u0085\u01a9\u0001\u0000\u0000" - + "\u0000\u0087\u01ac\u0001\u0000\u0000\u0000\u0089\u01af\u0001\u0000\u0000" - + "\u0000\u008b\u01b3\u0001\u0000\u0000\u0000\u008d\u01b7\u0001\u0000\u0000" - + "\u0000\u008f\u01bb\u0001\u0000\u0000\u0000\u0091\u01c2\u0001\u0000\u0000" - + "\u0000\u0093\u01c6\u0001\u0000\u0000\u0000\u0095\u01cc\u0001\u0000\u0000" - + "\u0000\u0097\u01ce\u0001\u0000\u0000\u0000\u0099\u01d0\u0001\u0000\u0000" - + "\u0000\u009b\u01de\u0001\u0000\u0000\u0000\u009d\u01e2\u0001\u0000\u0000" - + "\u0000\u009f\u01e7\u0001\u0000\u0000\u0000\u00a1\u01eb\u0001\u0000\u0000" - + "\u0000\u00a3\u01f5\u0001\u0000\u0000\u0000\u00a5\u01f9\u0001\u0000\u0000" - + "\u0000\u00a7\u0200\u0001\u0000\u0000\u0000\u00a9\u00ae\u0005\'\u0000\u0000" - + "\u00aa\u00ad\u0003\u0095J\u0000\u00ab\u00ad\b\u0000\u0000\u0000\u00ac" - + "\u00aa\u0001\u0000\u0000\u0000\u00ac\u00ab\u0001\u0000\u0000\u0000\u00ad" - + "\u00b0\u0001\u0000\u0000\u0000\u00ae\u00ac\u0001\u0000\u0000\u0000\u00ae" - + "\u00af\u0001\u0000\u0000\u0000\u00af\u00b1\u0001\u0000\u0000\u0000\u00b0" - + "\u00ae\u0001\u0000\u0000\u0000\u00b1\u00bc\u0005\'\u0000\u0000\u00b2\u00b7" - + "\u0005\"\u0000\u0000\u00b3\u00b6\u0003\u0095J\u0000\u00b4\u00b6\b\u0001" - + "\u0000\u0000\u00b5\u00b3\u0001\u0000\u0000\u0000\u00b5\u00b4\u0001\u0000" - + "\u0000\u0000\u00b6\u00b9\u0001\u0000\u0000\u0000\u00b7\u00b5\u0001\u0000" - + "\u0000\u0000\u00b7\u00b8\u0001\u0000\u0000\u0000\u00b8\u00ba\u0001\u0000" - + "\u0000\u0000\u00b9\u00b7\u0001\u0000\u0000\u0000\u00ba\u00bc\u0005\"\u0000" - + "\u0000\u00bb\u00a9\u0001\u0000\u0000\u0000\u00bb\u00b2\u0001\u0000\u0000" - + "\u0000\u00bc\u0002\u0001\u0000\u0000\u0000\u00bd\u00c0\u0003/\u0017\u0000" - + "\u00be\u00c0\u00031\u0018\u0000\u00bf\u00bd\u0001\u0000\u0000\u0000\u00bf" - + "\u00be\u0001\u0000\u0000\u0000\u00c0\u0004\u0001\u0000\u0000\u0000\u00c1" - + "\u00c2\u0005a\u0000\u0000\u00c2\u00c3\u0005n\u0000\u0000\u00c3\u00c4\u0005" - + "d\u0000\u0000\u00c4\u0006\u0001\u0000\u0000\u0000\u00c5\u00c6\u0005a\u0000" - + "\u0000\u00c6\u00c7\u0005s\u0000\u0000\u00c7\b\u0001\u0000\u0000\u0000" - + "\u00c8\u00c9\u0005d\u0000\u0000\u00c9\u00ca\u0005e\u0000\u0000\u00ca\u00cb" - + "\u0005f\u0000\u0000\u00cb\n\u0001\u0000\u0000\u0000\u00cc\u00cd\u0005" - + "e\u0000\u0000\u00cd\u00ce\u0005l\u0000\u0000\u00ce\u00cf\u0005i\u0000" - + "\u0000\u00cf\u00d0\u0005f\u0000\u0000\u00d0\f\u0001\u0000\u0000\u0000" - + "\u00d1\u00d2\u0005e\u0000\u0000\u00d2\u00d3\u0005l\u0000\u0000\u00d3\u00d4" - + "\u0005s\u0000\u0000\u00d4\u00d5\u0005e\u0000\u0000\u00d5\u000e\u0001\u0000" - + "\u0000\u0000\u00d6\u00d7\u0005F\u0000\u0000\u00d7\u00d8\u0005a\u0000\u0000" - + "\u00d8\u00d9\u0005l\u0000\u0000\u00d9\u00da\u0005s\u0000\u0000\u00da\u00db" - + "\u0005e\u0000\u0000\u00db\u0010\u0001\u0000\u0000\u0000\u00dc\u00dd\u0005" - + "f\u0000\u0000\u00dd\u00de\u0005o\u0000\u0000\u00de\u00df\u0005r\u0000" - + "\u0000\u00df\u0012\u0001\u0000\u0000\u0000\u00e0\u00e1\u0005f\u0000\u0000" - + "\u00e1\u00e2\u0005r\u0000\u0000\u00e2\u00e3\u0005o\u0000\u0000\u00e3\u00e4" - + "\u0005m\u0000\u0000\u00e4\u0014\u0001\u0000\u0000\u0000\u00e5\u00e6\u0005" - + "i\u0000\u0000\u00e6\u00e7\u0005f\u0000\u0000\u00e7\u0016\u0001\u0000\u0000" - + "\u0000\u00e8\u00e9\u0005i\u0000\u0000\u00e9\u00ea\u0005m\u0000\u0000\u00ea" - + "\u00eb\u0005p\u0000\u0000\u00eb\u00ec\u0005o\u0000\u0000\u00ec\u00ed\u0005" - + "r\u0000\u0000\u00ed\u00ee\u0005t\u0000\u0000\u00ee\u0018\u0001\u0000\u0000" - + "\u0000\u00ef\u00f0\u0005i\u0000\u0000\u00f0\u00f1\u0005n\u0000\u0000\u00f1" - + "\u001a\u0001\u0000\u0000\u0000\u00f2\u00f3\u0005i\u0000\u0000\u00f3\u00f4" - + "\u0005s\u0000\u0000\u00f4\u001c\u0001\u0000\u0000\u0000\u00f5\u00f6\u0005" - + "N\u0000\u0000\u00f6\u00f7\u0005o\u0000\u0000\u00f7\u00f8\u0005n\u0000" - + "\u0000\u00f8\u00f9\u0005e\u0000\u0000\u00f9\u001e\u0001\u0000\u0000\u0000" - + "\u00fa\u00fb\u0005n\u0000\u0000\u00fb\u00fc\u0005o\u0000\u0000\u00fc\u00fd" - + "\u0005t\u0000\u0000\u00fd \u0001\u0000\u0000\u0000\u00fe\u00ff\u0005o" - + "\u0000\u0000\u00ff\u0100\u0005r\u0000\u0000\u0100\"\u0001\u0000\u0000" - + "\u0000\u0101\u0102\u0005r\u0000\u0000\u0102\u0103\u0005e\u0000\u0000\u0103" - + "\u0104\u0005t\u0000\u0000\u0104\u0105\u0005u\u0000\u0000\u0105\u0106\u0005" - + "r\u0000\u0000\u0106\u0107\u0005n\u0000\u0000\u0107$\u0001\u0000\u0000" - + "\u0000\u0108\u0109\u0005T\u0000\u0000\u0109\u010a\u0005r\u0000\u0000\u010a" - + "\u010b\u0005u\u0000\u0000\u010b\u010c\u0005e\u0000\u0000\u010c&\u0001" - + "\u0000\u0000\u0000\u010d\u010e\u0005_\u0000\u0000\u010e(\u0001\u0000\u0000" - + "\u0000\u010f\u0110\u0005w\u0000\u0000\u0110\u0111\u0005h\u0000\u0000\u0111" - + "\u0112\u0005i\u0000\u0000\u0112\u0113\u0005l\u0000\u0000\u0113\u0114\u0005" - + "e\u0000\u0000\u0114*\u0001\u0000\u0000\u0000\u0115\u0116\u0004\u0015\u0000" - + "\u0000\u0116\u0122\u0003\u00a3Q\u0000\u0117\u0119\u0005\r\u0000\u0000" - + "\u0118\u0117\u0001\u0000\u0000\u0000\u0118\u0119\u0001\u0000\u0000\u0000" - + "\u0119\u011a\u0001\u0000\u0000\u0000\u011a\u011d\u0005\n\u0000\u0000\u011b" - + "\u011d\u0002\f\r\u0000\u011c\u0118\u0001\u0000\u0000\u0000\u011c\u011b" - + "\u0001\u0000\u0000\u0000\u011d\u011f\u0001\u0000\u0000\u0000\u011e\u0120" - + "\u0003\u00a3Q\u0000\u011f\u011e\u0001\u0000\u0000\u0000\u011f\u0120\u0001" - + "\u0000\u0000\u0000\u0120\u0122\u0001\u0000\u0000\u0000\u0121\u0115\u0001" - + "\u0000\u0000\u0000\u0121\u011c\u0001\u0000\u0000\u0000\u0122\u0123\u0001" - + "\u0000\u0000\u0000\u0123\u0124\u0006\u0015\u0000\u0000\u0124,\u0001\u0000" - + "\u0000\u0000\u0125\u0129\u0007\u0002\u0000\u0000\u0126\u0128\u0007\u0003" - + "\u0000\u0000\u0127\u0126\u0001\u0000\u0000\u0000\u0128\u012b\u0001\u0000" - + "\u0000\u0000\u0129\u0127\u0001\u0000\u0000\u0000\u0129\u012a\u0001\u0000" - + "\u0000\u0000\u012a.\u0001\u0000\u0000\u0000\u012b\u0129\u0001\u0000\u0000" - + "\u0000\u012c\u0130\u0003\u0097K\u0000\u012d\u012f\u0003\u0099L\u0000\u012e" - + "\u012d\u0001\u0000\u0000\u0000\u012f\u0132\u0001\u0000\u0000\u0000\u0130" - + "\u012e\u0001\u0000\u0000\u0000\u0130\u0131\u0001\u0000\u0000\u0000\u0131" - + "\u0139\u0001\u0000\u0000\u0000\u0132\u0130\u0001\u0000\u0000\u0000\u0133" - + "\u0135\u00050\u0000\u0000\u0134\u0133\u0001\u0000\u0000\u0000\u0135\u0136" - + "\u0001\u0000\u0000\u0000\u0136\u0134\u0001\u0000\u0000\u0000\u0136\u0137" - + "\u0001\u0000\u0000\u0000\u0137\u0139\u0001\u0000\u0000\u0000\u0138\u012c" - + "\u0001\u0000\u0000\u0000\u0138\u0134\u0001\u0000\u0000\u0000\u01390\u0001" - + "\u0000\u0000\u0000\u013a\u013d\u0003\u009bM\u0000\u013b\u013d\u0003\u009d" - + "N\u0000\u013c\u013a\u0001\u0000\u0000\u0000\u013c\u013b\u0001\u0000\u0000" - + "\u0000\u013d2\u0001\u0000\u0000\u0000\u013e\u013f\u0005.\u0000\u0000\u013f" - + "4\u0001\u0000\u0000\u0000\u0140\u0141\u0005.\u0000\u0000\u0141\u0142\u0005" - + ".\u0000\u0000\u0142\u0143\u0005.\u0000\u0000\u01436\u0001\u0000\u0000" - + "\u0000\u0144\u0145\u0005*\u0000\u0000\u01458\u0001\u0000\u0000\u0000\u0146" - + "\u0147\u0005(\u0000\u0000\u0147\u0148\u0006\u001c\u0001\u0000\u0148:\u0001" - + "\u0000\u0000\u0000\u0149\u014a\u0005)\u0000\u0000\u014a\u014b\u0006\u001d" - + "\u0002\u0000\u014b<\u0001\u0000\u0000\u0000\u014c\u014d\u0005,\u0000\u0000" - + "\u014d>\u0001\u0000\u0000\u0000\u014e\u014f\u0005:\u0000\u0000\u014f@" - + "\u0001\u0000\u0000\u0000\u0150\u0151\u0005;\u0000\u0000\u0151B\u0001\u0000" - + "\u0000\u0000\u0152\u0153\u0005*\u0000\u0000\u0153\u0154\u0005*\u0000\u0000" - + "\u0154D\u0001\u0000\u0000\u0000\u0155\u0156\u0005=\u0000\u0000\u0156F" - + "\u0001\u0000\u0000\u0000\u0157\u0158\u0005[\u0000\u0000\u0158\u0159\u0006" - + "#\u0003\u0000\u0159H\u0001\u0000\u0000\u0000\u015a\u015b\u0005]\u0000" - + "\u0000\u015b\u015c\u0006$\u0004\u0000\u015cJ\u0001\u0000\u0000\u0000\u015d" - + "\u015e\u0005|\u0000\u0000\u015eL\u0001\u0000\u0000\u0000\u015f\u0160\u0005" - + "^\u0000\u0000\u0160N\u0001\u0000\u0000\u0000\u0161\u0162\u0005&\u0000" - + "\u0000\u0162P\u0001\u0000\u0000\u0000\u0163\u0164\u0005<\u0000\u0000\u0164" - + "\u0165\u0005<\u0000\u0000\u0165R\u0001\u0000\u0000\u0000\u0166\u0167\u0005" - + ">\u0000\u0000\u0167\u0168\u0005>\u0000\u0000\u0168T\u0001\u0000\u0000" - + "\u0000\u0169\u016a\u0005+\u0000\u0000\u016aV\u0001\u0000\u0000\u0000\u016b" - + "\u016c\u0005-\u0000\u0000\u016cX\u0001\u0000\u0000\u0000\u016d\u016e\u0005" - + "/\u0000\u0000\u016eZ\u0001\u0000\u0000\u0000\u016f\u0170\u0005%\u0000" - + "\u0000\u0170\\\u0001\u0000\u0000\u0000\u0171\u0172\u0005/\u0000\u0000" - + "\u0172\u0173\u0005/\u0000\u0000\u0173^\u0001\u0000\u0000\u0000\u0174\u0175" - + "\u0005~\u0000\u0000\u0175`\u0001\u0000\u0000\u0000\u0176\u0177\u0005{" - + "\u0000\u0000\u0177\u0178\u00060\u0005\u0000\u0178b\u0001\u0000\u0000\u0000" - + "\u0179\u017a\u0005}\u0000\u0000\u017a\u017b\u00061\u0006\u0000\u017bd" - + "\u0001\u0000\u0000\u0000\u017c\u017d\u0005<\u0000\u0000\u017df\u0001\u0000" - + "\u0000\u0000\u017e\u017f\u0005>\u0000\u0000\u017fh\u0001\u0000\u0000\u0000" - + "\u0180\u0181\u0005=\u0000\u0000\u0181\u0182\u0005=\u0000\u0000\u0182j" - + "\u0001\u0000\u0000\u0000\u0183\u0184\u0005>\u0000\u0000\u0184\u0185\u0005" - + "=\u0000\u0000\u0185l\u0001\u0000\u0000\u0000\u0186\u0187\u0005<\u0000" - + "\u0000\u0187\u0188\u0005=\u0000\u0000\u0188n\u0001\u0000\u0000\u0000\u0189" - + "\u018a\u0005<\u0000\u0000\u018a\u018b\u0005>\u0000\u0000\u018bp\u0001" - + "\u0000\u0000\u0000\u018c\u018d\u0005!\u0000\u0000\u018d\u018e\u0005=\u0000" - + "\u0000\u018er\u0001\u0000\u0000\u0000\u018f\u0190\u0005@\u0000\u0000\u0190" - + "t\u0001\u0000\u0000\u0000\u0191\u0192\u0005-\u0000\u0000\u0192\u0193\u0005" - + ">\u0000\u0000\u0193v\u0001\u0000\u0000\u0000\u0194\u0195\u0005+\u0000" - + "\u0000\u0195\u0196\u0005=\u0000\u0000\u0196x\u0001\u0000\u0000\u0000\u0197" - + "\u0198\u0005-\u0000\u0000\u0198\u0199\u0005=\u0000\u0000\u0199z\u0001" - + "\u0000\u0000\u0000\u019a\u019b\u0005*\u0000\u0000\u019b\u019c\u0005=\u0000" - + "\u0000\u019c|\u0001\u0000\u0000\u0000\u019d\u019e\u0005@\u0000\u0000\u019e" - + "\u019f\u0005=\u0000\u0000\u019f~\u0001\u0000\u0000\u0000\u01a0\u01a1\u0005" - + "/\u0000\u0000\u01a1\u01a2\u0005=\u0000\u0000\u01a2\u0080\u0001\u0000\u0000" - + "\u0000\u01a3\u01a4\u0005%\u0000\u0000\u01a4\u01a5\u0005=\u0000\u0000\u01a5" - + "\u0082\u0001\u0000\u0000\u0000\u01a6\u01a7\u0005&\u0000\u0000\u01a7\u01a8" - + "\u0005=\u0000\u0000\u01a8\u0084\u0001\u0000\u0000\u0000\u01a9\u01aa\u0005" - + "|\u0000\u0000\u01aa\u01ab\u0005=\u0000\u0000\u01ab\u0086\u0001\u0000\u0000" - + "\u0000\u01ac\u01ad\u0005^\u0000\u0000\u01ad\u01ae\u0005=\u0000\u0000\u01ae" - + "\u0088\u0001\u0000\u0000\u0000\u01af\u01b0\u0005<\u0000\u0000\u01b0\u01b1" - + "\u0005<\u0000\u0000\u01b1\u01b2\u0005=\u0000\u0000\u01b2\u008a\u0001\u0000" - + "\u0000\u0000\u01b3\u01b4\u0005>\u0000\u0000\u01b4\u01b5\u0005>\u0000\u0000" - + "\u01b5\u01b6\u0005=\u0000\u0000\u01b6\u008c\u0001\u0000\u0000\u0000\u01b7" - + "\u01b8\u0005*\u0000\u0000\u01b8\u01b9\u0005*\u0000\u0000\u01b9\u01ba\u0005" - + "=\u0000\u0000\u01ba\u008e\u0001\u0000\u0000\u0000\u01bb\u01bc\u0005/\u0000" - + "\u0000\u01bc\u01bd\u0005/\u0000\u0000\u01bd\u01be\u0005=\u0000\u0000\u01be" - + "\u0090\u0001\u0000\u0000\u0000\u01bf\u01c3\u0003\u00a3Q\u0000\u01c0\u01c3" - + "\u0003\u00a5R\u0000\u01c1\u01c3\u0003\u00a7S\u0000\u01c2\u01bf\u0001\u0000" - + "\u0000\u0000\u01c2\u01c0\u0001\u0000\u0000\u0000\u01c2\u01c1\u0001\u0000" - + "\u0000\u0000\u01c3\u01c4\u0001\u0000\u0000\u0000\u01c4\u01c5\u0006H\u0007" - + "\u0000\u01c5\u0092\u0001\u0000\u0000\u0000\u01c6\u01c7\t\u0000\u0000\u0000" - + "\u01c7\u0094\u0001\u0000\u0000\u0000\u01c8\u01c9\u0005\\\u0000\u0000\u01c9" - + "\u01cd\t\u0000\u0000\u0000\u01ca\u01cb\u0005\\\u0000\u0000\u01cb\u01cd" - + "\u0003+\u0015\u0000\u01cc\u01c8\u0001\u0000\u0000\u0000\u01cc\u01ca\u0001" - + "\u0000\u0000\u0000\u01cd\u0096\u0001\u0000\u0000\u0000\u01ce\u01cf\u0007" - + "\u0004\u0000\u0000\u01cf\u0098\u0001\u0000\u0000\u0000\u01d0\u01d1\u0007" - + "\u0005\u0000\u0000\u01d1\u009a\u0001\u0000\u0000\u0000\u01d2\u01d4\u0003" - + "\u009fO\u0000\u01d3\u01d2\u0001\u0000\u0000\u0000\u01d3\u01d4\u0001\u0000" - + "\u0000\u0000\u01d4\u01d5\u0001\u0000\u0000\u0000\u01d5\u01d7\u0005.\u0000" - + "\u0000\u01d6\u01d8\u0003\u0099L\u0000\u01d7\u01d6\u0001\u0000\u0000\u0000" - + "\u01d8\u01d9\u0001\u0000\u0000\u0000\u01d9\u01d7\u0001\u0000\u0000\u0000" - + "\u01d9\u01da\u0001\u0000\u0000\u0000\u01da\u01df\u0001\u0000\u0000\u0000" - + "\u01db\u01dc\u0003\u009fO\u0000\u01dc\u01dd\u0005.\u0000\u0000\u01dd\u01df" - + "\u0001\u0000\u0000\u0000\u01de\u01d3\u0001\u0000\u0000\u0000\u01de\u01db" - + "\u0001\u0000\u0000\u0000\u01df\u009c\u0001\u0000\u0000\u0000\u01e0\u01e3" - + "\u0003\u009fO\u0000\u01e1\u01e3\u0003\u009bM\u0000\u01e2\u01e0\u0001\u0000" - + "\u0000\u0000\u01e2\u01e1\u0001\u0000\u0000\u0000\u01e3\u01e4\u0001\u0000" - + "\u0000\u0000\u01e4\u01e5\u0003\u00a1P\u0000\u01e5\u009e\u0001\u0000\u0000" - + "\u0000\u01e6\u01e8\u0003\u0099L\u0000\u01e7\u01e6\u0001\u0000\u0000\u0000" - + "\u01e8\u01e9\u0001\u0000\u0000\u0000\u01e9\u01e7\u0001\u0000\u0000\u0000" - + "\u01e9\u01ea\u0001\u0000\u0000\u0000\u01ea\u00a0\u0001\u0000\u0000\u0000" - + "\u01eb\u01ed\u0007\u0006\u0000\u0000\u01ec\u01ee\u0007\u0007\u0000\u0000" - + "\u01ed\u01ec\u0001\u0000\u0000\u0000\u01ed\u01ee\u0001\u0000\u0000\u0000" - + "\u01ee\u01f0\u0001\u0000\u0000\u0000\u01ef\u01f1\u0003\u0099L\u0000\u01f0" - + "\u01ef\u0001\u0000\u0000\u0000\u01f1\u01f2\u0001\u0000\u0000\u0000\u01f2" - + "\u01f0\u0001\u0000\u0000\u0000\u01f2\u01f3\u0001\u0000\u0000\u0000\u01f3" - + "\u00a2\u0001\u0000\u0000\u0000\u01f4\u01f6\u0007\b\u0000\u0000\u01f5\u01f4" - + "\u0001\u0000\u0000\u0000\u01f6\u01f7\u0001\u0000\u0000\u0000\u01f7\u01f5" - + "\u0001\u0000\u0000\u0000\u01f7\u01f8\u0001\u0000\u0000\u0000\u01f8\u00a4" - + "\u0001\u0000\u0000\u0000\u01f9\u01fd\u0005#\u0000\u0000\u01fa\u01fc\b" - + "\t\u0000\u0000\u01fb\u01fa\u0001\u0000\u0000\u0000\u01fc\u01ff\u0001\u0000" - + "\u0000\u0000\u01fd\u01fb\u0001\u0000\u0000\u0000\u01fd\u01fe\u0001\u0000" - + "\u0000\u0000\u01fe\u00a6\u0001\u0000\u0000\u0000\u01ff\u01fd\u0001\u0000" - + "\u0000\u0000\u0200\u0202\u0005\\\u0000\u0000\u0201\u0203\u0003\u00a3Q" - + "\u0000\u0202\u0201\u0001\u0000\u0000\u0000\u0202\u0203\u0001\u0000\u0000" - + "\u0000\u0203\u0209\u0001\u0000\u0000\u0000\u0204\u0206\u0005\r\u0000\u0000" - + "\u0205\u0204\u0001\u0000\u0000\u0000\u0205\u0206\u0001\u0000\u0000\u0000" - + "\u0206\u0207\u0001\u0000\u0000\u0000\u0207\u020a\u0005\n\u0000\u0000\u0208" - + "\u020a\u0002\f\r\u0000\u0209\u0205\u0001\u0000\u0000\u0000\u0209\u0208" - + "\u0001\u0000\u0000\u0000\u020a\u00a8\u0001\u0000\u0000\u0000\u001e\u0000" - + "\u00ac\u00ae\u00b5\u00b7\u00bb\u00bf\u0118\u011c\u011f\u0121\u0129\u0130" - + "\u0136\u0138\u013c\u01c2\u01cc\u01d3\u01d9\u01de\u01e2\u01e9\u01ed\u01f2" - + "\u01f7\u01fd\u0202\u0205\u0209\b\u0001\u0015\u0000\u0001\u001c\u0001\u0001" - + "\u001d\u0002\u0001#\u0003\u0001$\u0004\u00010\u0005\u00011\u0006\u0006" - + "\u0000\u0000"; - public static final ATN _ATN = new ATNDeserializer().deserialize(_serializedATN.toCharArray()); - - static { - _decisionToDFA = new DFA[_ATN.getNumberOfDecisions()]; - for (int i = 0; i < _ATN.getNumberOfDecisions(); i++) { - _decisionToDFA[i] = new DFA(_ATN.getDecisionState(i), i); - } - } -}>>>>>>>830 b3a5(Refactor using package`com.clp.project` -) +} From 082c9acf1a85457a2873cd58ca7f7a81791542b6 Mon Sep 17 00:00:00 2001 From: Santo Cariotti Date: Tue, 4 Jun 2024 14:55:10 +0200 Subject: [PATCH 06/23] wip --- Makefile | 7 +- progs/test2.py | 5 ++ src/Main.java | 78 ++++++++++++-------- src/ast/BoolType.java | 8 +++ src/ast/CompoundNode.java | 37 ++++++++++ src/ast/ErrorType.java | 7 ++ src/ast/IfNode.java | 54 ++++++++++++++ src/ast/IntNode.java | 30 ++++++++ src/ast/IntType.java | 7 ++ src/ast/Node.java | 17 +++++ src/ast/Python3VisitorImpl.java | 67 ++++++++++++++++++ src/ast/RootNode.java | 44 ++++++++++++ src/ast/SimpleStmtsNode.java | 39 ++++++++++ src/ast/Type.java | 37 ++++++++++ src/ast/VoidType.java | 7 ++ src/semanticanalysis/STentry.java | 41 +++++++++++ src/semanticanalysis/SemanticError.java | 13 ++++ src/semanticanalysis/SymbolTable.java | 94 +++++++++++++++++++++++++ 18 files changed, 561 insertions(+), 31 deletions(-) create mode 100644 progs/test2.py create mode 100644 src/ast/BoolType.java create mode 100644 src/ast/CompoundNode.java create mode 100644 src/ast/ErrorType.java create mode 100644 src/ast/IfNode.java create mode 100644 src/ast/IntNode.java create mode 100644 src/ast/IntType.java create mode 100644 src/ast/Node.java create mode 100644 src/ast/Python3VisitorImpl.java create mode 100644 src/ast/RootNode.java create mode 100644 src/ast/SimpleStmtsNode.java create mode 100644 src/ast/Type.java create mode 100644 src/ast/VoidType.java create mode 100644 src/semanticanalysis/STentry.java create mode 100644 src/semanticanalysis/SemanticError.java create mode 100644 src/semanticanalysis/SymbolTable.java diff --git a/Makefile b/Makefile index 62edbaa..cbe2f35 100644 --- a/Makefile +++ b/Makefile @@ -5,7 +5,7 @@ SRC_DIR = src PARSER_DIR = src/parser BIN_DIR = out MAIN_CLASS = com.clp.project.Main -SOURCES = $(wildcard $(SRC_DIR)/*.java) +SOURCES = $(wildcard $(SRC_DIR)/*.java $(SRC_DIR)/*/*.java) GRAMMARS = $(PARSER_DIR)/Python3Lexer.g4 $(PARSER_DIR)/Python3Parser.g4 ANTLR_OUTPUT = $(PARSER_DIR)/*.java DATE = $(shell date +%Y%m%d-%H%M%S) @@ -17,6 +17,9 @@ all: $(SOURCES) $(ANTLR_OUTPUT) $(ANTLR_OUTPUT): $(GRAMMARS) java -jar lib/antlr-4.13.1-complete.jar $^ +build: + $(JAVAC) $(JAVAC_FLAGS) $(SOURCES) + run: java -cp $(ANTLR_COMPLETE):$(BIN_DIR) $(MAIN_CLASS) $(ARGS) @@ -29,4 +32,4 @@ clean: release: clean zip -r ../python3-miniparser-$(DATE).zip . -.PHONY: all run clean release runall +.PHONY: all build run clean release runall diff --git a/progs/test2.py b/progs/test2.py new file mode 100644 index 0000000..e39ca98 --- /dev/null +++ b/progs/test2.py @@ -0,0 +1,5 @@ +x = 1 +if x == 1: + print("a") +else: + print("b") diff --git a/src/Main.java b/src/Main.java index e773b07..e8802db 100644 --- a/src/Main.java +++ b/src/Main.java @@ -4,6 +4,7 @@ import java.io.File; import java.io.FileReader; import java.io.IOException; +import java.util.ArrayList; import java.util.Arrays; import java.util.Objects; import javax.swing.*; @@ -11,41 +12,60 @@ import org.antlr.v4.runtime.*; import org.antlr.v4.runtime.tree.*; +import com.clp.project.ast.*; import com.clp.project.parser.*; +import com.clp.project.semanticanalysis.*; public class Main { public static void main(String[] args) { - for (File file : Objects.requireNonNull(new File("./progs/").listFiles())) { - try { - String fileStr = file.getPath(); - // FIXME: use the fileStr above - fileStr = "./progs/test.py"; - System.out.println(fileStr); - - System.out.println(readFile(fileStr)); - - CharStream cs = CharStreams.fromFileName(fileStr); - Python3Lexer lexer = new Python3Lexer(cs); - CommonTokenStream tokenStream = new CommonTokenStream(lexer); - Python3Parser parser = new Python3Parser(tokenStream); - Python3Parser.RootContext tree = parser.root(); - // String treeStr = tree.toString(); - // System.out.println(treeStr); - // Visualize the parse tree - JFrame frame = new JFrame("Parse Tree"); - JPanel panel = new JPanel(); - TreeViewer viewer = new TreeViewer(Arrays.asList(parser.getRuleNames()), tree); - viewer.setScale(1.5); // Zoom factor - panel.add(viewer); - frame.add(panel); - frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); - frame.setSize(800, 600); - frame.setVisible(true); - break; - } catch (Exception e) { - e.printStackTrace(); + // for (File file : Objects.requireNonNull(new File("./progs/").listFiles())) { + try { + // String fileStr = file.getPath(); + // FIXME: use the fileStr above + String fileStr = "./progs/test2.py"; + System.out.println(fileStr); + + System.out.println(readFile(fileStr)); + + CharStream cs = CharStreams.fromFileName(fileStr); + Python3Lexer lexer = new Python3Lexer(cs); + CommonTokenStream tokenStream = new CommonTokenStream(lexer); + Python3Parser parser = new Python3Parser(tokenStream); + Python3Parser.RootContext tree = parser.root(); + + if (parser.getNumberOfSyntaxErrors() > 0) { + System.err.println("Error on program parsing."); + return; } + + Python3VisitorImpl visitor = new Python3VisitorImpl(); + Node ast = visitor.visit(tree); + + SymbolTable ST = new SymbolTable(); + ArrayList errors = ast.checkSemantics(ST, 0); + if (errors.size() > 0) { + System.out.println("You had: " + errors.size() + " errors:"); + for (SemanticError e : errors) + System.out.println("\t" + e); + } else { + System.out.println("Visualizing AST..."); + System.out.println(ast.toPrint("")); + } + + JFrame frame = new JFrame("Parse Tree"); + JPanel panel = new JPanel(); + TreeViewer viewer = new TreeViewer(Arrays.asList(parser.getRuleNames()), + tree); + viewer.setScale(1.5); // Zoom factor + panel.add(viewer); + frame.add(panel); + frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + frame.setSize(800, 600); + frame.setVisible(true); + } catch (Exception e) { + e.printStackTrace(); } + // } } private static String readFile(String filePath) throws IOException { diff --git a/src/ast/BoolType.java b/src/ast/BoolType.java new file mode 100644 index 0000000..3c5a57a --- /dev/null +++ b/src/ast/BoolType.java @@ -0,0 +1,8 @@ +package com.clp.project.ast; + +public class BoolType extends Type { + public String toPrint(String s) { + return s + "Bool "; + } + +} diff --git a/src/ast/CompoundNode.java b/src/ast/CompoundNode.java new file mode 100644 index 0000000..410d284 --- /dev/null +++ b/src/ast/CompoundNode.java @@ -0,0 +1,37 @@ +package com.clp.project.ast; + +import java.util.ArrayList; + +import com.clp.project.semanticanalysis.SemanticError; +import com.clp.project.semanticanalysis.SymbolTable; + +public class CompoundNode implements Node { + private Node _if; + + public CompoundNode(Node _if) { + this._if = _if; + } + + @Override + public ArrayList checkSemantics(SymbolTable ST, int _nesting) { + ArrayList errors = new ArrayList(); + + errors.addAll(_if.checkSemantics(ST, _nesting)); + + return errors; + } + + public Type typeCheck() { + return new VoidType(); + } + + public String codeGeneration() { + return ""; + } + + public String toPrint(String s) { + // FIXME: fix + return ""; + } + +} diff --git a/src/ast/ErrorType.java b/src/ast/ErrorType.java new file mode 100644 index 0000000..03a1b0e --- /dev/null +++ b/src/ast/ErrorType.java @@ -0,0 +1,7 @@ +package com.clp.project.ast; + +public class ErrorType extends Type { + public String toPrint(String s) { + return s + "Error\n"; + } +} diff --git a/src/ast/IfNode.java b/src/ast/IfNode.java new file mode 100644 index 0000000..ae8805e --- /dev/null +++ b/src/ast/IfNode.java @@ -0,0 +1,54 @@ +package com.clp.project.ast; + +import java.util.ArrayList; + +import com.clp.project.semanticanalysis.SemanticError; +import com.clp.project.semanticanalysis.SymbolTable; + +public class IfNode implements Node { + private Node guard; + private Node thenbranch; + private Node elsebranch; + + public IfNode(Node _guard, Node _thenbranch, Node _elsebranch) { + guard = _guard; + thenbranch = _thenbranch; + elsebranch = _elsebranch; + } + + @Override + public ArrayList checkSemantics(SymbolTable ST, int _nesting) { + ArrayList errors = new ArrayList(); + + errors.addAll(guard.checkSemantics(ST, _nesting)); + errors.addAll(thenbranch.checkSemantics(ST, _nesting)); + errors.addAll(elsebranch.checkSemantics(ST, _nesting)); + + return errors; + } + + public Type typeCheck() { + if (guard.typeCheck() instanceof BoolType) { + Type thenexp = thenbranch.typeCheck(); + Type elseexp = elsebranch.typeCheck(); + if (thenexp.getClass().equals(elseexp.getClass())) + return thenexp; + else { + System.out.println("Type Error: incompatible types in then and else branches"); + return new ErrorType(); + } + } else { + System.out.println("Type Error: non boolean condition in if"); + return new ErrorType(); + } + } + + public String codeGeneration() { + return ""; + } + + public String toPrint(String s) { + return s + "If\n" + guard.toPrint(s + " ") + thenbranch.toPrint(s + " ") + elsebranch.toPrint(s + " "); + } + +} diff --git a/src/ast/IntNode.java b/src/ast/IntNode.java new file mode 100644 index 0000000..3626ca4 --- /dev/null +++ b/src/ast/IntNode.java @@ -0,0 +1,30 @@ +package com.clp.project.ast; + +import java.util.ArrayList; + +import com.clp.project.semanticanalysis.SemanticError; +import com.clp.project.semanticanalysis.SymbolTable; + +public class IntNode implements Node { + private Integer val; + + public IntNode(Integer _val) { + val = _val; + } + + public ArrayList checkSemantics(SymbolTable ST, int _nesting) { + return new ArrayList(); + } + + public Type typeCheck() { + return new IntType(); + } + + public String codeGeneration() { + return ""; + } + + public String toPrint(String s) { + return s + Integer.toString(val) + "\n"; + } +} diff --git a/src/ast/IntType.java b/src/ast/IntType.java new file mode 100644 index 0000000..aaca28a --- /dev/null +++ b/src/ast/IntType.java @@ -0,0 +1,7 @@ +package com.clp.project.ast; + +public class IntType extends Type { + public String toPrint(String s) { + return s + "Int "; + } +} diff --git a/src/ast/Node.java b/src/ast/Node.java new file mode 100644 index 0000000..b002575 --- /dev/null +++ b/src/ast/Node.java @@ -0,0 +1,17 @@ +package com.clp.project.ast; + +import java.util.ArrayList; + +import com.clp.project.semanticanalysis.SemanticError; +import com.clp.project.semanticanalysis.SymbolTable; + +public interface Node { + + ArrayList checkSemantics(SymbolTable ST, int _nesting); + + Type typeCheck(); + + String codeGeneration(); + + String toPrint(String s); +} diff --git a/src/ast/Python3VisitorImpl.java b/src/ast/Python3VisitorImpl.java new file mode 100644 index 0000000..bb9e9e2 --- /dev/null +++ b/src/ast/Python3VisitorImpl.java @@ -0,0 +1,67 @@ +package com.clp.project.ast; + +import java.util.ArrayList; +import java.util.List; + +import com.clp.project.parser.Python3ParserBaseVisitor; +import com.clp.project.parser.Python3Parser.*; + +public class Python3VisitorImpl extends Python3ParserBaseVisitor { + public Node visitRoot(RootContext ctx) { + ArrayList stmts = new ArrayList(); + ArrayList compStmts = new ArrayList(); + + for (Simple_stmtsContext s : ctx.simple_stmts()) { + stmts.add(visit(s)); + } + for (Compound_stmtContext s : ctx.compound_stmt()) { + compStmts.add(visit(s)); + } + + return new RootNode(stmts, compStmts); + } + + public Node visitSimple_stmts(Simple_stmtsContext ctx) { + ArrayList stmts = new ArrayList(); + + for (Simple_stmtContext stm : ctx.simple_stmt()) { + stmts.add(visit(stm)); + } + + return new SimpleStmtsNode(stmts); + } + + public Node visitSimple_stmt(Simple_stmtContext ctx) { + Node exp = visit(ctx.expr()); + + return exp; + } + + public Node visitCompound_stmt(Compound_stmtContext ctx) { + Node ifStmt = visit(ctx.if_stmt()); + // FIXME: adds below + // Node whileStmt = visit(ctx.while_stmt()); + // Node forStmt = visit(ctx.for_stmt()); + // Node funcDef = visit(ctx.funcdef()); + // + return new CompoundNode(ifStmt); + } + + public Node visitAssignment(AssignmentContext ctx) { + Node exp = visit(ctx.exprlist(1)); + return exp; + } + + // FIXME: add support for `elif` + public Node visitIf_stmt(If_stmtContext ctx) { + var blocks = ctx.block(); + Node condExp = visit(ctx.expr(0)); + Node thenExp = visit(blocks.get(0)); + Node elseExp = null; + if (blocks.size() > 1) { + elseExp = visit(blocks.get(1)); + } + + return new IfNode(condExp, thenExp, elseExp); + } +} diff --git a/src/ast/RootNode.java b/src/ast/RootNode.java new file mode 100644 index 0000000..75ddae3 --- /dev/null +++ b/src/ast/RootNode.java @@ -0,0 +1,44 @@ +package com.clp.project.ast; + +import java.util.ArrayList; + +import com.clp.project.semanticanalysis.SemanticError; +import com.clp.project.semanticanalysis.SymbolTable; + +public class RootNode implements Node { + private ArrayList stmts; + private ArrayList compoundStmts; + + public RootNode(ArrayList _stmts, ArrayList _compoundStmts) { + stmts = _stmts; + compoundStmts = _compoundStmts; + } + + @Override + public ArrayList checkSemantics(SymbolTable ST, int _nesting) { + ArrayList errors = new ArrayList(); + + for (Node stmt : stmts) { + errors.addAll(stmt.checkSemantics(ST, _nesting)); + } + for (Node stmt : compoundStmts) { + errors.addAll(stmt.checkSemantics(ST, _nesting)); + } + + return errors; + } + + public Type typeCheck() { + return new VoidType(); + } + + public String codeGeneration() { + return ""; + } + + public String toPrint(String s) { + // FIXME: fix + return ""; + } + +} diff --git a/src/ast/SimpleStmtsNode.java b/src/ast/SimpleStmtsNode.java new file mode 100644 index 0000000..1af62e7 --- /dev/null +++ b/src/ast/SimpleStmtsNode.java @@ -0,0 +1,39 @@ +package com.clp.project.ast; + +import java.util.ArrayList; + +import com.clp.project.semanticanalysis.SemanticError; +import com.clp.project.semanticanalysis.SymbolTable; + +public class SimpleStmtsNode implements Node { + private ArrayList stmts; + + public SimpleStmtsNode(ArrayList _stmts) { + stmts = _stmts; + } + + @Override + public ArrayList checkSemantics(SymbolTable ST, int _nesting) { + ArrayList errors = new ArrayList(); + + for (Node stmt : stmts) { + errors.addAll(stmt.checkSemantics(ST, _nesting)); + } + + return errors; + } + + public Type typeCheck() { + return new VoidType(); + } + + public String codeGeneration() { + return ""; + } + + public String toPrint(String s) { + // FIXME: fix + return ""; + } + +} diff --git a/src/ast/Type.java b/src/ast/Type.java new file mode 100644 index 0000000..b866dee --- /dev/null +++ b/src/ast/Type.java @@ -0,0 +1,37 @@ +package com.clp.project.ast; + +import java.util.ArrayList; + +import com.clp.project.semanticanalysis.SemanticError; +import com.clp.project.semanticanalysis.SymbolTable; + +public class Type implements Node { + public boolean isEqual(Type A, Type B) { + if (A.getClass().equals(B.getClass())) + return true; + else + return false; + } + + public String toPrint(String s) { + return s; + } + + public ArrayList checkSemantics(SymbolTable ST, int _nesting) { + // It is never invoked + return null; + } + + @Override + public Type typeCheck() { + // It is never invoked + return null; + } + + @Override + public String codeGeneration() { + // It is never invoked + return ""; + } + +} diff --git a/src/ast/VoidType.java b/src/ast/VoidType.java new file mode 100644 index 0000000..2726cc3 --- /dev/null +++ b/src/ast/VoidType.java @@ -0,0 +1,7 @@ +package com.clp.project.ast; + +public class VoidType extends Type { + public String toPrint(String s) { + return s + "Void "; + } +} diff --git a/src/semanticanalysis/STentry.java b/src/semanticanalysis/STentry.java new file mode 100644 index 0000000..b132096 --- /dev/null +++ b/src/semanticanalysis/STentry.java @@ -0,0 +1,41 @@ + +package com.clp.project.semanticanalysis; + +import com.clp.project.ast.Type; + +public class STentry { + private Type type; + private int offset; + private int nesting; + private String label; + + public STentry(Type _type, int _offset, int _nesting) { + type = _type; + offset = _offset; + nesting = _nesting; + } + + public STentry(Type _type, int _offset, int _nesting, String _label) { + type = _type; + offset = _offset; + nesting = _nesting; + label = _label; + } + + public Type gettype() { + return type; + } + + public int getoffset() { + return offset; + } + + public int getnesting() { + return nesting; + } + + public String getlabel() { + return label; + } + +} diff --git a/src/semanticanalysis/SemanticError.java b/src/semanticanalysis/SemanticError.java new file mode 100644 index 0000000..d2df7cf --- /dev/null +++ b/src/semanticanalysis/SemanticError.java @@ -0,0 +1,13 @@ +package com.clp.project.semanticanalysis; + +public class SemanticError { + private String msg; + + public SemanticError(String _msg) { + msg = _msg; + } + + public String toString() { + return msg; + } +} diff --git a/src/semanticanalysis/SymbolTable.java b/src/semanticanalysis/SymbolTable.java new file mode 100644 index 0000000..8e0e10f --- /dev/null +++ b/src/semanticanalysis/SymbolTable.java @@ -0,0 +1,94 @@ +package com.clp.project.semanticanalysis; + +import java.util.ArrayList; +import java.util.HashMap; +import com.clp.project.ast.*; + +public class SymbolTable { + private ArrayList> symbol_table; + private ArrayList offset; + + public SymbolTable() { + symbol_table = new ArrayList>(); + offset = new ArrayList(); + } + + public Integer nesting() { + return symbol_table.size() - 1; + } + + public STentry lookup(String id) { + int n = symbol_table.size() - 1; + boolean found = false; + STentry T = null; + while ((n >= 0) && !found) { + HashMap H = symbol_table.get(n); + T = H.get(id); + if (T != null) + found = true; + else + n = n - 1; + } + return T; + } + + public Integer nslookup(String id) { + int n = symbol_table.size() - 1; + boolean found = false; + while ((n >= 0) && !found) { + HashMap H = symbol_table.get(n); + if (H.get(id) != null) + found = true; + else + n = n - 1; + } + return n; + } + + public void add(HashMap H) { + symbol_table.add(H); + offset.add(1); // si inizia da 2 perche` prima ci sonop FP e AL + } + + public void remove() { + int x = symbol_table.size(); + symbol_table.remove(x - 1); + offset.remove(x - 1); + } + + public boolean top_lookup(String id) { + int n = symbol_table.size() - 1; + STentry T = null; + HashMap H = symbol_table.get(n); + T = H.get(id); + return (T != null); + } + + public void insert(String id, Type type, int _nesting, String _label) { + int n = symbol_table.size() - 1; + HashMap H = symbol_table.get(n); + symbol_table.remove(n); + int offs = offset.get(n); + offset.remove(n); + STentry idtype = new STentry(type, offs, _nesting, _label); + H.put(id, idtype); + symbol_table.add(H); + if (type.getClass().equals((new BoolType()).getClass())) + offs = offs + 1; // we always increment the offset by 1 otherwise we need ad-hoc + // bytecode operations + else if (type.getClass().equals((new IntType()).getClass())) + offs = offs + 1; + else + offs = offs + 1; + offset.add(offs); + } + + public void increaseoffset() { + int n = offset.size() - 1; + int offs = offset.get(n); + offset.remove(n); + offs = offs + 1; + offset.add(offs); + } + +} From c3dd4ef0dc0a22fd1db80c53b0eb1c0008addc9c Mon Sep 17 00:00:00 2001 From: Santo Cariotti Date: Tue, 4 Jun 2024 15:42:50 +0200 Subject: [PATCH 07/23] Fix generated comment --- src/parser/Python3Lexer.java | 8 +++++--- src/parser/Python3Parser.java | 2 +- src/parser/Python3ParserBaseListener.java | 2 +- src/parser/Python3ParserBaseVisitor.java | 2 +- src/parser/Python3ParserListener.java | 2 +- src/parser/Python3ParserVisitor.java | 2 +- 6 files changed, 10 insertions(+), 8 deletions(-) diff --git a/src/parser/Python3Lexer.java b/src/parser/Python3Lexer.java index 9a48207..ae33e3d 100644 --- a/src/parser/Python3Lexer.java +++ b/src/parser/Python3Lexer.java @@ -1,8 +1,10 @@ package com.clp.project.parser; -// import com.clp.project.parser.Python3LexerBase; - -// Generated from src/Python3Lexer.g4 by ANTLR 4.13.1 +// Generated from src/parser/Python3Lexer.g4 by ANTLR 4.13.1 +import org.antlr.v4.runtime.Lexer; +import org.antlr.v4.runtime.CharStream; +import org.antlr.v4.runtime.Token; +import org.antlr.v4.runtime.TokenStream; import org.antlr.v4.runtime.*; import org.antlr.v4.runtime.atn.*; import org.antlr.v4.runtime.dfa.DFA; diff --git a/src/parser/Python3Parser.java b/src/parser/Python3Parser.java index 4965501..a407482 100644 --- a/src/parser/Python3Parser.java +++ b/src/parser/Python3Parser.java @@ -1,6 +1,6 @@ package com.clp.project.parser; -// Generated from src/Python3Parser.g4 by ANTLR 4.13.1 +// Generated from src/parser/Python3Parser.g4 by ANTLR 4.13.1 import org.antlr.v4.runtime.atn.*; import org.antlr.v4.runtime.dfa.DFA; import org.antlr.v4.runtime.*; diff --git a/src/parser/Python3ParserBaseListener.java b/src/parser/Python3ParserBaseListener.java index 6fa323f..7191565 100644 --- a/src/parser/Python3ParserBaseListener.java +++ b/src/parser/Python3ParserBaseListener.java @@ -1,5 +1,5 @@ package com.clp.project.parser; -// Generated from src/Python3Parser.g4 by ANTLR 4.13.1 +// Generated from src/parser/Python3Parser.g4 by ANTLR 4.13.1 import org.antlr.v4.runtime.ParserRuleContext; import org.antlr.v4.runtime.tree.ErrorNode; diff --git a/src/parser/Python3ParserBaseVisitor.java b/src/parser/Python3ParserBaseVisitor.java index 4ad92a0..4fc355f 100644 --- a/src/parser/Python3ParserBaseVisitor.java +++ b/src/parser/Python3ParserBaseVisitor.java @@ -1,6 +1,6 @@ package com.clp.project.parser; -// Generated from src/Python3Parser.g4 by ANTLR 4.13.1 +// Generated from src/parser/Python3Parser.g4 by ANTLR 4.13.1 import org.antlr.v4.runtime.tree.AbstractParseTreeVisitor; /** diff --git a/src/parser/Python3ParserListener.java b/src/parser/Python3ParserListener.java index 248105d..f71a32d 100644 --- a/src/parser/Python3ParserListener.java +++ b/src/parser/Python3ParserListener.java @@ -1,6 +1,6 @@ package com.clp.project.parser; -// Generated from src/Python3Parser.g4 by ANTLR 4.13.1 +// Generated from src/parser/Python3Parser.g4 by ANTLR 4.13.1 import org.antlr.v4.runtime.tree.ParseTreeListener; /** diff --git a/src/parser/Python3ParserVisitor.java b/src/parser/Python3ParserVisitor.java index b286b61..88607ed 100644 --- a/src/parser/Python3ParserVisitor.java +++ b/src/parser/Python3ParserVisitor.java @@ -1,6 +1,6 @@ package com.clp.project.parser; -// Generated from src/Python3Parser.g4 by ANTLR 4.13.1 +// Generated from src/parser/Python3Parser.g4 by ANTLR 4.13.1 import org.antlr.v4.runtime.tree.ParseTreeVisitor; /** From 289494c27dbca156e2c79689ae6771ab1fe26988 Mon Sep 17 00:00:00 2001 From: Santo Cariotti Date: Tue, 4 Jun 2024 16:16:34 +0200 Subject: [PATCH 08/23] wip --- progs/test2.py | 4 ---- src/Main.java | 21 ++++++++++++++++++--- 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/progs/test2.py b/progs/test2.py index e39ca98..7d4290a 100644 --- a/progs/test2.py +++ b/progs/test2.py @@ -1,5 +1 @@ x = 1 -if x == 1: - print("a") -else: - print("b") diff --git a/src/Main.java b/src/Main.java index e8802db..21122f0 100644 --- a/src/Main.java +++ b/src/Main.java @@ -29,19 +29,34 @@ public static void main(String[] args) { CharStream cs = CharStreams.fromFileName(fileStr); Python3Lexer lexer = new Python3Lexer(cs); - CommonTokenStream tokenStream = new CommonTokenStream(lexer); - Python3Parser parser = new Python3Parser(tokenStream); + CommonTokenStream tokens = new CommonTokenStream(lexer); + Python3Parser parser = new Python3Parser(tokens); Python3Parser.RootContext tree = parser.root(); + // DEBUG + { + tokens.fill(); + for (Token token : tokens.getTokens()) { + System.out.println(token.toString()); + } + + System.out.println("Tree: " + tree); + } + + if (tree == null) { + System.err.println("The tree is null."); + return; + } + if (parser.getNumberOfSyntaxErrors() > 0) { System.err.println("Error on program parsing."); return; } Python3VisitorImpl visitor = new Python3VisitorImpl(); + SymbolTable ST = new SymbolTable(); Node ast = visitor.visit(tree); - SymbolTable ST = new SymbolTable(); ArrayList errors = ast.checkSemantics(ST, 0); if (errors.size() > 0) { System.out.println("You had: " + errors.size() + " errors:"); From e1729ee691743c535804e1c59f57aebbfb75c186 Mon Sep 17 00:00:00 2001 From: Santo Cariotti Date: Tue, 4 Jun 2024 18:53:38 +0200 Subject: [PATCH 09/23] wip --- src/Main.java | 36 ++++++++++---------- src/ast/Python3VisitorImpl.java | 39 +++++++++++++++++++-- src/ast/nodes/AssignmentNode.java | 43 ++++++++++++++++++++++++ src/ast/nodes/AtomNode.java | 33 ++++++++++++++++++ src/ast/nodes/AugassignNode.java | 32 ++++++++++++++++++ src/ast/{ => nodes}/CompoundNode.java | 3 +- src/ast/{ => nodes}/IfNode.java | 3 +- src/ast/{ => nodes}/IntNode.java | 3 +- src/ast/{ => nodes}/Node.java | 3 +- src/ast/{ => nodes}/RootNode.java | 15 +++++++-- src/ast/nodes/SimpleStmtNode.java | 37 ++++++++++++++++++++ src/ast/{ => nodes}/SimpleStmtsNode.java | 11 ++++-- src/ast/types/AtomType.java | 7 ++++ src/ast/{ => types}/BoolType.java | 2 +- src/ast/{ => types}/ErrorType.java | 2 +- src/ast/{ => types}/IntType.java | 2 +- src/ast/{ => types}/Type.java | 3 +- src/ast/{ => types}/VoidType.java | 2 +- src/semanticanalysis/STentry.java | 2 +- src/semanticanalysis/SymbolTable.java | 1 + 20 files changed, 243 insertions(+), 36 deletions(-) create mode 100644 src/ast/nodes/AssignmentNode.java create mode 100644 src/ast/nodes/AtomNode.java create mode 100644 src/ast/nodes/AugassignNode.java rename src/ast/{ => nodes}/CompoundNode.java (91%) rename src/ast/{ => nodes}/IfNode.java (95%) rename src/ast/{ => nodes}/IntNode.java (89%) rename src/ast/{ => nodes}/Node.java (82%) rename src/ast/{ => nodes}/RootNode.java (76%) create mode 100644 src/ast/nodes/SimpleStmtNode.java rename src/ast/{ => nodes}/SimpleStmtsNode.java (77%) create mode 100644 src/ast/types/AtomType.java rename src/ast/{ => types}/BoolType.java (76%) rename src/ast/{ => types}/ErrorType.java (76%) rename src/ast/{ => types}/IntType.java (75%) rename src/ast/{ => types}/Type.java (91%) rename src/ast/{ => types}/VoidType.java (76%) diff --git a/src/Main.java b/src/Main.java index 21122f0..9b403e8 100644 --- a/src/Main.java +++ b/src/Main.java @@ -13,6 +13,7 @@ import org.antlr.v4.runtime.tree.*; import com.clp.project.ast.*; +import com.clp.project.ast.nodes.*; import com.clp.project.parser.*; import com.clp.project.semanticanalysis.*; @@ -34,14 +35,25 @@ public static void main(String[] args) { Python3Parser.RootContext tree = parser.root(); // DEBUG - { - tokens.fill(); - for (Token token : tokens.getTokens()) { - System.out.println(token.toString()); - } + // { + // tokens.fill(); + // for (Token token : tokens.getTokens()) { + // System.out.println(token.toString()); + // } + // + // System.out.println("Tree: " + tree); + // } - System.out.println("Tree: " + tree); - } + JFrame frame = new JFrame("Parse Tree"); + JPanel panel = new JPanel(); + TreeViewer viewer = new TreeViewer(Arrays.asList(parser.getRuleNames()), + tree); + viewer.setScale(1.5); // Zoom factor + panel.add(viewer); + frame.add(panel); + frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + frame.setSize(800, 600); + frame.setVisible(true); if (tree == null) { System.err.println("The tree is null."); @@ -67,16 +79,6 @@ public static void main(String[] args) { System.out.println(ast.toPrint("")); } - JFrame frame = new JFrame("Parse Tree"); - JPanel panel = new JPanel(); - TreeViewer viewer = new TreeViewer(Arrays.asList(parser.getRuleNames()), - tree); - viewer.setScale(1.5); // Zoom factor - panel.add(viewer); - frame.add(panel); - frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); - frame.setSize(800, 600); - frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } diff --git a/src/ast/Python3VisitorImpl.java b/src/ast/Python3VisitorImpl.java index bb9e9e2..2c72c7f 100644 --- a/src/ast/Python3VisitorImpl.java +++ b/src/ast/Python3VisitorImpl.java @@ -3,6 +3,9 @@ import java.util.ArrayList; import java.util.List; +import com.clp.project.ast.*; +import com.clp.project.ast.nodes.*; +import com.clp.project.ast.types.*; import com.clp.project.parser.Python3ParserBaseVisitor; import com.clp.project.parser.Python3Parser.*; @@ -32,9 +35,9 @@ public Node visitSimple_stmts(Simple_stmtsContext ctx) { } public Node visitSimple_stmt(Simple_stmtContext ctx) { - Node exp = visit(ctx.expr()); + Node exp = visit(ctx.assignment()); - return exp; + return new SimpleStmtNode(exp); } public Node visitCompound_stmt(Compound_stmtContext ctx) { @@ -48,10 +51,40 @@ public Node visitCompound_stmt(Compound_stmtContext ctx) { } public Node visitAssignment(AssignmentContext ctx) { - Node exp = visit(ctx.exprlist(1)); + Node lhr = visit(ctx.exprlist(0)); + Node assign = visit(ctx.augassign()); + Node rhr = visit(ctx.exprlist(1)); + + return new AssignmentNode(lhr, assign, rhr); + } + + public Node visitExprlist(ExprlistContext ctx) { + Node exp = visit(ctx.expr(0)); + return exp; } + public Node visitExpr(ExprContext ctx) { + Node atom = visit(ctx.atom()); + + return atom; + } + + public Node visitAtom(AtomContext ctx) { + if (ctx.NUMBER() != null) { + return new AtomNode(ctx.NUMBER()); + } else if (ctx.TRUE() != null) { + return new AtomNode(ctx.TRUE()); + } else if (ctx.FALSE() != null) { + return new AtomNode(ctx.FALSE()); + } + return new AtomNode(ctx.NAME()); + } + + public Node visitAugassign(AugassignContext ctx) { + return new AugassignNode(ctx.ASSIGN()); + } + // FIXME: add support for `elif` public Node visitIf_stmt(If_stmtContext ctx) { var blocks = ctx.block(); diff --git a/src/ast/nodes/AssignmentNode.java b/src/ast/nodes/AssignmentNode.java new file mode 100644 index 0000000..5439209 --- /dev/null +++ b/src/ast/nodes/AssignmentNode.java @@ -0,0 +1,43 @@ +package com.clp.project.ast.nodes; + +import java.util.ArrayList; + +import com.clp.project.semanticanalysis.SemanticError; +import com.clp.project.semanticanalysis.SymbolTable; +import com.clp.project.ast.types.*; + +public class AssignmentNode implements Node { + private Node lhr; + private Node assign; + private Node rhr; + + public AssignmentNode(Node _lhr, Node _assign, Node _rhr) { + lhr = _lhr; + assign = _assign; + rhr = _rhr; + } + + @Override + public ArrayList checkSemantics(SymbolTable ST, int _nesting) { + ArrayList errors = new ArrayList(); + + errors.addAll(lhr.checkSemantics(ST, _nesting)); + errors.addAll(assign.checkSemantics(ST, _nesting)); + errors.addAll(rhr.checkSemantics(ST, _nesting)); + + return errors; + } + + public Type typeCheck() { + return new IntType(); + } + + public String codeGeneration() { + return ""; + } + + public String toPrint(String s) { + return s + "Assignment\n" + lhr.toPrint(s + " ") + assign.toPrint(s + " ") + rhr.toPrint(s + " "); + } + +} diff --git a/src/ast/nodes/AtomNode.java b/src/ast/nodes/AtomNode.java new file mode 100644 index 0000000..bdbc323 --- /dev/null +++ b/src/ast/nodes/AtomNode.java @@ -0,0 +1,33 @@ +package com.clp.project.ast.nodes; + +import java.util.ArrayList; + +import com.clp.project.semanticanalysis.SemanticError; +import com.clp.project.semanticanalysis.SymbolTable; +import com.clp.project.ast.types.*; +import org.antlr.v4.runtime.tree.TerminalNode; + +public class AtomNode implements Node { + private TerminalNode val; + + public AtomNode(TerminalNode _val) { + val = _val; + } + + public ArrayList checkSemantics(SymbolTable ST, int _nesting) { + return new ArrayList(); + } + + public Type typeCheck() { + return new AtomType(); + } + + public String codeGeneration() { + return ""; + } + + public String toPrint(String s) { + return s + "Atom(" + val.toString() + ")"; + + } +} diff --git a/src/ast/nodes/AugassignNode.java b/src/ast/nodes/AugassignNode.java new file mode 100644 index 0000000..d7f4fa2 --- /dev/null +++ b/src/ast/nodes/AugassignNode.java @@ -0,0 +1,32 @@ +package com.clp.project.ast.nodes; + +import java.util.ArrayList; + +import com.clp.project.semanticanalysis.SemanticError; +import com.clp.project.semanticanalysis.SymbolTable; +import com.clp.project.ast.types.*; +import org.antlr.v4.runtime.tree.TerminalNode; + +public class AugassignNode implements Node { + private TerminalNode val; + + public AugassignNode(TerminalNode _val) { + val = _val; + } + + public ArrayList checkSemantics(SymbolTable ST, int _nesting) { + return new ArrayList(); + } + + public Type typeCheck() { + return new ErrorType(); + } + + public String codeGeneration() { + return ""; + } + + public String toPrint(String s) { + return s + "Augassign(" + val + ")"; + } +} diff --git a/src/ast/CompoundNode.java b/src/ast/nodes/CompoundNode.java similarity index 91% rename from src/ast/CompoundNode.java rename to src/ast/nodes/CompoundNode.java index 410d284..6d227ec 100644 --- a/src/ast/CompoundNode.java +++ b/src/ast/nodes/CompoundNode.java @@ -1,9 +1,10 @@ -package com.clp.project.ast; +package com.clp.project.ast.nodes; import java.util.ArrayList; import com.clp.project.semanticanalysis.SemanticError; import com.clp.project.semanticanalysis.SymbolTable; +import com.clp.project.ast.types.*; public class CompoundNode implements Node { private Node _if; diff --git a/src/ast/IfNode.java b/src/ast/nodes/IfNode.java similarity index 95% rename from src/ast/IfNode.java rename to src/ast/nodes/IfNode.java index ae8805e..af899a1 100644 --- a/src/ast/IfNode.java +++ b/src/ast/nodes/IfNode.java @@ -1,9 +1,10 @@ -package com.clp.project.ast; +package com.clp.project.ast.nodes; import java.util.ArrayList; import com.clp.project.semanticanalysis.SemanticError; import com.clp.project.semanticanalysis.SymbolTable; +import com.clp.project.ast.types.*; public class IfNode implements Node { private Node guard; diff --git a/src/ast/IntNode.java b/src/ast/nodes/IntNode.java similarity index 89% rename from src/ast/IntNode.java rename to src/ast/nodes/IntNode.java index 3626ca4..75558c7 100644 --- a/src/ast/IntNode.java +++ b/src/ast/nodes/IntNode.java @@ -1,9 +1,10 @@ -package com.clp.project.ast; +package com.clp.project.ast.nodes; import java.util.ArrayList; import com.clp.project.semanticanalysis.SemanticError; import com.clp.project.semanticanalysis.SymbolTable; +import com.clp.project.ast.types.*; public class IntNode implements Node { private Integer val; diff --git a/src/ast/Node.java b/src/ast/nodes/Node.java similarity index 82% rename from src/ast/Node.java rename to src/ast/nodes/Node.java index b002575..58c7ea2 100644 --- a/src/ast/Node.java +++ b/src/ast/nodes/Node.java @@ -1,9 +1,10 @@ -package com.clp.project.ast; +package com.clp.project.ast.nodes; import java.util.ArrayList; import com.clp.project.semanticanalysis.SemanticError; import com.clp.project.semanticanalysis.SymbolTable; +import com.clp.project.ast.types.*; public interface Node { diff --git a/src/ast/RootNode.java b/src/ast/nodes/RootNode.java similarity index 76% rename from src/ast/RootNode.java rename to src/ast/nodes/RootNode.java index 75ddae3..58d0e73 100644 --- a/src/ast/RootNode.java +++ b/src/ast/nodes/RootNode.java @@ -1,9 +1,10 @@ -package com.clp.project.ast; +package com.clp.project.ast.nodes; import java.util.ArrayList; import com.clp.project.semanticanalysis.SemanticError; import com.clp.project.semanticanalysis.SymbolTable; +import com.clp.project.ast.types.*; public class RootNode implements Node { private ArrayList stmts; @@ -37,8 +38,16 @@ public String codeGeneration() { } public String toPrint(String s) { - // FIXME: fix - return ""; + String result = "Root\n"; + + for (Node stmt : stmts) { + result += stmt.toPrint(" "); + } + for (Node stmt : compoundStmts) { + result += stmt.toPrint(" "); + } + + return result; } } diff --git a/src/ast/nodes/SimpleStmtNode.java b/src/ast/nodes/SimpleStmtNode.java new file mode 100644 index 0000000..82c685c --- /dev/null +++ b/src/ast/nodes/SimpleStmtNode.java @@ -0,0 +1,37 @@ +package com.clp.project.ast.nodes; + +import java.util.ArrayList; + +import com.clp.project.semanticanalysis.SemanticError; +import com.clp.project.semanticanalysis.SymbolTable; +import com.clp.project.ast.types.*; + +public class SimpleStmtNode implements Node { + private Node stmt; + + public SimpleStmtNode(Node _stmt) { + stmt = _stmt; + } + + @Override + public ArrayList checkSemantics(SymbolTable ST, int _nesting) { + ArrayList errors = new ArrayList(); + + errors.addAll(stmt.checkSemantics(ST, _nesting)); + + return errors; + } + + public Type typeCheck() { + return new VoidType(); + } + + public String codeGeneration() { + return ""; + } + + public String toPrint(String s) { + return s + "SimpleStmt\n" + stmt.toPrint(s + " "); + } + +} diff --git a/src/ast/SimpleStmtsNode.java b/src/ast/nodes/SimpleStmtsNode.java similarity index 77% rename from src/ast/SimpleStmtsNode.java rename to src/ast/nodes/SimpleStmtsNode.java index 1af62e7..d2e3fde 100644 --- a/src/ast/SimpleStmtsNode.java +++ b/src/ast/nodes/SimpleStmtsNode.java @@ -1,9 +1,10 @@ -package com.clp.project.ast; +package com.clp.project.ast.nodes; import java.util.ArrayList; import com.clp.project.semanticanalysis.SemanticError; import com.clp.project.semanticanalysis.SymbolTable; +import com.clp.project.ast.types.*; public class SimpleStmtsNode implements Node { private ArrayList stmts; @@ -32,8 +33,12 @@ public String codeGeneration() { } public String toPrint(String s) { - // FIXME: fix - return ""; + String result = s + "SimpleStmts\n"; + for (Node stmt : stmts) { + result += stmt.toPrint(s + " "); + } + + return result; } } diff --git a/src/ast/types/AtomType.java b/src/ast/types/AtomType.java new file mode 100644 index 0000000..a32953a --- /dev/null +++ b/src/ast/types/AtomType.java @@ -0,0 +1,7 @@ +package com.clp.project.ast.types; + +public class AtomType extends Type { + public String toPrint(String s) { + return s + "Atom "; + } +} diff --git a/src/ast/BoolType.java b/src/ast/types/BoolType.java similarity index 76% rename from src/ast/BoolType.java rename to src/ast/types/BoolType.java index 3c5a57a..d6d9731 100644 --- a/src/ast/BoolType.java +++ b/src/ast/types/BoolType.java @@ -1,4 +1,4 @@ -package com.clp.project.ast; +package com.clp.project.ast.types; public class BoolType extends Type { public String toPrint(String s) { diff --git a/src/ast/ErrorType.java b/src/ast/types/ErrorType.java similarity index 76% rename from src/ast/ErrorType.java rename to src/ast/types/ErrorType.java index 03a1b0e..e4a54d3 100644 --- a/src/ast/ErrorType.java +++ b/src/ast/types/ErrorType.java @@ -1,4 +1,4 @@ -package com.clp.project.ast; +package com.clp.project.ast.types; public class ErrorType extends Type { public String toPrint(String s) { diff --git a/src/ast/IntType.java b/src/ast/types/IntType.java similarity index 75% rename from src/ast/IntType.java rename to src/ast/types/IntType.java index aaca28a..a953d1f 100644 --- a/src/ast/IntType.java +++ b/src/ast/types/IntType.java @@ -1,4 +1,4 @@ -package com.clp.project.ast; +package com.clp.project.ast.types; public class IntType extends Type { public String toPrint(String s) { diff --git a/src/ast/Type.java b/src/ast/types/Type.java similarity index 91% rename from src/ast/Type.java rename to src/ast/types/Type.java index b866dee..267a244 100644 --- a/src/ast/Type.java +++ b/src/ast/types/Type.java @@ -1,9 +1,10 @@ -package com.clp.project.ast; +package com.clp.project.ast.types; import java.util.ArrayList; import com.clp.project.semanticanalysis.SemanticError; import com.clp.project.semanticanalysis.SymbolTable; +import com.clp.project.ast.nodes.*; public class Type implements Node { public boolean isEqual(Type A, Type B) { diff --git a/src/ast/VoidType.java b/src/ast/types/VoidType.java similarity index 76% rename from src/ast/VoidType.java rename to src/ast/types/VoidType.java index 2726cc3..47cb6bc 100644 --- a/src/ast/VoidType.java +++ b/src/ast/types/VoidType.java @@ -1,4 +1,4 @@ -package com.clp.project.ast; +package com.clp.project.ast.types; public class VoidType extends Type { public String toPrint(String s) { diff --git a/src/semanticanalysis/STentry.java b/src/semanticanalysis/STentry.java index b132096..39279e3 100644 --- a/src/semanticanalysis/STentry.java +++ b/src/semanticanalysis/STentry.java @@ -1,7 +1,7 @@ package com.clp.project.semanticanalysis; -import com.clp.project.ast.Type; +import com.clp.project.ast.types.Type; public class STentry { private Type type; diff --git a/src/semanticanalysis/SymbolTable.java b/src/semanticanalysis/SymbolTable.java index 8e0e10f..6ba711b 100644 --- a/src/semanticanalysis/SymbolTable.java +++ b/src/semanticanalysis/SymbolTable.java @@ -3,6 +3,7 @@ import java.util.ArrayList; import java.util.HashMap; import com.clp.project.ast.*; +import com.clp.project.ast.types.*; public class SymbolTable { private ArrayList> symbol_table; From a9b6f1b169a73cc84934af47b96bb9c0401b2ae1 Mon Sep 17 00:00:00 2001 From: Santo Cariotti Date: Tue, 4 Jun 2024 18:54:34 +0200 Subject: [PATCH 10/23] fix makefile --- Makefile | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Makefile b/Makefile index cbe2f35..d04c40e 100644 --- a/Makefile +++ b/Makefile @@ -5,7 +5,8 @@ SRC_DIR = src PARSER_DIR = src/parser BIN_DIR = out MAIN_CLASS = com.clp.project.Main -SOURCES = $(wildcard $(SRC_DIR)/*.java $(SRC_DIR)/*/*.java) +PARSEALL_CLASS = com.clp.project.ParseAll +SOURCES = $(wildcard $(SRC_DIR)/*.java $(SRC_DIR)/*/*.java $(SRC_DIR)/*/*/*.java) GRAMMARS = $(PARSER_DIR)/Python3Lexer.g4 $(PARSER_DIR)/Python3Parser.g4 ANTLR_OUTPUT = $(PARSER_DIR)/*.java DATE = $(shell date +%Y%m%d-%H%M%S) From c49ce12c4ca3b06bf2f8824adb2c84f64c83125d Mon Sep 17 00:00:00 2001 From: Santo Cariotti Date: Tue, 4 Jun 2024 19:05:07 +0200 Subject: [PATCH 11/23] wip --- src/ast/nodes/CompoundNode.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/ast/nodes/CompoundNode.java b/src/ast/nodes/CompoundNode.java index 6d227ec..ecff0c1 100644 --- a/src/ast/nodes/CompoundNode.java +++ b/src/ast/nodes/CompoundNode.java @@ -31,8 +31,7 @@ public String codeGeneration() { } public String toPrint(String s) { - // FIXME: fix - return ""; + return s + "CompoundNode\n" + _if.toPrint(s + " "); } } From a3b8e7dc4710268807fbc153a38ffaab31e484b3 Mon Sep 17 00:00:00 2001 From: Santo Cariotti Date: Wed, 5 Jun 2024 21:10:30 +0200 Subject: [PATCH 12/23] wip --- progs/test2.py | 2 + src/ast/Python3VisitorImpl.java | 88 ++++++++++++++++++++++++++++--- src/ast/nodes/ArglistNode.java | 45 ++++++++++++++++ src/ast/nodes/AssignmentNode.java | 2 +- src/ast/nodes/AtomNode.java | 11 ++-- src/ast/nodes/AugassignNode.java | 2 +- src/ast/nodes/BlockNode.java | 27 ++++++++++ src/ast/nodes/CompNode.java | 32 +++++++++++ src/ast/nodes/CompoundNode.java | 10 ++-- src/ast/nodes/ExprNode.java | 76 ++++++++++++++++++++++++++ src/ast/nodes/IfNode.java | 12 ++++- src/ast/nodes/RootNode.java | 8 +-- src/ast/nodes/SimpleStmtNode.java | 28 ++++++++-- src/ast/nodes/TrailerNode.java | 37 +++++++++++++ 14 files changed, 350 insertions(+), 30 deletions(-) create mode 100644 src/ast/nodes/ArglistNode.java create mode 100644 src/ast/nodes/BlockNode.java create mode 100644 src/ast/nodes/CompNode.java create mode 100644 src/ast/nodes/ExprNode.java create mode 100644 src/ast/nodes/TrailerNode.java diff --git a/progs/test2.py b/progs/test2.py index 7d4290a..c62d44a 100644 --- a/progs/test2.py +++ b/progs/test2.py @@ -1 +1,3 @@ x = 1 +if y == 1: + print("a") diff --git a/src/ast/Python3VisitorImpl.java b/src/ast/Python3VisitorImpl.java index 2c72c7f..93db5d8 100644 --- a/src/ast/Python3VisitorImpl.java +++ b/src/ast/Python3VisitorImpl.java @@ -35,9 +35,19 @@ public Node visitSimple_stmts(Simple_stmtsContext ctx) { } public Node visitSimple_stmt(Simple_stmtContext ctx) { - Node exp = visit(ctx.assignment()); - return new SimpleStmtNode(exp); + Node assignment = null; + Node expr = null; + + if (ctx.assignment() != null) { + assignment = visit(ctx.assignment()); + } + + if (ctx.expr() != null) { + expr = visit(ctx.expr()); + } + + return new SimpleStmtNode(assignment, expr); } public Node visitCompound_stmt(Compound_stmtContext ctx) { @@ -65,20 +75,49 @@ public Node visitExprlist(ExprlistContext ctx) { } public Node visitExpr(ExprContext ctx) { - Node atom = visit(ctx.atom()); + Node atom = null; + Node compOp = null; + ArrayList exprs = new ArrayList(); + ArrayList trailers = new ArrayList(); + + if (ctx.atom() != null) { + atom = visit(ctx.atom()); + } + + if (ctx.comp_op() != null) { + compOp = visit(ctx.comp_op()); + } - return atom; + for (ExprContext s : ctx.expr()) { + exprs.add(visit(s)); + } + + for (TrailerContext s : ctx.trailer()) { + trailers.add(visit(s)); + } + + return new ExprNode(atom, compOp, exprs, trailers); } public Node visitAtom(AtomContext ctx) { if (ctx.NUMBER() != null) { - return new AtomNode(ctx.NUMBER()); + return new AtomNode(ctx.NUMBER().toString()); } else if (ctx.TRUE() != null) { - return new AtomNode(ctx.TRUE()); + return new AtomNode(ctx.TRUE().toString()); } else if (ctx.FALSE() != null) { - return new AtomNode(ctx.FALSE()); + return new AtomNode(ctx.FALSE().toString()); + } else if (ctx.NAME() != null) { + return new AtomNode(ctx.NAME().toString()); + } else if (ctx.STRING() != null) { + + var varName = ""; + for (var x : ctx.STRING()) { + varName += x; + } + + return new AtomNode(varName); } - return new AtomNode(ctx.NAME()); + return new AtomNode(ctx.NONE().toString()); } public Node visitAugassign(AugassignContext ctx) { @@ -97,4 +136,37 @@ public Node visitIf_stmt(If_stmtContext ctx) { return new IfNode(condExp, thenExp, elseExp); } + + public Node visitComp_op(Comp_opContext ctx) { + return new CompNode(ctx.EQUALS()); + } + + public Node visitBlock(BlockContext ctx) { + ArrayList stmts = new ArrayList(); + ArrayList compStmts = new ArrayList(); + + for (Simple_stmtsContext s : ctx.simple_stmts()) { + stmts.add(visit(s)); + } + for (Compound_stmtContext s : ctx.compound_stmt()) { + compStmts.add(visit(s)); + } + + return new BlockNode(stmts, compStmts); + } + + public Node visitTrailer(TrailerContext ctx) { + Node arglist = visit(ctx.arglist()); + return new TrailerNode(arglist); + } + + public Node visitArglist(ArglistContext ctx) { + ArrayList arguments = new ArrayList(); + + for (ArgumentContext c : ctx.argument()) { + arguments.add(visit(c)); + } + + return new ArglistNode(arguments); + } } diff --git a/src/ast/nodes/ArglistNode.java b/src/ast/nodes/ArglistNode.java new file mode 100644 index 0000000..0ebb816 --- /dev/null +++ b/src/ast/nodes/ArglistNode.java @@ -0,0 +1,45 @@ +package com.clp.project.ast.nodes; + +import java.util.ArrayList; + +import com.clp.project.semanticanalysis.SemanticError; +import com.clp.project.semanticanalysis.SymbolTable; +import com.clp.project.ast.types.*; + +public class ArglistNode implements Node { + protected ArrayList arguments; + + public ArglistNode(ArrayList arguments) { + this.arguments = arguments; + } + + @Override + public ArrayList checkSemantics(SymbolTable ST, int _nesting) { + ArrayList errors = new ArrayList(); + + for (var arg : arguments) { + errors.addAll(arg.checkSemantics(ST, _nesting)); + } + + return errors; + } + + public Type typeCheck() { + return new VoidType(); + } + + public String codeGeneration() { + return ""; + } + + public String toPrint(String s) { + String result = s + "ArglistNode\n"; + + for (Node arg : arguments) { + result += arg.toPrint(s + " "); + } + + return result; + } + +} diff --git a/src/ast/nodes/AssignmentNode.java b/src/ast/nodes/AssignmentNode.java index 5439209..8d3bb51 100644 --- a/src/ast/nodes/AssignmentNode.java +++ b/src/ast/nodes/AssignmentNode.java @@ -29,7 +29,7 @@ public ArrayList checkSemantics(SymbolTable ST, int _nesting) { } public Type typeCheck() { - return new IntType(); + return rhr.typeCheck(); } public String codeGeneration() { diff --git a/src/ast/nodes/AtomNode.java b/src/ast/nodes/AtomNode.java index bdbc323..969e40c 100644 --- a/src/ast/nodes/AtomNode.java +++ b/src/ast/nodes/AtomNode.java @@ -5,12 +5,11 @@ import com.clp.project.semanticanalysis.SemanticError; import com.clp.project.semanticanalysis.SymbolTable; import com.clp.project.ast.types.*; -import org.antlr.v4.runtime.tree.TerminalNode; public class AtomNode implements Node { - private TerminalNode val; + private String val; - public AtomNode(TerminalNode _val) { + public AtomNode(String _val) { val = _val; } @@ -27,7 +26,11 @@ public String codeGeneration() { } public String toPrint(String s) { - return s + "Atom(" + val.toString() + ")"; + if (val != null) { + return s + "Atom(" + val + ")\n"; + } + + return s + "Atom(null)\n"; } } diff --git a/src/ast/nodes/AugassignNode.java b/src/ast/nodes/AugassignNode.java index d7f4fa2..1e5ad15 100644 --- a/src/ast/nodes/AugassignNode.java +++ b/src/ast/nodes/AugassignNode.java @@ -27,6 +27,6 @@ public String codeGeneration() { } public String toPrint(String s) { - return s + "Augassign(" + val + ")"; + return s + "Augassign(" + val + ")\n"; } } diff --git a/src/ast/nodes/BlockNode.java b/src/ast/nodes/BlockNode.java new file mode 100644 index 0000000..a5870c0 --- /dev/null +++ b/src/ast/nodes/BlockNode.java @@ -0,0 +1,27 @@ +package com.clp.project.ast.nodes; + +import java.util.ArrayList; + +import com.clp.project.semanticanalysis.SemanticError; +import com.clp.project.semanticanalysis.SymbolTable; +import com.clp.project.ast.types.*; + +public class BlockNode extends RootNode implements Node { + public BlockNode(ArrayList _stmts, ArrayList _compoundStmts) { + super(_stmts, _compoundStmts); + } + + public String toPrint(String s) { + String result = s + "Block\n"; + + for (Node stmt : stmts) { + result += stmt.toPrint(s + " "); + } + for (Node stmt : compoundStmts) { + result += stmt.toPrint(s + " "); + } + + return result; + } + +} diff --git a/src/ast/nodes/CompNode.java b/src/ast/nodes/CompNode.java new file mode 100644 index 0000000..5a007eb --- /dev/null +++ b/src/ast/nodes/CompNode.java @@ -0,0 +1,32 @@ +package com.clp.project.ast.nodes; + +import java.util.ArrayList; + +import com.clp.project.semanticanalysis.SemanticError; +import com.clp.project.semanticanalysis.SymbolTable; +import com.clp.project.ast.types.*; +import org.antlr.v4.runtime.tree.TerminalNode; + +public class CompNode implements Node { + private TerminalNode val; + + public CompNode(TerminalNode _val) { + val = _val; + } + + public ArrayList checkSemantics(SymbolTable ST, int _nesting) { + return new ArrayList(); + } + + public Type typeCheck() { + return new ErrorType(); + } + + public String codeGeneration() { + return ""; + } + + public String toPrint(String s) { + return s + "CompNode(" + val + ")\n"; + } +} diff --git a/src/ast/nodes/CompoundNode.java b/src/ast/nodes/CompoundNode.java index ecff0c1..deabc50 100644 --- a/src/ast/nodes/CompoundNode.java +++ b/src/ast/nodes/CompoundNode.java @@ -7,17 +7,17 @@ import com.clp.project.ast.types.*; public class CompoundNode implements Node { - private Node _if; + private Node ifNode; - public CompoundNode(Node _if) { - this._if = _if; + public CompoundNode(Node ifNode) { + this.ifNode = ifNode; } @Override public ArrayList checkSemantics(SymbolTable ST, int _nesting) { ArrayList errors = new ArrayList(); - errors.addAll(_if.checkSemantics(ST, _nesting)); + errors.addAll(ifNode.checkSemantics(ST, _nesting)); return errors; } @@ -31,7 +31,7 @@ public String codeGeneration() { } public String toPrint(String s) { - return s + "CompoundNode\n" + _if.toPrint(s + " "); + return s + "CompoundNode\n" + ifNode.toPrint(s + " "); } } diff --git a/src/ast/nodes/ExprNode.java b/src/ast/nodes/ExprNode.java new file mode 100644 index 0000000..35c0770 --- /dev/null +++ b/src/ast/nodes/ExprNode.java @@ -0,0 +1,76 @@ +package com.clp.project.ast.nodes; + +import java.util.ArrayList; + +import com.clp.project.semanticanalysis.SemanticError; +import com.clp.project.semanticanalysis.SymbolTable; +import com.clp.project.ast.types.*; + +public class ExprNode implements Node { + private Node atom; + private Node compOp; + private ArrayList exprs; + private ArrayList trailers; + + public ExprNode(Node _atom, Node _compOp, ArrayList _exprs, ArrayList _trailers) { + atom = _atom; + compOp = _compOp; + exprs = _exprs; + trailers = _trailers; + } + + @Override + public ArrayList checkSemantics(SymbolTable ST, int _nesting) { + ArrayList errors = new ArrayList(); + + if (atom != null) { + errors.addAll(atom.checkSemantics(ST, _nesting)); + } + + if (compOp != null) { + errors.addAll(compOp.checkSemantics(ST, _nesting)); + } + + for (var expr : exprs) { + errors.addAll(expr.checkSemantics(ST, _nesting)); + } + + for (var trailer : trailers) { + errors.addAll(trailer.checkSemantics(ST, _nesting)); + } + + return errors; + } + + public Type typeCheck() { + // FIXME: wtf is that? + return new VoidType(); + } + + public String codeGeneration() { + return ""; + } + + public String toPrint(String s) { + String result = s + "Expr\n"; + + if (atom != null) { + result += atom.toPrint(s + " "); + } + + if (compOp != null) { + result += compOp.toPrint(s + " "); + } + + for (var expr : exprs) { + result += expr.toPrint(s + " "); + } + + for (var trailer : trailers) { + result += trailer.toPrint(s + " "); + } + + return result; + } + +} diff --git a/src/ast/nodes/IfNode.java b/src/ast/nodes/IfNode.java index af899a1..ebe68f9 100644 --- a/src/ast/nodes/IfNode.java +++ b/src/ast/nodes/IfNode.java @@ -23,7 +23,9 @@ public ArrayList checkSemantics(SymbolTable ST, int _nesting) { errors.addAll(guard.checkSemantics(ST, _nesting)); errors.addAll(thenbranch.checkSemantics(ST, _nesting)); - errors.addAll(elsebranch.checkSemantics(ST, _nesting)); + if (elsebranch != null) { + errors.addAll(elsebranch.checkSemantics(ST, _nesting)); + } return errors; } @@ -49,7 +51,13 @@ public String codeGeneration() { } public String toPrint(String s) { - return s + "If\n" + guard.toPrint(s + " ") + thenbranch.toPrint(s + " ") + elsebranch.toPrint(s + " "); + String result = s + "If\n" + guard.toPrint(s + " ") + thenbranch.toPrint(s + " "); + + if (elsebranch != null) { + result += elsebranch.toPrint(s + " "); + } + + return result; } } diff --git a/src/ast/nodes/RootNode.java b/src/ast/nodes/RootNode.java index 58d0e73..f925232 100644 --- a/src/ast/nodes/RootNode.java +++ b/src/ast/nodes/RootNode.java @@ -7,8 +7,8 @@ import com.clp.project.ast.types.*; public class RootNode implements Node { - private ArrayList stmts; - private ArrayList compoundStmts; + protected ArrayList stmts; + protected ArrayList compoundStmts; public RootNode(ArrayList _stmts, ArrayList _compoundStmts) { stmts = _stmts; @@ -41,10 +41,10 @@ public String toPrint(String s) { String result = "Root\n"; for (Node stmt : stmts) { - result += stmt.toPrint(" "); + result += stmt.toPrint(s + " "); } for (Node stmt : compoundStmts) { - result += stmt.toPrint(" "); + result += stmt.toPrint(s + " "); } return result; diff --git a/src/ast/nodes/SimpleStmtNode.java b/src/ast/nodes/SimpleStmtNode.java index 82c685c..93c97ab 100644 --- a/src/ast/nodes/SimpleStmtNode.java +++ b/src/ast/nodes/SimpleStmtNode.java @@ -7,17 +7,25 @@ import com.clp.project.ast.types.*; public class SimpleStmtNode implements Node { - private Node stmt; + private Node assignment; + private Node expr; - public SimpleStmtNode(Node _stmt) { - stmt = _stmt; + public SimpleStmtNode(Node assignment, Node expr) { + this.assignment = assignment; + this.expr = expr; } @Override public ArrayList checkSemantics(SymbolTable ST, int _nesting) { ArrayList errors = new ArrayList(); - errors.addAll(stmt.checkSemantics(ST, _nesting)); + if (assignment != null) { + errors.addAll(assignment.checkSemantics(ST, _nesting)); + } + + if (expr != null) { + errors.addAll(expr.checkSemantics(ST, _nesting)); + } return errors; } @@ -31,7 +39,17 @@ public String codeGeneration() { } public String toPrint(String s) { - return s + "SimpleStmt\n" + stmt.toPrint(s + " "); + String result = s + "SimpleStmt\n"; + + if (assignment != null) { + result += assignment.toPrint(s + " "); + } + + if (expr != null) { + result += expr.toPrint(s + " "); + } + + return result; } } diff --git a/src/ast/nodes/TrailerNode.java b/src/ast/nodes/TrailerNode.java new file mode 100644 index 0000000..bb50c47 --- /dev/null +++ b/src/ast/nodes/TrailerNode.java @@ -0,0 +1,37 @@ +package com.clp.project.ast.nodes; + +import java.util.ArrayList; + +import com.clp.project.semanticanalysis.SemanticError; +import com.clp.project.semanticanalysis.SymbolTable; +import com.clp.project.ast.types.*; + +public class TrailerNode implements Node { + private Node arglist; + + public TrailerNode(Node arglist) { + this.arglist = arglist; + } + + @Override + public ArrayList checkSemantics(SymbolTable ST, int _nesting) { + ArrayList errors = new ArrayList(); + + errors.addAll(arglist.checkSemantics(ST, _nesting)); + + return errors; + } + + public Type typeCheck() { + return new VoidType(); + } + + public String codeGeneration() { + return ""; + } + + public String toPrint(String s) { + return s + "TrailerNode\n" + arglist.toPrint(s + " "); + } + +} From 962c764d5d12041a3a484a957159d25663cffbd0 Mon Sep 17 00:00:00 2001 From: Santo Cariotti Date: Thu, 6 Jun 2024 07:25:02 +0200 Subject: [PATCH 13/23] wip --- progs/test.py | 23 +++++++------- src/Main.java | 2 +- src/ast/Python3VisitorImpl.java | 51 ++++++++++++++++++++++++++++--- src/ast/nodes/AtomNode.java | 2 +- src/ast/nodes/CompoundNode.java | 26 +++++++++++++--- src/ast/nodes/FuncdefNode.java | 51 +++++++++++++++++++++++++++++++ src/ast/nodes/ParamdefNode.java | 18 +++++++++++ src/ast/nodes/ParamlistNode.java | 46 ++++++++++++++++++++++++++++ src/ast/nodes/ReturnStmtNode.java | 40 ++++++++++++++++++++++++ src/ast/nodes/SimpleStmtNode.java | 12 +++++++- 10 files changed, 249 insertions(+), 22 deletions(-) create mode 100644 src/ast/nodes/FuncdefNode.java create mode 100644 src/ast/nodes/ParamdefNode.java create mode 100644 src/ast/nodes/ParamlistNode.java create mode 100644 src/ast/nodes/ReturnStmtNode.java diff --git a/progs/test.py b/progs/test.py index f168c9b..e1f94fd 100644 --- a/progs/test.py +++ b/progs/test.py @@ -1,12 +1,13 @@ -def find_first_duplicate(nums): - num_set = set() - no_duplicate = -1 +def find_first_duplicate(nums, x): + # num_set = set() + # no_duplicate = -1 + # + # for i in range(len(nums)): + # pass + # + # if nums[i] in num_set: + # return nums[i] + # else: + # num_set.add(nums[i]) - for i in range(len(nums)): - - if nums[i] in num_set: - return nums[i] - else: - num_set.add(nums[i]) - - return no_duplicate \ No newline at end of file + return no_duplicate diff --git a/src/Main.java b/src/Main.java index 9b403e8..05f2d01 100644 --- a/src/Main.java +++ b/src/Main.java @@ -23,7 +23,7 @@ public static void main(String[] args) { try { // String fileStr = file.getPath(); // FIXME: use the fileStr above - String fileStr = "./progs/test2.py"; + String fileStr = "./progs/test.py"; System.out.println(fileStr); System.out.println(readFile(fileStr)); diff --git a/src/ast/Python3VisitorImpl.java b/src/ast/Python3VisitorImpl.java index 93db5d8..432cc89 100644 --- a/src/ast/Python3VisitorImpl.java +++ b/src/ast/Python3VisitorImpl.java @@ -38,6 +38,7 @@ public Node visitSimple_stmt(Simple_stmtContext ctx) { Node assignment = null; Node expr = null; + Node returnStmt = null; if (ctx.assignment() != null) { assignment = visit(ctx.assignment()); @@ -47,17 +48,30 @@ public Node visitSimple_stmt(Simple_stmtContext ctx) { expr = visit(ctx.expr()); } - return new SimpleStmtNode(assignment, expr); + if (ctx.return_stmt() != null) { + returnStmt = visit(ctx.return_stmt()); + } + + return new SimpleStmtNode(assignment, expr, returnStmt); } public Node visitCompound_stmt(Compound_stmtContext ctx) { - Node ifStmt = visit(ctx.if_stmt()); + Node ifStmt = null; + if (ctx.if_stmt() != null) { + ifStmt = visit(ctx.if_stmt()); + } + + Node funcDef = null; + + if (ctx.funcdef() != null) { + funcDef = visit(ctx.funcdef()); + } + // FIXME: adds below // Node whileStmt = visit(ctx.while_stmt()); // Node forStmt = visit(ctx.for_stmt()); - // Node funcDef = visit(ctx.funcdef()); // - return new CompoundNode(ifStmt); + return new CompoundNode(ifStmt, funcDef); } public Node visitAssignment(AssignmentContext ctx) { @@ -69,6 +83,7 @@ public Node visitAssignment(AssignmentContext ctx) { } public Node visitExprlist(ExprlistContext ctx) { + // FIXME: you've used to be a list, c'mon Node exp = visit(ctx.expr(0)); return exp; @@ -169,4 +184,32 @@ public Node visitArglist(ArglistContext ctx) { return new ArglistNode(arguments); } + + public Node visitFuncdef(FuncdefContext ctx) { + Node paramlist = visit(ctx.paramlist()); + Node block = visit(ctx.block()); + + return new FuncdefNode(ctx.NAME(), paramlist, block); + } + + public Node visitParamlist(ParamlistContext ctx) { + ArrayList params = new ArrayList(); + + for (ParamdefContext s : ctx.paramdef()) { + params.add(visit(s)); + } + + return new ParamlistNode(params); + } + + public Node visitParamdef(ParamdefContext ctx) { + return new ParamdefNode(ctx.NAME().toString()); + } + + public Node visitReturn_stmt(Return_stmtContext ctx) { + Node exprList = visit(ctx.exprlist()); + + return new ReturnStmtNode(exprList); + + } } diff --git a/src/ast/nodes/AtomNode.java b/src/ast/nodes/AtomNode.java index 969e40c..a521d82 100644 --- a/src/ast/nodes/AtomNode.java +++ b/src/ast/nodes/AtomNode.java @@ -7,7 +7,7 @@ import com.clp.project.ast.types.*; public class AtomNode implements Node { - private String val; + protected String val; public AtomNode(String _val) { val = _val; diff --git a/src/ast/nodes/CompoundNode.java b/src/ast/nodes/CompoundNode.java index deabc50..7f4816b 100644 --- a/src/ast/nodes/CompoundNode.java +++ b/src/ast/nodes/CompoundNode.java @@ -8,16 +8,24 @@ public class CompoundNode implements Node { private Node ifNode; + private Node funcDef; - public CompoundNode(Node ifNode) { - this.ifNode = ifNode; + public CompoundNode(Node _ifNode, Node _funcDef) { + ifNode = _ifNode; + funcDef = _funcDef; } @Override public ArrayList checkSemantics(SymbolTable ST, int _nesting) { ArrayList errors = new ArrayList(); - errors.addAll(ifNode.checkSemantics(ST, _nesting)); + if (ifNode != null) { + errors.addAll(ifNode.checkSemantics(ST, _nesting)); + } + + if (funcDef != null) { + errors.addAll(funcDef.checkSemantics(ST, _nesting)); + } return errors; } @@ -31,7 +39,17 @@ public String codeGeneration() { } public String toPrint(String s) { - return s + "CompoundNode\n" + ifNode.toPrint(s + " "); + String result = s + "CompoundNode\n"; + + if (ifNode != null) { + result += ifNode.toPrint(s + " "); + } + + if (funcDef != null) { + result += funcDef.toPrint(s + " "); + } + + return result; } } diff --git a/src/ast/nodes/FuncdefNode.java b/src/ast/nodes/FuncdefNode.java new file mode 100644 index 0000000..3dbf581 --- /dev/null +++ b/src/ast/nodes/FuncdefNode.java @@ -0,0 +1,51 @@ +package com.clp.project.ast.nodes; + +import java.util.ArrayList; + +import com.clp.project.semanticanalysis.SemanticError; +import com.clp.project.semanticanalysis.SymbolTable; +import com.clp.project.ast.types.*; +import org.antlr.v4.runtime.tree.TerminalNode; + +public class FuncdefNode implements Node { + private TerminalNode name; + private Node paramlist; + private Node block; + + public FuncdefNode(TerminalNode _name, Node _paramlist, Node _block) { + name = _name; + paramlist = _paramlist; + block = _block; + } + + @Override + public ArrayList checkSemantics(SymbolTable ST, int _nesting) { + ArrayList errors = new ArrayList(); + + errors.addAll(paramlist.checkSemantics(ST, _nesting)); + errors.addAll(block.checkSemantics(ST, _nesting)); + + return errors; + } + + public Type typeCheck() { + return new VoidType(); + } + + public String codeGeneration() { + return ""; + } + + public String toPrint(String s) { + String result = s + "Funcdef(" + name + ")\n"; + + if (paramlist != null) { + result += paramlist.toPrint(s + " "); + } + + result += block.toPrint(s + " "); + + return result; + } + +} diff --git a/src/ast/nodes/ParamdefNode.java b/src/ast/nodes/ParamdefNode.java new file mode 100644 index 0000000..3a34b36 --- /dev/null +++ b/src/ast/nodes/ParamdefNode.java @@ -0,0 +1,18 @@ +package com.clp.project.ast.nodes; + +import java.util.ArrayList; + +import com.clp.project.semanticanalysis.SemanticError; +import com.clp.project.semanticanalysis.SymbolTable; +import com.clp.project.ast.types.*; + +public class ParamdefNode extends AtomNode implements Node { + public ParamdefNode(String val) { + super(val); + } + + @Override + public String toPrint(String s) { + return s + "Paramdef(" + val + ")\n"; + } +} diff --git a/src/ast/nodes/ParamlistNode.java b/src/ast/nodes/ParamlistNode.java new file mode 100644 index 0000000..c0e8d4b --- /dev/null +++ b/src/ast/nodes/ParamlistNode.java @@ -0,0 +1,46 @@ +package com.clp.project.ast.nodes; + +import java.util.ArrayList; + +import com.clp.project.semanticanalysis.SemanticError; +import com.clp.project.semanticanalysis.SymbolTable; +import com.clp.project.ast.types.*; + +public class ParamlistNode implements Node { + private ArrayList params; + + public ParamlistNode(ArrayList _params) { + params = _params; + } + + @Override + public ArrayList checkSemantics(SymbolTable ST, int _nesting) { + ArrayList errors = new ArrayList(); + + for (var param : params) { + errors.addAll(param.checkSemantics(ST, _nesting)); + } + + return errors; + } + + public Type typeCheck() { + // FIXME: wtf is that? + return new VoidType(); + } + + public String codeGeneration() { + return ""; + } + + public String toPrint(String s) { + String result = s + "Paramlist\n"; + + for (var param : params) { + result += param.toPrint(s + " "); + } + + return result; + } + +} diff --git a/src/ast/nodes/ReturnStmtNode.java b/src/ast/nodes/ReturnStmtNode.java new file mode 100644 index 0000000..108b9a4 --- /dev/null +++ b/src/ast/nodes/ReturnStmtNode.java @@ -0,0 +1,40 @@ +package com.clp.project.ast.nodes; + +import java.util.ArrayList; + +import com.clp.project.semanticanalysis.SemanticError; +import com.clp.project.semanticanalysis.SymbolTable; +import com.clp.project.ast.types.*; + +public class ReturnStmtNode implements Node { + private Node exprList; + + public ReturnStmtNode(Node exprList) { + this.exprList = exprList; + } + + @Override + public ArrayList checkSemantics(SymbolTable ST, int _nesting) { + ArrayList errors = new ArrayList(); + + errors.addAll(this.exprList.checkSemantics(ST, _nesting)); + + return errors; + } + + public Type typeCheck() { + // FIXME: wtf is that? + return new VoidType(); + } + + public String codeGeneration() { + return ""; + } + + public String toPrint(String s) { + String result = s + "ReturnStmt\n" + this.exprList.toPrint(s + " "); + + return result; + } + +} diff --git a/src/ast/nodes/SimpleStmtNode.java b/src/ast/nodes/SimpleStmtNode.java index 93c97ab..03029e8 100644 --- a/src/ast/nodes/SimpleStmtNode.java +++ b/src/ast/nodes/SimpleStmtNode.java @@ -9,10 +9,12 @@ public class SimpleStmtNode implements Node { private Node assignment; private Node expr; + private Node returnStmt; - public SimpleStmtNode(Node assignment, Node expr) { + public SimpleStmtNode(Node assignment, Node expr, Node returnStmt) { this.assignment = assignment; this.expr = expr; + this.returnStmt = returnStmt; } @Override @@ -27,6 +29,10 @@ public ArrayList checkSemantics(SymbolTable ST, int _nesting) { errors.addAll(expr.checkSemantics(ST, _nesting)); } + if (returnStmt != null) { + errors.addAll(returnStmt.checkSemantics(ST, _nesting)); + } + return errors; } @@ -49,6 +55,10 @@ public String toPrint(String s) { result += expr.toPrint(s + " "); } + if (returnStmt != null) { + result += returnStmt.toPrint(s + " "); + } + return result; } From 4b3cd5a3f70bbbf7eb1da953474776fbd643ecdb Mon Sep 17 00:00:00 2001 From: Santo Cariotti Date: Thu, 6 Jun 2024 07:25:08 +0200 Subject: [PATCH 14/23] Add label --- src/ast/nodes/BlockNode.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/ast/nodes/BlockNode.java b/src/ast/nodes/BlockNode.java index a5870c0..52dcf8a 100644 --- a/src/ast/nodes/BlockNode.java +++ b/src/ast/nodes/BlockNode.java @@ -11,6 +11,7 @@ public BlockNode(ArrayList _stmts, ArrayList _compoundStmts) { super(_stmts, _compoundStmts); } + @Override public String toPrint(String s) { String result = s + "Block\n"; From 5f61d6e54bda071f6a4a4566f85fcf86fe952d89 Mon Sep 17 00:00:00 2001 From: Santo Cariotti Date: Thu, 6 Jun 2024 21:01:38 +0200 Subject: [PATCH 15/23] wip --- progs/test.py | 24 +++--- src/ast/Python3VisitorImpl.java | 138 +++++++++++++++++++++++++++--- src/ast/nodes/CompoundNode.java | 26 +++++- src/ast/nodes/DottedNameNode.java | 42 +++++++++ src/ast/nodes/ExprNode.java | 16 ++-- src/ast/nodes/ForStmtNode.java | 40 +++++++++ src/ast/nodes/ImportNode.java | 68 +++++++++++++++ src/ast/nodes/ReturnStmtNode.java | 8 +- src/ast/nodes/RootNode.java | 6 +- src/ast/nodes/SimpleStmtNode.java | 12 ++- src/ast/nodes/TrailerNode.java | 38 +++++++- src/ast/nodes/WhileStmtNode.java | 40 +++++++++ 12 files changed, 419 insertions(+), 39 deletions(-) create mode 100644 src/ast/nodes/DottedNameNode.java create mode 100644 src/ast/nodes/ForStmtNode.java create mode 100644 src/ast/nodes/ImportNode.java create mode 100644 src/ast/nodes/WhileStmtNode.java diff --git a/progs/test.py b/progs/test.py index e1f94fd..718885d 100644 --- a/progs/test.py +++ b/progs/test.py @@ -1,13 +1,17 @@ +import math +from re import f4, r3 + +from abs import * + + def find_first_duplicate(nums, x): - # num_set = set() - # no_duplicate = -1 - # - # for i in range(len(nums)): - # pass - # - # if nums[i] in num_set: - # return nums[i] - # else: - # num_set.add(nums[i]) + num_set = set() + no_duplicate = -1 + + for i in range(len(nums)): + if nums[i] in num_set: + return nums[i] + else: + num_set.add(nums[i]) return no_duplicate diff --git a/src/ast/Python3VisitorImpl.java b/src/ast/Python3VisitorImpl.java index 432cc89..8a062a8 100644 --- a/src/ast/Python3VisitorImpl.java +++ b/src/ast/Python3VisitorImpl.java @@ -8,6 +8,7 @@ import com.clp.project.ast.types.*; import com.clp.project.parser.Python3ParserBaseVisitor; import com.clp.project.parser.Python3Parser.*; +import org.antlr.v4.runtime.tree.TerminalNode; public class Python3VisitorImpl extends Python3ParserBaseVisitor { public Node visitRoot(RootContext ctx) { @@ -35,9 +36,9 @@ public Node visitSimple_stmts(Simple_stmtsContext ctx) { } public Node visitSimple_stmt(Simple_stmtContext ctx) { - Node assignment = null; Node expr = null; + Node importStmt = null; Node returnStmt = null; if (ctx.assignment() != null) { @@ -48,11 +49,15 @@ public Node visitSimple_stmt(Simple_stmtContext ctx) { expr = visit(ctx.expr()); } + if (ctx.import_stm() != null) { + importStmt = visit(ctx.import_stm()); + } + if (ctx.return_stmt() != null) { returnStmt = visit(ctx.return_stmt()); } - return new SimpleStmtNode(assignment, expr, returnStmt); + return new SimpleStmtNode(assignment, expr, importStmt, returnStmt); } public Node visitCompound_stmt(Compound_stmtContext ctx) { @@ -62,16 +67,20 @@ public Node visitCompound_stmt(Compound_stmtContext ctx) { } Node funcDef = null; - if (ctx.funcdef() != null) { funcDef = visit(ctx.funcdef()); } - // FIXME: adds below - // Node whileStmt = visit(ctx.while_stmt()); - // Node forStmt = visit(ctx.for_stmt()); - // - return new CompoundNode(ifStmt, funcDef); + Node forStmt = null; + if (ctx.for_stmt() != null) { + forStmt = visit(ctx.for_stmt()); + } + + Node whileStmt = null; + if (ctx.while_stmt() != null) { + whileStmt = visit(ctx.while_stmt()); + } + return new CompoundNode(ifStmt, funcDef, forStmt, whileStmt); } public Node visitAssignment(AssignmentContext ctx) { @@ -94,6 +103,27 @@ public Node visitExpr(ExprContext ctx) { Node compOp = null; ArrayList exprs = new ArrayList(); ArrayList trailers = new ArrayList(); + String op = null; + + if (ctx.ADD(0) != null) { + op = ctx.ADD(0).toString(); + } + + if (ctx.MINUS(0) != null) { + op = ctx.MINUS(0).toString(); + } + + if (ctx.NOT() != null) { + op = ctx.NOT().toString(); + } + + if (ctx.STAR() != null) { + op = ctx.STAR().toString(); + } + + if (ctx.DIV() != null) { + op = ctx.DIV().toString(); + } if (ctx.atom() != null) { atom = visit(ctx.atom()); @@ -111,7 +141,7 @@ public Node visitExpr(ExprContext ctx) { trailers.add(visit(s)); } - return new ExprNode(atom, compOp, exprs, trailers); + return new ExprNode(atom, compOp, exprs, op, trailers); } public Node visitAtom(AtomContext ctx) { @@ -153,7 +183,29 @@ public Node visitIf_stmt(If_stmtContext ctx) { } public Node visitComp_op(Comp_opContext ctx) { - return new CompNode(ctx.EQUALS()); + Node comp = null; + if (ctx.LESS_THAN() != null) { + comp = new CompNode(ctx.LESS_THAN()); + } else if (ctx.GREATER_THAN() != null) { + comp = new CompNode(ctx.GREATER_THAN()); + } else if (ctx.EQUALS() != null) { + comp = new CompNode(ctx.EQUALS()); + } else if (ctx.GT_EQ() != null) { + comp = new CompNode(ctx.GT_EQ()); + } else if (ctx.LT_EQ() != null) { + comp = new CompNode(ctx.LT_EQ()); + } else if (ctx.NOT_EQ_2() != null) { + // We're ignoring NOT_EQ_1() because no one uses `<>` + comp = new CompNode(ctx.NOT_EQ_2()); + } else if (ctx.IN() != null) { + comp = new CompNode(ctx.IN()); + } else if (ctx.NOT() != null) { + comp = new CompNode(ctx.NOT()); + } else if (ctx.IS() != null) { + comp = new CompNode(ctx.IS()); + } + + return comp; } public Node visitBlock(BlockContext ctx) { @@ -171,8 +223,22 @@ public Node visitBlock(BlockContext ctx) { } public Node visitTrailer(TrailerContext ctx) { - Node arglist = visit(ctx.arglist()); - return new TrailerNode(arglist); + Node arglist = null; + if (ctx.arglist() != null) { + arglist = visit(ctx.arglist()); + } + + ArrayList exprs = new ArrayList(); + for (ExprContext expr : ctx.expr()) { + exprs.add(visit(expr)); + } + + TerminalNode methodCall = null; + if (ctx.DOT() != null) { + methodCall = ctx.NAME(); + } + + return new TrailerNode(arglist, exprs, methodCall); } public Node visitArglist(ArglistContext ctx) { @@ -207,9 +273,55 @@ public Node visitParamdef(ParamdefContext ctx) { } public Node visitReturn_stmt(Return_stmtContext ctx) { - Node exprList = visit(ctx.exprlist()); + Node exprList = null; + if (ctx.exprlist() != null) { + exprList = visit(ctx.exprlist()); + } return new ReturnStmtNode(exprList); + } + + public Node visitFor_stmt(For_stmtContext ctx) { + Node exprList = visit(ctx.exprlist()); + + // Block 1 is for the for-else statement + Node block = visit(ctx.block(0)); + + return new ForStmtNode(exprList, block); + } + + public Node visitWhile_stmt(While_stmtContext ctx) { + Node expr = visit(ctx.expr()); + + // Block 1 is for the while-else statement + Node block = visit(ctx.block(0)); + + return new WhileStmtNode(expr, block); + } + + public Node visitImport_stm(Import_stmContext ctx) { + boolean isFrom = ctx.FROM() != null; + boolean importAs = ctx.AS() != null; + boolean importAll = ctx.STAR() != null; + + Node dottedName = visit(ctx.dotted_name()); + + ArrayList names = new ArrayList(); + + for (var s : ctx.NAME()) { + names.add(s.toString()); + } + + return new ImportNode(dottedName, isFrom, importAs, importAll, names); + } + + public Node visitDotted_name(Dotted_nameContext ctx) { + ArrayList names = new ArrayList(); + + for (var name : ctx.NAME()) { + names.add(name); + } + return new DottedNameNode(names); } } diff --git a/src/ast/nodes/CompoundNode.java b/src/ast/nodes/CompoundNode.java index 7f4816b..256eeae 100644 --- a/src/ast/nodes/CompoundNode.java +++ b/src/ast/nodes/CompoundNode.java @@ -9,10 +9,14 @@ public class CompoundNode implements Node { private Node ifNode; private Node funcDef; + private Node forStmt; + private Node whileStmt; - public CompoundNode(Node _ifNode, Node _funcDef) { - ifNode = _ifNode; - funcDef = _funcDef; + public CompoundNode(Node ifNode, Node funcDef, Node forStmt, Node whileStmt) { + this.ifNode = ifNode; + this.funcDef = funcDef; + this.forStmt = forStmt; + this.whileStmt = whileStmt; } @Override @@ -27,6 +31,14 @@ public ArrayList checkSemantics(SymbolTable ST, int _nesting) { errors.addAll(funcDef.checkSemantics(ST, _nesting)); } + if (forStmt != null) { + errors.addAll(forStmt.checkSemantics(ST, _nesting)); + } + + if (whileStmt != null) { + errors.addAll(whileStmt.checkSemantics(ST, _nesting)); + } + return errors; } @@ -49,6 +61,14 @@ public String toPrint(String s) { result += funcDef.toPrint(s + " "); } + if (forStmt != null) { + result += forStmt.toPrint(s + " "); + } + + if (whileStmt != null) { + result += whileStmt.toPrint(s + " "); + } + return result; } diff --git a/src/ast/nodes/DottedNameNode.java b/src/ast/nodes/DottedNameNode.java new file mode 100644 index 0000000..f0102d1 --- /dev/null +++ b/src/ast/nodes/DottedNameNode.java @@ -0,0 +1,42 @@ +package com.clp.project.ast.nodes; + +import java.util.ArrayList; + +import com.clp.project.semanticanalysis.SemanticError; +import com.clp.project.semanticanalysis.SymbolTable; +import com.clp.project.ast.types.*; +import org.antlr.v4.runtime.tree.TerminalNode; + +public class DottedNameNode implements Node { + protected ArrayList names; + + public DottedNameNode(ArrayList names) { + this.names = names; + } + + @Override + public ArrayList checkSemantics(SymbolTable ST, int _nesting) { + ArrayList errors = new ArrayList(); + + return errors; + } + + public Type typeCheck() { + return new VoidType(); + } + + public String codeGeneration() { + return ""; + } + + public String toPrint(String s) { + String result = s + "DottedName\n"; + + for (var name : names) { + result += s + " " + name.toString() + "\n"; + } + + return result; + } + +} diff --git a/src/ast/nodes/ExprNode.java b/src/ast/nodes/ExprNode.java index 35c0770..a1ae22d 100644 --- a/src/ast/nodes/ExprNode.java +++ b/src/ast/nodes/ExprNode.java @@ -9,14 +9,16 @@ public class ExprNode implements Node { private Node atom; private Node compOp; + private String op; private ArrayList exprs; private ArrayList trailers; - public ExprNode(Node _atom, Node _compOp, ArrayList _exprs, ArrayList _trailers) { - atom = _atom; - compOp = _compOp; - exprs = _exprs; - trailers = _trailers; + public ExprNode(Node atom, Node compOp, ArrayList exprs, String op, ArrayList trailers) { + this.atom = atom; + this.compOp = compOp; + this.exprs = exprs; + this.op = op; + this.trailers = trailers; } @Override @@ -70,6 +72,10 @@ public String toPrint(String s) { result += trailer.toPrint(s + " "); } + if (op != null) { + result += s + " Op(" + op + ")\n"; + } + return result; } diff --git a/src/ast/nodes/ForStmtNode.java b/src/ast/nodes/ForStmtNode.java new file mode 100644 index 0000000..f7197e5 --- /dev/null +++ b/src/ast/nodes/ForStmtNode.java @@ -0,0 +1,40 @@ +package com.clp.project.ast.nodes; + +import java.util.ArrayList; + +import com.clp.project.semanticanalysis.SemanticError; +import com.clp.project.semanticanalysis.SymbolTable; +import com.clp.project.ast.types.*; + +public class ForStmtNode implements Node { + private Node exprList; + private Node block; + + public ForStmtNode(Node exprList, Node block) { + this.exprList = exprList; + this.block = block; + } + + @Override + public ArrayList checkSemantics(SymbolTable ST, int _nesting) { + ArrayList errors = new ArrayList(); + + errors.addAll(exprList.checkSemantics(ST, _nesting)); + errors.addAll(block.checkSemantics(ST, _nesting)); + + return errors; + } + + public Type typeCheck() { + return new VoidType(); + } + + public String codeGeneration() { + return ""; + } + + public String toPrint(String s) { + return s + "For\n" + exprList.toPrint(s + " ") + block.toPrint(s + " "); + } + +} diff --git a/src/ast/nodes/ImportNode.java b/src/ast/nodes/ImportNode.java new file mode 100644 index 0000000..fc5e1a6 --- /dev/null +++ b/src/ast/nodes/ImportNode.java @@ -0,0 +1,68 @@ +package com.clp.project.ast.nodes; + +import java.util.ArrayList; + +import com.clp.project.semanticanalysis.SemanticError; +import com.clp.project.semanticanalysis.SymbolTable; +import com.clp.project.ast.types.*; + +public class ImportNode implements Node { + private Node dottedName; + private boolean isFrom; + private boolean importAs; + private boolean importAll; + private ArrayList names; + + public ImportNode(Node dottedName, boolean isFrom, boolean importAs, boolean importAll, + ArrayList names) { + this.dottedName = dottedName; + this.isFrom = isFrom; + this.importAs = importAs; + this.importAll = importAll; + this.names = names; + } + + @Override + public ArrayList checkSemantics(SymbolTable ST, int _nesting) { + ArrayList errors = new ArrayList(); + + return errors; + } + + public Type typeCheck() { + return new VoidType(); + } + + public String codeGeneration() { + return ""; + } + + public String toPrint(String s) { + String result = s + "Import\n"; + + if (isFrom) { + result += s + " From\n" + dottedName.toPrint(s + " "); + } else { + result += dottedName.toPrint(s + " "); + } + + if (importAs) { + result += s + " As " + names.get(0) + "\n"; + } + + if (importAll) { + result += s + " All\n"; + } + + for (int i = 0; i < names.size(); ++i) { + if (i == 0 && importAs) + continue; + + result += s + " " + names.get(i) + "\n"; + } + + result += "\n"; + return result; + } + +} diff --git a/src/ast/nodes/ReturnStmtNode.java b/src/ast/nodes/ReturnStmtNode.java index 108b9a4..f9a82db 100644 --- a/src/ast/nodes/ReturnStmtNode.java +++ b/src/ast/nodes/ReturnStmtNode.java @@ -17,7 +17,9 @@ public ReturnStmtNode(Node exprList) { public ArrayList checkSemantics(SymbolTable ST, int _nesting) { ArrayList errors = new ArrayList(); + if (this.exprList != null) { errors.addAll(this.exprList.checkSemantics(ST, _nesting)); + } return errors; } @@ -32,7 +34,11 @@ public String codeGeneration() { } public String toPrint(String s) { - String result = s + "ReturnStmt\n" + this.exprList.toPrint(s + " "); + String result = s + "ReturnStmt\n"; + + if (this.exprList != null) { + result += this.exprList.toPrint(s + " "); + } return result; } diff --git a/src/ast/nodes/RootNode.java b/src/ast/nodes/RootNode.java index f925232..4c57a81 100644 --- a/src/ast/nodes/RootNode.java +++ b/src/ast/nodes/RootNode.java @@ -10,9 +10,9 @@ public class RootNode implements Node { protected ArrayList stmts; protected ArrayList compoundStmts; - public RootNode(ArrayList _stmts, ArrayList _compoundStmts) { - stmts = _stmts; - compoundStmts = _compoundStmts; + public RootNode(ArrayList stmts, ArrayList compoundStmts) { + this.stmts = stmts; + this.compoundStmts = compoundStmts; } @Override diff --git a/src/ast/nodes/SimpleStmtNode.java b/src/ast/nodes/SimpleStmtNode.java index 03029e8..79bb12d 100644 --- a/src/ast/nodes/SimpleStmtNode.java +++ b/src/ast/nodes/SimpleStmtNode.java @@ -9,11 +9,13 @@ public class SimpleStmtNode implements Node { private Node assignment; private Node expr; + private Node importStmt; private Node returnStmt; - public SimpleStmtNode(Node assignment, Node expr, Node returnStmt) { + public SimpleStmtNode(Node assignment, Node expr, Node importStmt, Node returnStmt) { this.assignment = assignment; this.expr = expr; + this.importStmt = importStmt; this.returnStmt = returnStmt; } @@ -29,6 +31,10 @@ public ArrayList checkSemantics(SymbolTable ST, int _nesting) { errors.addAll(expr.checkSemantics(ST, _nesting)); } + if (importStmt != null) { + errors.addAll(importStmt.checkSemantics(ST, _nesting)); + } + if (returnStmt != null) { errors.addAll(returnStmt.checkSemantics(ST, _nesting)); } @@ -55,6 +61,10 @@ public String toPrint(String s) { result += expr.toPrint(s + " "); } + if (importStmt != null) { + result += importStmt.toPrint(s + " "); + } + if (returnStmt != null) { result += returnStmt.toPrint(s + " "); } diff --git a/src/ast/nodes/TrailerNode.java b/src/ast/nodes/TrailerNode.java index bb50c47..671fcc9 100644 --- a/src/ast/nodes/TrailerNode.java +++ b/src/ast/nodes/TrailerNode.java @@ -5,19 +5,33 @@ import com.clp.project.semanticanalysis.SemanticError; import com.clp.project.semanticanalysis.SymbolTable; import com.clp.project.ast.types.*; +import org.antlr.v4.runtime.tree.TerminalNode; public class TrailerNode implements Node { private Node arglist; + private ArrayList exprs; + private TerminalNode methodCall; + private boolean isEmpty; - public TrailerNode(Node arglist) { + public TrailerNode(Node arglist, ArrayList exprs, TerminalNode methodCall) { this.arglist = arglist; + this.exprs = exprs; + this.methodCall = methodCall; + + this.isEmpty = (this.arglist == null && this.exprs.size() == 0 && this.methodCall == null); } @Override public ArrayList checkSemantics(SymbolTable ST, int _nesting) { ArrayList errors = new ArrayList(); - errors.addAll(arglist.checkSemantics(ST, _nesting)); + if (arglist != null) { + errors.addAll(arglist.checkSemantics(ST, _nesting)); + } + + for (var expr : exprs) { + errors.addAll(expr.checkSemantics(ST, _nesting)); + } return errors; } @@ -31,7 +45,25 @@ public String codeGeneration() { } public String toPrint(String s) { - return s + "TrailerNode\n" + arglist.toPrint(s + " "); + String result = s + "TrailerNode\n"; + + if (arglist != null) { + result += arglist.toPrint(s + " "); + } + + for (var expr : exprs) { + result += expr.toPrint(s + " "); + } + + if (methodCall != null) { + result += s + " Method(" + methodCall + ")\n"; + } + + if (isEmpty) { + result += s + " ()\n"; + } + + return result; } } diff --git a/src/ast/nodes/WhileStmtNode.java b/src/ast/nodes/WhileStmtNode.java new file mode 100644 index 0000000..5d77f2c --- /dev/null +++ b/src/ast/nodes/WhileStmtNode.java @@ -0,0 +1,40 @@ +package com.clp.project.ast.nodes; + +import java.util.ArrayList; + +import com.clp.project.semanticanalysis.SemanticError; +import com.clp.project.semanticanalysis.SymbolTable; +import com.clp.project.ast.types.*; + +public class WhileStmtNode implements Node { + private Node expr; + private Node block; + + public WhileStmtNode(Node expr, Node block) { + this.expr = expr; + this.block = block; + } + + @Override + public ArrayList checkSemantics(SymbolTable ST, int _nesting) { + ArrayList errors = new ArrayList(); + + errors.addAll(expr.checkSemantics(ST, _nesting)); + errors.addAll(block.checkSemantics(ST, _nesting)); + + return errors; + } + + public Type typeCheck() { + return new VoidType(); + } + + public String codeGeneration() { + return ""; + } + + public String toPrint(String s) { + return s + "While\n" + expr.toPrint(s + " ") + block.toPrint(s + " "); + } + +} From 291ade9b863719a5b49a33b2fa7699c81a329975 Mon Sep 17 00:00:00 2001 From: Santo Cariotti Date: Thu, 6 Jun 2024 21:02:17 +0200 Subject: [PATCH 16/23] fix style --- src/ast/Python3VisitorImpl.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ast/Python3VisitorImpl.java b/src/ast/Python3VisitorImpl.java index 8a062a8..90085ee 100644 --- a/src/ast/Python3VisitorImpl.java +++ b/src/ast/Python3VisitorImpl.java @@ -275,7 +275,7 @@ public Node visitParamdef(ParamdefContext ctx) { public Node visitReturn_stmt(Return_stmtContext ctx) { Node exprList = null; if (ctx.exprlist() != null) { - exprList = visit(ctx.exprlist()); + exprList = visit(ctx.exprlist()); } return new ReturnStmtNode(exprList); From 2b59a9b4d441b02218a780e00da4558cb19ee0ea Mon Sep 17 00:00:00 2001 From: Santo Cariotti Date: Fri, 7 Jun 2024 09:01:24 +0200 Subject: [PATCH 17/23] wip --- src/ast/Python3VisitorImpl.java | 264 ++++++++++++++++-------- src/ast/nodes/AssignmentNode.java | 21 +- src/ast/nodes/CompoundNode.java | 23 ++- src/ast/nodes/DottedNameNode.java | 17 +- src/ast/nodes/FuncdefNode.java | 32 ++- src/ast/nodes/ImportNode.java | 26 ++- src/ast/nodes/Node.java | 22 +- src/ast/nodes/ParamdefNode.java | 15 +- src/ast/nodes/ParamlistNode.java | 17 +- src/ast/nodes/ReturnStmtNode.java | 21 +- src/ast/nodes/RootNode.java | 21 +- src/ast/nodes/SimpleStmtNode.java | 41 ++-- src/ast/nodes/SimpleStmtsNode.java | 22 +- src/semanticanalysis/STentry.java | 42 ++-- src/semanticanalysis/SemanticError.java | 7 +- src/semanticanalysis/SymbolTable.java | 105 +++++++--- 16 files changed, 489 insertions(+), 207 deletions(-) diff --git a/src/ast/Python3VisitorImpl.java b/src/ast/Python3VisitorImpl.java index 90085ee..87f8bc1 100644 --- a/src/ast/Python3VisitorImpl.java +++ b/src/ast/Python3VisitorImpl.java @@ -10,21 +10,40 @@ import com.clp.project.parser.Python3Parser.*; import org.antlr.v4.runtime.tree.TerminalNode; +/** + * Implementation for the `Python3ParserBaseVisitor` class for the `Node` type. + * Overrides each `visitNODE` method from the base class. + */ public class Python3VisitorImpl extends Python3ParserBaseVisitor { + /** + * Since a root can be a simple_stmts or a compound_stmt, this method + * returns a new `RootNode` with a list of them. + * + * ``` + * root : NEWLINE* (simple_stmts | compound_stmt)* EOF; + * ``` + */ public Node visitRoot(RootContext ctx) { ArrayList stmts = new ArrayList(); ArrayList compStmts = new ArrayList(); - for (Simple_stmtsContext s : ctx.simple_stmts()) { - stmts.add(visit(s)); + for (Simple_stmtsContext stm : ctx.simple_stmts()) { + stmts.add(visit(stm)); } - for (Compound_stmtContext s : ctx.compound_stmt()) { - compStmts.add(visit(s)); + for (Compound_stmtContext stm : ctx.compound_stmt()) { + compStmts.add(visit(stm)); } return new RootNode(stmts, compStmts); } + /** + * Returns a `SimpleStmtsNode`, made by an array of SimpleStmtNode + * + * ``` + * simple_stmts : simple_stmt (';' simple_stmt)* ';'? NEWLINE ; + * ``` + */ public Node visitSimple_stmts(Simple_stmtsContext ctx) { ArrayList stmts = new ArrayList(); @@ -35,11 +54,52 @@ public Node visitSimple_stmts(Simple_stmtsContext ctx) { return new SimpleStmtsNode(stmts); } + /** + * Returns a `CompoundNode`. It can be built by a different kind of + * statements, so only one of theme won't be null. + * + * ``` + * compound_stmt : if_stmt | while_stmt | for_stmt | funcdef ; + * ``` + */ + public Node visitCompound_stmt(Compound_stmtContext ctx) { + Node ifStmt = null; + Node funcDef = null; + Node forStmt = null; + Node whileStmt = null; + + if (ctx.if_stmt() != null) { + ifStmt = visit(ctx.if_stmt()); + } + + if (ctx.funcdef() != null) { + funcDef = visit(ctx.funcdef()); + } + + if (ctx.for_stmt() != null) { + forStmt = visit(ctx.for_stmt()); + } + + if (ctx.while_stmt() != null) { + whileStmt = visit(ctx.while_stmt()); + } + + return new CompoundNode(ifStmt, funcDef, forStmt, whileStmt); + } + + /** + * Returns a `SimpleStmtNode`. It can be built by a different kind of + * statements, so only one of theme won't be null. + * + * ``` + * simple_stmt : assignment | expr | return_stmt | import_stm ; + * ``` + */ public Node visitSimple_stmt(Simple_stmtContext ctx) { Node assignment = null; Node expr = null; - Node importStmt = null; Node returnStmt = null; + Node importStmt = null; if (ctx.assignment() != null) { assignment = visit(ctx.assignment()); @@ -49,46 +109,139 @@ public Node visitSimple_stmt(Simple_stmtContext ctx) { expr = visit(ctx.expr()); } + if (ctx.return_stmt() != null) { + returnStmt = visit(ctx.return_stmt()); + } + if (ctx.import_stm() != null) { importStmt = visit(ctx.import_stm()); } - if (ctx.return_stmt() != null) { - returnStmt = visit(ctx.return_stmt()); - } + return new SimpleStmtNode(assignment, expr, returnStmt, importStmt); + } - return new SimpleStmtNode(assignment, expr, importStmt, returnStmt); + /** + * Returns an `AssignmentNode`. It's made by left side, an assignment and a + * right side. + * + * ``` + * assignment : exprlist augassign exprlist ; + * ``` + */ + public Node visitAssignment(AssignmentContext ctx) { + Node lhr = visit(ctx.exprlist(0)); + Node assign = visit(ctx.augassign()); + Node rhr = visit(ctx.exprlist(1)); + + return new AssignmentNode(lhr, assign, rhr); } - public Node visitCompound_stmt(Compound_stmtContext ctx) { - Node ifStmt = null; - if (ctx.if_stmt() != null) { - ifStmt = visit(ctx.if_stmt()); + /** + * Returns a `ReturnStmtNode`. The returned exprlist can be null. + * + * ``` + * return_stmt : 'return' exprlist? ; + * ``` + */ + public Node visitReturn_stmt(Return_stmtContext ctx) { + Node exprList = null; + if (ctx.exprlist() != null) { + exprList = visit(ctx.exprlist()); } - Node funcDef = null; - if (ctx.funcdef() != null) { - funcDef = visit(ctx.funcdef()); + return new ReturnStmtNode(exprList); + } + + /** + * Returns a `ImportNode`. An import can be made in different ways so we + * check the way in a module is imported (by from, by alias or by star). + * + * ``` + * import_stm : 'import' dotted_name ('as' NAME)? + * | 'from' dotted_name 'import' (NAME (',' NAME)* | '*') ; + * ``` + */ + public Node visitImport_stm(Import_stmContext ctx) { + boolean isFrom = ctx.FROM() != null; + boolean importAs = ctx.AS() != null; + boolean importAll = ctx.STAR() != null; + + Node dottedName = visit(ctx.dotted_name()); + + ArrayList names = new ArrayList(); + + for (var s : ctx.NAME()) { + names.add(s.toString()); } - Node forStmt = null; - if (ctx.for_stmt() != null) { - forStmt = visit(ctx.for_stmt()); + return new ImportNode(dottedName, isFrom, importAs, importAll, names); + } + + /** + * Returns a `DottedNameNode` used in `import_stm`. + * + * ``` + * dotted_name : NAME ('.' NAME)* ; + * ``` + */ + public Node visitDotted_name(Dotted_nameContext ctx) { + ArrayList names = new ArrayList(); + + for (var name : ctx.NAME()) { + names.add(name); } - Node whileStmt = null; - if (ctx.while_stmt() != null) { - whileStmt = visit(ctx.while_stmt()); + return new DottedNameNode(names); + } + + /** + * Returns a `FuncdefNode`. A paramlist can be null. + * + * ``` + * funcdef : 'def' NAME '(' paramlist? ')' ':' block ; + * ``` + */ + public Node visitFuncdef(FuncdefContext ctx) { + Node paramlist = null; + + if (ctx.paramlist() != null) { + paramlist = visit(ctx.paramlist()); } - return new CompoundNode(ifStmt, funcDef, forStmt, whileStmt); + + Node block = visit(ctx.block()); + + return new FuncdefNode(ctx.NAME(), paramlist, block); } - public Node visitAssignment(AssignmentContext ctx) { - Node lhr = visit(ctx.exprlist(0)); - Node assign = visit(ctx.augassign()); - Node rhr = visit(ctx.exprlist(1)); + /** + * Returns a `ParamlistNode`. We ignore the paramdef with default values + * (eg: is_used=False) because there is no test which uses this feature. + * + * ``` + * paramlist : paramdef ('=' expr)? (',' paramdef ('=' expr)?)* ; + * + * ``` + */ + public Node visitParamlist(ParamlistContext ctx) { + ArrayList params = new ArrayList(); - return new AssignmentNode(lhr, assign, rhr); + for (ParamdefContext s : ctx.paramdef()) { + params.add(visit(s)); + } + + return new ParamlistNode(params); + } + + /** + * Returns a `ParamdefNode`. We ignore the paramdef with type annotation + * (eg: x : int) because there is no test which uses this feature. + * + * ``` + * paramdef : NAME (':' expr)? ; + * ``` + */ + public Node visitParamdef(ParamdefContext ctx) { + return new ParamdefNode(ctx.NAME().toString()); } public Node visitExprlist(ExprlistContext ctx) { @@ -251,36 +404,6 @@ public Node visitArglist(ArglistContext ctx) { return new ArglistNode(arguments); } - public Node visitFuncdef(FuncdefContext ctx) { - Node paramlist = visit(ctx.paramlist()); - Node block = visit(ctx.block()); - - return new FuncdefNode(ctx.NAME(), paramlist, block); - } - - public Node visitParamlist(ParamlistContext ctx) { - ArrayList params = new ArrayList(); - - for (ParamdefContext s : ctx.paramdef()) { - params.add(visit(s)); - } - - return new ParamlistNode(params); - } - - public Node visitParamdef(ParamdefContext ctx) { - return new ParamdefNode(ctx.NAME().toString()); - } - - public Node visitReturn_stmt(Return_stmtContext ctx) { - Node exprList = null; - if (ctx.exprlist() != null) { - exprList = visit(ctx.exprlist()); - } - - return new ReturnStmtNode(exprList); - } - public Node visitFor_stmt(For_stmtContext ctx) { Node exprList = visit(ctx.exprlist()); @@ -299,29 +422,4 @@ public Node visitWhile_stmt(While_stmtContext ctx) { return new WhileStmtNode(expr, block); } - public Node visitImport_stm(Import_stmContext ctx) { - boolean isFrom = ctx.FROM() != null; - boolean importAs = ctx.AS() != null; - boolean importAll = ctx.STAR() != null; - - Node dottedName = visit(ctx.dotted_name()); - - ArrayList names = new ArrayList(); - - for (var s : ctx.NAME()) { - names.add(s.toString()); - } - - return new ImportNode(dottedName, isFrom, importAs, importAll, names); - } - - public Node visitDotted_name(Dotted_nameContext ctx) { - ArrayList names = new ArrayList(); - - for (var name : ctx.NAME()) { - names.add(name); - } - - return new DottedNameNode(names); - } } diff --git a/src/ast/nodes/AssignmentNode.java b/src/ast/nodes/AssignmentNode.java index 8d3bb51..d722f41 100644 --- a/src/ast/nodes/AssignmentNode.java +++ b/src/ast/nodes/AssignmentNode.java @@ -6,15 +6,18 @@ import com.clp.project.semanticanalysis.SymbolTable; import com.clp.project.ast.types.*; +/** + * Node for the `assignment` statement of the grammar. + */ public class AssignmentNode implements Node { private Node lhr; private Node assign; private Node rhr; - public AssignmentNode(Node _lhr, Node _assign, Node _rhr) { - lhr = _lhr; - assign = _assign; - rhr = _rhr; + public AssignmentNode(Node lhr, Node assign, Node rhr) { + this.lhr = lhr; + this.assign = assign; + this.rhr = rhr; } @Override @@ -28,16 +31,22 @@ public ArrayList checkSemantics(SymbolTable ST, int _nesting) { return errors; } + // TODO: check it out for this type + @Override public Type typeCheck() { return rhr.typeCheck(); } + // TODO: add code generation for assignment + @Override public String codeGeneration() { return ""; } - public String toPrint(String s) { - return s + "Assignment\n" + lhr.toPrint(s + " ") + assign.toPrint(s + " ") + rhr.toPrint(s + " "); + @Override + public String toPrint(String prefix) { + return prefix + "Assignment\n" + lhr.toPrint(prefix + " ") + assign.toPrint(prefix + " ") + + rhr.toPrint(prefix + " "); } } diff --git a/src/ast/nodes/CompoundNode.java b/src/ast/nodes/CompoundNode.java index 256eeae..bfa0523 100644 --- a/src/ast/nodes/CompoundNode.java +++ b/src/ast/nodes/CompoundNode.java @@ -6,6 +6,9 @@ import com.clp.project.semanticanalysis.SymbolTable; import com.clp.project.ast.types.*; +/** + * Node for the `compound_node` statement of the grammar. + */ public class CompoundNode implements Node { private Node ifNode; private Node funcDef; @@ -42,34 +45,40 @@ public ArrayList checkSemantics(SymbolTable ST, int _nesting) { return errors; } + @Override public Type typeCheck() { return new VoidType(); } + // TODO: add code generation for CompoundNode + @Override public String codeGeneration() { return ""; } - public String toPrint(String s) { - String result = s + "CompoundNode\n"; + @Override + public String toPrint(String prefix) { + String str = prefix + "CompoundNode\n"; + + prefix += " "; if (ifNode != null) { - result += ifNode.toPrint(s + " "); + str += ifNode.toPrint(prefix); } if (funcDef != null) { - result += funcDef.toPrint(s + " "); + str += funcDef.toPrint(prefix); } if (forStmt != null) { - result += forStmt.toPrint(s + " "); + str += forStmt.toPrint(prefix); } if (whileStmt != null) { - result += whileStmt.toPrint(s + " "); + str += whileStmt.toPrint(prefix); } - return result; + return str; } } diff --git a/src/ast/nodes/DottedNameNode.java b/src/ast/nodes/DottedNameNode.java index f0102d1..bc573b5 100644 --- a/src/ast/nodes/DottedNameNode.java +++ b/src/ast/nodes/DottedNameNode.java @@ -7,6 +7,9 @@ import com.clp.project.ast.types.*; import org.antlr.v4.runtime.tree.TerminalNode; +/** + * Node for the `dooted_name` statement of the grammar. + */ public class DottedNameNode implements Node { protected ArrayList names; @@ -21,22 +24,28 @@ public ArrayList checkSemantics(SymbolTable ST, int _nesting) { return errors; } + @Override public Type typeCheck() { return new VoidType(); } + // NOTE: we do not provide code generation for this node in the same way + // we do not want to do this for the import stm. + @Override public String codeGeneration() { return ""; } - public String toPrint(String s) { - String result = s + "DottedName\n"; + @Override + public String toPrint(String prexif) { + String str = prefix + "DottedName\n"; + prefix += " "; for (var name : names) { - result += s + " " + name.toString() + "\n"; + str += prefix + name.toString() + "\n"; } - return result; + return str; } } diff --git a/src/ast/nodes/FuncdefNode.java b/src/ast/nodes/FuncdefNode.java index 3dbf581..c48eb1c 100644 --- a/src/ast/nodes/FuncdefNode.java +++ b/src/ast/nodes/FuncdefNode.java @@ -7,45 +7,57 @@ import com.clp.project.ast.types.*; import org.antlr.v4.runtime.tree.TerminalNode; +/** + * Node for the `funcdef` statement of the grammar. + */ public class FuncdefNode implements Node { private TerminalNode name; private Node paramlist; private Node block; - public FuncdefNode(TerminalNode _name, Node _paramlist, Node _block) { - name = _name; - paramlist = _paramlist; - block = _block; + public FuncdefNode(TerminalNode name, Node paramlist, Node block) { + this.name = name; + this.paramlist = paramlist; + this.block = block; } @Override public ArrayList checkSemantics(SymbolTable ST, int _nesting) { ArrayList errors = new ArrayList(); - errors.addAll(paramlist.checkSemantics(ST, _nesting)); + if (paramlist != null) { + errors.addAll(paramlist.checkSemantics(ST, _nesting)); + } + errors.addAll(block.checkSemantics(ST, _nesting)); return errors; } + // FIXME: this type must be the same of the return stmt variable + @Override public Type typeCheck() { return new VoidType(); } + // TODO: code generation for funcdef + @Override public String codeGeneration() { return ""; } - public String toPrint(String s) { - String result = s + "Funcdef(" + name + ")\n"; + public String toPrint(String prefix) { + String str = prefix + "Funcdef(" + name + ")\n"; + + prefix += " "; if (paramlist != null) { - result += paramlist.toPrint(s + " "); + str += paramlist.toPrint(prefix); } - result += block.toPrint(s + " "); + str += block.toPrint(prefix); - return result; + return str; } } diff --git a/src/ast/nodes/ImportNode.java b/src/ast/nodes/ImportNode.java index fc5e1a6..e6ac8c7 100644 --- a/src/ast/nodes/ImportNode.java +++ b/src/ast/nodes/ImportNode.java @@ -6,6 +6,9 @@ import com.clp.project.semanticanalysis.SymbolTable; import com.clp.project.ast.types.*; +/** + * Node for the `import_stmt` statement of the grammar. + */ public class ImportNode implements Node { private Node dottedName; private boolean isFrom; @@ -29,40 +32,45 @@ public ArrayList checkSemantics(SymbolTable ST, int _nesting) { return errors; } + @Override public Type typeCheck() { return new VoidType(); } + // NOTE: we do not want to provide a code generation for this statement + @Override public String codeGeneration() { return ""; } - public String toPrint(String s) { - String result = s + "Import\n"; + @Override + public String toPrint(String prefix) { + String str = prefix + "Import\n"; + prefix += " "; if (isFrom) { - result += s + " From\n" + dottedName.toPrint(s + " "); + str += prefix + " From\n" + dottedName.toPrint(prefix + " "); } else { - result += dottedName.toPrint(s + " "); + str += dottedName.toPrint(prefix); } if (importAs) { - result += s + " As " + names.get(0) + "\n"; + str += prefix + " As " + names.get(0) + "\n"; } if (importAll) { - result += s + " All\n"; + str += prefix + " All\n"; } for (int i = 0; i < names.size(); ++i) { if (i == 0 && importAs) continue; - result += s + " " + names.get(i) + "\n"; + str += prefix + names.get(i) + "\n"; } - result += "\n"; - return result; + str += "\n"; + return str; } } diff --git a/src/ast/nodes/Node.java b/src/ast/nodes/Node.java index 58c7ea2..f807745 100644 --- a/src/ast/nodes/Node.java +++ b/src/ast/nodes/Node.java @@ -6,13 +6,31 @@ import com.clp.project.semanticanalysis.SymbolTable; import com.clp.project.ast.types.*; +/** + * Base interface for a Node. + */ public interface Node { - + /** + * Checks semantics for a given node for a SymbolTable ST and a level of + * nesting. + * Returns a list of `SemanticError`. + */ ArrayList checkSemantics(SymbolTable ST, int _nesting); + /** + * Checks the type for a given node. If there's any error, returns an + * `ErrorType`. + */ Type typeCheck(); + /** + * Returns a string for the Python Virtual Machine. + */ String codeGeneration(); - String toPrint(String s); + /** + * Returns a string for a given node with a prefix. + * It used when an AST wants to be visualized on screen. + */ + String toPrint(String prefix); } diff --git a/src/ast/nodes/ParamdefNode.java b/src/ast/nodes/ParamdefNode.java index 3a34b36..d8e04f7 100644 --- a/src/ast/nodes/ParamdefNode.java +++ b/src/ast/nodes/ParamdefNode.java @@ -4,15 +4,26 @@ import com.clp.project.semanticanalysis.SemanticError; import com.clp.project.semanticanalysis.SymbolTable; + import com.clp.project.ast.types.*; +/** + * Node for the `paramdef` statement of the grammar. Extends the `AtomNode` + * class. + */ public class ParamdefNode extends AtomNode implements Node { public ParamdefNode(String val) { super(val); } + // FIXME: it should returns the param' type + @Override + public Type typeCheck() { + return new VoidType(); + } + @Override - public String toPrint(String s) { - return s + "Paramdef(" + val + ")\n"; + public String toPrint(String prefix) { + return prefix + "Paramdef(" + val + ")\n"; } } diff --git a/src/ast/nodes/ParamlistNode.java b/src/ast/nodes/ParamlistNode.java index c0e8d4b..d4f40b8 100644 --- a/src/ast/nodes/ParamlistNode.java +++ b/src/ast/nodes/ParamlistNode.java @@ -6,6 +6,9 @@ import com.clp.project.semanticanalysis.SymbolTable; import com.clp.project.ast.types.*; +/** + * Node for the `param_list` statement of the grammar. + */ public class ParamlistNode implements Node { private ArrayList params; @@ -24,23 +27,27 @@ public ArrayList checkSemantics(SymbolTable ST, int _nesting) { return errors; } + @Override public Type typeCheck() { - // FIXME: wtf is that? return new VoidType(); } + // TODO: code generation for param list + @Override public String codeGeneration() { return ""; } - public String toPrint(String s) { - String result = s + "Paramlist\n"; + @Override + public String toPrint(String prefix) { + String str = prefix + "Paramlist\n"; + prefix += " "; for (var param : params) { - result += param.toPrint(s + " "); + str += param.toPrint(prefix); } - return result; + return str; } } diff --git a/src/ast/nodes/ReturnStmtNode.java b/src/ast/nodes/ReturnStmtNode.java index f9a82db..0cfd2b4 100644 --- a/src/ast/nodes/ReturnStmtNode.java +++ b/src/ast/nodes/ReturnStmtNode.java @@ -6,6 +6,9 @@ import com.clp.project.semanticanalysis.SymbolTable; import com.clp.project.ast.types.*; +/** + * Node for the `return_stmt` statement of the grammar. + */ public class ReturnStmtNode implements Node { private Node exprList; @@ -24,23 +27,31 @@ public ArrayList checkSemantics(SymbolTable ST, int _nesting) { return errors; } + @Override public Type typeCheck() { - // FIXME: wtf is that? + if (this.exprList != null) { + return this.exprList.typeCheck(); + } + return new VoidType(); } + // TODO: add code generation for return stmt + @Override public String codeGeneration() { return ""; } - public String toPrint(String s) { - String result = s + "ReturnStmt\n"; + @Override + public String toPrint(String prefix) { + String str = prefix + "ReturnStmt\n"; + prefix += " "; if (this.exprList != null) { - result += this.exprList.toPrint(s + " "); + str += this.exprList.toPrint(prefix); } - return result; + return str; } } diff --git a/src/ast/nodes/RootNode.java b/src/ast/nodes/RootNode.java index 4c57a81..12d2f22 100644 --- a/src/ast/nodes/RootNode.java +++ b/src/ast/nodes/RootNode.java @@ -6,7 +6,12 @@ import com.clp.project.semanticanalysis.SymbolTable; import com.clp.project.ast.types.*; +/** + * Node for the `root` statement of the grammar. + */ public class RootNode implements Node { + // stms and compundStmts are protected because they are reused for a + // BlockNode protected ArrayList stmts; protected ArrayList compoundStmts; @@ -29,25 +34,31 @@ public ArrayList checkSemantics(SymbolTable ST, int _nesting) { return errors; } + @Override public Type typeCheck() { return new VoidType(); } + // TODO: Code generation for RootNode + @Override public String codeGeneration() { return ""; } - public String toPrint(String s) { - String result = "Root\n"; + @Override + public String toPrint(String prefix) { + String str = "Root\n"; + + prefix += " "; for (Node stmt : stmts) { - result += stmt.toPrint(s + " "); + str += stmt.toPrint(prefix); } for (Node stmt : compoundStmts) { - result += stmt.toPrint(s + " "); + str += stmt.toPrint(prefix); } - return result; + return str; } } diff --git a/src/ast/nodes/SimpleStmtNode.java b/src/ast/nodes/SimpleStmtNode.java index 79bb12d..3f4dec3 100644 --- a/src/ast/nodes/SimpleStmtNode.java +++ b/src/ast/nodes/SimpleStmtNode.java @@ -6,17 +6,20 @@ import com.clp.project.semanticanalysis.SymbolTable; import com.clp.project.ast.types.*; +/** + * Node for the `simple_stmt` statement of the grammar. + */ public class SimpleStmtNode implements Node { private Node assignment; private Node expr; - private Node importStmt; private Node returnStmt; + private Node importStmt; - public SimpleStmtNode(Node assignment, Node expr, Node importStmt, Node returnStmt) { + public SimpleStmtNode(Node assignment, Node expr, Node returnStmt, Node importStmt) { this.assignment = assignment; this.expr = expr; - this.importStmt = importStmt; this.returnStmt = returnStmt; + this.importStmt = importStmt; } @Override @@ -31,45 +34,51 @@ public ArrayList checkSemantics(SymbolTable ST, int _nesting) { errors.addAll(expr.checkSemantics(ST, _nesting)); } - if (importStmt != null) { - errors.addAll(importStmt.checkSemantics(ST, _nesting)); - } - if (returnStmt != null) { errors.addAll(returnStmt.checkSemantics(ST, _nesting)); } + if (importStmt != null) { + errors.addAll(importStmt.checkSemantics(ST, _nesting)); + } + return errors; } + @Override public Type typeCheck() { return new VoidType(); } + // TODO: add code generation for SimpleStmtNode + @Override public String codeGeneration() { return ""; } - public String toPrint(String s) { - String result = s + "SimpleStmt\n"; + @Override + public String toPrint(String prefix) { + String str = prefix + "SimpleStmt\n"; + + prefix += " "; if (assignment != null) { - result += assignment.toPrint(s + " "); + str += assignment.toPrint(prefix); } if (expr != null) { - result += expr.toPrint(s + " "); + str += expr.toPrint(prefix); } - if (importStmt != null) { - result += importStmt.toPrint(s + " "); + if (returnStmt != null) { + str += returnStmt.toPrint(prefix); } - if (returnStmt != null) { - result += returnStmt.toPrint(s + " "); + if (importStmt != null) { + str += importStmt.toPrint(prefix); } - return result; + return str; } } diff --git a/src/ast/nodes/SimpleStmtsNode.java b/src/ast/nodes/SimpleStmtsNode.java index d2e3fde..c8013a3 100644 --- a/src/ast/nodes/SimpleStmtsNode.java +++ b/src/ast/nodes/SimpleStmtsNode.java @@ -6,11 +6,14 @@ import com.clp.project.semanticanalysis.SymbolTable; import com.clp.project.ast.types.*; +/** + * Node for the `simple_stmts` statement of the grammar. + */ public class SimpleStmtsNode implements Node { private ArrayList stmts; - public SimpleStmtsNode(ArrayList _stmts) { - stmts = _stmts; + public SimpleStmtsNode(ArrayList stmts) { + this.stmts = stmts; } @Override @@ -24,21 +27,28 @@ public ArrayList checkSemantics(SymbolTable ST, int _nesting) { return errors; } + @Override public Type typeCheck() { return new VoidType(); } + // TODO: Code generation for SimpleStmtsNode + @Override public String codeGeneration() { return ""; } - public String toPrint(String s) { - String result = s + "SimpleStmts\n"; + @Override + public String toPrint(String prefix) { + String str = prefix + "SimpleStmts\n"; + + prefix += " "; + for (Node stmt : stmts) { - result += stmt.toPrint(s + " "); + str += stmt.toPrint(prefix); } - return result; + return str; } } diff --git a/src/semanticanalysis/STentry.java b/src/semanticanalysis/STentry.java index 39279e3..42990fa 100644 --- a/src/semanticanalysis/STentry.java +++ b/src/semanticanalysis/STentry.java @@ -1,40 +1,54 @@ - package com.clp.project.semanticanalysis; import com.clp.project.ast.types.Type; +/** + * Entry class for the symbol table. + */ public class STentry { private Type type; private int offset; private int nesting; private String label; - public STentry(Type _type, int _offset, int _nesting) { - type = _type; - offset = _offset; - nesting = _nesting; + public STentry(Type type, int offset, int nesting) { + this.type = type; + this.offset = offset; + this.nesting = nesting; } - public STentry(Type _type, int _offset, int _nesting, String _label) { - type = _type; - offset = _offset; - nesting = _nesting; - label = _label; + public STentry(Type type, int offset, int nesting, String label) { + this.type = type; + this.offset = offset; + this.nesting = nesting; + this.label = label; } - public Type gettype() { + /** + * Getter for `type` + */ + public Type getType() { return type; } - public int getoffset() { + /** + * Getter for `offset` + */ + public int getOffset() { return offset; } - public int getnesting() { + /** + * Getter for `nesting` + */ + public int getNesting() { return nesting; } - public String getlabel() { + /** + * Getter for `label` + */ + public String getLabel() { return label; } diff --git a/src/semanticanalysis/SemanticError.java b/src/semanticanalysis/SemanticError.java index d2df7cf..1df1ce9 100644 --- a/src/semanticanalysis/SemanticError.java +++ b/src/semanticanalysis/SemanticError.java @@ -1,10 +1,13 @@ package com.clp.project.semanticanalysis; +/** + * Class respresents a semantic error. + */ public class SemanticError { private String msg; - public SemanticError(String _msg) { - msg = _msg; + public SemanticError(String msg) { + this.msg = msg; } public String toString() { diff --git a/src/semanticanalysis/SymbolTable.java b/src/semanticanalysis/SymbolTable.java index 6ba711b..1997710 100644 --- a/src/semanticanalysis/SymbolTable.java +++ b/src/semanticanalysis/SymbolTable.java @@ -5,25 +5,37 @@ import com.clp.project.ast.*; import com.clp.project.ast.types.*; +/** + * Class representing a symbol table. It's a list of hash table symbol table. + * We keep track of a ArrayList of HashMap called `symbolTable` and an array of + * integer called `offset`. + */ public class SymbolTable { - private ArrayList> symbol_table; + private ArrayList> symbolTable; private ArrayList offset; public SymbolTable() { - symbol_table = new ArrayList>(); - offset = new ArrayList(); + this.symbolTable = new ArrayList>(); + this.offset = new ArrayList(); } + /** + * Returns the nesting level. + */ public Integer nesting() { - return symbol_table.size() - 1; + return this.symbolTable.size() - 1; } + /** + * Check out if an `id` is into the symbol table. + * Returns an STentry object or null if the `id` is not found. + */ public STentry lookup(String id) { - int n = symbol_table.size() - 1; + int n = this.symbolTable.size() - 1; boolean found = false; STentry T = null; while ((n >= 0) && !found) { - HashMap H = symbol_table.get(n); + HashMap H = this.symbolTable.get(n); T = H.get(id); if (T != null) found = true; @@ -33,11 +45,12 @@ public STentry lookup(String id) { return T; } + // TODO: add doc public Integer nslookup(String id) { - int n = symbol_table.size() - 1; + int n = this.symbolTable.size() - 1; boolean found = false; while ((n >= 0) && !found) { - HashMap H = symbol_table.get(n); + HashMap H = this.symbolTable.get(n); if (H.get(id) != null) found = true; else @@ -46,50 +59,80 @@ public Integer nslookup(String id) { return n; } + /** + * Add an hashmap to the given symbol table and increase the offset level. + * We start from 2 because we have FP and AL before all. + * + * @param H is an hashmap that is must be added into the symbol table + */ public void add(HashMap H) { - symbol_table.add(H); - offset.add(1); // si inizia da 2 perche` prima ci sonop FP e AL + this.symbolTable.add(H); + this.offset.add(1); } + /** + * Remove the last level for the symbol table. + */ public void remove() { - int x = symbol_table.size(); - symbol_table.remove(x - 1); - offset.remove(x - 1); + int x = this.symbolTable.size(); + this.symbolTable.remove(x - 1); + this.offset.remove(x - 1); } + // TODO: add doc public boolean top_lookup(String id) { - int n = symbol_table.size() - 1; + int n = symbolTable.size() - 1; STentry T = null; - HashMap H = symbol_table.get(n); + HashMap H = symbolTable.get(n); T = H.get(id); return (T != null); } + /** + * Insert a new entry into the symbol table. + * + * @param id + * @param type + * @param _nesting + * @param _label + */ public void insert(String id, Type type, int _nesting, String _label) { - int n = symbol_table.size() - 1; - HashMap H = symbol_table.get(n); - symbol_table.remove(n); - int offs = offset.get(n); - offset.remove(n); + int n = symbolTable.size() - 1; + HashMap H = this.symbolTable.get(n); + this.symbolTable.remove(n); + + int offs = this.offset.get(n); + this.offset.remove(n); + STentry idtype = new STentry(type, offs, _nesting, _label); H.put(id, idtype); - symbol_table.add(H); - if (type.getClass().equals((new BoolType()).getClass())) - offs = offs + 1; // we always increment the offset by 1 otherwise we need ad-hoc - // bytecode operations - else if (type.getClass().equals((new IntType()).getClass())) + + this.symbolTable.add(H); + + // We always increment the offset by 1 otherwise we need ad-hoc bytecode + // operations + if (type.getClass().equals((new BoolType()).getClass())) { + offs = offs + 1; + } else if (type.getClass().equals((new IntType()).getClass())) { offs = offs + 1; - else + } else { offs = offs + 1; - offset.add(offs); + } + + this.offset.add(offs); } + /** + * Increase the offset level. + */ public void increaseoffset() { - int n = offset.size() - 1; - int offs = offset.get(n); - offset.remove(n); + int n = this.offset.size() - 1; + int offs = this.offset.get(n); + this.offset.remove(n); + offs = offs + 1; - offset.add(offs); + + this.offset.add(offs); } } From 2b54ae4705cf78eaa5092d7af36e21304524367b Mon Sep 17 00:00:00 2001 From: geno Date: Fri, 7 Jun 2024 11:37:21 +0200 Subject: [PATCH 18/23] fix typo --- src/ast/nodes/DottedNameNode.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ast/nodes/DottedNameNode.java b/src/ast/nodes/DottedNameNode.java index bc573b5..342950a 100644 --- a/src/ast/nodes/DottedNameNode.java +++ b/src/ast/nodes/DottedNameNode.java @@ -37,7 +37,7 @@ public String codeGeneration() { } @Override - public String toPrint(String prexif) { + public String toPrint(String prefix) { String str = prefix + "DottedName\n"; prefix += " "; From e82531c0d017b81ba745f88aacb2fc8b2c682547 Mon Sep 17 00:00:00 2001 From: geno Date: Fri, 7 Jun 2024 12:01:00 +0200 Subject: [PATCH 19/23] some docs added --- README.md | 3 +++ src/Main.java | 14 +++------- src/semanticanalysis/SymbolTable.java | 39 ++++++++++++++++++--------- 3 files changed, 34 insertions(+), 22 deletions(-) diff --git a/README.md b/README.md index 3c7e80b..d8b2780 100644 --- a/README.md +++ b/README.md @@ -6,5 +6,8 @@ Project for the AA 2023/2024 **Complementi di programmazione**'s course of the M ```shell make +make run make runall ``` + +For archlinux users, add the following env variable to visualize the window: `export _JAVA_AWT_WM_NONREPARENTING=1` diff --git a/src/Main.java b/src/Main.java index 05f2d01..c672e19 100644 --- a/src/Main.java +++ b/src/Main.java @@ -18,22 +18,21 @@ import com.clp.project.semanticanalysis.*; public class Main { + public static void main(String[] args) { + // for (File file : Objects.requireNonNull(new File("./progs/").listFiles())) { try { // String fileStr = file.getPath(); // FIXME: use the fileStr above String fileStr = "./progs/test.py"; System.out.println(fileStr); - System.out.println(readFile(fileStr)); - CharStream cs = CharStreams.fromFileName(fileStr); Python3Lexer lexer = new Python3Lexer(cs); CommonTokenStream tokens = new CommonTokenStream(lexer); Python3Parser parser = new Python3Parser(tokens); Python3Parser.RootContext tree = parser.root(); - // DEBUG // { // tokens.fill(); @@ -43,7 +42,6 @@ public static void main(String[] args) { // // System.out.println("Tree: " + tree); // } - JFrame frame = new JFrame("Parse Tree"); JPanel panel = new JPanel(); TreeViewer viewer = new TreeViewer(Arrays.asList(parser.getRuleNames()), @@ -54,31 +52,27 @@ public static void main(String[] args) { frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(800, 600); frame.setVisible(true); - if (tree == null) { System.err.println("The tree is null."); return; } - if (parser.getNumberOfSyntaxErrors() > 0) { System.err.println("Error on program parsing."); return; } - Python3VisitorImpl visitor = new Python3VisitorImpl(); SymbolTable ST = new SymbolTable(); Node ast = visitor.visit(tree); - ArrayList errors = ast.checkSemantics(ST, 0); if (errors.size() > 0) { System.out.println("You had: " + errors.size() + " errors:"); - for (SemanticError e : errors) + for (SemanticError e : errors) { System.out.println("\t" + e); + } } else { System.out.println("Visualizing AST..."); System.out.println(ast.toPrint("")); } - } catch (Exception e) { e.printStackTrace(); } diff --git a/src/semanticanalysis/SymbolTable.java b/src/semanticanalysis/SymbolTable.java index 1997710..d4fda37 100644 --- a/src/semanticanalysis/SymbolTable.java +++ b/src/semanticanalysis/SymbolTable.java @@ -6,11 +6,12 @@ import com.clp.project.ast.types.*; /** - * Class representing a symbol table. It's a list of hash table symbol table. - * We keep track of a ArrayList of HashMap called `symbolTable` and an array of + * Class representing a symbol table. It's a list of hash table symbol table. We + * keep track of a ArrayList of HashMap called `symbolTable` and an array of * integer called `offset`. */ public class SymbolTable { + private ArrayList> symbolTable; private ArrayList offset; @@ -27,8 +28,10 @@ public Integer nesting() { } /** - * Check out if an `id` is into the symbol table. - * Returns an STentry object or null if the `id` is not found. + * Check out if an `id` is into the symbol table. Returns an STentry object + * or null if the `id` is not found. + * + * @param id is the identifier of the STentry to find. */ public STentry lookup(String id) { int n = this.symbolTable.size() - 1; @@ -37,24 +40,31 @@ public STentry lookup(String id) { while ((n >= 0) && !found) { HashMap H = this.symbolTable.get(n); T = H.get(id); - if (T != null) - found = true; - else + if (T != null) { + found = true; + }else { n = n - 1; + } } return T; } - // TODO: add doc + /** + * Return the position of a STentry given the `id`, if it exists. Otherwise + * return `-1`. We start the search from the last inserted hashmap. + * + * @param id is the identifier of the STentry to find. + */ public Integer nslookup(String id) { int n = this.symbolTable.size() - 1; boolean found = false; while ((n >= 0) && !found) { HashMap H = this.symbolTable.get(n); - if (H.get(id) != null) - found = true; - else + if (H.get(id) != null) { + found = true; + }else { n = n - 1; + } } return n; } @@ -79,7 +89,12 @@ public void remove() { this.offset.remove(x - 1); } - // TODO: add doc + /** + * Return `true` if the `id` is present in the last inseted hashmap. + * Otherwise return `false`. + * + * @param id is the identifier of the STentry to find. + */ public boolean top_lookup(String id) { int n = symbolTable.size() - 1; STentry T = null; From 12bde8f72ea050a62eabbc311fcc9210458f0cf9 Mon Sep 17 00:00:00 2001 From: Santo Cariotti Date: Fri, 7 Jun 2024 19:04:28 +0200 Subject: [PATCH 20/23] wip --- src/ast/Python3VisitorImpl.java | 242 +++++++++++++++++++++---------- src/ast/nodes/AtomNode.java | 19 ++- src/ast/nodes/AugassignNode.java | 20 ++- src/ast/nodes/BlockNode.java | 24 ++- src/ast/nodes/CompNode.java | 21 ++- src/ast/nodes/ExprNode.java | 26 ++-- src/ast/nodes/ForStmtNode.java | 11 +- src/ast/nodes/IfNode.java | 24 ++- src/ast/nodes/WhileStmtNode.java | 12 +- 9 files changed, 274 insertions(+), 125 deletions(-) diff --git a/src/ast/Python3VisitorImpl.java b/src/ast/Python3VisitorImpl.java index 87f8bc1..a189666 100644 --- a/src/ast/Python3VisitorImpl.java +++ b/src/ast/Python3VisitorImpl.java @@ -244,13 +244,155 @@ public Node visitParamdef(ParamdefContext ctx) { return new ParamdefNode(ctx.NAME().toString()); } - public Node visitExprlist(ExprlistContext ctx) { - // FIXME: you've used to be a list, c'mon - Node exp = visit(ctx.expr(0)); + /** + * Returns an `AugassignNode`. We don't provide all kinds of assignment + * below. + * + * ``` + * augassign : '=' | '+=' | '-=' | '*=' | '@=' | '/=' | '%=' | '&=' | '|=' | + * '^=' | '<<=' | '>>=' | '**=' | '//=' + * ; + * ``` + */ + public Node visitAugassign(AugassignContext ctx) { + Node x = null; - return exp; + if (ctx.ASSIGN() != null) { + x = new AugassignNode(ctx.ASSIGN()); + } else if (ctx.ADD_ASSIGN() != null) { + x = new AugassignNode(ctx.ADD_ASSIGN()); + } else if (ctx.SUB_ASSIGN() != null) { + x = new AugassignNode(ctx.SUB_ASSIGN()); + } else if (ctx.MULT_ASSIGN() != null) { + x = new AugassignNode(ctx.MULT_ASSIGN()); + } else if (ctx.DIV_ASSIGN() != null) { + x = new AugassignNode(ctx.DIV_ASSIGN()); + } + + return x; + } + + /** + * Returns a `IfNode`. + * FIXME: add support for elif statement. + * + * ``` + * if_stmt : 'if' expr ':' block ('elif' expr ':' block)* ('else' ':' block)? ; + * ``` + */ + public Node visitIf_stmt(If_stmtContext ctx) { + var blocks = ctx.block(); + Node condExp = visit(ctx.expr(0)); + Node thenExp = visit(blocks.get(0)); + Node elseExp = null; + if (blocks.size() > 1) { + elseExp = visit(blocks.get(1)); + } + + return new IfNode(condExp, thenExp, elseExp); + } + + /** + * Returns a `WhileStmtNode`. We do not provide 'else' branch. + * + * ``` + * while_stmt : 'while' expr ':' block ('else' ':' block)? ; + * ``` + */ + public Node visitWhile_stmt(While_stmtContext ctx) { + Node expr = visit(ctx.expr()); + + // Block 1 is for the while-else statement + Node block = visit(ctx.block(0)); + + return new WhileStmtNode(expr, block); + } + + /** + * Returns a `ForSmtNode`. We do not provide 'else' branch. + * + * ``` + * for_stmt : 'for' exprlist ':' block ('else' ':' block)? ; + * ``` + */ + public Node visitFor_stmt(For_stmtContext ctx) { + Node exprList = visit(ctx.exprlist()); + + // Block 1 is for the for-else statement + Node block = visit(ctx.block(0)); + + return new ForStmtNode(exprList, block); + } + + /** + * Returns a `BlockNode`. A block can be be a simple_stmts or a list of + * simple_stms and/or compound_stmt, so we just use a list for each kind. + * + * ``` + * block : simple_stmts + * | NEWLINE INDENT (simple_stmts | compound_stmt)+ DEDENT ; + * ``` + */ + public Node visitBlock(BlockContext ctx) { + ArrayList stmts = new ArrayList(); + ArrayList compStmts = new ArrayList(); + + for (Simple_stmtsContext s : ctx.simple_stmts()) { + stmts.add(visit(s)); + } + for (Compound_stmtContext s : ctx.compound_stmt()) { + compStmts.add(visit(s)); + } + + return new BlockNode(stmts, compStmts); + } + + /** + * Returns a `CompNode`. It should never be null. + * + * ``` + * comp_op : '<' | '>' | '==' | '>=' | '<=' | '<>' | '!=' | 'in' | 'not' 'in' | + * 'is' | 'is' 'not' ; + * ``` + */ + public Node visitComp_op(Comp_opContext ctx) { + Node comp = null; + if (ctx.LESS_THAN() != null) { + comp = new CompNode(ctx.LESS_THAN()); + } else if (ctx.GREATER_THAN() != null) { + comp = new CompNode(ctx.GREATER_THAN()); + } else if (ctx.EQUALS() != null) { + comp = new CompNode(ctx.EQUALS()); + } else if (ctx.GT_EQ() != null) { + comp = new CompNode(ctx.GT_EQ()); + } else if (ctx.LT_EQ() != null) { + comp = new CompNode(ctx.LT_EQ()); + } else if (ctx.NOT_EQ_2() != null) { + // We're ignoring NOT_EQ_1() because no one uses `<>` + comp = new CompNode(ctx.NOT_EQ_2()); + } else if (ctx.IN() != null) { + comp = new CompNode(ctx.IN()); + } else if (ctx.NOT() != null) { + comp = new CompNode(ctx.NOT()); + } else if (ctx.IS() != null) { + comp = new CompNode(ctx.IS()); + } + + return comp; } + /** + * Returns an `ExprNode`. An expession can be a different kind of + * sub-expression. We do not provide all kinds of expr(s). + * + * ``` + * expr : atom trailer* | expr '**' expr | ('+' | '-' | '~')+ expr | expr ('*' | + * '@' | '/' | '%' | '//') expr | expr ('+' | '-') expr | expr ('<<' | '>>') + * expr | expr '&' expr | expr '^' expr | expr '|' expr | 'not' expr | expr + * comp_op expr | expr 'and' expr | expr 'or' expr | expr 'if' expr 'else' expr + * ; + * ``` + */ public Node visitExpr(ExprContext ctx) { Node atom = null; Node compOp = null; @@ -297,6 +439,15 @@ public Node visitExpr(ExprContext ctx) { return new ExprNode(atom, compOp, exprs, op, trailers); } + /** + * Returns an `AtomNode`. + * FIXME: add support for testlist_comp + * + * ``` + * atom : '(' testlist_comp? ')' | '[' testlist_comp? ']' | '{' testlist_comp? + * '}' | NAME | NUMBER | STRING+ | '...' | 'None' | 'True' | 'False' ; + * ``` + */ public Node visitAtom(AtomContext ctx) { if (ctx.NUMBER() != null) { return new AtomNode(ctx.NUMBER().toString()); @@ -318,63 +469,6 @@ public Node visitAtom(AtomContext ctx) { return new AtomNode(ctx.NONE().toString()); } - public Node visitAugassign(AugassignContext ctx) { - return new AugassignNode(ctx.ASSIGN()); - } - - // FIXME: add support for `elif` - public Node visitIf_stmt(If_stmtContext ctx) { - var blocks = ctx.block(); - Node condExp = visit(ctx.expr(0)); - Node thenExp = visit(blocks.get(0)); - Node elseExp = null; - if (blocks.size() > 1) { - elseExp = visit(blocks.get(1)); - } - - return new IfNode(condExp, thenExp, elseExp); - } - - public Node visitComp_op(Comp_opContext ctx) { - Node comp = null; - if (ctx.LESS_THAN() != null) { - comp = new CompNode(ctx.LESS_THAN()); - } else if (ctx.GREATER_THAN() != null) { - comp = new CompNode(ctx.GREATER_THAN()); - } else if (ctx.EQUALS() != null) { - comp = new CompNode(ctx.EQUALS()); - } else if (ctx.GT_EQ() != null) { - comp = new CompNode(ctx.GT_EQ()); - } else if (ctx.LT_EQ() != null) { - comp = new CompNode(ctx.LT_EQ()); - } else if (ctx.NOT_EQ_2() != null) { - // We're ignoring NOT_EQ_1() because no one uses `<>` - comp = new CompNode(ctx.NOT_EQ_2()); - } else if (ctx.IN() != null) { - comp = new CompNode(ctx.IN()); - } else if (ctx.NOT() != null) { - comp = new CompNode(ctx.NOT()); - } else if (ctx.IS() != null) { - comp = new CompNode(ctx.IS()); - } - - return comp; - } - - public Node visitBlock(BlockContext ctx) { - ArrayList stmts = new ArrayList(); - ArrayList compStmts = new ArrayList(); - - for (Simple_stmtsContext s : ctx.simple_stmts()) { - stmts.add(visit(s)); - } - for (Compound_stmtContext s : ctx.compound_stmt()) { - compStmts.add(visit(s)); - } - - return new BlockNode(stmts, compStmts); - } - public Node visitTrailer(TrailerContext ctx) { Node arglist = null; if (ctx.arglist() != null) { @@ -394,6 +488,13 @@ public Node visitTrailer(TrailerContext ctx) { return new TrailerNode(arglist, exprs, methodCall); } + public Node visitExprlist(ExprlistContext ctx) { + // FIXME: you've used to be a list, c'mon + Node exp = visit(ctx.expr(0)); + + return exp; + } + public Node visitArglist(ArglistContext ctx) { ArrayList arguments = new ArrayList(); @@ -403,23 +504,4 @@ public Node visitArglist(ArglistContext ctx) { return new ArglistNode(arguments); } - - public Node visitFor_stmt(For_stmtContext ctx) { - Node exprList = visit(ctx.exprlist()); - - // Block 1 is for the for-else statement - Node block = visit(ctx.block(0)); - - return new ForStmtNode(exprList, block); - } - - public Node visitWhile_stmt(While_stmtContext ctx) { - Node expr = visit(ctx.expr()); - - // Block 1 is for the while-else statement - Node block = visit(ctx.block(0)); - - return new WhileStmtNode(expr, block); - } - } diff --git a/src/ast/nodes/AtomNode.java b/src/ast/nodes/AtomNode.java index a521d82..fa81f92 100644 --- a/src/ast/nodes/AtomNode.java +++ b/src/ast/nodes/AtomNode.java @@ -6,31 +6,40 @@ import com.clp.project.semanticanalysis.SymbolTable; import com.clp.project.ast.types.*; +/** + * Node for the `atom` statement of the grammar. + */ public class AtomNode implements Node { protected String val; - public AtomNode(String _val) { - val = _val; + public AtomNode(String val) { + this.val = val; } + @Override public ArrayList checkSemantics(SymbolTable ST, int _nesting) { return new ArrayList(); } + // FIXME: this type for atom + @Override public Type typeCheck() { return new AtomType(); } + // TODO: add code generation for atom node + @Override public String codeGeneration() { return ""; } - public String toPrint(String s) { + @Override + public String toPrint(String prefix) { if (val != null) { - return s + "Atom(" + val + ")\n"; + return prefix + "Atom(" + val + ")\n"; } - return s + "Atom(null)\n"; + return prefix + "Atom(null)\n"; } } diff --git a/src/ast/nodes/AugassignNode.java b/src/ast/nodes/AugassignNode.java index 1e5ad15..3519b50 100644 --- a/src/ast/nodes/AugassignNode.java +++ b/src/ast/nodes/AugassignNode.java @@ -4,29 +4,39 @@ import com.clp.project.semanticanalysis.SemanticError; import com.clp.project.semanticanalysis.SymbolTable; + import com.clp.project.ast.types.*; import org.antlr.v4.runtime.tree.TerminalNode; +/** + * Node for the `augassign` statement of the grammar. + */ public class AugassignNode implements Node { private TerminalNode val; - public AugassignNode(TerminalNode _val) { - val = _val; + public AugassignNode(TerminalNode val) { + this.val = val; } + @Override public ArrayList checkSemantics(SymbolTable ST, int _nesting) { return new ArrayList(); } + // FIXME: use the right type + @Override public Type typeCheck() { - return new ErrorType(); + return new VoidType(); } + // TODO: add code generation for augassign node + @Override public String codeGeneration() { return ""; } - public String toPrint(String s) { - return s + "Augassign(" + val + ")\n"; + @Override + public String toPrint(String prefix) { + return prefix + "Augassign(" + val + ")\n"; } } diff --git a/src/ast/nodes/BlockNode.java b/src/ast/nodes/BlockNode.java index 52dcf8a..1fdfcc3 100644 --- a/src/ast/nodes/BlockNode.java +++ b/src/ast/nodes/BlockNode.java @@ -6,23 +6,33 @@ import com.clp.project.semanticanalysis.SymbolTable; import com.clp.project.ast.types.*; +/** + * Node for `block` statement of the grammar. + * It extends the `RootNode`. + */ public class BlockNode extends RootNode implements Node { - public BlockNode(ArrayList _stmts, ArrayList _compoundStmts) { - super(_stmts, _compoundStmts); + public BlockNode(ArrayList stmts, ArrayList compoundStmts) { + super(stmts, compoundStmts); } @Override - public String toPrint(String s) { - String result = s + "Block\n"; + public Type typeCheck() { + return new VoidType(); + } + + @Override + public String toPrint(String prefix) { + String str = prefix + "Block\n"; + prefix += " "; for (Node stmt : stmts) { - result += stmt.toPrint(s + " "); + str += stmt.toPrint(prefix); } for (Node stmt : compoundStmts) { - result += stmt.toPrint(s + " "); + str += stmt.toPrint(prefix); } - return result; + return str; } } diff --git a/src/ast/nodes/CompNode.java b/src/ast/nodes/CompNode.java index 5a007eb..6deb51c 100644 --- a/src/ast/nodes/CompNode.java +++ b/src/ast/nodes/CompNode.java @@ -7,26 +7,35 @@ import com.clp.project.ast.types.*; import org.antlr.v4.runtime.tree.TerminalNode; +/** + * Node for the `comp_op` statement of the grammar. + */ public class CompNode implements Node { - private TerminalNode val; + private TerminalNode op; - public CompNode(TerminalNode _val) { - val = _val; + public CompNode(TerminalNode op) { + this.op = op; } + @Override public ArrayList checkSemantics(SymbolTable ST, int _nesting) { return new ArrayList(); } + // TODO: it should be boolean, right? + @Override public Type typeCheck() { - return new ErrorType(); + return new VoidType(); } + // TODO: add code generation for CompNode + @Override public String codeGeneration() { return ""; } - public String toPrint(String s) { - return s + "CompNode(" + val + ")\n"; + @Override + public String toPrint(String prefix) { + return prefix + "CompNode(" + op + ")\n"; } } diff --git a/src/ast/nodes/ExprNode.java b/src/ast/nodes/ExprNode.java index a1ae22d..3aa112f 100644 --- a/src/ast/nodes/ExprNode.java +++ b/src/ast/nodes/ExprNode.java @@ -6,6 +6,9 @@ import com.clp.project.semanticanalysis.SymbolTable; import com.clp.project.ast.types.*; +/** + * Node for the `expr` statement of the grammar. + */ public class ExprNode implements Node { private Node atom; private Node compOp; @@ -44,39 +47,44 @@ public ArrayList checkSemantics(SymbolTable ST, int _nesting) { return errors; } + // FIXME: type for the expr + @Override public Type typeCheck() { - // FIXME: wtf is that? return new VoidType(); } + // TODO: add code generation for expr + @Override public String codeGeneration() { return ""; } - public String toPrint(String s) { - String result = s + "Expr\n"; + @Override + public String toPrint(String prefix) { + String str = prefix + "Expr\n"; + prefix += " "; if (atom != null) { - result += atom.toPrint(s + " "); + str += atom.toPrint(prefix); } if (compOp != null) { - result += compOp.toPrint(s + " "); + str += compOp.toPrint(prefix); } for (var expr : exprs) { - result += expr.toPrint(s + " "); + str += expr.toPrint(prefix); } for (var trailer : trailers) { - result += trailer.toPrint(s + " "); + str += trailer.toPrint(prefix); } if (op != null) { - result += s + " Op(" + op + ")\n"; + str += prefix + "Op(" + op + ")\n"; } - return result; + return str; } } diff --git a/src/ast/nodes/ForStmtNode.java b/src/ast/nodes/ForStmtNode.java index f7197e5..69dee74 100644 --- a/src/ast/nodes/ForStmtNode.java +++ b/src/ast/nodes/ForStmtNode.java @@ -6,6 +6,9 @@ import com.clp.project.semanticanalysis.SymbolTable; import com.clp.project.ast.types.*; +/** + * Node for the `for_stmt` statement of the grammar. + */ public class ForStmtNode implements Node { private Node exprList; private Node block; @@ -25,16 +28,20 @@ public ArrayList checkSemantics(SymbolTable ST, int _nesting) { return errors; } + @Override public Type typeCheck() { return new VoidType(); } + // TODO: add code generation for while + @Override public String codeGeneration() { return ""; } - public String toPrint(String s) { - return s + "For\n" + exprList.toPrint(s + " ") + block.toPrint(s + " "); + @Override + public String toPrint(String prefix) { + return prefix + "For\n" + exprList.toPrint(prefix + " ") + block.toPrint(prefix + " "); } } diff --git a/src/ast/nodes/IfNode.java b/src/ast/nodes/IfNode.java index ebe68f9..a4f160d 100644 --- a/src/ast/nodes/IfNode.java +++ b/src/ast/nodes/IfNode.java @@ -6,15 +6,18 @@ import com.clp.project.semanticanalysis.SymbolTable; import com.clp.project.ast.types.*; +/** + * Node for the `if` statement of the grammar. + */ public class IfNode implements Node { private Node guard; private Node thenbranch; private Node elsebranch; - public IfNode(Node _guard, Node _thenbranch, Node _elsebranch) { - guard = _guard; - thenbranch = _thenbranch; - elsebranch = _elsebranch; + public IfNode(Node guard, Node thenbranch, Node elsebranch) { + this.guard = guard; + this.thenbranch = thenbranch; + this.elsebranch = elsebranch; } @Override @@ -30,6 +33,8 @@ public ArrayList checkSemantics(SymbolTable ST, int _nesting) { return errors; } + // FIXME: fix the if statement + @Override public Type typeCheck() { if (guard.typeCheck() instanceof BoolType) { Type thenexp = thenbranch.typeCheck(); @@ -46,18 +51,21 @@ public Type typeCheck() { } } + // TODO: add code generation for if + @Override public String codeGeneration() { return ""; } - public String toPrint(String s) { - String result = s + "If\n" + guard.toPrint(s + " ") + thenbranch.toPrint(s + " "); + @Override + public String toPrint(String prefix) { + String str = prefix + "If\n" + guard.toPrint(prefix + " ") + thenbranch.toPrint(prefix + " "); if (elsebranch != null) { - result += elsebranch.toPrint(s + " "); + str += elsebranch.toPrint(prefix + " "); } - return result; + return str; } } diff --git a/src/ast/nodes/WhileStmtNode.java b/src/ast/nodes/WhileStmtNode.java index 5d77f2c..a84d60e 100644 --- a/src/ast/nodes/WhileStmtNode.java +++ b/src/ast/nodes/WhileStmtNode.java @@ -6,6 +6,9 @@ import com.clp.project.semanticanalysis.SymbolTable; import com.clp.project.ast.types.*; +/** + * Node for the `while_stmt` statement of the grammar. + */ public class WhileStmtNode implements Node { private Node expr; private Node block; @@ -25,16 +28,19 @@ public ArrayList checkSemantics(SymbolTable ST, int _nesting) { return errors; } + @Override public Type typeCheck() { return new VoidType(); } + // TODO: add code generation for while + @Override public String codeGeneration() { return ""; } - public String toPrint(String s) { - return s + "While\n" + expr.toPrint(s + " ") + block.toPrint(s + " "); + @Override + public String toPrint(String prefix) { + return prefix + "While\n" + expr.toPrint(prefix + " ") + block.toPrint(prefix + " "); } - } From b40a065eefd013b2493677a99b56a25e80b13cf9 Mon Sep 17 00:00:00 2001 From: Santo Cariotti Date: Sun, 9 Jun 2024 21:17:26 +0200 Subject: [PATCH 21/23] fix doc --- src/ast/Python3VisitorImpl.java | 25 ++++++++++++++++++++++++- src/ast/nodes/ArglistNode.java | 16 ++++++++++++---- src/ast/nodes/TrailerNode.java | 23 ++++++++++++++++------- 3 files changed, 52 insertions(+), 12 deletions(-) diff --git a/src/ast/Python3VisitorImpl.java b/src/ast/Python3VisitorImpl.java index a189666..0783409 100644 --- a/src/ast/Python3VisitorImpl.java +++ b/src/ast/Python3VisitorImpl.java @@ -469,6 +469,14 @@ public Node visitAtom(AtomContext ctx) { return new AtomNode(ctx.NONE().toString()); } + /** + * Returns an `TrailerNode`. + * + * ``` + * trailer : '(' arglist? ')' | '[' expr (',' expr)* ','? ']' | '.' NAME | '[' + * expr? ':' expr? (':' expr? )? ']' ; + * ``` + */ public Node visitTrailer(TrailerContext ctx) { Node arglist = null; if (ctx.arglist() != null) { @@ -480,6 +488,7 @@ public Node visitTrailer(TrailerContext ctx) { exprs.add(visit(expr)); } + // A trailer could be `.methodName()`. TerminalNode methodCall = null; if (ctx.DOT() != null) { methodCall = ctx.NAME(); @@ -488,13 +497,27 @@ public Node visitTrailer(TrailerContext ctx) { return new TrailerNode(arglist, exprs, methodCall); } + /** + * Returns a `Node`. + * FIXME: what to do in case of list?? + * + * ``` + * exprlist : expr (',' expr )* ','? ; + * ``` + */ public Node visitExprlist(ExprlistContext ctx) { - // FIXME: you've used to be a list, c'mon Node exp = visit(ctx.expr(0)); return exp; } + /** + * Returns a `ArglistNode`. + * + * ``` + * arglist : argument (',' argument)* ','? ; + * ``` + */ public Node visitArglist(ArglistContext ctx) { ArrayList arguments = new ArrayList(); diff --git a/src/ast/nodes/ArglistNode.java b/src/ast/nodes/ArglistNode.java index 0ebb816..8a5b4b9 100644 --- a/src/ast/nodes/ArglistNode.java +++ b/src/ast/nodes/ArglistNode.java @@ -6,6 +6,9 @@ import com.clp.project.semanticanalysis.SymbolTable; import com.clp.project.ast.types.*; +/** + * Node for the `arglist` statement of the grammar. + */ public class ArglistNode implements Node { protected ArrayList arguments; @@ -24,22 +27,27 @@ public ArrayList checkSemantics(SymbolTable ST, int _nesting) { return errors; } + @Override public Type typeCheck() { return new VoidType(); } + // TODO: add code generation for arglist node + @Override public String codeGeneration() { return ""; } - public String toPrint(String s) { - String result = s + "ArglistNode\n"; + @Override + public String toPrint(String prefix) { + String str = prefix + "ArglistNode\n"; + prefix += " "; for (Node arg : arguments) { - result += arg.toPrint(s + " "); + str += arg.toPrint(prefix); } - return result; + return str; } } diff --git a/src/ast/nodes/TrailerNode.java b/src/ast/nodes/TrailerNode.java index 671fcc9..fa7e1ed 100644 --- a/src/ast/nodes/TrailerNode.java +++ b/src/ast/nodes/TrailerNode.java @@ -7,6 +7,9 @@ import com.clp.project.ast.types.*; import org.antlr.v4.runtime.tree.TerminalNode; +/** + * Node for the `trailer` statement of the grammar. + */ public class TrailerNode implements Node { private Node arglist; private ArrayList exprs; @@ -36,34 +39,40 @@ public ArrayList checkSemantics(SymbolTable ST, int _nesting) { return errors; } + @Override public Type typeCheck() { return new VoidType(); } + // TODO: add code generation for trailer node + @Override public String codeGeneration() { return ""; } - public String toPrint(String s) { - String result = s + "TrailerNode\n"; + @Override + public String toPrint(String prefix) { + String str = prefix + "TrailerNode\n"; + + prefix += " "; if (arglist != null) { - result += arglist.toPrint(s + " "); + str += arglist.toPrint(prefix); } for (var expr : exprs) { - result += expr.toPrint(s + " "); + str += expr.toPrint(prefix); } if (methodCall != null) { - result += s + " Method(" + methodCall + ")\n"; + str += prefix + "Method(" + methodCall + ")\n"; } if (isEmpty) { - result += s + " ()\n"; + str += prefix + "()\n"; } - return result; + return str; } } From 47413f4966531cbc2a336afc1c40a43abcfcbdb2 Mon Sep 17 00:00:00 2001 From: Santo Cariotti Date: Sun, 9 Jun 2024 21:31:29 +0200 Subject: [PATCH 22/23] Doc for types --- src/ast/types/AtomType.java | 8 ++++++-- src/ast/types/BoolType.java | 8 +++++--- src/ast/types/ErrorType.java | 7 +++++-- src/ast/types/IntType.java | 7 +++++-- src/ast/types/Type.java | 3 +++ src/ast/types/VoidType.java | 7 +++++-- 6 files changed, 29 insertions(+), 11 deletions(-) diff --git a/src/ast/types/AtomType.java b/src/ast/types/AtomType.java index a32953a..a44a06c 100644 --- a/src/ast/types/AtomType.java +++ b/src/ast/types/AtomType.java @@ -1,7 +1,11 @@ package com.clp.project.ast.types; +/** + * An tom type. + * TODO: do I need to use this one? + */ public class AtomType extends Type { - public String toPrint(String s) { - return s + "Atom "; + public String toPrint(String prefix) { + return prefix + "Atom\n"; } } diff --git a/src/ast/types/BoolType.java b/src/ast/types/BoolType.java index d6d9731..7fc28ee 100644 --- a/src/ast/types/BoolType.java +++ b/src/ast/types/BoolType.java @@ -1,8 +1,10 @@ package com.clp.project.ast.types; +/** + * A boolean type. A bool is True or False. + */ public class BoolType extends Type { - public String toPrint(String s) { - return s + "Bool "; + public String toPrint(String prefix) { + return prefix + "Bool\n"; } - } diff --git a/src/ast/types/ErrorType.java b/src/ast/types/ErrorType.java index e4a54d3..30cb70e 100644 --- a/src/ast/types/ErrorType.java +++ b/src/ast/types/ErrorType.java @@ -1,7 +1,10 @@ package com.clp.project.ast.types; +/** + * Error type. + */ public class ErrorType extends Type { - public String toPrint(String s) { - return s + "Error\n"; + public String toPrint(String prefix) { + return prefix + "Error\n"; } } diff --git a/src/ast/types/IntType.java b/src/ast/types/IntType.java index a953d1f..9f4b250 100644 --- a/src/ast/types/IntType.java +++ b/src/ast/types/IntType.java @@ -1,7 +1,10 @@ package com.clp.project.ast.types; +/** + * An integer type. + */ public class IntType extends Type { - public String toPrint(String s) { - return s + "Int "; + public String toPrint(String prefix) { + return prefix + "Int\n"; } } diff --git a/src/ast/types/Type.java b/src/ast/types/Type.java index 267a244..4756255 100644 --- a/src/ast/types/Type.java +++ b/src/ast/types/Type.java @@ -6,6 +6,9 @@ import com.clp.project.semanticanalysis.SymbolTable; import com.clp.project.ast.nodes.*; +/** + * A node which represents a type class. + */ public class Type implements Node { public boolean isEqual(Type A, Type B) { if (A.getClass().equals(B.getClass())) diff --git a/src/ast/types/VoidType.java b/src/ast/types/VoidType.java index 47cb6bc..766aee2 100644 --- a/src/ast/types/VoidType.java +++ b/src/ast/types/VoidType.java @@ -1,7 +1,10 @@ package com.clp.project.ast.types; +/** + * A void type. Voids return nothing. + */ public class VoidType extends Type { - public String toPrint(String s) { - return s + "Void "; + public String toPrint(String prefix) { + return prefix + "Void\n"; } } From 39cbf45090ffb1684b1d689fe6e464111daf58cd Mon Sep 17 00:00:00 2001 From: Santo Cariotti Date: Sun, 9 Jun 2024 21:32:12 +0200 Subject: [PATCH 23/23] remove useless node --- src/ast/nodes/IntNode.java | 31 ------------------------------- 1 file changed, 31 deletions(-) delete mode 100644 src/ast/nodes/IntNode.java diff --git a/src/ast/nodes/IntNode.java b/src/ast/nodes/IntNode.java deleted file mode 100644 index 75558c7..0000000 --- a/src/ast/nodes/IntNode.java +++ /dev/null @@ -1,31 +0,0 @@ -package com.clp.project.ast.nodes; - -import java.util.ArrayList; - -import com.clp.project.semanticanalysis.SemanticError; -import com.clp.project.semanticanalysis.SymbolTable; -import com.clp.project.ast.types.*; - -public class IntNode implements Node { - private Integer val; - - public IntNode(Integer _val) { - val = _val; - } - - public ArrayList checkSemantics(SymbolTable ST, int _nesting) { - return new ArrayList(); - } - - public Type typeCheck() { - return new IntType(); - } - - public String codeGeneration() { - return ""; - } - - public String toPrint(String s) { - return s + Integer.toString(val) + "\n"; - } -}