How to access object attributes given a string

Santiago Romero sromero at gmail.com
Tue Feb 12 16:00:10 EST 2008


 Before I reinvent the wheel, I'm going to post the code.

 Feel free to give any advice, and think that I'm new to python, it's
only 1 month since I began programming in python "seriously" (up to
that moment, I wrote just a few log-text parsing system administration
scripts to speed up some old bash scripts).

 First of all, my currently test file:

KILLOBJECT(coin);\
REM Wait 5 seconds;\
IF FLAG(5);\
   SLEEP(5);\
   PLAYERSAY(test,5);\
   SETMAP(10,10,top,5);\
   IF FLAG(6);\
      NOP();\
      SLEEP(7);\
   ELIF FLAG(7);\
      NOP();\
      SLEEP(9);\
   ELSE;\
      SLEEP(999);\
   ENDIF;\
   IF FLAG_VALUE(7,1);\
      CHANGESCREEN(start,10,100);\
      PLAYERFACING(0);\
   ELSE;\
      PLAYERFACING(1);\
   ENDIF;\
ENDIF;\
SLEEP(12);\
SLEEP(11)

 And the parse code:


if __name__ == "__main__":
   """
   To test: create an example file test.par with language commands.
   """

   numline = 0
   try:
      fp = open("test.par")
   except:
      print "Error, cant open test.par"
      import sys
      sys.exit(0)

   while 1:
      # Read line, ignore comments and stop with EOF
      numline += 1
      line = fp.readline()
      if line == '': break

      line = line.strip()
      startline = numline
      # Join lines splitted with \ in the text file
      while line.endswith("\\"):
         next_line = fp.readline()
         if next_line == '': break
         if next_line.strip() == '': continue
         numline += 1
         next_line = next_line.strip()
         line = line[:-1] + next_line

      code = ExecParser_Parse( "exec", line, startline, "test.par" )

   for i in 0, 1:
      ExecParser_PrintCode( code, i )
      print
   ExecParser_Exec( 1, code, 0, debug=1 )


 And this is the output:

> Compiled opcodes: <

ExecType  CodeLevel      Opcode
----------------------------------------------------------
    1        0        KILLOBJECT ['coin']
    0        0        REM ['Wait 5 seconds']
    1        0        IF FLAG [5]
    1        1        SLEEP [5]
    1        1        PLAYERSAY ['test', 5]
    1        1        SETMAP [10, 10, 'top', 5]
    1        1        IF FLAG [6]
    1        2        NOP
    1        2        SLEEP [7]
    1        1        ELIF FLAG [7]
    1        2        NOP
    1        2        SLEEP [9]
    1        1        ELSE
    1        2        SLEEP [999]
    1        1        ENDIF
    1        1        IF FLAG_VALUE [7, 1]
    1        2        CHANGESCREEN ['start', 10, 100]
    1        2        PLAYERFACING [0]
    1        1        ELSE
    1        2        PLAYERFACING [1]
    1        1        ENDIF
    1        0        ENDIF
    1        0        SLEEP [12]
    1        0        SLEEP [11]
    1        0        END


> Formatted code (parsing opcodes): <

Main:
   KILLOBJECT( coin )
   REM Wait 5 seconds
   IF FLAG( 5 )
      SLEEP( 5 )
      PLAYERSAY( test, 5 )
      SETMAP( 10, 10, top, 5 )
      IF FLAG( 6 )
         NOP
         SLEEP( 7 )
      ELIF FLAG( 7 )
         NOP
         SLEEP( 9 )
      ELSE
         SLEEP( 999 )
      ENDIF
      IF FLAG_VALUE( 7, 1 )
         CHANGESCREEN( start, 10, 100 )
         PLAYERFACING( 0 )
      ELSE
         PLAYERFACING( 1 )
      ENDIF
   ENDIF
   SLEEP( 12 )
   SLEEP( 11 )
   END

Executing     KILLOBJECT ['coin']
Checking      IF FLAG [5]  (returned TRUE)
Executing        SLEEP [5]
Executing        PLAYERSAY ['test', 5]
Executing        SETMAP [10, 10, 'top', 5]
Checking         IF FLAG [6]  (returned FALSE)
Checking         ELIF FLAG [7]  (returned FALSE)
Entering         ELSE
Executing           SLEEP [999]
Checking         IF FLAG_VALUE [7, 1]  (returned FALSE)
Entering         ELSE
Executing           PLAYERFACING [1]
Executing     SLEEP [12]
Executing     SLEEP [11]
Executing     END



More information about the Python-list mailing list