@ianlangs/mathscript 1.0.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/README.md ADDED
@@ -0,0 +1,48 @@
1
+ # MathScript
2
+
3
+ ## Uso
4
+
5
+ msc --init <file list>
6
+
7
+ msc msconfig.json # o solo msc
8
+
9
+ ## Configuración
10
+
11
+ - msc --init <file list> -> genera msconfig.json
12
+ - Files -> archivos a traducir
13
+
14
+ ## Sintaxis
15
+
16
+ ### Funciones
17
+
18
+ ```ms
19
+ using("module")
20
+ fn f(x) {
21
+ return x
22
+ }
23
+ ```
24
+
25
+ compila a:
26
+
27
+ ```js
28
+ require("module")
29
+ function f(x) {
30
+ return x
31
+ }
32
+ ```
33
+
34
+ ### variables
35
+
36
+ ```ms
37
+ var x1 = value
38
+ let x2 = value
39
+ const x3 = value
40
+ mut x4 = value
41
+
42
+ //luego
43
+ immut x4
44
+ ```
45
+
46
+ ### tipado
47
+
48
+ el tipado es opcional, y solo es notacion, porque ms es tipado y no compilado
package/package.json ADDED
@@ -0,0 +1,31 @@
1
+ {
2
+ "type": "module",
3
+ "name": "@ianlangs/mathscript",
4
+ "version": "1.0.0",
5
+ "description": "superset de js",
6
+ "main": "src/main.js",
7
+ "scripts": {
8
+ "test": "node ./src/main.js --init"
9
+ },
10
+ "repository": {
11
+ "type": "git",
12
+ "url": "git+https://github.com/IanLangs/Mathscript.git"
13
+ },
14
+ "keywords": [
15
+ "ms",
16
+ "msc",
17
+ "math",
18
+ "superset",
19
+ "mathscript"
20
+ ],
21
+ "author": "ianlangs",
22
+ "license": "MIT",
23
+
24
+ "bugs": {
25
+ "url": "https://github.com/IanLangs/Mathscript/issues"
26
+ },
27
+ "bin": {
28
+ "msc": "./src/main.js"
29
+ },
30
+ "homepage": "https://github.com/IanLangs/Mathscript#readme"
31
+ }
package/src/main.js ADDED
@@ -0,0 +1,29 @@
1
+ #!/bin/env node
2
+ import T from './transpile.js'
3
+ import fs from 'fs'
4
+
5
+ function processConfig(config) {
6
+ for (let file of config.Files) {
7
+ try {
8
+ let code = fs.readFileSync(file, "utf-8")
9
+ code = T.transpile(code, ...(T.Consts))
10
+ fs.writeFileSync(file.replace(/\.ms$/g, ".js"), code, 'utf-8')
11
+ } catch(e) {
12
+ console.error(`Error procesando ${file}:`, e.message)
13
+ }
14
+ }
15
+ }
16
+
17
+ if(process.argv[2] == "-i" || process.argv[2] == "--init") {
18
+ let config = {Files:[]}
19
+ config.Files = process.argv.slice(3)
20
+ fs.writeFileSync("msconfig.json", JSON.stringify(config, ["Files"], 4))
21
+ process.exit(0)
22
+ } else if (process.argv[2] == "-t" || process.argv[2] == "--transpile") {
23
+ let config = {Files:process.argv.slice(3)}
24
+ processConfig(config)
25
+ } else if (process.argv.length == 2) {
26
+ processConfig(JSON.parse(fs.readFileSync("msconfig.json", 'utf-8')))
27
+ } else if (process.argv.length == 3) {
28
+ processConfig(JSON.parse(fs.readFileSync(process.argv[2], 'utf-8')))
29
+ }
@@ -0,0 +1,16 @@
1
+ const Consts = [
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
+ ]
8
+
9
+ function transpile(code, ...consts) {
10
+ for (let [i, j] of consts) {
11
+ code = code.replace(i, j)
12
+ }
13
+ return code
14
+ }
15
+
16
+ export default {transpile, Consts}
package/tests/funcs.js ADDED
@@ -0,0 +1,11 @@
1
+ function suma(a, b) {
2
+ return a + b
3
+ }
4
+
5
+ console.log(suma(3, 4)) // 7
6
+
7
+ function saludo(nombre) {
8
+ return "Hola " + nombre
9
+ }
10
+
11
+ console.log(saludo("Ian")) // "Hola Ian"
package/tests/funcs.ms ADDED
@@ -0,0 +1,11 @@
1
+ fn suma(a, b) {
2
+ return a + b
3
+ }
4
+
5
+ console.log(suma(3, 4)) // 7
6
+
7
+ fn saludo(nombre::string) {
8
+ return "Hola " + nombre
9
+ }
10
+
11
+ console.log(saludo("Ian")) // "Hola Ian"
@@ -0,0 +1,7 @@
1
+ {
2
+ "Files": [
3
+ "vars.ms",
4
+ "using.ms",
5
+ "funcs.ms"
6
+ ]
7
+ }
package/tests/using.js ADDED
@@ -0,0 +1,4 @@
1
+ //const fs = require('fs') inea borrada para poder subir all modulo
2
+ import fs from 'fs' //linea parra que siga andando
3
+ const files = fs.readdirSync('.')
4
+ console.log(files.length) // número de archivos en la carpeta
package/tests/using.ms ADDED
@@ -0,0 +1,3 @@
1
+ const fs = using('fs')
2
+ const files = fs.readdirSync('.')
3
+ console.log(files.length) // número de archivos en la carpeta
package/tests/vars.js ADDED
@@ -0,0 +1,23 @@
1
+ // Mutable
2
+ let x= (()=>{let v=5; return {get:()=>v,set:n=>v=n}})()
3
+ console.log(x.get()) // 5
4
+ x.set(10)
5
+ console.log(x.get()) // 10
6
+
7
+ // Inmutable
8
+ delete x.set
9
+ try {
10
+ x.set(20) // debería dar error
11
+ } catch(e) {
12
+ console.log("error mutabilidad") // esperado
13
+ }
14
+
15
+ // Variable
16
+ let y = "hola"
17
+ console.log(y) // "hola"
18
+ delete y.set // no cambia nada
19
+
20
+
21
+ // Const
22
+ const z = 2
23
+ // z = 3 //error
package/tests/vars.ms ADDED
@@ -0,0 +1,23 @@
1
+ // Mutable
2
+ mut x::number = 5
3
+ console.log(x.get()) // 5
4
+ x.set(10)
5
+ console.log(x.get()) // 10
6
+
7
+ // Inmutable
8
+ immut x
9
+ try {
10
+ x.set(20) // debería dar error
11
+ } catch(e) {
12
+ console.log("error mutabilidad") // esperado
13
+ }
14
+
15
+ // Variable
16
+ let y = "hola"
17
+ console.log(y) // "hola"
18
+ immut y // no cambia nada
19
+
20
+
21
+ // Const
22
+ const z = 2
23
+ // z = 3 //error