nxu/uxnrepl

72 lines
1.4 KiB
Python
Executable File

#!/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()