tsslang 1.0.4 → 1.0.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/package.json +1 -1
  2. package/tss.js +217 -0
  3. package/tss_runtime.js +132 -0
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tsslang",
3
- "version": "1.0.4",
3
+ "version": "1.0.6",
4
4
  "description": "TSSLang programming language",
5
5
  "license": "MIT",
6
6
  "bin": {
package/tss.js ADDED
@@ -0,0 +1,217 @@
1
+ #!/usr/bin/env node
2
+
3
+ const fs = require("fs");
4
+ const path = require("path");
5
+ const { execSync } = require("child_process");
6
+
7
+ const file = process.argv[2];
8
+ if (!file) {
9
+ console.log("Use: tss <file.tss>");
10
+ process.exit(1);
11
+ }
12
+ const code = fs.readFileSync(file, "utf8");
13
+
14
+ function stripComments(line) {
15
+ const i = line.indexOf("#");
16
+ if (i >= 0) return line.slice(0, i);
17
+ return line;
18
+ }
19
+
20
+ function exprToJS(expr) {
21
+ expr = expr.trim();
22
+
23
+ expr = expr.replace(/\btrue\b/g, "true");
24
+ expr = expr.replace(/\bfalse\b/g, "false");
25
+ expr = expr.replace(/\bnull\b/g, "null");
26
+
27
+ expr = expr.replace(/\band\b/g, "&&");
28
+ expr = expr.replace(/\bor\b/g, "||");
29
+ expr = expr.replace(/\bnot\b/g, "!");
30
+
31
+ expr = expr.replace(/\bjson\.parse\b/g, "__tss.json.parse");
32
+ expr = expr.replace(/\bjson\.stringify\b/g, "__tss.json.stringify");
33
+
34
+ expr = expr.replace(/\bfile\.read\b/g, "__tss.file.read");
35
+ expr = expr.replace(/\bfile\.write\b/g, "__tss.file.write");
36
+
37
+ expr = expr.replace(/\bserver\./g, "__tss.server.");
38
+
39
+ return expr;
40
+ }
41
+
42
+ function compileTSS(source) {
43
+ const lines = source.split("\n");
44
+
45
+ let js = "";
46
+ let indent = 0;
47
+
48
+ function emit(line) {
49
+ js += " ".repeat(indent) + line + "\n";
50
+ }
51
+
52
+ function inc() {
53
+ indent++;
54
+ }
55
+
56
+ function dec() {
57
+ indent = Math.max(0, indent - 1);
58
+ }
59
+
60
+ emit(`"use strict";`);
61
+ emit(`const __tss = require(path.join(__dirname, "tss_runtime.js"));`);
62
+ emit(`(async () => {`);
63
+ inc();
64
+
65
+ for (let index = 0; index < lines.length; index++) {
66
+ const rawLine = lines[index];
67
+ let line = stripComments(rawLine).trim();
68
+ if (!line) continue;
69
+
70
+ const closeElseInline = line.match(/^\}\s*else\s*\{$/);
71
+ if (closeElseInline) {
72
+ dec();
73
+ emit(`} else {`);
74
+ inc();
75
+ continue;
76
+ }
77
+
78
+ if (line === "else {") {
79
+ dec();
80
+ emit(`} else {`);
81
+ inc();
82
+ continue;
83
+ }
84
+
85
+ if (line === "}") {
86
+ dec();
87
+ emit("}");
88
+ continue;
89
+ }
90
+
91
+ if (line.startsWith("print ")) {
92
+ const rest = line.slice(6);
93
+ emit(`console.log(${exprToJS(rest)});`);
94
+ continue;
95
+ }
96
+
97
+ if (line.startsWith("let ")) {
98
+ emit(`let ${exprToJS(line.slice(4))};`);
99
+ continue;
100
+ }
101
+
102
+ if (/^[A-Za-z_]\w*\s*=/.test(line)) {
103
+ emit(`${exprToJS(line)};`);
104
+ continue;
105
+ }
106
+
107
+ if (line.startsWith("if ")) {
108
+ const m = line.match(/^if\s+(.+)\s*\{$/);
109
+ if (!m) throw new Error(`Syntax error (if) at line ${index + 1}`);
110
+ emit(`if (${exprToJS(m[1])}) {`);
111
+ inc();
112
+ continue;
113
+ }
114
+
115
+ if (line.startsWith("while ")) {
116
+ const m = line.match(/^while\s+(.+)\s*\{$/);
117
+ if (!m) throw new Error(`Syntax error (while) at line ${index + 1}`);
118
+ emit(`while (${exprToJS(m[1])}) {`);
119
+ inc();
120
+ continue;
121
+ }
122
+
123
+ if (line.startsWith("for ")) {
124
+ const m = line.match(/^for\s+([A-Za-z_]\w*)\s+in\s+(.+)\.\.(.+)\s*\{$/);
125
+ if (!m) throw new Error(`Syntax error (for) at line ${index + 1}`);
126
+ const varName = m[1];
127
+ const start = exprToJS(m[2]);
128
+ const end = exprToJS(m[3]);
129
+ emit(`for (let ${varName} = (${start}); ${varName} <= (${end}); ${varName}++) {`);
130
+ inc();
131
+ continue;
132
+ }
133
+
134
+ if (line.startsWith("fn ")) {
135
+ const m = line.match(/^fn\s+([A-Za-z_]\w*)\s*\((.*?)\)\s*\{$/);
136
+ if (!m) throw new Error(`Syntax error (fn) at line ${index + 1}`);
137
+ const name = m[1];
138
+ const args = m[2].trim();
139
+ emit(`function ${name}(${args}) {`);
140
+ inc();
141
+ continue;
142
+ }
143
+
144
+ if (line.startsWith("return ")) {
145
+ emit(`return ${exprToJS(line.slice(7))};`);
146
+ continue;
147
+ }
148
+
149
+ if (line.startsWith("run ")) {
150
+ const rest = line.slice(4).trim();
151
+ emit(`await __tss.run(${exprToJS(rest)});`);
152
+ continue;
153
+ }
154
+
155
+ if (line.startsWith("sleep ")) {
156
+ const rest = line.slice(6).trim();
157
+ emit(`await __tss.sleep(${exprToJS(rest)});`);
158
+ continue;
159
+ }
160
+
161
+ if (line.startsWith("server.get ")) {
162
+ const m = line.match(/^server\.get\s+(.+)\s*\{$/);
163
+ if (!m) throw new Error(`Syntax error (server.get) at line ${index + 1}`);
164
+ const route = exprToJS(m[1]);
165
+ emit(`__tss.server.get(${route}, async (req, res) => {`);
166
+ inc();
167
+ continue;
168
+ }
169
+
170
+ if (line.startsWith("server.post ")) {
171
+ const m = line.match(/^server\.post\s+(.+)\s*\{$/);
172
+ if (!m) throw new Error(`Syntax error (server.post) at line ${index + 1}`);
173
+ const route = exprToJS(m[1]);
174
+ emit(`__tss.server.post(${route}, async (req, res) => {`);
175
+ inc();
176
+ continue;
177
+ }
178
+
179
+ if (line.startsWith("listen ")) {
180
+ const port = exprToJS(line.slice(7));
181
+ emit(`await __tss.server.listen(${port});`);
182
+ continue;
183
+ }
184
+
185
+ if (line.startsWith("res.json ")) {
186
+ emit(`res.json(${exprToJS(line.slice(9))});`);
187
+ continue;
188
+ }
189
+
190
+ if (line.startsWith("res.text ")) {
191
+ emit(`res.text(${exprToJS(line.slice(9))});`);
192
+ continue;
193
+ }
194
+
195
+ if (line.startsWith("js:")) {
196
+ emit(line.slice(3).trim());
197
+ continue;
198
+ }
199
+
200
+ throw new Error(`Unknown command at line ${index + 1}: ${rawLine.trim()}`);
201
+ }
202
+
203
+ dec();
204
+ emit(`})();`);
205
+
206
+ return js;
207
+ }
208
+
209
+ try {
210
+ const outJS = compileTSS(code);
211
+ const outPath = path.join(process.cwd(), ".tss_out.js");
212
+ fs.writeFileSync(outPath, outJS, "utf8");
213
+ execSync(`node "${outPath}"`, { stdio: "inherit" });
214
+ } catch (e) {
215
+ console.error("TSS Error:", e.message);
216
+ process.exit(1);
217
+ }
package/tss_runtime.js ADDED
@@ -0,0 +1,132 @@
1
+ "use strict";
2
+ const fs = require("fs");
3
+ const { exec } = require("child_process");
4
+ const http = require("http");
5
+ const url = require("url");
6
+
7
+ function sleep(ms) {
8
+ return new Promise((r) => setTimeout(r, Number(ms) || 0));
9
+ }
10
+
11
+ function run(cmd) {
12
+ return new Promise((resolve, reject) => {
13
+ exec(String(cmd), { shell: true }, (err, stdout, stderr) => {
14
+ if (stdout) process.stdout.write(stdout);
15
+ if (stderr) process.stderr.write(stderr);
16
+ if (err) reject(err);
17
+ else resolve(true);
18
+ });
19
+ });
20
+ }
21
+
22
+ const file = {
23
+ read(p) {
24
+ return fs.readFileSync(String(p), "utf8");
25
+ },
26
+ write(p, content) {
27
+ fs.writeFileSync(String(p), String(content ?? ""), "utf8");
28
+ return true;
29
+ },
30
+ };
31
+
32
+ const json = {
33
+ parse(s) {
34
+ return JSON.parse(String(s));
35
+ },
36
+ stringify(v) {
37
+ return JSON.stringify(v);
38
+ },
39
+ };
40
+
41
+ // -------------------------
42
+ // Minimal server
43
+ // -------------------------
44
+ class TSSServer {
45
+ constructor() {
46
+ this.routes = { GET: [], POST: [] };
47
+ this.httpServer = null;
48
+ }
49
+
50
+ get(route, handler) {
51
+ this.routes.GET.push([route, handler]);
52
+ }
53
+
54
+ post(route, handler) {
55
+ this.routes.POST.push([route, handler]);
56
+ }
57
+
58
+ _match(method, pathname) {
59
+ const list = this.routes[method] || [];
60
+ for (const [r, h] of list) {
61
+ if (String(r) === pathname) return h;
62
+ }
63
+ return null;
64
+ }
65
+
66
+ async listen(port) {
67
+ port = Number(port) || 3000;
68
+
69
+ if (this.httpServer) throw new Error("server already running");
70
+
71
+ this.httpServer = http.createServer(async (req, res) => {
72
+ const parsed = url.parse(req.url, true);
73
+ const pathname = parsed.pathname;
74
+
75
+ // Request helper
76
+ const reqObj = {
77
+ method: req.method,
78
+ path: pathname,
79
+ query: parsed.query || {},
80
+ headers: req.headers,
81
+ body: null,
82
+ };
83
+
84
+ // Read body for POST/PUT
85
+ if (req.method === "POST" || req.method === "PUT") {
86
+ reqObj.body = await new Promise((resolve) => {
87
+ let data = "";
88
+ req.on("data", (chunk) => (data += chunk));
89
+ req.on("end", () => resolve(data));
90
+ });
91
+ }
92
+
93
+ // Response helper
94
+ const resObj = {
95
+ json(obj) {
96
+ const s = JSON.stringify(obj);
97
+ res.writeHead(200, { "Content-Type": "application/json" });
98
+ res.end(s);
99
+ },
100
+ text(txt) {
101
+ res.writeHead(200, { "Content-Type": "text/plain" });
102
+ res.end(String(txt));
103
+ },
104
+ status(code) {
105
+ res.statusCode = Number(code) || 200;
106
+ return resObj;
107
+ },
108
+ };
109
+
110
+ const handler = this._match(req.method, pathname);
111
+ if (!handler) {
112
+ res.writeHead(404, { "Content-Type": "text/plain" });
113
+ res.end("404 Not Found");
114
+ return;
115
+ }
116
+
117
+ try {
118
+ await handler(reqObj, resObj);
119
+ } catch (e) {
120
+ res.writeHead(500, { "Content-Type": "text/plain" });
121
+ res.end("Server Error: " + e.message);
122
+ }
123
+ });
124
+
125
+ await new Promise((resolve) => this.httpServer.listen(port, resolve));
126
+ console.log(`✅ TSS Server running on http://127.0.0.1:${port}`);
127
+ }
128
+ }
129
+
130
+ const server = new TSSServer();
131
+
132
+ module.exports = { sleep, run, file, json, server };