add 1 not ok, dont come back with result

This commit is contained in:
Vu Tuan Minh
2025-04-01 21:13:01 +02:00
parent 40e2bc5522
commit 52a5c00f74
12 changed files with 387 additions and 242 deletions

View File

@@ -7,6 +7,7 @@ import org.antlr.runtime.ANTLRInputStream;
import org.antlr.runtime.CharStream;
import org.antlr.runtime.CommonTokenStream;
import org.antlr.runtime.RecognitionException;
import org.antlr.runtime.Token;
import TP2.asd.Program.*;
import java.util.*;
@@ -28,6 +29,10 @@ public class Main {
VSLLexer lexer = new VSLLexer(input);
CommonTokenStream tokens = new CommonTokenStream(lexer);
for (Token token : tokens.getTokens()) {
System.out.println("Token: " + token.getText() + " Type: " + token.getType());
}
// Instantiate Parser
VSLParser parser = new VSLParser(tokens);

View File

@@ -0,0 +1,34 @@
package TP2.asd;
import java.util.Map;
import TP2.asd.Interface.*;
import TP2.asd.Program.*;
public class Eval {
public class ExprEval implements ExprVisitor<Map<String,Integer>,Integer>{
public Integer visitConst(ConstImp c, Map<String,Integer> h){
return c.c();
}
public Integer visitBinOp(BinopExpressionImp e, Map<String, Integer> 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();
}
}
}
public class InstructionEval implements InstrVisitor<Map<String,Integer>,Integer>{
@Override
public Integer visitReturn(Return_instrImp e, Map<String, Integer> h) {
ExprEval exprEval = new ExprEval();
return e.e().accept(exprEval, h);
}
}
}

View File

@@ -1,36 +1,47 @@
package TP2.asd;
import java.util.ArrayList;
import java.util.Map;
import TP2.asd.Program.*;
public interface Interface{
public interface Expression {
public <H,S> S accept(ExprVisitor<H,S> v, H h);
}
public interface Interface{
public interface ProgramI {
public <H,S> S accept(ProgramVisitor<H,S> v, H h);
}
public interface Function {
public <H,S> S accept(FunctionVisitor<H,S> v, H h);
}
public interface Instruction {
public <H,S> S accept(InstrVisitor<H,S> v, H h);
}
public interface Expression {
public <H,S> S accept(ExprVisitor<H,S> v, H h);
}
public interface ProgramVisitor<H,S> {
public S visitProgram(ProgramImp p, H h);
public S visitFunction(Function f, H h);
public S visitProgram(ProgramImp programImp, H h);
}
public interface FunctionVisitor<H,S> {
public S visitFunction(FunctionImp e, H h);
}
public interface InstrVisitor<H,S>{
public S visitReturn(Return_instr e, H h);
public S visitReturn(Return_instrImp e, H h);
}
public interface ExprVisitor<H,S> {
public S visitConst(Const e,H h);
public S visitBinOp(BinopExpression e, H h);
public S visitConst(ConstImp e,H h);
public S visitBinOp(BinopExpressionImp e, H h);
}
public interface Type{
public String prettyprinter();
}
public enum Op {PLUS, MINUS, TIMES, DIV}
public enum Op {PLUS, MINUS, TIMES, DIV,MOD}
//Eval
public interface ExprEval extends ExprVisitor<Map<String, Integer>, Integer> {

View File

@@ -2,55 +2,108 @@ package TP2.asd;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import TP2.asd.Interface.*;
import TP2.asd.Eval.*;
public class Program{
public static record ProgramImp(ArrayList<Function> instructions){
public static record ProgramImp(ArrayList<Function> instructions) implements ProgramI{
public <H, S> S accept(ProgramVisitor<H, S> v, H h) {
return v.visitProgram(this, h);
}
}
public static record Function(Type type, String nom, ArrayList<Instruction> instructions){
public Function(Type type, String name, Instruction instruction) {
public static record FunctionImp(Type type, String nom, ArrayList<Instruction> instructions)implements Function {
public FunctionImp(Type type, String name, Instruction instruction) {
this(type, name, new ArrayList<>() {{ add(instruction); }});
}
public <H, S> S accept(ProgramVisitor<H, S> v, H h) {
public <H, S> S accept(FunctionVisitor<H, S> v, H h) {
return v.visitFunction(this, h);
}
}
public static record Const(int c) implements Expression{
public static record ConstImp(int c) implements Expression{
public <H, S> S accept(ExprVisitor<H, S> v, H h) {
return v.visitConst(this, h);
}
}
public static record BinopExpression(Op op,Expression e1, Expression e2) implements Expression{
public static record BinopExpressionImp(Op op,Expression e1, Expression e2) implements Expression{
public <H, S> S accept(ExprVisitor<H, S> v, H h) {
return v.visitBinOp(this, h);
}
}
public static record Return_instr(Expression e) implements Instruction{
public static record Return_instrImp(Expression e) implements Instruction{
public <H, S> S accept(InstrVisitor<H, S> v, H h) {
return v.visitReturn(this,h);
}
}
public static record Type_void() implements Type{
public static record Type_voidImp() implements Type{
public String prettyprinter() {
return "VOID";
}
}
public static record Type_int() implements Type{
public static record Type_intImp() implements Type{
public String prettyprinter() {
return "INT";
}
}
}
//Eval
public static class ProgramEval implements ProgramVisitor<Map<String, Integer>, Integer> {
@Override
public Integer visitProgram(ProgramImp e, Map<String, Integer> h) {
Integer result = null;
FunctionEval functionEval = new FunctionEval();
for (Function function : e.instructions()) {
result = function.accept(functionEval, h);
}
return result;
}
}
public static class FunctionEval implements FunctionVisitor<Map<String, Integer>, Integer> {
@Override
public Integer visitFunction(FunctionImp e, Map<String, Integer> h) {
InstructionEval instructionEval = new InstructionEval();
Integer result = null;
for (Instruction instr : e.instructions()) {
result = instr.accept(instructionEval, h);
}
return result;
}
}
public static class InstructionEval implements InstrVisitor<Map<String,Integer>,Integer>{
@Override
public Integer visitReturn(Return_instrImp e, Map<String, Integer> h) {
ExprEval exprEval = new ExprEval();
return e.e().accept(exprEval, h);
}
}
public static class ExprEval implements ExprVisitor<Map<String,Integer>,Integer>{
public Integer visitConst(ConstImp c, Map<String,Integer> h){
return c.c();
}
public Integer visitBinOp(BinopExpressionImp e, Map<String, Integer> 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);
case Op.MOD: return e.e1().accept(this, h)%e.e2().accept(this, h);
default: throw new IllegalArgumentException();
}
}
}
}