Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.18 KB | None | 0 0
  1. open Tfbsrxast;;
  2. open Tfbsrxpp;;
  3.  
  4. exception TypeError
  5.  
  6. (*
  7. * If you would like typechecking to be enabled by your interpreter by default,
  8. * then change the following value to true. Whether or not typechecking is
  9. * enabled by default, you can explicitly enable it or disable it using
  10. * command-line arguments.
  11. *)
  12. let typecheck_default_enabled = true;;
  13.  
  14. (*
  15. * Replace this with your typechecker code. Your code should not throw the
  16. * following exception; if you need to raise an exception, create your own
  17. * exception type here.
  18. *)
  19.  
  20. let rec typecheck_aux gamma e = match e with
  21. | Var(ident) -> lookup gamma ident
  22. | Function(ident, fbtype, expr) ->
  23. let t = typecheck_aux ((ident, fbtype)::gamma) expr in TArrow(fbtype, t)
  24. | Letrec(ident, ident2, fbtype, expr1, fbtype2, expr2) -> TInt
  25. | Appl(expr1, expr2) -> let TArrow(t1, t2) = (typecheck_aux gamma expr1) in
  26. if typecheck_aux gamma expr2 = t1 then t2 else raise TypeError
  27. | Plus(expr1, expr2) ->
  28. if (typecheck_aux gamma expr1 = TInt && typecheck_aux gamma expr2 = TInt)
  29. then TInt else raise TypeError
  30. | Minus(expr1, expr2) ->
  31. if (typecheck_aux gamma expr1 = TInt && typecheck_aux gamma expr2 = TInt)
  32. then TInt else raise TypeError
  33. | Equal(expr1, expr2) ->
  34. if (typecheck_aux gamma expr1 = TInt && typecheck_aux gamma expr2 = TInt)
  35. then TBool else raise TypeError
  36. | And(expr1, expr2) ->
  37. if (typecheck_aux gamma expr1 = TBool && typecheck_aux gamma expr2 = TBool)
  38. then TBool else raise TypeError
  39. | Or(expr1, expr2) ->
  40. if (typecheck_aux gamma expr1 = TBool && typecheck_aux gamma expr2 = TBool)
  41. then TBool else raise TypeError
  42. | Not(expr1) -> if (typecheck_aux gamma expr1 = TBool)
  43. then TBool else raise TypeError
  44. | If(expr1, expr2, expr3) ->
  45. if (typecheck_aux gamma expr1) = TBool
  46. then (let (t1, t2) = ((typecheck_aux gamma expr2), (typecheck_aux gamma expr3))
  47. in (if t1 = t2 then t1 else raise TypeError))
  48. else raise TypeError
  49. | Int(int) -> TInt
  50. | Bool(bool) -> TBool
  51. | Ref(expr) -> TRef(typecheck_aux gamma expr)
  52. | Set(expr1, expr2) -> let TRef(t) = (typecheck_aux gamma expr1) in
  53. if (typecheck_aux gamma expr2) = t then t else raise TypeError
  54. | Get(expr) -> let TRef(t) = (typecheck_aux gamma expr) in t
  55. | Record(recordlist) -> TRec(rec_helper recordlist gamma)
  56. | Select(label, expr) -> let TRec(t) = (typecheck_aux gamma expr) in (select_helper t label)
  57. | Raise(exnid, fbtype, expr) -> TInt
  58. | Try(expr1, exnid, ident, fbtype, expr2) -> TInt
  59. | Cell(int) -> TInt
  60.  
  61. and lookup gamma ident = match gamma with
  62. [] -> raise TypeError
  63. | (id, t)::xs -> if id = ident then t
  64. else lookup xs ident
  65.  
  66. and rec_helper recordlist gamma = match recordlist with
  67. [] -> []
  68. | (l, e)::rs -> (l, (typecheck_aux gamma e)) :: (rec_helper rs gamma)
  69.  
  70. and select_helper reclist label =
  71. match reclist with
  72. [] -> raise TypeError
  73. | (l, t)::rs -> if l = label then t else (select_helper rs label)
  74.  
  75. ;;
  76.  
  77. let typecheck e = typecheck_aux [] e;;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement