11 #include <readline/readline.h>
12 #include <readline/history.h>
14 #include "Cycript.tab.hh"
16 static jmp_buf ctrlc_;
22 int main(int argc, const char *argv[]) {
25 rl_bind_key('\t', rl_insert);
27 struct sigaction action;
28 sigemptyset(&action.sa_mask);
29 action.sa_handler = &sigint;
31 sigaction(SIGINT, &action, NULL);
35 std::vector<std::string> lines;
38 const char *prompt("cy# ");
40 if (setjmp(ctrlc_) != 0) {
47 char *line(readline(prompt));
53 if (line[0] == '\\') {
59 lines.push_back(line);
64 cy::parser parser(driver);
66 driver.data_ = command.c_str();
67 driver.size_ = command.size();
69 if (parser.parse() != 0 || !driver.errors_.empty()) {
70 for (CYDriver::Errors::const_iterator i(driver.errors_.begin()); i != driver.errors_.end(); ++i) {
71 cy::position begin(i->location_.begin);
72 if (begin.line != lines.size() || begin.column - 1 != lines.back().size()) {
73 std::cerr << i->message_ << std::endl;
74 add_history(command.c_str());
79 driver.errors_.clear();
86 if (driver.source_ == NULL)
89 add_history(command.c_str());
91 std::ostringstream str;
92 driver.source_->Show(str);
94 std::string code(str.str());
95 std::cout << code << std::endl;
99 JSStringRef script(JSStringCreateWithUTF8CString(code.c_str()));
101 JSContextRef context(CYGetJSContext());
103 JSValueRef exception(NULL);
104 JSValueRef result(JSEvaluateScript(context, script, NULL, NULL, 0, &exception));
105 JSStringRelease(script);
107 if (exception != NULL)
110 if (JSValueIsUndefined(context, result))
116 json = CYCopyJSONString(context, result);
117 } @catch (id error) {
118 CYThrow(context, error, &result);
122 fputs([reinterpret_cast<const NSString *>(json) UTF8String], fout);