diff options
Diffstat (limited to 'sources')
-rw-r--r-- | sources/meta/scalac/ast/Tree.java | 23 | ||||
-rw-r--r-- | sources/scala/Except.scala | 10 | ||||
-rw-r--r-- | sources/scala/Monitor.scala | 3 | ||||
-rw-r--r-- | sources/scala/Predef.scala | 23 | ||||
-rw-r--r-- | sources/scala/runtime/NativeLoop.java (renamed from sources/scala/NativeLoop.java) | 2 | ||||
-rw-r--r-- | sources/scala/runtime/NativeMonitor.java (renamed from sources/scala/NativeMonitor.java) | 2 | ||||
-rw-r--r-- | sources/scala/runtime/ScalaRunTime.scala | 35 | ||||
-rw-r--r-- | sources/scalac/ast/parser/Parser.java | 106 | ||||
-rw-r--r-- | sources/scalac/ast/parser/Scanner.java | 7 | ||||
-rw-r--r-- | sources/scalac/ast/parser/Tokens.java | 6 | ||||
-rw-r--r-- | sources/scalac/util/Names.java | 6 |
11 files changed, 177 insertions, 46 deletions
diff --git a/sources/meta/scalac/ast/Tree.java b/sources/meta/scalac/ast/Tree.java index 7d6568cbe6..9f0af9759a 100644 --- a/sources/meta/scalac/ast/Tree.java +++ b/sources/meta/scalac/ast/Tree.java @@ -102,7 +102,10 @@ public class Tree { n_SelectFromType = node("SelectFromType", Type, HasSym), n_FunType = node("FunType" , Type, NoSym), n_CompoundType = node("CompoundType" , Type, NoSym), - n_AppliedType = node("AppliedType" , Type, NoSym); + n_AppliedType = node("AppliedType" , Type, NoSym), + n_Try = node("Try" , Term, NoSym), + n_While = node("While" , Term, NoSym), + n_DoUntil = node("DoUntil" , Term, NoSym); public final TreeNode[] nodes; public int arrays; @@ -336,6 +339,24 @@ public class Tree { addField(t_TypeTree, "tpe"). addField(t_TypeTrees, "args"); + n_Try. + setDescription("Try Expression"). + setRange(Phase.PARSER, Phase.END). + addField(t_TermTree, "block"). + addField(t_TermTree, "catcher"). + addField(t_TermTree, "finalizer"); + + n_While. + setDescription("While Loop"). + setRange(Phase.PARSER, Phase.END). + addField(t_TermTree, "cond"). + addField(t_TermTree, "block"); + + n_DoUntil. + setDescription("Do-Until Loop"). + setRange(Phase.PARSER, Phase.END). + addField(t_TermTree, "block"). + addField(t_TermTree, "cond"); } //######################################################################## diff --git a/sources/scala/Except.scala b/sources/scala/Except.scala deleted file mode 100644 index ce73a35e9c..0000000000 --- a/sources/scala/Except.scala +++ /dev/null @@ -1,10 +0,0 @@ -package scala; - -class Except[a](r: scala.runtime.ResultOrException[a]) { - def except[b >: a](handler: PartialFunction[Throwable, b]): b = - if (r.exc == null) r.result as b - else if (handler isDefinedAt r.exc) handler(r.exc) - else r.exc.throw; - def finally(def handler: Unit): a = - if (r.exc == null) r.result as a else { handler; r.exc.throw } -} diff --git a/sources/scala/Monitor.scala b/sources/scala/Monitor.scala index 168f72a6ca..08e6201612 100644 --- a/sources/scala/Monitor.scala +++ b/sources/scala/Monitor.scala @@ -11,7 +11,8 @@ package scala; trait Monitor { - def synchronized[a](def p: a): a = NativeMonitor.synchronised(this, p); + def synchronized[a](def p: a): a = + scala.runtime.NativeMonitor.synchronised(this, p); def await(def cond: Boolean) = while (!cond) { this.wait() } } diff --git a/sources/scala/Predef.scala b/sources/scala/Predef.scala index 218b5d926e..db81ed9576 100644 --- a/sources/scala/Predef.scala +++ b/sources/scala/Predef.scala @@ -33,27 +33,8 @@ object Predef { def exit: scala.Unit = System.exit(0); - def synchronized[A](obj: AnyRef)(def body: A) = NativeMonitor.synchronised(obj, body); - - def try[a](def block: a): Except[a] = - new Except(scala.runtime.ResultOrException.tryBlock(block)); - - def while(def cond: Boolean)(def body: Unit): Unit = NativeLoop.loopWhile(cond, body); - /* if (cond) { - body; while(cond)(body) - } */ - - trait Until { - def until(def condition: Boolean): Unit - } - - def repeat(def command: Unit): Until = - new Until { - def until(def condition: Boolean): Unit = { - command ; - if (condition) {} else until(condition) - } - } + def synchronized[A](obj: AnyRef)(def body: A) = + scala.runtime.NativeMonitor.synchronised(obj, body); type Pair = Tuple2; def Pair[a, b](x: a, y: b) = Tuple2(x, y); diff --git a/sources/scala/NativeLoop.java b/sources/scala/runtime/NativeLoop.java index 921259b90b..b849bd222d 100644 --- a/sources/scala/NativeLoop.java +++ b/sources/scala/runtime/NativeLoop.java @@ -7,7 +7,7 @@ ** $Id$ \* */ -package scala; +package scala.runtime; public class NativeLoop { diff --git a/sources/scala/NativeMonitor.java b/sources/scala/runtime/NativeMonitor.java index ade1a10110..0afda00674 100644 --- a/sources/scala/NativeMonitor.java +++ b/sources/scala/runtime/NativeMonitor.java @@ -7,7 +7,7 @@ ** $Id$ \* */ -package scala; +package scala.runtime; public class NativeMonitor { diff --git a/sources/scala/runtime/ScalaRunTime.scala b/sources/scala/runtime/ScalaRunTime.scala new file mode 100644 index 0000000000..18bbcb27a7 --- /dev/null +++ b/sources/scala/runtime/ScalaRunTime.scala @@ -0,0 +1,35 @@ +package scala.runtime; + +object ScalaRunTime { + + class Try[a](r: scala.runtime.ResultOrException[a]) { + def Catch[b >: a](handler: PartialFunction[Throwable, b]): b = + if (r.exc == null) + r.result as b + else if (/*!(r.exc is NonLocalReturn) && */handler isDefinedAt r.exc) + handler(r.exc) + else + r.exc.throw; + + def Finally(handler: Unit): a = + if (r.exc == null) r.result as a else r.exc.throw; + } + + def Try[a](def block: a): Try[a] = + new Try(ResultOrException.tryBlock(block)); + + def While(def cond: Boolean)(def body: Unit): Unit = + NativeLoop.loopWhile(cond, body); + + trait DoWhile { + def While(def condition: Boolean): Unit; + } + + def Do(def command: Unit): DoWhile = + new DoWhile { + def While(def condition: Boolean): Unit = { + command; + NativeLoop.loopWhile(condition, command); + } + } +} diff --git a/sources/scalac/ast/parser/Parser.java b/sources/scalac/ast/parser/Parser.java index 87c8989f33..1f6c7e31f8 100644 --- a/sources/scalac/ast/parser/Parser.java +++ b/sources/scalac/ast/parser/Parser.java @@ -153,6 +153,7 @@ public class Parser implements Tokens { case SYMBOLLIT: case TRUE: case FALSE: case NULL: case IDENTIFIER: case THIS: case SUPER: case IF: case FOR: case NEW: case USCORE: + case TRY: case WHILE: case DO: case LPAREN: case LBRACE: return true; default: @@ -199,6 +200,14 @@ public class Parser implements Tokens { return make.Select(pos, make.Ident(pos, Names.scala), name); } + Tree scalaRuntimeDot(int pos, Name name) { + return make.Select(pos, scalaDot(pos, Names.runtime), name); + } + + Tree ScalaRunTimeDot(int pos, Name name) { + return make.Select(pos, scalaRuntimeDot(pos, Names.ScalaRunTime), name); + } + Tree scalaBooleanDot(int pos, Name name) { return make.Select(pos, scalaDot(pos, Names.Boolean), name); } @@ -253,6 +262,54 @@ public class Parser implements Tokens { return make.Apply(pos, make.Select(pos, rhs, name), new Tree[]{cont}); } + Tree makeTry(int pos, Tree body, Tree catcher, Tree finalizer) { + Tree t = body; + if (catcher != Tree.Empty) + t = + make.Apply( + pos, + make.Select( + pos, + make.Apply( + pos, ScalaRunTimeDot(pos, Names.Try), new Tree[]{t}), + Names.Catch), + new Tree[]{catcher}); + if (finalizer != Tree.Empty) + t = + make.Apply( + pos, + make.Select( + pos, + make.Apply( + pos, ScalaRunTimeDot(pos, Names.Try), new Tree[]{t}), + Names.Finally), + new Tree[]{finalizer}); + return t; + } + + Tree makeWhile(int pos, Tree cond, Tree body) { + return + make.Apply( + pos, + make.Apply( + pos, ScalaRunTimeDot(pos, Names.While), new Tree[]{cond}), + new Tree[]{body}); + } + + Tree makeDoWhile(int pos, Tree body, Tree cond) { + return + make.Apply( + pos, + make.Select( + pos, + make.Apply( + pos, + ScalaRunTimeDot(pos, Names.Do), + new Tree[]{body}), + Names.While), + new Tree[]{cond}); + } + /** Convert tree to formal parameter list */ ValDef[] convertToParams(Tree t) { @@ -674,6 +731,9 @@ public class Parser implements Tokens { /** Expr ::= Bindings `=>' Expr * | if `(' Expr `)' Expr [[`;'] else Expr] + * | try `{' block `}' [catch Expr] [finally Expr] + * | while `(' Expr `)' Expr + * | do Expr [`;'] while `(' Expr `)' * | for `(' Enumerators `)' (do | yield) Expr * | [SimpleExpr `.'] Id `=' Expr * | SimpleExpr ArgumentExprs `=' Expr @@ -696,19 +756,45 @@ public class Parser implements Tokens { } else { elsep = Tree.Empty; } - return make.If(pos, cond, thenp, elsep) ; + return make.If(pos, cond, thenp, elsep); + } else if (s.token == TRY) { + int pos = s.skipToken(); + accept(LBRACE); + Tree body = block(pos); + accept(RBRACE); + Tree catcher = Tree.Empty; + if (s.token == CATCH) { + s.nextToken(); + catcher = expr(); + } + Tree finalizer = Tree.Empty; + if (s.token == FINALLY) { + s.nextToken(); + finalizer = expr(); + } + return makeTry(pos, body, catcher, finalizer); + } else if (s.token == WHILE) { + int pos = s.skipToken(); + accept(LPAREN); + Tree cond = expr(); + accept(RPAREN); + Tree body = expr(); + return makeWhile(pos, cond, body); + } else if (s.token == DO) { + int pos = s.skipToken(); + Tree body = expr(); + if (s.token == SEMI) s.nextToken(); + accept(WHILE); + accept(LPAREN); + Tree cond = expr(); + accept(RPAREN); + return makeDoWhile(pos, body, cond); } else if (s.token == FOR) { s.nextToken(); Tree[] enums; - if (s.token == LBRACE) { - accept(LBRACE); - enums = enumerators(); - accept(RBRACE); - } else { - accept(LPAREN); - enums = enumerators(); - accept(RPAREN); - } + accept(LPAREN); + enums = enumerators(); + accept(RPAREN); if (s.token == DO) { return makeFor(s.skipToken(), enums, Names.foreach, Names.foreach, expr()); } else if (s.token == YIELD) { diff --git a/sources/scalac/ast/parser/Scanner.java b/sources/scalac/ast/parser/Scanner.java index 5573ce3e05..da022bd953 100644 --- a/sources/scalac/ast/parser/Scanner.java +++ b/sources/scalac/ast/parser/Scanner.java @@ -115,7 +115,7 @@ public class Scanner extends TokenData { fetchToken(); switch (token) { case ELSE: case EXTENDS: case WITH: - case YIELD: case DO: + case YIELD: case CATCH: case FINALLY: case COMMA: case SEMI: case DOT: case COLON: case EQUALS: case ARROW: case LARROW: case SUBTYPE: case SUPERTYPE: @@ -798,6 +798,7 @@ public class Scanner extends TokenData { enterKeyword("as", AS); enterKeyword("case", CASE); enterKeyword("class", CLASS); + enterKeyword("catch", CATCH); enterKeyword("constr", CONSTR); enterKeyword("def", DEF); enterKeyword("do", DO); @@ -805,6 +806,7 @@ public class Scanner extends TokenData { enterKeyword("extends", EXTENDS); enterKeyword("false", FALSE); enterKeyword("final", FINAL); + enterKeyword("finally", FINALLY); enterKeyword("for", FOR); enterKeyword("if", IF); enterKeyword("import", IMPORT); @@ -816,15 +818,18 @@ public class Scanner extends TokenData { enterKeyword("package", PACKAGE); enterKeyword("private", PRIVATE); enterKeyword("protected", PROTECTED); + enterKeyword("return", RETURN); enterKeyword("sealed", SEALED); enterKeyword("super", SUPER); enterKeyword("this", THIS); enterKeyword("trait", TRAIT); enterKeyword("true", TRUE); + enterKeyword("try", TRY); enterKeyword("type", TYPE); enterKeyword("val", VAL); enterKeyword("var", VAR); enterKeyword("with", WITH); + enterKeyword("while", WHILE); enterKeyword("yield", YIELD); enterKeyword(".", DOT); enterKeyword("_", USCORE); diff --git a/sources/scalac/ast/parser/Tokens.java b/sources/scalac/ast/parser/Tokens.java index f2e3bc39ad..9ec7878918 100644 --- a/sources/scalac/ast/parser/Tokens.java +++ b/sources/scalac/ast/parser/Tokens.java @@ -61,6 +61,12 @@ public interface Tokens { DO = 51, TRAIT = 52, SEALED = 53, + /* THROW = 54, */ + TRY = 55, + CATCH = 56, + FINALLY = 57, + WHILE = 58, + RETURN = 59, /* special symbols */ COMMA = 61, diff --git a/sources/scalac/util/Names.java b/sources/scalac/util/Names.java index 1c394a8ba2..069fafbbad 100644 --- a/sources/scalac/util/Names.java +++ b/sources/scalac/util/Names.java @@ -37,9 +37,12 @@ public class Names { public static final Name AnyRef = Name.fromString("AnyRef"); public static final Name Array = Name.fromString("Array"); public static final Name Byte = Name.fromString("Byte"); + public static final Name Catch = Name.fromString("Catch"); public static final Name Char = Name.fromString("Char"); public static final Name Boolean = Name.fromString("Boolean"); + public static final Name Do = Name.fromString("Do"); public static final Name Double = Name.fromString("Double"); + public static final Name Finally = Name.fromString("Finally"); public static final Name Float = Name.fromString("Float"); public static final Name Function = Name.fromString("Function"); public static final Name Int = Name.fromString("Int"); @@ -50,13 +53,16 @@ public class Names { public static final Name Object = Name.fromString("Object"); public static final Name PartialFunction = Name.fromString("PartialFunction"); public static final Name Predef = Name.fromString("Predef"); + public static final Name ScalaRunTime = Name.fromString("ScalaRunTime"); public static final Name Seq = Name.fromString("Seq"); public static final Name Short = Name.fromString("Short"); public static final Name String = Name.fromString("String"); public static final Name Symbol = Name.fromString("Symbol"); public static final Name Throwable = Name.fromString("Throwable"); + public static final Name Try = Name.fromString("Try"); public static final Name Tuple = Name.fromString("Tuple"); public static final Name Unit = Name.fromString("Unit"); + public static final Name While = Name.fromString("While"); public static final Name apply = Name.fromString("apply"); public static final Name as = Name.fromString("as"); public static final Name box = Name.fromString("box"); |