-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathay.ts
208 lines (197 loc) · 6.41 KB
/
ay.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#!/usr/bin/env bun
import type { BunFile } from "bun";
import * as fs from "fs";
import { objectChecker } from "./error_handling/objectChCompiled";
const out = __dirname + "/out.js";
const programName = Bun.argv[2];
const bunfile = Bun.file(programName);
const program = await bunfile.text();
// this function breaks the whole program into lines
function parse(codes: any): string[] {
return codes.split("\n");
}
// thus function breaks a line into words by white space
function tokenize(code: string): string[] {
return code.split(/\s+/);
}
//this function is peak asf
function parser(inputString: string): string[] {
// Separate the input string into segments
const segments = inputString.match(/(["'`].*?["'`])|\S+/g);
if (!segments) return [];
const result = [];
for (const segment of segments) {
if (
segment.startsWith('"') ||
segment.startsWith("'") ||
segment.startsWith("`")
) {
// Quoted strings
result.push(segment);
} else {
// Split by parentheses, square brackets, braces, and operators
const tokens = segment
.split(/([()\[\]{}])/)
.filter((token) => token.trim() !== "");
// Combine adjacent parentheses, square brackets, or braces
let combinedToken = "";
for (const token of tokens) {
if (
token === "(" ||
token === "[" ||
token === "{" ||
token === ")" ||
token === "]" ||
token === "}"
) {
if (combinedToken !== "") {
result.push(combinedToken);
combinedToken = "";
}
result.push(token);
} else {
combinedToken += token;
}
}
// Push any remaining combined token
if (combinedToken !== "") {
result.push(combinedToken);
}
}
}
return result;
}
//this one different
function parseStr(inputString: string): string[] {
const regex = /(["'`])(.*?)\1|\S+/g;
const matches = inputString.match(regex);
if (matches) {
return matches;
} else {
return [];
}
}
// this function breaks a quote statement apart
function parseStatement(statement: any): string[] {
const regex = /"([^"]+)"|(\w+)|([=\[\]\(\){}÷*+\-])/g; // Matches either a quoted string or a word /("[^"]+"|\w+)/g
const matches = statement.match(regex);
return matches;
}
async function fileText(f: BunFile) {
let d = await f.text();
return d;
}
let exporters: string[] = [];
function generateCode(program: any) {
let code = "";
let lines = parse(program);
let newLines = lines.filter((line) => {
return line !== "\r";
});
if (newLines[0].includes("#")) {
newLines[0] = "";
}
newLines.forEach((el) => {
el.includes("{")
? (el += "")
: el.includes(";")
? (el += "")
: el.includes("}")
? (el += "")
: el.includes(",")
? (el += "")
: el.includes(":")
? (el += "")
: (el += " ;");
let values = parseStr(el);
if (
el.includes("for (") ||
el.includes("for(") ||
el.includes("if(") ||
el.includes("if (") ||
el.includes("exp@ f")
) {
values = parser(el);
}
values[values.length] = "\n";
for (let i = 0; i < values.length; i++) {
if (values[i] == "l") {
values[i] = "let";
}
if (values[i] == "print") {
values[i] = `console.log(${values[i + 1]})`;
values[i + 1] = " ";
}
if (values[i] == "f") {
values[i] = "function";
}
if (values[i] == "exp@") {
exporters.push(values[i + 2]); // exported variable name to list of exported in a file
values[i] = "";
}
if (values[i] == "imp@") {
// let impe = imp(values, values[i]);
// values[i] = impe
let impStatementLength = values.length;
// import for variable
let importForV = values[i + 1].slice(1, -1);
let endMark = impStatementLength - 2;
// import location
let importLocation = values[endMark];
let impLength = importLocation.length;
importLocation = importLocation.slice(1, impLength - 1);
let ayImport = fs.readFileSync(process.cwd() + importLocation, "utf-8");
let dFr = values[2];
// console.log(importForV,importLocation,values)
values[i] = "";
values[i + 1] = "";
values[2] = "";
values[endMark] = "";
if (importForV == importLocation) {
code += generateCode(ayImport);
} else {
//to do actually make sure the file isn't loaded and executed
let importsForV: string[] = [];
let tempCode = generateCode(ayImport);
tempCode += `module.exports = {${exporters}}\n`;
let condition: any;
if (importForV.split(",").length > 1) {
condition = exporters.some((r) => importForV.includes(r));
importsForV = importForV.split(",");
for (let i = 0; i < importsForV.length; i++) {
if (condition) {
importForV = importsForV.join(", ");
}
}
}
if (exporters.includes(importForV) || condition) {
const math = `const {rand, round, PI, floor, exp, degToRad, radToDeg} = require('./math')\n`;
const utils = `const {print, timer, Day, interval, read, write, appendFile, dirname} = require('./utils')\n`;
const AY = `const {AY} = require(__dirname +'/objects/AY');\n`;
const exec = ` ${math} ${utils} ${AY} try {\n${tempCode}}catch(e){\n console.error(e.message);\n}`;
const out2 = __dirname + "/out2.js";
fs.writeFileSync(out2, exec);
code += `const {${importForV}} = require("./out2.js")`;
}
if (!exporters.includes(importForV) && !condition) {
console.log("exports: ", exporters);
console.log("No exports found ", importForV);
process.exit(1);
}
}
}
}
code += values.join(" ");
});
return code;
}
const math = `import { rand, round, PI, floor, exp, degToRad, radToDeg } from './math';\n`;
const utils = `import { print, timer, Day, interval, read, write, appendFile, dirname } from './utils';\n`;
const m = "./objects" + "/" + "AY";
const AY = `import {AY} from '${m}'\n`;
const exec = ` ${AY} ${math} ${utils} try {\n${generateCode(
program
)}}catch(e){\n console.error(e.message);\n}`;
objectChecker(generateCode(program));
await Bun.write(out, exec);
await import(out);