Skip to content

Instantly share code, notes, and snippets.

@RixInGithub
Last active December 28, 2023 10:59
Show Gist options
  • Save RixInGithub/cf60bbd2d886dcb8d1294b93e15ec1a7 to your computer and use it in GitHub Desktop.
Save RixInGithub/cf60bbd2d886dcb8d1294b93e15ec1a7 to your computer and use it in GitHub Desktop.
GradientScript parser

What is this??

I'm thinking of making a scripting/programming language for GradientOS called "GradientScript".

For now, it only knows how to "import modules".

I'm thinking of parsing the code, then executing it on a setTimeout and then return an GrSEnviron(ment)

Where do you run this?

You must run this inside of devtools (F12) in GradientOS!! 😠⚡⛈

After you run this, you can, of course, do whatever you want. But for now it's in the rael beta modez.

- From the Gradient Man (RAEL!1!!)

if (!("grOS" in window)) grOS = {}
grOS.execGrS = function(grS) {
class GrSEnviron {
constructor() {
var types = ["script", "module"]
var type = "script"
this.getType = function() {
return type
}
this.setType = function(nw) {
if (types.indexOf(nw) == -1) {
throw TypeError("Unknown type")
}
type = nw
}
this.freezeEnv = function() {
delete this.setType
delete this.freezeEnv
delete this.addVars
}
var vars = new Map()
this.getVars = function() {
if ("freezeEnv" in this) return vars
var res = new Map()
for (var k of Array.from(vars.keys())) {
res.set(k, vars.get(k))
}
return res
}
this.addVars = function(nm, val) {
vars.set(nm, val)
}
}
}
function scr2Obj() {
var lns = grS.split("\n")
var res = []
function parseStr(st) {
if (!((st.startsWith("\"")) || (st.endsWith("\"")))) return false
try {
return JSON.parse(st)
} catch (_) {return false}
}
var currObj = null
var lnCount = 0
for (var ln of lns) {
lnCount += 1
if (ln.startsWith("&")) {
res.push({"type": "import"})
currObj = parseStr(ln.slice(1))
if (currObj === false) {
throw SyntaxError(`Invalid string at import statement at line ${lnCount}`)
}
res[res.length - 1].module = currObj
}
}
return res
}
function obj2Env(obj) {
console.log(obj)
var env = new GrSEnviron()
env.freezeEnv()
return env
}
return obj2Env(scr2Obj())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment