Advertisement
jchero

simplify final

Apr 24th, 2019
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.27 KB | None | 0 0
  1. package simplify
  2. import "fmt"
  3. import "hw1/expr"
  4.  
  5. // Depth should return the simplified expresion
  6. var domaths bool
  7. func init() {
  8. domaths = true
  9. }
  10.  
  11. func Simplify(e expr.Expr, env expr.Env) expr.Expr {
  12. //TODO implement the simplify
  13.  
  14. // Case 1: Any Var should be replaced with its value
  15. v, ok := e.(expr.Var)
  16. if ok == true {
  17. v = v
  18. if e.Eval(env) == 0 {
  19. return e
  20. }
  21. ans := fmt.Sprintf("%f", e.Eval(env))
  22. tp, ok := expr.Parse(ans)
  23. if ok == nil {
  24. return tp
  25. }
  26. }
  27.  
  28. // Case 2: Any Unary with Literal should be replaced with Literal
  29. u, ok := e.(expr.Unary)
  30. if ok == true {
  31. temp := Simplify(u.X, env)
  32. if temp == u.X {
  33. return e
  34. }
  35. l, ok3 := temp.(expr.Literal)
  36. if ok3 == true {
  37. l = l
  38. if u.Op == '-' {
  39. l = -1 * l
  40. }
  41. return l
  42. }
  43. }
  44.  
  45.  
  46. b, ok := e.(expr.Binary)
  47. if ok == true {
  48.  
  49. var ans expr.Literal
  50. var ans2 expr.Literal
  51.  
  52. // Case 4: Any Binary with addition of 0 should reduce
  53. l0, ok := b.X.(expr.Literal)
  54. if ok == true {
  55. l0 = l0
  56. if l0 == 0 && b.Op == '+' {
  57. return b.Y
  58. }
  59. }
  60. l2, ok2 := b.Y.(expr.Literal)
  61. if ok2 == true {
  62. l2 = l2
  63. if l2 == 0 && b.Op == '+' {
  64. return b.X
  65. }
  66. }
  67.  
  68. // Case 5: Any binary operator performing multiplications with 0/1 should be reduced
  69. ll, lok := b.X.(expr.Literal)
  70. ll = ll
  71. if lok == true {
  72. if ll == 1 && b.Op == '*' {
  73. return Simplify(b.Y, env)
  74. } else if ll == 0 && b.Op == '*' {
  75. ans := fmt.Sprintf("%f", ll.Eval(env))
  76. tp, ok := expr.Parse(ans)
  77. if ok == nil {
  78. return tp
  79. }
  80. }
  81. }
  82. ll2, lok2 := b.Y.(expr.Literal)
  83. ll2 = ll2
  84. if lok2 == true {
  85. if ll2 == 1 && b.Op == '*' {
  86. return Simplify(b.X, env)
  87. } else if ll2 == 0 && b.Op == '*' {
  88. ans := fmt.Sprintf("%f", ll2.Eval(env))
  89. tp, ok := expr.Parse(ans)
  90. if ok == nil {
  91. return tp
  92. }
  93. }
  94. }
  95.  
  96. // Case 3: Any Binary with both Literals return Literals
  97. l, ok2 := b.X.(expr.Literal)
  98. // Own Case: Binary with Var
  99. v, okk2 := b.X.(expr.Var)
  100. // Own case: One child is Binary, one is Var
  101. bl, bok := b.X.(expr.Binary)
  102. // Own case: One child is Unary, one is Var
  103. u, uok := b.X.(expr.Unary)
  104.  
  105. v = v
  106. bl = bl
  107. u = u
  108.  
  109. if ok2 == true || okk2 == true || bok == true || uok == true {
  110.  
  111.  
  112. if ok2 == true { // Literal
  113. ans = l
  114. } else if okk2 == true { // Var
  115. temp := Simplify(b.X, env)
  116. if b.X != temp {
  117. tpl, okkk := temp.(expr.Literal)
  118. okkk = okkk
  119. ans = tpl
  120. } else {
  121. domaths = false
  122. }
  123. } else if bok == true { // Binary
  124. domaths = true
  125. temp := Simplify(b.X, env)
  126. if b.X != temp {
  127. tp2, okkk := temp.(expr.Literal)
  128. okkk = okkk
  129. ans = tp2
  130. }
  131. } else if uok == true {
  132. domaths = true
  133. temp := Simplify(b.X, env)
  134. if b.X != temp {
  135. tp2, okkk := temp.(expr.Literal)
  136. okkk = okkk
  137. ans = tp2
  138. }
  139.  
  140. }
  141.  
  142.  
  143. l2, ok3 := b.Y.(expr.Literal)
  144. v2, okk3 := b.Y.(expr.Var)
  145. bl2, bok2 := b.Y.(expr.Binary)
  146. u2, uok2 := b.Y.(expr.Unary)
  147.  
  148. v2 = v2
  149. bl2 = bl2
  150. u2 = u2
  151.  
  152. if ok3 == true || okk3 == true || bok2 == true || uok2 == true {
  153.  
  154. if ok3 == true { // Literal
  155. ans2 = l2
  156. } else if okk3 == true { // Var
  157. temp := Simplify(b.Y, env)
  158. if b.Y != temp {
  159. tpl2, okkk2 := temp.(expr.Literal)
  160. okkk2 = okkk2
  161. ans2 = tpl2
  162. if domaths == false {
  163. b.Y = ans2
  164. return b
  165. }
  166. } else {
  167. domaths = false
  168. b.X = ans
  169. return b
  170. }
  171. } else if bok2 == true { // Binary
  172. domaths = true
  173. temp := Simplify(bl2, env)
  174. if b.Y != temp {
  175. tp2, okkk := temp.(expr.Literal)
  176. okkk = okkk
  177. ans2 = tp2
  178. }
  179. } else if uok2 == true {
  180. domaths = true
  181. temp := Simplify(b.Y, env)
  182. if b.Y != temp {
  183. tp2, okkk := temp.(expr.Literal)
  184. okkk = okkk
  185. ans2 = tp2
  186. }
  187. }
  188.  
  189. // Calculations
  190. if domaths == true {
  191. if b.Op == '+' {
  192. ans += ans2
  193. } else if b.Op == '-' {
  194. ans -= ans2
  195. } else if b.Op == '*' {
  196. ans *= ans2
  197. } else if b.Op == '/' {
  198. ans /= ans2
  199. }
  200. } else {
  201. return e
  202. }
  203. return ans
  204. }
  205. }
  206.  
  207.  
  208.  
  209. }
  210.  
  211.  
  212.  
  213. panic("TODO: implement this!")
  214. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement