From 3f9072c7bd524cfc7bed9198a9500df8d83a0019 Mon Sep 17 00:00:00 2001 From: Vu Tuan Minh Date: Tue, 1 Apr 2025 17:52:45 +0200 Subject: [PATCH] error 4 --- src/main/antlr/.antlr/VSLLexer.java | 2 +- src/main/antlr/.antlr/VSLParser.interp | 2 +- src/main/antlr/.antlr/VSLParser.java | 177 +++++++++++++------------ src/main/antlr/VSLParser.g | 2 + src/main/java/TP2/asd/ExprEval.java | 23 ---- src/main/java/TP2/asd/Interface.java | 52 ++++---- src/main/java/TP2/asd/Program.java | 81 ++++++----- 7 files changed, 161 insertions(+), 178 deletions(-) delete mode 100644 src/main/java/TP2/asd/ExprEval.java diff --git a/src/main/antlr/.antlr/VSLLexer.java b/src/main/antlr/.antlr/VSLLexer.java index 788a8f1..b065b3a 100644 --- a/src/main/antlr/.antlr/VSLLexer.java +++ b/src/main/antlr/.antlr/VSLLexer.java @@ -133,7 +133,7 @@ public class VSLLexer extends Lexer { private void TEXT_action(RuleContext _localctx, int actionIndex) { switch (actionIndex) { case 0: - getText().substring(1,getText().length()-1) + getText().substring(1,getText().length()-1); break; } } diff --git a/src/main/antlr/.antlr/VSLParser.interp b/src/main/antlr/.antlr/VSLParser.interp index 98b230c..e6244f6 100644 --- a/src/main/antlr/.antlr/VSLParser.interp +++ b/src/main/antlr/.antlr/VSLParser.interp @@ -74,4 +74,4 @@ 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 +[4, 1, 28, 76, 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, 4, 1, 5, 1, 5, 1, 5, 1, 5, 1, 5, 1, 5, 5, 5, 48, 8, 5, 10, 5, 12, 5, 51, 9, 5, 1, 6, 1, 6, 1, 6, 1, 6, 1, 6, 1, 6, 5, 6, 59, 8, 6, 10, 6, 12, 6, 62, 9, 6, 1, 7, 1, 7, 1, 7, 1, 8, 1, 8, 1, 8, 1, 9, 1, 9, 1, 9, 1, 9, 3, 9, 74, 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, 68, 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, 41, 1, 0, 0, 0, 12, 52, 1, 0, 0, 0, 14, 63, 1, 0, 0, 0, 16, 66, 1, 0, 0, 0, 18, 73, 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, 40, 6, 4, -1, 0, 40, 9, 1, 0, 0, 0, 41, 42, 3, 12, 6, 0, 42, 49, 6, 5, -1, 0, 43, 44, 7, 0, 0, 0, 44, 45, 3, 12, 6, 0, 45, 46, 6, 5, -1, 0, 46, 48, 1, 0, 0, 0, 47, 43, 1, 0, 0, 0, 48, 51, 1, 0, 0, 0, 49, 47, 1, 0, 0, 0, 49, 50, 1, 0, 0, 0, 50, 11, 1, 0, 0, 0, 51, 49, 1, 0, 0, 0, 52, 53, 3, 14, 7, 0, 53, 60, 6, 6, -1, 0, 54, 55, 7, 1, 0, 0, 55, 56, 3, 14, 7, 0, 56, 57, 6, 6, -1, 0, 57, 59, 1, 0, 0, 0, 58, 54, 1, 0, 0, 0, 59, 62, 1, 0, 0, 0, 60, 58, 1, 0, 0, 0, 60, 61, 1, 0, 0, 0, 61, 13, 1, 0, 0, 0, 62, 60, 1, 0, 0, 0, 63, 64, 5, 20, 0, 0, 64, 65, 6, 7, -1, 0, 65, 15, 1, 0, 0, 0, 66, 67, 5, 19, 0, 0, 67, 68, 6, 8, -1, 0, 68, 17, 1, 0, 0, 0, 69, 70, 5, 3, 0, 0, 70, 74, 6, 9, -1, 0, 71, 72, 5, 4, 0, 0, 72, 74, 6, 9, -1, 0, 73, 69, 1, 0, 0, 0, 73, 71, 1, 0, 0, 0, 74, 19, 1, 0, 0, 0, 3, 49, 60, 73] \ No newline at end of file diff --git a/src/main/antlr/.antlr/VSLParser.java b/src/main/antlr/.antlr/VSLParser.java index 297a228..7dc7eab 100644 --- a/src/main/antlr/.antlr/VSLParser.java +++ b/src/main/antlr/.antlr/VSLParser.java @@ -1,9 +1,9 @@ // 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 TP2.asd.Program.*; + import TP2.asd.*; + import TP2.asd.Interface.*; import org.antlr.v4.runtime.atn.*; import org.antlr.v4.runtime.dfa.DFA; @@ -108,7 +108,7 @@ public class VSLParser extends Parser { @SuppressWarnings("CheckReturnValue") public static class ProgramContext extends ParserRuleContext { - public Program.Program p; + public Program p; public FunctionContext func; public TerminalNode EOF() { return getToken(VSLParser.EOF, 0); } public FunctionContext function() { @@ -130,7 +130,7 @@ public class VSLParser extends Parser { ((ProgramContext)_localctx).func = function(); setState(21); match(EOF); - ((ProgramContext)_localctx).p = new Program.Program(((ProgramContext)_localctx).func.out); + ((ProgramContext)_localctx).p = new Program(((ProgramContext)_localctx).func.out); } } catch (RecognitionException re) { @@ -146,7 +146,7 @@ public class VSLParser extends Parser { @SuppressWarnings("CheckReturnValue") public static class FunctionContext extends ParserRuleContext { - public List out; + public List out; public TypeContext t; public IdentContext i; public List_instrContext instrs; @@ -172,7 +172,7 @@ public class VSLParser extends Parser { FunctionContext _localctx = new FunctionContext(_ctx, getState()); enterRule(_localctx, 2, RULE_function); - SymTable.SymTable sym_table = new SymTable.SymTable(); + SymTable sym_table = new SymTable(); try { enterOuterAlt(_localctx, 1); @@ -190,7 +190,7 @@ public class VSLParser extends Parser { 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)); + _localctx.out.add(new Function(((FunctionContext)_localctx).t.return_type, ((FunctionContext)_localctx).i.out, ((FunctionContext)_localctx).instrs.out)); } } @@ -207,7 +207,7 @@ public class VSLParser extends Parser { @SuppressWarnings("CheckReturnValue") public static class PrototypeContext extends ParserRuleContext { - public Program.Program p; + public Program p; public PrototypeContext(ParserRuleContext parent, int invokingState) { super(parent, invokingState); } @@ -235,25 +235,25 @@ public class VSLParser extends Parser { @SuppressWarnings("CheckReturnValue") public static class List_instrContext extends ParserRuleContext { - public SymTable.SymTable table; - public List out; + public SymTable table; + public ArrayList 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) { + public List_instrContext(ParserRuleContext parent, int invokingState, 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 { + public final List_instrContext list_instr(SymTable table) throws RecognitionException { List_instrContext _localctx = new List_instrContext(_ctx, getState(), table); enterRule(_localctx, 6, RULE_list_instr); - List instructions = new List(); + ((List_instrContext)_localctx).out = new ArrayList<>(); try { enterOuterAlt(_localctx, 1); @@ -261,8 +261,7 @@ public class VSLParser extends Parser { setState(34); ((List_instrContext)_localctx).instruction = instruction(table); - instructions.add(((List_instrContext)_localctx).instruction.out); - ((List_instrContext)_localctx).out = instructions; + _localctx.out.addAll(((List_instrContext)_localctx).instruction.out); } } @@ -279,21 +278,22 @@ public class VSLParser extends Parser { @SuppressWarnings("CheckReturnValue") public static class InstructionContext extends ParserRuleContext { - public SymTable.SymTable table; - public List out; + public SymTable table; + public ArrayList out; + public ExpressionContext e; 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) { + public InstructionContext(ParserRuleContext parent, int invokingState, SymTable table) { super(parent, invokingState); this.table = table; } @Override public int getRuleIndex() { return RULE_instruction; } } - public final InstructionContext instruction(SymTable.SymTable table) throws RecognitionException { + public final InstructionContext instruction(SymTable table) throws RecognitionException { InstructionContext _localctx = new InstructionContext(_ctx, getState(), table); enterRule(_localctx, 8, RULE_instruction); try { @@ -302,7 +302,9 @@ public class VSLParser extends Parser { setState(37); match(RETURN); setState(38); - expression(SymTable table); + ((InstructionContext)_localctx).e = expression(table); + ((InstructionContext)_localctx).out = new ArrayList<>(); + _localctx.out.add(new Return_instr(((InstructionContext)_localctx).e.out)); } } catch (RecognitionException re) { @@ -318,7 +320,7 @@ public class VSLParser extends Parser { @SuppressWarnings("CheckReturnValue") public static class ExpressionContext extends ParserRuleContext { - public SymTable.SymTable table; + public SymTable table; public Type return_Type; public Expression out; public Td_expressionContext left; @@ -339,33 +341,33 @@ public class VSLParser extends Parser { return getToken(VSLParser.MINUS, i); } public ExpressionContext(ParserRuleContext parent, int invokingState) { super(parent, invokingState); } - public ExpressionContext(ParserRuleContext parent, int invokingState, SymTable.SymTable table) { + public ExpressionContext(ParserRuleContext parent, int invokingState, SymTable table) { super(parent, invokingState); this.table = table; } @Override public int getRuleIndex() { return RULE_expression; } } - public final ExpressionContext expression(SymTable.SymTable table) throws RecognitionException { + public final ExpressionContext expression(SymTable table) throws RecognitionException { ExpressionContext _localctx = new ExpressionContext(_ctx, getState(), table); enterRule(_localctx, 10, RULE_expression); int _la; try { enterOuterAlt(_localctx, 1); { - setState(40); + setState(41); ((ExpressionContext)_localctx).left = td_expression(table); ((ExpressionContext)_localctx).out = ((ExpressionContext)_localctx).left.out; ((ExpressionContext)_localctx).return_Type = ((ExpressionContext)_localctx).left.return_Type; - setState(48); + setState(49); _errHandler.sync(this); _la = _input.LA(1); while (_la==PLUS || _la==MINUS) { { { - setState(42); + setState(43); ((ExpressionContext)_localctx).op = _input.LT(1); _la = _input.LA(1); if ( !(_la==PLUS || _la==MINUS) ) { @@ -376,13 +378,13 @@ public class VSLParser extends Parser { _errHandler.reportMatch(this); consume(); } - setState(43); + setState(44); ((ExpressionContext)_localctx).right = td_expression(table); - ((ExpressionContext)_localctx).out = new Program.BinopExpression( + ((ExpressionContext)_localctx).out = new BinopExpression( switch(((ExpressionContext)_localctx).op.getType()) { - case PLUS -> Op.PLUS; - case MINUS -> Op.MINUS; + case PLUS -> PLUS; + case MINUS -> MINUS; default -> throw new IllegalArgumentException("Unknown operator"); }, ((ExpressionContext)_localctx).left.out, @@ -392,7 +394,7 @@ public class VSLParser extends Parser { } } - setState(50); + setState(51); _errHandler.sync(this); _la = _input.LA(1); } @@ -411,7 +413,7 @@ public class VSLParser extends Parser { @SuppressWarnings("CheckReturnValue") public static class Td_expressionContext extends ParserRuleContext { - public SymTable.SymTable table; + public SymTable table; public Type return_Type; public Expression out; public LitContext left; @@ -432,33 +434,33 @@ public class VSLParser extends Parser { 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) { + public Td_expressionContext(ParserRuleContext parent, int invokingState, 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 { + public final Td_expressionContext td_expression(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); + setState(52); ((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); + setState(60); _errHandler.sync(this); _la = _input.LA(1); while (_la==DIV || _la==TIMES) { { { - setState(53); + setState(54); ((Td_expressionContext)_localctx).op = _input.LT(1); _la = _input.LA(1); if ( !(_la==DIV || _la==TIMES) ) { @@ -469,13 +471,13 @@ public class VSLParser extends Parser { _errHandler.reportMatch(this); consume(); } - setState(54); + setState(55); ((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; + case TIMES -> TIMES; + case DIV -> DIV; default -> throw new IllegalArgumentException("Unknown operator"); }, ((Td_expressionContext)_localctx).left.out, @@ -485,7 +487,7 @@ public class VSLParser extends Parser { } } - setState(61); + setState(62); _errHandler.sync(this); _la = _input.LA(1); } @@ -520,11 +522,11 @@ public class VSLParser extends Parser { try { enterOuterAlt(_localctx, 1); { - setState(62); + setState(63); ((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)); + ((LitContext)_localctx).return_Type = new Type_int(); + ((LitContext)_localctx).out = new Const((((LitContext)_localctx).NUMBER!=null?Integer.valueOf(((LitContext)_localctx).NUMBER.getText()):0)); } } @@ -556,7 +558,7 @@ public class VSLParser extends Parser { try { enterOuterAlt(_localctx, 1); { - setState(65); + setState(66); ((IdentContext)_localctx).TEXT = match(TEXT); ((IdentContext)_localctx).out = ((IdentContext)_localctx).TEXT.getText(); } @@ -587,23 +589,23 @@ public class VSLParser extends Parser { TypeContext _localctx = new TypeContext(_ctx, getState()); enterRule(_localctx, 18, RULE_type); try { - setState(72); + setState(73); _errHandler.sync(this); switch (_input.LA(1)) { case TYPE_INT: enterOuterAlt(_localctx, 1); { - setState(68); + setState(69); match(TYPE_INT); - ((TypeContext)_localctx).return_type = new Program.Type.Type_int(); + ((TypeContext)_localctx).return_type = new Type_int(); } break; case TYPE_VOID: enterOuterAlt(_localctx, 2); { - setState(70); + setState(71); match(TYPE_VOID); - ((TypeContext)_localctx).return_type = new Program.Type.Type_void(); + ((TypeContext)_localctx).return_type = new Type_void(); } break; default: @@ -622,47 +624,48 @@ public class VSLParser extends Parser { } public static final String _serializedATN = - "\u0004\u0001\u001cK\u0002\u0000\u0007\u0000\u0002\u0001\u0007\u0001\u0002"+ + "\u0004\u0001\u001cL\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"; + "\u0001\u0003\u0001\u0004\u0001\u0004\u0001\u0004\u0001\u0004\u0001\u0005"+ + "\u0001\u0005\u0001\u0005\u0001\u0005\u0001\u0005\u0001\u0005\u0005\u0005"+ + "0\b\u0005\n\u0005\f\u00053\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\tJ\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\u001aD\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\f4\u0001\u0000\u0000\u0000\u000e?\u0001\u0000\u0000\u0000\u0010"+ + "B\u0001\u0000\u0000\u0000\u0012I\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\'(\u0006\u0004\uffff\uffff\u0000(\t\u0001"+ + "\u0000\u0000\u0000)*\u0003\f\u0006\u0000*1\u0006\u0005\uffff\uffff\u0000"+ + "+,\u0007\u0000\u0000\u0000,-\u0003\f\u0006\u0000-.\u0006\u0005\uffff\uffff"+ + "\u0000.0\u0001\u0000\u0000\u0000/+\u0001\u0000\u0000\u000003\u0001\u0000"+ + "\u0000\u00001/\u0001\u0000\u0000\u000012\u0001\u0000\u0000\u00002\u000b"+ + "\u0001\u0000\u0000\u000031\u0001\u0000\u0000\u000045\u0003\u000e\u0007"+ + "\u00005<\u0006\u0006\uffff\uffff\u000067\u0007\u0001\u0000\u000078\u0003"+ + "\u000e\u0007\u000089\u0006\u0006\uffff\uffff\u00009;\u0001\u0000\u0000"+ + "\u0000:6\u0001\u0000\u0000\u0000;>\u0001\u0000\u0000\u0000<:\u0001\u0000"+ + "\u0000\u0000<=\u0001\u0000\u0000\u0000=\r\u0001\u0000\u0000\u0000><\u0001"+ + "\u0000\u0000\u0000?@\u0005\u0014\u0000\u0000@A\u0006\u0007\uffff\uffff"+ + "\u0000A\u000f\u0001\u0000\u0000\u0000BC\u0005\u0013\u0000\u0000CD\u0006"+ + "\b\uffff\uffff\u0000D\u0011\u0001\u0000\u0000\u0000EF\u0005\u0003\u0000"+ + "\u0000FJ\u0006\t\uffff\uffff\u0000GH\u0005\u0004\u0000\u0000HJ\u0006\t"+ + "\uffff\uffff\u0000IE\u0001\u0000\u0000\u0000IG\u0001\u0000\u0000\u0000"+ + "J\u0013\u0001\u0000\u0000\u0000\u00031,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 4f85f53..8812369 100644 --- a/src/main/java/TP2/asd/Interface.java +++ b/src/main/java/TP2/asd/Interface.java @@ -2,37 +2,39 @@ package TP2.asd; import java.util.Map; -interface Expression { - public S accept(ExprVisitor v, H h); -} +public interface Interface{ + public interface Expression { + public S accept(ExprVisitor v, H h); + } -interface Instruction { - public S accept(InstrVisitor v, H h); -} + public interface Instruction { + public S accept(InstrVisitor v, H h); + } -interface ProgramVisitor { - public S visitProgram(Program p, H h); - public S visitFunction(Function f, H h); -} + public interface ProgramVisitor { + public S visitProgram(Program p, H h); + public S visitFunction(Program.Function f, H h); + } -interface InstrVisitor{ - public S visitReturn(Return_instr e, H h); -} + public interface InstrVisitor{ + public S visitReturn(Program.Return_instr e, H h); + } -interface ExprVisitor { - public S visitConst(Const e,H h); - public S visitBinOp(BinopExpression e, H h); -} + public interface ExprVisitor { + public S visitConst(Program.Const e,H h); + public S visitBinOp(Program.BinopExpression e, H h); + } -interface Type{ - public String prettyprinter(); -} + public interface Type{ + public String prettyprinter(); + } -enum Op {PLUS, MINUS, TIMES,DIV} + public enum Op {PLUS, MINUS, TIMES,DIV} -//Eval -interface ExprEval extends ExprVisitor, Integer> { -} + //Eval + public interface ExprEval extends ExprVisitor, Integer> { + } -interface TypeCheck extends ExprVisitor, Type> { + public 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 409203e..a70791b 100644 --- a/src/main/java/TP2/asd/Program.java +++ b/src/main/java/TP2/asd/Program.java @@ -1,49 +1,48 @@ package TP2.asd; import java.util.ArrayList; +import TP2.asd.Interface.*; - -public record Program(ArrayList functions) { - - -} -record Function(Type type, String nom, ArrayList instructions){ - public Function(Type type, String name, Instruction instruction) { - this(type, name, new ArrayList<>() {{ add(instruction); }}); +public class Program{ + public static record Function(Type type, String nom, ArrayList instructions){ + public Function(Type type, String name, Instruction instruction) { + this(type, name, new ArrayList<>() {{ add(instruction); }}); + } + + public S accept(ProgramVisitor v, H h) { + return v.visitFunction(this, h); + } } - - public S accept(ProgramVisitor v, H h) { - return v.visitFunction(this, h); + + public static record Const(int c) implements Expression{ + public S accept(ExprVisitor v, H h) { + return v.visitConst(this, h); + } + } + + public static record BinopExpression(Op op,Expression e1, Expression e2) implements Expression{ + public S accept(ExprVisitor v, H h) { + return v.visitBinOp(this, h); + } + + } + + public static record Return_instr(Expression e) implements Instruction{ + public S accept(InstrVisitor v, H h) { + return v.visitReturn(this,h); + } + } + + public static record Type_void() implements Type{ + public String prettyprinter() { + return "VOID"; + } + } + + public static record Type_int() implements Type{ + public String prettyprinter() { + return "INT"; + } } } -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); - } -} - -record Type_void() implements Type{ - public String prettyprinter() { - return "VOID"; - } -} - -record Type_int() implements Type{ - public String prettyprinter() { - return "INT"; - } -}