Update of /cvsroot/wisp/wisp/users/dig
In directory sc8-pr-cvs1:/tmp/cvs-serv26686
Modified Files:
tran.py
Log Message:
extracted the Preprocessor class
Index: tran.py
===================================================================
RCS file: /cvsroot/wisp/wisp/users/dig/tran.py,v
retrieving revision 1.15
retrieving revision 1.16
diff -u -d -r1.15 -r1.16
--- tran.py 16 Apr 2003 12:04:20 -0000 1.15
+++ tran.py 16 Apr 2003 13:01:33 -0000 1.16
@@ -33,9 +33,33 @@
try: tok = string.atol(tok, 10)
except: pass
return tok
- def push (this, filename):
+ def push_file (this, filename):
this.push_source(open(filename, 'r'), filename)
+class Macro_Cursor:
+ def __init__ (this, sequence):
+ this.sequence = sequence
+ this.current = 0
+ def get_next (this):
+ if this.current >= len(this.sequence): return None
+ datum = this.sequence[this.current]
+ this.current += 1
+ return datum
+
+class Preprocessor (Lexer):
+ def __init__ (this, filename):
+ Lexer.__init__(this, filename)
+ this.macros = []
+ def push_macro (this, l):
+ this.macros.append(Macro_Cursor(l))
+ def get_token (this):
+ tok = None
+ while this.macros and tok == None:
+ tok = this.macros[-1].get_next()
+ if tok == None: this.macros.pop()
+ if tok <> None: return tok
+ else: return Lexer.get_token(this)
+
class Register (tuple):
def __new__ (cls, *args):
return tuple.__new__(cls, args)
@@ -208,17 +232,17 @@
else:
arg = []
if m[2] & MA_PREFIX:
- tok = get_token()
+ tok = prep.get_token()
if type(tok) <> StringType: raise 'word expected'
arg.append(tok)
apply(m[1], arg)
elif mtype == 'macro':
- Macrostack.append([0, m[1]])
+ prep.push_macro(m[1])
elif mtype == 'simple':
Regstack.append(m[1])
elif mtype == 'include':
- fn = get_token() + '.tran'
- lex.push(fn)
+ fn = prep.get_token() + '.tran'
+ prep.push_file(fn)
else: raise 'Unknown meaning type in', `Meaning[tok]`
def state_record (tok):
global State, current_recordee
@@ -281,31 +305,20 @@
current_recordee = None
current_register_family = 0
State = state_outer
-Macrostack = []
Registers = {Generic_Register: 'reg'} # for reverse translation
-def get_token ():
- if Macrostack:
- tok = Macrostack[-1][1][Macrostack[-1][0]]
- Macrostack[-1][0] += 1
- if Macrostack[-1][0] >= len(Macrostack[-1][1]):
- Macrostack.pop()
- return tok
- else:
- return lex.get_token()
-
def mainloop ():
- tok = get_token()
+ tok = prep.get_token()
while tok <> None:
State(tok)
- tok = get_token()
+ tok = prep.get_token()
def main ():
- global lex
+ global prep
opts, args = getopt(sys.argv[1:], 'o:', ['output='])
if len(args) != 1:
raise 'Invalid argument count -- must be 1', args
- lex = Lexer(args[0])
+ prep = Preprocessor(args[0])
output_name = 'a.out'
for opt, arg in opts:
if opt in ('-o', '--output'): output_name = arg
|