@ianlangs/mathscript 1.1.3 → 1.2.0
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/src/transpile.js +77 -10
- package/tests/using.js +1 -2
- package/tests/vars.js +2 -2
- package/tests/vars.ms +1 -1
package/package.json
CHANGED
package/src/transpile.js
CHANGED
|
@@ -1,16 +1,83 @@
|
|
|
1
1
|
const Consts = [
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
2
|
+
[/::\s*[^\s\(\)]*/g, ""],
|
|
3
|
+
[/mut\s+([a-zA-Z_]\w*)\s*=\s*(.+)/g, "let $1 = (()=>{let v=$2; return {get:()=>v,set:n=>v=n}})()"],
|
|
4
|
+
[/immut\s+([a-zA-Z_]\w*)/g, "delete $1.set"],
|
|
5
|
+
[/using\(/g, "require("],
|
|
6
|
+
[/fn/g, "function"],
|
|
7
7
|
]
|
|
8
8
|
|
|
9
|
-
function
|
|
10
|
-
|
|
11
|
-
|
|
9
|
+
function analyzeMS(code) {
|
|
10
|
+
const lines = code.split("\n")
|
|
11
|
+
const scopes = [new Map()] // nombre -> { mut: boolean, kind }
|
|
12
|
+
|
|
13
|
+
function findVar(name) {
|
|
14
|
+
for (let s of scopes) {
|
|
15
|
+
if (s.has(name)) return s.get(name)
|
|
16
|
+
}
|
|
17
|
+
return null
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
lines.forEach((line, i) => {
|
|
21
|
+
const ln = i + 1
|
|
22
|
+
const trimmed = line.trim()
|
|
23
|
+
|
|
24
|
+
if (trimmed.includes("{")) scopes.unshift(new Map())
|
|
25
|
+
if (trimmed.includes("}")) scopes.shift()
|
|
26
|
+
|
|
27
|
+
// mut x = ...
|
|
28
|
+
const mutMatch = line.match(/^\s*mut\s+([a-zA-Z_]\w*)/)
|
|
29
|
+
if (mutMatch) {
|
|
30
|
+
const name = mutMatch[1]
|
|
31
|
+
scopes[0].set(name, { mut: true, kind: "mut" })
|
|
32
|
+
return
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
// let x = ...
|
|
36
|
+
const letMatch = line.match(/^\s*let\s+([a-zA-Z_]\w*)/)
|
|
37
|
+
if (letMatch) {
|
|
38
|
+
const name = letMatch[1]
|
|
39
|
+
scopes[0].set(name, { mut: false, kind: "let" })
|
|
40
|
+
return
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
// const x = ...
|
|
44
|
+
const constMatch = line.match(/^\s*const\s+([a-zA-Z_]\w*)/)
|
|
45
|
+
if (constMatch) {
|
|
46
|
+
const name = constMatch[1]
|
|
47
|
+
scopes[0].set(name, { mut: false, kind: "const" })
|
|
48
|
+
return
|
|
12
49
|
}
|
|
13
|
-
|
|
50
|
+
|
|
51
|
+
// immut x
|
|
52
|
+
const im = line.match(/^\s*immut\s+([a-zA-Z_]\w*)/)
|
|
53
|
+
if (im) {
|
|
54
|
+
const name = im[1]
|
|
55
|
+
const v = findVar(name)
|
|
56
|
+
|
|
57
|
+
if (!v) {
|
|
58
|
+
throw new Error(`MS Compile Error (line ${ln}): '${name}' is not declared`)
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
if (!v.mut) {
|
|
62
|
+
throw new Error(
|
|
63
|
+
`MS Compile Error (line ${ln}): '${name}' is not mutable (declared as ${v.kind})`
|
|
64
|
+
)
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
v.mut = false
|
|
68
|
+
return
|
|
69
|
+
}
|
|
70
|
+
})
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
function transpile(code, ...consts) {
|
|
75
|
+
analyzeMS(code)
|
|
76
|
+
|
|
77
|
+
for (let [i, j] of consts) {
|
|
78
|
+
code = code.replace(i, j)
|
|
79
|
+
}
|
|
80
|
+
return code
|
|
14
81
|
}
|
|
15
82
|
|
|
16
|
-
export default {transpile, Consts}
|
|
83
|
+
export default { transpile, Consts }
|
package/tests/using.js
CHANGED
package/tests/vars.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
// Mutable
|
|
2
|
-
let x= (()=>{let v=5; return {get:()=>v,set:n=>v=n}})()
|
|
2
|
+
let x = (()=>{let v=5; return {get:()=>v,set:n=>v=n}})()
|
|
3
3
|
console.log(x.get()) // 5
|
|
4
4
|
x.set(10)
|
|
5
5
|
console.log(x.get()) // 10
|
|
@@ -15,7 +15,7 @@ try {
|
|
|
15
15
|
// Variable
|
|
16
16
|
let y = "hola"
|
|
17
17
|
console.log(y) // "hola"
|
|
18
|
-
delete y.set // no
|
|
18
|
+
// delete y.set // error no es mutable
|
|
19
19
|
|
|
20
20
|
|
|
21
21
|
// Const
|