From 9ab44e5b8c934d5bed0efab7aebb10135d21ca30 Mon Sep 17 00:00:00 2001 From: Martin Odersky Date: Wed, 12 Mar 2003 13:39:16 +0000 Subject: *** empty log message *** --- sources/examples/auction.scala | 58 ++++++++++++++++++++++++++++++++++ sources/scala/concurrent/Actor.scala | 15 +++++++++ sources/scalac/ast/parser/Parser.java | 22 ++++++++++--- sources/scalac/ast/parser/Scanner.java | 4 +-- sources/scalac/ast/parser/Tokens.java | 14 ++++---- 5 files changed, 99 insertions(+), 14 deletions(-) create mode 100644 sources/examples/auction.scala create mode 100644 sources/scala/concurrent/Actor.scala (limited to 'sources') diff --git a/sources/examples/auction.scala b/sources/examples/auction.scala new file mode 100644 index 0000000000..9f96dc2984 --- /dev/null +++ b/sources/examples/auction.scala @@ -0,0 +1,58 @@ +package examples; + +import java.util.Date; +import scala.concurrent._; + +trait AuctionMessage; +case class + Offer(bid: Int, client: Actor), // make a bid + Inquire(client: Actor) extends AuctionMessage; // inquire status + +trait AuctionReply; +case class + Status(asked: Int, expiration: Date), // asked sum, expiration date + BestOffer(), // yours is the best offer + BeatenOffer(maxBid: Int), // offer beaten by maxBid + AuctionConcluded(seller: Actor, client: Actor), // auction concluded + AuctionFailed(), // failed with no bids + AuctionOver() extends AuctionReply; // bidding is closed + +class Auction(seller: Actor, minBid: Int, closing: Date) extends Actor() { + + val timeToShutdown = 36000000; // msec + val bidIncrement = 10; + override def run() { + var maxBid = minBid - bidIncrement; + var maxBidder: Actor = _; + var running = true; + while (running) { + receiveWithin ((closing.getTime() - new Date().getTime())) { + case Offer(bid, client) => + if (bid >= maxBid + bidIncrement) { + if (maxBid >= minBid) maxBidder send BeatenOffer(bid); + maxBid = bid; + maxBidder = client; + client send BestOffer(); + } else { + client send BeatenOffer(maxBid); + } + + case Inquire(client) => + client send Status(maxBid, closing); + + case TIMEOUT() => + if (maxBid >= minBid) { + val reply = AuctionConcluded(seller, maxBidder); + maxBidder send reply; + seller send reply; + } else { + seller send AuctionFailed(); + } + receiveWithin(timeToShutdown) { + case Offer(_, client) => client send AuctionOver() + case TIMEOUT() => running = false; + } + } + } + } +} diff --git a/sources/scala/concurrent/Actor.scala b/sources/scala/concurrent/Actor.scala new file mode 100644 index 0000000000..f6a1447f0e --- /dev/null +++ b/sources/scala/concurrent/Actor.scala @@ -0,0 +1,15 @@ +package scala.concurrent; + +abstract class Actor() extends Thread() { + + type Message = AnyRef; + + private val mb = new MailBox(); + + def send(msg: Message): Unit = + mb.send(msg); + def receive[a](f: PartialFunction[Message, a]): a = + mb.receive(f); + def receiveWithin[a](msec: Long)(f: PartialFunction[Message, a]): a = + mb.receiveWithin(msec)(f); +} \ No newline at end of file diff --git a/sources/scalac/ast/parser/Parser.java b/sources/scalac/ast/parser/Parser.java index 518ded67e6..6481ac730e 100644 --- a/sources/scalac/ast/parser/Parser.java +++ b/sources/scalac/ast/parser/Parser.java @@ -144,7 +144,7 @@ public class Parser implements Tokens { switch (s.token) { case CHARLIT: case INTLIT: case LONGLIT: case FLOATLIT: case DOUBLELIT: case STRINGLIT: - case SYMBOLLIT: case NULL: case IDENTIFIER: + case SYMBOLLIT: case TRUE: case FALSE: case NULL: case IDENTIFIER: case THIS: case SUPER: case IF: case FOR: case NEW: case USCORE: case LPAREN: case LBRACE: @@ -193,6 +193,10 @@ public class Parser implements Tokens { return make.Select(pos, make.Ident(pos, Names.scala), name); } + Tree scalaBooleanDot(int pos, Name name) { + return make.Select(pos, scalaDot(pos, Names.Boolean), name); + } + /** Create tree for for-comprehension or * where mapName and flatmapName are chosen * corresponding to whether this is a for-do or a for-yield. @@ -452,6 +456,12 @@ public class Parser implements Tokens { case STRINGLIT: t = make.Literal(s.pos, s.name.toString()); break; + case TRUE: + t = scalaBooleanDot(s.pos, Names.True); + break; + case FALSE: + t = scalaBooleanDot(s.pos, Names.False); + break; case NULL: t = make.Ident(s.pos, Names.null_); break; @@ -774,6 +784,8 @@ public class Parser implements Tokens { case DOUBLELIT: case STRINGLIT: case SYMBOLLIT: + case TRUE: + case FALSE: case NULL: t = literal(false); break; @@ -943,12 +955,10 @@ public class Parser implements Tokens { new Tree.CaseDef[]{ (CaseDef)make.CaseDef( rhs.pos, pat.duplicate(), Tree.Empty, - make.Select(rhs.pos, - scalaDot(rhs.pos, Names.Boolean), Names.True)), + scalaBooleanDot(rhs.pos, Names.True)), (CaseDef)make.CaseDef( rhs.pos, make.Ident(rhs.pos, Names.WILDCARD), Tree.Empty, - make.Select(rhs.pos, - scalaDot(rhs.pos, Names.Boolean), Names.False))})}); + scalaBooleanDot(rhs.pos, Names.False))})}); return make.PatDef(pos, 0, pat, rhs); } @@ -1012,6 +1022,8 @@ public class Parser implements Tokens { case DOUBLELIT: case STRINGLIT: case SYMBOLLIT: + case TRUE: + case FALSE: case NULL: return literal(true); case LPAREN: diff --git a/sources/scalac/ast/parser/Scanner.java b/sources/scalac/ast/parser/Scanner.java index a86014d650..82eef0d408 100644 --- a/sources/scalac/ast/parser/Scanner.java +++ b/sources/scalac/ast/parser/Scanner.java @@ -791,18 +791,18 @@ public class Scanner extends TokenData { enterKeyword("final", FINAL); enterKeyword("private", PRIVATE); enterKeyword("protected", PROTECTED); - enterKeyword("qualified", QUALIFIED); enterKeyword("override", OVERRIDE); enterKeyword("var", VAR); enterKeyword("def", DEF); enterKeyword("type", TYPE); enterKeyword("extends", EXTENDS); - enterKeyword("let", LET); enterKeyword("module", MODULE); enterKeyword("class",CLASS); enterKeyword("constr",CONSTR); enterKeyword("import", IMPORT); enterKeyword("package", PACKAGE); + enterKeyword("true", TRUE); + enterKeyword("false", FALSE); enterKeyword(".", DOT); enterKeyword("_", USCORE); enterKeyword(":", COLON); diff --git a/sources/scalac/ast/parser/Tokens.java b/sources/scalac/ast/parser/Tokens.java index 2fa82eb5f3..10503ddd48 100644 --- a/sources/scalac/ast/parser/Tokens.java +++ b/sources/scalac/ast/parser/Tokens.java @@ -42,13 +42,13 @@ public interface Tokens { FINAL = 32, PRIVATE = 33, PROTECTED = 34, - QUALIFIED = 35, - OVERRIDE = 36, - VAR = 37, - DEF = 38, - TYPE = 39, - EXTENDS = 40, - LET = 41, + OVERRIDE = 35, + VAR = 36, + DEF = 37, + TYPE = 38, + EXTENDS = 39, + TRUE = 40, + FALSE = 41, MODULE = 43, CLASS = 44, CONSTR = 45, -- cgit v1.2.3