Advertisement
EBobkunov

compiler part with evaluation

Nov 12th, 2024
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.08 KB | Source Code | 0 0
  1. @Override
  2.     public Obj visitReadStatement(ReadStatement read) {
  3.         Obj value;
  4.         var scan = new Scanner(System.in);
  5.         switch (read.getReadType().type()) {
  6.             case ReadInt -> value = new IntegerObj(scan.nextInt());
  7.             case ReadReal -> value = new RealObj(scan.nextDouble());
  8.             case ReadString -> value = new StringObj(scan.nextLine());
  9.             default -> throw new RuntimeException("unexpected read");
  10.         }
  11.  
  12.         var tail = read.getDest();
  13.         var ident = find(tail.getIdentifier().lexeme());
  14.         if (tail.getTail() instanceof EmptyTail) {
  15.             assign(tail.getIdentifier().lexeme(), value);
  16.             return new EmptyObj();
  17.         }
  18.         var tails = ((AccessTailList) tail.getTail()).getTails();
  19.  
  20.         for (int idx = 0; idx < tails.size() - 1; idx++) {
  21.             var t = tails.get(idx);
  22.             switch (t) {
  23.                 case FunctionCall f -> {
  24.                     if (!(ident instanceof FunctionObj func)) {
  25.                         throw Errors.notCallableObject(ident.toString());
  26.                     }
  27.                     List<Obj> evaluatedArgs = new LinkedList<>();
  28.                     for (var expr : f.getExpressions().getExpressions()) {
  29.                         evaluatedArgs.add(expr.accept(this));
  30.                     }
  31.                     ident = func.eval(evaluatedArgs);
  32.                     if (ident instanceof ReturnObj ro) {
  33.                         ident = ro.ret();
  34.                     }
  35.                 }
  36.                 case ArrayAccess access -> {
  37.                     if (!(ident instanceof ArrayObj arr)) {
  38.                         throw Errors.indexAccessToNotArray(ident.toString());
  39.                     }
  40.                     var index = access.getExpression().accept(this);
  41.                     if (!(index instanceof IntegerObj intIdx)) {
  42.                         throw Errors.notIntegerArrayIndex(index);
  43.                     }
  44.                     ident = arr.get(intIdx.getValue());
  45.                 }
  46.                 case TupleAccess access -> {
  47.                     if (!(ident instanceof TupleObj tuple)) {
  48.                         throw Errors.namedAccessToNoTuple(ident.toString(), ident.type());
  49.                     }
  50.                     if (access.getIdentifier() != null) {
  51.                         ident = tuple.getByName(access.getIdentifier().lexeme());
  52.                     } else {
  53.                         var ind = access.getLiteral().literal();
  54.                         if (!(ind instanceof Integer i)) {
  55.                             throw Errors.literalAccessError();
  56.                         }
  57.                         ident = tuple.getByInd(i);
  58.                     }
  59.                 }
  60.                 default -> throw new IllegalStateException("Unexpected value: " + t);
  61.             }
  62.         }
  63.  
  64.         switch (tails.getLast()) {
  65.             case TupleAccess access -> {
  66.                 if (!(ident instanceof TupleObj tuple)) {
  67.                     throw Errors.namedAccessToNoTuple(ident.toString(), ident.type());
  68.                 }
  69.                 if (access.getIdentifier() != null) {
  70.                     tuple.setByName(access.getIdentifier().lexeme(), value);
  71.                 } else {
  72.                     var ind = access.getLiteral().literal();
  73.                     if (!(ind instanceof Integer i)) {
  74.                         throw Errors.literalAccessError();
  75.                     }
  76.                     tuple.setByInd(i, value);
  77.                 }
  78.             }
  79.             case ArrayAccess access -> {
  80.                 if (!(ident instanceof ArrayObj arr)) {
  81.                     throw Errors.indexAccessToNotArray(ident.toString());
  82.                 }
  83.                 var index = access.getExpression().accept(this);
  84.                 if (!(index instanceof IntegerObj intIdx)) {
  85.                     throw Errors.notIntegerArrayIndex(index);
  86.                 }
  87.                 arr.set(intIdx.getValue(), value);
  88.             }
  89.             default -> throw new IllegalStateException("Unexpected value: " + tails.getLast());
  90.         }
  91.         return new EmptyObj();
  92.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement