simpleline 1.0.2
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/README.md +0 -0
- package/bin/simpleline.js +21 -0
- package/command.smplin +1 -0
- package/lib/index.js +222 -0
- package/package.json +13 -0
package/README.md
ADDED
|
File without changes
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
const fs = require("fs");
|
|
4
|
+
const path = require("path");
|
|
5
|
+
const sl = require("../lib/index");
|
|
6
|
+
|
|
7
|
+
const args = process.argv.slice(2);
|
|
8
|
+
|
|
9
|
+
if (args.length === 0) {
|
|
10
|
+
console.log("Usage: simpleline <file.smplin>");
|
|
11
|
+
process.exit(1);
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
const filePath = path.resolve(process.cwd(), args[0]);
|
|
15
|
+
|
|
16
|
+
try {
|
|
17
|
+
const code = fs.readFileSync(filePath, "utf-8");
|
|
18
|
+
sl.run(code);
|
|
19
|
+
} catch (err) {
|
|
20
|
+
console.error("Error reading file:", err.message);
|
|
21
|
+
}
|
package/command.smplin
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|Hello, World| > log
|
package/lib/index.js
ADDED
|
@@ -0,0 +1,222 @@
|
|
|
1
|
+
function run(code) {
|
|
2
|
+
runCode(code, { "PI": Math.PI, "E": Math.E })
|
|
3
|
+
function runCode(code, setVars) {
|
|
4
|
+
function decide(txt) {
|
|
5
|
+
if (txt.match(/\/.*\//)) txt = RegExp(txt.replaceAll("/", ""))
|
|
6
|
+
if (typeof (txt) == 'string') {
|
|
7
|
+
txt = txt.replaceAll("|", "\"")
|
|
8
|
+
if (txt.match(/^[0-9]+(\.[0-9]+)?$/g)) {
|
|
9
|
+
value = parseFloat(txt);
|
|
10
|
+
} else if (txt.match(/^\".*\"/)) {
|
|
11
|
+
value = txt.replaceAll("\"", "")
|
|
12
|
+
} else if (txt.match(/^\[.*\]/)) {
|
|
13
|
+
value = JSON.parse("[" + txt.replace("[", "").replace("]", "").split(", ").map((v) => variableReplace(v)) + "]")
|
|
14
|
+
} else if (txt.match(/.+( .*| (.* <? )+)?/g)) {
|
|
15
|
+
let splt = txt.split(" < ")
|
|
16
|
+
let first = splt[0].split(" ")
|
|
17
|
+
let firstFirst = first.shift()
|
|
18
|
+
let inp = first.join(" ")
|
|
19
|
+
value = fn(firstFirst, inp, value)
|
|
20
|
+
}
|
|
21
|
+
} else {
|
|
22
|
+
value = txt
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
function variableReplace(txt) {
|
|
26
|
+
if (vars[txt]) {
|
|
27
|
+
return vars[txt]
|
|
28
|
+
} else {
|
|
29
|
+
return txt
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
function fn(type, input, val) {
|
|
33
|
+
input = input.replaceAll("\"", "")
|
|
34
|
+
input = variableReplace(input)
|
|
35
|
+
if (type == "store") {
|
|
36
|
+
if (input != "") {
|
|
37
|
+
if (input.match(/\[.+\]/)) {
|
|
38
|
+
vars[variableReplace(input.toString().replaceAll(/|\[|\]/g, ""))] = val
|
|
39
|
+
} else {
|
|
40
|
+
vars[input] = val
|
|
41
|
+
}
|
|
42
|
+
} else {
|
|
43
|
+
vars[curVar] = val
|
|
44
|
+
}
|
|
45
|
+
return val
|
|
46
|
+
} else if (type == "+") {
|
|
47
|
+
if (typeof (val) == 'number') {
|
|
48
|
+
return val + parseFloat(input)
|
|
49
|
+
} else {
|
|
50
|
+
return val + input
|
|
51
|
+
}
|
|
52
|
+
} else if (type == "get_var") {
|
|
53
|
+
return variableReplace(val)
|
|
54
|
+
} else if (type == "-") {
|
|
55
|
+
return val - parseFloat(input)
|
|
56
|
+
} else if (type == "*") {
|
|
57
|
+
return val * parseFloat(input)
|
|
58
|
+
} else if (type == "/") {
|
|
59
|
+
return val / parseFloat(input)
|
|
60
|
+
} else if (type == "^") {
|
|
61
|
+
return Math.pow(val, parseFloat(input))
|
|
62
|
+
} else if (type == "=") {
|
|
63
|
+
if (val != null && val !== 'number') {
|
|
64
|
+
return val == input
|
|
65
|
+
} else {
|
|
66
|
+
return val == parseFloat(input)
|
|
67
|
+
}
|
|
68
|
+
} else if (type == "->") {
|
|
69
|
+
return val > parseFloat(input)
|
|
70
|
+
} else if (type == "<-") {
|
|
71
|
+
return val < parseFloat(input)
|
|
72
|
+
} else if (type == "=->") {
|
|
73
|
+
return val >= parseFloat(input)
|
|
74
|
+
} else if (type == "<-=") {
|
|
75
|
+
return val <= parseFloat(input)
|
|
76
|
+
} else if (type == "log") {
|
|
77
|
+
console.log(val)
|
|
78
|
+
return val
|
|
79
|
+
} else if (type == "?") {
|
|
80
|
+
shouldContinueLine = (val == true)
|
|
81
|
+
if (val == false) lastLineIsFalse = 2
|
|
82
|
+
return val
|
|
83
|
+
} else if (type == "??") {
|
|
84
|
+
if (lastLineIsFalse == 1) {
|
|
85
|
+
if (val != null) {
|
|
86
|
+
shouldContinueLine = (val == true)
|
|
87
|
+
if (val == false) lastLineIsFalse = 2
|
|
88
|
+
return val
|
|
89
|
+
} else {
|
|
90
|
+
shouldContinueLine = true
|
|
91
|
+
return true
|
|
92
|
+
}
|
|
93
|
+
} else {
|
|
94
|
+
shouldContinueLine = false
|
|
95
|
+
return false
|
|
96
|
+
}
|
|
97
|
+
} else if (type == "!") {
|
|
98
|
+
for (vars[input] = 0; vars[input] < val - 1; vars[input]++) {
|
|
99
|
+
run(repeatPattern, vars);
|
|
100
|
+
}
|
|
101
|
+
return vars[input]
|
|
102
|
+
} else if (type == "length") {
|
|
103
|
+
if (typeof (val) == 'object') {
|
|
104
|
+
return val.length
|
|
105
|
+
}
|
|
106
|
+
else {
|
|
107
|
+
return val
|
|
108
|
+
}
|
|
109
|
+
} else if (type == "round") {
|
|
110
|
+
return Math.round(val)
|
|
111
|
+
} else if (type == "ceil") {
|
|
112
|
+
return Math.ceil(val)
|
|
113
|
+
} else if (type == "floor") {
|
|
114
|
+
return Math.floor(val)
|
|
115
|
+
} else if (type == "trunc") {
|
|
116
|
+
return Math.trunc(val)
|
|
117
|
+
} else if (type == "abs") {
|
|
118
|
+
return Math.abs(val)
|
|
119
|
+
} else if (type == "sign") {
|
|
120
|
+
return Math.sign(val)
|
|
121
|
+
} else if (type == "exp") {
|
|
122
|
+
return Math.exp(val)
|
|
123
|
+
} else if (type == "logr") {
|
|
124
|
+
return Math.log(val)
|
|
125
|
+
} else if (type == "sin") {
|
|
126
|
+
return Math.sin(val)
|
|
127
|
+
} else if (type == "cos") {
|
|
128
|
+
return Math.cos(val)
|
|
129
|
+
} else if (type == "tan") {
|
|
130
|
+
return Math.tan(val)
|
|
131
|
+
} else if (type == "asin") {
|
|
132
|
+
return Math.asin(val)
|
|
133
|
+
} else if (type == "acos") {
|
|
134
|
+
return Math.acos(val)
|
|
135
|
+
} else if (type == "atan") {
|
|
136
|
+
return Math.atan(val)
|
|
137
|
+
} else if (type == "sinh") {
|
|
138
|
+
return Math.sinh(val)
|
|
139
|
+
} else if (type == "cosh") {
|
|
140
|
+
return Math.cosh(val)
|
|
141
|
+
} else if (type == "tanh") {
|
|
142
|
+
return Math.tanh(val)
|
|
143
|
+
} else if (type == "asinh") {
|
|
144
|
+
return Math.asinh(val)
|
|
145
|
+
} else if (type == "acosh") {
|
|
146
|
+
return Math.acosh(val)
|
|
147
|
+
} else if (type == "atanh") {
|
|
148
|
+
return Math.atanh(val)
|
|
149
|
+
} else if (type == "max") {
|
|
150
|
+
return Math.max(val, input)
|
|
151
|
+
} else if (type == "min") {
|
|
152
|
+
return Math.min(val, input)
|
|
153
|
+
} else if (type == "sqrt") {
|
|
154
|
+
return Math.sqrt(val)
|
|
155
|
+
} else if (type == "random") {
|
|
156
|
+
return Math.random() * (input - val) + val;
|
|
157
|
+
} else if (type == "match") {
|
|
158
|
+
return input.match(val) != null
|
|
159
|
+
} else if (type == "split") {
|
|
160
|
+
return val.split(input)
|
|
161
|
+
} else if (type.match(/.+\[[0-9]\]/g)) {
|
|
162
|
+
let varia = type.replace(/[\[0-9\]]/g, "")
|
|
163
|
+
return vars[varia][parseInt(type.replace(/[^0-9]/g, ""))]
|
|
164
|
+
} else if (type.match(/.+\[.+\]/g)) {
|
|
165
|
+
let varia = type.replace(/\[.+\]/g, "")
|
|
166
|
+
return vars[varia][vars[type.replace(/.+\[/g, "").replace(/\]/g, "")]]
|
|
167
|
+
} else if (vars[type] != null) {
|
|
168
|
+
curVar = type;
|
|
169
|
+
return vars[type]
|
|
170
|
+
} else {
|
|
171
|
+
return val
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
/**
|
|
175
|
+
* Get all indexes of a specific character in a string
|
|
176
|
+
* @param {string} str - The string to search
|
|
177
|
+
* @param {string} char - The character to find (must be a single character)
|
|
178
|
+
* @returns {number[]} - Array of indexes where the character occurs
|
|
179
|
+
*/
|
|
180
|
+
function getAllIndexes(str, char) {
|
|
181
|
+
// Input validation
|
|
182
|
+
if (typeof str !== 'string' || typeof char !== 'string') {
|
|
183
|
+
throw new TypeError('Both arguments must be strings.');
|
|
184
|
+
}
|
|
185
|
+
if (char.length !== 1) {
|
|
186
|
+
throw new Error('The search character must be exactly one character long.');
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
const indexes = [];
|
|
190
|
+
for (let i = 0; i < str.length; i++) {
|
|
191
|
+
if (str[i] === char) {
|
|
192
|
+
indexes.push(i);
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
return indexes;
|
|
196
|
+
}
|
|
197
|
+
let currentLine = "";
|
|
198
|
+
let repeatPattern = "";
|
|
199
|
+
let vars = setVars
|
|
200
|
+
let value = null;
|
|
201
|
+
let curVar = "";
|
|
202
|
+
let shouldContinueLine = true;
|
|
203
|
+
let lastLineIsFalse = 0;
|
|
204
|
+
code.split("\n").map((v) => v.trim()).forEach((s, i) => {
|
|
205
|
+
shouldContinueLine = true;
|
|
206
|
+
if (lastLineIsFalse > 0) lastLineIsFalse -= 1;
|
|
207
|
+
value = null
|
|
208
|
+
currentLine = s.replace(/^.*! [A-Za-z]+ >|> \?!.*$/g, '').trim();
|
|
209
|
+
repeatPattern = currentLine.replace(/^.*! [A-Za-z]+ >|> \?!.*$/g, '').trim()
|
|
210
|
+
s.split(" > ").map((v) => v.trim()).forEach((h) => {
|
|
211
|
+
if (h == "?!") shouldContinueLine = true;
|
|
212
|
+
currentLine = currentLine.replace(h + " > ", "")
|
|
213
|
+
repeatPattern = currentLine.replace(/^.*! [A-Za-z]+ >|> \?!.*$/g, '').trim()
|
|
214
|
+
if (shouldContinueLine) decide(h, vars)
|
|
215
|
+
})
|
|
216
|
+
})
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
module.exports = {
|
|
221
|
+
run
|
|
222
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "simpleline",
|
|
3
|
+
"version": "1.0.2",
|
|
4
|
+
"description": "Simple Line programming language",
|
|
5
|
+
"main": "lib/index.js",
|
|
6
|
+
"bin": {
|
|
7
|
+
"simpleline": "./bin/simpleline.js"
|
|
8
|
+
},
|
|
9
|
+
"type": "commonjs",
|
|
10
|
+
"keywords": [ "language", "programming", "simpleline" ],
|
|
11
|
+
"author": "ecnryptionStudios",
|
|
12
|
+
"license": "MIT"
|
|
13
|
+
}
|