This commit is contained in:
Vu Tuan Minh
2025-04-01 17:52:45 +02:00
parent 2a901e4a37
commit 3f9072c7bd
7 changed files with 161 additions and 178 deletions

View File

@@ -133,7 +133,7 @@ public class VSLLexer extends Lexer {
private void TEXT_action(RuleContext _localctx, int actionIndex) { private void TEXT_action(RuleContext _localctx, int actionIndex) {
switch (actionIndex) { switch (actionIndex) {
case 0: case 0:
getText().substring(1,getText().length()-1) getText().substring(1,getText().length()-1);
break; break;
} }
} }

View File

@@ -74,4 +74,4 @@ type
atn: 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] [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]

View File

@@ -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 // Generated from /home/tuanvu/Desktop/M1/S8/PDS/tp2-vsl-pds/src/main/antlr/VSLParser.g by ANTLR 4.13.1
package TP2; package TP2;
import TP2.asd.Program.*;
import TP2.Program.Program; import TP2.asd.*;
import TP2.SymTable.SymTable; import TP2.asd.Interface.*;
import org.antlr.v4.runtime.atn.*; import org.antlr.v4.runtime.atn.*;
import org.antlr.v4.runtime.dfa.DFA; import org.antlr.v4.runtime.dfa.DFA;
@@ -108,7 +108,7 @@ public class VSLParser extends Parser {
@SuppressWarnings("CheckReturnValue") @SuppressWarnings("CheckReturnValue")
public static class ProgramContext extends ParserRuleContext { public static class ProgramContext extends ParserRuleContext {
public Program.Program p; public Program p;
public FunctionContext func; public FunctionContext func;
public TerminalNode EOF() { return getToken(VSLParser.EOF, 0); } public TerminalNode EOF() { return getToken(VSLParser.EOF, 0); }
public FunctionContext function() { public FunctionContext function() {
@@ -130,7 +130,7 @@ public class VSLParser extends Parser {
((ProgramContext)_localctx).func = function(); ((ProgramContext)_localctx).func = function();
setState(21); setState(21);
match(EOF); match(EOF);
((ProgramContext)_localctx).p = new Program.Program(((ProgramContext)_localctx).func.out); ((ProgramContext)_localctx).p = new Program(((ProgramContext)_localctx).func.out);
} }
} }
catch (RecognitionException re) { catch (RecognitionException re) {
@@ -146,7 +146,7 @@ public class VSLParser extends Parser {
@SuppressWarnings("CheckReturnValue") @SuppressWarnings("CheckReturnValue")
public static class FunctionContext extends ParserRuleContext { public static class FunctionContext extends ParserRuleContext {
public List<Program.Function> out; public List<Function> out;
public TypeContext t; public TypeContext t;
public IdentContext i; public IdentContext i;
public List_instrContext instrs; public List_instrContext instrs;
@@ -172,7 +172,7 @@ public class VSLParser extends Parser {
FunctionContext _localctx = new FunctionContext(_ctx, getState()); FunctionContext _localctx = new FunctionContext(_ctx, getState());
enterRule(_localctx, 2, RULE_function); enterRule(_localctx, 2, RULE_function);
SymTable.SymTable sym_table = new SymTable.SymTable(); SymTable sym_table = new SymTable();
try { try {
enterOuterAlt(_localctx, 1); enterOuterAlt(_localctx, 1);
@@ -190,7 +190,7 @@ public class VSLParser extends Parser {
setState(29); setState(29);
((FunctionContext)_localctx).instrs = list_instr(sym_table); ((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") @SuppressWarnings("CheckReturnValue")
public static class PrototypeContext extends ParserRuleContext { public static class PrototypeContext extends ParserRuleContext {
public Program.Program p; public Program p;
public PrototypeContext(ParserRuleContext parent, int invokingState) { public PrototypeContext(ParserRuleContext parent, int invokingState) {
super(parent, invokingState); super(parent, invokingState);
} }
@@ -235,25 +235,25 @@ public class VSLParser extends Parser {
@SuppressWarnings("CheckReturnValue") @SuppressWarnings("CheckReturnValue")
public static class List_instrContext extends ParserRuleContext { public static class List_instrContext extends ParserRuleContext {
public SymTable.SymTable table; public SymTable table;
public List<Program.Instruction> out; public ArrayList<Instruction> out;
public InstructionContext instruction; public InstructionContext instruction;
public InstructionContext instruction() { public InstructionContext instruction() {
return getRuleContext(InstructionContext.class,0); return getRuleContext(InstructionContext.class,0);
} }
public List_instrContext(ParserRuleContext parent, int invokingState) { super(parent, invokingState); } 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); super(parent, invokingState);
this.table = table; this.table = table;
} }
@Override public int getRuleIndex() { return RULE_list_instr; } @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); List_instrContext _localctx = new List_instrContext(_ctx, getState(), table);
enterRule(_localctx, 6, RULE_list_instr); enterRule(_localctx, 6, RULE_list_instr);
List<Program.Instruction> instructions = new List<Program.Instruction>(); ((List_instrContext)_localctx).out = new ArrayList<>();
try { try {
enterOuterAlt(_localctx, 1); enterOuterAlt(_localctx, 1);
@@ -261,8 +261,7 @@ public class VSLParser extends Parser {
setState(34); setState(34);
((List_instrContext)_localctx).instruction = instruction(table); ((List_instrContext)_localctx).instruction = instruction(table);
instructions.add(((List_instrContext)_localctx).instruction.out); _localctx.out.addAll(((List_instrContext)_localctx).instruction.out);
((List_instrContext)_localctx).out = instructions;
} }
} }
@@ -279,21 +278,22 @@ public class VSLParser extends Parser {
@SuppressWarnings("CheckReturnValue") @SuppressWarnings("CheckReturnValue")
public static class InstructionContext extends ParserRuleContext { public static class InstructionContext extends ParserRuleContext {
public SymTable.SymTable table; public SymTable table;
public List<Program.Instruction> out; public ArrayList<Instruction> out;
public ExpressionContext e;
public TerminalNode RETURN() { return getToken(VSLParser.RETURN, 0); } public TerminalNode RETURN() { return getToken(VSLParser.RETURN, 0); }
public ExpressionContext expression() { public ExpressionContext expression() {
return getRuleContext(ExpressionContext.class,0); return getRuleContext(ExpressionContext.class,0);
} }
public InstructionContext(ParserRuleContext parent, int invokingState) { super(parent, invokingState); } 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); super(parent, invokingState);
this.table = table; this.table = table;
} }
@Override public int getRuleIndex() { return RULE_instruction; } @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); InstructionContext _localctx = new InstructionContext(_ctx, getState(), table);
enterRule(_localctx, 8, RULE_instruction); enterRule(_localctx, 8, RULE_instruction);
try { try {
@@ -302,7 +302,9 @@ public class VSLParser extends Parser {
setState(37); setState(37);
match(RETURN); match(RETURN);
setState(38); 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) { catch (RecognitionException re) {
@@ -318,7 +320,7 @@ public class VSLParser extends Parser {
@SuppressWarnings("CheckReturnValue") @SuppressWarnings("CheckReturnValue")
public static class ExpressionContext extends ParserRuleContext { public static class ExpressionContext extends ParserRuleContext {
public SymTable.SymTable table; public SymTable table;
public Type return_Type; public Type return_Type;
public Expression out; public Expression out;
public Td_expressionContext left; public Td_expressionContext left;
@@ -339,33 +341,33 @@ public class VSLParser extends Parser {
return getToken(VSLParser.MINUS, i); return getToken(VSLParser.MINUS, i);
} }
public ExpressionContext(ParserRuleContext parent, int invokingState) { super(parent, invokingState); } 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); super(parent, invokingState);
this.table = table; this.table = table;
} }
@Override public int getRuleIndex() { return RULE_expression; } @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); ExpressionContext _localctx = new ExpressionContext(_ctx, getState(), table);
enterRule(_localctx, 10, RULE_expression); enterRule(_localctx, 10, RULE_expression);
int _la; int _la;
try { try {
enterOuterAlt(_localctx, 1); enterOuterAlt(_localctx, 1);
{ {
setState(40); setState(41);
((ExpressionContext)_localctx).left = td_expression(table); ((ExpressionContext)_localctx).left = td_expression(table);
((ExpressionContext)_localctx).out = ((ExpressionContext)_localctx).left.out; ((ExpressionContext)_localctx).out = ((ExpressionContext)_localctx).left.out;
((ExpressionContext)_localctx).return_Type = ((ExpressionContext)_localctx).left.return_Type; ((ExpressionContext)_localctx).return_Type = ((ExpressionContext)_localctx).left.return_Type;
setState(48); setState(49);
_errHandler.sync(this); _errHandler.sync(this);
_la = _input.LA(1); _la = _input.LA(1);
while (_la==PLUS || _la==MINUS) { while (_la==PLUS || _la==MINUS) {
{ {
{ {
setState(42); setState(43);
((ExpressionContext)_localctx).op = _input.LT(1); ((ExpressionContext)_localctx).op = _input.LT(1);
_la = _input.LA(1); _la = _input.LA(1);
if ( !(_la==PLUS || _la==MINUS) ) { if ( !(_la==PLUS || _la==MINUS) ) {
@@ -376,13 +378,13 @@ public class VSLParser extends Parser {
_errHandler.reportMatch(this); _errHandler.reportMatch(this);
consume(); consume();
} }
setState(43); setState(44);
((ExpressionContext)_localctx).right = td_expression(table); ((ExpressionContext)_localctx).right = td_expression(table);
((ExpressionContext)_localctx).out = new Program.BinopExpression( ((ExpressionContext)_localctx).out = new BinopExpression(
switch(((ExpressionContext)_localctx).op.getType()) { switch(((ExpressionContext)_localctx).op.getType()) {
case PLUS -> Op.PLUS; case PLUS -> PLUS;
case MINUS -> Op.MINUS; case MINUS -> MINUS;
default -> throw new IllegalArgumentException("Unknown operator"); default -> throw new IllegalArgumentException("Unknown operator");
}, },
((ExpressionContext)_localctx).left.out, ((ExpressionContext)_localctx).left.out,
@@ -392,7 +394,7 @@ public class VSLParser extends Parser {
} }
} }
setState(50); setState(51);
_errHandler.sync(this); _errHandler.sync(this);
_la = _input.LA(1); _la = _input.LA(1);
} }
@@ -411,7 +413,7 @@ public class VSLParser extends Parser {
@SuppressWarnings("CheckReturnValue") @SuppressWarnings("CheckReturnValue")
public static class Td_expressionContext extends ParserRuleContext { public static class Td_expressionContext extends ParserRuleContext {
public SymTable.SymTable table; public SymTable table;
public Type return_Type; public Type return_Type;
public Expression out; public Expression out;
public LitContext left; public LitContext left;
@@ -432,33 +434,33 @@ public class VSLParser extends Parser {
return getToken(VSLParser.DIV, i); return getToken(VSLParser.DIV, i);
} }
public Td_expressionContext(ParserRuleContext parent, int invokingState) { super(parent, invokingState); } 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); super(parent, invokingState);
this.table = table; this.table = table;
} }
@Override public int getRuleIndex() { return RULE_td_expression; } @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); Td_expressionContext _localctx = new Td_expressionContext(_ctx, getState(), table);
enterRule(_localctx, 12, RULE_td_expression); enterRule(_localctx, 12, RULE_td_expression);
int _la; int _la;
try { try {
enterOuterAlt(_localctx, 1); enterOuterAlt(_localctx, 1);
{ {
setState(51); setState(52);
((Td_expressionContext)_localctx).left = lit(); ((Td_expressionContext)_localctx).left = lit();
((Td_expressionContext)_localctx).out = ((Td_expressionContext)_localctx).left.out; ((Td_expressionContext)_localctx).out = ((Td_expressionContext)_localctx).left.out;
((Td_expressionContext)_localctx).return_Type = ((Td_expressionContext)_localctx).left.return_Type; ((Td_expressionContext)_localctx).return_Type = ((Td_expressionContext)_localctx).left.return_Type;
setState(59); setState(60);
_errHandler.sync(this); _errHandler.sync(this);
_la = _input.LA(1); _la = _input.LA(1);
while (_la==DIV || _la==TIMES) { while (_la==DIV || _la==TIMES) {
{ {
{ {
setState(53); setState(54);
((Td_expressionContext)_localctx).op = _input.LT(1); ((Td_expressionContext)_localctx).op = _input.LT(1);
_la = _input.LA(1); _la = _input.LA(1);
if ( !(_la==DIV || _la==TIMES) ) { if ( !(_la==DIV || _la==TIMES) ) {
@@ -469,13 +471,13 @@ public class VSLParser extends Parser {
_errHandler.reportMatch(this); _errHandler.reportMatch(this);
consume(); consume();
} }
setState(54); setState(55);
((Td_expressionContext)_localctx).right = lit(); ((Td_expressionContext)_localctx).right = lit();
((Td_expressionContext)_localctx).out = new Program.BinopExpression( ((Td_expressionContext)_localctx).out = new Program.BinopExpression(
switch(((Td_expressionContext)_localctx).op.getType()) { switch(((Td_expressionContext)_localctx).op.getType()) {
case TIMES -> Op.TIMES; case TIMES -> TIMES;
case DIV -> Op.DIV; case DIV -> DIV;
default -> throw new IllegalArgumentException("Unknown operator"); default -> throw new IllegalArgumentException("Unknown operator");
}, },
((Td_expressionContext)_localctx).left.out, ((Td_expressionContext)_localctx).left.out,
@@ -485,7 +487,7 @@ public class VSLParser extends Parser {
} }
} }
setState(61); setState(62);
_errHandler.sync(this); _errHandler.sync(this);
_la = _input.LA(1); _la = _input.LA(1);
} }
@@ -520,11 +522,11 @@ public class VSLParser extends Parser {
try { try {
enterOuterAlt(_localctx, 1); enterOuterAlt(_localctx, 1);
{ {
setState(62); setState(63);
((LitContext)_localctx).NUMBER = match(NUMBER); ((LitContext)_localctx).NUMBER = match(NUMBER);
((LitContext)_localctx).return_Type = new Program.Type_int(); ((LitContext)_localctx).return_Type = new Type_int();
((LitContext)_localctx).out = new Program.Const((((LitContext)_localctx).NUMBER!=null?Integer.valueOf(((LitContext)_localctx).NUMBER.getText()):0)); ((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 { try {
enterOuterAlt(_localctx, 1); enterOuterAlt(_localctx, 1);
{ {
setState(65); setState(66);
((IdentContext)_localctx).TEXT = match(TEXT); ((IdentContext)_localctx).TEXT = match(TEXT);
((IdentContext)_localctx).out = ((IdentContext)_localctx).TEXT.getText(); ((IdentContext)_localctx).out = ((IdentContext)_localctx).TEXT.getText();
} }
@@ -587,23 +589,23 @@ public class VSLParser extends Parser {
TypeContext _localctx = new TypeContext(_ctx, getState()); TypeContext _localctx = new TypeContext(_ctx, getState());
enterRule(_localctx, 18, RULE_type); enterRule(_localctx, 18, RULE_type);
try { try {
setState(72); setState(73);
_errHandler.sync(this); _errHandler.sync(this);
switch (_input.LA(1)) { switch (_input.LA(1)) {
case TYPE_INT: case TYPE_INT:
enterOuterAlt(_localctx, 1); enterOuterAlt(_localctx, 1);
{ {
setState(68); setState(69);
match(TYPE_INT); match(TYPE_INT);
((TypeContext)_localctx).return_type = new Program.Type.Type_int(); ((TypeContext)_localctx).return_type = new Type_int();
} }
break; break;
case TYPE_VOID: case TYPE_VOID:
enterOuterAlt(_localctx, 2); enterOuterAlt(_localctx, 2);
{ {
setState(70); setState(71);
match(TYPE_VOID); match(TYPE_VOID);
((TypeContext)_localctx).return_type = new Program.Type.Type_void(); ((TypeContext)_localctx).return_type = new Type_void();
} }
break; break;
default: default:
@@ -622,47 +624,48 @@ public class VSLParser extends Parser {
} }
public static final String _serializedATN = 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"+ "\u0002\u0007\u0002\u0002\u0003\u0007\u0003\u0002\u0004\u0007\u0004\u0002"+
"\u0005\u0007\u0005\u0002\u0006\u0007\u0006\u0002\u0007\u0007\u0007\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"+ "\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\u0001\u0001\u0001\u0001\u0001\u0001\u0001"+
"\u0001\u0001\u0001\u0001\u0001\u0002\u0001\u0002\u0001\u0003\u0001\u0003"+ "\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\u0003\u0001\u0004\u0001\u0004\u0001\u0004\u0001\u0004\u0001\u0005"+
"\u0001\u0005\u0001\u0005\u0001\u0005\u0001\u0005\u0005\u0005/\b\u0005"+ "\u0001\u0005\u0001\u0005\u0001\u0005\u0001\u0005\u0001\u0005\u0005\u0005"+
"\n\u0005\f\u00052\t\u0005\u0001\u0006\u0001\u0006\u0001\u0006\u0001\u0006"+ "0\b\u0005\n\u0005\f\u00053\t\u0005\u0001\u0006\u0001\u0006\u0001\u0006"+
"\u0001\u0006\u0001\u0006\u0005\u0006:\b\u0006\n\u0006\f\u0006=\t\u0006"+ "\u0001\u0006\u0001\u0006\u0001\u0006\u0005\u0006;\b\u0006\n\u0006\f\u0006"+
"\u0001\u0007\u0001\u0007\u0001\u0007\u0001\b\u0001\b\u0001\b\u0001\t\u0001"+ ">\t\u0006\u0001\u0007\u0001\u0007\u0001\u0007\u0001\b\u0001\b\u0001\b"+
"\t\u0001\t\u0001\t\u0003\tI\b\t\u0001\t\u0000\u0000\n\u0000\u0002\u0004"+ "\u0001\t\u0001\t\u0001\t\u0001\t\u0003\tJ\b\t\u0001\t\u0000\u0000\n\u0000"+
"\u0006\b\n\f\u000e\u0010\u0012\u0000\u0002\u0001\u0000\u0017\u0018\u0001"+ "\u0002\u0004\u0006\b\n\f\u000e\u0010\u0012\u0000\u0002\u0001\u0000\u0017"+
"\u0000\u0019\u001aC\u0000\u0014\u0001\u0000\u0000\u0000\u0002\u0018\u0001"+ "\u0018\u0001\u0000\u0019\u001aD\u0000\u0014\u0001\u0000\u0000\u0000\u0002"+
"\u0000\u0000\u0000\u0004 \u0001\u0000\u0000\u0000\u0006\"\u0001\u0000"+ "\u0018\u0001\u0000\u0000\u0000\u0004 \u0001\u0000\u0000\u0000\u0006\""+
"\u0000\u0000\b%\u0001\u0000\u0000\u0000\n(\u0001\u0000\u0000\u0000\f3"+ "\u0001\u0000\u0000\u0000\b%\u0001\u0000\u0000\u0000\n)\u0001\u0000\u0000"+
"\u0001\u0000\u0000\u0000\u000e>\u0001\u0000\u0000\u0000\u0010A\u0001\u0000"+ "\u0000\f4\u0001\u0000\u0000\u0000\u000e?\u0001\u0000\u0000\u0000\u0010"+
"\u0000\u0000\u0012H\u0001\u0000\u0000\u0000\u0014\u0015\u0003\u0002\u0001"+ "B\u0001\u0000\u0000\u0000\u0012I\u0001\u0000\u0000\u0000\u0014\u0015\u0003"+
"\u0000\u0015\u0016\u0005\u0000\u0000\u0001\u0016\u0017\u0006\u0000\uffff"+ "\u0002\u0001\u0000\u0015\u0016\u0005\u0000\u0000\u0001\u0016\u0017\u0006"+
"\uffff\u0000\u0017\u0001\u0001\u0000\u0000\u0000\u0018\u0019\u0005\u0001"+ "\u0000\uffff\uffff\u0000\u0017\u0001\u0001\u0000\u0000\u0000\u0018\u0019"+
"\u0000\u0000\u0019\u001a\u0003\u0012\t\u0000\u001a\u001b\u0003\u0010\b"+ "\u0005\u0001\u0000\u0000\u0019\u001a\u0003\u0012\t\u0000\u001a\u001b\u0003"+
"\u0000\u001b\u001c\u0005\u0015\u0000\u0000\u001c\u001d\u0005\u0016\u0000"+ "\u0010\b\u0000\u001b\u001c\u0005\u0015\u0000\u0000\u001c\u001d\u0005\u0016"+
"\u0000\u001d\u001e\u0003\u0006\u0003\u0000\u001e\u001f\u0006\u0001\uffff"+ "\u0000\u0000\u001d\u001e\u0003\u0006\u0003\u0000\u001e\u001f\u0006\u0001"+
"\uffff\u0000\u001f\u0003\u0001\u0000\u0000\u0000 !\u0001\u0000\u0000\u0000"+ "\uffff\uffff\u0000\u001f\u0003\u0001\u0000\u0000\u0000 !\u0001\u0000\u0000"+
"!\u0005\u0001\u0000\u0000\u0000\"#\u0003\b\u0004\u0000#$\u0006\u0003\uffff"+ "\u0000!\u0005\u0001\u0000\u0000\u0000\"#\u0003\b\u0004\u0000#$\u0006\u0003"+
"\uffff\u0000$\u0007\u0001\u0000\u0000\u0000%&\u0005\u0012\u0000\u0000"+ "\uffff\uffff\u0000$\u0007\u0001\u0000\u0000\u0000%&\u0005\u0012\u0000"+
"&\'\u0003\n\u0005\u0000\'\t\u0001\u0000\u0000\u0000()\u0003\f\u0006\u0000"+ "\u0000&\'\u0003\n\u0005\u0000\'(\u0006\u0004\uffff\uffff\u0000(\t\u0001"+
")0\u0006\u0005\uffff\uffff\u0000*+\u0007\u0000\u0000\u0000+,\u0003\f\u0006"+ "\u0000\u0000\u0000)*\u0003\f\u0006\u0000*1\u0006\u0005\uffff\uffff\u0000"+
"\u0000,-\u0006\u0005\uffff\uffff\u0000-/\u0001\u0000\u0000\u0000.*\u0001"+ "+,\u0007\u0000\u0000\u0000,-\u0003\f\u0006\u0000-.\u0006\u0005\uffff\uffff"+
"\u0000\u0000\u0000/2\u0001\u0000\u0000\u00000.\u0001\u0000\u0000\u0000"+ "\u0000.0\u0001\u0000\u0000\u0000/+\u0001\u0000\u0000\u000003\u0001\u0000"+
"01\u0001\u0000\u0000\u00001\u000b\u0001\u0000\u0000\u000020\u0001\u0000"+ "\u0000\u00001/\u0001\u0000\u0000\u000012\u0001\u0000\u0000\u00002\u000b"+
"\u0000\u000034\u0003\u000e\u0007\u00004;\u0006\u0006\uffff\uffff\u0000"+ "\u0001\u0000\u0000\u000031\u0001\u0000\u0000\u000045\u0003\u000e\u0007"+
"56\u0007\u0001\u0000\u000067\u0003\u000e\u0007\u000078\u0006\u0006\uffff"+ "\u00005<\u0006\u0006\uffff\uffff\u000067\u0007\u0001\u0000\u000078\u0003"+
"\uffff\u00008:\u0001\u0000\u0000\u000095\u0001\u0000\u0000\u0000:=\u0001"+ "\u000e\u0007\u000089\u0006\u0006\uffff\uffff\u00009;\u0001\u0000\u0000"+
"\u0000\u0000\u0000;9\u0001\u0000\u0000\u0000;<\u0001\u0000\u0000\u0000"+ "\u0000:6\u0001\u0000\u0000\u0000;>\u0001\u0000\u0000\u0000<:\u0001\u0000"+
"<\r\u0001\u0000\u0000\u0000=;\u0001\u0000\u0000\u0000>?\u0005\u0014\u0000"+ "\u0000\u0000<=\u0001\u0000\u0000\u0000=\r\u0001\u0000\u0000\u0000><\u0001"+
"\u0000?@\u0006\u0007\uffff\uffff\u0000@\u000f\u0001\u0000\u0000\u0000"+ "\u0000\u0000\u0000?@\u0005\u0014\u0000\u0000@A\u0006\u0007\uffff\uffff"+
"AB\u0005\u0013\u0000\u0000BC\u0006\b\uffff\uffff\u0000C\u0011\u0001\u0000"+ "\u0000A\u000f\u0001\u0000\u0000\u0000BC\u0005\u0013\u0000\u0000CD\u0006"+
"\u0000\u0000DE\u0005\u0003\u0000\u0000EI\u0006\t\uffff\uffff\u0000FG\u0005"+ "\b\uffff\uffff\u0000D\u0011\u0001\u0000\u0000\u0000EF\u0005\u0003\u0000"+
"\u0004\u0000\u0000GI\u0006\t\uffff\uffff\u0000HD\u0001\u0000\u0000\u0000"+ "\u0000FJ\u0006\t\uffff\uffff\u0000GH\u0005\u0004\u0000\u0000HJ\u0006\t"+
"HF\u0001\u0000\u0000\u0000I\u0013\u0001\u0000\u0000\u0000\u00030;H"; "\uffff\uffff\u0000IE\u0001\u0000\u0000\u0000IG\u0001\u0000\u0000\u0000"+
"J\u0013\u0001\u0000\u0000\u0000\u00031<I";
public static final ATN _ATN = public static final ATN _ATN =
new ATNDeserializer().deserialize(_serializedATN.toCharArray()); new ATNDeserializer().deserialize(_serializedATN.toCharArray());
static { static {

View File

@@ -8,7 +8,9 @@ options {
@header { @header {
package TP2; package TP2;
import TP2.asd.Program.*;
import TP2.asd.*; import TP2.asd.*;
import TP2.asd.Interface.*;
} }
// On syntax error, raise exception rather than silently recovery // On syntax error, raise exception rather than silently recovery

View File

@@ -1,23 +0,0 @@
package TP2.asd;
import java.util.Map;
public class ExprEval implements ExprVisitor<Map<String, Type>,Integer> {
@Override
public Integer visitConst(Const c, Map<String, Type> h) {
return c.c();
}
@Override
public Integer visitBinOp(BinopExpression e, Map<String, Type> 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();
}
}
}

View File

@@ -2,37 +2,39 @@ package TP2.asd;
import java.util.Map; import java.util.Map;
interface Expression { public interface Interface{
public interface Expression {
public <H,S> S accept(ExprVisitor<H,S> v, H h); public <H,S> S accept(ExprVisitor<H,S> v, H h);
} }
interface Instruction { public interface Instruction {
public <H,S> S accept(InstrVisitor<H,S> v, H h); public <H,S> S accept(InstrVisitor<H,S> v, H h);
} }
interface ProgramVisitor<H,S> { public interface ProgramVisitor<H,S> {
public S visitProgram(Program p, H h); public S visitProgram(Program p, H h);
public S visitFunction(Function f, H h); public S visitFunction(Program.Function f, H h);
} }
interface InstrVisitor<H,S>{ public interface InstrVisitor<H,S>{
public S visitReturn(Return_instr e, H h); public S visitReturn(Program.Return_instr e, H h);
} }
interface ExprVisitor<H,S> { public interface ExprVisitor<H,S> {
public S visitConst(Const e,H h); public S visitConst(Program.Const e,H h);
public S visitBinOp(BinopExpression e, H h); public S visitBinOp(Program.BinopExpression e, H h);
} }
interface Type{ public interface Type{
public String prettyprinter(); public String prettyprinter();
} }
enum Op {PLUS, MINUS, TIMES,DIV} public enum Op {PLUS, MINUS, TIMES,DIV}
//Eval //Eval
interface ExprEval extends ExprVisitor<Map<String, Integer>, Integer> { public interface ExprEval extends ExprVisitor<Map<String, Integer>, Integer> {
} }
interface TypeCheck extends ExprVisitor<Map<String, Type>, Type> { public interface TypeCheck extends ExprVisitor<Map<String, Type>, Type> {
}
} }

View File

@@ -1,13 +1,10 @@
package TP2.asd; package TP2.asd;
import java.util.ArrayList; import java.util.ArrayList;
import TP2.asd.Interface.*;
public class Program{
public record Program(ArrayList<Function> functions) { public static record Function(Type type, String nom, ArrayList<Instruction> instructions){
}
record Function(Type type, String nom, ArrayList<Instruction> instructions){
public Function(Type type, String name, Instruction instruction) { public Function(Type type, String name, Instruction instruction) {
this(type, name, new ArrayList<>() {{ add(instruction); }}); this(type, name, new ArrayList<>() {{ add(instruction); }});
} }
@@ -17,33 +14,35 @@ record Function(Type type, String nom, ArrayList<Instruction> instructions){
} }
} }
record Const(int c) implements Expression{ public static record Const(int c) implements Expression{
public <H, S> S accept(ExprVisitor<H, S> v, H h) { public <H, S> S accept(ExprVisitor<H, S> v, H h) {
return v.visitConst(this, h); return v.visitConst(this, h);
} }
} }
record BinopExpression(Op op,Expression e1, Expression e2) implements Expression{ public static record BinopExpression(Op op,Expression e1, Expression e2) implements Expression{
public <H, S> S accept(ExprVisitor<H, S> v, H h) { public <H, S> S accept(ExprVisitor<H, S> v, H h) {
return v.visitBinOp(this, h); return v.visitBinOp(this, h);
} }
} }
record Return_instr(Expression e) implements Instruction{ public static record Return_instr(Expression e) implements Instruction{
public <H, S> S accept(InstrVisitor<H, S> v, H h) { public <H, S> S accept(InstrVisitor<H, S> v, H h) {
return v.visitReturn(this,h); return v.visitReturn(this,h);
} }
} }
record Type_void() implements Type{ public static record Type_void() implements Type{
public String prettyprinter() { public String prettyprinter() {
return "VOID"; return "VOID";
} }
} }
record Type_int() implements Type{ public static record Type_int() implements Type{
public String prettyprinter() { public String prettyprinter() {
return "INT"; return "INT";
} }
} }
}