1+ #pragma once
2+
3+ // ═══════════════════════════════════════════════════════════
4+ // ──── Minimal JSON Helpers (for kernel mode) ───────────────
5+ // ═══════════════════════════════════════════════════════════
6+
7+ // Minimal JSON value: string→string map parser (for kernel protocol)
8+ inline std::unordered_map<std::string, std::string> parse_json_object (const std::string &json)
9+ {
10+ std::unordered_map<std::string, std::string> result;
11+ size_t i = json.find (' {' );
12+ if (i == std::string::npos)
13+ return result;
14+ i++;
15+
16+ auto skipWS = [&]()
17+ { while (i < json.size () && std::isspace (json[i])) i++; };
18+ auto readString = [&]() -> std::string
19+ {
20+ skipWS ();
21+ if (i >= json.size () || json[i] != ' "' )
22+ return " " ;
23+ i++; // skip opening "
24+ std::string s;
25+ while (i < json.size () && json[i] != ' "' )
26+ {
27+ if (json[i] == ' \\ ' && i + 1 < json.size ())
28+ {
29+ i++;
30+ if (json[i] == ' n' )
31+ s += ' \n ' ;
32+ else if (json[i] == ' t' )
33+ s += ' \t ' ;
34+ else if (json[i] == ' \\ ' )
35+ s += ' \\ ' ;
36+ else if (json[i] == ' "' )
37+ s += ' "' ;
38+ else if (json[i] == ' /' )
39+ s += ' /' ;
40+ else
41+ s += json[i];
42+ }
43+ else
44+ s += json[i];
45+ i++;
46+ }
47+ if (i < json.size ())
48+ i++; // skip closing "
49+ return s;
50+ };
51+
52+ while (i < json.size ())
53+ {
54+ skipWS ();
55+ if (json[i] == ' }' )
56+ break ;
57+ if (json[i] == ' ,' )
58+ {
59+ i++;
60+ continue ;
61+ }
62+ std::string key = readString ();
63+ skipWS ();
64+ if (i < json.size () && json[i] == ' :' )
65+ i++;
66+ skipWS ();
67+ // Value can be string, number, or null
68+ if (i < json.size () && json[i] == ' "' )
69+ {
70+ result[key] = readString ();
71+ }
72+ else
73+ {
74+ // Read non-string value as raw text until , or }
75+ std::string val;
76+ while (i < json.size () && json[i] != ' ,' && json[i] != ' }' )
77+ val += json[i++];
78+ // trim
79+ while (!val.empty () && std::isspace (val.back ()))
80+ val.pop_back ();
81+ result[key] = val;
82+ }
83+ }
84+ return result;
85+ }
86+
87+ inline std::string json_escape (const std::string &s)
88+ {
89+ std::string out;
90+ out.reserve (s.size () + 10 );
91+ for (char c : s)
92+ {
93+ switch (c)
94+ {
95+ case ' "' :
96+ out += " \\\" " ;
97+ break ;
98+ case ' \\ ' :
99+ out += " \\\\ " ;
100+ break ;
101+ case ' \n ' :
102+ out += " \\ n" ;
103+ break ;
104+ case ' \r ' :
105+ out += " \\ r" ;
106+ break ;
107+ case ' \t ' :
108+ out += " \\ t" ;
109+ break ;
110+ default :
111+ out += c;
112+ }
113+ }
114+ return out;
115+ }
116+
117+ inline std::string make_json_response (const std::string &cellId, const std::string &status,
118+ const std::string &stdoutStr, const std::string &stderrStr,
119+ const std::string &result, int execCount)
120+ {
121+ return " {\" cell_id\" :\" " + json_escape (cellId) + " \" ,"
122+ " \" status\" :\" " +
123+ json_escape (status) + " \" ,"
124+ " \" stdout\" :\" " +
125+ json_escape (stdoutStr) + " \" ,"
126+ " \" stderr\" :\" " +
127+ json_escape (stderrStr) + " \" ,"
128+ " \" result\" :\" " +
129+ json_escape (result) + " \" ,"
130+ " \" execution_count\" :" +
131+ std::to_string (execCount) + " }" ;
132+ }
133+
134+ // ═══════════════════════════════════════════════════════════
135+ // ──── Kernel Mode ──────────────────────────────────────────
136+ // ═══════════════════════════════════════════════════════════
137+
138+ void runKernel ()
139+ {
140+ Scope globalScope;
141+ globalScope.define (" PI" , var (3.14159265 ));
142+ globalScope.define (" e" , var (2.7182818 ));
143+ int executionCount = 0 ;
144+
145+ // Signal ready
146+ std::cout << " {\" status\" :\" kernel_ready\" ,\" version\" :\" 2.0\" }" << std::endl;
147+ std::cout.flush ();
148+
149+ std::string line;
150+ while (std::getline (std::cin, line))
151+ {
152+ if (line.empty ())
153+ continue ;
154+
155+ auto cmd = parse_json_object (line);
156+ std::string action = cmd[" action" ];
157+
158+ if (action == " shutdown" )
159+ {
160+ std::cout << " {\" status\" :\" shutdown_ok\" }" << std::endl;
161+ std::cout.flush ();
162+ break ;
163+ }
164+
165+ if (action == " reset" )
166+ {
167+ globalScope.clear ();
168+ globalScope.define (" PI" , var (3.14159265 ));
169+ globalScope.define (" e" , var (2.7182818 ));
170+ executionCount = 0 ;
171+ std::cout << " {\" status\" :\" reset_ok\" }" << std::endl;
172+ std::cout.flush ();
173+ continue ;
174+ }
175+
176+ if (action == " execute" )
177+ {
178+ std::string cellId = cmd[" cell_id" ];
179+ std::string code = cmd[" code" ];
180+ executionCount++;
181+
182+ // Capture stdout
183+ std::ostringstream capturedOut;
184+ std::streambuf *oldBuf = std::cout.rdbuf (capturedOut.rdbuf ());
185+
186+ std::string errorStr;
187+ std::string resultStr;
188+
189+ try
190+ {
191+ Tokenizer tokenizer;
192+ auto tokens = tokenizer.tokenize (code);
193+ Parser parser (tokens);
194+ auto program = parser.parseProgram ();
195+
196+ for (auto &stmt : program->statements )
197+ stmt->execute (globalScope);
198+ }
199+ catch (ReturnException &e)
200+ {
201+ resultStr = format_output (e.value );
202+ }
203+ catch (std::exception &e)
204+ {
205+ errorStr = e.what ();
206+ }
207+
208+ // Restore stdout
209+ std::cout.rdbuf (oldBuf);
210+ std::string stdoutStr = capturedOut.str ();
211+
212+ std::string status = errorStr.empty () ? " ok" : " error" ;
213+ std::cout << make_json_response (cellId, status, stdoutStr, errorStr, resultStr, executionCount) << std::endl;
214+ std::cout.flush ();
215+ continue ;
216+ }
217+
218+ if (action == " complete" )
219+ {
220+ std::string code = cmd[" code" ];
221+ // Simple completion: list scope variables and builtins that match prefix
222+ std::vector<std::string> matches;
223+ for (auto &[name, _] : globalScope.getAll ())
224+ {
225+ if (name.find (code) == 0 )
226+ matches.push_back (name);
227+ }
228+ // Return as JSON array
229+ std::string out = " {\" status\" :\" ok\" ,\" completions\" :[" ;
230+ for (size_t i = 0 ; i < matches.size (); i++)
231+ {
232+ if (i > 0 )
233+ out += " ," ;
234+ out += " \" " + json_escape (matches[i]) + " \" " ;
235+ }
236+ out += " ]}" ;
237+ std::cout << out << std::endl;
238+ std::cout.flush ();
239+ continue ;
240+ }
241+
242+ // Unknown action
243+ std::cout << " {\" status\" :\" error\" ,\" stderr\" :\" Unknown action: " + json_escape (action) + " \" }" << std::endl;
244+ std::cout.flush ();
245+ }
246+ }
0 commit comments