From 02d57fa858bdfb32deb05b34a9d2b05dd3967382 Mon Sep 17 00:00:00 2001 From: Vu Tuan Minh Date: Tue, 1 Apr 2025 16:52:50 +0200 Subject: [PATCH] =?UTF-8?q?parser=20+=20asd=20non=20test=C3=A9=20non=20bui?= =?UTF-8?q?lder?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/Visitor/ASDVisitor.java | 9 - src/main/antlr/.antlr/VSLLexer.interp | 104 ++++ src/main/antlr/.antlr/VSLLexer.java | 276 ++++++++++ src/main/antlr/.antlr/VSLLexer.tokens | 52 ++ src/main/antlr/.antlr/VSLParser.interp | 77 +++ src/main/antlr/.antlr/VSLParser.java | 674 +++++++++++++++++++++++++ src/main/antlr/.antlr/VSLParser.tokens | 52 ++ src/main/antlr/VSLParser.g | 88 ++-- src/main/java/TP2/Main.java | 2 +- src/main/java/TP2/asd/ExprEval.java | 23 + src/main/java/TP2/asd/Interface.java | 31 +- src/main/java/TP2/asd/Program.java | 39 +- src/main/java/TP2/asd/SymTable.java | 7 + 13 files changed, 1374 insertions(+), 60 deletions(-) delete mode 100644 src/main/Visitor/ASDVisitor.java create mode 100644 src/main/antlr/.antlr/VSLLexer.interp create mode 100644 src/main/antlr/.antlr/VSLLexer.java create mode 100644 src/main/antlr/.antlr/VSLLexer.tokens create mode 100644 src/main/antlr/.antlr/VSLParser.interp create mode 100644 src/main/antlr/.antlr/VSLParser.java create mode 100644 src/main/antlr/.antlr/VSLParser.tokens create mode 100644 src/main/java/TP2/asd/ExprEval.java create mode 100644 src/main/java/TP2/asd/SymTable.java diff --git a/src/main/Visitor/ASDVisitor.java b/src/main/Visitor/ASDVisitor.java deleted file mode 100644 index 527fa7f..0000000 --- a/src/main/Visitor/ASDVisitor.java +++ /dev/null @@ -1,9 +0,0 @@ -package TP2.Visitor; - -import java.util.ArrayList; - - -public interface ASDVisitor { - public ArrayList visit(Ftion function); - public Instruction visit(Instruction instr); -} diff --git a/src/main/antlr/.antlr/VSLLexer.interp b/src/main/antlr/.antlr/VSLLexer.interp new file mode 100644 index 0000000..1c5dda4 --- /dev/null +++ b/src/main/antlr/.antlr/VSLLexer.interp @@ -0,0 +1,104 @@ +token literal names: +null +'FUNC' +'PROTO' +'INT' +'VOID' +'<=' +'<' +'>=' +'>' +'IF' +'THEN' +'ELSE' +'FI' +'WHILE' +'DO' +'DONE' +'READ' +'PRINT' +'RETURN' +null +null +'(' +')' +'+' +'-' +'/' +'*' +null +null + +token symbolic names: +null +FUNCTION +PROTOTYPE +TYPE_INT +TYPE_VOID +LE +LT +GE +GT +IF +THEN +ELSE +FIN +WHILE +DO +DONE +READ +PRINT +RETURN +TEXT +NUMBER +ParO +ParF +PLUS +MINUS +DIV +TIMES +WS +COMMENT + +rule names: +FUNCTION +PROTOTYPE +TYPE_INT +TYPE_VOID +LE +LT +GE +GT +IF +THEN +ELSE +FIN +WHILE +DO +DONE +READ +PRINT +RETURN +TEXT +NUMBER +ParO +ParF +PLUS +MINUS +DIV +TIMES +WS +COMMENT +LETTER +DIGIT +ASCII + +channel names: +DEFAULT_TOKEN_CHANNEL +HIDDEN + +mode names: +DEFAULT_MODE + +atn: +[4, 0, 28, 187, 6, -1, 2, 0, 7, 0, 2, 1, 7, 1, 2, 2, 7, 2, 2, 3, 7, 3, 2, 4, 7, 4, 2, 5, 7, 5, 2, 6, 7, 6, 2, 7, 7, 7, 2, 8, 7, 8, 2, 9, 7, 9, 2, 10, 7, 10, 2, 11, 7, 11, 2, 12, 7, 12, 2, 13, 7, 13, 2, 14, 7, 14, 2, 15, 7, 15, 2, 16, 7, 16, 2, 17, 7, 17, 2, 18, 7, 18, 2, 19, 7, 19, 2, 20, 7, 20, 2, 21, 7, 21, 2, 22, 7, 22, 2, 23, 7, 23, 2, 24, 7, 24, 2, 25, 7, 25, 2, 26, 7, 26, 2, 27, 7, 27, 2, 28, 7, 28, 2, 29, 7, 29, 2, 30, 7, 30, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 2, 1, 2, 1, 2, 1, 3, 1, 3, 1, 3, 1, 3, 1, 3, 1, 4, 1, 4, 1, 4, 1, 5, 1, 5, 1, 6, 1, 6, 1, 6, 1, 7, 1, 7, 1, 8, 1, 8, 1, 8, 1, 9, 1, 9, 1, 9, 1, 9, 1, 9, 1, 10, 1, 10, 1, 10, 1, 10, 1, 10, 1, 11, 1, 11, 1, 11, 1, 12, 1, 12, 1, 12, 1, 12, 1, 12, 1, 12, 1, 13, 1, 13, 1, 13, 1, 14, 1, 14, 1, 14, 1, 14, 1, 14, 1, 15, 1, 15, 1, 15, 1, 15, 1, 15, 1, 16, 1, 16, 1, 16, 1, 16, 1, 16, 1, 16, 1, 17, 1, 17, 1, 17, 1, 17, 1, 17, 1, 17, 1, 17, 1, 18, 1, 18, 4, 18, 144, 8, 18, 11, 18, 12, 18, 145, 1, 18, 1, 18, 1, 18, 1, 19, 4, 19, 152, 8, 19, 11, 19, 12, 19, 153, 1, 20, 1, 20, 1, 21, 1, 21, 1, 22, 1, 22, 1, 23, 1, 23, 1, 24, 1, 24, 1, 25, 1, 25, 1, 26, 1, 26, 1, 26, 1, 27, 1, 27, 1, 27, 1, 27, 5, 27, 175, 8, 27, 10, 27, 12, 27, 178, 9, 27, 1, 27, 1, 27, 1, 28, 1, 28, 1, 29, 1, 29, 1, 30, 1, 30, 0, 0, 31, 1, 1, 3, 2, 5, 3, 7, 4, 9, 5, 11, 6, 13, 7, 15, 8, 17, 9, 19, 10, 21, 11, 23, 12, 25, 13, 27, 14, 29, 15, 31, 16, 33, 17, 35, 18, 37, 19, 39, 20, 41, 21, 43, 22, 45, 23, 47, 24, 49, 25, 51, 26, 53, 27, 55, 28, 57, 0, 59, 0, 61, 0, 1, 0, 3, 2, 0, 9, 10, 32, 32, 1, 0, 10, 10, 2, 0, 10, 10, 34, 34, 186, 0, 1, 1, 0, 0, 0, 0, 3, 1, 0, 0, 0, 0, 5, 1, 0, 0, 0, 0, 7, 1, 0, 0, 0, 0, 9, 1, 0, 0, 0, 0, 11, 1, 0, 0, 0, 0, 13, 1, 0, 0, 0, 0, 15, 1, 0, 0, 0, 0, 17, 1, 0, 0, 0, 0, 19, 1, 0, 0, 0, 0, 21, 1, 0, 0, 0, 0, 23, 1, 0, 0, 0, 0, 25, 1, 0, 0, 0, 0, 27, 1, 0, 0, 0, 0, 29, 1, 0, 0, 0, 0, 31, 1, 0, 0, 0, 0, 33, 1, 0, 0, 0, 0, 35, 1, 0, 0, 0, 0, 37, 1, 0, 0, 0, 0, 39, 1, 0, 0, 0, 0, 41, 1, 0, 0, 0, 0, 43, 1, 0, 0, 0, 0, 45, 1, 0, 0, 0, 0, 47, 1, 0, 0, 0, 0, 49, 1, 0, 0, 0, 0, 51, 1, 0, 0, 0, 0, 53, 1, 0, 0, 0, 0, 55, 1, 0, 0, 0, 1, 63, 1, 0, 0, 0, 3, 68, 1, 0, 0, 0, 5, 74, 1, 0, 0, 0, 7, 78, 1, 0, 0, 0, 9, 83, 1, 0, 0, 0, 11, 86, 1, 0, 0, 0, 13, 88, 1, 0, 0, 0, 15, 91, 1, 0, 0, 0, 17, 93, 1, 0, 0, 0, 19, 96, 1, 0, 0, 0, 21, 101, 1, 0, 0, 0, 23, 106, 1, 0, 0, 0, 25, 109, 1, 0, 0, 0, 27, 115, 1, 0, 0, 0, 29, 118, 1, 0, 0, 0, 31, 123, 1, 0, 0, 0, 33, 128, 1, 0, 0, 0, 35, 134, 1, 0, 0, 0, 37, 141, 1, 0, 0, 0, 39, 151, 1, 0, 0, 0, 41, 155, 1, 0, 0, 0, 43, 157, 1, 0, 0, 0, 45, 159, 1, 0, 0, 0, 47, 161, 1, 0, 0, 0, 49, 163, 1, 0, 0, 0, 51, 165, 1, 0, 0, 0, 53, 167, 1, 0, 0, 0, 55, 170, 1, 0, 0, 0, 57, 181, 1, 0, 0, 0, 59, 183, 1, 0, 0, 0, 61, 185, 1, 0, 0, 0, 63, 64, 5, 70, 0, 0, 64, 65, 5, 85, 0, 0, 65, 66, 5, 78, 0, 0, 66, 67, 5, 67, 0, 0, 67, 2, 1, 0, 0, 0, 68, 69, 5, 80, 0, 0, 69, 70, 5, 82, 0, 0, 70, 71, 5, 79, 0, 0, 71, 72, 5, 84, 0, 0, 72, 73, 5, 79, 0, 0, 73, 4, 1, 0, 0, 0, 74, 75, 5, 73, 0, 0, 75, 76, 5, 78, 0, 0, 76, 77, 5, 84, 0, 0, 77, 6, 1, 0, 0, 0, 78, 79, 5, 86, 0, 0, 79, 80, 5, 79, 0, 0, 80, 81, 5, 73, 0, 0, 81, 82, 5, 68, 0, 0, 82, 8, 1, 0, 0, 0, 83, 84, 5, 60, 0, 0, 84, 85, 5, 61, 0, 0, 85, 10, 1, 0, 0, 0, 86, 87, 5, 60, 0, 0, 87, 12, 1, 0, 0, 0, 88, 89, 5, 62, 0, 0, 89, 90, 5, 61, 0, 0, 90, 14, 1, 0, 0, 0, 91, 92, 5, 62, 0, 0, 92, 16, 1, 0, 0, 0, 93, 94, 5, 73, 0, 0, 94, 95, 5, 70, 0, 0, 95, 18, 1, 0, 0, 0, 96, 97, 5, 84, 0, 0, 97, 98, 5, 72, 0, 0, 98, 99, 5, 69, 0, 0, 99, 100, 5, 78, 0, 0, 100, 20, 1, 0, 0, 0, 101, 102, 5, 69, 0, 0, 102, 103, 5, 76, 0, 0, 103, 104, 5, 83, 0, 0, 104, 105, 5, 69, 0, 0, 105, 22, 1, 0, 0, 0, 106, 107, 5, 70, 0, 0, 107, 108, 5, 73, 0, 0, 108, 24, 1, 0, 0, 0, 109, 110, 5, 87, 0, 0, 110, 111, 5, 72, 0, 0, 111, 112, 5, 73, 0, 0, 112, 113, 5, 76, 0, 0, 113, 114, 5, 69, 0, 0, 114, 26, 1, 0, 0, 0, 115, 116, 5, 68, 0, 0, 116, 117, 5, 79, 0, 0, 117, 28, 1, 0, 0, 0, 118, 119, 5, 68, 0, 0, 119, 120, 5, 79, 0, 0, 120, 121, 5, 78, 0, 0, 121, 122, 5, 69, 0, 0, 122, 30, 1, 0, 0, 0, 123, 124, 5, 82, 0, 0, 124, 125, 5, 69, 0, 0, 125, 126, 5, 65, 0, 0, 126, 127, 5, 68, 0, 0, 127, 32, 1, 0, 0, 0, 128, 129, 5, 80, 0, 0, 129, 130, 5, 82, 0, 0, 130, 131, 5, 73, 0, 0, 131, 132, 5, 78, 0, 0, 132, 133, 5, 84, 0, 0, 133, 34, 1, 0, 0, 0, 134, 135, 5, 82, 0, 0, 135, 136, 5, 69, 0, 0, 136, 137, 5, 84, 0, 0, 137, 138, 5, 85, 0, 0, 138, 139, 5, 82, 0, 0, 139, 140, 5, 78, 0, 0, 140, 36, 1, 0, 0, 0, 141, 143, 5, 34, 0, 0, 142, 144, 3, 61, 30, 0, 143, 142, 1, 0, 0, 0, 144, 145, 1, 0, 0, 0, 145, 143, 1, 0, 0, 0, 145, 146, 1, 0, 0, 0, 146, 147, 1, 0, 0, 0, 147, 148, 5, 34, 0, 0, 148, 149, 6, 18, 0, 0, 149, 38, 1, 0, 0, 0, 150, 152, 3, 59, 29, 0, 151, 150, 1, 0, 0, 0, 152, 153, 1, 0, 0, 0, 153, 151, 1, 0, 0, 0, 153, 154, 1, 0, 0, 0, 154, 40, 1, 0, 0, 0, 155, 156, 5, 40, 0, 0, 156, 42, 1, 0, 0, 0, 157, 158, 5, 41, 0, 0, 158, 44, 1, 0, 0, 0, 159, 160, 5, 43, 0, 0, 160, 46, 1, 0, 0, 0, 161, 162, 5, 45, 0, 0, 162, 48, 1, 0, 0, 0, 163, 164, 5, 47, 0, 0, 164, 50, 1, 0, 0, 0, 165, 166, 5, 42, 0, 0, 166, 52, 1, 0, 0, 0, 167, 168, 7, 0, 0, 0, 168, 169, 6, 26, 1, 0, 169, 54, 1, 0, 0, 0, 170, 171, 5, 47, 0, 0, 171, 172, 5, 47, 0, 0, 172, 176, 1, 0, 0, 0, 173, 175, 8, 1, 0, 0, 174, 173, 1, 0, 0, 0, 175, 178, 1, 0, 0, 0, 176, 174, 1, 0, 0, 0, 176, 177, 1, 0, 0, 0, 177, 179, 1, 0, 0, 0, 178, 176, 1, 0, 0, 0, 179, 180, 6, 27, 2, 0, 180, 56, 1, 0, 0, 0, 181, 182, 2, 97, 122, 0, 182, 58, 1, 0, 0, 0, 183, 184, 2, 48, 57, 0, 184, 60, 1, 0, 0, 0, 185, 186, 8, 2, 0, 0, 186, 62, 1, 0, 0, 0, 4, 0, 145, 153, 176, 3, 1, 18, 0, 1, 26, 1, 1, 27, 2] \ No newline at end of file diff --git a/src/main/antlr/.antlr/VSLLexer.java b/src/main/antlr/.antlr/VSLLexer.java new file mode 100644 index 0000000..788a8f1 --- /dev/null +++ b/src/main/antlr/.antlr/VSLLexer.java @@ -0,0 +1,276 @@ +// Generated from /home/tuanvu/Desktop/M1/S8/PDS/tp2-vsl-pds/src/main/antlr/VSLLexer.g by ANTLR 4.13.1 + + package TP2; + +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; +import org.antlr.v4.runtime.misc.*; + +@SuppressWarnings({"all", "warnings", "unchecked", "unused", "cast", "CheckReturnValue", "this-escape"}) +public class VSLLexer extends Lexer { + static { RuntimeMetaData.checkVersion("4.13.1", RuntimeMetaData.VERSION); } + + protected static final DFA[] _decisionToDFA; + protected static final PredictionContextCache _sharedContextCache = + new PredictionContextCache(); + public static final int + FUNCTION=1, PROTOTYPE=2, TYPE_INT=3, TYPE_VOID=4, LE=5, LT=6, GE=7, GT=8, + IF=9, THEN=10, ELSE=11, FIN=12, WHILE=13, DO=14, DONE=15, READ=16, PRINT=17, + RETURN=18, TEXT=19, NUMBER=20, ParO=21, ParF=22, PLUS=23, MINUS=24, DIV=25, + TIMES=26, WS=27, COMMENT=28; + public static String[] channelNames = { + "DEFAULT_TOKEN_CHANNEL", "HIDDEN" + }; + + public static String[] modeNames = { + "DEFAULT_MODE" + }; + + private static String[] makeRuleNames() { + return new String[] { + "FUNCTION", "PROTOTYPE", "TYPE_INT", "TYPE_VOID", "LE", "LT", "GE", "GT", + "IF", "THEN", "ELSE", "FIN", "WHILE", "DO", "DONE", "READ", "PRINT", + "RETURN", "TEXT", "NUMBER", "ParO", "ParF", "PLUS", "MINUS", "DIV", "TIMES", + "WS", "COMMENT", "LETTER", "DIGIT", "ASCII" + }; + } + public static final String[] ruleNames = makeRuleNames(); + + private static String[] makeLiteralNames() { + return new String[] { + null, "'FUNC'", "'PROTO'", "'INT'", "'VOID'", "'<='", "'<'", "'>='", + "'>'", "'IF'", "'THEN'", "'ELSE'", "'FI'", "'WHILE'", "'DO'", "'DONE'", + "'READ'", "'PRINT'", "'RETURN'", null, null, "'('", "')'", "'+'", "'-'", + "'/'", "'*'" + }; + } + private static final String[] _LITERAL_NAMES = makeLiteralNames(); + private static String[] makeSymbolicNames() { + return new String[] { + null, "FUNCTION", "PROTOTYPE", "TYPE_INT", "TYPE_VOID", "LE", "LT", "GE", + "GT", "IF", "THEN", "ELSE", "FIN", "WHILE", "DO", "DONE", "READ", "PRINT", + "RETURN", "TEXT", "NUMBER", "ParO", "ParF", "PLUS", "MINUS", "DIV", "TIMES", + "WS", "COMMENT" + }; + } + 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 VSLLexer(CharStream input) { + super(input); + _interp = new LexerATNSimulator(this,_ATN,_decisionToDFA,_sharedContextCache); + } + + @Override + public String getGrammarFileName() { return "VSLLexer.g"; } + + @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 18: + TEXT_action((RuleContext)_localctx, actionIndex); + break; + case 26: + WS_action((RuleContext)_localctx, actionIndex); + break; + case 27: + COMMENT_action((RuleContext)_localctx, actionIndex); + break; + } + } + private void TEXT_action(RuleContext _localctx, int actionIndex) { + switch (actionIndex) { + case 0: + getText().substring(1,getText().length()-1) + break; + } + } + private void WS_action(RuleContext _localctx, int actionIndex) { + switch (actionIndex) { + case 1: + skip(); + break; + } + } + private void COMMENT_action(RuleContext _localctx, int actionIndex) { + switch (actionIndex) { + case 2: + skip(); + break; + } + } + + public static final String _serializedATN = + "\u0004\u0000\u001c\u00bb\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\u0001\u0000\u0001\u0000\u0001\u0000\u0001\u0000"+ + "\u0001\u0000\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001"+ + "\u0001\u0001\u0001\u0002\u0001\u0002\u0001\u0002\u0001\u0002\u0001\u0003"+ + "\u0001\u0003\u0001\u0003\u0001\u0003\u0001\u0003\u0001\u0004\u0001\u0004"+ + "\u0001\u0004\u0001\u0005\u0001\u0005\u0001\u0006\u0001\u0006\u0001\u0006"+ + "\u0001\u0007\u0001\u0007\u0001\b\u0001\b\u0001\b\u0001\t\u0001\t\u0001"+ + "\t\u0001\t\u0001\t\u0001\n\u0001\n\u0001\n\u0001\n\u0001\n\u0001\u000b"+ + "\u0001\u000b\u0001\u000b\u0001\f\u0001\f\u0001\f\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\u000f"+ + "\u0001\u0010\u0001\u0010\u0001\u0010\u0001\u0010\u0001\u0010\u0001\u0010"+ + "\u0001\u0011\u0001\u0011\u0001\u0011\u0001\u0011\u0001\u0011\u0001\u0011"+ + "\u0001\u0011\u0001\u0012\u0001\u0012\u0004\u0012\u0090\b\u0012\u000b\u0012"+ + "\f\u0012\u0091\u0001\u0012\u0001\u0012\u0001\u0012\u0001\u0013\u0004\u0013"+ + "\u0098\b\u0013\u000b\u0013\f\u0013\u0099\u0001\u0014\u0001\u0014\u0001"+ + "\u0015\u0001\u0015\u0001\u0016\u0001\u0016\u0001\u0017\u0001\u0017\u0001"+ + "\u0018\u0001\u0018\u0001\u0019\u0001\u0019\u0001\u001a\u0001\u001a\u0001"+ + "\u001a\u0001\u001b\u0001\u001b\u0001\u001b\u0001\u001b\u0005\u001b\u00af"+ + "\b\u001b\n\u001b\f\u001b\u00b2\t\u001b\u0001\u001b\u0001\u001b\u0001\u001c"+ + "\u0001\u001c\u0001\u001d\u0001\u001d\u0001\u001e\u0001\u001e\u0000\u0000"+ + "\u001f\u0001\u0001\u0003\u0002\u0005\u0003\u0007\u0004\t\u0005\u000b\u0006"+ + "\r\u0007\u000f\b\u0011\t\u0013\n\u0015\u000b\u0017\f\u0019\r\u001b\u000e"+ + "\u001d\u000f\u001f\u0010!\u0011#\u0012%\u0013\'\u0014)\u0015+\u0016-\u0017"+ + "/\u00181\u00193\u001a5\u001b7\u001c9\u0000;\u0000=\u0000\u0001\u0000\u0003"+ + "\u0002\u0000\t\n \u0001\u0000\n\n\u0002\u0000\n\n\"\"\u00ba\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\u0000"+ + "5\u0001\u0000\u0000\u0000\u00007\u0001\u0000\u0000\u0000\u0001?\u0001"+ + "\u0000\u0000\u0000\u0003D\u0001\u0000\u0000\u0000\u0005J\u0001\u0000\u0000"+ + "\u0000\u0007N\u0001\u0000\u0000\u0000\tS\u0001\u0000\u0000\u0000\u000b"+ + "V\u0001\u0000\u0000\u0000\rX\u0001\u0000\u0000\u0000\u000f[\u0001\u0000"+ + "\u0000\u0000\u0011]\u0001\u0000\u0000\u0000\u0013`\u0001\u0000\u0000\u0000"+ + "\u0015e\u0001\u0000\u0000\u0000\u0017j\u0001\u0000\u0000\u0000\u0019m"+ + "\u0001\u0000\u0000\u0000\u001bs\u0001\u0000\u0000\u0000\u001dv\u0001\u0000"+ + "\u0000\u0000\u001f{\u0001\u0000\u0000\u0000!\u0080\u0001\u0000\u0000\u0000"+ + "#\u0086\u0001\u0000\u0000\u0000%\u008d\u0001\u0000\u0000\u0000\'\u0097"+ + "\u0001\u0000\u0000\u0000)\u009b\u0001\u0000\u0000\u0000+\u009d\u0001\u0000"+ + "\u0000\u0000-\u009f\u0001\u0000\u0000\u0000/\u00a1\u0001\u0000\u0000\u0000"+ + "1\u00a3\u0001\u0000\u0000\u00003\u00a5\u0001\u0000\u0000\u00005\u00a7"+ + "\u0001\u0000\u0000\u00007\u00aa\u0001\u0000\u0000\u00009\u00b5\u0001\u0000"+ + "\u0000\u0000;\u00b7\u0001\u0000\u0000\u0000=\u00b9\u0001\u0000\u0000\u0000"+ + "?@\u0005F\u0000\u0000@A\u0005U\u0000\u0000AB\u0005N\u0000\u0000BC\u0005"+ + "C\u0000\u0000C\u0002\u0001\u0000\u0000\u0000DE\u0005P\u0000\u0000EF\u0005"+ + "R\u0000\u0000FG\u0005O\u0000\u0000GH\u0005T\u0000\u0000HI\u0005O\u0000"+ + "\u0000I\u0004\u0001\u0000\u0000\u0000JK\u0005I\u0000\u0000KL\u0005N\u0000"+ + "\u0000LM\u0005T\u0000\u0000M\u0006\u0001\u0000\u0000\u0000NO\u0005V\u0000"+ + "\u0000OP\u0005O\u0000\u0000PQ\u0005I\u0000\u0000QR\u0005D\u0000\u0000"+ + "R\b\u0001\u0000\u0000\u0000ST\u0005<\u0000\u0000TU\u0005=\u0000\u0000"+ + "U\n\u0001\u0000\u0000\u0000VW\u0005<\u0000\u0000W\f\u0001\u0000\u0000"+ + "\u0000XY\u0005>\u0000\u0000YZ\u0005=\u0000\u0000Z\u000e\u0001\u0000\u0000"+ + "\u0000[\\\u0005>\u0000\u0000\\\u0010\u0001\u0000\u0000\u0000]^\u0005I"+ + "\u0000\u0000^_\u0005F\u0000\u0000_\u0012\u0001\u0000\u0000\u0000`a\u0005"+ + "T\u0000\u0000ab\u0005H\u0000\u0000bc\u0005E\u0000\u0000cd\u0005N\u0000"+ + "\u0000d\u0014\u0001\u0000\u0000\u0000ef\u0005E\u0000\u0000fg\u0005L\u0000"+ + "\u0000gh\u0005S\u0000\u0000hi\u0005E\u0000\u0000i\u0016\u0001\u0000\u0000"+ + "\u0000jk\u0005F\u0000\u0000kl\u0005I\u0000\u0000l\u0018\u0001\u0000\u0000"+ + "\u0000mn\u0005W\u0000\u0000no\u0005H\u0000\u0000op\u0005I\u0000\u0000"+ + "pq\u0005L\u0000\u0000qr\u0005E\u0000\u0000r\u001a\u0001\u0000\u0000\u0000"+ + "st\u0005D\u0000\u0000tu\u0005O\u0000\u0000u\u001c\u0001\u0000\u0000\u0000"+ + "vw\u0005D\u0000\u0000wx\u0005O\u0000\u0000xy\u0005N\u0000\u0000yz\u0005"+ + "E\u0000\u0000z\u001e\u0001\u0000\u0000\u0000{|\u0005R\u0000\u0000|}\u0005"+ + "E\u0000\u0000}~\u0005A\u0000\u0000~\u007f\u0005D\u0000\u0000\u007f \u0001"+ + "\u0000\u0000\u0000\u0080\u0081\u0005P\u0000\u0000\u0081\u0082\u0005R\u0000"+ + "\u0000\u0082\u0083\u0005I\u0000\u0000\u0083\u0084\u0005N\u0000\u0000\u0084"+ + "\u0085\u0005T\u0000\u0000\u0085\"\u0001\u0000\u0000\u0000\u0086\u0087"+ + "\u0005R\u0000\u0000\u0087\u0088\u0005E\u0000\u0000\u0088\u0089\u0005T"+ + "\u0000\u0000\u0089\u008a\u0005U\u0000\u0000\u008a\u008b\u0005R\u0000\u0000"+ + "\u008b\u008c\u0005N\u0000\u0000\u008c$\u0001\u0000\u0000\u0000\u008d\u008f"+ + "\u0005\"\u0000\u0000\u008e\u0090\u0003=\u001e\u0000\u008f\u008e\u0001"+ + "\u0000\u0000\u0000\u0090\u0091\u0001\u0000\u0000\u0000\u0091\u008f\u0001"+ + "\u0000\u0000\u0000\u0091\u0092\u0001\u0000\u0000\u0000\u0092\u0093\u0001"+ + "\u0000\u0000\u0000\u0093\u0094\u0005\"\u0000\u0000\u0094\u0095\u0006\u0012"+ + "\u0000\u0000\u0095&\u0001\u0000\u0000\u0000\u0096\u0098\u0003;\u001d\u0000"+ + "\u0097\u0096\u0001\u0000\u0000\u0000\u0098\u0099\u0001\u0000\u0000\u0000"+ + "\u0099\u0097\u0001\u0000\u0000\u0000\u0099\u009a\u0001\u0000\u0000\u0000"+ + "\u009a(\u0001\u0000\u0000\u0000\u009b\u009c\u0005(\u0000\u0000\u009c*"+ + "\u0001\u0000\u0000\u0000\u009d\u009e\u0005)\u0000\u0000\u009e,\u0001\u0000"+ + "\u0000\u0000\u009f\u00a0\u0005+\u0000\u0000\u00a0.\u0001\u0000\u0000\u0000"+ + "\u00a1\u00a2\u0005-\u0000\u0000\u00a20\u0001\u0000\u0000\u0000\u00a3\u00a4"+ + "\u0005/\u0000\u0000\u00a42\u0001\u0000\u0000\u0000\u00a5\u00a6\u0005*"+ + "\u0000\u0000\u00a64\u0001\u0000\u0000\u0000\u00a7\u00a8\u0007\u0000\u0000"+ + "\u0000\u00a8\u00a9\u0006\u001a\u0001\u0000\u00a96\u0001\u0000\u0000\u0000"+ + "\u00aa\u00ab\u0005/\u0000\u0000\u00ab\u00ac\u0005/\u0000\u0000\u00ac\u00b0"+ + "\u0001\u0000\u0000\u0000\u00ad\u00af\b\u0001\u0000\u0000\u00ae\u00ad\u0001"+ + "\u0000\u0000\u0000\u00af\u00b2\u0001\u0000\u0000\u0000\u00b0\u00ae\u0001"+ + "\u0000\u0000\u0000\u00b0\u00b1\u0001\u0000\u0000\u0000\u00b1\u00b3\u0001"+ + "\u0000\u0000\u0000\u00b2\u00b0\u0001\u0000\u0000\u0000\u00b3\u00b4\u0006"+ + "\u001b\u0002\u0000\u00b48\u0001\u0000\u0000\u0000\u00b5\u00b6\u0002az"+ + "\u0000\u00b6:\u0001\u0000\u0000\u0000\u00b7\u00b8\u000209\u0000\u00b8"+ + "<\u0001\u0000\u0000\u0000\u00b9\u00ba\b\u0002\u0000\u0000\u00ba>\u0001"+ + "\u0000\u0000\u0000\u0004\u0000\u0091\u0099\u00b0\u0003\u0001\u0012\u0000"+ + "\u0001\u001a\u0001\u0001\u001b\u0002"; + 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); + } + } +} \ No newline at end of file diff --git a/src/main/antlr/.antlr/VSLLexer.tokens b/src/main/antlr/.antlr/VSLLexer.tokens new file mode 100644 index 0000000..56acf9f --- /dev/null +++ b/src/main/antlr/.antlr/VSLLexer.tokens @@ -0,0 +1,52 @@ +FUNCTION=1 +PROTOTYPE=2 +TYPE_INT=3 +TYPE_VOID=4 +LE=5 +LT=6 +GE=7 +GT=8 +IF=9 +THEN=10 +ELSE=11 +FIN=12 +WHILE=13 +DO=14 +DONE=15 +READ=16 +PRINT=17 +RETURN=18 +TEXT=19 +NUMBER=20 +ParO=21 +ParF=22 +PLUS=23 +MINUS=24 +DIV=25 +TIMES=26 +WS=27 +COMMENT=28 +'FUNC'=1 +'PROTO'=2 +'INT'=3 +'VOID'=4 +'<='=5 +'<'=6 +'>='=7 +'>'=8 +'IF'=9 +'THEN'=10 +'ELSE'=11 +'FI'=12 +'WHILE'=13 +'DO'=14 +'DONE'=15 +'READ'=16 +'PRINT'=17 +'RETURN'=18 +'('=21 +')'=22 +'+'=23 +'-'=24 +'/'=25 +'*'=26 diff --git a/src/main/antlr/.antlr/VSLParser.interp b/src/main/antlr/.antlr/VSLParser.interp new file mode 100644 index 0000000..98b230c --- /dev/null +++ b/src/main/antlr/.antlr/VSLParser.interp @@ -0,0 +1,77 @@ +token literal names: +null +'FUNC' +'PROTO' +'INT' +'VOID' +'<=' +'<' +'>=' +'>' +'IF' +'THEN' +'ELSE' +'FI' +'WHILE' +'DO' +'DONE' +'READ' +'PRINT' +'RETURN' +null +null +'(' +')' +'+' +'-' +'/' +'*' +null +null + +token symbolic names: +null +FUNCTION +PROTOTYPE +TYPE_INT +TYPE_VOID +LE +LT +GE +GT +IF +THEN +ELSE +FIN +WHILE +DO +DONE +READ +PRINT +RETURN +TEXT +NUMBER +ParO +ParF +PLUS +MINUS +DIV +TIMES +WS +COMMENT + +rule names: +program +function +prototype +list_instr +instruction +expression +td_expression +lit +ident +type + + +atn: +[4, 1, 28, 75, 2, 0, 7, 0, 2, 1, 7, 1, 2, 2, 7, 2, 2, 3, 7, 3, 2, 4, 7, 4, 2, 5, 7, 5, 2, 6, 7, 6, 2, 7, 7, 7, 2, 8, 7, 8, 2, 9, 7, 9, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 2, 1, 3, 1, 3, 1, 3, 1, 4, 1, 4, 1, 4, 1, 5, 1, 5, 1, 5, 1, 5, 1, 5, 1, 5, 5, 5, 47, 8, 5, 10, 5, 12, 5, 50, 9, 5, 1, 6, 1, 6, 1, 6, 1, 6, 1, 6, 1, 6, 5, 6, 58, 8, 6, 10, 6, 12, 6, 61, 9, 6, 1, 7, 1, 7, 1, 7, 1, 8, 1, 8, 1, 8, 1, 9, 1, 9, 1, 9, 1, 9, 3, 9, 73, 8, 9, 1, 9, 0, 0, 10, 0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 0, 2, 1, 0, 23, 24, 1, 0, 25, 26, 67, 0, 20, 1, 0, 0, 0, 2, 24, 1, 0, 0, 0, 4, 32, 1, 0, 0, 0, 6, 34, 1, 0, 0, 0, 8, 37, 1, 0, 0, 0, 10, 40, 1, 0, 0, 0, 12, 51, 1, 0, 0, 0, 14, 62, 1, 0, 0, 0, 16, 65, 1, 0, 0, 0, 18, 72, 1, 0, 0, 0, 20, 21, 3, 2, 1, 0, 21, 22, 5, 0, 0, 1, 22, 23, 6, 0, -1, 0, 23, 1, 1, 0, 0, 0, 24, 25, 5, 1, 0, 0, 25, 26, 3, 18, 9, 0, 26, 27, 3, 16, 8, 0, 27, 28, 5, 21, 0, 0, 28, 29, 5, 22, 0, 0, 29, 30, 3, 6, 3, 0, 30, 31, 6, 1, -1, 0, 31, 3, 1, 0, 0, 0, 32, 33, 1, 0, 0, 0, 33, 5, 1, 0, 0, 0, 34, 35, 3, 8, 4, 0, 35, 36, 6, 3, -1, 0, 36, 7, 1, 0, 0, 0, 37, 38, 5, 18, 0, 0, 38, 39, 3, 10, 5, 0, 39, 9, 1, 0, 0, 0, 40, 41, 3, 12, 6, 0, 41, 48, 6, 5, -1, 0, 42, 43, 7, 0, 0, 0, 43, 44, 3, 12, 6, 0, 44, 45, 6, 5, -1, 0, 45, 47, 1, 0, 0, 0, 46, 42, 1, 0, 0, 0, 47, 50, 1, 0, 0, 0, 48, 46, 1, 0, 0, 0, 48, 49, 1, 0, 0, 0, 49, 11, 1, 0, 0, 0, 50, 48, 1, 0, 0, 0, 51, 52, 3, 14, 7, 0, 52, 59, 6, 6, -1, 0, 53, 54, 7, 1, 0, 0, 54, 55, 3, 14, 7, 0, 55, 56, 6, 6, -1, 0, 56, 58, 1, 0, 0, 0, 57, 53, 1, 0, 0, 0, 58, 61, 1, 0, 0, 0, 59, 57, 1, 0, 0, 0, 59, 60, 1, 0, 0, 0, 60, 13, 1, 0, 0, 0, 61, 59, 1, 0, 0, 0, 62, 63, 5, 20, 0, 0, 63, 64, 6, 7, -1, 0, 64, 15, 1, 0, 0, 0, 65, 66, 5, 19, 0, 0, 66, 67, 6, 8, -1, 0, 67, 17, 1, 0, 0, 0, 68, 69, 5, 3, 0, 0, 69, 73, 6, 9, -1, 0, 70, 71, 5, 4, 0, 0, 71, 73, 6, 9, -1, 0, 72, 68, 1, 0, 0, 0, 72, 70, 1, 0, 0, 0, 73, 19, 1, 0, 0, 0, 3, 48, 59, 72] \ No newline at end of file diff --git a/src/main/antlr/.antlr/VSLParser.java b/src/main/antlr/.antlr/VSLParser.java new file mode 100644 index 0000000..297a228 --- /dev/null +++ b/src/main/antlr/.antlr/VSLParser.java @@ -0,0 +1,674 @@ +// Generated from /home/tuanvu/Desktop/M1/S8/PDS/tp2-vsl-pds/src/main/antlr/VSLParser.g by ANTLR 4.13.1 + + package TP2; + + import TP2.Program.Program; + import TP2.SymTable.SymTable; + +import org.antlr.v4.runtime.atn.*; +import org.antlr.v4.runtime.dfa.DFA; +import org.antlr.v4.runtime.*; +import org.antlr.v4.runtime.misc.*; +import org.antlr.v4.runtime.tree.*; +import java.util.List; +import java.util.Iterator; +import java.util.ArrayList; + +@SuppressWarnings({"all", "warnings", "unchecked", "unused", "cast", "CheckReturnValue"}) +public class VSLParser extends Parser { + static { RuntimeMetaData.checkVersion("4.13.1", RuntimeMetaData.VERSION); } + + protected static final DFA[] _decisionToDFA; + protected static final PredictionContextCache _sharedContextCache = + new PredictionContextCache(); + public static final int + FUNCTION=1, PROTOTYPE=2, TYPE_INT=3, TYPE_VOID=4, LE=5, LT=6, GE=7, GT=8, + IF=9, THEN=10, ELSE=11, FIN=12, WHILE=13, DO=14, DONE=15, READ=16, PRINT=17, + RETURN=18, TEXT=19, NUMBER=20, ParO=21, ParF=22, PLUS=23, MINUS=24, DIV=25, + TIMES=26, WS=27, COMMENT=28; + public static final int + RULE_program = 0, RULE_function = 1, RULE_prototype = 2, RULE_list_instr = 3, + RULE_instruction = 4, RULE_expression = 5, RULE_td_expression = 6, RULE_lit = 7, + RULE_ident = 8, RULE_type = 9; + private static String[] makeRuleNames() { + return new String[] { + "program", "function", "prototype", "list_instr", "instruction", "expression", + "td_expression", "lit", "ident", "type" + }; + } + public static final String[] ruleNames = makeRuleNames(); + + private static String[] makeLiteralNames() { + return new String[] { + null, "'FUNC'", "'PROTO'", "'INT'", "'VOID'", "'<='", "'<'", "'>='", + "'>'", "'IF'", "'THEN'", "'ELSE'", "'FI'", "'WHILE'", "'DO'", "'DONE'", + "'READ'", "'PRINT'", "'RETURN'", null, null, "'('", "')'", "'+'", "'-'", + "'/'", "'*'" + }; + } + private static final String[] _LITERAL_NAMES = makeLiteralNames(); + private static String[] makeSymbolicNames() { + return new String[] { + null, "FUNCTION", "PROTOTYPE", "TYPE_INT", "TYPE_VOID", "LE", "LT", "GE", + "GT", "IF", "THEN", "ELSE", "FIN", "WHILE", "DO", "DONE", "READ", "PRINT", + "RETURN", "TEXT", "NUMBER", "ParO", "ParF", "PLUS", "MINUS", "DIV", "TIMES", + "WS", "COMMENT" + }; + } + 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; + } + + @Override + public String getGrammarFileName() { return "VSLParser.g"; } + + @Override + public String[] getRuleNames() { return ruleNames; } + + @Override + public String getSerializedATN() { return _serializedATN; } + + @Override + public ATN getATN() { return _ATN; } + + public VSLParser(TokenStream input) { + super(input); + _interp = new ParserATNSimulator(this,_ATN,_decisionToDFA,_sharedContextCache); + } + + @SuppressWarnings("CheckReturnValue") + public static class ProgramContext extends ParserRuleContext { + public Program.Program p; + public FunctionContext func; + public TerminalNode EOF() { return getToken(VSLParser.EOF, 0); } + public FunctionContext function() { + return getRuleContext(FunctionContext.class,0); + } + public ProgramContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + @Override public int getRuleIndex() { return RULE_program; } + } + + public final ProgramContext program() throws RecognitionException { + ProgramContext _localctx = new ProgramContext(_ctx, getState()); + enterRule(_localctx, 0, RULE_program); + try { + enterOuterAlt(_localctx, 1); + { + setState(20); + ((ProgramContext)_localctx).func = function(); + setState(21); + match(EOF); + ((ProgramContext)_localctx).p = new Program.Program(((ProgramContext)_localctx).func.out); + } + } + catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } + finally { + exitRule(); + } + return _localctx; + } + + @SuppressWarnings("CheckReturnValue") + public static class FunctionContext extends ParserRuleContext { + public List out; + public TypeContext t; + public IdentContext i; + public List_instrContext instrs; + public TerminalNode FUNCTION() { return getToken(VSLParser.FUNCTION, 0); } + public TerminalNode ParO() { return getToken(VSLParser.ParO, 0); } + public TerminalNode ParF() { return getToken(VSLParser.ParF, 0); } + public TypeContext type() { + return getRuleContext(TypeContext.class,0); + } + public IdentContext ident() { + return getRuleContext(IdentContext.class,0); + } + public List_instrContext list_instr() { + return getRuleContext(List_instrContext.class,0); + } + public FunctionContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + @Override public int getRuleIndex() { return RULE_function; } + } + + public final FunctionContext function() throws RecognitionException { + FunctionContext _localctx = new FunctionContext(_ctx, getState()); + enterRule(_localctx, 2, RULE_function); + + SymTable.SymTable sym_table = new SymTable.SymTable(); + + try { + enterOuterAlt(_localctx, 1); + { + setState(24); + match(FUNCTION); + setState(25); + ((FunctionContext)_localctx).t = type(); + setState(26); + ((FunctionContext)_localctx).i = ident(); + setState(27); + match(ParO); + setState(28); + match(ParF); + setState(29); + ((FunctionContext)_localctx).instrs = list_instr(sym_table); + + _localctx.out.add(new Program.Function(((FunctionContext)_localctx).t.return_type, ((FunctionContext)_localctx).i.out, ((FunctionContext)_localctx).instrs.out)); + + } + } + catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } + finally { + exitRule(); + } + return _localctx; + } + + @SuppressWarnings("CheckReturnValue") + public static class PrototypeContext extends ParserRuleContext { + public Program.Program p; + public PrototypeContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + @Override public int getRuleIndex() { return RULE_prototype; } + } + + public final PrototypeContext prototype() throws RecognitionException { + PrototypeContext _localctx = new PrototypeContext(_ctx, getState()); + enterRule(_localctx, 4, RULE_prototype); + try { + enterOuterAlt(_localctx, 1); + { + } + } + catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } + finally { + exitRule(); + } + return _localctx; + } + + @SuppressWarnings("CheckReturnValue") + public static class List_instrContext extends ParserRuleContext { + public SymTable.SymTable table; + public List out; + public InstructionContext instruction; + public InstructionContext instruction() { + return getRuleContext(InstructionContext.class,0); + } + public List_instrContext(ParserRuleContext parent, int invokingState) { super(parent, invokingState); } + public List_instrContext(ParserRuleContext parent, int invokingState, SymTable.SymTable table) { + super(parent, invokingState); + this.table = table; + } + @Override public int getRuleIndex() { return RULE_list_instr; } + } + + public final List_instrContext list_instr(SymTable.SymTable table) throws RecognitionException { + List_instrContext _localctx = new List_instrContext(_ctx, getState(), table); + enterRule(_localctx, 6, RULE_list_instr); + + List instructions = new List(); + + try { + enterOuterAlt(_localctx, 1); + { + setState(34); + ((List_instrContext)_localctx).instruction = instruction(table); + + instructions.add(((List_instrContext)_localctx).instruction.out); + ((List_instrContext)_localctx).out = instructions; + + } + } + catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } + finally { + exitRule(); + } + return _localctx; + } + + @SuppressWarnings("CheckReturnValue") + public static class InstructionContext extends ParserRuleContext { + public SymTable.SymTable table; + public List out; + public TerminalNode RETURN() { return getToken(VSLParser.RETURN, 0); } + public ExpressionContext expression() { + return getRuleContext(ExpressionContext.class,0); + } + public InstructionContext(ParserRuleContext parent, int invokingState) { super(parent, invokingState); } + public InstructionContext(ParserRuleContext parent, int invokingState, SymTable.SymTable table) { + super(parent, invokingState); + this.table = table; + } + @Override public int getRuleIndex() { return RULE_instruction; } + } + + public final InstructionContext instruction(SymTable.SymTable table) throws RecognitionException { + InstructionContext _localctx = new InstructionContext(_ctx, getState(), table); + enterRule(_localctx, 8, RULE_instruction); + try { + enterOuterAlt(_localctx, 1); + { + setState(37); + match(RETURN); + setState(38); + expression(SymTable table); + } + } + catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } + finally { + exitRule(); + } + return _localctx; + } + + @SuppressWarnings("CheckReturnValue") + public static class ExpressionContext extends ParserRuleContext { + public SymTable.SymTable table; + public Type return_Type; + public Expression out; + public Td_expressionContext left; + public Token op; + public Td_expressionContext right; + public List td_expression() { + return getRuleContexts(Td_expressionContext.class); + } + public Td_expressionContext td_expression(int i) { + return getRuleContext(Td_expressionContext.class,i); + } + public List PLUS() { return getTokens(VSLParser.PLUS); } + public TerminalNode PLUS(int i) { + return getToken(VSLParser.PLUS, i); + } + public List MINUS() { return getTokens(VSLParser.MINUS); } + public TerminalNode MINUS(int i) { + return getToken(VSLParser.MINUS, i); + } + public ExpressionContext(ParserRuleContext parent, int invokingState) { super(parent, invokingState); } + public ExpressionContext(ParserRuleContext parent, int invokingState, SymTable.SymTable table) { + super(parent, invokingState); + this.table = table; + } + @Override public int getRuleIndex() { return RULE_expression; } + } + + public final ExpressionContext expression(SymTable.SymTable table) throws RecognitionException { + ExpressionContext _localctx = new ExpressionContext(_ctx, getState(), table); + enterRule(_localctx, 10, RULE_expression); + int _la; + try { + enterOuterAlt(_localctx, 1); + { + setState(40); + ((ExpressionContext)_localctx).left = td_expression(table); + + ((ExpressionContext)_localctx).out = ((ExpressionContext)_localctx).left.out; + ((ExpressionContext)_localctx).return_Type = ((ExpressionContext)_localctx).left.return_Type; + + setState(48); + _errHandler.sync(this); + _la = _input.LA(1); + while (_la==PLUS || _la==MINUS) { + { + { + setState(42); + ((ExpressionContext)_localctx).op = _input.LT(1); + _la = _input.LA(1); + if ( !(_la==PLUS || _la==MINUS) ) { + ((ExpressionContext)_localctx).op = (Token)_errHandler.recoverInline(this); + } + else { + if ( _input.LA(1)==Token.EOF ) matchedEOF = true; + _errHandler.reportMatch(this); + consume(); + } + setState(43); + ((ExpressionContext)_localctx).right = td_expression(table); + + ((ExpressionContext)_localctx).out = new Program.BinopExpression( + switch(((ExpressionContext)_localctx).op.getType()) { + case PLUS -> Op.PLUS; + case MINUS -> Op.MINUS; + default -> throw new IllegalArgumentException("Unknown operator"); + }, + ((ExpressionContext)_localctx).left.out, + ((ExpressionContext)_localctx).right.out + ); + ((ExpressionContext)_localctx).return_Type = ((ExpressionContext)_localctx).right.return_Type; + + } + } + setState(50); + _errHandler.sync(this); + _la = _input.LA(1); + } + } + } + catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } + finally { + exitRule(); + } + return _localctx; + } + + @SuppressWarnings("CheckReturnValue") + public static class Td_expressionContext extends ParserRuleContext { + public SymTable.SymTable table; + public Type return_Type; + public Expression out; + public LitContext left; + public Token op; + public LitContext right; + public List lit() { + return getRuleContexts(LitContext.class); + } + public LitContext lit(int i) { + return getRuleContext(LitContext.class,i); + } + public List TIMES() { return getTokens(VSLParser.TIMES); } + public TerminalNode TIMES(int i) { + return getToken(VSLParser.TIMES, i); + } + public List DIV() { return getTokens(VSLParser.DIV); } + public TerminalNode DIV(int i) { + return getToken(VSLParser.DIV, i); + } + public Td_expressionContext(ParserRuleContext parent, int invokingState) { super(parent, invokingState); } + public Td_expressionContext(ParserRuleContext parent, int invokingState, SymTable.SymTable table) { + super(parent, invokingState); + this.table = table; + } + @Override public int getRuleIndex() { return RULE_td_expression; } + } + + public final Td_expressionContext td_expression(SymTable.SymTable table) throws RecognitionException { + Td_expressionContext _localctx = new Td_expressionContext(_ctx, getState(), table); + enterRule(_localctx, 12, RULE_td_expression); + int _la; + try { + enterOuterAlt(_localctx, 1); + { + setState(51); + ((Td_expressionContext)_localctx).left = lit(); + + ((Td_expressionContext)_localctx).out = ((Td_expressionContext)_localctx).left.out; + ((Td_expressionContext)_localctx).return_Type = ((Td_expressionContext)_localctx).left.return_Type; + + setState(59); + _errHandler.sync(this); + _la = _input.LA(1); + while (_la==DIV || _la==TIMES) { + { + { + setState(53); + ((Td_expressionContext)_localctx).op = _input.LT(1); + _la = _input.LA(1); + if ( !(_la==DIV || _la==TIMES) ) { + ((Td_expressionContext)_localctx).op = (Token)_errHandler.recoverInline(this); + } + else { + if ( _input.LA(1)==Token.EOF ) matchedEOF = true; + _errHandler.reportMatch(this); + consume(); + } + setState(54); + ((Td_expressionContext)_localctx).right = lit(); + + ((Td_expressionContext)_localctx).out = new Program.BinopExpression( + switch(((Td_expressionContext)_localctx).op.getType()) { + case TIMES -> Op.TIMES; + case DIV -> Op.DIV; + default -> throw new IllegalArgumentException("Unknown operator"); + }, + ((Td_expressionContext)_localctx).left.out, + ((Td_expressionContext)_localctx).right.out + ); + ((Td_expressionContext)_localctx).return_Type = ((Td_expressionContext)_localctx).right.return_Type; + + } + } + setState(61); + _errHandler.sync(this); + _la = _input.LA(1); + } + } + } + catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } + finally { + exitRule(); + } + return _localctx; + } + + @SuppressWarnings("CheckReturnValue") + public static class LitContext extends ParserRuleContext { + public Expression out; + public Type return_Type; + public Token NUMBER; + public TerminalNode NUMBER() { return getToken(VSLParser.NUMBER, 0); } + public LitContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + @Override public int getRuleIndex() { return RULE_lit; } + } + + public final LitContext lit() throws RecognitionException { + LitContext _localctx = new LitContext(_ctx, getState()); + enterRule(_localctx, 14, RULE_lit); + try { + enterOuterAlt(_localctx, 1); + { + setState(62); + ((LitContext)_localctx).NUMBER = match(NUMBER); + + ((LitContext)_localctx).return_Type = new Program.Type_int(); + ((LitContext)_localctx).out = new Program.Const((((LitContext)_localctx).NUMBER!=null?Integer.valueOf(((LitContext)_localctx).NUMBER.getText()):0)); + + } + } + catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } + finally { + exitRule(); + } + return _localctx; + } + + @SuppressWarnings("CheckReturnValue") + public static class IdentContext extends ParserRuleContext { + public String out; + public Token TEXT; + public TerminalNode TEXT() { return getToken(VSLParser.TEXT, 0); } + public IdentContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + @Override public int getRuleIndex() { return RULE_ident; } + } + + public final IdentContext ident() throws RecognitionException { + IdentContext _localctx = new IdentContext(_ctx, getState()); + enterRule(_localctx, 16, RULE_ident); + try { + enterOuterAlt(_localctx, 1); + { + setState(65); + ((IdentContext)_localctx).TEXT = match(TEXT); + ((IdentContext)_localctx).out = ((IdentContext)_localctx).TEXT.getText(); + } + } + catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } + finally { + exitRule(); + } + return _localctx; + } + + @SuppressWarnings("CheckReturnValue") + public static class TypeContext extends ParserRuleContext { + public Type return_type; + public TerminalNode TYPE_INT() { return getToken(VSLParser.TYPE_INT, 0); } + public TerminalNode TYPE_VOID() { return getToken(VSLParser.TYPE_VOID, 0); } + public TypeContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + @Override public int getRuleIndex() { return RULE_type; } + } + + public final TypeContext type() throws RecognitionException { + TypeContext _localctx = new TypeContext(_ctx, getState()); + enterRule(_localctx, 18, RULE_type); + try { + setState(72); + _errHandler.sync(this); + switch (_input.LA(1)) { + case TYPE_INT: + enterOuterAlt(_localctx, 1); + { + setState(68); + match(TYPE_INT); + ((TypeContext)_localctx).return_type = new Program.Type.Type_int(); + } + break; + case TYPE_VOID: + enterOuterAlt(_localctx, 2); + { + setState(70); + match(TYPE_VOID); + ((TypeContext)_localctx).return_type = new Program.Type.Type_void(); + } + break; + default: + throw new NoViableAltException(this); + } + } + catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } + finally { + exitRule(); + } + return _localctx; + } + + public static final String _serializedATN = + "\u0004\u0001\u001cK\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\u0001\u0000\u0001\u0000\u0001\u0000\u0001\u0000"+ + "\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001"+ + "\u0001\u0001\u0001\u0001\u0001\u0002\u0001\u0002\u0001\u0003\u0001\u0003"+ + "\u0001\u0003\u0001\u0004\u0001\u0004\u0001\u0004\u0001\u0005\u0001\u0005"+ + "\u0001\u0005\u0001\u0005\u0001\u0005\u0001\u0005\u0005\u0005/\b\u0005"+ + "\n\u0005\f\u00052\t\u0005\u0001\u0006\u0001\u0006\u0001\u0006\u0001\u0006"+ + "\u0001\u0006\u0001\u0006\u0005\u0006:\b\u0006\n\u0006\f\u0006=\t\u0006"+ + "\u0001\u0007\u0001\u0007\u0001\u0007\u0001\b\u0001\b\u0001\b\u0001\t\u0001"+ + "\t\u0001\t\u0001\t\u0003\tI\b\t\u0001\t\u0000\u0000\n\u0000\u0002\u0004"+ + "\u0006\b\n\f\u000e\u0010\u0012\u0000\u0002\u0001\u0000\u0017\u0018\u0001"+ + "\u0000\u0019\u001aC\u0000\u0014\u0001\u0000\u0000\u0000\u0002\u0018\u0001"+ + "\u0000\u0000\u0000\u0004 \u0001\u0000\u0000\u0000\u0006\"\u0001\u0000"+ + "\u0000\u0000\b%\u0001\u0000\u0000\u0000\n(\u0001\u0000\u0000\u0000\f3"+ + "\u0001\u0000\u0000\u0000\u000e>\u0001\u0000\u0000\u0000\u0010A\u0001\u0000"+ + "\u0000\u0000\u0012H\u0001\u0000\u0000\u0000\u0014\u0015\u0003\u0002\u0001"+ + "\u0000\u0015\u0016\u0005\u0000\u0000\u0001\u0016\u0017\u0006\u0000\uffff"+ + "\uffff\u0000\u0017\u0001\u0001\u0000\u0000\u0000\u0018\u0019\u0005\u0001"+ + "\u0000\u0000\u0019\u001a\u0003\u0012\t\u0000\u001a\u001b\u0003\u0010\b"+ + "\u0000\u001b\u001c\u0005\u0015\u0000\u0000\u001c\u001d\u0005\u0016\u0000"+ + "\u0000\u001d\u001e\u0003\u0006\u0003\u0000\u001e\u001f\u0006\u0001\uffff"+ + "\uffff\u0000\u001f\u0003\u0001\u0000\u0000\u0000 !\u0001\u0000\u0000\u0000"+ + "!\u0005\u0001\u0000\u0000\u0000\"#\u0003\b\u0004\u0000#$\u0006\u0003\uffff"+ + "\uffff\u0000$\u0007\u0001\u0000\u0000\u0000%&\u0005\u0012\u0000\u0000"+ + "&\'\u0003\n\u0005\u0000\'\t\u0001\u0000\u0000\u0000()\u0003\f\u0006\u0000"+ + ")0\u0006\u0005\uffff\uffff\u0000*+\u0007\u0000\u0000\u0000+,\u0003\f\u0006"+ + "\u0000,-\u0006\u0005\uffff\uffff\u0000-/\u0001\u0000\u0000\u0000.*\u0001"+ + "\u0000\u0000\u0000/2\u0001\u0000\u0000\u00000.\u0001\u0000\u0000\u0000"+ + "01\u0001\u0000\u0000\u00001\u000b\u0001\u0000\u0000\u000020\u0001\u0000"+ + "\u0000\u000034\u0003\u000e\u0007\u00004;\u0006\u0006\uffff\uffff\u0000"+ + "56\u0007\u0001\u0000\u000067\u0003\u000e\u0007\u000078\u0006\u0006\uffff"+ + "\uffff\u00008:\u0001\u0000\u0000\u000095\u0001\u0000\u0000\u0000:=\u0001"+ + "\u0000\u0000\u0000;9\u0001\u0000\u0000\u0000;<\u0001\u0000\u0000\u0000"+ + "<\r\u0001\u0000\u0000\u0000=;\u0001\u0000\u0000\u0000>?\u0005\u0014\u0000"+ + "\u0000?@\u0006\u0007\uffff\uffff\u0000@\u000f\u0001\u0000\u0000\u0000"+ + "AB\u0005\u0013\u0000\u0000BC\u0006\b\uffff\uffff\u0000C\u0011\u0001\u0000"+ + "\u0000\u0000DE\u0005\u0003\u0000\u0000EI\u0006\t\uffff\uffff\u0000FG\u0005"+ + "\u0004\u0000\u0000GI\u0006\t\uffff\uffff\u0000HD\u0001\u0000\u0000\u0000"+ + "HF\u0001\u0000\u0000\u0000I\u0013\u0001\u0000\u0000\u0000\u00030;H"; + 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); + } + } +} \ No newline at end of file diff --git a/src/main/antlr/.antlr/VSLParser.tokens b/src/main/antlr/.antlr/VSLParser.tokens new file mode 100644 index 0000000..56acf9f --- /dev/null +++ b/src/main/antlr/.antlr/VSLParser.tokens @@ -0,0 +1,52 @@ +FUNCTION=1 +PROTOTYPE=2 +TYPE_INT=3 +TYPE_VOID=4 +LE=5 +LT=6 +GE=7 +GT=8 +IF=9 +THEN=10 +ELSE=11 +FIN=12 +WHILE=13 +DO=14 +DONE=15 +READ=16 +PRINT=17 +RETURN=18 +TEXT=19 +NUMBER=20 +ParO=21 +ParF=22 +PLUS=23 +MINUS=24 +DIV=25 +TIMES=26 +WS=27 +COMMENT=28 +'FUNC'=1 +'PROTO'=2 +'INT'=3 +'VOID'=4 +'<='=5 +'<'=6 +'>='=7 +'>'=8 +'IF'=9 +'THEN'=10 +'ELSE'=11 +'FI'=12 +'WHILE'=13 +'DO'=14 +'DONE'=15 +'READ'=16 +'PRINT'=17 +'RETURN'=18 +'('=21 +')'=22 +'+'=23 +'-'=24 +'/'=25 +'*'=26 diff --git a/src/main/antlr/VSLParser.g b/src/main/antlr/VSLParser.g index e43744c..d3fed92 100644 --- a/src/main/antlr/VSLParser.g +++ b/src/main/antlr/VSLParser.g @@ -10,6 +10,7 @@ options { package TP2; import TP2.Program.Program; + import TP2.SymTable.SymTable; } // On syntax error, raise exception rather than silently recovery @@ -23,21 +24,24 @@ options { program returns [Program.Program p] : func=function EOF - {$p = new Program.Program($func.p);} + {$p = new Program.Program($func.out);} ; -function returns [Program.List out] +function returns [List out] @init{ - SymTable sym_table = new SymTable(); + SymTable.SymTable sym_table = new SymTable.SymTable(); }: - FUNCTION type ident ParO ParF list_instr[sym_table] + FUNCTION t=type i=ident ParO ParF instrs=list_instr[sym_table] + { + $out.add(new Program.Function($t.return_type, $i.out, $instrs.out)); + } ; prototype returns [Program.Program p]: ; -list_instr [SymTable table] returns [Program.List out] +list_instr [SymTable.SymTable table] returns [List out] @init{ List instructions = new List(); }: @@ -47,46 +51,60 @@ list_instr [SymTable table] returns [Program.List out] } ; -instruction [SymTable table] returns [List out]: +instruction [SymTable.SymTable table] returns [List out]: RETURN expression[SymTable table] ; -expression [SymTable table] returns [Type return_Type, Expression out]: - //Binop(op,exp,exp) et this.return_type est meme que left ou right - left_exp=expression[table] op=(PLUS| MINUS | DIV | TIMES) right_exp=expression[table]{ - switch($op.getType()){ - case PLUS: - $out = new Program.BinopExpression("PLUS",$left_exp,$right_exp); - break; - case MINUS: - $out = new Program.BinopExpression("MINUS", $l.out, $r.out); - break; - case TIMES: - $out = new Program.BinopExpression("TIMES", $l.out, $r.out); - break; - case DIV: - $out = new Program.BinopExpression("DIV", $l.out, $r.out); - break; - } - $return_Type = $right_exp.return_Type - } - | - //Const(lit) et peut-etre var aussi ( dans le futur )? - lit{ - $out=lit.out; - $return_Type= $lit.return_Type +expression [SymTable.SymTable table] returns [Type return_Type, Expression out]: +//Fix LL1 and compare add and sub first + left=td_expression[table] { + $out = $left.out; + $return_Type = $left.return_Type; } +//maybe it will be binop(s) with add/sub here +//if it is mul or div, they are all gonna be left=td_expression[table] to handle + (op=(PLUS | MINUS) right=td_expression[table] { + $out = new Program.BinopExpression( + switch($op.getType()) { + case PLUS -> Op.PLUS; + case MINUS -> Op.MINUS; + default -> throw new IllegalArgumentException("Unknown operator"); + }, + $left.out, + $right.out + ); + $return_Type = $right.return_Type; + })* ; -lit returns [Expression out, return_Type retType]: +td_expression [SymTable.SymTable table] returns [Type return_Type, Expression out]: + left=lit { + $out = $left.out; + $return_Type = $left.return_Type; + } + (op=(TIMES | DIV) right=lit { + $out = new Program.BinopExpression( + switch($op.getType()) { + case TIMES -> Op.TIMES; + case DIV -> Op.DIV; + default -> throw new IllegalArgumentException("Unknown operator"); + }, + $left.out, + $right.out + ); + $return_Type = $right.return_Type; + })* +; + +lit returns [Expression out, Type return_Type]: NUMBER { - $return_Type = new Program.IntType(); - $out = new Program.IntergerNum($NUMBER.int); + $return_Type = new Program.Type_int(); + $out = new Program.Const($NUMBER.int); } ; -ident returns [String ident]: - TEXT { $ident = $TEXT.getText(); } +ident returns [String out]: + TEXT { $out = $TEXT.getText(); } ; type returns [Type return_type]: diff --git a/src/main/java/TP2/Main.java b/src/main/java/TP2/Main.java index c8d89b5..f11dd26 100644 --- a/src/main/java/TP2/Main.java +++ b/src/main/java/TP2/Main.java @@ -31,7 +31,7 @@ public class Main { VSLParser parser = new VSLParser(tokens); // Parse - Program ast = parser.program(); + ProgramImpl ast = parser.program(); // Pretty-print the program (to debug parsing) System.err.println("todo " + ast); diff --git a/src/main/java/TP2/asd/ExprEval.java b/src/main/java/TP2/asd/ExprEval.java new file mode 100644 index 0000000..e14e802 --- /dev/null +++ b/src/main/java/TP2/asd/ExprEval.java @@ -0,0 +1,23 @@ +package TP2.asd; + +import java.util.Map; + +public class ExprEval implements ExprVisitor,Integer> { + + @Override + public Integer visitConst(Const c, Map h) { + return c.c(); + } + + @Override + public Integer visitBinOp(BinopExpression e, Map h) { + switch(e.op()){ + case Op.PLUS: return e.e1().accept(this, h)+e.e2().accept(this, h); + case Op.MINUS: return e.e1().accept(this, h)-e.e2().accept(this, h); + case Op.TIMES: return e.e1().accept(this, h)*e.e2().accept(this, h); + case Op.DIV: return e.e1().accept(this, h)/e.e2().accept(this, h); + default: throw new IllegalArgumentException(); + } + } + +} \ No newline at end of file diff --git a/src/main/java/TP2/asd/Interface.java b/src/main/java/TP2/asd/Interface.java index ad0cc1c..4f85f53 100644 --- a/src/main/java/TP2/asd/Interface.java +++ b/src/main/java/TP2/asd/Interface.java @@ -1,9 +1,38 @@ package TP2.asd; +import java.util.Map; + +interface Expression { + public S accept(ExprVisitor v, H h); +} + interface Instruction { - public String prettyprinter(); + public S accept(InstrVisitor v, H h); +} + +interface ProgramVisitor { + public S visitProgram(Program p, H h); + public S visitFunction(Function f, H h); +} + +interface InstrVisitor{ + public S visitReturn(Return_instr e, H h); +} + +interface ExprVisitor { + public S visitConst(Const e,H h); + public S visitBinOp(BinopExpression e, H h); } interface Type{ public String prettyprinter(); +} + +enum Op {PLUS, MINUS, TIMES,DIV} + +//Eval +interface ExprEval extends ExprVisitor, Integer> { +} + +interface TypeCheck extends ExprVisitor, Type> { } \ No newline at end of file diff --git a/src/main/java/TP2/asd/Program.java b/src/main/java/TP2/asd/Program.java index b132c8b..c84a301 100644 --- a/src/main/java/TP2/asd/Program.java +++ b/src/main/java/TP2/asd/Program.java @@ -1,29 +1,40 @@ package TP2.asd; import java.util.ArrayList; +import java.util.Map; +import java.util.Stack; import java.util.stream.Collectors; public record Program(ArrayList functions) { - + } - record Function(Type type, String nom, ArrayList instructions){ - public String prettyprinter(){ - return "FUNC " +type.prettyprinter() +" "+ nom - + instructions.stream().map(Instruction::prettyprinter).collect(Collectors.joining("\n")); - // Map: appel prettyprinter pour isntruction, combiner dans 1 paragraph avec \n au milieu + public Function(Type type, String name, Instruction instruction) { + this(type, name, new ArrayList<>() {{ add(instruction); }}); } -} -record Return_instr(Expression e) implements Instruction{ - public String prettyprinter(){ - return "RETURN" +e.prettyprinter(); + + public S accept(ProgramVisitor v, H h) { + return v.visitFunction(this, h); } } -record Expression(){ - public String prettyprinter(){ - return ""; +record Const(int c) implements Expression{ + public S accept(ExprVisitor v, H h) { + return v.visitConst(this, h); + } +} + +record BinopExpression(Op op,Expression e1, Expression e2) implements Expression{ + public S accept(ExprVisitor v, H h) { + return v.visitBinOp(this, h); + } + +} + +record Return_instr(Expression e) implements Instruction{ + public S accept(InstrVisitor v, H h) { + return v.visitReturn(this,h); } } @@ -37,4 +48,4 @@ record Type_int() implements Type{ public String prettyprinter() { return "INT"; } -} \ No newline at end of file +} diff --git a/src/main/java/TP2/asd/SymTable.java b/src/main/java/TP2/asd/SymTable.java new file mode 100644 index 0000000..25f6e0f --- /dev/null +++ b/src/main/java/TP2/asd/SymTable.java @@ -0,0 +1,7 @@ +package TP2.asd; + +public class SymTable { + public SymTable(){ + + } +}