diff options
author | Martin Odersky <odersky@gmail.com> | 2003-04-02 12:50:48 +0000 |
---|---|---|
committer | Martin Odersky <odersky@gmail.com> | 2003-04-02 12:50:48 +0000 |
commit | a3d34c650abb28375a64b417b3c934d948f4fc69 (patch) | |
tree | 904256b5b7c5abf8b4b0e0612ef44ef7061ec926 /sources/examples/expressions | |
parent | 3ceaf4b02d0adc4eab065dca3c7c7cac16ef36b9 (diff) | |
download | scala-a3d34c650abb28375a64b417b3c934d948f4fc69.tar.gz scala-a3d34c650abb28375a64b417b3c934d948f4fc69.tar.bz2 scala-a3d34c650abb28375a64b417b3c934d948f4fc69.zip |
*** empty log message ***
Diffstat (limited to 'sources/examples/expressions')
-rw-r--r-- | sources/examples/expressions/expressions-current.scala | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/sources/examples/expressions/expressions-current.scala b/sources/examples/expressions/expressions-current.scala new file mode 100644 index 0000000000..8783fc9544 --- /dev/null +++ b/sources/examples/expressions/expressions-current.scala @@ -0,0 +1,63 @@ +package test; + +abstract class Lang { + trait Visitor { + def caseNum(n: int): unit; + } + + abstract class Exp { + def visit(v: visitor): unit; + } + + type visitor <: Visitor; + + class Num(n: int) extends Exp { + def visit(v: visitor): unit = v.caseNum(n); + } + + class Eval(result: Ref[int]): visitor extends Visitor { + def caseNum(n: int) = result.elem = n; + } +} + +abstract class Lang2 extends Lang { + abstract class Visitor2 extends Visitor { + def casePlus(left: Exp, right: Exp): unit; + } + + type visitor <: Visitor2; + + class Plus(l: Exp, r: Exp) extends Exp { + def visit(v: visitor): unit = v.casePlus(l, r); + } + + class Eval2(result: Ref[int]): visitor extends Eval(result) with Visitor2 { + def casePlus(l: Exp, r: Exp) = + result.elem = { l.visit(this); result.elem } + { r.visit(this); result.elem } + } + + class Show2(result: Ref[String]): visitor extends Visitor2 { + def caseNum(n: int) = result.elem = n.toString(); + def casePlus(l: Exp, r: Exp) = + result.elem = + "(" + { l.visit(this); result.elem } + + "+" + { r.visit(this); result.elem }+ ")"; + } +} + +object Main { + + def main(args: Array[String]) = { + val l1 = new Lang { type visitor = Visitor } + val e1: l1.Exp = new l1.Num(42); + + val iref = new Ref(0); + System.out.println("eval: " + { e1.visit(new l1.Eval(iref)); iref.elem }); + + val l2 = new Lang2 { type visitor = Visitor2 } + val e2: l2.Exp = new l2.Plus(new l2.Num(5), new l2.Num(37)); + val sref = new Ref(""); + System.out.println("eval: " + { e2.visit(new l2.Eval2(iref)); iref.elem }); + System.out.println("show: " + { e2.visit(new l2.Show2(sref)); sref.elem }); + } +} |