-def chew():
- # Demand input for next scan
- global inqueue
- inqueue = None
-
-def chew2():
- # return IHEOL next time
- global inqueue
- inqueue = []
-
-def scan():
- # Get a token from the user
- global inqueue, line, citem, aaitem
- aaitem = 0.0
- citem = ''
-
- # Read a line if nothing here
- if inqueue == None:
- line = cgetline()
- if curwnd==prompt_window:
- clrscr()
- setwnd(message_window)
- clrscr()
- # Skip leading white space
- line = line.lstrip()
- if line:
- inqueue = line.split()
- else:
- inqueue = []
- return IHEOL
- elif not inqueue:
- return IHEOL
- # From here on in it's all looking at the queue
- citem = inqueue.pop(0)
- if citem == IHEOL:
- return IHEOL
- try:
- aaitem = float(citem)
- return IHREAL
- except ValueError:
- pass
- # Treat as alpha
- citem = citem.lower()
- return IHALPHA
+class sstscanner:
+ def __init__(self):
+ self.type = None
+ self.token = None
+ self.real = 0.0
+ self.inqueue = []
+ def next(self):
+ # Get a token from the user
+ self.real = 0.0
+ self.token = ''
+ # Fill the token quue if nothing here
+ while not self.inqueue:
+ line = cgetline()
+ if curwnd==prompt_window:
+ clrscr()
+ setwnd(message_window)
+ clrscr()
+ if line == '':
+ return None
+ # Skip leading white space
+ line = line.lstrip()
+ if not line:
+ continue
+ else:
+ self.inqueue = line.lstrip().split() + ["IHEOL"]
+ # From here on in it's all looking at the queue
+ self.token = self.inqueue.pop(0)
+ if self.token == "IHEOL":
+ self.type = "IHEOL"
+ return "IHEOL"
+ try:
+ self.real = float(self.token)
+ self.type = "IHREAL"
+ return "IHREAL"
+ except ValueError:
+ pass
+ # Treat as alpha
+ self.token = self.token.lower()
+ self.type = "IHALPHA"
+ self.real = None
+ return "IHALPHA"
+ def push(self, tok):
+ self.inqueue.append(tok)
+ def waiting(self):
+ return self.inqueue
+ def chew(self):
+ # Demand input for next scan
+ self.inqueue = []
+ self.real = self.token = None
+ def chew2(self):
+ # return "IHEOL" next time
+ self.inqueue = ["IHEOL"]
+ self.real = self.token = None
+ def sees(self, s):
+ # compares s to item and returns true if it matches to the length of s
+ return s.startswith(self.token)
+ def int(self):
+ # Round token value to nearest integer
+ return int(round(scanner.real))
+ def getcoord(self):
+ s = coord()
+ scanner.next()
+ if scanner.type != "IHREAL":
+ huh()
+ return None
+ s.i = scanner.int()-1
+ scanner.next()
+ if scanner.type != "IHREAL":
+ huh()
+ return None
+ s.j = scanner.int()-1
+ return s
+ def __repr__(str):
+ return "<sstcanner: token=%s, type=%s, queue=%s>" % (scanner.token, scanner.type, scanner.inqueue)