Skip to content

Instantly share code, notes, and snippets.

@mwcampbell
Created October 2, 2011 01:05
Show Gist options
  • Save mwcampbell/1256903 to your computer and use it in GitHub Desktop.
Save mwcampbell/1256903 to your computer and use it in GitHub Desktop.
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