Skip to content

Commit

Permalink
Add NodeJS compatibility & mod function
Browse files Browse the repository at this point in the history
  • Loading branch information
GulgDev committed Jun 13, 2024
1 parent b01dd8c commit 03acb2b
Show file tree
Hide file tree
Showing 15 changed files with 316 additions and 37 deletions.
16 changes: 13 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# SigmaScript
![Logo](demo/logo.png)
![Logo](https://gulgdev.github.io/SigmaScript/demo/logo.png)

Programming language made for sigma males.

Expand Down Expand Up @@ -88,7 +88,7 @@ print "lambda = " @ lambda;
```

### Libraries
There's 7 default libs in SigmaScript: `js`, `dom`, `fn`, `ref`, `string`, `struct`, `array`, `math`. To use a lib in your program simply add `use` header:
There's 8 default libs in SigmaScript: `js`, `dom`, `fn`, `ref`, `string`, `struct`, `array`, `math`. To use a lib in your program simply add `use` header:
```ss
use js;
Expand Down Expand Up @@ -124,7 +124,7 @@ Struct lib allows you to create and modify structures. Use `struct` to create a
Array lib allows you to create and modify arrays. Use `array` to create a new array. Use `array_add` and `array_remove` to add/remove elements of array. Use `array_at` to access elements of array. Use `array_set` to modify elements of array. Use `array_length` to get length of array. Use `array_find` to find index of specific element in array.

#### Math
Math lib allows you to use math functions. It supports `abs`, `sign`, `sqrt`, `sin`, `cos`, `tan`, `asin`, `acos`, `atan`, `sinh`, `cosh`, `tanh`, `asinh`, `acosh`, `atanh`, `exp`, `round`, `floor`, `ceil`, `random`, `randint`.
Math lib allows you to use math functions. It supports `abs`, `sign`, `sqrt`, `mod`, `sin`, `cos`, `tan`, `asin`, `acos`, `atan`, `sinh`, `cosh`, `tanh`, `asinh`, `acosh`, `atanh`, `exp`, `round`, `floor`, `ceil`, `random`, `randint`.

#### Native libraries
You can create your own JS libs and then use them in SS. See [this article](native-libs.md) to learn how to do it.
Expand All @@ -145,6 +145,16 @@ See [this article](ssx.md) to learn more about SSX.
## Best practices
See [this article](best-practices.md) to be a good sigma-scripter!

## NodeJS
You can also run scripts in NodeJS environment by installing SigmaScript globally:
```
npm i -g sigmascript
```
And then:
```
npx sigmascript lib1.ss lib2.ss main.ss
```

## More info
Browse the source code if you want to know more about how this project works.

Expand Down
137 changes: 135 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 7 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,21 +1,26 @@
{
"name": "sigmascript",
"version": "1.0.0",
"description": "",
"description": "Programming language made for sigma males",
"main": "index.js",
"scripts": {
"build": "webpack --config webpack.prod.js",
"start": "webpack --watch --config webpack.dev.js"
},
"bin": "bin/sigmascript.js",
"keywords": [],
"author": "",
"author": "Gulg",
"license": "ISC",
"devDependencies": {
"terser-webpack-plugin": "^5.3.10",
"ts-loader": "^9.5.1",
"typescript": "^5.4.5",
"webpack": "^5.91.0",
"webpack-cli": "^5.1.4",
"webpack-dev-server": "^5.0.4",
"webpack-merge": "^5.10.0"
},
"dependencies": {
"node-fetch": "^3.3.2"
}
}
48 changes: 48 additions & 0 deletions src/bin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { NodeRuntime } from "./runtimes/node";
import { SigmaScript } from "./sigmascript/sigmascript";
import fetch from "node-fetch";
import { readFile } from "fs/promises";

function isURL(path: string) {
let url;
try {
url = new URL(path);
} catch {
return false;
}
return true;
}

const sigmaScript = new SigmaScript();
NodeRuntime.addLibraries(sigmaScript);

for (const path of process.argv.slice(2)) {
let source;
if (isURL(path)) {
let response;
try {
response = await fetch(path);
} catch (err) {
console.error(`${path} - ${err.message}`);
continue;
}
if (!response.ok) {
console.error(`${path} - ${response.status} (${response.statusText})`);
continue;
}
try {
source = await response.text();
} catch (err) {
console.error(`${path} - ${err.message}`);
continue;
}
} else {
try {
source = await readFile(path, { encoding: "utf8" });
} catch (err) {
console.error(`${path} - ${err.message}`);
continue;
}
}
sigmaScript.load(source);
}
22 changes: 22 additions & 0 deletions src/runtimes/browser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { SigmaScript } from "../sigmascript/sigmascript";
import { DOMLib } from "../sigmascript/lib/dom";
import { FnLib } from "../sigmascript/lib/fn";
import { BrowserJSLib } from "../sigmascript/lib/js-browser";
import { RefLib } from "../sigmascript/lib/ref";
import { StringLib } from "../sigmascript/lib/string";
import { StructLib } from "../sigmascript/lib/struct";
import { ArrayLib } from "../sigmascript/lib/array";
import { MathLib } from "../sigmascript/lib/math";

export namespace BrowserRuntime {
export function addLibraries(sigmaScript: SigmaScript) {
sigmaScript.addLib("dom", new DOMLib(sigmaScript));
sigmaScript.addLib("fn", new FnLib(sigmaScript));
sigmaScript.addLib("js", new BrowserJSLib(sigmaScript));
sigmaScript.addLib("ref", new RefLib(sigmaScript));
sigmaScript.addLib("string", new StringLib(sigmaScript));
sigmaScript.addLib("struct", new StructLib(sigmaScript));
sigmaScript.addLib("array", new ArrayLib(sigmaScript));
sigmaScript.addLib("math", new MathLib(sigmaScript));
}
}
20 changes: 20 additions & 0 deletions src/runtimes/node.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { SigmaScript } from "../sigmascript/sigmascript";
import { FnLib } from "../sigmascript/lib/fn";
import { NodeJSLib } from "../sigmascript/lib/js-node";
import { RefLib } from "../sigmascript/lib/ref";
import { StringLib } from "../sigmascript/lib/string";
import { StructLib } from "../sigmascript/lib/struct";
import { ArrayLib } from "../sigmascript/lib/array";
import { MathLib } from "../sigmascript/lib/math";

export namespace NodeRuntime {
export function addLibraries(sigmaScript: SigmaScript) {
sigmaScript.addLib("fn", new FnLib(sigmaScript));
sigmaScript.addLib("js", new NodeJSLib(sigmaScript));
sigmaScript.addLib("ref", new RefLib(sigmaScript));
sigmaScript.addLib("string", new StringLib(sigmaScript));
sigmaScript.addLib("struct", new StructLib(sigmaScript));
sigmaScript.addLib("array", new ArrayLib(sigmaScript));
sigmaScript.addLib("math", new MathLib(sigmaScript));
}
}
Loading

0 comments on commit 03acb2b

Please sign in to comment.