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) { error:
45 if (!JSValueIsUndefined(context, result)) {
49 json = CYPoolJSONString(pool, context, result, &exception);
50 if (exception != NULL)
67 rl_bind_key('\t', rl_insert);
69 struct sigaction action;
70 sigemptyset(&action.sa_mask);
71 action.sa_handler = &sigint;
73 sigaction(SIGINT, &action, NULL);
77 std::vector<std::string> lines;
80 const char *prompt("cy# ");
82 if (setjmp(ctrlc_) != 0) {
89 char *line(readline(prompt));
95 if (line[0] == '\\') {
96 std::string data(line + 1);
97 if (data == "bypass") {
99 fprintf(fout, "bypass == %s\n", bypass ? "true" : "false");
101 } else if (data == "debug") {
103 fprintf(fout, "debug == %s\n", debug ? "true" : "false");
111 lines.push_back(line);
121 cy::parser parser(driver);
123 driver.data_ = command.c_str();
124 driver.size_ = command.size();
126 if (parser.parse() != 0 || !driver.errors_.empty()) {
127 for (CYDriver::Errors::const_iterator i(driver.errors_.begin()); i != driver.errors_.end(); ++i) {
128 cy::position begin(i->location_.begin);
129 if (begin.line != lines.size() || begin.column - 1 != lines.back().size()) {
130 std::cerr << i->message_ << std::endl;
131 add_history(command.c_str());
136 driver.errors_.clear();
143 if (driver.source_ == NULL)
146 std::ostringstream str;
147 driver.source_->Show(str);
151 add_history(command.c_str());
154 std::cout << code << std::endl;
156 Run(code.c_str(), fout);
163 void *Map(const char *path, size_t *psize) {
165 _syscall(fd = open(path, O_RDONLY));
168 _syscall(fstat(fd, &stat));
169 size_t size(stat.st_size);
174 _syscall(base = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0));
180 int main(int argc, const char *argv[]) {
186 CYSetArgs(argc - 1, argv + 1);
190 if (script == NULL || strcmp(script, "-") == 0)
193 CYDriver driver(script);
194 cy::parser parser(driver);
197 char *start(reinterpret_cast<char *>(Map(script, &size)));
198 char *end(start + size);
200 if (size >= 2 && start[0] == '#' && start[1] == '!') {
202 while (start != end && *start++ != '\n');
205 driver.data_ = start;
206 driver.size_ = end - start;
208 if (parser.parse() != 0 || !driver.errors_.empty()) {
209 for (CYDriver::Errors::const_iterator i(driver.errors_.begin()); i != driver.errors_.end(); ++i)
210 std::cerr << i->location_.begin << ": " << i->message_ << std::endl;
211 } else if (driver.source_ != NULL) {
212 std::ostringstream str;
213 driver.source_->Show(str);
214 std::string code(str.str());
215 std::cout << code << std::endl;
216 Run(code.c_str(), stdout);