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] == '\\') {
58 lines.push_back(line);
63 cy::parser parser(driver);
65 driver.data_ = command.c_str();
66 driver.size_ = command.size();
68 if (parser.parse() != 0 || !driver.errors_.empty()) {
69 for (CYDriver::Errors::const_iterator i(driver.errors_.begin()); i != driver.errors_.end(); ++i) {
70 cy::position begin(i->location_.begin);
71 if (begin.line != lines.size() || begin.column - 1 != lines.back().size()) {
72 std::cerr << i->message_ << std::endl;
77 driver.errors_.clear();
84 if (driver.source_ == NULL)
87 add_history(command.c_str());
89 std::ostringstream str;
90 driver.source_->Show(str);
92 std::string code(str.str());
93 std::cout << code << std::endl;
97 JSStringRef script(JSStringCreateWithUTF8CString(code.c_str()));
99 JSContextRef context(CYGetJSContext());
101 JSValueRef exception(NULL);
102 JSValueRef result(JSEvaluateScript(context, script, NULL, NULL, 0, &exception));
103 JSStringRelease(script);
105 if (exception != NULL)
108 if (JSValueIsUndefined(context, result))
114 json = CYCopyJSONString(context, result);
115 } @catch (id error) {
116 CYThrow(context, error, &result);
120 fputs([reinterpret_cast<const NSString *>(json) UTF8String], fout);