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[]) {
28 rl_bind_key('\t', rl_insert);
30 struct sigaction action;
31 sigemptyset(&action.sa_mask);
32 action.sa_handler = &sigint;
34 sigaction(SIGINT, &action, NULL);
38 std::vector<std::string> lines;
41 const char *prompt("cy# ");
43 if (setjmp(ctrlc_) != 0) {
50 char *line(readline(prompt));
56 if (line[0] == '\\') {
57 std::string data(line + 1);
58 if (data == "bypass") {
60 fprintf(fout, "bypass == %s\n", bypass ? "true" : "false");
62 } else if (data == "debug") {
64 fprintf(fout, "debug == %s\n", debug ? "true" : "false");
72 lines.push_back(line);
82 cy::parser parser(driver);
84 driver.data_ = command.c_str();
85 driver.size_ = command.size();
87 if (parser.parse() != 0 || !driver.errors_.empty()) {
88 for (CYDriver::Errors::const_iterator i(driver.errors_.begin()); i != driver.errors_.end(); ++i) {
89 cy::position begin(i->location_.begin);
90 if (begin.line != lines.size() || begin.column - 1 != lines.back().size()) {
91 std::cerr << i->message_ << std::endl;
92 add_history(command.c_str());
97 driver.errors_.clear();
104 if (driver.source_ == NULL)
107 std::ostringstream str;
108 driver.source_->Show(str);
112 add_history(command.c_str());
115 std::cout << code << std::endl;
119 JSStringRef script(JSStringCreateWithUTF8CString(code.c_str()));
121 JSContextRef context(CYGetJSContext());
123 JSValueRef exception(NULL);
124 JSValueRef result(JSEvaluateScript(context, script, NULL, NULL, 0, &exception));
125 JSStringRelease(script);
127 if (exception != NULL)
130 if (JSValueIsUndefined(context, result))
136 json = CYCopyJSONString(context, result);
137 } @catch (id error) {
138 CYThrow(context, error, &result);
142 fputs([reinterpret_cast<const NSString *>(json) UTF8String], fout);