def form_room(x, env): """ ROOM special form handler. This is a special form so can pass current room key down to EXIT. """ key, desc, name, exits = x[1:5] # unpack arguments # evaluate values name = mud.eval(name, env) # need this in case name is a global variable reference desc = mud.eval(desc, env) # ditto # create a new environment with ROOM-KEY, to pass to the EXIT special form parms = ['ROOM-KEY'] args = [key] newenv = mud.Env(parms, args, env) exits = mud.eval(exits, newenv) # parse EXIT special form # return a simple room object room = {'key': key, 'name': name, 'desc': desc, 'exits': exits} return room
def form_cexit(x, env): """ CEXIT special form handler. <CEXIT cond tform fform unk fn> => (eval tform) """ if mud.debug: print 'cexit',x # (_, cond, tform, fform, unk, fn) = x # we just want to eval the tform tform = x[2] value = mud.eval(tform, env) return value
def form_exit(x, env): """ EXIT special form handler. Transform any special exit forms and return as an unconditional exit list, e.g. <EXIT "N" "NHOUSE" "W" (CEXIT ,WH "WHOUS" <>)> => ["N","NHOUS","W","WHOUS"]. """ if mud.debug: print 'exit',x exits = [] tokens = x[1:] while tokens: token = tokens.pop(0) # pop from start of list if isinstance(token, str) and token.startswith(','): val = mud.eval(token) if val is None: if mud.debug: print 'unknown/unparsed gvar',token val = token # leave the value as the plain comma-prefixed token token = val if isinstance(token, mud.List): token = mud.eval(token, env) # evaluate CEXIT, NEXIT, DOOR, SETG exits.append(token) return exits
def form_setg(x, env): """ SETG/PSETG special form handler. Set a global variable value to an evaluated form value and return that value. eg <SETG foo 32> Note that this is different from the set! special form, which sets a local variable. """ if mud.debug: print 'setg',x var = x[1] form = x[2] value = mud.eval(form, env) mud.global_env[',' + var] = value # set global variable value (adding comma is a cheat) return value
def get_rooms(muddle): "Parse the given Muddle code and return a list of ROOM objects." program = "(list " + muddle + ")" objs = mud.eval(mud.parse(program)) # parse the program and get objects rooms = [obj for obj in objs if isinstance(obj, dict)] # filter down to room objects return rooms