Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > compiler module supports 'statements as expressions'

Reply
Thread Tools

compiler module supports 'statements as expressions'

 
 
Tom Locke
Guest
Posts: n/a
 
      05-04-2004
Hi,

I am writing a compiler (in python) that targets python byte-code,
using compiler.ast and compiler.pycodegen.

The language is lisp-like in that there is no statement / expression
separation.

e.g. I can do (in a python-like syntax)

print (if x: "yes" else: "no")

or

while line = raw_input("> "); line != "":
print line

Having hacked away for a short while on a translator to make these
things into separate statements that would be legal in Python, I was
VERY surprised when I accidentally discovered that this seems to be
LEGAL at the byte-code level. (CPython 2.3.3, Win XP)

compiler.pycodegen.ModuleCodeGenerator generates working code for AST
fragments like these:

Printnl([If([(Name('x'), Stmt([Const('yes')]))],
Stmt([Const('no')]))],
None)

and

While(Stmt([Assign([AssName('line', 'OP_ASSIGN')],
CallFunc(Name('raw_input'), [Const('> ')],
None, None)),
Compare(Name('line'), [('!=', Const(''))])]),
Stmt([Printnl([Name('line')], None)]), None)]))

AST 'statement' nodes, when used in an expression context, evaluate as
follows.

If: value of the executed clause

Stmt: value of the last statement in the list

While and Assign: Undefined – python crashes if you try to do
something with these values.

This is really very helpful for me – it's just the semantics I wanted!

I think I'm right in saying that there's no legal Python that parses
into ASTs like these, right?

Can I rely on this?? Is it there for a reason? (e.g. to support
certain optimizations?) Is this just serendipity – the 'natural'
behavior of the stack based byte-code interpreter?

Thanks,

Tom.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
VoIP phone supports IAX2 and SIP,Supports Auto-provision! gery VOIP 4 12-16-2007 10:31 AM
VoIP phone supports IAX2 and SIP,Supports Auto-provision! gery VOIP 0 10-22-2007 03:28 AM
Any compiler supports 16 bytes float on IA-32? Wei-Chao Hsu C++ 1 07-26-2004 12:56 PM
How to determine (in compile time) whether a compiler supports wide characters or not Ashabul Yeameen C Programming 2 09-17-2003 06:35 PM
How to determine whether a compiler supports wide character or not Ashabul Yeameen C Programming 1 09-16-2003 07:45 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57