Fahad/Eclipse/ScalaProjects/bin/POSIX/PosixAlgo.sc
author Chengsong
Wed, 23 Aug 2023 03:02:31 +0100
changeset 668 3831621d7b14
parent 40 e5afb97b54f6
permissions -rw-r--r--
added technical Overview section, almost done introduction
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
40
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
     1
package POSIX
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
     2
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
     3
object PosixAlgo {
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
     4
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
     5
  abstract class Rexp
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
     6
  case object NULL extends Rexp
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
     7
  case object EMPTY extends Rexp
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
     8
  case class CHAR(c: Char) extends Rexp
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
     9
  case class ALT(r1: Rexp, r2: Rexp) extends Rexp
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    10
  case class SEQ(r1: Rexp, r2: Rexp) extends Rexp
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    11
  case class STAR(r: Rexp) extends Rexp
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    12
  case class RECD(x: String, r: Rexp) extends Rexp
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    13
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    14
	abstract class Val
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    15
  case object Void extends Val
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    16
  case class Chr(c: Char) extends Val
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    17
  case class Sequ(v1: Val, v2: Val) extends Val
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    18
  case class Left(v: Val) extends Val
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    19
  case class Right(v: Val) extends Val
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    20
  case class Stars(vs: List[Val]) extends Val
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    21
  case class Rec(x: String, v: Val) extends Val
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    22
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    23
	def nullable(r: Rexp): Boolean = r match {
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    24
    case NULL => false
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    25
    case EMPTY => true
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    26
    case CHAR(_) => false
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    27
    case ALT(r1, r2) => nullable(r1) || nullable(r2)
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    28
    case SEQ(r1, r2) => nullable(r1) && nullable(r2)
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    29
    case STAR(_) => true
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    30
    case RECD(_, r1) => nullable(r1)
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    31
  }                                               //> nullable: (r: POSIX.PosixAlgo.Rexp)Boolean
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    32
  
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    33
  def der(c: Char, r: Rexp): Rexp = r match {
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    34
    case NULL => NULL
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    35
    case EMPTY => NULL
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    36
    case CHAR(d) => if (c == d) EMPTY else NULL
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    37
    case ALT(r1, r2) => ALT(der(c, r1), der(c, r2))
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    38
    case SEQ(r1, r2) =>
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    39
      if (nullable(r1)) ALT(SEQ(der(c, r1), r2), der(c, r2))
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    40
      else SEQ(der(c, r1), r2)
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    41
    case STAR(r) => SEQ(der(c, r), STAR(r))
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    42
    case RECD(_, r1) => der(c, r1)
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    43
  }                                               //> der: (c: Char, r: POSIX.PosixAlgo.Rexp)POSIX.PosixAlgo.Rexp
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    44
}