--** Simple streams


#INCLUDE "course.module"


--* This outputs even numbers from 0 upwards
--
-- @param out The output stream
--
PROC S0 (CHAN INT out!)
  SKIP  -- replace with your own code
:


--* This outputs odd numbers from 1 upwards
--
-- @param out The output stream
--
PROC S1 (CHAN INT out!)
  SKIP  -- replace with your own code
:


--* This pauses execution of the invoking process by delay microseconds.
--
-- @param delay The length of the pause (in microseconds)
--
PROC pause (VAL INT delay)
  TIMER tim:
  INT t:
  SEQ
    tim ? t
    tim ? AFTER t PLUS delay
:


--* This inputs numbers and tabulates them into lines of text output.
--  It pauses for 'delay' microseconds after each line.
--
-- @param delay The length of the pause (in microseconds) between lines
-- @param in Numbers coming in
-- @param out Characters going out
--
PROC print.stream (VAL INT delay, CHAN INT in?, CHAN BYTE out!)
  WHILE TRUE
    INT n:
    SEQ
      in ? n
      out.int (n, 10, out!)             -- out.int is from "course.module"
      out.string ("*c*n", 0, out!)      -- out.string is from "course.module"
      pause (delay)
:


--* This multiplexes numbers from its two input channels to its output channel.
--  It alternates taking one number from one channel and forwarding it - then
--  from the other channel and forwarding it.  It continues doing this for ever.
--  It assumes numbers will always be offered to both input channels.
--
-- @param in0 The channel to be taken first
-- @param in1 The channel to be taken second
-- @param out The output channel (a merger of the two input channels)
--
PROC alternate (CHAN INT in0?, in1?, out!)
  SKIP  -- replace with your own code
:


--* The main process.  This has been extended to let some parameters
--  be entered interactively.
--
-- @param keyboard The standard input channel (stdin, in Unix-speak)
-- @param screen The standard output channel (stdout, in Unix-speak)
-- @param error The standard error channel (stderr, in Unix-speak)
--
PROC q1 (CHAN BYTE keyboard?, screen!, error!)
  SKIP  -- replace with your own code
:
