From 0bb4f7967beabcd9353b0f34c2e4c49734a79af5 Mon Sep 17 00:00:00 2001 From: d_m Date: Mon, 22 Jan 2024 23:34:41 -0500 Subject: [PATCH] python uxn repl --- uxnrepl | 71 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100755 uxnrepl diff --git a/uxnrepl b/uxnrepl new file mode 100755 index 0000000..d580530 --- /dev/null +++ b/uxnrepl @@ -0,0 +1,71 @@ +#!/usr/bin/python + +from os import system +from sys import stdin, stdout +from tempfile import mkstemp + +template = ''' +|0100 + ( start ) %s ( end ) + + #05 DEI ,emit-wst/n STR + ;wst print + +@dump-wst + #04 DEI #01 GTH ?&next !emit-wst &next STH !dump-wst + +@emit-wst + #05 DEI LIT [ &n $1 ] GTH ?&next #0a18 DEO !start-rst + &next STHr emit #2018 DEO !emit-wst + +@start-rst + ;rst print + +@dump-rst + #05 DEI #00 GTH ?&next !emit-rst &next STHr !dump-rst + +@emit-rst + #04 DEI #01 GTH ?&next #0a18 DEO #800f DEO BRK + &next emit #2018 DEO !emit-rst + +@print ( addr* -> ) + LDAk DUP ?{ POP POP2 JMP2r } #18 DEO INC2 !print + +@emit + DUP #04 SFT ,&ch JSR + &ch #0f AND DUP #09 GTH #27 MUL ADD #30 ADD #18 DEO JMP2r + +@rst "rst 20 00 +@wst "wst 20 00 +''' + +_, tmp_tal = mkstemp(suffix='.tal', prefix='uxnrepl') +_, tmp_rom = mkstemp(suffix='.rom', prefix='uxnrepl') + +def write_rom(s): + f = open(tmp_tal, 'w') + prog = template % s + f.write(prog) + f.close() + +def run(s): + write_rom(s) + res = system('uxnasm %s %s 2>/dev/null' % (tmp_tal, tmp_rom)) + if res != 0: + system('uxnasm %s %s' % (tmp_tal, tmp_rom)) + return + system('uxncli %s' % tmp_rom) + +def main(): + print('uxnrepl (ctrl-d to exit)') + while True: + stdout.write('> ') + stdout.flush() + s = stdin.readline() + if not s: + print('bye!') + break + run(s) + +if __name__ == "__main__": + main()