2. About me
• Computer science bachelor student at TU Berlin!
• Programming/Python since ~2008!
• Primarily involved with Pocoo projects (Sphinx,
Werkzeug, Flask, Babel, …)
3. PyPy Python Interpreter
• Fast Python implementation!
• Just-in-Time compilation!
• Proper garbage collection (no reference counting)!
• Written in Python
5. PyPy based interpreters
• Topaz (Ruby)!
• HippyVM (PHP)!
• Pyrolog (Prolog)!
• pycket (Racket)!
• Various other interpreters for (Scheme, Javascript,
io, Gameboy)
6. RPython
• Python subset!
• Statically typed!
• Garbage collected!
• Standard library almost entirely unavailable!
• Some missing builtins (print, open(), …)!
• rpython.rlib!
• exceptions are (sometimes) ignored!
• Not a really a language, rather a "state"
13. 10 PRINT TAB(32);"HAMURABI"
20 PRINT TAB(15);"CREATIVE COMPUTING MORRISTOWN, NEW JERSEY"
30 PRINT:PRINT:PRINT
80 PRINT "TRY YOUR HAND AT GOVERNING ANCIENT SUMERIA"
90 PRINT "FOR A TEN-YEAR TERM OF OFFICE.":PRINT
95 D1=0: P1=0
100 Z=0: P=95:S=2800: H=3000: E=H-S
110 Y=3: A=H/Y: I=5: Q=1
210 D=0
215 PRINT:PRINT:PRINT "HAMURABI: I BEG TO REPORT TO YOU,": Z=Z+1
217 PRINT "IN YEAR";Z;",";D;"PEOPLE STARVED,";I;"CAME TO THE CITY,"
218 P=P+I
227 IF Q>0 THEN 230
228 P=INT(P/2)
229 PRINT "A HORRIBLE PLAGUE STRUCK! HALF THE PEOPLE DIED."
230 PRINT "POPULATION IS NOW";P
232 PRINT "THE CITY NOW OWNS ";A;"ACRES."
235 PRINT "YOU HARVESTED";Y;"BUSHELS PER ACRE."
250 PRINT "THE RATS ATE";E;"BUSHELS."
260 PRINT "YOU NOW HAVE ";S;"BUSHELS IN STORE.": PRINT
270 REM *** MORE CODE THAT DID NOT FIT INTO THE SLIDE FOLLOWS
19. >>> from basic.lexer import lex
>>> source = open("hello.bas").read()
>>> for token in lex(source):
... print token
Token("NUMBER", "10")
Token("PRINT", "PRINT")
Token("STRING",'"HELLO BASIC!"')
Token(":", "n")
20. Grammar
• A set of formal rules that defines the syntax!
• terminals = tokens!
• nonterminals = rules defining a sequence of one or
more (non)terminals
21. 10 PRINT TAB(32);"HAMURABI"
20 PRINT TAB(15);"CREATIVE COMPUTING MORRISTOWN, NEW JERSEY"
30 PRINT:PRINT:PRINT
80 PRINT "TRY YOUR HAND AT GOVERNING ANCIENT SUMERIA"
90 PRINT "FOR A TEN-YEAR TERM OF OFFICE.":PRINT
95 D1=0: P1=0
100 Z=0: P=95:S=2800: H=3000: E=H-S
110 Y=3: A=H/Y: I=5: Q=1
210 D=0
215 PRINT:PRINT:PRINT "HAMURABI: I BEG TO REPORT TO YOU,": Z=Z+1
217 PRINT "IN YEAR";Z;",";D;"PEOPLE STARVED,";I;"CAME TO THE CITY,"
218 P=P+I
227 IF Q>0 THEN 230
228 P=INT(P/2)
229 PRINT "A HORRIBLE PLAGUE STRUCK! HALF THE PEOPLE DIED."
230 PRINT "POPULATION IS NOW";P
232 PRINT "THE CITY NOW OWNS ";A;"ACRES."
235 PRINT "YOU HARVESTED";Y;"BUSHELS PER ACRE."
250 PRINT "THE RATS ATE";E;"BUSHELS."
260 PRINT "YOU NOW HAVE ";S;"BUSHELS IN STORE.": PRINT
270 REM *** MORE CODE THAT DID NOT FIT INTO THE SLIDE FOLLOWS
70. class Line(BaseBox):
...
def compile(self, program):
program.lineno2instruction[self.lineno] = len(program.instructions)
for statement in self.statements:
statement.compile(program)
71. class Line(BaseBox):
...
def compile(self, program):
program.lineno2instruction[self.lineno] = len(program.instructions)
for statement in self.statements:
statement.compile(program)
72. class Print(Statement):
def compile(self, program):
for expression in self.expressions:
expression.compile(program)
program.instructions.append(
bytecode.Print(
len(self.expressions),
self.newline
)
)
73. class Print(Statement):
...
def compile(self, program):
for expression in self.expressions:
expression.compile(program)
program.instructions.append(
bytecode.Print(
len(self.expressions),
self.newline
)
)
93. Benchmark
10 N = 1
20 IF N <= 10000 THEN 40
30 END
40 GOSUB 100
50 IF R = 0 THEN 70
60 PRINT "PRIME"; N
70 N = N + 1: GOTO 20
100 REM *** ISPRIME N -> R
110 IF N <= 2 THEN 170
120 FOR I = 2 TO (N - 1)
130 A = N: B = I: GOSUB 200
140 IF R <> 0 THEN 160
150 R = 0: RETURN
160 NEXT I
170 R = 1: RETURN
200 REM *** MOD A -> B -> R
210 R = A - (B * INT(A / B))
220 RETURN