SkidanovAlex / interpreter

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

interpreter

This is the code for the article originally published on habrahabr:

http://habrahabr.ru/post/148901/

There's an english version of the article:

http://kukuruku.co/hub/algorithms/using-the-quick-raise-of-matrices-to-power-to-write-a-very-fast-interpreter-of-a-simple-programming-language

Interpreter of a very simple language, working on a set of four variables (A, B, C and D) and supporting the following set of operations:

V = A
V += A
V -= A
V *= C

Where V is any variable, C is a constant and A is either variable or a constant. It also supports loops, for example

A = 1
loop 100
A *= 2
end
end

(here first end ends the loop and the second end ends the program) will compute 2 to the power of 100. The interesting thing about this interpreter is that it performs loops in O(log N) operations, where N is the number of iterations. For example the following code will compute the sum of first 10^18 numbers:

loop 1000000000
loop 1000000000
A += 1
B += A
end
end
end

It will immediately print

[1000000000000000000L, 500000000000000000500000000000000000L, 0L, 0L]

Where second number represents the value of B after the program execution.

About


Languages

Language:Python 100.0%