make the isa transpiler into a module

there's some other stuff as well
This commit is contained in:
randomuser 2024-02-01 17:00:28 -06:00
parent badb755812
commit c93b0b493b
10 changed files with 94 additions and 41 deletions

15
cli.py
View File

@ -1,15 +0,0 @@
from server import main
import argparse
def entry():
parser = argparse.ArgumentParser(
prog="desmos-sync",
description="Synchronize from local file to Desmos calculator",
)
parser.add_argument("filename")
args = parser.parse_args()
main(args.filename)
if __name__ == "__main__":
entry()

View File

@ -11,6 +11,8 @@ INSTRUCTIONS
------------
STO ( value, address ) -> store a value into an address
MOV ( fromaddr, toaddr) -> move a value from one address to another
PSTO ( value, ptr ) -> store value into the address referenced in ptr
ADD ( addra, addrb, toaddr ) -> add a value from two addresses to a third address
SUB ( addra, addrb, toaddr ) -> subtract a value from two addresses to a third address
MUL ( addra, addrb, toaddr ) -> multiply a value from two addresses to a third address

View File

@ -1,7 +1,6 @@
ticker 1 : loopaction, loop
ticker 1 : main
# Main memory structure
: B = [1...100] * 0
id testing : B = [1, 4, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
# Operations on memory
: q(l, v, i)=ifval(l, i, [1...length(l)], v)
: setlistval(index, v) = B -> q(B, v, index)
@ -21,12 +20,17 @@ ticker 1 : loopaction, loop
: idiv(a, b, t_o) = setlistval(t_o, odiv(B[a], B[b]))
: imul(a, b, t_o) = setlistval(t_o, omul(B[a], B[b]))
: icmp(a, b, z) = { a = b : equals -> 1 , a > b : greater -> 1 , a < b : less -> 1}
: icmp(a, b, z) = { \
a = b : equals -> 1 , \
a > b : greater -> 1 , \
a < b : less -> 1 \
}
: irst(a, b, c) = equals -> 0, greater -> 0, less -> 0
: ield(addr, b) = setlistval(addr, equals)
: igld(addr, b) = setlistval(addr, greater)
: illd(addr, b) = setlistval(addr, less)
: ibe(addr, b, c) = {equals = 1 : ijmp(addr, 0, 0), jumped -> 1}
: ibne(addr, b, c) = {equals = 0 : ijmp(addr, 0, 0), jumped -> 1}
: ibg(addr, b, c) = {greater = 1 : ijmp(addr, 0, 0), jumped -> 1}
@ -84,6 +88,7 @@ ticker 1 : loopaction, loop
: loop = {execution = 0 : execution -> 1, execution = 1 : execution -> 2, execution = 2 : execution -> 0}
: loopaction = {execution = 0 : load(ip), execution = 1 : exec, execution = 2 : incip}
: main = loopaction, loop
: sto = 1
: add = 2
@ -101,4 +106,4 @@ ticker 1 : loopaction, loop
: div = 14
: rst = 15
: instwidth = [2,3,1,1,1,1,1,1,1,1,3,3,3,3,0]
: instwidth = [2,3,1,1,1,1,1,1,1,1,3,3,3,3,0]

37
desmosisa/__init__.py Normal file
View File

@ -0,0 +1,37 @@
from .server import main
import argparse
import pyperclip
import json
def get_overrides(file):
fd = open(file, "r")
data = json.loads(fd.read())
fd.close()
return data
def entry():
parser = argparse.ArgumentParser(
prog="desmos-sync",
description="Synchronize from local file to Desmos calculator",
)
parser.add_argument('--copy', action="store_true", help="copy the client side JS to clipboard")
parser.add_argument('--run', help="specify file to start the desmos server for")
parser.add_argument('--overrides', help="specify file that contains overrides for desmos expressions")
args = parser.parse_args()
if args.overrides:
args.overrides = get_overrides(args.overrides)
if args.run:
main(args.run, args.overrides if args.overrides else {})
elif args.copy:
fd = open("console.js", "r")
buffer = fd.read()
pyperclip.copy(buffer)
print("copied")
else:
parser.print_help()
if __name__ == "__main__":
entry()

4
desmosisa/__main__.py Normal file
View File

@ -0,0 +1,4 @@
from . import entry
if __name__ == "__main__":
entry()

View File

@ -6,7 +6,7 @@ import json
import random
import queue
import functools
from parser import Parser
from .parser import Parser, Statement
from watchdog.events import FileSystemEventHandler
from watchdog.events import FileModifiedEvent
from watchdog.observers import Observer
@ -19,7 +19,7 @@ class FSEHandler(FileSystemEventHandler):
def on_modified(self, event):
self.queue.put("")
async def serv(websocket, file):
async def serv(websocket, file, overrides={}):
message = await websocket.recv()
lmtime = 0
epsilon = 0.25 # tweak this to what makes sense. 0.25 seconds makes sense to me.
@ -44,7 +44,7 @@ async def serv(websocket, file):
continue
else:
lmtime = time.time()
parser = Parser(file)
parser.parse()
@ -52,7 +52,7 @@ async def serv(websocket, file):
json.dumps(
{
"message": "clear",
"payload": "none",
"payload": "none",
}
)
)
@ -70,25 +70,38 @@ async def serv(websocket, file):
)
continue
if not line["comment"]:
await websocket.send(
json.dumps(
{
"message": "expression",
"id": "placeholder" + str(random.randint(1, 100100)),
"payload": line.latex,
}
)
# if the line has been assigned an id, make it so
if line["id"]:
ident = line.commands["id"]
print("performing substitution")
else:
# else just choose a safe option
ident = "placeholder" + str(random.randint(1, 100100))
if ident in overrides.keys():
to_send = overrides[ident]
else:
to_send = line.latex
await websocket.send(
json.dumps(
{
"message": "expression",
"id": ident,
"payload": to_send,
}
)
async def start_server(file):
wrapper = functools.partial(serv, file=file)
)
async def start_server(file, overrides):
wrapper = functools.partial(serv, file=file, overrides=overrides)
async with serve(wrapper, "localhost", 8765):
await asyncio.Future()
def main(file):
asyncio.run(start_server(file))
def main(file, overrides):
asyncio.run(start_server(file, overrides))
if __name__ == "__main__":
main("data/testing.desmos")
main("data/testing.desmos")

3
pyproject.toml Normal file
View File

@ -0,0 +1,3 @@
[build-system]
requires = ["setuptools"]
build-backend = "setuptools.build_meta"

View File

@ -2,6 +2,9 @@
let
my-python-packages = ps: with ps; [
pip
pkgs.python311Packages.websockets
pkgs.python311Packages.watchdog
pkgs.python311Packages.pyperclip
];
my-python = pkgs.python3.withPackages my-python-packages;
in my-python.env
in my-python.env

1
test.override Normal file
View File

@ -0,0 +1 @@
{"testing": "\\left[1,2,3,4,5,1\\right]"}