Created
October 2, 2011 01:05
-
-
Save mwcampbell/1256903 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type | |
TExpr = object ## abstract base class for an expression | |
TLiteral = object of TExpr | |
x: int | |
TPlusExpr = object of TExpr | |
a, b: ref TExpr | |
method eval(e: ref TExpr): int = | |
quit "to override!" | |
method eval(e: ref TLiteral): int = return e.x | |
method eval(e: ref TPlusExpr): int = | |
# watch out: relies on dynamic binding | |
return eval(e.a) + eval(e.b) | |
proc newLit(x: int): ref TLiteral = | |
new(result) | |
result.x = x | |
proc newPlus(a, b: ref TExpr): ref TPlusExpr = | |
new(result) | |
result.a = a | |
result.b = b | |
for i in 1..20000000: | |
echo($i) | |
discard eval(newPlus(newPlus(newLit(1), newLit(2)), newLit(4))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment