Advertisement
Guest User

Untitled

a guest
Jul 20th, 2018
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.88 KB | None | 0 0
  1. -# LANGUAGE MultiParamTypeClasses #-}
  2. {-# LANGUAGE FlexibleInstances #-}
  3.  
  4. module Main where
  5. import Data.Bool
  6. import Data.Map as M
  7.  
  8. class Interpretor a where
  9. validate_AST :: a -> Bool
  10. get_next_state :: (a, M.Map String AExp) -> (a, M.Map String AExp)
  11.  
  12. class Operation data_type return_type operations where
  13. operation :: data_type -> operations -> data_type -> M.Map String data_type -> return_type
  14.  
  15.  
  16. --operatiile peste tipurile de date
  17. data AOp = Plus | Minus
  18. data AExp = AOperation AExp AOp AExp | AValue Integer | AString String
  19. --tipurile de date
  20. data BExp = BCompare AExp BAOp AExp | BOperation BExp BLOp BExp | BValue Bool
  21. data BLOp = And | Or
  22. data BAOp = Greater | Lesser
  23. --AST-ul propriu zis
  24. data AST = Init [String] AST | Asign String AExp | If BExp AST AST | While BExp AST | Instructions AST AST | No_AST
  25.  
  26.  
  27. instance Operation AExp AExp AOp where
  28. --operatiile alese
  29. operation (AValue a) Plus (AValue b) _ = AValue (a + b)
  30. operation (AValue a) Minus (AValue b) _ = AValue (a - b)
  31. --spargere in expresii mici, trebuie gasit o solutie sa nu fie luat cu copy paste de fiecare data
  32. operation exp1@(AString symb1) op exp2@(AString symb2) symb_map = operation (symb_map M.! symb1) op (symb_map M.! symb2) symb_map
  33. operation exp1@(AString symb1) op exp2 symb_map = operation (symb_map M.! symb1) op exp2 symb_map
  34. operation exp1 op exp2@(AString symb2) symb_map = operation exp1 op (symb_map M.! symb2) symb_map
  35. operation exp1@(AOperation argv11 argv21 argv31) op exp2@(AOperation argv12 argv22 argv32) symb_map = operation (operation argv11 argv21 argv31 symb_map) op (operation argv12 argv22 argv32 symb_map) symb_map
  36. operation exp1@(AOperation argv1 argv2 argv3) op exp2 symb_map = operation (operation argv1 argv2 argv3 symb_map) op exp2 symb_map
  37. operation exp1 op exp2@(AOperation argv1 argv2 argv3) symb_map = operation exp1 op (operation argv1 argv2 argv3 symb_map) symb_map
  38.  
  39.  
  40. instance Operation BExp BExp BLOp where
  41. --operatiile alese
  42. operation (BValue a) And (BValue b) _ = BValue (a && b)
  43. operation (BValue a) Or (BValue b) _ = BValue (a || b)
  44. --spargere in chestiile elementare
  45. operation exp1@(BOperation argv11 argv21 argv31) op exp2@(BOperation argv12 argv22 argv32) m = operation (operation argv11 argv21 argv31 m) op (operation argv12 argv22 argv32 m) m
  46. operation exp1@(BOperation argv1 argv2 argv3) op exp2 m = operation (operation argv1 argv2 argv3 m) op exp2 m
  47. operation exp1 op exp2@(BOperation argv1 argv2 argv3) m = operation exp1 op (operation argv1 argv2 argv3 m) m
  48.  
  49.  
  50.  
  51.  
  52. instance Interpretor AST where
  53. --folosit pentru validarea unui AST
  54. validate_AST (Init _ remaining_AST) = validate_AST remaining_AST
  55. validate_AST (Asign _ _) = True
  56. validate_AST (If _ first_branch second_branch) = validate_AST first_branch && validate_AST second_branch
  57. validate_AST (Instructions next_instr remaining_AST) = validate_AST next_instr && validate_AST remaining_AST
  58. validate_AST (While _ while_AST) = validate_AST while_AST
  59. --initializez by default cu 0
  60. get_next_state ((Init params remaining_AST), _) = get_next_state (remaining_AST, M.fromList (zip params (repeat (AValue 0))))
  61. get_next_state ((Asign symbol exp1@(AValue number)), symbol_map) = (No_AST , M.insert symbol exp1 symbol_map)
  62. get_next_state ((Asign symbol exp1@(AString assign_to_symbol)), symbol_map) = (No_AST, M.insert symbol (symbol_map M.! assign_to_symbol) symbol_map)
  63. get_next_state ((Asign symbol exp1@(AOperation argv1 argv2 argv3)), symbol_map) = (No_AST, M.insert symbol (operation argv1 argv2 argv3 symbol_map) symbol_map)
  64. get_next_state ((Instructions block1 block2), symbol_map) = get_next_state (block2, new_symbol_map)
  65. where
  66. (_, new_symbol_map) = get_next_state (block1, symbol_map)
  67.  
  68.  
  69. instance Show AOp where
  70. show Plus = "+"
  71. show Minus = "-"
  72.  
  73. instance Show BLOp where
  74. show And = "&&"
  75. show Or = "||"
  76.  
  77. instance Show BAOp where
  78. show Greater = ">"
  79. show Lesser = "<"
  80.  
  81. instance Show AExp where
  82. show (AOperation exp1 op exp2) = show exp1 ++ " " ++ show op ++ " " ++ show exp2
  83. show (AValue number) = show number
  84.  
  85. instance Show BExp where
  86. show (BValue boolean) = show boolean
  87. show (BCompare exp1 boolean arithmetic) = (show exp1) ++ (show boolean) ++ (show arithmetic)
  88. show (BOperation bool1 op bool2) = (show bool1) ++ (show op) ++ (show bool2)
  89.  
  90. instance Show AST where
  91. show (Init params remaining_AST) = "(Init " ++ (show params) ++ (show remaining_AST) ++ ")"
  92. show (Asign expr1 expr2) = "(Asign " ++ show expr1 ++ " " ++ show expr2 ++ ")"
  93. show (If bool_expr first_branch second_branch) = "(If " ++ (show bool_expr) ++ " " ++ (show first_branch) ++ " " ++ (show second_branch) ++ ")"
  94. show (Instructions next_instr remaining_AST) = " " ++ (show next_instr) ++ " " ++ (show remaining_AST)
  95. show (While bool_expr while_AST) = "(while " ++ (show bool_expr) ++ (show while_AST) ++ ")"
  96.  
  97.  
  98.  
  99. main = do putStrLn (show (M.toList final_map))
  100. where
  101. (final_state, final_map) = (get_next_state ((Init ["n", "s"] (Instructions (Asign "n" (AValue 5)) (Asign "s" (AOperation (AValue 5) Plus (AValue 2))))), M.fromList []))
  102. --do putStrLn (show (operation (AOperation (AOperation (AString "n") Plus (AValue 3)) Plus (AOperation (AString "m") Minus (AValue 2))) Plus (AOperation (AValue 3) Plus (AValue 5)) (M.fromList [("n", (AValue 2)), ("m", (AValue 5))])))
  103. --where
  104. --current_AST = (Init ["n", "s"] (Instructions (Asign "n" (AValue 5)) (While (BValue True) (Instructions (Asign "s" (AOperation (AValue 5) Plus (AValue 2))) (Asign "s" (AOperation (AValue 1) Minus (AValue 2)))))))
  105. --test:
  106. --putStrLn (show (Root (Init [] (Instructions (Asign "n" (AValue 5)) (While (BValue True) (Instructions (Asign "s" (AOperation (AValue 5) Plus (AValue 2))) (Asign "s" (AOperation (AValue 1) Minus (AValue 2)))))))))
  107. --mai astept pana sa fac operatii pe Var, maine ma ocup.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement