Advertisement
LucaVinci110898

FSharpCodeFormatter

Dec 16th, 2017
362
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
F# 4.54 KB | None | 0 0
  1. //*****************************************************************************************************************//
  2. //    _____ ____  _                       ____          _      _____                          _   _
  3. //   |  ___/ ___|| |__   __ _ _ __ _ __  / ___|___   __| | ___|  ___|__  _ __ _ __ ___   __ _| |_| |_ ___ _ __
  4. //   | |_  \___ \| '_ \ / _` | '__| '_ \| |   / _ \ / _` |/ _ \ |_ / _ \| '__| '_ ' _ \ / _' | __| __/ _ \ '__|
  5. //   |  _|  ___) | | | | (_| | |  | |_) | |__| (_) | (_| |  __/  _| (_) | |  | | | | | | (_| | |_| ||  __/ |
  6. //   |_|   |____/|_| |_|\__,_|_|  | .__/ \____\___/ \__,_|\___|_|  \___/|_|  |_| |_| |_|\__,_|\__|\__\___|_|   v9999.99.9
  7. //                                |_|          
  8. //*****************************************************************************************************************//
  9. // Luca Vinci       868166
  10. // Lorenzo Rubin    870036
  11.  
  12. module FSharpCodeFormatter.Formatter
  13.  
  14. open Lib
  15.  
  16. /// Controlla l'ultimo elemento
  17. let rec end_with (s : string) token =
  18.     let rec aux = function
  19.     | x :: [] when x = token -> true
  20.     | x :: xs -> aux xs
  21.     | _ -> false
  22.     in aux (tokenize_line s)
  23.    
  24. /// Controlla il primo elemento
  25. let start_with (s : string) token =
  26.     match tokenize_line s with
  27.     | x :: xs when x = token -> true
  28.     | _ -> false
  29.  
  30. /// Restituisce il primo elemento dello stack
  31. let first (i : int list) =
  32.     match i with
  33.     | [] -> failwith "LISTA_VUOTA"
  34.     | [s] -> s
  35.     | s :: ss -> s
  36.  
  37. /// Elimina il primo elemento dello stack
  38. let rm_first (i : int list) =
  39.     match i with
  40.     | [] -> failwith "LISTA_VUOTA"
  41.     | s :: ss -> ss
  42.    
  43. /// Funzione indent che data in input una lista che rappresenta le linee di un programma indentato in
  44. /// modo errato, restituisce la rappresentazione del programma indentato correttamente.
  45. let indent (lines : string list) =
  46.     let rec aux lines n l =
  47.         match lines with
  48.         | [] -> []
  49.         | s :: ss when s = ""                                         -> (0, s) :: aux ss 0 [0] // Quando c'è una stringa vuota azzera le tabulazioni e lo stack
  50.         | s :: ss when start_with s "let"  && end_with s "="          -> (n, s) :: aux ss (n+1) ((n+1) :: l)
  51.         | s :: ss when start_with s "if"   && end_with s "then"       -> (n, s) :: aux ss (n+1) ((n+1) :: l)
  52.         | s :: ss when start_with s "let"  && not (end_with s "=")    -> (n, s) :: aux ss n l
  53.         | s :: ss when start_with s "if"   && not (end_with s "then") -> (n, s) :: aux ss n l  
  54.         | s :: ss when s = "else"                                     -> (first l, s) :: aux ss (first l + 1) (first l + 1 :: rm_first l)
  55.         | s :: ss when start_with s "fun"  && end_with s "->"         -> (first l, s) :: aux ss (first l + 1) (first l + 1 :: rm_first l)      
  56.         | s :: ss when start_with s "elif" && end_with s "then"       -> (first l, s) :: aux ss (first l + 1) (first l + 1 :: first l :: rm_first l)
  57.         | s :: ss when start_with s "in"                              -> (first l, s) :: aux ss (first l - 1) (rm_first l)
  58.         | s :: ss when start_with s "else"                            -> (first l, s) :: aux ss n (rm_first l)
  59.         | s :: ss when start_with s "elif" && not (end_with s "then") -> (first l, s) :: aux ss n (first l :: rm_first l)
  60.         | s :: ss when start_with s "match"                           -> (first l, s) :: aux ss n l
  61.         | s :: ss when start_with s "|"    && end_with s "->"         ->
  62.                                                                         if (tokenize_line ss.Head).Head = "match" then
  63.                                                                             (first l, s) :: aux ss (first l + 1) (first l + 1 :: rm_first l)
  64.                                                                         else (first l, s) :: aux ss (first l + 1) (first l + 1 :: l)
  65.         | s :: ss when start_with s "|"    && not (end_with s "->")   ->
  66.                                                                         if (ss.Head <> "") && (tokenize_line ss.Head).Head = "|" then
  67.                                                                             (first l, s) :: aux ss (first l) (first l :: rm_first l)
  68.                                                                         else (first l, s) :: aux ss (first l - 1) (rm_first l)
  69.  
  70.         | s :: ss                                                     -> (first l, s) :: aux ss (first l - 1) (rm_first l)
  71.     in aux lines 0 [0]
  72.  
  73.  
  74. // se non vuoi realizzare la versione avanzata, non modificarla
  75. let split (w : int) (s : string) = split_lines s
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement