Skip to content

Instantly share code, notes, and snippets.

@benwaffle
Created November 26, 2014 05:35
Show Gist options
  • Save benwaffle/86f207b6fd53a838c1a0 to your computer and use it in GitHub Desktop.
Save benwaffle/86f207b6fd53a838c1a0 to your computer and use it in GitHub Desktop.
tiny nashorn (javascript) interpreter
import java.util.NoSuchElementException;
import java.util.Scanner;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
public class JS {
public static void main(String[] args) {
ScriptEngine se = new ScriptEngineManager().getEngineByName("nashorn");
Scanner s = new Scanner(System.in);
String in = "";
System.out.print("> ");
while (true){
try {
in += s.nextLine(); // get more code
se.eval(in); // try eval
} catch (ScriptException e) {
if (e.getMessage().contains("but found eof")){ // incomplete expression
System.out.print("|\t");
} else { // JS error
in = "";
System.out.print("Error: " + e.getMessage() + "\n> ");
}
continue;
} catch (NoSuchElementException e){ // EOF, no more code
break;
}
// JS expression successfully evaluated
in = "";
System.out.print("> ");
}
s.close();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment