Skip to content

Commit

Permalink
Fixed Major fn.declarationEnv bug
Browse files Browse the repository at this point in the history
  • Loading branch information
rhpo committed Mar 13, 2024
1 parent 407dc4a commit 8326793
Show file tree
Hide file tree
Showing 8 changed files with 209 additions and 52 deletions.
35 changes: 35 additions & 0 deletions graph.ln
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@

use 'plot' as plot

{plot} = plot

f = eval(
"fn f x: " +
#input('Expression? ')
"x**2"
)

# plot.animateScale(f, 10)

scale = {
x: 0.1,
y: 1
}

keypress(fn: key {

if key == "RIGHT": scale.x = scale.x + 0.1
else if key == "LEFT": scale.x = scale.x - 0.1

plot(f, scale)

})

t = task(fn: {
plot(f, scale)
})

interval(
t.run,
2000
)
26 changes: 24 additions & 2 deletions package-lock.json

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

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
{
"dependencies": {
"@types/keypress": "^2.0.30",
"@types/prompt-sync": "^4.2.0",
"colors": "^1.4.0",
"js-beautify": "^1.14.11",
"keypress": "^0.2.1",
"pkg": "^5.8.1",
"rcedit": "^3.1.0",
"robotjs": "^0.6.0",
"run-async": "^3.0.0",
"terminal-kit": "^3.0.0",
"unescape-js": "^1.1.4",
"upx": "^1.0.6"
Expand Down
59 changes: 59 additions & 0 deletions plot.lnx
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@

use "luna:console" as console

out fn plot f scale=({
x: 0.1,
y: 1
}) {

{ width, height } = console.size()

origin = {
x: math.int(width / 2),
y: math.int(height / 2)
}

console.clear()

margin = 1

# WARNING: all tasks must be done in one loop, otherwise the screen will be cleared and the axis will be lost
range(height - margin).each(fn :y {
range(width).each(fn :x {

X = x - origin.x
Fx = math.int(f(X * scale.x) * scale.y)

#debug {origin.x, x, origin.y, y}
if (x == origin.x) || (y == origin.y):
puts("*")
else if Fx == origin.y - y:
puts("#")
else
puts(" ")
})
print()
})

}

out fn animateScale f del=(10) {
scale = { x: 0.1, y: 1 }
max = 10
step = 0.01


range(2 * max).each(fn :i {
if i == max:
step *= -1

plot(f, scale)
scale.x = scale.x - step
scale.y = scale.y - step
sleep(del)
})
}

# f = eval("use 'luna:math' as math lambda x: " + input("Enter a function: "))
# animateScale(f, 0)

2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ Relation between the runtime of Luna $(t_{l})$ and the runtime of Python $(t_{p}

Like so: $t_{p}(t_{l}) = \Delta_{l,p} * t_{l}$

Luna has a max of recursive function call of $n = 2428 \pm \Delta R, \ \ \Delta R \in \mathbb{Z}, \ \ |\Delta R| \approx 50$
Luna has a max of recursive function call of $n = 2428 \pm \Delta R, \ \ \Delta R \in \mathbb{Z}, \ \ |\Delta R| \approx 50$ OR $(\frac{11}{100})$ of the max recursive call of JS (the motherlang).

**UPDATE** 🔥: When **Compiled**, **Luna** gets **20x faster** than *Python* ⚡!

Expand Down
117 changes: 75 additions & 42 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
import { terminal } from "terminal-kit";
import { colorize } from "./lib/ui";
import { Luna, createContext } from "./luna";
import { MK } from "./runtime/values";
import { FNVal, MK } from "./runtime/values";
import { KEYWORDS } from "./lib/tokenizer";
import { exec } from "pkg";

import beautify from "js-beautify/js";
import keypress from "keypress";

process.argv = process.argv.filter((c) => {
return !c.includes("snapshot");
Expand Down Expand Up @@ -58,6 +58,9 @@ const checkBrackets = (expression: string) => {
return stack.length;
};

keypress(process.stdin);

process.stdin.resume();
process.stdin.setRawMode(true);
let onKeypressQueue: any[] = [];

Expand All @@ -82,7 +85,7 @@ let env = createContext([
},

{
name: "onkeypress",
name: "keypress",
value: MK.nativeFunc((args, scope) => {
onKeypressQueue.push(args[0]);
return MK.void();
Expand Down Expand Up @@ -130,60 +133,90 @@ function exit() {
process.exit(0);
}

terminal.on("key", (name: any) => {
onKeypressQueue.forEach((cb) => {
evaluateFunctionCall(cb, [MK.string(name)], env);
});
// terminal.on("key", async (name: any) => {
// onKeypressQueue.forEach((cb) => {
// evaluateFunctionCall(cb, [MK.string(name)], env);
//});
//
// if (["CTRL_C", "ESCAPE"].includes(name)) exit();
//});

process.stdin.on("keypress", function (ch, key) {
key &&
onKeypressQueue.forEach((cb: FNVal) => {
evaluateFunctionCall(
cb,
[
MK.object({
name: MK.string(key.name),
ctrl: MK.bool(key.ctrl),
shift: MK.bool(key.shift),
meta: MK.bool(key.meta),
sequence: MK.string(key.sequence),
code: MK.string(key.code),
raw: MK.string(key.raw),
full: MK.string(key.full),
}),
],
cb.declarationEnv
);
});

if (["CTRL_C", "ESCAPE"].includes(name)) exit();
if (key && ((key.ctrl && key.name === "c") || key.name === "escape")) {
exit();
}
});

async function cli() {
let code = "";
function cli() {
setTimeout(async () => {
let code = "";

async function getCode(prompt = ">> ") {
let str = await ask(prompt);
async function getCode(prompt = ">> ") {
let str = await ask(prompt);

let check = checkBrackets(code + str);
let check = checkBrackets(code + str);

if (Number.isNaN(check)) {
console.log(Err("SyntaxError", "Unmatched bracket in REPL-Only"));
if (Number.isNaN(check)) {
console.log(Err("SyntaxError", "Unmatched bracket in REPL-Only"));

await cli();
} else if (check === 0) {
code += code !== "" ? " " + str : str;
} else {
code += code !== "" ? " " + str : str;
await cli();
} else if (check === 0) {
code += code !== "" ? " " + str : str;
} else {
code += code !== "" ? " " + str : str;

await getCode(" ".repeat(check) + "... ".gray);
await getCode(" ".repeat(check) + "... ".gray);
}
}
}

await getCode();
getCode().then(async () => {
if (code.trim() !== "") {
history.push(code);

if (code.trim() !== "") {
history.push(code);
try {
let luna = new Luna(env);

try {
let luna = new Luna(env);
setImmediate(async () => {
let result = luna.evaluate(code);
let output = colorize(result);
output && console.log(output);

let result = luna.evaluate(code);
await cli();
});

// ADD: Support for "void" values, which are not meant to be displayed.
// if result = void then return of colorize(result) should be null
// if null, console.log() should not be called
let output = colorize(result);
output && console.log(output);
// ADD: Support for "void" values, which are not meant to be displayed.
// if result = void then return of colorize(result) should be null
// if null, console.log() should not be called
} catch (e: any) {
console.log(e.stack || e);

await cli();
} catch (e: any) {
console.log(e.stack || e);

await cli();
}
} else {
await cli();
}
await cli();
}
} else {
await cli();
}
});
}, 0);
}

let welcome = `Welcome to the ${systemDefaults.name} REPL!`.green;
Expand Down
2 changes: 1 addition & 1 deletion src/lib/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ export default class Environment {

(value as RuntimeValue).reactiveCallbacks =
(value as RuntimeValue).reactiveCallbacks || [];
(value as FNVal).declarationEnv = this.parent as Environment;
(value as FNVal).declarationEnv = this;

this.variables.set(name, value as RuntimeValue);
}
Expand Down
17 changes: 11 additions & 6 deletions src/runtime/evaluation/eval.ts
Original file line number Diff line number Diff line change
Expand Up @@ -875,7 +875,8 @@ export function evaluateFunctionCall(

let f = fn as FNVal;

const fnScope = env || f.declarationEnv;
const fnScope =
typeof f.declarationEnv !== "undefined" ? env : f.declarationEnv;

if (f.parameters.length > args.length && sys.dev) {
throw Err(
Expand Down Expand Up @@ -1377,11 +1378,15 @@ export function evaluateFunctionDeclaration(
body: declaration.body,
};

let c = env.declareVar(
fn.name,
MK.func(fn.name, fn.parameters, fn.body, fn.export, env),
true
);
let func = MK.func(fn.name, fn.parameters, fn.body, fn.export, env);

// func.declarationEnv !== undefined...

let c = env.declareVar(fn.name, func, true);

let t = env.lookupVar(fn.name);

// t.declarationEnv == undefined...

return c;
}

0 comments on commit 8326793

Please sign in to comment.