daily pastebin goal
25%
SHARE
TWEET

Untitled

a guest Oct 12th, 2017 56 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  * PingPong.abs -- PingPong example
  3.  *
  4.  */
  5. module PingPong;
  6.  
  7. data PingMsg = Fine
  8.              | HelloPing
  9.              | ByePing
  10.              ;
  11.  
  12. data PongMsg = NoMsg
  13.          | Hello(Ping)
  14.          | HowAreYou
  15.          | ByePong
  16.          ;
  17.  
  18. interface Ping {
  19.     Unit ping(PingMsg m);
  20. }
  21.  
  22. interface Pong {
  23.     Unit hello(Ping ping);
  24.     Unit pong(PongMsg m);
  25. }
  26.  
  27. class PingImpl(Pong pong) implements Ping {
  28.     Unit run(){
  29.       pong!hello(this);
  30.     }
  31.  
  32.     Unit ping(PingMsg msg){
  33.       PongMsg reply = case msg {
  34.          HelloPing => HowAreYou;
  35.          Fine => ByePong;
  36.          // ByePing => NoMsg;
  37.       };
  38.      
  39.       if (reply != NoMsg) {
  40.          println("Pinging with: " + toString(reply));
  41.          Fut<Unit> fu = pong!pong(reply);
  42.         fu.get;
  43.       } else {
  44.          println("NoMsg");
  45.       }
  46.      }
  47. }
  48.  
  49. class PongImpl implements Pong {
  50.     Ping ping;
  51.  
  52.     Unit hello(Ping ping) {
  53.        this.ping = ping;
  54.        println("Hello");
  55.        ping!ping(HelloPing);
  56.     }
  57.  
  58.     Unit pong(PongMsg msg){
  59.       if (msg == HowAreYou) {
  60.          println("Fine");
  61.          ping!ping(Fine);
  62.       }
  63.        else {
  64.           println("Bye!");
  65.          ping!ping(ByePing);
  66.        }
  67.     }
  68.  
  69. }
  70.  
  71. {
  72.     Pong pong = new PongImpl();
  73.     new PingImpl(pong);
  74. }
  75.  
  76.  
  77. /*
  78. ================ Output if run in Erlang ================
  79. Start m_PingPong
  80. Hello
  81. Pinging with: HowAreYou
  82. Fine
  83. Pinging with: ByePong
  84. Bye!
  85. Uncaught PatternMatchFailException in method ping and no recovery block in class definition, killing object PingPong.PingImpl:<0.71.0>
  86. */
RAW Paste Data
Top