From 64c41122d057279fc7fc43f05f05be24b96262e7 Mon Sep 17 00:00:00 2001 From: Marvin Kaiser Date: Tue, 29 Oct 2019 09:51:43 +0100 Subject: [PATCH 1/5] Rewrote grammar, implemented two visitors --- .../hsrm/compiler/Klang/.antlr/Klang.interp | 23 +- .../hsrm/compiler/Klang/.antlr/Klang.tokens | 18 +- .../compiler/Klang/.antlr/KlangLexer.interp | 23 +- .../compiler/Klang/.antlr/KlangLexer.java | 30 +- .../compiler/Klang/.antlr/KlangLexer.tokens | 18 +- .../compiler/Klang/.antlr/KlangParser.java | 298 +++++++++++++++--- .../antlr4/de/hsrm/compiler/Klang/Klang.g4 | 37 ++- .../de/hsrm/compiler/Klang/JSVisitor.java | 71 +++++ .../java/de/hsrm/compiler/Klang/Klang.java | 7 +- .../java/de/hsrm/compiler/Klang/Value.java | 55 ++++ .../java/de/hsrm/compiler/Klang/Visitor.java | 62 +++- 11 files changed, 539 insertions(+), 103 deletions(-) create mode 100644 src/main/java/de/hsrm/compiler/Klang/JSVisitor.java create mode 100644 src/main/java/de/hsrm/compiler/Klang/Value.java diff --git a/src/main/antlr4/de/hsrm/compiler/Klang/.antlr/Klang.interp b/src/main/antlr4/de/hsrm/compiler/Klang/.antlr/Klang.interp index 8c41048..2bb4da2 100644 --- a/src/main/antlr4/de/hsrm/compiler/Klang/.antlr/Klang.interp +++ b/src/main/antlr4/de/hsrm/compiler/Klang/.antlr/Klang.interp @@ -1,20 +1,33 @@ token literal names: null -null +'print' +';' '*' +'+' +'-' +'%' +null null token symbolic names: null -INTEGER_LITERAL +PRINT +SCOL MULT +ADD +SUB +MOD +INTEGER_LITERAL WS rule names: parse -multiplicativeExpr -unaryExpression +block +statement +print +expression +atom atn: -[3, 24715, 42794, 33075, 47597, 16764, 15335, 30598, 22884, 3, 5, 21, 4, 2, 9, 2, 4, 3, 9, 3, 4, 4, 9, 4, 3, 2, 3, 2, 3, 3, 3, 3, 3, 3, 7, 3, 14, 10, 3, 12, 3, 14, 3, 17, 11, 3, 3, 4, 3, 4, 3, 4, 2, 2, 5, 2, 4, 6, 2, 2, 2, 18, 2, 8, 3, 2, 2, 2, 4, 10, 3, 2, 2, 2, 6, 18, 3, 2, 2, 2, 8, 9, 5, 4, 3, 2, 9, 3, 3, 2, 2, 2, 10, 15, 5, 6, 4, 2, 11, 12, 7, 4, 2, 2, 12, 14, 5, 6, 4, 2, 13, 11, 3, 2, 2, 2, 14, 17, 3, 2, 2, 2, 15, 13, 3, 2, 2, 2, 15, 16, 3, 2, 2, 2, 16, 5, 3, 2, 2, 2, 17, 15, 3, 2, 2, 2, 18, 19, 7, 3, 2, 2, 19, 7, 3, 2, 2, 2, 3, 15] \ No newline at end of file +[3, 24715, 42794, 33075, 47597, 16764, 15335, 30598, 22884, 3, 10, 47, 4, 2, 9, 2, 4, 3, 9, 3, 4, 4, 9, 4, 4, 5, 9, 5, 4, 6, 9, 6, 4, 7, 9, 7, 3, 2, 3, 2, 3, 3, 7, 3, 18, 10, 3, 12, 3, 14, 3, 21, 11, 3, 3, 4, 3, 4, 3, 5, 3, 5, 3, 5, 3, 5, 3, 6, 3, 6, 3, 6, 3, 6, 3, 6, 3, 6, 3, 6, 3, 6, 3, 6, 3, 6, 3, 6, 3, 6, 3, 6, 3, 6, 5, 6, 43, 10, 6, 3, 7, 3, 7, 3, 7, 2, 2, 8, 2, 4, 6, 8, 10, 12, 2, 3, 3, 2, 6, 7, 2, 44, 2, 14, 3, 2, 2, 2, 4, 19, 3, 2, 2, 2, 6, 22, 3, 2, 2, 2, 8, 24, 3, 2, 2, 2, 10, 42, 3, 2, 2, 2, 12, 44, 3, 2, 2, 2, 14, 15, 5, 4, 3, 2, 15, 3, 3, 2, 2, 2, 16, 18, 5, 6, 4, 2, 17, 16, 3, 2, 2, 2, 18, 21, 3, 2, 2, 2, 19, 17, 3, 2, 2, 2, 19, 20, 3, 2, 2, 2, 20, 5, 3, 2, 2, 2, 21, 19, 3, 2, 2, 2, 22, 23, 5, 8, 5, 2, 23, 7, 3, 2, 2, 2, 24, 25, 7, 3, 2, 2, 25, 26, 5, 10, 6, 2, 26, 27, 7, 4, 2, 2, 27, 9, 3, 2, 2, 2, 28, 29, 5, 12, 7, 2, 29, 30, 7, 5, 2, 2, 30, 31, 5, 12, 7, 2, 31, 43, 3, 2, 2, 2, 32, 33, 5, 12, 7, 2, 33, 34, 9, 2, 2, 2, 34, 35, 5, 12, 7, 2, 35, 43, 3, 2, 2, 2, 36, 37, 5, 12, 7, 2, 37, 38, 7, 8, 2, 2, 38, 39, 5, 12, 7, 2, 39, 43, 3, 2, 2, 2, 40, 41, 7, 7, 2, 2, 41, 43, 5, 12, 7, 2, 42, 28, 3, 2, 2, 2, 42, 32, 3, 2, 2, 2, 42, 36, 3, 2, 2, 2, 42, 40, 3, 2, 2, 2, 43, 11, 3, 2, 2, 2, 44, 45, 7, 9, 2, 2, 45, 13, 3, 2, 2, 2, 4, 19, 42] \ No newline at end of file diff --git a/src/main/antlr4/de/hsrm/compiler/Klang/.antlr/Klang.tokens b/src/main/antlr4/de/hsrm/compiler/Klang/.antlr/Klang.tokens index 9aec4a6..6ce5d08 100644 --- a/src/main/antlr4/de/hsrm/compiler/Klang/.antlr/Klang.tokens +++ b/src/main/antlr4/de/hsrm/compiler/Klang/.antlr/Klang.tokens @@ -1,4 +1,14 @@ -INTEGER_LITERAL=1 -MULT=2 -WS=3 -'*'=2 +PRINT=1 +SCOL=2 +MULT=3 +ADD=4 +SUB=5 +MOD=6 +INTEGER_LITERAL=7 +WS=8 +'print'=1 +';'=2 +'*'=3 +'+'=4 +'-'=5 +'%'=6 diff --git a/src/main/antlr4/de/hsrm/compiler/Klang/.antlr/KlangLexer.interp b/src/main/antlr4/de/hsrm/compiler/Klang/.antlr/KlangLexer.interp index 6224623..ac545b3 100644 --- a/src/main/antlr4/de/hsrm/compiler/Klang/.antlr/KlangLexer.interp +++ b/src/main/antlr4/de/hsrm/compiler/Klang/.antlr/KlangLexer.interp @@ -1,18 +1,33 @@ token literal names: null -null +'print' +';' '*' +'+' +'-' +'%' +null null token symbolic names: null -INTEGER_LITERAL +PRINT +SCOL MULT +ADD +SUB +MOD +INTEGER_LITERAL WS rule names: -INTEGER_LITERAL +PRINT +SCOL MULT +ADD +SUB +MOD +INTEGER_LITERAL WS channel names: @@ -23,4 +38,4 @@ mode names: DEFAULT_MODE atn: -[3, 24715, 42794, 33075, 47597, 16764, 15335, 30598, 22884, 2, 5, 20, 8, 1, 4, 2, 9, 2, 4, 3, 9, 3, 4, 4, 9, 4, 3, 2, 6, 2, 11, 10, 2, 13, 2, 14, 2, 12, 3, 3, 3, 3, 3, 4, 3, 4, 3, 4, 3, 4, 2, 2, 5, 3, 3, 5, 4, 7, 5, 3, 2, 4, 3, 2, 50, 59, 5, 2, 11, 12, 15, 15, 34, 34, 2, 20, 2, 3, 3, 2, 2, 2, 2, 5, 3, 2, 2, 2, 2, 7, 3, 2, 2, 2, 3, 10, 3, 2, 2, 2, 5, 14, 3, 2, 2, 2, 7, 16, 3, 2, 2, 2, 9, 11, 9, 2, 2, 2, 10, 9, 3, 2, 2, 2, 11, 12, 3, 2, 2, 2, 12, 10, 3, 2, 2, 2, 12, 13, 3, 2, 2, 2, 13, 4, 3, 2, 2, 2, 14, 15, 7, 44, 2, 2, 15, 6, 3, 2, 2, 2, 16, 17, 9, 3, 2, 2, 17, 18, 3, 2, 2, 2, 18, 19, 8, 4, 2, 2, 19, 8, 3, 2, 2, 2, 4, 2, 12, 3, 8, 2, 2] \ No newline at end of file +[3, 24715, 42794, 33075, 47597, 16764, 15335, 30598, 22884, 2, 10, 44, 8, 1, 4, 2, 9, 2, 4, 3, 9, 3, 4, 4, 9, 4, 4, 5, 9, 5, 4, 6, 9, 6, 4, 7, 9, 7, 4, 8, 9, 8, 4, 9, 9, 9, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 3, 3, 3, 3, 4, 3, 4, 3, 5, 3, 5, 3, 6, 3, 6, 3, 7, 3, 7, 3, 8, 6, 8, 37, 10, 8, 13, 8, 14, 8, 38, 3, 9, 3, 9, 3, 9, 3, 9, 2, 2, 10, 3, 3, 5, 4, 7, 5, 9, 6, 11, 7, 13, 8, 15, 9, 17, 10, 3, 2, 4, 3, 2, 50, 59, 5, 2, 11, 12, 15, 15, 34, 34, 2, 44, 2, 3, 3, 2, 2, 2, 2, 5, 3, 2, 2, 2, 2, 7, 3, 2, 2, 2, 2, 9, 3, 2, 2, 2, 2, 11, 3, 2, 2, 2, 2, 13, 3, 2, 2, 2, 2, 15, 3, 2, 2, 2, 2, 17, 3, 2, 2, 2, 3, 19, 3, 2, 2, 2, 5, 25, 3, 2, 2, 2, 7, 27, 3, 2, 2, 2, 9, 29, 3, 2, 2, 2, 11, 31, 3, 2, 2, 2, 13, 33, 3, 2, 2, 2, 15, 36, 3, 2, 2, 2, 17, 40, 3, 2, 2, 2, 19, 20, 7, 114, 2, 2, 20, 21, 7, 116, 2, 2, 21, 22, 7, 107, 2, 2, 22, 23, 7, 112, 2, 2, 23, 24, 7, 118, 2, 2, 24, 4, 3, 2, 2, 2, 25, 26, 7, 61, 2, 2, 26, 6, 3, 2, 2, 2, 27, 28, 7, 44, 2, 2, 28, 8, 3, 2, 2, 2, 29, 30, 7, 45, 2, 2, 30, 10, 3, 2, 2, 2, 31, 32, 7, 47, 2, 2, 32, 12, 3, 2, 2, 2, 33, 34, 7, 39, 2, 2, 34, 14, 3, 2, 2, 2, 35, 37, 9, 2, 2, 2, 36, 35, 3, 2, 2, 2, 37, 38, 3, 2, 2, 2, 38, 36, 3, 2, 2, 2, 38, 39, 3, 2, 2, 2, 39, 16, 3, 2, 2, 2, 40, 41, 9, 3, 2, 2, 41, 42, 3, 2, 2, 2, 42, 43, 8, 9, 2, 2, 43, 18, 3, 2, 2, 2, 4, 2, 38, 3, 8, 2, 2] \ No newline at end of file diff --git a/src/main/antlr4/de/hsrm/compiler/Klang/.antlr/KlangLexer.java b/src/main/antlr4/de/hsrm/compiler/Klang/.antlr/KlangLexer.java index 2be147b..e62d46a 100644 --- a/src/main/antlr4/de/hsrm/compiler/Klang/.antlr/KlangLexer.java +++ b/src/main/antlr4/de/hsrm/compiler/Klang/.antlr/KlangLexer.java @@ -1,4 +1,4 @@ -// Generated from /home/nitrix/Development/hsrm/cb/klang/src/main/antlr4/de/hsrm/compiler/Klang/Klang.g4 by ANTLR 4.7.1 +// Generated from /home/marvin/Documents/university/compiler/klang/src/main/antlr4/de/hsrm/compiler/Klang/Klang.g4 by ANTLR 4.7.1 import org.antlr.v4.runtime.Lexer; import org.antlr.v4.runtime.CharStream; import org.antlr.v4.runtime.Token; @@ -16,7 +16,7 @@ public class KlangLexer extends Lexer { protected static final PredictionContextCache _sharedContextCache = new PredictionContextCache(); public static final int - INTEGER_LITERAL=1, MULT=2, WS=3; + PRINT=1, SCOL=2, MULT=3, ADD=4, SUB=5, MOD=6, INTEGER_LITERAL=7, WS=8; public static String[] channelNames = { "DEFAULT_TOKEN_CHANNEL", "HIDDEN" }; @@ -26,14 +26,15 @@ public class KlangLexer extends Lexer { }; public static final String[] ruleNames = { - "INTEGER_LITERAL", "MULT", "WS" + "PRINT", "SCOL", "MULT", "ADD", "SUB", "MOD", "INTEGER_LITERAL", "WS" }; private static final String[] _LITERAL_NAMES = { - null, null, "'*'" + null, "'print'", "';'", "'*'", "'+'", "'-'", "'%'" }; private static final String[] _SYMBOLIC_NAMES = { - null, "INTEGER_LITERAL", "MULT", "WS" + null, "PRINT", "SCOL", "MULT", "ADD", "SUB", "MOD", "INTEGER_LITERAL", + "WS" }; public static final Vocabulary VOCABULARY = new VocabularyImpl(_LITERAL_NAMES, _SYMBOLIC_NAMES); @@ -93,13 +94,18 @@ public class KlangLexer extends Lexer { public ATN getATN() { return _ATN; } public static final String _serializedATN = - "\3\u608b\ua72a\u8133\ub9ed\u417c\u3be7\u7786\u5964\2\5\24\b\1\4\2\t\2"+ - "\4\3\t\3\4\4\t\4\3\2\6\2\13\n\2\r\2\16\2\f\3\3\3\3\3\4\3\4\3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2\2$#\3\2\2\2%&\3\2\2\2&$\3\2\2\2&\'\3\2\2\2\'\20\3\2\2"+ + "\2()\t\3\2\2)*\3\2\2\2*+\b\t\2\2+\22\3\2\2\2\4\2&\3\b\2\2"; public static final ATN _ATN = new ATNDeserializer().deserialize(_serializedATN.toCharArray()); static { diff --git a/src/main/antlr4/de/hsrm/compiler/Klang/.antlr/KlangLexer.tokens b/src/main/antlr4/de/hsrm/compiler/Klang/.antlr/KlangLexer.tokens index 9aec4a6..6ce5d08 100644 --- a/src/main/antlr4/de/hsrm/compiler/Klang/.antlr/KlangLexer.tokens +++ b/src/main/antlr4/de/hsrm/compiler/Klang/.antlr/KlangLexer.tokens @@ -1,4 +1,14 @@ -INTEGER_LITERAL=1 -MULT=2 -WS=3 -'*'=2 +PRINT=1 +SCOL=2 +MULT=3 +ADD=4 +SUB=5 +MOD=6 +INTEGER_LITERAL=7 +WS=8 +'print'=1 +';'=2 +'*'=3 +'+'=4 +'-'=5 +'%'=6 diff --git a/src/main/antlr4/de/hsrm/compiler/Klang/.antlr/KlangParser.java b/src/main/antlr4/de/hsrm/compiler/Klang/.antlr/KlangParser.java index fb9766a..d385994 100644 --- a/src/main/antlr4/de/hsrm/compiler/Klang/.antlr/KlangParser.java +++ b/src/main/antlr4/de/hsrm/compiler/Klang/.antlr/KlangParser.java @@ -1,4 +1,4 @@ -// Generated from /home/nitrix/Development/hsrm/cb/klang/src/main/antlr4/de/hsrm/compiler/Klang/Klang.g4 by ANTLR 4.7.1 +// Generated from /home/marvin/Documents/university/compiler/klang/src/main/antlr4/de/hsrm/compiler/Klang/Klang.g4 by ANTLR 4.7.1 import org.antlr.v4.runtime.atn.*; import org.antlr.v4.runtime.dfa.DFA; import org.antlr.v4.runtime.*; @@ -16,18 +16,20 @@ public class KlangParser extends Parser { protected static final PredictionContextCache _sharedContextCache = new PredictionContextCache(); public static final int - INTEGER_LITERAL=1, MULT=2, WS=3; + PRINT=1, SCOL=2, MULT=3, ADD=4, SUB=5, MOD=6, INTEGER_LITERAL=7, WS=8; public static final int - RULE_parse = 0, RULE_multiplicativeExpr = 1, RULE_unaryExpression = 2; + RULE_parse = 0, RULE_block = 1, RULE_statement = 2, RULE_print = 3, RULE_expression = 4, + RULE_atom = 5; public static final String[] ruleNames = { - "parse", "multiplicativeExpr", "unaryExpression" + "parse", "block", "statement", "print", "expression", "atom" }; private static final String[] _LITERAL_NAMES = { - null, null, "'*'" + null, "'print'", "';'", "'*'", "'+'", "'-'", "'%'" }; private static final String[] _SYMBOLIC_NAMES = { - null, "INTEGER_LITERAL", "MULT", "WS" + null, "PRINT", "SCOL", "MULT", "ADD", "SUB", "MOD", "INTEGER_LITERAL", + "WS" }; public static final Vocabulary VOCABULARY = new VocabularyImpl(_LITERAL_NAMES, _SYMBOLIC_NAMES); @@ -79,8 +81,8 @@ public class KlangParser extends Parser { _interp = new ParserATNSimulator(this,_ATN,_decisionToDFA,_sharedContextCache); } public static class ParseContext extends ParserRuleContext { - public MultiplicativeExprContext multiplicativeExpr() { - return getRuleContext(MultiplicativeExprContext.class,0); + public BlockContext block() { + return getRuleContext(BlockContext.class,0); } public ParseContext(ParserRuleContext parent, int invokingState) { super(parent, invokingState); @@ -94,8 +96,8 @@ public class KlangParser extends Parser { try { enterOuterAlt(_localctx, 1); { - setState(6); - multiplicativeExpr(); + setState(12); + block(); } } catch (RecognitionException re) { @@ -109,45 +111,37 @@ public class KlangParser extends Parser { return _localctx; } - public static class MultiplicativeExprContext extends ParserRuleContext { - public List unaryExpression() { - return getRuleContexts(UnaryExpressionContext.class); + public static class BlockContext extends ParserRuleContext { + public List statement() { + return getRuleContexts(StatementContext.class); } - public UnaryExpressionContext unaryExpression(int i) { - return getRuleContext(UnaryExpressionContext.class,i); + public StatementContext statement(int i) { + return getRuleContext(StatementContext.class,i); } - public List MULT() { return getTokens(KlangParser.MULT); } - public TerminalNode MULT(int i) { - return getToken(KlangParser.MULT, i); - } - public MultiplicativeExprContext(ParserRuleContext parent, int invokingState) { + public BlockContext(ParserRuleContext parent, int invokingState) { super(parent, invokingState); } - @Override public int getRuleIndex() { return RULE_multiplicativeExpr; } + @Override public int getRuleIndex() { return RULE_block; } } - public final MultiplicativeExprContext multiplicativeExpr() throws RecognitionException { - MultiplicativeExprContext _localctx = new MultiplicativeExprContext(_ctx, getState()); - enterRule(_localctx, 2, RULE_multiplicativeExpr); + public final BlockContext block() throws RecognitionException { + BlockContext _localctx = new BlockContext(_ctx, getState()); + enterRule(_localctx, 2, RULE_block); int _la; try { enterOuterAlt(_localctx, 1); { - setState(8); - unaryExpression(); - setState(13); + setState(17); _errHandler.sync(this); _la = _input.LA(1); - while (_la==MULT) { + while (_la==PRINT) { { { - setState(9); - match(MULT); - setState(10); - unaryExpression(); + setState(14); + statement(); } } - setState(15); + setState(19); _errHandler.sync(this); _la = _input.LA(1); } @@ -164,21 +158,225 @@ public class KlangParser extends Parser { return _localctx; } - public static class UnaryExpressionContext extends ParserRuleContext { - public TerminalNode INTEGER_LITERAL() { return getToken(KlangParser.INTEGER_LITERAL, 0); } - public UnaryExpressionContext(ParserRuleContext parent, int invokingState) { + public static class StatementContext extends ParserRuleContext { + public PrintContext print() { + return getRuleContext(PrintContext.class,0); + } + public StatementContext(ParserRuleContext parent, int invokingState) { super(parent, invokingState); } - @Override public int getRuleIndex() { return RULE_unaryExpression; } + @Override public int getRuleIndex() { return RULE_statement; } } - public final UnaryExpressionContext unaryExpression() throws RecognitionException { - UnaryExpressionContext _localctx = new UnaryExpressionContext(_ctx, getState()); - enterRule(_localctx, 4, RULE_unaryExpression); + public final StatementContext statement() throws RecognitionException { + StatementContext _localctx = new StatementContext(_ctx, getState()); + enterRule(_localctx, 4, RULE_statement); try { enterOuterAlt(_localctx, 1); { - setState(16); + setState(20); + print(); + } + } + catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } + finally { + exitRule(); + } + return _localctx; + } + + public static class PrintContext extends ParserRuleContext { + public TerminalNode PRINT() { return getToken(KlangParser.PRINT, 0); } + public ExpressionContext expression() { + return getRuleContext(ExpressionContext.class,0); + } + public TerminalNode SCOL() { return getToken(KlangParser.SCOL, 0); } + public PrintContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + @Override public int getRuleIndex() { return RULE_print; } + } + + public final PrintContext print() throws RecognitionException { + PrintContext _localctx = new PrintContext(_ctx, getState()); + enterRule(_localctx, 6, RULE_print); + try { + enterOuterAlt(_localctx, 1); + { + setState(22); + match(PRINT); + setState(23); + expression(); + setState(24); + match(SCOL); + } + } + catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } + finally { + exitRule(); + } + return _localctx; + } + + public static class ExpressionContext extends ParserRuleContext { + public ExpressionContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + @Override public int getRuleIndex() { return RULE_expression; } + + public ExpressionContext() { } + public void copyFrom(ExpressionContext ctx) { + super.copyFrom(ctx); + } + } + public static class AdditiveExpressionContext extends ExpressionContext { + public Token op; + public List atom() { + return getRuleContexts(AtomContext.class); + } + public AtomContext atom(int i) { + return getRuleContext(AtomContext.class,i); + } + public TerminalNode ADD() { return getToken(KlangParser.ADD, 0); } + public TerminalNode SUB() { return getToken(KlangParser.SUB, 0); } + public AdditiveExpressionContext(ExpressionContext ctx) { copyFrom(ctx); } + } + public static class ModuloExpressionContext extends ExpressionContext { + public List atom() { + return getRuleContexts(AtomContext.class); + } + public AtomContext atom(int i) { + return getRuleContext(AtomContext.class,i); + } + public TerminalNode MOD() { return getToken(KlangParser.MOD, 0); } + public ModuloExpressionContext(ExpressionContext ctx) { copyFrom(ctx); } + } + public static class UnaryNegateExpressionContext extends ExpressionContext { + public TerminalNode SUB() { return getToken(KlangParser.SUB, 0); } + public AtomContext atom() { + return getRuleContext(AtomContext.class,0); + } + public UnaryNegateExpressionContext(ExpressionContext ctx) { copyFrom(ctx); } + } + public static class MultiplicationExpressionContext extends ExpressionContext { + public List atom() { + return getRuleContexts(AtomContext.class); + } + public AtomContext atom(int i) { + return getRuleContext(AtomContext.class,i); + } + public TerminalNode MULT() { return getToken(KlangParser.MULT, 0); } + public MultiplicationExpressionContext(ExpressionContext ctx) { copyFrom(ctx); } + } + + public final ExpressionContext expression() throws RecognitionException { + ExpressionContext _localctx = new ExpressionContext(_ctx, getState()); + enterRule(_localctx, 8, RULE_expression); + int _la; + try { + setState(40); + _errHandler.sync(this); + switch ( getInterpreter().adaptivePredict(_input,1,_ctx) ) { + case 1: + _localctx = new MultiplicationExpressionContext(_localctx); + enterOuterAlt(_localctx, 1); + { + setState(26); + atom(); + setState(27); + match(MULT); + setState(28); + atom(); + } + break; + case 2: + _localctx = new AdditiveExpressionContext(_localctx); + enterOuterAlt(_localctx, 2); + { + setState(30); + atom(); + setState(31); + ((AdditiveExpressionContext)_localctx).op = _input.LT(1); + _la = _input.LA(1); + if ( !(_la==ADD || _la==SUB) ) { + ((AdditiveExpressionContext)_localctx).op = (Token)_errHandler.recoverInline(this); + } + else { + if ( _input.LA(1)==Token.EOF ) matchedEOF = true; + _errHandler.reportMatch(this); + consume(); + } + setState(32); + atom(); + } + break; + case 3: + _localctx = new ModuloExpressionContext(_localctx); + enterOuterAlt(_localctx, 3); + { + setState(34); + atom(); + setState(35); + match(MOD); + setState(36); + atom(); + } + break; + case 4: + _localctx = new UnaryNegateExpressionContext(_localctx); + enterOuterAlt(_localctx, 4); + { + setState(38); + match(SUB); + setState(39); + atom(); + } + break; + } + } + catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } + finally { + exitRule(); + } + return _localctx; + } + + public static class AtomContext extends ParserRuleContext { + public AtomContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + @Override public int getRuleIndex() { return RULE_atom; } + + public AtomContext() { } + public void copyFrom(AtomContext ctx) { + super.copyFrom(ctx); + } + } + public static class IntAtomContext extends AtomContext { + public TerminalNode INTEGER_LITERAL() { return getToken(KlangParser.INTEGER_LITERAL, 0); } + public IntAtomContext(AtomContext ctx) { copyFrom(ctx); } + } + + public final AtomContext atom() throws RecognitionException { + AtomContext _localctx = new AtomContext(_ctx, getState()); + enterRule(_localctx, 10, RULE_atom); + try { + _localctx = new IntAtomContext(_localctx); + enterOuterAlt(_localctx, 1); + { + setState(42); match(INTEGER_LITERAL); } } @@ -194,12 +392,18 @@ public class KlangParser extends Parser { } public static final String _serializedATN = - "\3\u608b\ua72a\u8133\ub9ed\u417c\u3be7\u7786\u5964\3\5\25\4\2\t\2\4\3"+ - "\t\3\4\4\t\4\3\2\3\2\3\3\3\3\3\3\7\3\16\n\3\f\3\16\3\21\13\3\3\4\3\4\3"+ - "\4\2\2\5\2\4\6\2\2\2\22\2\b\3\2\2\2\4\n\3\2\2\2\6\22\3\2\2\2\b\t\5\4\3"+ - "\2\t\3\3\2\2\2\n\17\5\6\4\2\13\f\7\4\2\2\f\16\5\6\4\2\r\13\3\2\2\2\16"+ - "\21\3\2\2\2\17\r\3\2\2\2\17\20\3\2\2\2\20\5\3\2\2\2\21\17\3\2\2\2\22\23"+ - "\7\3\2\2\23\7\3\2\2\2\3\17"; + "\3\u608b\ua72a\u8133\ub9ed\u417c\u3be7\u7786\u5964\3\n/\4\2\t\2\4\3\t"+ + "\3\4\4\t\4\4\5\t\5\4\6\t\6\4\7\t\7\3\2\3\2\3\3\7\3\22\n\3\f\3\16\3\25"+ + "\13\3\3\4\3\4\3\5\3\5\3\5\3\5\3\6\3\6\3\6\3\6\3\6\3\6\3\6\3\6\3\6\3\6"+ + "\3\6\3\6\3\6\3\6\5\6+\n\6\3\7\3\7\3\7\2\2\b\2\4\6\b\n\f\2\3\3\2\6\7\2"+ + ",\2\16\3\2\2\2\4\23\3\2\2\2\6\26\3\2\2\2\b\30\3\2\2\2\n*\3\2\2\2\f,\3"+ + "\2\2\2\16\17\5\4\3\2\17\3\3\2\2\2\20\22\5\6\4\2\21\20\3\2\2\2\22\25\3"+ + "\2\2\2\23\21\3\2\2\2\23\24\3\2\2\2\24\5\3\2\2\2\25\23\3\2\2\2\26\27\5"+ + "\b\5\2\27\7\3\2\2\2\30\31\7\3\2\2\31\32\5\n\6\2\32\33\7\4\2\2\33\t\3\2"+ + "\2\2\34\35\5\f\7\2\35\36\7\5\2\2\36\37\5\f\7\2\37+\3\2\2\2 !\5\f\7\2!"+ + "\"\t\2\2\2\"#\5\f\7\2#+\3\2\2\2$%\5\f\7\2%&\7\b\2\2&\'\5\f\7\2\'+\3\2"+ + "\2\2()\7\7\2\2)+\5\f\7\2*\34\3\2\2\2* \3\2\2\2*$\3\2\2\2*(\3\2\2\2+\13"+ + "\3\2\2\2,-\7\t\2\2-\r\3\2\2\2\4\23*"; public static final ATN _ATN = new ATNDeserializer().deserialize(_serializedATN.toCharArray()); static { diff --git a/src/main/antlr4/de/hsrm/compiler/Klang/Klang.g4 b/src/main/antlr4/de/hsrm/compiler/Klang/Klang.g4 index 4bb2d57..2f2b5cb 100644 --- a/src/main/antlr4/de/hsrm/compiler/Klang/Klang.g4 +++ b/src/main/antlr4/de/hsrm/compiler/Klang/Klang.g4 @@ -1,25 +1,44 @@ grammar Klang; parse - : multiplicativeExpr + : block ; -multiplicativeExpr - : unaryExpression (MULT unaryExpression)* +block + : statement* ; -unaryExpression - : INTEGER_LITERAL +statement + : print ; +print + : PRINT expression SCOL + ; + +expression + : atom MULT atom #multiplicationExpression + | atom op=(ADD | SUB) atom #additiveExpression + | atom MOD atom #moduloExpression + | SUB atom #unaryNegateExpression + ; + +atom + : INTEGER_LITERAL #intAtom + ; + +PRINT: 'print'; +SCOL: ';'; + +MULT: '*'; +ADD: '+'; +SUB: '-'; +MOD: '%'; + INTEGER_LITERAL : [0-9]+ ; -MULT - : '*' - ; - WS : [ \t\r\n] -> skip ; diff --git a/src/main/java/de/hsrm/compiler/Klang/JSVisitor.java b/src/main/java/de/hsrm/compiler/Klang/JSVisitor.java new file mode 100644 index 0000000..a2054c7 --- /dev/null +++ b/src/main/java/de/hsrm/compiler/Klang/JSVisitor.java @@ -0,0 +1,71 @@ +package de.hsrm.compiler.Klang; + +public class JSVisitor extends KlangBaseVisitor { + private final StringBuilder sb; + + public JSVisitor(StringBuilder sb) { + this.sb = sb; + } + + @Override + public Void visitStatement(KlangParser.StatementContext ctx) { + for (int i = 0; i < ctx.children.size(); i++) { + this.visit(ctx.children.get(i)); + sb.append("\n"); + } + return null; + } + + @Override + public Void visitPrint(KlangParser.PrintContext ctx) { + sb.append("console.log("); + this.visit(ctx.expression()); + sb.append(");"); + return null; + } + + @Override + public Void visitMultiplicationExpression(KlangParser.MultiplicationExpressionContext ctx) { + this.visit(ctx.atom(0)); + sb.append(" * "); + this.visit(ctx.atom(1)); + return null; + } + + @Override + public Void visitAdditiveExpression(KlangParser.AdditiveExpressionContext ctx) { + this.visit(ctx.atom(0)); + + switch (ctx.op.getType()) { + case KlangParser.ADD: + sb.append(" + "); + break; + case KlangParser.SUB: + sb.append(" - "); + break; + } + this.visit(ctx.atom(1)); + return null; + } + + @Override + public Void visitModuloExpression(KlangParser.ModuloExpressionContext ctx) { + this.visit(ctx.atom(0)); + sb.append(" % "); + this.visit(ctx.atom(1)); + return null; + } + + @Override + public Void visitUnaryNegateExpression(KlangParser.UnaryNegateExpressionContext ctx) { + sb.append("-"); + this.visit(ctx.atom()); + return null; + } + + @Override + public Void visitIntAtom(KlangParser.IntAtomContext ctx) { + sb.append(ctx.getText()); + return null; + } +} \ No newline at end of file diff --git a/src/main/java/de/hsrm/compiler/Klang/Klang.java b/src/main/java/de/hsrm/compiler/Klang/Klang.java index f80498d..fd370b3 100644 --- a/src/main/java/de/hsrm/compiler/Klang/Klang.java +++ b/src/main/java/de/hsrm/compiler/Klang/Klang.java @@ -19,8 +19,9 @@ public class Klang { KlangParser parser = new KlangParser(tokens); ParseTree tree = parser.parse(); // begin parsing at init rule - Visitor visitor = new Visitor(); - int result = visitor.visit(tree); - System.out.println(result); + StringBuilder sb = new StringBuilder(); + JSVisitor visitor = new JSVisitor(sb); + visitor.visit(tree); + System.out.println(sb.toString()); } } diff --git a/src/main/java/de/hsrm/compiler/Klang/Value.java b/src/main/java/de/hsrm/compiler/Klang/Value.java new file mode 100644 index 0000000..880275a --- /dev/null +++ b/src/main/java/de/hsrm/compiler/Klang/Value.java @@ -0,0 +1,55 @@ +package de.hsrm.compiler.Klang; + +public class Value { + + public static Value VOID = new Value(new Object()); + + final Object value; + + public Value(Object value) { + this.value = value; + } + + public Integer asInteger() { + return (Integer)value; + } + + public String asString() { + return String.valueOf(value); + } + + public boolean isDouble() { + return value instanceof Double; + } + + @Override + public int hashCode() { + + if(value == null) { + return 0; + } + + return this.value.hashCode(); + } + + @Override + public boolean equals(Object o) { + + if(value == o) { + return true; + } + + if(value == null || o == null || o.getClass() != value.getClass()) { + return false; + } + + Value that = (Value)o; + + return this.value.equals(that.value); + } + + @Override + public String toString() { + return String.valueOf(value); + } +} diff --git a/src/main/java/de/hsrm/compiler/Klang/Visitor.java b/src/main/java/de/hsrm/compiler/Klang/Visitor.java index 2db4539..bdc084d 100644 --- a/src/main/java/de/hsrm/compiler/Klang/Visitor.java +++ b/src/main/java/de/hsrm/compiler/Klang/Visitor.java @@ -1,19 +1,51 @@ package de.hsrm.compiler.Klang; -public class Visitor extends KlangBaseVisitor { - @Override - public Integer visitMultiplicativeExpr(KlangParser.MultiplicativeExprContext ctx) { - int result = 1; - - for (var expr: ctx.unaryExpression()) { - result *= this.visit(expr); - } - - return result; - } - - @Override - public Integer visitUnaryExpression(KlangParser.UnaryExpressionContext ctx) { - return Integer.parseInt(ctx.getText()); +public class Visitor extends KlangBaseVisitor { + + @Override + public Value visitPrint(KlangParser.PrintContext ctx) { + Value value = this.visit(ctx.expression()); + System.out.println(value); + return value; + } + + @Override + public Value visitMultiplicationExpression(KlangParser.MultiplicationExpressionContext ctx) { + Value left = this.visit(ctx.atom(0)); + Value right = this.visit(ctx.atom(1)); + return new Value(left.asInteger() * right.asInteger()); + } + + @Override + public Value visitAdditiveExpression(KlangParser.AdditiveExpressionContext ctx) { + Value left = this.visit(ctx.atom(0)); + Value right = this.visit(ctx.atom(1)); + + switch (ctx.op.getType()) { + case KlangParser.ADD: + return new Value(left.asInteger() + right.asInteger()); + case KlangParser.SUB: + return new Value(left.asInteger() - right.asInteger()); + default: + throw new RuntimeException("Unknown operator for additive expression: "+ KlangParser.VOCABULARY.getDisplayName(ctx.op.getType())); } + } + + @Override + public Value visitModuloExpression(KlangParser.ModuloExpressionContext ctx) { + Value left = this.visit(ctx.atom(0)); + Value right = this.visit(ctx.atom(1)); + return new Value(left.asInteger() % right.asInteger()); + } + + @Override + public Value visitUnaryNegateExpression(KlangParser.UnaryNegateExpressionContext ctx) { + Value value = this.visit(ctx.atom()); + return new Value(-value.asInteger()); + } + + @Override + public Value visitIntAtom(KlangParser.IntAtomContext ctx) { + return new Value(Integer.parseInt(ctx.getText())); + } } \ No newline at end of file From d43fa90bc3807042d66ca28b9120303d7b7e26f6 Mon Sep 17 00:00:00 2001 From: Marvin Kaiser Date: Tue, 29 Oct 2019 10:30:18 +0100 Subject: [PATCH 2/5] Added if statement --- .../hsrm/compiler/Klang/.antlr/Klang.interp | 14 +- .../hsrm/compiler/Klang/.antlr/Klang.tokens | 34 ++- .../compiler/Klang/.antlr/KlangLexer.interp | 17 +- .../compiler/Klang/.antlr/KlangLexer.java | 44 +-- .../compiler/Klang/.antlr/KlangLexer.tokens | 34 ++- .../compiler/Klang/.antlr/KlangParser.java | 251 ++++++++++++++---- .../antlr4/de/hsrm/compiler/Klang/Klang.g4 | 20 ++ .../java/de/hsrm/compiler/Klang/Klang.java | 6 +- .../java/de/hsrm/compiler/Klang/Visitor.java | 19 +- 9 files changed, 343 insertions(+), 96 deletions(-) diff --git a/src/main/antlr4/de/hsrm/compiler/Klang/.antlr/Klang.interp b/src/main/antlr4/de/hsrm/compiler/Klang/.antlr/Klang.interp index 2bb4da2..6d3ac3d 100644 --- a/src/main/antlr4/de/hsrm/compiler/Klang/.antlr/Klang.interp +++ b/src/main/antlr4/de/hsrm/compiler/Klang/.antlr/Klang.interp @@ -1,7 +1,12 @@ token literal names: null 'print' +'if' +'then' +'else' ';' +'{' +'}' '*' '+' '-' @@ -12,7 +17,12 @@ null token symbolic names: null PRINT +IF +THEN +ELSE SCOL +OBRK +CBRK MULT ADD SUB @@ -23,11 +33,13 @@ WS rule names: parse block +braced_block statement print +if_statement expression atom atn: -[3, 24715, 42794, 33075, 47597, 16764, 15335, 30598, 22884, 3, 10, 47, 4, 2, 9, 2, 4, 3, 9, 3, 4, 4, 9, 4, 4, 5, 9, 5, 4, 6, 9, 6, 4, 7, 9, 7, 3, 2, 3, 2, 3, 3, 7, 3, 18, 10, 3, 12, 3, 14, 3, 21, 11, 3, 3, 4, 3, 4, 3, 5, 3, 5, 3, 5, 3, 5, 3, 6, 3, 6, 3, 6, 3, 6, 3, 6, 3, 6, 3, 6, 3, 6, 3, 6, 3, 6, 3, 6, 3, 6, 3, 6, 3, 6, 5, 6, 43, 10, 6, 3, 7, 3, 7, 3, 7, 2, 2, 8, 2, 4, 6, 8, 10, 12, 2, 3, 3, 2, 6, 7, 2, 44, 2, 14, 3, 2, 2, 2, 4, 19, 3, 2, 2, 2, 6, 22, 3, 2, 2, 2, 8, 24, 3, 2, 2, 2, 10, 42, 3, 2, 2, 2, 12, 44, 3, 2, 2, 2, 14, 15, 5, 4, 3, 2, 15, 3, 3, 2, 2, 2, 16, 18, 5, 6, 4, 2, 17, 16, 3, 2, 2, 2, 18, 21, 3, 2, 2, 2, 19, 17, 3, 2, 2, 2, 19, 20, 3, 2, 2, 2, 20, 5, 3, 2, 2, 2, 21, 19, 3, 2, 2, 2, 22, 23, 5, 8, 5, 2, 23, 7, 3, 2, 2, 2, 24, 25, 7, 3, 2, 2, 25, 26, 5, 10, 6, 2, 26, 27, 7, 4, 2, 2, 27, 9, 3, 2, 2, 2, 28, 29, 5, 12, 7, 2, 29, 30, 7, 5, 2, 2, 30, 31, 5, 12, 7, 2, 31, 43, 3, 2, 2, 2, 32, 33, 5, 12, 7, 2, 33, 34, 9, 2, 2, 2, 34, 35, 5, 12, 7, 2, 35, 43, 3, 2, 2, 2, 36, 37, 5, 12, 7, 2, 37, 38, 7, 8, 2, 2, 38, 39, 5, 12, 7, 2, 39, 43, 3, 2, 2, 2, 40, 41, 7, 7, 2, 2, 41, 43, 5, 12, 7, 2, 42, 28, 3, 2, 2, 2, 42, 32, 3, 2, 2, 2, 42, 36, 3, 2, 2, 2, 42, 40, 3, 2, 2, 2, 43, 11, 3, 2, 2, 2, 44, 45, 7, 9, 2, 2, 45, 13, 3, 2, 2, 2, 4, 19, 42] \ No newline at end of file +[3, 24715, 42794, 33075, 47597, 16764, 15335, 30598, 22884, 3, 15, 71, 4, 2, 9, 2, 4, 3, 9, 3, 4, 4, 9, 4, 4, 5, 9, 5, 4, 6, 9, 6, 4, 7, 9, 7, 4, 8, 9, 8, 4, 9, 9, 9, 3, 2, 3, 2, 3, 3, 7, 3, 22, 10, 3, 12, 3, 14, 3, 25, 11, 3, 3, 4, 3, 4, 7, 4, 29, 10, 4, 12, 4, 14, 4, 32, 11, 4, 3, 4, 3, 4, 3, 5, 3, 5, 5, 5, 38, 10, 5, 3, 6, 3, 6, 3, 6, 3, 6, 3, 7, 3, 7, 3, 7, 3, 7, 3, 7, 3, 7, 5, 7, 50, 10, 7, 3, 8, 3, 8, 3, 8, 3, 8, 3, 8, 3, 8, 3, 8, 3, 8, 3, 8, 3, 8, 3, 8, 3, 8, 3, 8, 3, 8, 3, 8, 5, 8, 67, 10, 8, 3, 9, 3, 9, 3, 9, 2, 2, 10, 2, 4, 6, 8, 10, 12, 14, 16, 2, 3, 3, 2, 11, 12, 2, 70, 2, 18, 3, 2, 2, 2, 4, 23, 3, 2, 2, 2, 6, 26, 3, 2, 2, 2, 8, 37, 3, 2, 2, 2, 10, 39, 3, 2, 2, 2, 12, 43, 3, 2, 2, 2, 14, 66, 3, 2, 2, 2, 16, 68, 3, 2, 2, 2, 18, 19, 5, 4, 3, 2, 19, 3, 3, 2, 2, 2, 20, 22, 5, 8, 5, 2, 21, 20, 3, 2, 2, 2, 22, 25, 3, 2, 2, 2, 23, 21, 3, 2, 2, 2, 23, 24, 3, 2, 2, 2, 24, 5, 3, 2, 2, 2, 25, 23, 3, 2, 2, 2, 26, 30, 7, 8, 2, 2, 27, 29, 5, 8, 5, 2, 28, 27, 3, 2, 2, 2, 29, 32, 3, 2, 2, 2, 30, 28, 3, 2, 2, 2, 30, 31, 3, 2, 2, 2, 31, 33, 3, 2, 2, 2, 32, 30, 3, 2, 2, 2, 33, 34, 7, 9, 2, 2, 34, 7, 3, 2, 2, 2, 35, 38, 5, 10, 6, 2, 36, 38, 5, 12, 7, 2, 37, 35, 3, 2, 2, 2, 37, 36, 3, 2, 2, 2, 38, 9, 3, 2, 2, 2, 39, 40, 7, 3, 2, 2, 40, 41, 5, 14, 8, 2, 41, 42, 7, 7, 2, 2, 42, 11, 3, 2, 2, 2, 43, 44, 7, 4, 2, 2, 44, 45, 5, 14, 8, 2, 45, 46, 7, 5, 2, 2, 46, 49, 5, 6, 4, 2, 47, 48, 7, 6, 2, 2, 48, 50, 5, 6, 4, 2, 49, 47, 3, 2, 2, 2, 49, 50, 3, 2, 2, 2, 50, 13, 3, 2, 2, 2, 51, 52, 5, 16, 9, 2, 52, 53, 7, 10, 2, 2, 53, 54, 5, 16, 9, 2, 54, 67, 3, 2, 2, 2, 55, 56, 5, 16, 9, 2, 56, 57, 9, 2, 2, 2, 57, 58, 5, 16, 9, 2, 58, 67, 3, 2, 2, 2, 59, 60, 5, 16, 9, 2, 60, 61, 7, 13, 2, 2, 61, 62, 5, 16, 9, 2, 62, 67, 3, 2, 2, 2, 63, 64, 7, 12, 2, 2, 64, 67, 5, 16, 9, 2, 65, 67, 5, 16, 9, 2, 66, 51, 3, 2, 2, 2, 66, 55, 3, 2, 2, 2, 66, 59, 3, 2, 2, 2, 66, 63, 3, 2, 2, 2, 66, 65, 3, 2, 2, 2, 67, 15, 3, 2, 2, 2, 68, 69, 7, 14, 2, 2, 69, 17, 3, 2, 2, 2, 7, 23, 30, 37, 49, 66] \ No newline at end of file diff --git a/src/main/antlr4/de/hsrm/compiler/Klang/.antlr/Klang.tokens b/src/main/antlr4/de/hsrm/compiler/Klang/.antlr/Klang.tokens index 6ce5d08..e7ec9c1 100644 --- a/src/main/antlr4/de/hsrm/compiler/Klang/.antlr/Klang.tokens +++ b/src/main/antlr4/de/hsrm/compiler/Klang/.antlr/Klang.tokens @@ -1,14 +1,24 @@ PRINT=1 -SCOL=2 -MULT=3 -ADD=4 -SUB=5 -MOD=6 -INTEGER_LITERAL=7 -WS=8 +IF=2 +THEN=3 +ELSE=4 +SCOL=5 +OBRK=6 +CBRK=7 +MULT=8 +ADD=9 +SUB=10 +MOD=11 +INTEGER_LITERAL=12 +WS=13 'print'=1 -';'=2 -'*'=3 -'+'=4 -'-'=5 -'%'=6 +'if'=2 +'then'=3 +'else'=4 +';'=5 +'{'=6 +'}'=7 +'*'=8 +'+'=9 +'-'=10 +'%'=11 diff --git a/src/main/antlr4/de/hsrm/compiler/Klang/.antlr/KlangLexer.interp b/src/main/antlr4/de/hsrm/compiler/Klang/.antlr/KlangLexer.interp index ac545b3..6a31f8e 100644 --- a/src/main/antlr4/de/hsrm/compiler/Klang/.antlr/KlangLexer.interp +++ b/src/main/antlr4/de/hsrm/compiler/Klang/.antlr/KlangLexer.interp @@ -1,7 +1,12 @@ token literal names: null 'print' +'if' +'then' +'else' ';' +'{' +'}' '*' '+' '-' @@ -12,7 +17,12 @@ null token symbolic names: null PRINT +IF +THEN +ELSE SCOL +OBRK +CBRK MULT ADD SUB @@ -22,7 +32,12 @@ WS rule names: PRINT +IF +THEN +ELSE SCOL +OBRK +CBRK MULT ADD SUB @@ -38,4 +53,4 @@ mode names: DEFAULT_MODE atn: -[3, 24715, 42794, 33075, 47597, 16764, 15335, 30598, 22884, 2, 10, 44, 8, 1, 4, 2, 9, 2, 4, 3, 9, 3, 4, 4, 9, 4, 4, 5, 9, 5, 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, 2, 2, 42, 43, 8, 9, 2, 2, 43, 18, 3, 2, 2, 2, 4, 2, 38, 3, 8, 2, 2] \ No newline at end of file +[3, 24715, 42794, 33075, 47597, 16764, 15335, 30598, 22884, 2, 15, 71, 8, 1, 4, 2, 9, 2, 4, 3, 9, 3, 4, 4, 9, 4, 4, 5, 9, 5, 4, 6, 9, 6, 4, 7, 9, 7, 4, 8, 9, 8, 4, 9, 9, 9, 4, 10, 9, 10, 4, 11, 9, 11, 4, 12, 9, 12, 4, 13, 9, 13, 4, 14, 9, 14, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 3, 3, 3, 3, 3, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 5, 3, 5, 3, 5, 3, 5, 3, 5, 3, 6, 3, 6, 3, 7, 3, 7, 3, 8, 3, 8, 3, 9, 3, 9, 3, 10, 3, 10, 3, 11, 3, 11, 3, 12, 3, 12, 3, 13, 6, 13, 64, 10, 13, 13, 13, 14, 13, 65, 3, 14, 3, 14, 3, 14, 3, 14, 2, 2, 15, 3, 3, 5, 4, 7, 5, 9, 6, 11, 7, 13, 8, 15, 9, 17, 10, 19, 11, 21, 12, 23, 13, 25, 14, 27, 15, 3, 2, 4, 3, 2, 50, 59, 5, 2, 11, 12, 15, 15, 34, 34, 2, 71, 2, 3, 3, 2, 2, 2, 2, 5, 3, 2, 2, 2, 2, 7, 3, 2, 2, 2, 2, 9, 3, 2, 2, 2, 2, 11, 3, 2, 2, 2, 2, 13, 3, 2, 2, 2, 2, 15, 3, 2, 2, 2, 2, 17, 3, 2, 2, 2, 2, 19, 3, 2, 2, 2, 2, 21, 3, 2, 2, 2, 2, 23, 3, 2, 2, 2, 2, 25, 3, 2, 2, 2, 2, 27, 3, 2, 2, 2, 3, 29, 3, 2, 2, 2, 5, 35, 3, 2, 2, 2, 7, 38, 3, 2, 2, 2, 9, 43, 3, 2, 2, 2, 11, 48, 3, 2, 2, 2, 13, 50, 3, 2, 2, 2, 15, 52, 3, 2, 2, 2, 17, 54, 3, 2, 2, 2, 19, 56, 3, 2, 2, 2, 21, 58, 3, 2, 2, 2, 23, 60, 3, 2, 2, 2, 25, 63, 3, 2, 2, 2, 27, 67, 3, 2, 2, 2, 29, 30, 7, 114, 2, 2, 30, 31, 7, 116, 2, 2, 31, 32, 7, 107, 2, 2, 32, 33, 7, 112, 2, 2, 33, 34, 7, 118, 2, 2, 34, 4, 3, 2, 2, 2, 35, 36, 7, 107, 2, 2, 36, 37, 7, 104, 2, 2, 37, 6, 3, 2, 2, 2, 38, 39, 7, 118, 2, 2, 39, 40, 7, 106, 2, 2, 40, 41, 7, 103, 2, 2, 41, 42, 7, 112, 2, 2, 42, 8, 3, 2, 2, 2, 43, 44, 7, 103, 2, 2, 44, 45, 7, 110, 2, 2, 45, 46, 7, 117, 2, 2, 46, 47, 7, 103, 2, 2, 47, 10, 3, 2, 2, 2, 48, 49, 7, 61, 2, 2, 49, 12, 3, 2, 2, 2, 50, 51, 7, 125, 2, 2, 51, 14, 3, 2, 2, 2, 52, 53, 7, 127, 2, 2, 53, 16, 3, 2, 2, 2, 54, 55, 7, 44, 2, 2, 55, 18, 3, 2, 2, 2, 56, 57, 7, 45, 2, 2, 57, 20, 3, 2, 2, 2, 58, 59, 7, 47, 2, 2, 59, 22, 3, 2, 2, 2, 60, 61, 7, 39, 2, 2, 61, 24, 3, 2, 2, 2, 62, 64, 9, 2, 2, 2, 63, 62, 3, 2, 2, 2, 64, 65, 3, 2, 2, 2, 65, 63, 3, 2, 2, 2, 65, 66, 3, 2, 2, 2, 66, 26, 3, 2, 2, 2, 67, 68, 9, 3, 2, 2, 68, 69, 3, 2, 2, 2, 69, 70, 8, 14, 2, 2, 70, 28, 3, 2, 2, 2, 4, 2, 65, 3, 8, 2, 2] \ No newline at end of file diff --git a/src/main/antlr4/de/hsrm/compiler/Klang/.antlr/KlangLexer.java b/src/main/antlr4/de/hsrm/compiler/Klang/.antlr/KlangLexer.java index e62d46a..1e028a0 100644 --- a/src/main/antlr4/de/hsrm/compiler/Klang/.antlr/KlangLexer.java +++ b/src/main/antlr4/de/hsrm/compiler/Klang/.antlr/KlangLexer.java @@ -16,7 +16,8 @@ public class KlangLexer extends Lexer { protected static final PredictionContextCache _sharedContextCache = new PredictionContextCache(); public static final int - PRINT=1, SCOL=2, MULT=3, ADD=4, SUB=5, MOD=6, INTEGER_LITERAL=7, WS=8; + PRINT=1, IF=2, THEN=3, ELSE=4, SCOL=5, OBRK=6, CBRK=7, MULT=8, ADD=9, + SUB=10, MOD=11, INTEGER_LITERAL=12, WS=13; public static String[] channelNames = { "DEFAULT_TOKEN_CHANNEL", "HIDDEN" }; @@ -26,15 +27,17 @@ public class KlangLexer extends Lexer { }; public static final String[] ruleNames = { - "PRINT", "SCOL", "MULT", "ADD", "SUB", "MOD", "INTEGER_LITERAL", "WS" + "PRINT", "IF", "THEN", "ELSE", "SCOL", "OBRK", "CBRK", "MULT", "ADD", + "SUB", "MOD", "INTEGER_LITERAL", "WS" }; private static final String[] _LITERAL_NAMES = { - null, "'print'", "';'", "'*'", "'+'", "'-'", "'%'" + null, "'print'", "'if'", "'then'", "'else'", "';'", "'{'", "'}'", "'*'", + "'+'", "'-'", "'%'" }; private static final String[] _SYMBOLIC_NAMES = { - null, "PRINT", "SCOL", "MULT", "ADD", "SUB", "MOD", "INTEGER_LITERAL", - "WS" + null, "PRINT", "IF", "THEN", "ELSE", "SCOL", "OBRK", "CBRK", "MULT", "ADD", + "SUB", "MOD", "INTEGER_LITERAL", "WS" }; public static final Vocabulary VOCABULARY = new VocabularyImpl(_LITERAL_NAMES, _SYMBOLIC_NAMES); @@ -94,18 +97,25 @@ public class KlangLexer extends Lexer { public ATN getATN() { return _ATN; } public static final String _serializedATN = - "\3\u608b\ua72a\u8133\ub9ed\u417c\u3be7\u7786\u5964\2\n,\b\1\4\2\t\2\4"+ - "\3\t\3\4\4\t\4\4\5\t\5\4\6\t\6\4\7\t\7\4\b\t\b\4\t\t\t\3\2\3\2\3\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A\3\16\3\16\3\16\3\16\2"+ + "\2\17\3\3\5\4\7\5\t\6\13\7\r\b\17\t\21\n\23\13\25\f\27\r\31\16\33\17\3"+ + "\2\4\3\2\62;\5\2\13\f\17\17\"\"\2G\2\3\3\2\2\2\2\5\3\2\2\2\2\7\3\2\2\2"+ + "\2\t\3\2\2\2\2\13\3\2\2\2\2\r\3\2\2\2\2\17\3\2\2\2\2\21\3\2\2\2\2\23\3"+ + "\2\2\2\2\25\3\2\2\2\2\27\3\2\2\2\2\31\3\2\2\2\2\33\3\2\2\2\3\35\3\2\2"+ + "\2\5#\3\2\2\2\7&\3\2\2\2\t+\3\2\2\2\13\60\3\2\2\2\r\62\3\2\2\2\17\64\3"+ + "\2\2\2\21\66\3\2\2\2\238\3\2\2\2\25:\3\2\2\2\27<\3\2\2\2\31?\3\2\2\2\33"+ + "C\3\2\2\2\35\36\7r\2\2\36\37\7t\2\2\37 \7k\2\2 !\7p\2\2!\"\7v\2\2\"\4"+ + "\3\2\2\2#$\7k\2\2$%\7h\2\2%\6\3\2\2\2&\'\7v\2\2\'(\7j\2\2()\7g\2\2)*\7"+ + "p\2\2*\b\3\2\2\2+,\7g\2\2,-\7n\2\2-.\7u\2\2./\7g\2\2/\n\3\2\2\2\60\61"+ + "\7=\2\2\61\f\3\2\2\2\62\63\7}\2\2\63\16\3\2\2\2\64\65\7\177\2\2\65\20"+ + "\3\2\2\2\66\67\7,\2\2\67\22\3\2\2\289\7-\2\29\24\3\2\2\2:;\7/\2\2;\26"+ + "\3\2\2\2<=\7\'\2\2=\30\3\2\2\2>@\t\2\2\2?>\3\2\2\2@A\3\2\2\2A?\3\2\2\2"+ + "AB\3\2\2\2B\32\3\2\2\2CD\t\3\2\2DE\3\2\2\2EF\b\16\2\2F\34\3\2\2\2\4\2"+ + "A\3\b\2\2"; public static final ATN _ATN = new ATNDeserializer().deserialize(_serializedATN.toCharArray()); static { diff --git a/src/main/antlr4/de/hsrm/compiler/Klang/.antlr/KlangLexer.tokens b/src/main/antlr4/de/hsrm/compiler/Klang/.antlr/KlangLexer.tokens index 6ce5d08..e7ec9c1 100644 --- a/src/main/antlr4/de/hsrm/compiler/Klang/.antlr/KlangLexer.tokens +++ b/src/main/antlr4/de/hsrm/compiler/Klang/.antlr/KlangLexer.tokens @@ -1,14 +1,24 @@ PRINT=1 -SCOL=2 -MULT=3 -ADD=4 -SUB=5 -MOD=6 -INTEGER_LITERAL=7 -WS=8 +IF=2 +THEN=3 +ELSE=4 +SCOL=5 +OBRK=6 +CBRK=7 +MULT=8 +ADD=9 +SUB=10 +MOD=11 +INTEGER_LITERAL=12 +WS=13 'print'=1 -';'=2 -'*'=3 -'+'=4 -'-'=5 -'%'=6 +'if'=2 +'then'=3 +'else'=4 +';'=5 +'{'=6 +'}'=7 +'*'=8 +'+'=9 +'-'=10 +'%'=11 diff --git a/src/main/antlr4/de/hsrm/compiler/Klang/.antlr/KlangParser.java b/src/main/antlr4/de/hsrm/compiler/Klang/.antlr/KlangParser.java index d385994..ec7ddb7 100644 --- a/src/main/antlr4/de/hsrm/compiler/Klang/.antlr/KlangParser.java +++ b/src/main/antlr4/de/hsrm/compiler/Klang/.antlr/KlangParser.java @@ -16,20 +16,23 @@ public class KlangParser extends Parser { protected static final PredictionContextCache _sharedContextCache = new PredictionContextCache(); public static final int - PRINT=1, SCOL=2, MULT=3, ADD=4, SUB=5, MOD=6, INTEGER_LITERAL=7, WS=8; + PRINT=1, IF=2, THEN=3, ELSE=4, SCOL=5, OBRK=6, CBRK=7, MULT=8, ADD=9, + SUB=10, MOD=11, INTEGER_LITERAL=12, WS=13; public static final int - RULE_parse = 0, RULE_block = 1, RULE_statement = 2, RULE_print = 3, RULE_expression = 4, - RULE_atom = 5; + RULE_parse = 0, RULE_block = 1, RULE_braced_block = 2, RULE_statement = 3, + RULE_print = 4, RULE_if_statement = 5, RULE_expression = 6, RULE_atom = 7; public static final String[] ruleNames = { - "parse", "block", "statement", "print", "expression", "atom" + "parse", "block", "braced_block", "statement", "print", "if_statement", + "expression", "atom" }; private static final String[] _LITERAL_NAMES = { - null, "'print'", "';'", "'*'", "'+'", "'-'", "'%'" + null, "'print'", "'if'", "'then'", "'else'", "';'", "'{'", "'}'", "'*'", + "'+'", "'-'", "'%'" }; private static final String[] _SYMBOLIC_NAMES = { - null, "PRINT", "SCOL", "MULT", "ADD", "SUB", "MOD", "INTEGER_LITERAL", - "WS" + null, "PRINT", "IF", "THEN", "ELSE", "SCOL", "OBRK", "CBRK", "MULT", "ADD", + "SUB", "MOD", "INTEGER_LITERAL", "WS" }; public static final Vocabulary VOCABULARY = new VocabularyImpl(_LITERAL_NAMES, _SYMBOLIC_NAMES); @@ -96,7 +99,7 @@ public class KlangParser extends Parser { try { enterOuterAlt(_localctx, 1); { - setState(12); + setState(16); block(); } } @@ -131,17 +134,17 @@ public class KlangParser extends Parser { try { enterOuterAlt(_localctx, 1); { - setState(17); + setState(21); _errHandler.sync(this); _la = _input.LA(1); - while (_la==PRINT) { + while (_la==PRINT || _la==IF) { { { - setState(14); + setState(18); statement(); } } - setState(19); + setState(23); _errHandler.sync(this); _la = _input.LA(1); } @@ -158,10 +161,66 @@ public class KlangParser extends Parser { return _localctx; } + public static class Braced_blockContext extends ParserRuleContext { + public TerminalNode OBRK() { return getToken(KlangParser.OBRK, 0); } + public TerminalNode CBRK() { return getToken(KlangParser.CBRK, 0); } + public List statement() { + return getRuleContexts(StatementContext.class); + } + public StatementContext statement(int i) { + return getRuleContext(StatementContext.class,i); + } + public Braced_blockContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + @Override public int getRuleIndex() { return RULE_braced_block; } + } + + public final Braced_blockContext braced_block() throws RecognitionException { + Braced_blockContext _localctx = new Braced_blockContext(_ctx, getState()); + enterRule(_localctx, 4, RULE_braced_block); + int _la; + try { + enterOuterAlt(_localctx, 1); + { + setState(24); + match(OBRK); + setState(28); + _errHandler.sync(this); + _la = _input.LA(1); + while (_la==PRINT || _la==IF) { + { + { + setState(25); + statement(); + } + } + setState(30); + _errHandler.sync(this); + _la = _input.LA(1); + } + setState(31); + match(CBRK); + } + } + catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } + finally { + exitRule(); + } + return _localctx; + } + public static class StatementContext extends ParserRuleContext { public PrintContext print() { return getRuleContext(PrintContext.class,0); } + public If_statementContext if_statement() { + return getRuleContext(If_statementContext.class,0); + } public StatementContext(ParserRuleContext parent, int invokingState) { super(parent, invokingState); } @@ -170,12 +229,27 @@ public class KlangParser extends Parser { public final StatementContext statement() throws RecognitionException { StatementContext _localctx = new StatementContext(_ctx, getState()); - enterRule(_localctx, 4, RULE_statement); + enterRule(_localctx, 6, RULE_statement); try { - enterOuterAlt(_localctx, 1); - { - setState(20); - print(); + setState(35); + _errHandler.sync(this); + switch (_input.LA(1)) { + case PRINT: + enterOuterAlt(_localctx, 1); + { + setState(33); + print(); + } + break; + case IF: + enterOuterAlt(_localctx, 2); + { + setState(34); + if_statement(); + } + break; + default: + throw new NoViableAltException(this); } } catch (RecognitionException re) { @@ -203,15 +277,15 @@ public class KlangParser extends Parser { public final PrintContext print() throws RecognitionException { PrintContext _localctx = new PrintContext(_ctx, getState()); - enterRule(_localctx, 6, RULE_print); + enterRule(_localctx, 8, RULE_print); try { enterOuterAlt(_localctx, 1); { - setState(22); + setState(37); match(PRINT); - setState(23); + setState(38); expression(); - setState(24); + setState(39); match(SCOL); } } @@ -226,6 +300,65 @@ public class KlangParser extends Parser { return _localctx; } + public static class If_statementContext extends ParserRuleContext { + public TerminalNode IF() { return getToken(KlangParser.IF, 0); } + public ExpressionContext expression() { + return getRuleContext(ExpressionContext.class,0); + } + public TerminalNode THEN() { return getToken(KlangParser.THEN, 0); } + public List braced_block() { + return getRuleContexts(Braced_blockContext.class); + } + public Braced_blockContext braced_block(int i) { + return getRuleContext(Braced_blockContext.class,i); + } + public TerminalNode ELSE() { return getToken(KlangParser.ELSE, 0); } + public If_statementContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + @Override public int getRuleIndex() { return RULE_if_statement; } + } + + public final If_statementContext if_statement() throws RecognitionException { + If_statementContext _localctx = new If_statementContext(_ctx, getState()); + enterRule(_localctx, 10, RULE_if_statement); + int _la; + try { + enterOuterAlt(_localctx, 1); + { + setState(41); + match(IF); + setState(42); + expression(); + setState(43); + match(THEN); + setState(44); + braced_block(); + setState(47); + _errHandler.sync(this); + _la = _input.LA(1); + if (_la==ELSE) { + { + setState(45); + match(ELSE); + setState(46); + braced_block(); + } + } + + } + } + catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } + finally { + exitRule(); + } + return _localctx; + } + public static class ExpressionContext extends ParserRuleContext { public ExpressionContext(ParserRuleContext parent, int invokingState) { super(parent, invokingState); @@ -266,6 +399,12 @@ public class KlangParser extends Parser { } public UnaryNegateExpressionContext(ExpressionContext ctx) { copyFrom(ctx); } } + public static class AtomExpressionContext extends ExpressionContext { + public AtomContext atom() { + return getRuleContext(AtomContext.class,0); + } + public AtomExpressionContext(ExpressionContext ctx) { copyFrom(ctx); } + } public static class MultiplicationExpressionContext extends ExpressionContext { public List atom() { return getRuleContexts(AtomContext.class); @@ -279,21 +418,21 @@ public class KlangParser extends Parser { public final ExpressionContext expression() throws RecognitionException { ExpressionContext _localctx = new ExpressionContext(_ctx, getState()); - enterRule(_localctx, 8, RULE_expression); + enterRule(_localctx, 12, RULE_expression); int _la; try { - setState(40); + setState(64); _errHandler.sync(this); - switch ( getInterpreter().adaptivePredict(_input,1,_ctx) ) { + switch ( getInterpreter().adaptivePredict(_input,4,_ctx) ) { case 1: _localctx = new MultiplicationExpressionContext(_localctx); enterOuterAlt(_localctx, 1); { - setState(26); + setState(49); atom(); - setState(27); + setState(50); match(MULT); - setState(28); + setState(51); atom(); } break; @@ -301,9 +440,9 @@ public class KlangParser extends Parser { _localctx = new AdditiveExpressionContext(_localctx); enterOuterAlt(_localctx, 2); { - setState(30); + setState(53); atom(); - setState(31); + setState(54); ((AdditiveExpressionContext)_localctx).op = _input.LT(1); _la = _input.LA(1); if ( !(_la==ADD || _la==SUB) ) { @@ -314,7 +453,7 @@ public class KlangParser extends Parser { _errHandler.reportMatch(this); consume(); } - setState(32); + setState(55); atom(); } break; @@ -322,11 +461,11 @@ public class KlangParser extends Parser { _localctx = new ModuloExpressionContext(_localctx); enterOuterAlt(_localctx, 3); { - setState(34); + setState(57); atom(); - setState(35); + setState(58); match(MOD); - setState(36); + setState(59); atom(); } break; @@ -334,9 +473,17 @@ public class KlangParser extends Parser { _localctx = new UnaryNegateExpressionContext(_localctx); enterOuterAlt(_localctx, 4); { - setState(38); + setState(61); match(SUB); - setState(39); + setState(62); + atom(); + } + break; + case 5: + _localctx = new AtomExpressionContext(_localctx); + enterOuterAlt(_localctx, 5); + { + setState(63); atom(); } break; @@ -371,12 +518,12 @@ public class KlangParser extends Parser { public final AtomContext atom() throws RecognitionException { AtomContext _localctx = new AtomContext(_ctx, getState()); - enterRule(_localctx, 10, RULE_atom); + enterRule(_localctx, 14, RULE_atom); try { _localctx = new IntAtomContext(_localctx); enterOuterAlt(_localctx, 1); { - setState(42); + setState(66); match(INTEGER_LITERAL); } } @@ -392,18 +539,24 @@ public class KlangParser extends Parser { } public static final String _serializedATN = - "\3\u608b\ua72a\u8133\ub9ed\u417c\u3be7\u7786\u5964\3\n/\4\2\t\2\4\3\t"+ - "\3\4\4\t\4\4\5\t\5\4\6\t\6\4\7\t\7\3\2\3\2\3\3\7\3\22\n\3\f\3\16\3\25"+ - "\13\3\3\4\3\4\3\5\3\5\3\5\3\5\3\6\3\6\3\6\3\6\3\6\3\6\3\6\3\6\3\6\3\6"+ - "\3\6\3\6\3\6\3\6\5\6+\n\6\3\7\3\7\3\7\2\2\b\2\4\6\b\n\f\2\3\3\2\6\7\2"+ - ",\2\16\3\2\2\2\4\23\3\2\2\2\6\26\3\2\2\2\b\30\3\2\2\2\n*\3\2\2\2\f,\3"+ - "\2\2\2\16\17\5\4\3\2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\31\27\3\2\2\2\32\36\7\b\2"+ + "\2\33\35\5\b\5\2\34\33\3\2\2\2\35 \3\2\2\2\36\34\3\2\2\2\36\37\3\2\2\2"+ + "\37!\3\2\2\2 \36\3\2\2\2!\"\7\t\2\2\"\7\3\2\2\2#&\5\n\6\2$&\5\f\7\2%#"+ + "\3\2\2\2%$\3\2\2\2&\t\3\2\2\2\'(\7\3\2\2()\5\16\b\2)*\7\7\2\2*\13\3\2"+ + "\2\2+,\7\4\2\2,-\5\16\b\2-.\7\5\2\2.\61\5\6\4\2/\60\7\6\2\2\60\62\5\6"+ + "\4\2\61/\3\2\2\2\61\62\3\2\2\2\62\r\3\2\2\2\63\64\5\20\t\2\64\65\7\n\2"+ + "\2\65\66\5\20\t\2\66C\3\2\2\2\678\5\20\t\289\t\2\2\29:\5\20\t\2:C\3\2"+ + "\2\2;<\5\20\t\2<=\7\r\2\2=>\5\20\t\2>C\3\2\2\2?@\7\f\2\2@C\5\20\t\2AC"+ + "\5\20\t\2B\63\3\2\2\2B\67\3\2\2\2B;\3\2\2\2B?\3\2\2\2BA\3\2\2\2C\17\3"+ + "\2\2\2DE\7\16\2\2E\21\3\2\2\2\7\27\36%\61B"; public static final ATN _ATN = new ATNDeserializer().deserialize(_serializedATN.toCharArray()); static { diff --git a/src/main/antlr4/de/hsrm/compiler/Klang/Klang.g4 b/src/main/antlr4/de/hsrm/compiler/Klang/Klang.g4 index 2f2b5cb..19f19a2 100644 --- a/src/main/antlr4/de/hsrm/compiler/Klang/Klang.g4 +++ b/src/main/antlr4/de/hsrm/compiler/Klang/Klang.g4 @@ -8,27 +8,47 @@ block : statement* ; +braced_block + : OBRK statement* CBRK + ; + statement : print + | if_statement ; print : PRINT expression SCOL ; +if_statement + : IF expression THEN braced_block (ELSE braced_block)? + ; + expression : atom MULT atom #multiplicationExpression | atom op=(ADD | SUB) atom #additiveExpression | atom MOD atom #moduloExpression | SUB atom #unaryNegateExpression + | atom #atomExpression ; atom : INTEGER_LITERAL #intAtom ; +/* + if 5 = 5 then whatever else whatever + */ + PRINT: 'print'; +IF: 'if'; +THEN: 'then'; +ELSE: 'else'; + SCOL: ';'; +OBRK: '{'; +CBRK: '}'; MULT: '*'; ADD: '+'; diff --git a/src/main/java/de/hsrm/compiler/Klang/Klang.java b/src/main/java/de/hsrm/compiler/Klang/Klang.java index fd370b3..9def19d 100644 --- a/src/main/java/de/hsrm/compiler/Klang/Klang.java +++ b/src/main/java/de/hsrm/compiler/Klang/Klang.java @@ -19,9 +19,9 @@ public class Klang { KlangParser parser = new KlangParser(tokens); ParseTree tree = parser.parse(); // begin parsing at init rule - StringBuilder sb = new StringBuilder(); - JSVisitor visitor = new JSVisitor(sb); + // StringBuilder sb = new StringBuilder(); + Visitor visitor = new Visitor(); visitor.visit(tree); - System.out.println(sb.toString()); + // System.out.println(sb.toString()); } } diff --git a/src/main/java/de/hsrm/compiler/Klang/Visitor.java b/src/main/java/de/hsrm/compiler/Klang/Visitor.java index bdc084d..939c19c 100644 --- a/src/main/java/de/hsrm/compiler/Klang/Visitor.java +++ b/src/main/java/de/hsrm/compiler/Klang/Visitor.java @@ -9,6 +9,17 @@ public class Visitor extends KlangBaseVisitor { return value; } + @Override + public Value visitIf_statement(KlangParser.If_statementContext ctx) { + Value cond = this.visit(ctx.expression()); + if (cond.asInteger() != 0) { + this.visit(ctx.braced_block(0)); + } else if (ctx.braced_block().size() > 1) { + this.visit(ctx.braced_block(1)); + } + return null; + } + @Override public Value visitMultiplicationExpression(KlangParser.MultiplicationExpressionContext ctx) { Value left = this.visit(ctx.atom(0)); @@ -27,7 +38,8 @@ public class Visitor extends KlangBaseVisitor { case KlangParser.SUB: return new Value(left.asInteger() - right.asInteger()); default: - throw new RuntimeException("Unknown operator for additive expression: "+ KlangParser.VOCABULARY.getDisplayName(ctx.op.getType())); + throw new RuntimeException( + "Unknown operator for additive expression: " + KlangParser.VOCABULARY.getDisplayName(ctx.op.getType())); } } @@ -44,6 +56,11 @@ public class Visitor extends KlangBaseVisitor { return new Value(-value.asInteger()); } + @Override + public Value visitAtomExpression(KlangParser.AtomExpressionContext ctx) { + return this.visit(ctx.atom()); + } + @Override public Value visitIntAtom(KlangParser.IntAtomContext ctx) { return new Value(Integer.parseInt(ctx.getText())); From dfefee8a71b3c9593798393c861be62533334b91 Mon Sep 17 00:00:00 2001 From: Marvin Kaiser Date: Tue, 29 Oct 2019 10:32:17 +0100 Subject: [PATCH 3/5] removed "then" from if statement --- .../hsrm/compiler/Klang/.antlr/Klang.interp | 4 +- .../hsrm/compiler/Klang/.antlr/Klang.tokens | 38 +++++---- .../compiler/Klang/.antlr/KlangLexer.interp | 5 +- .../compiler/Klang/.antlr/KlangLexer.java | 50 ++++++------ .../compiler/Klang/.antlr/KlangLexer.tokens | 38 +++++---- .../compiler/Klang/.antlr/KlangParser.java | 81 +++++++++---------- .../antlr4/de/hsrm/compiler/Klang/Klang.g4 | 3 +- 7 files changed, 102 insertions(+), 117 deletions(-) diff --git a/src/main/antlr4/de/hsrm/compiler/Klang/.antlr/Klang.interp b/src/main/antlr4/de/hsrm/compiler/Klang/.antlr/Klang.interp index 6d3ac3d..e4cbc42 100644 --- a/src/main/antlr4/de/hsrm/compiler/Klang/.antlr/Klang.interp +++ b/src/main/antlr4/de/hsrm/compiler/Klang/.antlr/Klang.interp @@ -2,7 +2,6 @@ token literal names: null 'print' 'if' -'then' 'else' ';' '{' @@ -18,7 +17,6 @@ token symbolic names: null PRINT IF -THEN ELSE SCOL OBRK @@ -42,4 +40,4 @@ atom atn: -[3, 24715, 42794, 33075, 47597, 16764, 15335, 30598, 22884, 3, 15, 71, 4, 2, 9, 2, 4, 3, 9, 3, 4, 4, 9, 4, 4, 5, 9, 5, 4, 6, 9, 6, 4, 7, 9, 7, 4, 8, 9, 8, 4, 9, 9, 9, 3, 2, 3, 2, 3, 3, 7, 3, 22, 10, 3, 12, 3, 14, 3, 25, 11, 3, 3, 4, 3, 4, 7, 4, 29, 10, 4, 12, 4, 14, 4, 32, 11, 4, 3,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2, 2, 48, 50, 5, 6, 4, 2, 49, 47, 3, 2, 2, 2, 49, 50, 3, 2, 2, 2, 50, 13, 3, 2, 2, 2, 51, 52, 5, 16, 9, 2, 52, 53, 7, 10, 2, 2, 53, 54, 5, 16, 9, 2, 54, 67, 3, 2, 2, 2, 55, 56, 5, 16, 9, 2, 56, 57, 9, 2, 2, 2, 57, 58, 5, 16, 9, 2, 58, 67, 3, 2, 2, 2, 59, 60, 5, 16, 9, 2, 60, 61, 7, 13, 2, 2, 61, 62, 5, 16, 9, 2, 62, 67, 3, 2, 2, 2, 63, 64, 7, 12, 2, 2, 64, 67, 5, 16, 9, 2, 65, 67, 5, 16, 9, 2, 66, 51, 3, 2, 2, 2, 66, 55, 3, 2, 2, 2, 66, 59, 3, 2, 2, 2, 66, 63, 3, 2, 2, 2, 66, 65, 3, 2, 2, 2, 67, 15, 3, 2, 2, 2, 68, 69, 7, 14, 2, 2, 69, 17, 3, 2, 2, 2, 7, 23, 30, 37, 49, 66] \ No newline at end of file +[3, 24715, 42794, 33075, 47597, 16764, 15335, 30598, 22884, 3, 14, 70, 4, 2, 9, 2, 4, 3, 9, 3, 4, 4, 9, 4, 4, 5, 9, 5, 4, 6, 9, 6, 4, 7, 9, 7, 4, 8, 9, 8, 4, 9, 9, 9, 3, 2, 3, 2, 3, 3, 7, 3, 22, 10, 3, 12, 3, 14, 3, 25, 11, 3, 3, 4, 3, 4, 7, 4, 29, 10, 4, 12, 4, 14, 4, 32, 11, 4, 3, 4, 3, 4, 3, 5, 3, 5, 5, 5, 38, 10, 5, 3, 6, 3, 6, 3, 6, 3, 6, 3, 7, 3, 7, 3, 7, 3, 7, 3, 7, 5, 7, 49, 10, 7, 3, 8, 3, 8, 3, 8, 3, 8, 3, 8, 3, 8, 3, 8, 3, 8, 3, 8, 3, 8, 3, 8, 3, 8, 3, 8, 3, 8, 3, 8, 5, 8, 66, 10, 8, 3, 9, 3, 9, 3, 9, 2, 2, 10, 2, 4, 6, 8, 10, 12, 14, 16, 2, 3, 3, 2, 10, 11, 2, 69, 2, 18, 3, 2, 2, 2, 4, 23, 3, 2, 2, 2, 6, 26, 3, 2, 2, 2, 8, 37, 3, 2, 2, 2, 10, 39, 3, 2, 2, 2, 12, 43, 3, 2, 2, 2, 14, 65, 3, 2, 2, 2, 16, 67, 3, 2, 2, 2, 18, 19, 5, 4, 3, 2, 19, 3, 3, 2, 2, 2, 20, 22, 5, 8, 5, 2, 21, 20, 3, 2, 2, 2, 22, 25, 3, 2, 2, 2, 23, 21, 3, 2, 2, 2, 23, 24, 3, 2, 2, 2, 24, 5, 3, 2, 2, 2, 25, 23, 3, 2, 2, 2, 26, 30, 7, 7, 2, 2, 27, 29, 5, 8, 5, 2, 28, 27, 3, 2, 2, 2, 29, 32, 3, 2, 2, 2, 30, 28, 3, 2, 2, 2, 30, 31, 3, 2, 2, 2, 31, 33, 3, 2, 2, 2, 32, 30, 3, 2, 2, 2, 33, 34, 7, 8, 2, 2, 34, 7, 3, 2, 2, 2, 35, 38, 5, 10, 6, 2, 36, 38, 5, 12, 7, 2, 37, 35, 3, 2, 2, 2, 37, 36, 3, 2, 2, 2, 38, 9, 3, 2, 2, 2, 39, 40, 7, 3, 2, 2, 40, 41, 5, 14, 8, 2, 41, 42, 7, 6, 2, 2, 42, 11, 3, 2, 2, 2, 43, 44, 7, 4, 2, 2, 44, 45, 5, 14, 8, 2, 45, 48, 5, 6, 4, 2, 46, 47, 7, 5, 2, 2, 47, 49, 5, 6, 4, 2, 48, 46, 3, 2, 2, 2, 48, 49, 3, 2, 2, 2, 49, 13, 3, 2, 2, 2, 50, 51, 5, 16, 9, 2, 51, 52, 7, 9, 2, 2, 52, 53, 5, 16, 9, 2, 53, 66, 3, 2, 2, 2, 54, 55, 5, 16, 9, 2, 55, 56, 9, 2, 2, 2, 56, 57, 5, 16, 9, 2, 57, 66, 3, 2, 2, 2, 58, 59, 5, 16, 9, 2, 59, 60, 7, 12, 2, 2, 60, 61, 5, 16, 9, 2, 61, 66, 3, 2, 2, 2, 62, 63, 7, 11, 2, 2, 63, 66, 5, 16, 9, 2, 64, 66, 5, 16, 9, 2, 65, 50, 3, 2, 2, 2, 65, 54, 3, 2, 2, 2, 65, 58, 3, 2, 2, 2, 65, 62, 3, 2, 2, 2, 65, 64, 3, 2, 2, 2, 66, 15, 3, 2, 2, 2, 67, 68, 7, 13, 2, 2, 68, 17, 3, 2, 2, 2, 7, 23, 30, 37, 48, 65] \ No newline at end of file diff --git a/src/main/antlr4/de/hsrm/compiler/Klang/.antlr/Klang.tokens b/src/main/antlr4/de/hsrm/compiler/Klang/.antlr/Klang.tokens index e7ec9c1..bcfa984 100644 --- a/src/main/antlr4/de/hsrm/compiler/Klang/.antlr/Klang.tokens +++ b/src/main/antlr4/de/hsrm/compiler/Klang/.antlr/Klang.tokens @@ -1,24 +1,22 @@ PRINT=1 IF=2 -THEN=3 -ELSE=4 -SCOL=5 -OBRK=6 -CBRK=7 -MULT=8 -ADD=9 -SUB=10 -MOD=11 -INTEGER_LITERAL=12 -WS=13 +ELSE=3 +SCOL=4 +OBRK=5 +CBRK=6 +MULT=7 +ADD=8 +SUB=9 +MOD=10 +INTEGER_LITERAL=11 +WS=12 'print'=1 'if'=2 -'then'=3 -'else'=4 -';'=5 -'{'=6 -'}'=7 -'*'=8 -'+'=9 -'-'=10 -'%'=11 +'else'=3 +';'=4 +'{'=5 +'}'=6 +'*'=7 +'+'=8 +'-'=9 +'%'=10 diff --git a/src/main/antlr4/de/hsrm/compiler/Klang/.antlr/KlangLexer.interp b/src/main/antlr4/de/hsrm/compiler/Klang/.antlr/KlangLexer.interp index 6a31f8e..74fc623 100644 --- a/src/main/antlr4/de/hsrm/compiler/Klang/.antlr/KlangLexer.interp +++ b/src/main/antlr4/de/hsrm/compiler/Klang/.antlr/KlangLexer.interp @@ -2,7 +2,6 @@ token literal names: null 'print' 'if' -'then' 'else' ';' '{' @@ -18,7 +17,6 @@ token symbolic names: null PRINT IF -THEN ELSE SCOL OBRK @@ -33,7 +31,6 @@ WS rule names: PRINT IF -THEN ELSE SCOL OBRK @@ -53,4 +50,4 @@ mode names: DEFAULT_MODE atn: -[3, 24715, 42794, 33075, 47597, 16764, 15335, 30598, 22884, 2, 15, 71, 8, 1, 4, 2, 9, 2, 4, 3, 9, 3, 4, 4, 9, 4, 4, 5, 9, 5, 4, 6, 9, 6, 4, 7, 9, 7, 4, 8, 9, 8, 4, 9, 9, 9, 4, 10, 9, 10, 4, 11, 9, 11, 4, 12, 9, 12, 4, 13, 9, 13, 4, 14, 9, 14, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 3, 3, 3, 3, 3, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 5, 3, 5, 3, 5, 3, 5, 3, 5, 3, 6, 3, 6, 3, 7, 3, 7, 3, 8, 3, 8, 3, 9, 3, 9, 3, 10, 3, 10, 3, 11, 3, 11, 3, 12, 3, 12, 3, 13, 6, 13, 64, 10, 13, 13, 13, 14, 13, 65, 3, 14, 3, 14, 3, 14, 3, 14, 2, 2, 15, 3, 3, 5, 4, 7, 5, 9, 6, 11, 7, 13, 8, 15, 9, 17, 10, 19, 11, 21, 12, 23, 13, 25, 14, 27, 15, 3, 2, 4, 3, 2, 50, 59, 5, 2, 11, 12, 15, 15, 34, 34, 2, 71, 2, 3, 3, 2, 2, 2, 2, 5, 3, 2, 2, 2, 2, 7, 3, 2, 2, 2, 2, 9, 3, 2, 2, 2, 2, 11, 3, 2, 2, 2, 2, 13, 3, 2, 2, 2, 2, 15, 3, 2, 2, 2, 2, 17, 3, 2, 2, 2, 2, 19, 3, 2, 2, 2, 2, 21, 3, 2, 2, 2, 2, 23, 3, 2, 2, 2, 2, 25, 3, 2, 2, 2, 2, 27, 3, 2, 2, 2, 3, 29, 3, 2, 2, 2, 5, 35, 3, 2, 2, 2, 7, 38, 3, 2, 2, 2, 9, 43, 3, 2, 2, 2, 11, 48, 3, 2, 2, 2, 13, 50, 3, 2, 2, 2, 15, 52, 3, 2, 2, 2, 17, 54, 3, 2, 2, 2, 19, 56, 3, 2, 2, 2, 21, 58, 3, 2, 2, 2, 23, 60, 3, 2, 2, 2, 25, 63, 3, 2, 2, 2, 27, 67, 3, 2, 2, 2, 29, 30, 7, 114, 2, 2, 30, 31, 7, 116, 2, 2, 31, 32, 7, 107, 2, 2, 32, 33, 7, 112, 2, 2, 33, 34, 7, 118, 2, 2, 34, 4, 3, 2, 2, 2, 35, 36, 7, 107, 2, 2, 36, 37, 7, 104, 2, 2, 37, 6, 3, 2, 2, 2, 38, 39, 7, 118, 2, 2, 39, 40, 7, 106, 2, 2, 40, 41, 7, 103, 2, 2, 41, 42, 7, 112, 2, 2, 42, 8, 3, 2, 2, 2, 43, 44, 7, 103, 2, 2, 44, 45, 7, 110, 2, 2, 45, 46, 7, 117, 2, 2, 46, 47, 7, 103, 2, 2, 47, 10, 3, 2, 2, 2, 48, 49, 7, 61, 2, 2, 49, 12, 3, 2, 2, 2, 50, 51, 7, 125, 2, 2, 51, 14, 3, 2, 2, 2, 52, 53, 7, 127, 2, 2, 53, 16, 3, 2, 2, 2, 54, 55, 7, 44, 2, 2, 55, 18, 3, 2, 2, 2, 56, 57, 7, 45, 2, 2, 57, 20, 3, 2, 2, 2, 58, 59, 7, 47, 2, 2, 59, 22, 3, 2, 2, 2, 60, 61, 7, 39, 2, 2, 61, 24, 3, 2, 2, 2, 62, 64, 9, 2, 2, 2, 63, 62, 3, 2, 2, 2, 64, 65, 3, 2, 2, 2, 65, 63, 3, 2, 2, 2, 65, 66, 3, 2, 2, 2, 66, 26, 3, 2, 2, 2, 67, 68, 9, 3, 2, 2, 68, 69, 3, 2, 2, 2, 69, 70, 8, 14, 2, 2, 70, 28, 3, 2, 2, 2, 4, 2, 65, 3, 8, 2, 2] \ No newline at end of file +[3, 24715, 42794, 33075, 47597, 16764, 15335, 30598, 22884, 2, 14, 64, 8, 1, 4, 2, 9, 2, 4, 3, 9, 3, 4, 4, 9, 4, 4, 5, 9, 5, 4, 6, 9, 6, 4, 7, 9, 7, 4, 8, 9, 8, 4, 9, 9, 9, 4, 10, 9, 10, 4, 11, 9, 11, 4, 12, 9, 12, 4, 13, 9, 13, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 3, 3, 3, 3, 3, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 5, 3, 5, 3, 6, 3, 6, 3, 7, 3, 7, 3, 8, 3, 8, 3, 9, 3, 9, 3, 10, 3, 10, 3, 11, 3, 11, 3, 12, 6, 12, 57, 10, 12, 13, 12, 14, 12, 58, 3, 13, 3, 13, 3, 13, 3, 13, 2, 2, 14, 3, 3, 5, 4, 7, 5, 9, 6, 11, 7, 13, 8, 15, 9, 17, 10, 19, 11, 21, 12, 23, 13, 25, 14, 3, 2, 4, 3, 2, 50, 59, 5, 2, 11, 12, 15, 15, 34, 34, 2, 64, 2, 3, 3, 2, 2, 2, 2, 5, 3, 2, 2, 2, 2, 7, 3, 2, 2, 2, 2, 9, 3, 2, 2, 2, 2, 11, 3, 2, 2, 2, 2, 13, 3, 2, 2, 2, 2, 15, 3, 2, 2, 2, 2, 17, 3, 2, 2, 2, 2, 19, 3, 2, 2, 2, 2, 21, 3, 2, 2, 2, 2, 23, 3, 2, 2, 2, 2, 25, 3, 2, 2, 2, 3, 27, 3, 2, 2, 2, 5, 33, 3, 2, 2, 2, 7, 36, 3, 2, 2, 2, 9, 41, 3, 2, 2, 2, 11, 43, 3, 2, 2, 2, 13, 45, 3, 2, 2, 2, 15, 47, 3, 2, 2, 2, 17, 49, 3, 2, 2, 2, 19, 51, 3, 2, 2, 2, 21, 53, 3, 2, 2, 2, 23, 56, 3, 2, 2, 2, 25, 60, 3, 2, 2, 2, 27, 28, 7, 114, 2, 2, 28, 29, 7, 116, 2, 2, 29, 30, 7, 107, 2, 2, 30, 31, 7, 112, 2, 2, 31, 32, 7, 118, 2, 2, 32, 4, 3, 2, 2, 2, 33, 34, 7, 107, 2, 2, 34, 35, 7, 104, 2, 2, 35, 6, 3, 2, 2, 2, 36, 37, 7, 103, 2, 2, 37, 38, 7, 110, 2, 2, 38, 39, 7, 117, 2, 2, 39, 40, 7, 103, 2, 2, 40, 8, 3, 2, 2, 2, 41, 42, 7, 61, 2, 2, 42, 10, 3, 2, 2, 2, 43, 44, 7, 125, 2, 2, 44, 12, 3, 2, 2, 2, 45, 46, 7, 127, 2, 2, 46, 14, 3, 2, 2, 2, 47, 48, 7, 44, 2, 2, 48, 16, 3, 2, 2, 2, 49, 50, 7, 45, 2, 2, 50, 18, 3, 2, 2, 2, 51, 52, 7, 47, 2, 2, 52, 20, 3, 2, 2, 2, 53, 54, 7, 39, 2, 2, 54, 22, 3, 2, 2, 2, 55, 57, 9, 2, 2, 2, 56, 55, 3, 2, 2, 2, 57, 58, 3, 2, 2, 2, 58, 56, 3, 2, 2, 2, 58, 59, 3, 2, 2, 2, 59, 24, 3, 2, 2, 2, 60, 61, 9, 3, 2, 2, 61, 62, 3, 2, 2, 2, 62, 63, 8, 13, 2, 2, 63, 26, 3, 2, 2, 2, 4, 2, 58, 3, 8, 2, 2] \ No newline at end of file diff --git a/src/main/antlr4/de/hsrm/compiler/Klang/.antlr/KlangLexer.java b/src/main/antlr4/de/hsrm/compiler/Klang/.antlr/KlangLexer.java index 1e028a0..60aef1c 100644 --- a/src/main/antlr4/de/hsrm/compiler/Klang/.antlr/KlangLexer.java +++ b/src/main/antlr4/de/hsrm/compiler/Klang/.antlr/KlangLexer.java @@ -16,8 +16,8 @@ public class KlangLexer extends Lexer { protected static final PredictionContextCache _sharedContextCache = new PredictionContextCache(); public static final int - PRINT=1, IF=2, THEN=3, ELSE=4, SCOL=5, OBRK=6, CBRK=7, MULT=8, ADD=9, - SUB=10, MOD=11, INTEGER_LITERAL=12, WS=13; + PRINT=1, IF=2, ELSE=3, SCOL=4, OBRK=5, CBRK=6, MULT=7, ADD=8, SUB=9, MOD=10, + INTEGER_LITERAL=11, WS=12; public static String[] channelNames = { "DEFAULT_TOKEN_CHANNEL", "HIDDEN" }; @@ -27,17 +27,17 @@ public class KlangLexer extends Lexer { }; public static final String[] ruleNames = { - "PRINT", "IF", "THEN", "ELSE", "SCOL", "OBRK", "CBRK", "MULT", "ADD", - "SUB", "MOD", "INTEGER_LITERAL", "WS" + "PRINT", "IF", "ELSE", "SCOL", "OBRK", "CBRK", "MULT", "ADD", "SUB", "MOD", + "INTEGER_LITERAL", "WS" }; private static final String[] _LITERAL_NAMES = { - null, "'print'", "'if'", "'then'", "'else'", "';'", "'{'", "'}'", "'*'", - "'+'", "'-'", "'%'" + null, "'print'", "'if'", "'else'", "';'", "'{'", "'}'", "'*'", "'+'", + "'-'", "'%'" }; private static final String[] _SYMBOLIC_NAMES = { - null, "PRINT", "IF", "THEN", "ELSE", "SCOL", "OBRK", "CBRK", "MULT", "ADD", - "SUB", "MOD", "INTEGER_LITERAL", "WS" + null, "PRINT", "IF", "ELSE", "SCOL", "OBRK", "CBRK", "MULT", "ADD", "SUB", + "MOD", "INTEGER_LITERAL", "WS" }; public static final Vocabulary VOCABULARY = new VocabularyImpl(_LITERAL_NAMES, _SYMBOLIC_NAMES); @@ -97,25 +97,23 @@ public class KlangLexer extends Lexer { public ATN getATN() { return _ATN; } public static final String _serializedATN = - "\3\u608b\ua72a\u8133\ub9ed\u417c\u3be7\u7786\u5964\2\17G\b\1\4\2\t\2\4"+ + "\3\u608b\ua72a\u8133\ub9ed\u417c\u3be7\u7786\u5964\2\16@\b\1\4\2\t\2\4"+ "\3\t\3\4\4\t\4\4\5\t\5\4\6\t\6\4\7\t\7\4\b\t\b\4\t\t\t\4\n\t\n\4\13\t"+ - "\13\4\f\t\f\4\r\t\r\4\16\t\16\3\2\3\2\3\2\3\2\3\2\3\2\3\3\3\3\3\3\3\4"+ - "\3\4\3\4\3\4\3\4\3\5\3\5\3\5\3\5\3\5\3\6\3\6\3\7\3\7\3\b\3\b\3\t\3\t\3"+ - "\n\3\n\3\13\3\13\3\f\3\f\3\r\6\r@\n\r\r\r\16\r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3\6\3\6\3\7\3\7\3\b\3\b\3\t\3\t\3\n\3\n\3\13\3\13\3\f\6"+ + "\f9\n\f\r\f\16\f:\3\r\3\r\3\r\3\r\2\2\16\3\3\5\4\7\5\t\6\13\7\r\b\17\t"+ + "\21\n\23\13\25\f\27\r\31\16\3\2\4\3\2\62;\5\2\13\f\17\17\"\"\2@\2\3\3"+ + "\2\2\2\2\5\3\2\2\2\2\7\3\2\2\2\2\t\3\2\2\2\2\13\3\2\2\2\2\r\3\2\2\2\2"+ + "\17\3\2\2\2\2\21\3\2\2\2\2\23\3\2\2\2\2\25\3\2\2\2\2\27\3\2\2\2\2\31\3"+ + "\2\2\2\3\33\3\2\2\2\5!\3\2\2\2\7$\3\2\2\2\t)\3\2\2\2\13+\3\2\2\2\r-\3"+ + "\2\2\2\17/\3\2\2\2\21\61\3\2\2\2\23\63\3\2\2\2\25\65\3\2\2\2\278\3\2\2"+ + "\2\31<\3\2\2\2\33\34\7r\2\2\34\35\7t\2\2\35\36\7k\2\2\36\37\7p\2\2\37"+ + " \7v\2\2 \4\3\2\2\2!\"\7k\2\2\"#\7h\2\2#\6\3\2\2\2$%\7g\2\2%&\7n\2\2&"+ + "\'\7u\2\2\'(\7g\2\2(\b\3\2\2\2)*\7=\2\2*\n\3\2\2\2+,\7}\2\2,\f\3\2\2\2"+ + "-.\7\177\2\2.\16\3\2\2\2/\60\7,\2\2\60\20\3\2\2\2\61\62\7-\2\2\62\22\3"+ + "\2\2\2\63\64\7/\2\2\64\24\3\2\2\2\65\66\7\'\2\2\66\26\3\2\2\2\679\t\2"+ + "\2\28\67\3\2\2\29:\3\2\2\2:8\3\2\2\2:;\3\2\2\2;\30\3\2\2\2<=\t\3\2\2="+ + ">\3\2\2\2>?\b\r\2\2?\32\3\2\2\2\4\2:\3\b\2\2"; public static final ATN _ATN = new ATNDeserializer().deserialize(_serializedATN.toCharArray()); static { diff --git a/src/main/antlr4/de/hsrm/compiler/Klang/.antlr/KlangLexer.tokens b/src/main/antlr4/de/hsrm/compiler/Klang/.antlr/KlangLexer.tokens index e7ec9c1..bcfa984 100644 --- a/src/main/antlr4/de/hsrm/compiler/Klang/.antlr/KlangLexer.tokens +++ b/src/main/antlr4/de/hsrm/compiler/Klang/.antlr/KlangLexer.tokens @@ -1,24 +1,22 @@ PRINT=1 IF=2 -THEN=3 -ELSE=4 -SCOL=5 -OBRK=6 -CBRK=7 -MULT=8 -ADD=9 -SUB=10 -MOD=11 -INTEGER_LITERAL=12 -WS=13 +ELSE=3 +SCOL=4 +OBRK=5 +CBRK=6 +MULT=7 +ADD=8 +SUB=9 +MOD=10 +INTEGER_LITERAL=11 +WS=12 'print'=1 'if'=2 -'then'=3 -'else'=4 -';'=5 -'{'=6 -'}'=7 -'*'=8 -'+'=9 -'-'=10 -'%'=11 +'else'=3 +';'=4 +'{'=5 +'}'=6 +'*'=7 +'+'=8 +'-'=9 +'%'=10 diff --git a/src/main/antlr4/de/hsrm/compiler/Klang/.antlr/KlangParser.java b/src/main/antlr4/de/hsrm/compiler/Klang/.antlr/KlangParser.java index ec7ddb7..f78ccf6 100644 --- a/src/main/antlr4/de/hsrm/compiler/Klang/.antlr/KlangParser.java +++ b/src/main/antlr4/de/hsrm/compiler/Klang/.antlr/KlangParser.java @@ -16,8 +16,8 @@ public class KlangParser extends Parser { protected static final PredictionContextCache _sharedContextCache = new PredictionContextCache(); public static final int - PRINT=1, IF=2, THEN=3, ELSE=4, SCOL=5, OBRK=6, CBRK=7, MULT=8, ADD=9, - SUB=10, MOD=11, INTEGER_LITERAL=12, WS=13; + PRINT=1, IF=2, ELSE=3, SCOL=4, OBRK=5, CBRK=6, MULT=7, ADD=8, SUB=9, MOD=10, + INTEGER_LITERAL=11, WS=12; public static final int RULE_parse = 0, RULE_block = 1, RULE_braced_block = 2, RULE_statement = 3, RULE_print = 4, RULE_if_statement = 5, RULE_expression = 6, RULE_atom = 7; @@ -27,12 +27,12 @@ public class KlangParser extends Parser { }; private static final String[] _LITERAL_NAMES = { - null, "'print'", "'if'", "'then'", "'else'", "';'", "'{'", "'}'", "'*'", - "'+'", "'-'", "'%'" + null, "'print'", "'if'", "'else'", "';'", "'{'", "'}'", "'*'", "'+'", + "'-'", "'%'" }; private static final String[] _SYMBOLIC_NAMES = { - null, "PRINT", "IF", "THEN", "ELSE", "SCOL", "OBRK", "CBRK", "MULT", "ADD", - "SUB", "MOD", "INTEGER_LITERAL", "WS" + null, "PRINT", "IF", "ELSE", "SCOL", "OBRK", "CBRK", "MULT", "ADD", "SUB", + "MOD", "INTEGER_LITERAL", "WS" }; public static final Vocabulary VOCABULARY = new VocabularyImpl(_LITERAL_NAMES, _SYMBOLIC_NAMES); @@ -305,7 +305,6 @@ public class KlangParser extends Parser { public ExpressionContext expression() { return getRuleContext(ExpressionContext.class,0); } - public TerminalNode THEN() { return getToken(KlangParser.THEN, 0); } public List braced_block() { return getRuleContexts(Braced_blockContext.class); } @@ -331,17 +330,15 @@ public class KlangParser extends Parser { setState(42); expression(); setState(43); - match(THEN); - setState(44); braced_block(); - setState(47); + setState(46); _errHandler.sync(this); _la = _input.LA(1); if (_la==ELSE) { { - setState(45); + setState(44); match(ELSE); - setState(46); + setState(45); braced_block(); } } @@ -421,18 +418,18 @@ public class KlangParser extends Parser { enterRule(_localctx, 12, RULE_expression); int _la; try { - setState(64); + setState(63); _errHandler.sync(this); switch ( getInterpreter().adaptivePredict(_input,4,_ctx) ) { case 1: _localctx = new MultiplicationExpressionContext(_localctx); enterOuterAlt(_localctx, 1); { - setState(49); + setState(48); atom(); - setState(50); + setState(49); match(MULT); - setState(51); + setState(50); atom(); } break; @@ -440,9 +437,9 @@ public class KlangParser extends Parser { _localctx = new AdditiveExpressionContext(_localctx); enterOuterAlt(_localctx, 2); { - setState(53); + setState(52); atom(); - setState(54); + setState(53); ((AdditiveExpressionContext)_localctx).op = _input.LT(1); _la = _input.LA(1); if ( !(_la==ADD || _la==SUB) ) { @@ -453,7 +450,7 @@ public class KlangParser extends Parser { _errHandler.reportMatch(this); consume(); } - setState(55); + setState(54); atom(); } break; @@ -461,11 +458,11 @@ public class KlangParser extends Parser { _localctx = new ModuloExpressionContext(_localctx); enterOuterAlt(_localctx, 3); { - setState(57); + setState(56); atom(); - setState(58); + setState(57); match(MOD); - setState(59); + setState(58); atom(); } break; @@ -473,9 +470,9 @@ public class KlangParser extends Parser { _localctx = new UnaryNegateExpressionContext(_localctx); enterOuterAlt(_localctx, 4); { - setState(61); + setState(60); match(SUB); - setState(62); + setState(61); atom(); } break; @@ -483,7 +480,7 @@ public class KlangParser extends Parser { _localctx = new AtomExpressionContext(_localctx); enterOuterAlt(_localctx, 5); { - setState(63); + setState(62); atom(); } break; @@ -523,7 +520,7 @@ public class KlangParser extends Parser { _localctx = new IntAtomContext(_localctx); enterOuterAlt(_localctx, 1); { - setState(66); + setState(65); match(INTEGER_LITERAL); } } @@ -539,24 +536,24 @@ public class KlangParser extends Parser { } public static final String _serializedATN = - "\3\u608b\ua72a\u8133\ub9ed\u417c\u3be7\u7786\u5964\3\17G\4\2\t\2\4\3\t"+ + "\3\u608b\ua72a\u8133\ub9ed\u417c\u3be7\u7786\u5964\3\16F\4\2\t\2\4\3\t"+ "\3\4\4\t\4\4\5\t\5\4\6\t\6\4\7\t\7\4\b\t\b\4\t\t\t\3\2\3\2\3\3\7\3\26"+ "\n\3\f\3\16\3\31\13\3\3\4\3\4\7\4\35\n\4\f\4\16\4 \13\4\3\4\3\4\3\5\3"+ - "\5\5\5&\n\5\3\6\3\6\3\6\3\6\3\7\3\7\3\7\3\7\3\7\3\7\5\7\62\n\7\3\b\3\b"+ - "\3\b\3\b\3\b\3\b\3\b\3\b\3\b\3\b\3\b\3\b\3\b\3\b\3\b\5\bC\n\b\3\t\3\t"+ - "\3\t\2\2\n\2\4\6\b\n\f\16\20\2\3\3\2\13\f\2F\2\22\3\2\2\2\4\27\3\2\2\2"+ - "\6\32\3\2\2\2\b%\3\2\2\2\n\'\3\2\2\2\f+\3\2\2\2\16B\3\2\2\2\20D\3\2\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\23\3\3\2\2\2\24\26\5\b\5\2\25\24\3\2\2\2\26\31\3\2\2\2\27"+ + "\25\3\2\2\2\27\30\3\2\2\2\30\5\3\2\2\2\31\27\3\2\2\2\32\36\7\7\2\2\33"+ + "\35\5\b\5\2\34\33\3\2\2\2\35 \3\2\2\2\36\34\3\2\2\2\36\37\3\2\2\2\37!"+ + "\3\2\2\2 \36\3\2\2\2!\"\7\b\2\2\"\7\3\2\2\2#&\5\n\6\2$&\5\f\7\2%#\3\2"+ + "\2\2%$\3\2\2\2&\t\3\2\2\2\'(\7\3\2\2()\5\16\b\2)*\7\6\2\2*\13\3\2\2\2"+ + "+,\7\4\2\2,-\5\16\b\2-\60\5\6\4\2./\7\5\2\2/\61\5\6\4\2\60.\3\2\2\2\60"+ + "\61\3\2\2\2\61\r\3\2\2\2\62\63\5\20\t\2\63\64\7\t\2\2\64\65\5\20\t\2\65"+ + "B\3\2\2\2\66\67\5\20\t\2\678\t\2\2\289\5\20\t\29B\3\2\2\2:;\5\20\t\2;"+ + "<\7\f\2\2<=\5\20\t\2=B\3\2\2\2>?\7\13\2\2?B\5\20\t\2@B\5\20\t\2A\62\3"+ + "\2\2\2A\66\3\2\2\2A:\3\2\2\2A>\3\2\2\2A@\3\2\2\2B\17\3\2\2\2CD\7\r\2\2"+ + "D\21\3\2\2\2\7\27\36%\60A"; public static final ATN _ATN = new ATNDeserializer().deserialize(_serializedATN.toCharArray()); static { diff --git a/src/main/antlr4/de/hsrm/compiler/Klang/Klang.g4 b/src/main/antlr4/de/hsrm/compiler/Klang/Klang.g4 index 19f19a2..8212ce1 100644 --- a/src/main/antlr4/de/hsrm/compiler/Klang/Klang.g4 +++ b/src/main/antlr4/de/hsrm/compiler/Klang/Klang.g4 @@ -22,7 +22,7 @@ print ; if_statement - : IF expression THEN braced_block (ELSE braced_block)? + : IF expression braced_block (ELSE braced_block)? ; expression @@ -43,7 +43,6 @@ atom PRINT: 'print'; IF: 'if'; -THEN: 'then'; ELSE: 'else'; SCOL: ';'; From 8ad114107a908f8aa52409ee878a776bfc70025e Mon Sep 17 00:00:00 2001 From: Marvin Kaiser Date: Tue, 29 Oct 2019 10:33:24 +0100 Subject: [PATCH 4/5] removed unnecessary comment --- src/main/antlr4/de/hsrm/compiler/Klang/Klang.g4 | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/main/antlr4/de/hsrm/compiler/Klang/Klang.g4 b/src/main/antlr4/de/hsrm/compiler/Klang/Klang.g4 index 8212ce1..4a8adde 100644 --- a/src/main/antlr4/de/hsrm/compiler/Klang/Klang.g4 +++ b/src/main/antlr4/de/hsrm/compiler/Klang/Klang.g4 @@ -37,10 +37,6 @@ atom : INTEGER_LITERAL #intAtom ; -/* - if 5 = 5 then whatever else whatever - */ - PRINT: 'print'; IF: 'if'; ELSE: 'else'; From d14a7d7cbde2d25c38e404aa24f64aeccff9d1ea Mon Sep 17 00:00:00 2001 From: Marvin Kaiser Date: Mon, 4 Nov 2019 15:41:32 +0100 Subject: [PATCH 5/5] Added basic typing structure --- .../compiler/Klang/types/IntegerType.java | 20 +++++++++++++++++++ .../compiler/Klang/types/PrimitiveType.java | 13 ++++++++++++ .../de/hsrm/compiler/Klang/types/Type.java | 12 +++++++++++ 3 files changed, 45 insertions(+) create mode 100644 src/main/java/de/hsrm/compiler/Klang/types/IntegerType.java create mode 100644 src/main/java/de/hsrm/compiler/Klang/types/PrimitiveType.java create mode 100644 src/main/java/de/hsrm/compiler/Klang/types/Type.java diff --git a/src/main/java/de/hsrm/compiler/Klang/types/IntegerType.java b/src/main/java/de/hsrm/compiler/Klang/types/IntegerType.java new file mode 100644 index 0000000..3803b9e --- /dev/null +++ b/src/main/java/de/hsrm/compiler/Klang/types/IntegerType.java @@ -0,0 +1,20 @@ +package de.hsrm.compiler.Klang.types; + +public class IntegerType extends PrimitiveType { + + private static IntegerType instance = null; + + public static IntegerType getType() { + if (instance != null) { + return instance; + } + instance = new IntegerType(); + return instance; + } + + @Override + public boolean isIntegerType() { + return true; + } + +} \ No newline at end of file diff --git a/src/main/java/de/hsrm/compiler/Klang/types/PrimitiveType.java b/src/main/java/de/hsrm/compiler/Klang/types/PrimitiveType.java new file mode 100644 index 0000000..b45c08f --- /dev/null +++ b/src/main/java/de/hsrm/compiler/Klang/types/PrimitiveType.java @@ -0,0 +1,13 @@ +package de.hsrm.compiler.Klang.types; + +public abstract class PrimitiveType extends Type { + + @Override + public boolean isPrimitiveType() { + return true; + } + + public boolean isIntegerType() { + return false; + }; +} \ No newline at end of file diff --git a/src/main/java/de/hsrm/compiler/Klang/types/Type.java b/src/main/java/de/hsrm/compiler/Klang/types/Type.java new file mode 100644 index 0000000..db90f30 --- /dev/null +++ b/src/main/java/de/hsrm/compiler/Klang/types/Type.java @@ -0,0 +1,12 @@ +package de.hsrm.compiler.Klang.types; + +public abstract class Type { + + // Returns an instance of IntegerType + // Used for adding new types to a node + public static IntegerType getIntegerType() { + return IntegerType.getType(); + } + + public abstract boolean isPrimitiveType(); +} \ No newline at end of file