11 #include <readline/readline.h>
12 #include <readline/history.h>
19 #include <sys/types.h>
23 #include "Cycript.tab.hh"
25 static jmp_buf ctrlc_;
31 void Run(const char *code, FILE *fout) { _pooled
32 JSStringRef script(JSStringCreateWithUTF8CString(code));
34 JSContextRef context(CYGetJSContext());
36 JSValueRef exception(NULL);
37 JSValueRef result(JSEvaluateScript(context, script, NULL, NULL, 0, &exception));
38 JSStringRelease(script);
40 if (exception != NULL)
43 if (!JSValueIsUndefined(context, result)) {
47 json = CYCopyJSONString(context, result);
49 CYThrow(context, error, &result);
54 fputs([reinterpret_cast<const NSString *>(json) UTF8String], fout);
69 rl_bind_key('\t', rl_insert);
71 struct sigaction action;
72 sigemptyset(&action.sa_mask);
73 action.sa_handler = &sigint;
75 sigaction(SIGINT, &action, NULL);
79 std::vector<std::string> lines;
82 const char *prompt("cy# ");
84 if (setjmp(ctrlc_) != 0) {
91 char *line(readline(prompt));
97 if (line[0] == '\\') {
98 std::string data(line + 1);
99 if (data == "bypass") {
101 fprintf(fout, "bypass == %s\n", bypass ? "true" : "false");
103 } else if (data == "debug") {
105 fprintf(fout, "debug == %s\n", debug ? "true" : "false");
113 lines.push_back(line);
123 cy::parser parser(driver);
125 driver.data_ = command.c_str();
126 driver.size_ = command.size();
128 if (parser.parse() != 0 || !driver.errors_.empty()) {
129 for (CYDriver::Errors::const_iterator i(driver.errors_.begin()); i != driver.errors_.end(); ++i) {
130 cy::position begin(i->location_.begin);
131 if (begin.line != lines.size() || begin.column - 1 != lines.back().size()) {
132 std::cerr << i->message_ << std::endl;
133 add_history(command.c_str());
138 driver.errors_.clear();
145 if (driver.source_ == NULL)
148 std::ostringstream str;
149 driver.source_->Show(str);
153 add_history(command.c_str());
156 std::cout << code << std::endl;
158 Run(code.c_str(), fout);
165 void *Map(const char *path, size_t *psize) {
167 _syscall(fd = open(path, O_RDONLY));
170 _syscall(fstat(fd, &stat));
171 size_t size(stat.st_size);
176 _syscall(base = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0));
182 int main(int argc, const char *argv[]) {
188 CYSetArgs(argc - 1, argv + 1);
192 if (script == NULL || strcmp(script, "-") == 0)
195 CYDriver driver(script);
196 cy::parser parser(driver);
199 char *start(reinterpret_cast<char *>(Map(script, &size)));
200 char *end(start + size);
202 if (size >= 2 && start[0] == '#' && start[1] == '!') {
204 while (start != end && *start++ != '\n');
207 driver.data_ = start;
208 driver.size_ = end - start;
210 if (parser.parse() != 0 || !driver.errors_.empty()) {
211 for (CYDriver::Errors::const_iterator i(driver.errors_.begin()); i != driver.errors_.end(); ++i)
212 std::cerr << i->location_.begin << ": " << i->message_ << std::endl;
213 } else if (driver.source_ != NULL) {
214 std::ostringstream str;
215 driver.source_->Show(str);
216 std::string code(str.str());
217 std::cout << code << std::endl;
218 Run(code.c_str(), stdout);