Skip to content

Commit

Permalink
ffi: visit: opens new tab
Browse files Browse the repository at this point in the history
  • Loading branch information
panzerstadt committed Nov 26, 2023
1 parent 83f36e0 commit f2c4eac
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 2 deletions.
2 changes: 2 additions & 0 deletions components/interpreter/interface.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { useHistory } from "./useHistory";
import { Tips } from "./tips";
import { useStd } from "./useStd";
import { Container } from "../terminals/Neumorphic/Container";
import { useRedirect } from "./useRedirect";

export type Line = { type: LineType; value: string };
type LineType = StdEnvs | "usr" | "usr-tmp";
Expand All @@ -27,6 +28,7 @@ export const Interpreter: React.FC<InterpreterProps> = ({ focus }) => {
useStd(program, "debug", setLines, () => scrollToBottom()); // vvvv
useStd(program, "err", setLines, () => scrollToBottom(), true);
useStd(program, "info", setLines, () => scrollToBottom(), true);
useRedirect(program, true);

const [userHistory, { back, forward, add, addBatch }] = useHistory();
useEffect(() => {
Expand Down
10 changes: 10 additions & 0 deletions components/interpreter/program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ export class Program {
stddebug = "";
stdenv = "";

urlredirect = "";

constructor() {
this.interpreter = new BabyJs();
this.interpreter.setLogger(this.output());
Expand Down Expand Up @@ -60,6 +62,9 @@ export class Program {
.map((s) => `${this.timestamp()}:${phase}:${s}\n`)
.join("");
},
visit: (url: string) => {
this.urlredirect = `${this.timestamp()}:${url}`;
},
};
}

Expand Down Expand Up @@ -158,6 +163,11 @@ thrice(fn (a) {
print a;
});
// you might have also realised.. uh.. there are no:
- arrays
- objects
- classes (yet)
// come back from time to time to see this list grow!
-----------------
Expand Down
20 changes: 20 additions & 0 deletions components/interpreter/useRedirect.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { MutableRefObject, useEffect } from "react";
import { Program } from "./program";

const removeTimestamp = (str: string) => {
const length = Date.now().toString().length + ":".length;
return str.slice(length);
};

export const useRedirect = (ref: MutableRefObject<Program | undefined>, newtab?: boolean) => {
const currentRef = ref.current?.urlredirect;
useEffect(() => {
if (!currentRef) return;

let path = removeTimestamp(currentRef);
if (!path.startsWith("http")) {
path = `https://${path}`;
}
window.open(path, newtab ? "_blank" : "_self");
}, [currentRef, newtab]);
};
16 changes: 16 additions & 0 deletions programs/babyjs/functions/foreignfunctions/blogInterface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Callable } from "../../callable";
import { Interpreter } from "../../interpreters/interpreter";

import data from "../../../blog/data.json";
import { RuntimeError } from "../../errors";

export class Ls extends Callable {
arity(): number {
Expand All @@ -11,3 +12,18 @@ export class Ls extends Callable {
return data.blogposts;
}
}

export class Visit extends Callable {
arity(): number {
return 1;
}
call(interpreter: Interpreter, _arguments: Object[]): Object {
const path = _arguments[0];
if (!path || typeof path !== "string") {
throw new RuntimeError(`Please enter a blogpost path to visit.`);
}

interpreter.logger.visit?.(path);
return {};
}
}
5 changes: 3 additions & 2 deletions programs/babyjs/interpreters/interpreter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { PrintStyle, printAST } from "./pprinter";
import { isTruthy } from "../constants";
import { Callable } from "../callable";
import { Clock } from "../functions/foreignfunctions/clock";
import { Ls } from "../functions/foreignfunctions/blogInterface";
import { Ls, Visit } from "../functions/foreignfunctions/blogInterface";
import { Function } from "../functions/basefunction";
import { Return } from "../return";

Expand All @@ -26,12 +26,13 @@ export class Interpreter {
private loop_upper_bound = 10_000;
readonly globals = new Environment();
private environment = this.globals;
logger: Console | LoggerType = console;
logger: LoggerType = console;

constructor() {
// FFI (this is where we allow the users to work with files, read user input etc)
this.globals.define("clock", new Clock());
this.globals.define("ls", new Ls());
this.globals.define("visit", new Visit());
}

public setLogger(newLogger: LoggerType) {
Expand Down
1 change: 1 addition & 0 deletions programs/babyjs/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ export interface LoggerType {
error: (phase: Phase, str: string) => void;
debug?: (phase: Phase, ...strs: string[]) => void;
environment?: (...strs: string[]) => void;
visit?: (str: string) => void;
}

export type Phase = "scan" | "parse" | "interpret";

0 comments on commit f2c4eac

Please sign in to comment.