Error: not a statement

From Esolang
Jump to navigation Jump to search

Error: not a statement is a restricted subset of Java by User:Fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff, which only allows expressions statements that would normally be flagged as not a statement.

The only statement that is allowed is return expr; and the following expressions are banned:

  • Increment/decrement operators
  • Assignments
  • Explicit method calls

So how do you do anything?

Well, the following operators can perform arbitrary functions without breaking the rules:

  • The + operator
  • The new operator

However both have problems.

The + operator can only return a string and can't use its other argument for computation. And the new operator can't return anything because it can't assign its fields and perform arbitrary computation.

A basic program

public record Adding(int first, int second) {  
  @Override
  public String toString() {
    return "" + (first + second);
  }
}

Limitations

Unfortunately, there is no IO, as that requires a method call. If you want IO, create this class

import java.util.Scanner;
public record IO(String out, IO.Mode mode) {
  @Override
  public String toString() {
    return switch(mode) {
      case Mode.OUTPUT -> {System.out.println(out); yield out;}
      case Mode.INPUT -> new Scanner(System.in).nextLine();
    };
  }

  public static enum Mode {
    OUTPUT, INPUT;
  }
}

Deadfish interpreter

import java.util.Scanner;

record Program(char[] code, int acc, int stmt, String output) {
    @Override public String toString() {
        return code.length == stmt ? output
            : output + switch (code[stmt]) {
            case 'i' -> new Program(code, acc + 1, stmt + 1, output);
            case 'd' -> new Program(code, acc - 1, stmt + 1, output);
            case 's' -> new Program(code, acc * acc, stmt + 1, output);
            case 'o' -> new Program(code, acc, stmt + 1, output + acc);
            default -> {throw new IllegalArgumentException(code[stmt] + "");}
        };
    }
}

// The main class doesn't count because it is just used for IO
public class Main {
    public static void main (String[] args) {
        final Scanner sc = new Scanner(System.in);
        final char[] code = sc.nextLine().toCharArray();
        final Program program = new Program(code, 0, 0, "");
        System.out.println(program + "");
    }
}