1

I'm trying to make a "compiler" for my game (so that people could do intresting stuff but not inject code), for mainly declaritive "code" (It would look like this: {"player_location":"IceHall.A7", "print", "You are teleported somewhere", "tiles":{"FirePlace.B3":{'Type':"Corner", "Actions+":{....}}}}. This is how a action is represented; It is called when the player does it. Anyways, it would have to be compiled into a function. When I tryed out something similar in the interactive interpreter (specifically:

def compile(code):
    def act():
       exec code
    return act

). This (which is would be more or less what would be in the final, with the exception of "code" being constructed by me) raised a odd error:

File "", line 3 SyntaxError:
unqualified exec is not allowed in function 'act' it is a nested function.

How do I get around this?

technillogue
  • 1,482
  • 3
  • 16
  • 27
  • 3
    You probably want to write your own parser using a lib like pyparsing. – Jakob Bowyer Jun 29 '11 at 11:03
  • 1
    What exactly does `code` hold? Your program worked fine for me, it did not give any such Syntax Error. Give an example of what string `code` can hold. – Pushpak Dagade Jun 29 '11 at 12:02
  • 6
    Try this [previous response](http://stackoverflow.com/questions/4484872/in-python-why-doesnt-exec-work-in-a-function-with-a-subfunction). Looks like they had the same problem and solved it. – thegrinner Jun 29 '11 at 14:19

1 Answers1

0

The answer, as said in this question, is the lack of context. I wanted exec code in locals(), globals()

Community
  • 1
  • 1
technillogue
  • 1,482
  • 3
  • 16
  • 27