Skip to content

Instantly share code, notes, and snippets.

@dtinth
Created July 28, 2012 15:57
Show Gist options
  • Save dtinth/3193829 to your computer and use it in GitHub Desktop.
Save dtinth/3193829 to your computer and use it in GitHub Desktop.
print("Enter your name: ")
puts("Hello, " + gets())
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import org.mozilla.javascript.*;
public class Main {
static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
public static void main(String[] args) throws FileNotFoundException, IOException {
Context cx = Context.enter();
try {
ScriptableObject scope = cx.initStandardObjects();
String[] functions = { "gets", "puts", "print" };
scope.defineFunctionProperties(functions, Main.class, ScriptableObject.DONTENUM);
cx.evaluateReader(scope, new FileReader(args[0]), args[0], 1, null);
} finally {
Context.exit();
}
}
public static String gets() throws IOException {
String line = reader.readLine();
if (line.endsWith("\r\n")) {
line = line.substring(0, line.length() - 2);
} else if (line.endsWith("\n")) {
line = line.substring(0, line.length() - 1);
}
return line;
}
public static void puts(String x) {
System.out.println(x);
}
public static void print(String x) {
System.out.print(x);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment