Skip to content

Commit

Permalink
Attempt at Python3 compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesbowman committed Sep 26, 2015
1 parent 05cad81 commit a956fe0
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 79 deletions.
21 changes: 16 additions & 5 deletions doc/shell.tex
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
\newcommand{\shcmd}[2]{
\subsubsection{\mach{\##1} - #2}
\index{#1@\mach{\##1}}
\index{\##1@\mach{\##1}}
}

\section{The SwapForth shell} \index{shell}

The SwapForth shell is a Python program that runs on the host PC.
Expand All @@ -11,13 +17,18 @@ \section{The SwapForth shell} \index{shell}
\item \mach{\^{}C} for interrupt
\end{itemize}

\subsection{Invocation}

The shell is a Python program. To run it, go to the appropriate directory and type:

\begin{framed}
\begin{Verbatim}
python shell.py -h /dev/ttyUSB0
\end{Verbatim}
\end{framed}

\subsection{Command reference}

\newcommand{\shcmd}[2]{
\subsubsection{\mach{\##1} - #2}
\index{#1@\mach{\##1}}
\index{\##1@\mach{\##1}}
}
\shcmd{bye}{quit SwapForth shell}
\shcmd{flash}{copy the target state to a local file}
\shcmd{include}{send local source file}
Expand Down
29 changes: 15 additions & 14 deletions j1a/shell.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
#!/usr/bin/env python
from __future__ import print_function

import sys
from datetime import datetime
import time
import array
import struct
import os

try:
import serial
except:
print "This tool needs PySerial, but it was not found"
sys.exit(1)

sys.path.append("../shell")
import swapforth

class TetheredJ1a(swapforth.TetheredTarget):
cellsize = 2

def open_ser(self, port, speed):
try:
import serial
except:
print("This tool needs PySerial, but it was not found")
sys.exit(1)
self.ser = serial.Serial(port, 4 * 115200, timeout=None, rtscts=0)

def reset(self, fullreset = True):
ser = self.ser
ser.setDTR(1)
Expand All @@ -30,23 +31,23 @@ def reset(self, fullreset = True):
time.sleep(0.1)

for c in ' 1 tth !':
ser.write(c)
ser.write(c.encode('utf-8'))
ser.flush()
time.sleep(0.001)
ser.flushInput()
# print repr(ser.read(ser.inWaiting()))
ser.write('\r')
# print(repr(ser.read(ser.inWaiting())))
ser.write(b'\r')

while 1:
c = ser.read(1)
# print repr(c)
if c == chr(30):
print(repr(c))
if c == b'\x1e':
break

def boot(self, bootfile = None):
sys.stdout.write('Contacting... ')
self.reset()
print 'established'
print('established')

def interrupt(self):
self.reset(False)
Expand Down
3 changes: 2 additions & 1 deletion python/go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# time echo | PYTHONPATH=../shell/:$PYTHONPATH python nuc.py
PYTHONPATH=../shell/:$PYTHONPATH python nuc.py -c 2 -b
set -e
PYTHONPATH=../shell/:$PYTHONPATH python3 nuc.py -c 2 -b
echo PASSED
39 changes: 26 additions & 13 deletions python/nuc.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
from __future__ import print_function
from __future__ import division

import sys
import getopt
import struct
Expand All @@ -8,6 +11,9 @@
import time
import re

if sys.version_info[0] < 3:
input = raw_input

sys.path.append("../shell")
import swapforth

Expand All @@ -19,7 +25,10 @@ def setimmediate(func):
return func

def ba(x):
return array.array('B', x)
if type(x) == str:
return array.array('B', [ord(c) for c in x])
else:
return array.array('B', x)

class ForthException(Exception):
def __init__(self, value):
Expand Down Expand Up @@ -111,7 +120,7 @@ def dlit(self, d):
def pops(self):
n = self.d.pop()
a = self.d.pop()
return self.ram[a:a+n].tostring()
return self.ram[a:a+n].tostring().decode("utf-8")

# Start of Forth words
#
Expand Down Expand Up @@ -371,7 +380,7 @@ def u_m_slash_mod(self):
u1 = self.u32(self.d.pop())
ud = self.dpop() & (65536**self.CELL - 1)
self.lit(self.w32(ud % u1))
self.lit(self.w32(ud / u1))
self.lit(self.w32(ud // u1))

def MS(self):
time.sleep(0.001 * self.d.pop())
Expand Down Expand Up @@ -400,7 +409,8 @@ def xt(self, c):

def SFIND(self):
(a, n) = self.d[-2:]
s = self.ram[a:a+n].tostring().upper()
s = self.ram[a:a+n].tostring().decode("utf-8").upper()
# print('HERE', s.decode("utf-8"), self.dict)
if s in self.dict:
x = self.dict[s]
self.d[-2] = self.xt(x)
Expand Down Expand Up @@ -457,7 +467,7 @@ def colon(self):
def endcolon():
self.lastword = partial(self.inner, self.code)
if self.defining in self.dict:
print 'warning: refining %s' % self.defining
print('warning: refining %s' % self.defining)
self.dict[self.defining] = self.lastword
self.dosemi = endcolon

Expand Down Expand Up @@ -521,7 +531,7 @@ def EXIT(self):

def ACCEPT(self):
(a, n) = self.popn(2)
s = raw_input()[:n]
s = input()[:n]
ns = len(s)
self.ram[a:a + ns] = s
self.lit(ns)
Expand Down Expand Up @@ -667,7 +677,7 @@ def UNLOOP(self):
self.loopC = self.r.pop()

def QUIT(self):
print 'QUIT'
print('QUIT')
raise swapforth.Bye

@setimmediate
Expand Down Expand Up @@ -788,7 +798,10 @@ def putcmd(self, cmd):
self.q('0 >IN !')

import threading
import Queue
try:
import queue
except ImportError:
import Queue as queue

class AsyncSwapForth(SwapForth):

Expand Down Expand Up @@ -832,7 +845,7 @@ def __init__(self, *options):
self.interpreting = False

self.ready = threading.Event()
self.cmdq = Queue.Queue()
self.cmdq = queue.Queue()
self.t = threading.Thread(target = AsyncSwapForth, args = (self.cmdq, self.ready) + options)
self.t.setDaemon(True)
self.t.start()
Expand Down Expand Up @@ -865,9 +878,9 @@ def command_response(self, cmd):
if '-b' in optdict:
endian = '>'
except getopt.GetoptError:
print "usage:"
print " -c N cell size, one of 2,4 or 8"
print " -b big-endian. Default is little-endian"
print("usage:")
print(" -c N cell size, one of 2,4 or 8")
print(" -b big-endian. Default is little-endian")
sys.exit(1)

dpans = {}
Expand All @@ -886,6 +899,6 @@ def command_response(self, cmd):
words = set(t.command_response('words').split())
missing = dpans['CORE'] - words
print(len(missing), "MISSING CORE", " ".join(sorted(missing)))
print words - allw
print(words - allw)

t.shell()
Loading

0 comments on commit a956fe0

Please sign in to comment.