]>
Commit | Line | Data |
---|---|---|
1 | #define _GNU_SOURCE | |
2 | ||
3 | #include <substrate.h> | |
4 | #include "cycript.hpp" | |
5 | ||
6 | #include <cstdio> | |
7 | #include <sstream> | |
8 | ||
9 | #include <setjmp.h> | |
10 | ||
11 | #include <readline/readline.h> | |
12 | #include <readline/history.h> | |
13 | ||
14 | #include "Cycript.tab.hh" | |
15 | ||
16 | static jmp_buf ctrlc_; | |
17 | ||
18 | void sigint(int) { | |
19 | longjmp(ctrlc_, 1); | |
20 | } | |
21 | ||
22 | int main(int argc, const char *argv[]) { | |
23 | FILE *fout(stdout); | |
24 | ||
25 | rl_bind_key('\t', rl_insert); | |
26 | ||
27 | struct sigaction action; | |
28 | sigemptyset(&action.sa_mask); | |
29 | action.sa_handler = &sigint; | |
30 | action.sa_flags = 0; | |
31 | sigaction(SIGINT, &action, NULL); | |
32 | ||
33 | restart: for (;;) { | |
34 | std::string command; | |
35 | std::vector<std::string> lines; | |
36 | ||
37 | bool extra(false); | |
38 | const char *prompt("cy# "); | |
39 | ||
40 | if (setjmp(ctrlc_) != 0) { | |
41 | fputs("\n", fout); | |
42 | fflush(fout); | |
43 | goto restart; | |
44 | } | |
45 | ||
46 | read: | |
47 | char *line(readline(prompt)); | |
48 | if (line == NULL) | |
49 | break; | |
50 | ||
51 | if (!extra) { | |
52 | extra = true; | |
53 | if (line[0] == '\\') { | |
54 | add_history(line); | |
55 | goto restart; | |
56 | } | |
57 | } | |
58 | ||
59 | lines.push_back(line); | |
60 | command += line; | |
61 | free(line); | |
62 | ||
63 | CYDriver driver(""); | |
64 | cy::parser parser(driver); | |
65 | ||
66 | driver.data_ = command.c_str(); | |
67 | driver.size_ = command.size(); | |
68 | ||
69 | if (parser.parse() != 0 || !driver.errors_.empty()) { | |
70 | for (CYDriver::Errors::const_iterator i(driver.errors_.begin()); i != driver.errors_.end(); ++i) { | |
71 | cy::position begin(i->location_.begin); | |
72 | if (begin.line != lines.size() || begin.column - 1 != lines.back().size()) { | |
73 | std::cerr << i->message_ << std::endl; | |
74 | add_history(command.c_str()); | |
75 | goto restart; | |
76 | } | |
77 | } | |
78 | ||
79 | driver.errors_.clear(); | |
80 | ||
81 | command += '\n'; | |
82 | prompt = "cy> "; | |
83 | goto read; | |
84 | } | |
85 | ||
86 | if (driver.source_ == NULL) | |
87 | goto restart; | |
88 | ||
89 | add_history(command.c_str()); | |
90 | ||
91 | std::ostringstream str; | |
92 | driver.source_->Show(str); | |
93 | ||
94 | std::string code(str.str()); | |
95 | std::cout << code << std::endl; | |
96 | ||
97 | _pooled | |
98 | ||
99 | JSStringRef script(JSStringCreateWithUTF8CString(code.c_str())); | |
100 | ||
101 | JSContextRef context(CYGetJSContext()); | |
102 | ||
103 | JSValueRef exception(NULL); | |
104 | JSValueRef result(JSEvaluateScript(context, script, NULL, NULL, 0, &exception)); | |
105 | JSStringRelease(script); | |
106 | ||
107 | if (exception != NULL) | |
108 | result = exception; | |
109 | ||
110 | if (JSValueIsUndefined(context, result)) | |
111 | goto restart; | |
112 | ||
113 | CFStringRef json; | |
114 | ||
115 | @try { json: | |
116 | json = CYCopyJSONString(context, result); | |
117 | } @catch (id error) { | |
118 | CYThrow(context, error, &result); | |
119 | goto json; | |
120 | } | |
121 | ||
122 | fputs([reinterpret_cast<const NSString *>(json) UTF8String], fout); | |
123 | CFRelease(json); | |
124 | ||
125 | fputs("\n", fout); | |
126 | fflush(fout); | |
127 | } | |
128 | ||
129 | fputs("\n", fout); | |
130 | fflush(fout); | |
131 | ||
132 | return 0; | |
133 | } |