Advertisement
Guest User

Untitled

a guest
Dec 11th, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.69 KB | None | 0 0
  1. #use "reader.ml";;
  2.  
  3. type constant =
  4. | Sexpr of sexpr
  5. | Void
  6.  
  7. type expr =
  8. | Const of constant
  9. | Var of string
  10. | If of expr * expr * expr
  11. | Seq of expr list
  12. | Set of expr * expr
  13. | Def of expr * expr
  14. | Or of expr list
  15. | LambdaSimple of string list * expr
  16. | LambdaOpt of string list * string * expr
  17. | Applic of expr * (expr list);;
  18.  
  19. let rec expr_eq e1 e2 =
  20. match e1, e2 with
  21. | Const Void, Const Void -> true
  22. | Const(Sexpr s1), Const(Sexpr s2) -> sexpr_eq s1 s2
  23. | Var(v1), Var(v2) -> String.equal v1 v2
  24. | If(t1, th1, el1), If(t2, th2, el2) -> (expr_eq t1 t2) &&
  25. (expr_eq th1 th2) &&
  26. (expr_eq el1 el2)
  27. | (Seq(l1), Seq(l2)
  28. | Or(l1), Or(l2)) -> List.for_all2 expr_eq l1 l2
  29. | (Set(var1, val1), Set(var2, val2)
  30. | Def(var1, val1), Def(var2, val2)) -> (expr_eq var1 var2) &&
  31. (expr_eq val1 val2)
  32. | LambdaSimple(vars1, body1), LambdaSimple(vars2, body2) ->
  33. (List.for_all2 String.equal vars1 vars2) &&
  34. (expr_eq body1 body2)
  35. | LambdaOpt(vars1, var1, body1), LambdaOpt(vars2, var2, body2) ->
  36. (String.equal var1 var2) &&
  37. (List.for_all2 String.equal vars1 vars2) &&
  38. (expr_eq body1 body2)
  39. | Applic(e1, args1), Applic(e2, args2) ->
  40. (expr_eq e1 e2) &&
  41. (List.for_all2 expr_eq args1 args2)
  42. | _ -> false;;
  43.  
  44.  
  45. exception X_syntax_error;;
  46.  
  47. module type TAG_PARSER = sig
  48. val tag_parse_expression : sexpr -> expr
  49. val tag_parse_expressions : sexpr list -> expr list
  50. end;; (* signature TAG_PARSER *)
  51.  
  52. module Tag_Parser : TAG_PARSER = struct
  53.  
  54. let reserved_word_list =
  55. ["and"; "begin"; "cond"; "define"; "else";
  56. "if"; "lambda"; "let"; "let*"; "letrec"; "or";
  57. "quasiquote"; "quote"; "set!"; "unquote";
  58. "unquote-splicing"];;
  59.  
  60. (* work on the tag parser starts here *)
  61. let rec pairsToList=(function
  62. | Pair(Symbol(x),y) -> x::(pairsToList y)
  63. | Nil -> [];
  64. | _ -> raise X_no_match
  65. );;
  66.  
  67. let rec pairsToListDotted=(function
  68. | Pair(Symbol(x),y) -> x::(pairsToListDotted y)
  69. | Symbol(x) -> [];
  70. | _ -> raise X_no_match
  71. );;
  72. let rec lastOneInListDotted=(function
  73. | Pair(Symbol(x),y) -> lastOneInListDotted y
  74. | Symbol(x) -> x;
  75. | _ -> raise X_no_match
  76. );;
  77. let rec hasLastOneInListDotted=(function
  78. | Pair(Symbol(x),y) -> hasLastOneInListDotted y
  79. | Symbol(x) -> true
  80. | _ -> false
  81. );;
  82.  
  83. (* let lastElementInList =function *)
  84.  
  85. let rec tag_parse s= (function
  86. | Number(x) -> Const(Sexpr(Number(x)))
  87. | Bool(x) -> Const(Sexpr(Bool(x)))
  88. | Char(x) -> Const(Sexpr(Char(x)))
  89. | String(x) -> Const(Sexpr(String(x)))
  90. | TagRef(x) -> Const(Sexpr(TagRef(x)))
  91. | Pair(Symbol("Quoted"),Pair(x,_)) -> Const(Sexpr(x))
  92. | Pair(Symbol("if"), Pair(test, Pair(dit, Pair(dif, Nil))))-> If(tag_parse test, tag_parse dit, tag_parse dif)
  93. | Pair(Symbol("if"), Pair(test, Pair(dit, Nil)))-> If(tag_parse test, tag_parse dit, Const(Void))
  94. | Pair(Symbol("quote"), Pair(x, Nil)) -> Const(Sexpr(x))
  95. | Pair(Symbol("lambda"), Pair(Symbol(arg), Pair(body, Nil)))-> LambdaOpt([], arg, tag_parse body)
  96. | Pair(Symbol("lambda"), Pair(args, Pair(body, Nil)))-> if(hasLastOneInListDotted args)
  97. then LambdaOpt(pairsToListDotted args, lastOneInListDotted args, tag_parse body)
  98. else LambdaSimple(pairsToList args, tag_parse body)
  99. | Pair(Symbol("define"), Pair(Symbol(name), Pair(body, Nil)))-> Def (Var(name),tag_parse body)
  100. | Pair(Symbol("set!"), Pair(Symbol(name), Pair(body, Nil)))-> Set (Var(name),tag_parse body)
  101. | Pair(Symbol("begin"), Nil)-> Const(Void)
  102. | Pair(Symbol("begin"), Pair(Symbol(name),Nil))-> Var(name)
  103. | Pair(Symbol("begin"), args)-> Seq(taggedList args)
  104. | Pair(Symbol("or"), Nil)-> Const(Sexpr(Bool(false)))
  105. | Pair(Symbol("or"), Pair(arg,Nil))-> tag_parse arg
  106. | Pair(Symbol("or"), args)-> Or(taggedList args)
  107. | Pair(Symbol("cond"), Pair(rib1, ribs))-> condMaker (rib1,ribs)
  108. | Pair(Symbol("let"), Pair(args, body))-> if (args=Nil) then Applic(LambdaSimple([], tag_parse body),[])
  109. else Applic(LambdaSimple(leftSideLet args, tag_parse body),rightSideLet args)
  110. | Pair(Symbol("let*"), Pair(Nil, body))-> Applic(LambdaSimple([], tag_parse body),[])
  111. | Pair(Symbol("let*"), Pair(Pair(Pair(Symbol arg,Pair(argLeft,Nil)),Nil), body))-> Applic(LambdaSimple([arg], tag_parse body),[tag_parse argLeft])
  112. | Pair(Symbol("let*"), Pair(Pair(arg, args), body))-> tag_parse (Pair(Symbol("let"), Pair(Pair(arg, Nil), Pair(Symbol("let*"), Pair(args, body)))))
  113. | Pair(Symbol("and"), Nil)-> Const(Sexpr(Bool(true)))
  114. | Pair(Symbol("and"), Pair(arg,Nil))-> tag_parse arg
  115. | Pair(Symbol("and"), Pair(arg,args))-> If(tag_parse arg, tag_parse(Pair (Symbol("and"),args)), Const(Sexpr(Bool(false))))
  116. | Pair(Symbol("letrec"), Pair(args, body))-> tag_parse (Pair(Symbol("let"),Pair(letrecLeft (leftSideLet args), setMaker(leftSideLet args,rightSideLetrec args,body))))
  117.  
  118.  
  119. | Pair(x,y) -> Applic((tag_parse x),taggedList y)
  120. | Symbol(x) -> Var(x)
  121. | _ -> raise X_no_match
  122. ) s
  123. and taggedList s=(function
  124. | Pair(x,y) -> (tag_parse x)::(taggedList y)
  125. | Nil -> [];
  126. | _ -> raise X_no_match
  127. ) s
  128. and condMaker s= (function
  129. (* | (Pair(value,Pair(Symbol("=>"),Pair(body,Nil))),rest) ->
  130. tag_parse(Pair(Pair(Symbol("let"), Pair(
  131. Pair(Pair (Symbol "value", value),
  132. Pair(Pair (Symbol "f",Pair (Pair (Symbol "lambda", Pair (Nil, body)), Nil)),
  133. Pair(Pair (Symbol "rest",Pair (Pair (Symbol "lambda", Pair (Nil, rest)), Nil)),Nil))),
  134. Pair (Symbol "if",Pair (Symbol "value",Pair (Pair (Pair (Symbol "f", Nil), Pair (Symbol "value", Nil)),Pair (Pair (Symbol "rest", Nil), Nil)))))),Nil)) *)
  135. | (Pair(Symbol "else",body),rest)-> tag_parse (Pair(Symbol "begin",body))
  136. | (Pair(test,body),rest) -> If(tag_parse test, tag_parse (Pair(Symbol "begin",body)), restCondMaker rest)
  137. | _ -> raise X_no_match
  138. ) s
  139. and restCondMaker s= (function
  140. | Pair(rib1,rest) -> condMaker(rib1,rest)
  141. | Nil -> Const(Void)
  142. | _ -> raise X_no_match
  143. ) s
  144. and leftSideLet s=(function
  145. | Pair(Pair(Symbol x,y),Nil) -> [x]
  146. | Pair(Pair(Symbol x,y),z) -> x:: (leftSideLet z)
  147. | _ -> raise X_no_match
  148. ) s
  149. and rightSideLet s=(function
  150. | Pair(Pair(x,Pair(y,Nil)),Nil) -> [tag_parse y]
  151. | Pair(Pair(x,Pair(y,Nil)),z) -> (tag_parse y):: (rightSideLet z)
  152. | _ -> raise X_no_match
  153. )s
  154. and rightSideLetrec s=(function
  155. | Pair(Pair(x,Pair(y,Nil)),Nil) -> [y]
  156. | Pair(Pair(x,Pair(y,Nil)),z) -> y:: (rightSideLetrec z)
  157. | _ -> raise X_no_match
  158. )s
  159. and letrecLeft s =(function
  160. | [] -> Nil
  161. | a :: b -> Pair (Pair(Symbol a, Pair(Symbol "'whatever",Nil)), letrecLeft b)
  162. )s
  163. and setMaker s =(function
  164. | ([],[],body) -> Pair(Pair(Symbol("let"), Pair(Nil, body)),Nil)
  165. | (a :: b,c::d,body) -> Pair (Pair (Symbol "set!", Pair (Symbol a, Pair (c, Nil))), setMaker (b,d,body))
  166. | _ -> Nil
  167. )s
  168. ;;
  169.  
  170. let tag_parse_expression sexpr = tag_parse sexpr;;
  171.  
  172. let tag_parse_expressions sexpr = raise X_not_yet_implemented;;
  173.  
  174.  
  175. end;; (* struct Tag_Parser *)
  176.  
  177.  
  178. (* Pair (Symbol "cond", Pair(Pair (Symbol "t",Pair (Number (Int 1), Pair (Number (Int 2), Pair (Number (Int 3), Nil)))),Nil)) *)
  179.  
  180. (* Pair (Symbol "cond",Pair(Pair (Symbol "t",Pair(Pair (Number (Int 1), Pair (Number (Int 2), Pair (Number (Int 3), Nil))),Nil)),Nil)) *)
  181. (* Pair (Symbol "cond", Pair(Pair (Symbol "t",Pair (Pair (Number (Int 1), Nil), Pair (Pair (Number (Int 2), Nil), Pair (Pair (Number (Int 3), Nil), Nil)))),Nil)) *)
  182.  
  183. (* Pair (Symbol "cond", Pair (Pair (Bool true, Pair (Number (Int 1), Pair (Number (Int 2), Pair (Number (Int 3), Nil)))),Nil)) *)
  184.  
  185. let rec try3 s =(function
  186. | [] -> Nil
  187. | a :: b -> Pair (Pair(a, Pair(Symbol "'whatever",Nil)), try3 b)
  188. )s
  189. ;;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement