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_;
27 static void sigint(int) {
31 static JSStringRef Result_;
33 void Run(const char *code, FILE *fout) { _pooled
34 JSStringRef script(JSStringCreateWithUTF8CString(code));
36 JSContextRef context(CYGetJSContext());
38 JSValueRef exception(NULL);
39 JSValueRef result(JSEvaluateScript(context, script, NULL, NULL, 0, &exception));
40 JSStringRelease(script);
42 if (exception != NULL) { error:
47 if (!JSValueIsUndefined(context, result)) {
51 json = CYPoolJSONString(pool, context, result, &exception);
52 if (exception != NULL)
55 CYSetProperty(context, CYGetGlobalObject(context), Result_, result);
65 static void Console() {
71 rl_bind_key('\t', rl_insert);
73 struct sigaction action;
74 sigemptyset(&action.sa_mask);
75 action.sa_handler = &sigint;
77 sigaction(SIGINT, &action, NULL);
81 std::vector<std::string> lines;
84 const char *prompt("cy# ");
86 if (setjmp(ctrlc_) != 0) {
93 char *line(readline(prompt));
99 if (line[0] == '\\') {
100 std::string data(line + 1);
101 if (data == "bypass") {
103 fprintf(fout, "bypass == %s\n", bypass ? "true" : "false");
105 } else if (data == "debug") {
107 fprintf(fout, "debug == %s\n", debug ? "true" : "false");
115 lines.push_back(line);
125 cy::parser parser(driver);
127 driver.data_ = command.c_str();
128 driver.size_ = command.size();
130 if (parser.parse() != 0 || !driver.errors_.empty()) {
131 for (CYDriver::Errors::const_iterator i(driver.errors_.begin()); i != driver.errors_.end(); ++i) {
132 cy::position begin(i->location_.begin);
133 if (begin.line != lines.size() || begin.column - 1 != lines.back().size()) {
134 std::cerr << i->message_ << std::endl;
135 add_history(command.c_str());
140 driver.errors_.clear();
147 if (driver.source_ == NULL)
150 std::ostringstream str;
151 driver.source_->Show(str);
155 add_history(command.c_str());
158 std::cout << code << std::endl;
160 Run(code.c_str(), fout);
167 static void *Map(const char *path, size_t *psize) {
169 _syscall(fd = open(path, O_RDONLY));
172 _syscall(fstat(fd, &stat));
173 size_t size(stat.st_size);
178 _syscall(base = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0));
184 int main(int argc, const char *argv[]) {
190 CYSetArgs(argc - 1, argv + 1);
194 Result_ = CYCopyJSString("_");
196 if (script == NULL || strcmp(script, "-") == 0)
199 CYDriver driver(script);
200 cy::parser parser(driver);
203 char *start(reinterpret_cast<char *>(Map(script, &size)));
204 char *end(start + size);
206 if (size >= 2 && start[0] == '#' && start[1] == '!') {
208 while (start != end && *start++ != '\n');
211 driver.data_ = start;
212 driver.size_ = end - start;
214 if (parser.parse() != 0 || !driver.errors_.empty()) {
215 for (CYDriver::Errors::const_iterator i(driver.errors_.begin()); i != driver.errors_.end(); ++i)
216 std::cerr << i->location_.begin << ": " << i->message_ << std::endl;
217 } else if (driver.source_ != NULL) {
218 std::ostringstream str;
219 driver.source_->Show(str);
220 std::string code(str.str());
221 std::cout << code << std::endl;
222 Run(code.c_str(), stdout);