@ianlangs/mathscript 1.6.3 → 1.7.1
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 +2 -7
- package/package.json +1 -1
- package/src/transpile.js +41 -50
- package/src/web-t.js +58 -38
- package/tests/funcs.js +3 -3
- package/tests/funcs.ms +1 -1
- package/tests/using.js +1 -1
- package/tests/vars.js +3 -12
- package/tests/vars.ms +3 -8
package/README.md
CHANGED
|
@@ -43,12 +43,7 @@ function f(x) {
|
|
|
43
43
|
var x1 = value
|
|
44
44
|
let x2 = value
|
|
45
45
|
const x3 = value
|
|
46
|
-
mut x4 = value
|
|
47
46
|
|
|
48
|
-
//luego
|
|
49
|
-
immut
|
|
47
|
+
//luego, solo con let
|
|
48
|
+
immut x2
|
|
50
49
|
```
|
|
51
|
-
|
|
52
|
-
### tipado
|
|
53
|
-
|
|
54
|
-
el tipado es opcional, y solo es notacion, porque ms es tipado y no compilado
|
package/package.json
CHANGED
package/src/transpile.js
CHANGED
|
@@ -1,93 +1,84 @@
|
|
|
1
|
+
// transpile.js
|
|
1
2
|
export function transpile(code, filename = "<input>") {
|
|
2
|
-
|
|
3
|
-
const
|
|
4
|
-
const
|
|
5
|
-
const output = []
|
|
3
|
+
const lines = code.split('\n');
|
|
4
|
+
const vars = new Map();
|
|
5
|
+
const output = [];
|
|
6
6
|
|
|
7
7
|
function error(line, msg) {
|
|
8
|
-
console.error(`\n[MS ERROR] ${filename}:${line}\n ${msg}\n`)
|
|
9
|
-
process.exit(1)
|
|
8
|
+
console.error(`\n[MS ERROR] ${filename}:${line}\n ${msg}\n`);
|
|
9
|
+
process.exit(1);
|
|
10
10
|
}
|
|
11
11
|
|
|
12
12
|
for (let i = 0; i < lines.length; i++) {
|
|
13
|
-
const raw = lines[i]
|
|
14
|
-
const line = raw.trim()
|
|
15
|
-
const ln = i + 1
|
|
13
|
+
const raw = lines[i];
|
|
14
|
+
const line = raw.trim();
|
|
15
|
+
const ln = i + 1;
|
|
16
16
|
|
|
17
17
|
if (!line) {
|
|
18
|
-
output.push(raw)
|
|
19
|
-
continue
|
|
18
|
+
output.push(raw);
|
|
19
|
+
continue;
|
|
20
20
|
}
|
|
21
21
|
|
|
22
|
-
//
|
|
23
|
-
let m = line.match(/^
|
|
22
|
+
// let x = value
|
|
23
|
+
let m = line.match(/^let\s+([a-zA-Z_]\w*)\s*=\s*(.+)$/);
|
|
24
24
|
if (m) {
|
|
25
|
-
const name = m[1]
|
|
26
|
-
const value = m[2]
|
|
25
|
+
const name = m[1];
|
|
26
|
+
const value = m[2];
|
|
27
27
|
|
|
28
28
|
if (vars.has(name)) {
|
|
29
|
-
error(ln, `'${name}' ya está declarada`)
|
|
29
|
+
error(ln, `'${name}' ya está declarada`);
|
|
30
30
|
}
|
|
31
31
|
|
|
32
|
-
vars.set(name, { mutable: true })
|
|
33
|
-
output.push(
|
|
34
|
-
continue
|
|
32
|
+
vars.set(name, { mutable: true });
|
|
33
|
+
output.push(raw);
|
|
34
|
+
continue;
|
|
35
35
|
}
|
|
36
36
|
|
|
37
37
|
// immut x
|
|
38
|
-
m = line.match(/^immut\s+([a-zA-Z_]\w*)$/)
|
|
38
|
+
m = line.match(/^immut\s+([a-zA-Z_]\w*)$/);
|
|
39
39
|
if (m) {
|
|
40
|
-
const name = m[1]
|
|
40
|
+
const name = m[1];
|
|
41
41
|
|
|
42
42
|
if (!vars.has(name)) {
|
|
43
|
-
error(ln, `'${name}' no está declarada`)
|
|
43
|
+
error(ln, `'${name}' no está declarada`);
|
|
44
44
|
}
|
|
45
45
|
|
|
46
|
-
const info = vars.get(name)
|
|
46
|
+
const info = vars.get(name);
|
|
47
47
|
if (!info.mutable) {
|
|
48
|
-
error(ln, `'${name}' no es mutable`)
|
|
48
|
+
error(ln, `'${name}' no es mutable`);
|
|
49
49
|
}
|
|
50
50
|
|
|
51
|
-
info.mutable = false
|
|
52
|
-
continue
|
|
51
|
+
info.mutable = false;
|
|
52
|
+
continue;
|
|
53
53
|
}
|
|
54
54
|
|
|
55
55
|
// x = y
|
|
56
|
-
m = line.match(/^([a-zA-Z_]\w*)\s*=\s*(.+)$/)
|
|
56
|
+
m = line.match(/^([a-zA-Z_]\w*)\s*=\s*(.+)$/);
|
|
57
57
|
if (m) {
|
|
58
|
-
const name = m[1]
|
|
59
|
-
|
|
58
|
+
const name = m[1];
|
|
60
59
|
if (vars.has(name)) {
|
|
61
|
-
const info = vars.get(name)
|
|
60
|
+
const info = vars.get(name);
|
|
62
61
|
if (!info.mutable) {
|
|
63
|
-
error(ln, `'${name}' es inmutable`)
|
|
62
|
+
error(ln, `'${name}' es inmutable`);
|
|
64
63
|
}
|
|
65
64
|
}
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
continue
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
// let x =
|
|
72
|
-
m = line.match(/^let\s+([a-zA-Z_]\w*)\s*=/)
|
|
73
|
-
if (m) {
|
|
74
|
-
vars.set(m[1], { mutable: true })
|
|
75
|
-
output.push(raw)
|
|
76
|
-
continue
|
|
65
|
+
output.push(raw);
|
|
66
|
+
continue;
|
|
77
67
|
}
|
|
78
68
|
|
|
79
|
-
// const x =
|
|
80
|
-
m = line.match(/^const\s+([a-zA-Z_]\w*)\s*=/)
|
|
69
|
+
// const x = ...
|
|
70
|
+
m = line.match(/^const\s+([a-zA-Z_]\w*)\s*=/);
|
|
81
71
|
if (m) {
|
|
82
|
-
vars.set(m[1], { mutable: false })
|
|
83
|
-
output.push(raw)
|
|
84
|
-
continue
|
|
72
|
+
vars.set(m[1], { mutable: false });
|
|
73
|
+
output.push(raw);
|
|
74
|
+
continue;
|
|
85
75
|
}
|
|
86
76
|
|
|
87
|
-
|
|
77
|
+
// fn -> function, usin -> require
|
|
78
|
+
output.push(line.replace(/\bfn\b/g, "function").replace("using(", "require("));
|
|
88
79
|
}
|
|
89
80
|
|
|
90
|
-
return output.join('\n')
|
|
81
|
+
return output.join('\n');
|
|
91
82
|
}
|
|
92
83
|
|
|
93
|
-
export default { transpile }
|
|
84
|
+
export default { transpile };
|
package/src/web-t.js
CHANGED
|
@@ -1,43 +1,63 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
const
|
|
4
|
-
const
|
|
5
|
-
const
|
|
6
|
-
|
|
7
|
-
function
|
|
8
|
-
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
function logToConsole(msg) {
|
|
12
|
-
const line = document.createElement("div")
|
|
13
|
-
line.textContent = msg
|
|
14
|
-
consoleDiv.appendChild(line)
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
runButton.addEventListener("click", () => {
|
|
18
|
-
clearConsole()
|
|
19
|
-
|
|
20
|
-
const code = editor.value
|
|
21
|
-
|
|
22
|
-
const result = transpile(code, "playground.ms")
|
|
23
|
-
|
|
24
|
-
if (result.errors.length > 0) {
|
|
25
|
-
result.errors.forEach(err => logToConsole(err))
|
|
26
|
-
return
|
|
1
|
+
// web-transpile.js
|
|
2
|
+
export function transpileWeb(code, filename = "<input>") {
|
|
3
|
+
const lines = code.split('\n');
|
|
4
|
+
const vars = new Map();
|
|
5
|
+
const output = [];
|
|
6
|
+
|
|
7
|
+
function error(line, msg) {
|
|
8
|
+
throw new Error(`[MS ERROR] ${filename}:${line}\n ${msg}`);
|
|
27
9
|
}
|
|
28
10
|
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
11
|
+
for (let i = 0; i < lines.length; i++) {
|
|
12
|
+
const raw = lines[i];
|
|
13
|
+
const line = raw.trim();
|
|
14
|
+
const ln = i + 1;
|
|
15
|
+
|
|
16
|
+
if (!line) {
|
|
17
|
+
output.push(raw);
|
|
18
|
+
continue;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
let m = line.match(/^let\s+([a-zA-Z_]\w*)\s*=\s*(.+)$/);
|
|
22
|
+
if (m) {
|
|
23
|
+
const name = m[1];
|
|
24
|
+
if (vars.has(name)) error(ln, `'${name}' ya está declarada`);
|
|
25
|
+
vars.set(name, { mutable: true });
|
|
26
|
+
output.push(raw);
|
|
27
|
+
continue;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
m = line.match(/^immut\s+([a-zA-Z_]\w*)$/);
|
|
31
|
+
if (m) {
|
|
32
|
+
const name = m[1];
|
|
33
|
+
if (!vars.has(name)) error(ln, `'${name}' no está declarada`);
|
|
34
|
+
const info = vars.get(name);
|
|
35
|
+
if (!info.mutable) error(ln, `'${name}' no es mutable`);
|
|
36
|
+
info.mutable = false;
|
|
37
|
+
continue;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
m = line.match(/^([a-zA-Z_]\w*)\s*=\s*(.+)$/);
|
|
41
|
+
if (m) {
|
|
42
|
+
const name = m[1];
|
|
43
|
+
if (vars.has(name) && !vars.get(name).mutable) {
|
|
44
|
+
error(ln, `'${name}' es inmutable`);
|
|
45
|
+
}
|
|
46
|
+
output.push(raw);
|
|
47
|
+
continue;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
m = line.match(/^const\s+([a-zA-Z_]\w*)\s*=/);
|
|
51
|
+
if (m) {
|
|
52
|
+
vars.set(m[1], { mutable: false });
|
|
53
|
+
output.push(raw);
|
|
54
|
+
continue;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
output.push(line.replace(/\bfn\b/g, "function"));
|
|
34
58
|
}
|
|
35
59
|
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
} catch (e) {
|
|
39
|
-
logToConsole("[RUNTIME ERROR] " + e.message)
|
|
40
|
-
}
|
|
60
|
+
return output.join('\n');
|
|
61
|
+
}
|
|
41
62
|
|
|
42
|
-
|
|
43
|
-
})
|
|
63
|
+
export default { transpileWeb };
|
package/tests/funcs.js
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
function suma(a, b) {
|
|
2
|
-
|
|
2
|
+
return a + b
|
|
3
3
|
}
|
|
4
4
|
|
|
5
5
|
console.log(suma(3, 4)) // 7
|
|
6
6
|
|
|
7
|
-
function saludo(nombre
|
|
8
|
-
|
|
7
|
+
function saludo(nombre) {
|
|
8
|
+
return "Hola " + nombre
|
|
9
9
|
}
|
|
10
10
|
|
|
11
11
|
console.log(saludo("Ian")) // "Hola Ian"
|
package/tests/funcs.ms
CHANGED
package/tests/using.js
CHANGED
package/tests/vars.js
CHANGED
|
@@ -5,18 +5,9 @@ x = 10
|
|
|
5
5
|
console.log(x) // 10
|
|
6
6
|
|
|
7
7
|
// Inmutable
|
|
8
|
-
|
|
9
|
-
//x = 20 // debería dar error
|
|
10
|
-
} catch(e) {
|
|
11
|
-
console.log("error mutabilidad") // esperado
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
// Variable
|
|
15
|
-
let y = "hola"
|
|
16
|
-
console.log(y) // "hola"
|
|
17
|
-
// immut y // error no es mutable
|
|
18
|
-
|
|
8
|
+
//x = 20 // error
|
|
19
9
|
|
|
20
10
|
// Const
|
|
21
11
|
const z = 2
|
|
22
|
-
// z = 3 //error
|
|
12
|
+
// z = 3 //error
|
|
13
|
+
// immut z // error
|
package/tests/vars.ms
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
// Mutable
|
|
2
|
-
|
|
2
|
+
let x = 5
|
|
3
3
|
console.log(x) // 5
|
|
4
4
|
x = 10
|
|
5
5
|
console.log(x) // 10
|
|
@@ -8,12 +8,7 @@ console.log(x) // 10
|
|
|
8
8
|
immut x
|
|
9
9
|
//x = 20 // error
|
|
10
10
|
|
|
11
|
-
// Variable
|
|
12
|
-
let y = "hola"
|
|
13
|
-
console.log(y) // "hola"
|
|
14
|
-
// immut y // error no es mutable
|
|
15
|
-
|
|
16
|
-
|
|
17
11
|
// Const
|
|
18
12
|
const z = 2
|
|
19
|
-
// z = 3 //error
|
|
13
|
+
// z = 3 //error
|
|
14
|
+
// immut z // error
|