87 |
87 |
88 // extracts a string from value |
88 // extracts a string from value |
89 def flatten(v: Val) : String = v match { |
89 def flatten(v: Val) : String = v match { |
90 case Void => "" |
90 case Void => "" |
91 case Chr(c) => c.toString |
91 case Chr(c) => c.toString |
92 case Left(n, v) => flatten(v) |
92 case Left(v) => flatten(v) |
93 case Right(n, v) => flatten(v) |
93 case Right(v) => flatten(v) |
94 case Sequ(v1, v2) => flatten(v1) + flatten(v2) |
94 case Sequ(v1, v2) => flatten(v1) + flatten(v2) |
95 case Stars(vs) => vs.map(flatten).mkString |
95 case Stars(vs) => vs.map(flatten).mkString |
96 case Rec(_, v) => flatten(v) |
96 case Rec(_, v) => flatten(v) |
97 } |
97 } |
98 |
98 |
99 // extracts an environment from a value |
99 // extracts an environment from a value |
100 def env(v: Val) : List[(String, String)] = v match { |
100 def env(v: Val) : List[(String, String)] = v match { |
101 case Void => Nil |
101 case Void => Nil |
102 case Chr(c) => Nil |
102 case Chr(c) => Nil |
103 case Left(n, v) => env(v) |
103 case Left(v) => env(v) |
104 case Right(n, v) => env(v) |
104 case Right(v) => env(v) |
105 case Sequ(v1, v2) => env(v1) ::: env(v2) |
105 case Sequ(v1, v2) => env(v1) ::: env(v2) |
106 case Stars(vs) => vs.flatMap(env) |
106 case Stars(vs) => vs.flatMap(env) |
107 case Rec(x, v) => (x, flatten(v))::env(v) |
107 case Rec(x, v) => (x, flatten(v))::env(v) |
108 } |
108 } |
109 |
109 |
110 def left_inc(v: Val) = v match { |
|
111 case Left(v, n) => Left(v, n + 1) |
|
112 case v => Left(v, 1) |
|
113 } |
|
114 |
|
115 def right_inc(v: Val) = v match { |
|
116 case Right(v, n) => Right(v, n + 1) |
|
117 case v => Right(v, 1) |
|
118 } |
|
119 |
110 |
120 def mkeps(r: Rexp) : Val = r match { |
111 def mkeps(r: Rexp) : Val = r match { |
121 case EMPTY => Void |
112 case EMPTY => Void |
122 case ALT(r1, r2) => |
113 case ALT(r1, r2) => |
123 if (nullable(r1)) left_inc(mkeps(r1)) else right_inc(mkeps(r2)) |
114 if (nullable(r1)) Left(mkeps(r1)) else Right(mkeps(r2)) |
124 case SEQ(r1, r2) => Sequ(mkeps(r1), mkeps(r2)) |
115 case SEQ(r1, r2) => Sequ(mkeps(r1), mkeps(r2)) |
125 case STAR(r) => Stars(Nil) |
116 case STAR(r) => Stars(Nil) |
126 case RECD(x, r) => Rec(x, mkeps(r)) |
117 case RECD(x, r) => Rec(x, mkeps(r)) |
127 } |
118 } |
128 |
119 |
129 def inj(r: Rexp, c: Char, v: Val) : Val = (r, v) match { |
120 def inj(r: Rexp, c: Char, v: Val) : Val = (r, v) match { |
130 case (STAR(r), Sequ(v1, Stars(vs))) => Stars(inj(r, c, v1)::vs) |
121 case (STAR(r), Sequ(v1, Stars(vs))) => Stars(inj(r, c, v1)::vs) |
131 case (SEQ(r1, r2), Sequ(v1, v2)) => Sequ(inj(r1, c, v1), v2) |
122 case (SEQ(r1, r2), Sequ(v1, v2)) => Sequ(inj(r1, c, v1), v2) |
132 case (SEQ(r1, r2), Left(Sequ(v1, v2))) => Sequ(inj(r1, c, v1), v2) |
123 case (SEQ(r1, r2), Left(Sequ(v1, v2))) => Sequ(inj(r1, c, v1), v2) |
133 case (SEQ(r1, r2), Right(v2)) => Sequ(mkeps(r1), inj(r2, c, v2)) |
124 case (SEQ(r1, r2), Right(v2)) => Sequ(mkeps(r1), inj(r2, c, v2)) |
134 case (ALT(r1, r2), Left(v1), 1) => Left(inj(r1, c, v1), 1) |
125 case (ALT(r1, r2), Left(v1)) => Left(inj(r1, c, v1)) |
135 case (ALT(r1, r2), Left(v1), n) => inc_left(inj(r1, c, Left(v1, n - 1))) |
|
136 case (ALT(r1, r2), Right(v2)) => Right(inj(r2, c, v2)) |
126 case (ALT(r1, r2), Right(v2)) => Right(inj(r2, c, v2)) |
137 case (CHAR(d), Void) => Chr(d) |
127 case (CHAR(d), Void) => Chr(d) |
138 case (RECD(x, r1), _) => Rec(x, inj(r1, c, v)) |
128 case (RECD(x, r1), _) => Rec(x, inj(r1, c, v)) |
139 } |
129 } |
140 |
130 |