import jdk.jshell.JShell;
import jdk.jshell.SnippetEvent;
import java.util.List;

public class EvaluasiPotongan {
    public static void main(String[] args) {
        // JShell API: evaluator yang sama dengan tool jshell.
        try (JShell shell = JShell.create()) {
            String[] potongan = {
                    "int luas = 6 * 7;",
                    "luas + 6",
                    "\"Halo \" + \"REPL\"",
                    "luas / 0"
            };
            for (String p : potongan) {
                List<SnippetEvent> peristiwa = shell.eval(p);
                SnippetEvent e = peristiwa.get(0);
                if (e.exception() != null) {
                    System.out.printf("%-22s -> galat=%s: %s%n", p,
                            e.exception().getClass().getSimpleName(),
                            e.exception().getMessage());
                } else {
                    System.out.printf("%-22s -> nilai=%s%n", p, e.value());
                }
            }
        }
    }
}
