From 8756e09d5b402bb4958d4466291b6d90539bf06b Mon Sep 17 00:00:00 2001 From: colevandersWands <18554853+colevandersWands@users.noreply.github.com> Date: Tue, 6 Aug 2024 10:36:42 +0200 Subject: [PATCH 1/8] define .asPalimpsest as an instance property instead of a static property This avoids the bug of non-palimpsest coems being executed as palimpsest, without needing to refresh the page or writing more complex logic in setNameValue --- src/environment.js | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/src/environment.js b/src/environment.js index ce64c71..db3e8d9 100644 --- a/src/environment.js +++ b/src/environment.js @@ -1,10 +1,10 @@ -import { runtimeError } from './errors.js'; +import { runtimeError } from "./errors.js"; class Environment { - constructor(enclosing = null) { this.values = new Map(); this.enclosing = enclosing; + this.asPalimpsest = false; } get(token) { @@ -39,7 +39,7 @@ class Environment { setNameValue(name, value) { if (name === "as" && value.literal === "palimpsest") { - Environment.asPalimpsest = true; + this.asPalimpsest = true; return; } @@ -48,7 +48,7 @@ class Environment { // redefine in current environment if (set) { - if (Environment.asPalimpsest) { + if (this.asPalimpsest) { let values = set[1]; values.push(value); return this.values.set(set[0], values); @@ -66,7 +66,7 @@ class Environment { } // define new in current environment - if (Environment.asPalimpsest) { + if (this.asPalimpsest) { return this.values.set(pattern, [value]); } else { return this.values.set(pattern, value); @@ -79,6 +79,4 @@ class Environment { } } -Environment.asPalimpsest = false; - -export { Environment }; \ No newline at end of file +export { Environment }; From af9bed69587b8f601723570c780dc6e5d4a24cf5 Mon Sep 17 00:00:00 2001 From: colevandersWands <18554853+colevandersWands@users.noreply.github.com> Date: Tue, 6 Aug 2024 10:39:34 +0200 Subject: [PATCH 2/8] extract first element of callee when executing as palimpsest avoids the "Can only call functions." error that occurs in valid palimpsests --- src/interpreter.js | 44 +++++++++++++++++++++++--------------------- 1 file changed, 23 insertions(+), 21 deletions(-) diff --git a/src/interpreter.js b/src/interpreter.js index 7da4905..59ede6d 100644 --- a/src/interpreter.js +++ b/src/interpreter.js @@ -1,4 +1,4 @@ -import { runtimeError, ReturnError } from './errors.js'; +import { runtimeError, ReturnError } from "./errors.js"; import { Binary, Unary, @@ -14,19 +14,19 @@ import { VarStatement, Condition, Directive, - Comment -} from './types.js'; -import { Environment } from './environment.js'; -import { Tokenizer } from './tokenizer.js'; + Comment, +} from "./types.js"; +import { Environment } from "./environment.js"; +import { Tokenizer } from "./tokenizer.js"; const token = Tokenizer.tokenEnum; -const isTruthy = val => Boolean(val); +const isTruthy = (val) => Boolean(val); const isEqual = (a, b) => a === b; class CoemCallable { constructor(declaration, closure) { - this.declaration = declaration - this.closure = closure + this.declaration = declaration; + this.closure = closure; } // call(interpreter, args) { @@ -48,10 +48,9 @@ class CoemCallable { } toString() { - return `<${this.declaration.name.lexeme}()>` - }; + return `<${this.declaration.name.lexeme}()>`; + } } -; class Interpreter { // constructor(environment, printfunc = console.log) { constructor(environment, source) { @@ -88,18 +87,18 @@ class Interpreter { } else { this.lines[line].push(print); } - } + }; const getArgPrint = (arg) => { if (Array.isArray(arg)) { return arg.join(", "); } return arg; - } + }; - this.environment.setBuiltin('print', nativePrint); - this.environment.setBuiltin('know', nativePrint); - this.environment.setBuiltin('say', nativePrint); + this.environment.setBuiltin("print", nativePrint); + this.environment.setBuiltin("know", nativePrint); + this.environment.setBuiltin("say", nativePrint); } interpret(expr) { @@ -117,7 +116,8 @@ class Interpreter { else if (expr instanceof VarStatement) return this.visitVarStatement(expr); else if (expr instanceof Return) return this.visitReturnStatement(expr); // Doesn't need its own, it can just evaluate like grouping - else if (expr instanceof ExpressionStatement) return this.visitExpressionStmt(expr); + else if (expr instanceof ExpressionStatement) + return this.visitExpressionStmt(expr); else if (expr instanceof Var) return this.visitVar(expr); else if (expr instanceof Literal) return this.visitLiteral(expr); else if (expr instanceof Unary) return this.visitUnary(expr); @@ -220,13 +220,15 @@ class Interpreter { visitCall(expr) { const callee = this.evaluate(expr.callee); - let args = expr.arguments.map(arg => this.evaluate(arg)); + if (Array.isArray(callee)) callee = callee[0]; + + let args = expr.arguments.map((arg) => this.evaluate(arg)); if (!callee.call) { - throw runtimeError('Can only call functions.', expr.dash); + throw runtimeError("Can only call functions.", expr.dash); } - return callee.call(this, args, expr.callee) + return callee.call(this, args, expr.callee); } visitUnary(expr) { @@ -268,4 +270,4 @@ class Interpreter { } } -export { Interpreter }; \ No newline at end of file +export { Interpreter }; From ce2e28c64cc0b522f30ed48e3066ca2fc5bdaaae Mon Sep 17 00:00:00 2001 From: colevandersWands <18554853+colevandersWands@users.noreply.github.com> Date: Wed, 7 Aug 2024 21:08:09 +0200 Subject: [PATCH 3/8] add folder of examples --- examples/be.coem | 7 +++++++ examples/briefly.coem | 6 ++++++ examples/cat-detected.be.coem | 10 ++++++++++ examples/docks.coem | 6 ++++++ examples/reminder.coem | 9 +++++++++ examples/sleep.coem | 6 ++++++ examples/snail-train.coem | 1 + index.js => examples/test.coem | 28 +++------------------------- examples/woman.coem | 10 ++++++++++ 9 files changed, 58 insertions(+), 25 deletions(-) create mode 100644 examples/be.coem create mode 100644 examples/briefly.coem create mode 100644 examples/cat-detected.be.coem create mode 100644 examples/docks.coem create mode 100644 examples/reminder.coem create mode 100644 examples/sleep.coem create mode 100644 examples/snail-train.coem rename index.js => examples/test.coem (64%) create mode 100644 examples/woman.coem diff --git a/examples/be.coem b/examples/be.coem new file mode 100644 index 0000000..d5642cc --- /dev/null +++ b/examples/be.coem @@ -0,0 +1,7 @@ +#as palimpsest + +let flag be black +let flag be yellow +let flag be red + +know—flag— \ No newline at end of file diff --git a/examples/briefly.coem b/examples/briefly.coem new file mode 100644 index 0000000..5d03130 --- /dev/null +++ b/examples/briefly.coem @@ -0,0 +1,6 @@ +say—surrender— +say—alabaster— +let (switchblade|honeysuckle|goldenrod) +say—autumn— † despite the green in my eyes. +say—beauty— † despite daylight. +say—“i’d kill for it”— \ No newline at end of file diff --git a/examples/cat-detected.be.coem b/examples/cat-detected.be.coem new file mode 100644 index 0000000..9e1879a --- /dev/null +++ b/examples/cat-detected.be.coem @@ -0,0 +1,10 @@ +let (ch|k)at(|tze) be “🐈” + +let savoir be know +savoir—chat is “🐈”— + +let weten be know +weten—kat is “🐈”— + +let wissen be know +wissen—katze is “🐈”— \ No newline at end of file diff --git a/examples/docks.coem b/examples/docks.coem new file mode 100644 index 0000000..968ab4f --- /dev/null +++ b/examples/docks.coem @@ -0,0 +1,6 @@ +let se(e|a) be “blue” +let mis(t|sed) be “thick” +let mou?rning be “dark” +let me be nothing +know—see, mist, morning— +know—sea, missed, mourning— \ No newline at end of file diff --git a/examples/reminder.coem b/examples/reminder.coem new file mode 100644 index 0000000..42d9964 --- /dev/null +++ b/examples/reminder.coem @@ -0,0 +1,9 @@ +† this is a reminder + +to breathe?——: + † in and out, and thus to + know—myself—. + +let me|myself|I be “present” +if—I am “present”—: + let me be breath——. \ No newline at end of file diff --git a/examples/sleep.coem b/examples/sleep.coem new file mode 100644 index 0000000..475cf00 --- /dev/null +++ b/examples/sleep.coem @@ -0,0 +1,6 @@ +to sl?eep—when—: + “dreams”. + +sleep—“now”— +sleep—“never”— +seep—“continually”— \ No newline at end of file diff --git a/examples/snail-train.coem b/examples/snail-train.coem new file mode 100644 index 0000000..c65652d --- /dev/null +++ b/examples/snail-train.coem @@ -0,0 +1 @@ +print—“i_@”— \ No newline at end of file diff --git a/index.js b/examples/test.coem similarity index 64% rename from index.js rename to examples/test.coem index 8b028f9..c54f0f5 100644 --- a/index.js +++ b/examples/test.coem @@ -1,8 +1,4 @@ -import { run, Environment } from './src/main.js'; - -const debug = true; - -const source = `#as palimpsest +#as palimpsest to conditional——: let tomorrow be not coming @@ -28,29 +24,11 @@ to conditional——: let end be “watching the sky burn”. † conditional—— + say—“It doesn’t matter.”— say—“That would be enough.”— let this be “us alive” let this be “right here” let this be “feeling lucky” let you be wanting -this`; - -function handleError(e, source = "") { - if (!e) return null; - console.error(e); -} - -function handleOutput(...txt) { - console.log(...txt); -} - -const browserEnv = new Environment(); -try { - // console.log(run(source, browserEnv, handleOutput, false)); - let echo = run(source, browserEnv, debug); - console.log(echo); - handleError(null); -} catch (e) { - handleError(e, source); -} \ No newline at end of file +this \ No newline at end of file diff --git a/examples/woman.coem b/examples/woman.coem new file mode 100644 index 0000000..658e842 --- /dev/null +++ b/examples/woman.coem @@ -0,0 +1,10 @@ +† based on Emmy Meli's “I Am Woman” + +#as palimpsest + +let I|me|myself be “woman” +let me be “fearless” +let me be “sexy” +let me be “divine” + +know—myself— \ No newline at end of file From 5655081f08f1725aefbbfdc02e9071a14f390d5d Mon Sep 17 00:00:00 2001 From: colevandersWands <18554853+colevandersWands@users.noreply.github.com> Date: Wed, 7 Aug 2024 21:08:24 +0200 Subject: [PATCH 4/8] add browser sandbox --- examples/index.html | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 examples/index.html diff --git a/examples/index.html b/examples/index.html new file mode 100644 index 0000000..739125d --- /dev/null +++ b/examples/index.html @@ -0,0 +1,38 @@ + + +
+ + +