exps/Spiral.scala
changeset 304 82a99eec5b73
equal deleted inserted replaced
303:877834bfe53b 304:82a99eec5b73
       
     1 import Element.elem
       
     2 import RexpRelated._
       
     3 import RexpRelated.Rexp._
       
     4 object Spiral{
       
     5 
       
     6   val space = elem(" ")
       
     7   val corner = elem("+")
       
     8 
       
     9   def spiral(nEdges: Int, direction: Int): Element = {
       
    10     if(nEdges == 1)
       
    11       elem("+")
       
    12     else {
       
    13       val sp = spiral(nEdges - 1, (direction + 3) % 4)
       
    14       def verticalBar = elem('|', 1, sp.height)
       
    15       def horizontalBar = elem('-', sp.width, 1)
       
    16       if(direction == 0)
       
    17         (corner beside horizontalBar) above sp//(sp beside space)
       
    18       else if (direction == 1)
       
    19         sp beside (corner above verticalBar)
       
    20       else if (direction == 2)
       
    21         (space beside sp) above (horizontalBar beside corner)
       
    22       else
       
    23         (verticalBar above corner) beside (space above sp)
       
    24     }
       
    25   }
       
    26   val alphabet = ("""abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.:"=()\;-+*!<>\/%{} """+"\n\t").toSet//Set('a','b','c')
       
    27   def regx_tree(r: ARexp): Element = {
       
    28     r match {
       
    29       case APRED(bs, f) => {
       
    30         val Some(d) = alphabet.find(f)
       
    31         d match {
       
    32           case '\n' => elem("\\n")
       
    33           case '\t' => elem("\\t")
       
    34           case ' ' => elem("space")
       
    35           case d => elem(d.toString)
       
    36         }   
       
    37       }
       
    38       case AONE(bs) => {
       
    39         elem("ONE")
       
    40       }
       
    41       case AZERO => {
       
    42         elem("ZERO")
       
    43       }
       
    44       case ASEQ(bs, r1, r2) => {
       
    45         binary_print("SEQ", r1, r2)
       
    46       }
       
    47       case AALTS(bs, rs) => {
       
    48         //elem("Awaiting completion")
       
    49         list_print("ALT", rs)
       
    50       }
       
    51       case ASTAR(bs, r) => {
       
    52         list_print("STA", List(r))
       
    53       }
       
    54     }
       
    55   }
       
    56   val port = elem(" └-")
       
    57   def list_print(name: String, rs: List[ARexp]): Element = {
       
    58     rs match {
       
    59       case r::Nil => {
       
    60         val pref = regx_tree(r)
       
    61         val head = elem(name)
       
    62         (head left_align (port up_align pref) ) 
       
    63       }
       
    64       case r2::r1::Nil => {
       
    65         binary_print(name, r2, r1)
       
    66       }
       
    67       case r::rs1 => {
       
    68         val pref = regx_tree(r)
       
    69         val head = elem(name)
       
    70         if (pref.height > 1){
       
    71           val link = elem('|', 1, pref.height - 1)
       
    72           (head left_align ((port above link) beside pref)) left_align tail_print(rs1)    
       
    73         }
       
    74         else{
       
    75           (head left_align (port beside pref) ) left_align tail_print(rs1)
       
    76         }
       
    77       }
       
    78     }
       
    79   }
       
    80   def tail_print(rs: List[ARexp]): Element = {
       
    81 
       
    82     rs match {
       
    83       case r2::r1::Nil => {
       
    84         val pref = regx_tree(r2)
       
    85         val suff = regx_tree(r1)
       
    86         if (pref.height > 1){
       
    87           val link = elem('|', 1, pref.height - 1)
       
    88           ((port above link) beside pref) left_align (port up_align suff)
       
    89         }
       
    90         else{
       
    91           (port beside pref) left_align (port up_align suff)
       
    92         } 
       
    93       }
       
    94       case r2::rs1 => {
       
    95         val pref = regx_tree(r2)
       
    96         
       
    97         if (pref.height > 1){
       
    98           val link = elem('|', 1, pref.height - 1)
       
    99           ((port above link) beside pref) left_align tail_print(rs1)//(port up_align tail_print(rs1) )
       
   100         }
       
   101         else{
       
   102           (port beside pref) left_align tail_print(rs1)//(port up_align tail_print(rs1))
       
   103         } 
       
   104         //pref left_align tail_print(rs1)
       
   105       }
       
   106     }
       
   107   }
       
   108 
       
   109   def binary_print(name: String, r1: ARexp, r2: ARexp): Element = {
       
   110     val pref = regx_tree(r1)
       
   111     val suff = regx_tree(r2)
       
   112     val head = elem(name)
       
   113     if (pref.height > 1){
       
   114       val link = elem('|', 1, pref.height - 1)
       
   115       (head left_align ((port above link) beside pref) ) left_align (port up_align suff)
       
   116     }
       
   117     else{
       
   118       (head left_align (port beside pref) ) left_align (port up_align suff)
       
   119     }
       
   120   }
       
   121   def illustration(r: Rexp, s: String){
       
   122     var i_like_imperative_style = internalise(r)
       
   123     val all_chars = s.toList
       
   124     for (i <- 0 to s.length - 1){
       
   125       val der_res =  bder(all_chars(i), i_like_imperative_style)
       
   126       val simp_res = bsimp(der_res)
       
   127       println("The three regxes are the original regex, the regex after derivative w.r.t " + all_chars(i) + " and the simplification of the derivative.")
       
   128       //println(regx_tree(i_like_imperative_style) up_align regx_tree(der_res) up_align regx_tree(simp_res))
       
   129       println(asize(i_like_imperative_style), asize(der_res), asize(simp_res))
       
   130       i_like_imperative_style = simp_res
       
   131     }
       
   132   }
       
   133   val ran = scala.util.Random
       
   134   var alphabet_size = 3
       
   135   def balanced_seq_star_gen(depth: Int, star: Boolean): Rexp = {
       
   136     if(depth == 1){
       
   137       ((ran.nextInt(6) + 97).toChar).toString
       
   138     }
       
   139     else if(star){
       
   140       STAR(balanced_seq_star_gen(depth - 1, false))
       
   141     }
       
   142     else{
       
   143       SEQ(balanced_seq_star_gen(depth - 1, true), balanced_seq_star_gen(depth - 1, true))
       
   144     }
       
   145   }
       
   146   def random_struct_gen(depth:Int): Rexp = {
       
   147     val dice = ran.nextInt(3)
       
   148     (dice, depth) match {
       
   149       case (_, 0) => ((ran.nextInt(alphabet_size) + 97).toChar).toString
       
   150       case (0, i) => STAR(random_struct_gen(i - 1))
       
   151       case (1, i) => SEQ(random_struct_gen(i - 1), random_struct_gen(i - 1))
       
   152       case (2, i) => ALTS( List(random_struct_gen(i - 1), random_struct_gen(i - 1)) )
       
   153     }
       
   154   }
       
   155   def rd_string_gen(alp_size: Int, len: Int): String = {
       
   156     ((ran.nextInt(alp_size) + 97).toChar).toString + rd_string_gen(alp_size, len - 1)
       
   157   }
       
   158   //def stay_same_hpth(r: Rexp, )
       
   159   def main(args: Array[String]) {
       
   160     val depth = args(0).toInt
       
   161     alphabet_size = args(1).toInt
       
   162     //illustration(random_struct_gen(depth), rd_string_gen(alphabet_size, 20))//"abcabadaaadcabdbabcdaadbabbcbbdabdabbcbdbabdbcdb")
       
   163     for( i <- 50 to 400 by 20){
       
   164       println(i+" iterations of prog2:")
       
   165       blexing_simp(AWHILE_REGS, prog2 * i)
       
   166     }
       
   167       
       
   168   } 
       
   169 }