How to access object attributes given a string
Santiago Romero
sromero at gmail.com
Tue Feb 12 16:19:15 EST 2008
And the rest of the code:
#--------------------------------------------------------------------
def ExecParser_Exec( exec_type, code, game, debug=0 ):
"""
Execute the previously "compiled" code:
"""
code_level = 0
#player = game.player
#world = game.world
# Take only opcodes for EXEC or EXEC2, deppending on exec_type
exec_code = filter( lambda x : x[0] == exec_type, code )
i = -1
while 1:
i += 1
cmd_level, cmd, params = exec_code[i][1:4]
spaces = " "*((code_level+1)*3)
# End of script (appended by the compiler)
if code_level == cmd_level:
if cmd.startswith("IF ") or cmd.startswith("ELIF "):
# Get boolean funtion and evaluate it
# IF true:
# remove all ELIF/ELSE blocks until next ENDIF at the
same code level
# increase codelevel
# IF false:
# remove all code until next ELIF/ELSE/ENDIF at the
same code level
booleanf = cmd.split(" ")[1]
if debug: print "Checking ", spaces, cmd, params,
" (returned",
if ExecParser_CheckBoolean(booleanf, params):
if debug: print "TRUE)"
# Ignore lines until we find an ENDIF at the same code
level:
done = 0
j = i
# Ignore all lines until we found the same code level
(next ELIF/ELSE/ENDIF):
while not done:
j += 1
next_cmd_level, next_cmd = exec_code[j][1:3]
if next_cmd_level==cmd_level or
next_cmd.startswith("END"):
done = 1
nextcond_line = j
# Next branch is an endif, do nothing:
if exec_code[nextcond_line][2].startswith("ENDIF") or \
exec_code[nextcond_line][2] == "END":
pass
# Next branch is ELIF or ELSE: delete all until ENDIF
+cmd_level found
else:
done = 0
while not done:
if exec_code[nextcond_line]
[2].startswith("ENDIF") or \
exec_code[nextcond_line][2] == "END":
done = 1
else:
del exec_code[nextcond_line]
# - Endif -> stop here
code_level += 1
i -= 1
else:
if debug: print "FALSE)"
done = 0
# Ignore all lines in the current
while not done:
i += 1
next_cmd_level, next_cmd = exec_code[i][1:3]
if (next_cmd.startswith("ELIF ") or
next_cmd.startswith("ELSE")):
i -= 1
done = 1
elif next_cmd.startswith("ENDIF") and
(next_cmd_level == code_level):
done = 1
continue
if cmd.startswith("ELSE") and cmd_level != -1:
if debug: print "Entering ", spaces, "ELSE"
code_level += 1
elif cmd.startswith("ENDIF") and cmd_level != -1:
if code_level > 0: code_level -= 1
else:
if code_level == cmd_level:
if cmd == "END":
if debug: print "Executing", spaces, cmd
return 1
else:
if debug: print "Executing", " "*((code_level+1)*3),
cmd, params
ExecParser_ExecOpcode( cmd, params, game, debug )
return 1
#--------------------------------------------------------------------
def ExecParser_PrintCode( code, formatted=0 ):
"""
Print code compiled/parsed by ExecParser_Parse().
Prints opcodes (formatted=0) or indented source code (formatted=1)
from a previously compiled/parsed opcode list.
"""
if not formatted:
# Print opcodes:
print "\n> Compiled opcodes: <"
print "\nExecType CodeLevel Opcode"
print
"----------------------------------------------------------"
for c in code:
print " %-8s %-8s" % (c[0],c[1]),
for f in c[2:]:
if f != []: print f,
print
else:
# Display source code indented
print "\n> Formatted code (parsing opcodes): <"
print "\nMain:"
for i in code:
spaces = " " * 3*(1+i[1])
func = i[2]
params = [ str(x) for x in i[3] ]
if func == "REM":
print "%s%s %s" % (spaces, func, i[3][0])
else:
if params != []:
print "%s%s(" % (spaces, func),
for i,p in enumerate(params):
if i == len(params)-1: print "%s )" % (p)
else: print "%s," % (p),
else :
print "%s%s" % (spaces, func)
That's all. I can put the .py and test.par files online if anyone
wants to test it and point me to the right direction on "how to code
your own small scripting language written in python scripting
language" X-D
More information about the Python-list
mailing list