Parrot YAML ain't machine language

Bernhard on 2004-11-28T15:17:32

In the last weeks I have used some off-work hours for looking into Abstract Syntax Tree for Parrot. It looks like this is one of the less civilised areas of Parrot, as documentation is somewhat scarce.

Leo Tötsch added support for AST when coding for the infamous Piethon bet. So far one can take a Python program and turn it into PAST, Parro abstract syntax tree As Simple Text.

bernhard@linux:~/devel/Parrot/parrot/languages/python> cat hello_10.py
print 10;
bernhard@linux:~/devel/Parrot/parrot/languages/python> ./ast2past.py hello_10.py > hello_10.past
bernhard@linux:~/devel/Parrot/parrot/languages/python> more hello_10.past

# generated by ast2past.py on Sun Nov 28 16:06:29 2004

Parrot_AST(
  version(Const('0.1'))
 _options(
 ) # _options
 Src_File("hello_10.py")
 Py_Module(
  _()
  Stmts(
   Py_Print(
    Const(10)
   ) # Py_Print
   Py_Print_nl()
   Void(
    Const(None)
   ) # Void
  ) # Stmts
 ) # Py_Module
 # end
) # Parrot_AST
bernhard@linux:~/devel/Parrot/parrot/languages/python> ../../parrot hello_10.past
10
I have been trying to make that functionality available as a Parrot registered compiler. After making some educated guesses, it looks like it might be working.
bernhard@linux:~/devel/Parrot/parrot> cat eval_9.imc
.sub main @MAIN
     .local pmc past_compiler
     past_compiler = compreg "PAST"
     .local string past_source
     past_source = 'Parrot_AST( Py_Module( Stmts( Py_Print( Const(9) ) ) ) )'
     .local pmc past_compiled_sub
     past_compiled_sub = compile past_compiler, past_source
     print "before\n"
     past_compiled_sub()
     print "after\n"
     end
.end

bernhard@linux:~/devel/Parrot/parrot> ./parrot eval_9.imc
before
9after
bernhard@linux:~/devel/Parrot/parrot>

I still need to check wether the python tests are still working. If so I could add and some AST compiler tests.

The idea of Abstract Syntax Trees is mostly that the representation, that is the programming language, doesn't matter much. So it should't be hard to add support for any other tree format. For example one could use YAML. This might bring future support for Parrot YAML, which is an acronym for "Parrot YAML Ain't Machine Language".