-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d29997c
commit 14905d1
Showing
38 changed files
with
1,357 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,14 @@ | ||
from dsl import db | ||
import mathdsl | ||
|
||
def x(input, name): | ||
print("Hello world!") | ||
if True: | ||
while 2 < 1: | ||
with db.change("rooms") as table: | ||
table.column("lastName").replace("<target>", "<value>") | ||
table.column("firstName").trim("both") | ||
# Math term transformed into function | ||
f, args = mathdsl.compile("20\\pi^2+\\sin x") | ||
print("f(x):", f(x=90)) | ||
|
||
with db.change("students") as table: | ||
table.column("name").replace("Mister", "Mr."); | ||
table.column("lastName").trim("right"); | ||
# Math term with a matrix | ||
rotation, args = mathdsl.compile("\\begin{pmatrix}\\cos\\theta & -\\sin\\theta \\\\ \\sin\\theta & \\cos\\theta\\end{pmatrix}") | ||
print("rotation(theta):", rotation(theta=0.5)) | ||
|
||
y = 42 | ||
y + 10 | ||
|
||
if y > 42: | ||
x = 3 | ||
# Math term evaluated immediately, using variables from local scope | ||
r = 5 | ||
x = mathdsl.evaluate("r^r", locals()) | ||
print("x:", x) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import sympy | ||
import latex2sympy2 | ||
|
||
|
||
def compile(latex): | ||
symbolic = latex2sympy2.latex2sympy(latex) | ||
simplified = sympy.simplify(symbolic) | ||
args = tuple(simplified.free_symbols) | ||
return sympy.lambdify(args, simplified, "numpy"), args | ||
|
||
|
||
def evaluate(latex, scope): | ||
f, args = compile(latex) | ||
return f(**{symbol.name: scope[symbol.name] for symbol in args}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
<script lang="ts"> | ||
import DarkMode from "svelte-dark-mode"; | ||
import type { Theme } from "svelte-dark-mode/types/DarkMode.svelte"; | ||
import { basicSetup } from "codemirror"; | ||
import { EditorState, Annotation, Compartment } from "@codemirror/state"; | ||
import type { Extension } from "@codemirror/state"; | ||
import { EditorView, keymap } from "@codemirror/view"; | ||
import { indentWithTab } from "@codemirror/commands"; | ||
import { autocompletion } from "@codemirror/autocomplete"; | ||
import { indentUnit } from "@codemirror/language"; | ||
import { python } from "@codemirror/lang-python"; | ||
import { onDestroy, onMount } from "svelte"; | ||
import { example } from "./code"; | ||
import { projectionPlugin, completions } from "@puredit/projections"; | ||
import { oneDark } from "@codemirror/theme-one-dark"; | ||
import { indentationMarkers } from "@replit/codemirror-indentation-markers"; | ||
import { projectionPluginConfig } from "./projections"; | ||
let theme: Theme | undefined; | ||
let container: HTMLDivElement; | ||
let projectionalEditor: EditorView | undefined; | ||
let codeEditor: EditorView | undefined; | ||
const syncChangeAnnotation = Annotation.define<boolean>(); | ||
const darkThemeCompartment = new Compartment(); | ||
onMount(() => { | ||
const extensions: Extension[] = [ | ||
basicSetup, | ||
indentUnit.of(" "), // 4 spaces for Python | ||
keymap.of([indentWithTab]), | ||
darkThemeCompartment.of(theme === "dark" ? oneDark : []), | ||
indentationMarkers(), | ||
EditorView.theme({ | ||
".cm-scroller": { | ||
fontFamily: "var(--mono-font, monospace)", | ||
fontSize: "14px", | ||
}, | ||
".cm-tooltip": { | ||
fontFamily: "var(--system-font, sans-serif)", | ||
}, | ||
}), | ||
python(), | ||
]; | ||
const projectionalEditorExtensions = extensions.concat([ | ||
projectionPlugin(projectionPluginConfig), | ||
autocompletion({ | ||
activateOnTyping: true, | ||
override: [completions], | ||
}), | ||
]); | ||
const codeEditorExtensions = extensions; | ||
projectionalEditor = new EditorView({ | ||
state: EditorState.create({ | ||
doc: example, | ||
extensions: projectionalEditorExtensions, | ||
}), | ||
parent: container, | ||
dispatch(tr) { | ||
projectionalEditor!.update([tr]); | ||
if (!tr.changes.empty && !tr.annotation(syncChangeAnnotation)) { | ||
codeEditor!.dispatch({ | ||
changes: tr.changes, | ||
annotations: syncChangeAnnotation.of(true), | ||
filter: false, | ||
}); | ||
} | ||
}, | ||
}); | ||
codeEditor = new EditorView({ | ||
state: EditorState.create({ | ||
doc: example, | ||
extensions: codeEditorExtensions, | ||
}), | ||
parent: container, | ||
dispatch(tr) { | ||
codeEditor!.update([tr]); | ||
if (!tr.changes.empty && !tr.annotation(syncChangeAnnotation)) { | ||
projectionalEditor!.dispatch({ | ||
changes: tr.changes, | ||
annotations: syncChangeAnnotation.of(true), | ||
filter: false, | ||
}); | ||
} | ||
}, | ||
}); | ||
}); | ||
onDestroy(() => { | ||
projectionalEditor?.destroy(); | ||
codeEditor?.destroy(); | ||
}); | ||
function onThemeChange(theme?: Theme) { | ||
const transaction = { | ||
effects: [ | ||
darkThemeCompartment.reconfigure(theme === "dark" ? oneDark : []), | ||
], | ||
}; | ||
projectionalEditor?.dispatch(transaction); | ||
codeEditor?.dispatch(transaction); | ||
} | ||
// Dynamically update color scheme on theme change | ||
$: onThemeChange(theme); | ||
</script> | ||
|
||
<DarkMode bind:theme /> | ||
|
||
<div class="container" bind:this={container} /> | ||
|
||
<style> | ||
.container { | ||
width: 100%; | ||
height: 100%; | ||
display: grid; | ||
grid-template-columns: 50% 50%; | ||
} | ||
</style> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
async function fetchText(url: string): Promise<string> { | ||
const resp = await fetch(url); | ||
return resp.text(); | ||
} | ||
|
||
export const example = await fetchText("/examples/example.py"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
<script type="ts"> | ||
import type { EditorState } from "@codemirror/state"; | ||
import type { EditorView } from "@codemirror/view"; | ||
import type { SyntaxNode } from "@puredit/parser"; | ||
import type { FocusGroup } from "@puredit/projections/focus"; | ||
import { | ||
stringLiteralValue, | ||
stringLiteralValueChange, | ||
} from "@puredit/projections/shared"; | ||
import { MathfieldElement } from "mathlive"; | ||
import { onDestroy, onMount } from "svelte"; | ||
export let view: EditorView | null; | ||
export let node: SyntaxNode; | ||
export let state: EditorState; | ||
export let focusGroup: FocusGroup | null = null; | ||
let target: HTMLElement; | ||
MathfieldElement.fontsDirectory = | ||
"https://unpkg.com/[email protected]/dist/fonts"; | ||
MathfieldElement.soundsDirectory = | ||
"https://unpkg.com/[email protected]/dist/sounds"; | ||
const mfe = new MathfieldElement(); | ||
function updateMathfield(value: string) { | ||
if (!mfe.hasFocus()) { | ||
mfe.setValue(value, { silenceNotifications: true }); | ||
} | ||
} | ||
$: updateMathfield(stringLiteralValue(node, state.doc)); | ||
mfe.addEventListener("input", () => { | ||
view?.dispatch({ | ||
filter: false, | ||
changes: stringLiteralValueChange(node, mfe.value), | ||
}); | ||
}); | ||
mfe.addEventListener("move-out", (e) => { | ||
switch (e.detail.direction) { | ||
case "forward": | ||
case "downward": | ||
focusGroup.next(mfe); | ||
break; | ||
case "backward": | ||
case "upward": | ||
focusGroup.previous(mfe); | ||
break; | ||
} | ||
}); | ||
onMount(() => { | ||
target.appendChild(mfe); | ||
mfe.inlineShortcuts = { | ||
...mfe.inlineShortcuts, | ||
matrix: "\\begin{pmatrix} \\end{pmatrix}", | ||
col: "&", | ||
row: "\\\\", | ||
}; | ||
}); | ||
$: if (mfe && focusGroup) { | ||
focusGroup.registerElement(mfe); | ||
} | ||
onDestroy(() => { | ||
if (mfe && focusGroup) { | ||
focusGroup.unregisterElement(mfe); | ||
} | ||
}); | ||
</script> | ||
<span bind:this={target} /> | ||
<!-- svelte-ignore css-unused-selector --> | ||
<style global> | ||
math-field { | ||
display: inline-flex; | ||
font-size: 1.2em; | ||
} | ||
</style> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<script lang="ts"> | ||
import type { EditorState } from "@codemirror/state"; | ||
import type { EditorView } from "@codemirror/view"; | ||
import type { Match } from "@puredit/parser"; | ||
import type { FocusGroup } from "@puredit/projections/focus"; | ||
import { onMount } from "svelte"; | ||
import EquationEditor from "./EquationEditor.svelte"; | ||
export let isNew: boolean; | ||
export let view: EditorView | null; | ||
export let match: Match; | ||
// svelte-ignore unused-export-let | ||
export let context: object; | ||
export let state: EditorState; | ||
export let focusGroup: FocusGroup; | ||
onMount(() => { | ||
if (isNew) { | ||
requestAnimationFrame(() => { | ||
focusGroup.first(); | ||
}); | ||
} | ||
}); | ||
</script> | ||
|
||
<EquationEditor node={match.args.latex} {state} {view} {focusGroup} /> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { arg, contextVariable } from "@puredit/parser"; | ||
import { svelteProjection } from "@puredit/projections/svelte"; | ||
import type { Projection } from "@puredit/projections/types"; | ||
import MathProjection from "./MathProjection.svelte"; | ||
import { pythonParser } from "./parser"; | ||
|
||
const dsl = contextVariable("mathdsl"); | ||
const latex = arg("latex", "string"); | ||
|
||
export const [pattern, draft] = pythonParser.expressionPattern` | ||
${dsl}.compile(${latex}) | ||
`; | ||
|
||
export const widget = svelteProjection(MathProjection); | ||
|
||
export const compileMathProjection: Projection = { | ||
name: "compile math", | ||
description: | ||
"Transforms an expression in mathematical notation into a reusable functions, using free symbols as named parameters.", | ||
pattern, | ||
draft, | ||
requiredContextVariables: [], | ||
widgets: [widget], | ||
}; |
Oops, something went wrong.