web: add /kripke

This commit is contained in:
2025-02-18 05:24:54 +09:00
parent 6d262460f2
commit 7a81594a86
5 changed files with 75 additions and 5 deletions

View File

@@ -0,0 +1,20 @@
<script lang="ts">
import katex from "katex";
import "katex/dist/katex.min.css";
import type { HTMLAttributes } from "svelte/elements";
export interface KatexProps extends HTMLAttributes<HTMLDivElement> {
displayMode?: boolean;
math: string;
}
let { displayMode, math, ...rest }: KatexProps = $props();
let katexString = $derived.by(() =>
katex.renderToString(math, { displayMode, throwOnError: false }),
);
</script>
<div {...rest}>
{@html katexString}
</div>

View File

@@ -0,0 +1,17 @@
<script lang="ts">
import Katex from "$lib/components/katex.svelte";
import { type Formula, latexSymbols, prettyPrint } from "@cannorin/kripke";
import FormulaInput from "./formula-input.svelte";
let formula: Formula | undefined = $state(undefined);
let math = $derived.by(() => {
if (formula) return prettyPrint(formula, { symbols: latexSymbols });
return "\\phantom{p}";
});
</script>
<div class="flex flex-col min-h-screen items-center gap-12 lg:gap-16 py-8">
<Katex math={math} />
<FormulaInput bind:formula />
</div>

View File

@@ -0,0 +1,25 @@
<script lang="ts">
import { type Formula, tryParse } from "@cannorin/kripke";
let {
formula = $bindable<Formula | undefined>(undefined),
}: {
formula?: Formula | undefined;
} = $props();
let input = $state("");
let error = $state(false);
$effect(() => {
const fml = tryParse(input);
if (fml) {
formula = fml;
error = false;
} else if (input !== null && input.length > 0) error = true;
});
</script>
<input
class={["rounded border border-foreground ring-0 focus:outline-none focus:ring-0 p-2", error && "border-primary"]}
type="text"
bind:value={input} />

View File

@@ -146,3 +146,11 @@ EXP.setPattern(
export function parse(expr: string): Formula {
return expectSingleResult(expectEOF(EXP.parse(lexer.parse(expr))));
}
export function tryParse(expr: string): Formula | undefined {
try {
return parse(expr);
} catch {
return undefined;
}
}

View File

@@ -79,11 +79,11 @@ export const unicodeSymbols: Symbols = {
};
export const latexSymbols: Symbols = {
top: "\\top",
bot: "\\bot",
box: "\\Box",
diamond: "\\Diamond",
not: "\\neg",
top: "\\top ",
bot: "\\bot ",
box: "\\Box ",
diamond: "\\Diamond ",
not: "\\neg ",
and: "\\land",
or: "\\lor",
to: "\\to",