Skip to content

Commit 02f276c

Browse files
committed
addind file IO
1 parent 7921474 commit 02f276c

13 files changed

Lines changed: 2251 additions & 1451 deletions

include/pythonic/REPL/ScriptIt.cpp

Lines changed: 1 addition & 1420 deletions
Large diffs are not rendered by default.

include/pythonic/REPL/issue.nsit

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"nsit_format": "1.0",
3+
"metadata": {
4+
"title": "New",
5+
"created": "2026-02-13T07:10:15.793124Z",
6+
"modified": "2026-02-13T07:10:50.081375Z",
7+
"kernel": "scriptit",
8+
"kernel_version": "2.0"
9+
},
10+
"cells": [
11+
{
12+
"id": "35ffd0e2-792a-47e2-85b5-98b21446bda7",
13+
"type": "code",
14+
"source": "var x =10 y\nprint(x)",
15+
"outputs": [
16+
{
17+
"type": "stdout",
18+
"text": "None\n"
19+
},
20+
{
21+
"type": "stderr",
22+
"text": "pythonic: TypeError: TypeError: unsupported operand type(s) for *: 'none' and 'none'"
23+
}
24+
],
25+
"execution_count": 1,
26+
"metadata": {},
27+
"_running": false
28+
}
29+
]
30+
}
Lines changed: 246 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,246 @@
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+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
{
2+
"nsit_format": "1.0",
3+
"metadata": {
4+
"title": "Untitled",
5+
"created": "2026-02-13T06:46:49.145552Z",
6+
"modified": "2026-02-13T06:54:54.318341Z",
7+
"kernel": "scriptit",
8+
"kernel_version": "2.0"
9+
},
10+
"cells": [
11+
{
12+
"id": "b0f50c2e-5a26-4fc7-b28d-c6fbaad38149",
13+
"type": "code",
14+
"source": "var a = 10.\nprint(2*a)\nsin(a)\n--> This is comment\nthis is also one\n<--",
15+
"outputs": [
16+
{
17+
"type": "stdout",
18+
"text": "20\n-0.544021\n"
19+
}
20+
],
21+
"execution_count": 34,
22+
"metadata": {},
23+
"_running": false
24+
},
25+
{
26+
"id": "cmlkj6wtse5zo1",
27+
"type": "markdown",
28+
"source": "# Next cell\n",
29+
"outputs": [],
30+
"execution_count": null,
31+
"metadata": {}
32+
},
33+
{
34+
"id": "cmlkj79a1yazu8",
35+
"type": "code",
36+
"source": "cos(a)",
37+
"outputs": [
38+
{
39+
"type": "stdout",
40+
"text": "-0.839072\n"
41+
}
42+
],
43+
"execution_count": 35,
44+
"metadata": {},
45+
"_running": false
46+
}
47+
]
48+
}

0 commit comments

Comments
 (0)