77 lines
1.7 KiB
Python
77 lines
1.7 KiB
Python
#!/usr/bin/python
|
|
|
|
from os import system
|
|
from subprocess import run
|
|
from sys import stdin, stdout
|
|
from tempfile import mkstemp
|
|
|
|
sandbox = None
|
|
|
|
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
|
|
'''
|
|
|
|
def write_rom(path, s):
|
|
f = open(path, 'w')
|
|
prog = template % s.decode('utf-8')
|
|
f.write(prog)
|
|
f.close()
|
|
|
|
def execute(s, sandbox=None):
|
|
_, tmp_tal = mkstemp(suffix='.tal', prefix='uxnrepl')
|
|
_, tmp_rom = mkstemp(suffix='.rom', prefix='uxnrepl')
|
|
write_rom(tmp_tal, s)
|
|
res = run(['uxnasm', tmp_tal, tmp_rom], cwd=sandbox, capture_output=True)
|
|
##print('*** uxnasm.res is %r' % res)
|
|
if res.returncode != 0:
|
|
return res.stderr
|
|
res = run(['uxncli', tmp_rom], cwd=sandbox, capture_output=True)
|
|
##print('*** uxncli.res is %r' % res)
|
|
return res.stdout
|
|
|
|
def main():
|
|
print('uxnrepl (ctrl-d to exit)')
|
|
while True:
|
|
stdout.write('> ')
|
|
stdout.flush()
|
|
s = stdin.readline()
|
|
if not s:
|
|
print('bye!')
|
|
break
|
|
stdout.write(execute(s).decode('utf-8'))
|
|
stdout.flush()
|
|
|
|
if __name__ == "__main__":
|
|
main()
|