tsslang 1.0.2 → 1.0.3
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.
- package/package.json +1 -1
- package/tss.js +218 -1
package/package.json
CHANGED
package/tss.js
CHANGED
|
@@ -1,2 +1,219 @@
|
|
|
1
|
+
cat > tss.js <<'EOF'
|
|
1
2
|
#!/usr/bin/env node
|
|
2
|
-
|
|
3
|
+
|
|
4
|
+
const fs = require("fs");
|
|
5
|
+
const path = require("path");
|
|
6
|
+
const { execSync } = require("child_process");
|
|
7
|
+
|
|
8
|
+
const file = process.argv[2];
|
|
9
|
+
if (!file) {
|
|
10
|
+
console.log("Use: tss <file.tss>");
|
|
11
|
+
process.exit(1);
|
|
12
|
+
}
|
|
13
|
+
const code = fs.readFileSync(file, "utf8");
|
|
14
|
+
|
|
15
|
+
function stripComments(line) {
|
|
16
|
+
const i = line.indexOf("#");
|
|
17
|
+
if (i >= 0) return line.slice(0, i);
|
|
18
|
+
return line;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function exprToJS(expr) {
|
|
22
|
+
expr = expr.trim();
|
|
23
|
+
|
|
24
|
+
expr = expr.replace(/\btrue\b/g, "true");
|
|
25
|
+
expr = expr.replace(/\bfalse\b/g, "false");
|
|
26
|
+
expr = expr.replace(/\bnull\b/g, "null");
|
|
27
|
+
|
|
28
|
+
expr = expr.replace(/\band\b/g, "&&");
|
|
29
|
+
expr = expr.replace(/\bor\b/g, "||");
|
|
30
|
+
expr = expr.replace(/\bnot\b/g, "!");
|
|
31
|
+
|
|
32
|
+
expr = expr.replace(/\bjson\.parse\b/g, "__tss.json.parse");
|
|
33
|
+
expr = expr.replace(/\bjson\.stringify\b/g, "__tss.json.stringify");
|
|
34
|
+
|
|
35
|
+
expr = expr.replace(/\bfile\.read\b/g, "__tss.file.read");
|
|
36
|
+
expr = expr.replace(/\bfile\.write\b/g, "__tss.file.write");
|
|
37
|
+
|
|
38
|
+
expr = expr.replace(/\bserver\./g, "__tss.server.");
|
|
39
|
+
|
|
40
|
+
return expr;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function compileTSS(source) {
|
|
44
|
+
const lines = source.split("\n");
|
|
45
|
+
|
|
46
|
+
let js = "";
|
|
47
|
+
let indent = 0;
|
|
48
|
+
|
|
49
|
+
function emit(line) {
|
|
50
|
+
js += " ".repeat(indent) + line + "\n";
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
function inc() {
|
|
54
|
+
indent++;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function dec() {
|
|
58
|
+
indent = Math.max(0, indent - 1);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
emit(`"use strict";`);
|
|
62
|
+
emit(`const __tss = require("./tss_runtime");`);
|
|
63
|
+
emit(`(async () => {`);
|
|
64
|
+
inc();
|
|
65
|
+
|
|
66
|
+
for (let index = 0; index < lines.length; index++) {
|
|
67
|
+
const rawLine = lines[index];
|
|
68
|
+
let line = stripComments(rawLine).trim();
|
|
69
|
+
if (!line) continue;
|
|
70
|
+
|
|
71
|
+
const closeElseInline = line.match(/^\}\s*else\s*\{$/);
|
|
72
|
+
if (closeElseInline) {
|
|
73
|
+
dec();
|
|
74
|
+
emit(`} else {`);
|
|
75
|
+
inc();
|
|
76
|
+
continue;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
if (line === "else {") {
|
|
80
|
+
dec();
|
|
81
|
+
emit(`} else {`);
|
|
82
|
+
inc();
|
|
83
|
+
continue;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
if (line === "}") {
|
|
87
|
+
dec();
|
|
88
|
+
emit("}");
|
|
89
|
+
continue;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
if (line.startsWith("print ")) {
|
|
93
|
+
const rest = line.slice(6);
|
|
94
|
+
emit(`console.log(${exprToJS(rest)});`);
|
|
95
|
+
continue;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
if (line.startsWith("let ")) {
|
|
99
|
+
emit(`let ${exprToJS(line.slice(4))};`);
|
|
100
|
+
continue;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
if (/^[A-Za-z_]\w*\s*=/.test(line)) {
|
|
104
|
+
emit(`${exprToJS(line)};`);
|
|
105
|
+
continue;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
if (line.startsWith("if ")) {
|
|
109
|
+
const m = line.match(/^if\s+(.+)\s*\{$/);
|
|
110
|
+
if (!m) throw new Error(`Syntax error (if) at line ${index + 1}`);
|
|
111
|
+
emit(`if (${exprToJS(m[1])}) {`);
|
|
112
|
+
inc();
|
|
113
|
+
continue;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
if (line.startsWith("while ")) {
|
|
117
|
+
const m = line.match(/^while\s+(.+)\s*\{$/);
|
|
118
|
+
if (!m) throw new Error(`Syntax error (while) at line ${index + 1}`);
|
|
119
|
+
emit(`while (${exprToJS(m[1])}) {`);
|
|
120
|
+
inc();
|
|
121
|
+
continue;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
if (line.startsWith("for ")) {
|
|
125
|
+
const m = line.match(/^for\s+([A-Za-z_]\w*)\s+in\s+(.+)\.\.(.+)\s*\{$/);
|
|
126
|
+
if (!m) throw new Error(`Syntax error (for) at line ${index + 1}`);
|
|
127
|
+
const varName = m[1];
|
|
128
|
+
const start = exprToJS(m[2]);
|
|
129
|
+
const end = exprToJS(m[3]);
|
|
130
|
+
emit(`for (let ${varName} = (${start}); ${varName} <= (${end}); ${varName}++) {`);
|
|
131
|
+
inc();
|
|
132
|
+
continue;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
if (line.startsWith("fn ")) {
|
|
136
|
+
const m = line.match(/^fn\s+([A-Za-z_]\w*)\s*\((.*?)\)\s*\{$/);
|
|
137
|
+
if (!m) throw new Error(`Syntax error (fn) at line ${index + 1}`);
|
|
138
|
+
const name = m[1];
|
|
139
|
+
const args = m[2].trim();
|
|
140
|
+
emit(`function ${name}(${args}) {`);
|
|
141
|
+
inc();
|
|
142
|
+
continue;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
if (line.startsWith("return ")) {
|
|
146
|
+
emit(`return ${exprToJS(line.slice(7))};`);
|
|
147
|
+
continue;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
if (line.startsWith("run ")) {
|
|
151
|
+
const rest = line.slice(4).trim();
|
|
152
|
+
emit(`await __tss.run(${exprToJS(rest)});`);
|
|
153
|
+
continue;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
if (line.startsWith("sleep ")) {
|
|
157
|
+
const rest = line.slice(6).trim();
|
|
158
|
+
emit(`await __tss.sleep(${exprToJS(rest)});`);
|
|
159
|
+
continue;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
if (line.startsWith("server.get ")) {
|
|
163
|
+
const m = line.match(/^server\.get\s+(.+)\s*\{$/);
|
|
164
|
+
if (!m) throw new Error(`Syntax error (server.get) at line ${index + 1}`);
|
|
165
|
+
const route = exprToJS(m[1]);
|
|
166
|
+
emit(`__tss.server.get(${route}, async (req, res) => {`);
|
|
167
|
+
inc();
|
|
168
|
+
continue;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
if (line.startsWith("server.post ")) {
|
|
172
|
+
const m = line.match(/^server\.post\s+(.+)\s*\{$/);
|
|
173
|
+
if (!m) throw new Error(`Syntax error (server.post) at line ${index + 1}`);
|
|
174
|
+
const route = exprToJS(m[1]);
|
|
175
|
+
emit(`__tss.server.post(${route}, async (req, res) => {`);
|
|
176
|
+
inc();
|
|
177
|
+
continue;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
if (line.startsWith("listen ")) {
|
|
181
|
+
const port = exprToJS(line.slice(7));
|
|
182
|
+
emit(`await __tss.server.listen(${port});`);
|
|
183
|
+
continue;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
if (line.startsWith("res.json ")) {
|
|
187
|
+
emit(`res.json(${exprToJS(line.slice(9))});`);
|
|
188
|
+
continue;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
if (line.startsWith("res.text ")) {
|
|
192
|
+
emit(`res.text(${exprToJS(line.slice(9))});`);
|
|
193
|
+
continue;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
if (line.startsWith("js:")) {
|
|
197
|
+
emit(line.slice(3).trim());
|
|
198
|
+
continue;
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
throw new Error(`Unknown command at line ${index + 1}: ${rawLine.trim()}`);
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
dec();
|
|
205
|
+
emit(`})();`);
|
|
206
|
+
|
|
207
|
+
return js;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
try {
|
|
211
|
+
const outJS = compileTSS(code);
|
|
212
|
+
const outPath = path.join(process.cwd(), ".tss_out.js");
|
|
213
|
+
fs.writeFileSync(outPath, outJS, "utf8");
|
|
214
|
+
execSync(`node "${outPath}"`, { stdio: "inherit" });
|
|
215
|
+
} catch (e) {
|
|
216
|
+
console.error("TSS Error:", e.message);
|
|
217
|
+
process.exit(1);
|
|
218
|
+
}
|
|
219
|
+
EOF
|