- Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathLOLPython.py
More file actions
Latest commit
797 lines (639 loc) · 20.8 KB
/
Copy pathLOLPython.py
File metadata and controls
797 lines (639 loc) · 20.8 KB
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
#!/usr/bin/env python
"""
Implementation of the LOLPython language.
Converts from LOLPython to Python then optionally runs the Python.
This package depends on PLY -- http://www.dabeaz.com/ply/
Written by Andrew Dalke <dalke@dalkescientific.com>
Dalke Scientific Software, LLC
1 June 2007, Gothenburg, Sweden
This software is in the public domain. For details see:
http://creativecommons.org/licenses/publicdomain/
"""
importsys
importkeyword
importos
importtypes
fromcStringIOimportStringIO
fromplyimport*
__NAME__="lolpython"
__VERSION__="1.0"
# Translating LOLPython tokens to Python tokens
# This could be cleaned up. For example, some of
# these tokens could be merged into one.
tokens= (
"NAME", # variable names
"RESERVED", # Used for Python reserved names
"NUMBER", # Integers and floats
"STRING",
"OP", # Like the Python OP
"CLOSE", # Don't really need this..
"COMMENT",
"AUTOCALL", # write t.value then add '('
"INLINE", # write t.value directly
"FUTURE", # for the "I FUTURE CAT WITH" statement
"PRINT", # VISIBLE -> stdout or COMPLAIN -> stderr
"ENDMARKER",
"COLON",
"WS",
"NEWLINE",
)
# Helper functions for making given token types
defOP(t, value):
t.type="OP"
t.value=value
returnt
defRESERVED(t, value):
t.type="RESERVED"
t.value=value
returnt
defAUTOCALL(t, value):
t.type="AUTOCALL"
t.value="tuple"
t.lexer.paren_stack.append(")")
returnt
defINLINE(t, value):
t.type="INLINE"
t.value=value
returnt
#####
# ply uses a large regex for token detection, and sre is limited to
# 100 groups. This grammar pushes the limit. I use (?:non-grouping)
# parens to keep the count down.
deft_ASSIGN(t): # cannot be a simple pattern because it must
r'CAN[ ]+HA[SZ]\b'# come before the t_NAME definition
returnOP(t, "=")
deft_SINGLE_QUOTE_STRING(t):
r"'([^\\']+|\\'|\\\\)*'"# I think this is right ...
t.type="STRING"
t.value=t.value[1:-1].decode("string-escape")
returnt
deft_DOUBLE_QUOTE_STRING(t):
r'"([^\\"]+|\\"|\\\\)*"'
t.type="STRING"
t.value=t.value[1:-1].decode("string-escape")
returnt
# and LOL quoted strings! They end with /LOL
# No way to have "/LOL" in the string.
deft_LOL_STRING(t):
r"LOL[ ]*((?!/LOL).|\n)*[ ]*/LOL"
t.type="STRING"
t.value=t.value[3:-4].strip(" ")
returnt
# Aliases for the same thing - for extra cuteness
deft_LSQUARE(t):
r"(?:SOME|LOOK[ ]AT|LET[ ]+THE)\b"
t.lexer.paren_stack.append(']')
returnOP(t, "[")
deft_LPAREN(t):
r"(?:WIT|THEZ)\b"
t.lexer.paren_stack.append(')')
returnOP(t, "(")
deft_LBRACE(t):
r"BUCKET\b"
t.lexer.paren_stack.append("}")
returnOP(t, "{")
deft_CLOSE(t):
r"(?:OK(!+|\b)|!+)"
stack=t.lexer.paren_stack
ift.value.startswith("OK"):
num_closes=len(t.value)-1# OK -> 1, OK! -> 2, OK!!->3
else:
num_closes=len(t.value) # ! -> 1, !! -> 2
# Which close is this? I use "OK" to match (, [ and {
iflen(stack) <num_closes:
raiseAssertionError("not enough opens on the stack: line %d"
% (t.lineno,))
t.value="".join(stack[-num_closes:][::-1])
delstack[-num_closes:]
returnt
deft_EQ(t):
r"KINDA[ ]+LIKE\b"
returnOP(t, "==")
deft_NE(t):
r"(?:KINDA[ ]+)?NOT[ ]+LIKE\b"
returnOP(t, "!=")
deft_is(t):
r"KINDA[ ]+IS\b"
returnRESERVED(t, "is")
deft_GT(t):
r"ATE[ ]+MORE[ ]+CHEEZBURGERS?[ ]+THAN\b"
returnOP(t, ">")
deft_LT(t):
r"ATE[ ]+FEWER[ ]+CHEEZBURGERS?[ ]+THAN\b"
returnOP(t, "<")
deft_GTE(t):
r"BIG[ ]+LIKE\b"
returnOP(t, ">=")
deft_LTE(t):
r"SMALL[ ]+LIKE\b"
returnOP(t, "<=")
deft_RETURN(t):
r"U[ ]+TAKE\b"
returnRESERVED(t, "return")
deft_yield(t):
r"U[ ]+BORROW\b"
returnRESERVED(t, "yield")
deft_ELIF(t):
r"OR[ ]+IZ\b"
returnRESERVED(t, "elif")
deft_ELSE(t):
r"(?:(?:I[ ]+GIVE[ ]+UP|IZ[ ]+KEWL|ALL[ ]+DONE)|NOPE)\b"
returnRESERVED(t, "else")
deft_COLON(t):
r"\?"
t.value=":"
returnt
deft_FROM(t):
r"IN[ ]+MAI\b"
returnRESERVED(t, "from")
deft_EXCEPT(t):
r"O[ ]+NOES\b"
returnRESERVED(t, "except")
deft_PLUS(t):
r"ALONG[ ]+WITH\b"
returnOP(t, "+")
deft_MINUS(t):
r"TAKE[ ]+AWAY\b"
returnOP(t, "-")
deft_PLUS_EQUAL(t):
r"GETZ[ ]+ANOTHR\b"
returnOP(t, "+=")
deft_MINUS_EQUAL(t):
r"THROW[SZ]?[ ]+AWAY\b"
returnOP(t, "-=")
deft_DIV(t):
r"SMASHES[ ]+INTO\b"
returnOP(t, "/")
deft_DIV_EQUAL(t):
r"SMASHES[ ]+INTO[ ]+HAS\b"
returnOP(t, "/=")
deft_TRUEDIV(t):
r"SMASHES[ ]+NICELY[ ]+INTO\b"
returnOP(t, "//")
deft_MUL(t):
r"OF[ ]THOSE\b"
returnOP(t, "*")
deft_MUL_EQUAL(t):
r"COPIES[ ]+(?:HIM|HER|IT)SELF[ ]+BY\b"
returnOP(t, "*=")
deft_POW(t):
r"BY[ ]+GRAYSKULL[ ]+POWER"
returnOP(t, "**")
deft_IN(t):
r"IN[ ]+(?:UR|THE|THIS)\b"
returnOP(t, "in")
deft_del(t):
r"DO[ ]+NOT[ ]+WANT\b"
returnRESERVED(t, "del")
deft_and(t):
r"\&"
returnRESERVED(t, "and")
deft_or(t):
r"OR[ ]+MABEE\b"
returnRESERVED(t, "or")
deft_pass(t):
r"I[ ]+IZ[ ]+CUTE\b"
returnRESERVED(t, "pass")
deft_forever(t):
r"WHILE[ ]+I[ ]+CUTE\b"
returnINLINE(t, "while 1")
deft_def(t):
r"SO[ ]+IM[ ]+LIKE\b"
returnRESERVED(t, "def")
deft_class(t):
r"ME[ ]+MAKE[ ]\b"
returnRESERVED(t, "class")
deft_future(t):
r"I[ ]+FUTURE[ ]+CAT[ ]+WITH\b"
t.type="FUTURE"
returnt
deft_assert(t):
r"SO[ ]+GOOD\b"
returnRESERVED(t, "assert")
deft_assert_not(t):
r"AINT[ ]+GOOD\b"
returnINLINE(t, "assert not ")
deft_for(t):
r"GIMME[ ]+EACH\b"
returnRESERVED(t, "for")
deft_list(t):
r"ALL[ ]+OF\b"
returnAUTOCALL(t, "tuple")
RESERVED_VALUES= {
"EASTERBUNNY": ("NUMBER", "0"),
"CHEEZBURGER": ("NUMBER", "1"),
"CHOKOLET": ("NUMBER", "-1"),
"TWIN": ("NUMBER", "2"),
"TWINZ": ("NUMBER", "2"),
"TWINS": ("NUMBER", "2"),
"EVILTWIN": ("NUMBER", "-2"),
"EVILTWINZ": ("NUMBER", "-2"),
"EVILTWINS": ("NUMBER", "-2"),
"ALLFINGERZ": ("NUMBER", "10"),
"TOEZ": ("NUMBER", "-10"),
"ONE": ("NUMBER", "1"),
"ONCE": ("NUMBER", "1"),
"TWO": ("NUMBER", "2"),
"TWICE": ("NUMBER", "2"),
"THR33": ("NUMBER", "3"),
"FOUR": ("NUMBER", "4"),
"FIV": ("NUMBER", "5"),
"SIKS": ("NUMBER", "6"),
"SEVN": ("NUMBER", "7"),
"ATE": ("NUMBER", "8"),
"NINE": ("NUMBER", "9"),
"MEH": ("NAME", "False"),
"YEAH": ("NAME", "True"),
"VISIBLE": ("PRINT", "stdout"),
"COMPLAIN": ("PRINT", "stderr"),
"AND": ("OP", ","),
"BLACKHOLE": ("RESERVED", "ZeroDivisionError"),
"DONOTLIKE": ("AUTOCALL", "AssertionError"),
"ANTI": ("OP", "-"),
"IZ": ("RESERVED", "if"),
"GIMME": ("RESERVED", "import"),
"LIKE": ("RESERVED", "as"),
"OWN": ("OP", "."),
"PLZ": ("RESERVED", "try"),
"HALP": ("RESERVED", "raise"),
"WHATEVER": ("RESERVED", "finally"),
"KTHX": ("RESERVED", "continue"),
"KTHXBYE": ("RESERVED", "break"),
"OVER": ("OP", "/"),
"AINT": ("RESERVED", "not"),
"ME": ("RESERVED", "self"),
"STRING": ("AUTOCALL", "str"),
"NUMBR": ("AUTOCALL", "int"),
"BIGNESS": ("AUTOCALL", "len"),
"NUMBRZ": ("AUTOCALL", "range"),
"ADDED": ("AUTOCALL", ".append"),
"ARGZ": ("INLINE", "_lol_sys.argv"),
"THINGZ": ("INLINE", "()"), # invisible tuple didn't sound right
"THING": ("INLINE", "()"), # sometimes it's better in singular form
"MY": ("INLINE", "self."),
"MYSELF": ("INLINE", "(self)"),
"EVEN": ("INLINE", "% 2 == 0"),
"ODD": ("INLINE", "% 2 == 1"),
"WIF": ("RESERVED", "with"),
"ITSLIKETHIS":("RESERVED","class")
}
deft_FLOAT(t):
r"""(?:\d+(?:\.\d*)?|\.\d+)(?:[eE][-+]? \d+)?"""
t.value=t.value
t.type="NUMBER"
returnt
deft_INT(t):
r"\d+"
t.type="NUMBER"
returnt
deft_INVISIBLE(t):
r"INVISIBLE([ ]+(LIST|STRING|BUCKET))?\b"
if"LIST"int.value:
t.type="INLINE"
t.value="[]"
elif"STRING"int.value:
t.type="INLINE"
t.value='""'
elif"BUCKET"int.value:
t.type="INLINE"
t.value="{}"
else:
RESERVED(t, "None")
returnt
# Not consuming the newline. Needed for "IZ EASTERBUNNY? BTW comment"
deft_COMMENT(t):
r"[ ]*(?:BTW|WTF)[^\n]*"
returnt
deft_NAME(t):
r'[a-zA-Z_][a-zA-Z0-9_]*'
ift.valueinRESERVED_VALUES:
type, value=RESERVED_VALUES[t.value]
t.type=type
t.value=value
ift.type=="AUTOCALL":
t.lexer.paren_stack.append(")")
returnt
deft_WS(t):
r' [ ]+ '
ift.lexer.at_line_startandnott.lexer.paren_stack:
returnt
# Don't generate newline tokens when inside of parens
deft_newline(t):
r'\n+'
t.lexer.lineno+=len(t.value)
t.type="NEWLINE"
ifnott.lexer.paren_stack:
returnt
deft_error(t):
raiseSyntaxError("Unknown symbol %r"% (t.value[0],))
print"Skipping", repr(t.value[0])
t.lexer.skip(1)
## I implemented INDENT / DEDENT generation as a post-processing filter
# The original lex token stream contains WS and NEWLINE characters.
# WS will only occur before any other tokens on a line.
# I have three filters. One tags tokens by adding two attributes.
# "must_indent" is True if the token must be indented from the
# previous code. The other is "at_line_start" which is True for WS
# and the first non-WS/non-NEWLINE on a line. It flags the check so
# see if the new line has changed indication level.
# Python's syntax has three INDENT states
# 0) no colon hence no need to indent
# 1) "if 1: go()" - simple statements have a COLON but no need for an indent
# 2) "if 1:\n go()" - complex statements have a COLON NEWLINE and must indent
NO_INDENT=0
MAY_INDENT=1
MUST_INDENT=2
# only care about whitespace at the start of a line
deftrack_tokens_filter(lexer, tokens):
lexer.at_line_start=at_line_start=True
indent=NO_INDENT
fortokenintokens:
token.at_line_start=at_line_start
iftoken.type=="COLON":
at_line_start=False
indent=MAY_INDENT
token.must_indent=False
eliftoken.type=="NEWLINE":
at_line_start=True
ifindent==MAY_INDENT:
indent=MUST_INDENT
token.must_indent=False
eliftoken.type=="WS":
asserttoken.at_line_start==True
at_line_start=True
token.must_indent=False
eliftoken.type=="COMMENT":
pass
else:
# A real token; only indent after COLON NEWLINE
ifindent==MUST_INDENT:
token.must_indent=True
else:
token.must_indent=False
at_line_start=False
indent=NO_INDENT
yieldtoken
lexer.at_line_start=at_line_start
def_new_token(type, lineno):
tok=lex.LexToken()
tok.type=type
tok.value=None
tok.lineno=lineno
tok.lexpos=-1
returntok
# Synthesize a DEDENT tag
defDEDENT(lineno):
return_new_token("DEDENT", lineno)
# Synthesize an INDENT tag
defINDENT(lineno):
return_new_token("INDENT", lineno)
# Track the indentation level and emit the right INDENT / DEDENT events.
defindentation_filter(tokens):
# A stack of indentation levels; will never pop item 0
levels= [0]
token=None
depth=0
prev_was_ws=False
fortokenintokens:
## if 1:
## print "Process", token,
## if token.at_line_start:
## print "at_line_start",
## if token.must_indent:
## print "must_indent",
## print
# WS only occurs at the start of the line
# There may be WS followed by NEWLINE so
# only track the depth here. Don't indent/dedent
# until there's something real.
iftoken.type=="WS":
assertdepth==0
depth=len(token.value)
prev_was_ws=True
# Don't forward WS to the parser
continue
iftoken.type=="NEWLINE":
depth=0
ifprev_was_wsortoken.at_line_start:
# ignore blank lines
continue
# pass the other cases on through
yieldtoken
continue
iftoken.type=="COMMENT":
yieldtoken
continue
# then it must be a real token (not WS, not NEWLINE)
# which can affect the indentation level
prev_was_ws=False
iftoken.must_indent:
# The current depth must be larger than the previous level
ifnot (depth>levels[-1]):
raiseIndentationError("expected an indented block")
levels.append(depth)
yieldINDENT(token.lineno)
eliftoken.at_line_start:
# Must be on the same level or one of the previous levels
ifdepth==levels[-1]:
# At the same level
pass
elifdepth>levels[-1]:
raiseIndentationError("indentation increase but not in new block")
else:
# Back up; but only if it matches a previous level
try:
i=levels.index(depth)
exceptValueError:
raiseIndentationError("inconsistent indentation")
for_inrange(i+1, len(levels)):
yieldDEDENT(token.lineno)
levels.pop()
yieldtoken
### Finished processing ###
# Must dedent any remaining levels
iflen(levels) >1:
asserttokenisnotNone
for_inrange(1, len(levels)):
yieldDEDENT(token.lineno)
# The top-level filter adds an ENDMARKER, if requested.
# Python's grammar uses it.
deftoken_filter(lexer, add_endmarker=True):
token=None
tokens=iter(lexer.token, None)
tokens=track_tokens_filter(lexer, tokens)
fortokeninindentation_filter(tokens):
yieldtoken
ifadd_endmarker:
lineno=1
iftokenisnotNone:
lineno=token.lineno
yield_new_token("ENDMARKER", lineno)
classLOLLexer(object):
def__init__(self, debug=0, optimize=0, lextab='lextab', reflags=0):
self.lexer=lex.lex(debug=debug, optimize=optimize,
lextab=lextab, reflags=reflags)
self.token_stream=None
definput(self, s, add_endmarker=True):
self.lexer.paren_stack= []
self.lexer.input(s)
self.token_stream=token_filter(self.lexer, add_endmarker)
deftoken(self):
try:
returnself.token_stream.next()
exceptStopIteration:
returnNone
# Helper class to generate logically correct indented Python code
classIndentWriter(object):
def__init__(self, outfile):
self.outfile=outfile
self.at_first_column=True
self.indent=0
defwrite(self, text):
ifself.at_first_column:
self.outfile.write(" "*self.indent)
self.at_first_column=False
self.outfile.write(text)
# Split things up because the from __future__ statements must
# go before any other code.
HEADER="""# LOLPython to Python converter version 1.0
# Written by Andrew Dalke, who should have been working on better things.
"""
BODY="""
# sys is used for COMPLAIN and ARGZ
import sys as _lol_sys
"""
defto_python(s):
L=LOLLexer()
L.input(s)
header=StringIO()
header.write(HEADER)
header_output=IndentWriter(header)
body=StringIO()
body.write(BODY)
body_output=IndentWriter(body)
write=body_output.write
output=body_output
fortiniter(L.token_stream):
ift.type=="NAME":
# Need to escape names which are Python variables Do that
# by appending an "_". But then I also need to make sure
# that "yield_" does not collide with "yield". And you
# thought you were being clever trying to use a Python
# variable. :)
name=t.value.rstrip("_")
ifnameinkeyword.kwlist:
write(t.value+"_ ")
else:
write(t.value+" ")
elift.typein ("RESERVED", "OP", "NUMBER", "CLOSE"):
# While not pretty, I'll put a space after each
# term because it's the simplest solution. Otherwise
# I'll need to track the amount of whitespace between
# the tokens in the original text.
write(t.value+" ")
# XXX escape names which are special in Python!
elift.type=="STRING":
write(repr(t.value) +" ")
elift.type=="COMMENT":
# Not enough information to keep comments on the correct
# indentation level. This is good enough. Ugly though.
# Maybe I need to fix the tokenizer.
write("#"+t.value[3:]+"\n")
output.at_first_column=True
elift.type=="COLON":
write(":")
elift.type=="INDENT":
output.indent+=1
pass
elift.type=="DEDENT":
output.indent-=1
pass
elift.type=="NEWLINE":
write(t.value)
output.at_first_column=True
output=body_output
write=output.write
elift.type=="PRINT":
ift.value=="stdout":
write("print ")
elift.value=="stderr":
write("print >>_lol_sys.stderr, ")
else:
raiseAssertionError(t.value)
elift.type=="AUTOCALL":
write(t.value+"(")
elift.type=="INLINE":
write(t.value)
elift.type=="ENDMARKER":
write("\n# The end.\n")
elift.type=="WS":
output.leading_ws=t.value
elift.type=="FUTURE":
# Write to the header. This is a hack. Err, a hairball.
output=header_output
write=output.write
write("from __future__ import ")
else:
raiseAssertionError(t.type)
returnheader.getvalue() +body.getvalue()
# API code for doing the translation and exec'ing the result
defexecfile(infile, module_name="__lolmain__"):
"file, module_name -- exec the lolpython file in a newly created module"
ifnothasattr(infile, "read"):
s=open(infile).read()
else:
s=infile.read()
returnexecstring(s, module_name)
defexecstring(s, module_name="__lolmain__"):
"s, module_name -- exec the lolpython string in a newly created module"
python_s=to_python(s)
# Doing this bit of trickiness so I can have LOLPython code act
# like __main__. This fix is enough to fool unittest.
m=types.ModuleType(module_name)
sys.modules[module_name] =m
execpython_sinm.__dict__
returnm
defconvert_file(infile, outfile):
"read LOLPython code from infile, write converted Python code to outfile"
ifnothasattr(outfile, "write"):
outfile=open(outfile, "w")
outfile.write(to_python(infile.read()))
defconvert(filenames):
"convert LOLPython filenames into corresponding Python '.py' files"
ifnotfilenames:
convert_file(sys.stdin, sys.stdout)
else:
forfilenameinfilenames:
base, ext=os.path.splitext(filename)
convert_file(open(filename), open(base+".py", "w"))
defhelp():
print"""convert and run a lolpython program
Commands are:
lolpython Read a lolpython program from stdin and execute it
lolpython --convert Convert a lolpython program from stdin
and generate python to stdout
lolpython --convert filename1 [filename....]
Convert a list of lolpython files into Python files
lolpython filename [arg1 [arg2 ...]]
Run a lolpython program using optional arguments
"""
defmain(argv):
iflen(argv) >=2:
ifargv[1] =="--convert":
convert(argv[2:])
return
ifargv[1] =="--help":
help()
return
ifargv[1] =="--version":
print__NAME__+" "+__VERSION__
return
# otherwise, run the lolpython program
sys.argv=sys.argv[1:]
filename=sys.argv[0]
execfile(filename, "__main__")
else:
# commands from stdin
execfile(sys.stdin)
if__name__=="__main__":
main(sys.argv)