Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //*****************************************************************************************************************//
- // _____ ____ _ ____ _ _____ _ _
- // | ___/ ___|| |__ __ _ _ __ _ __ / ___|___ __| | ___| ___|__ _ __ _ __ ___ __ _| |_| |_ ___ _ __
- // | |_ \___ \| '_ \ / _` | '__| '_ \| | / _ \ / _` |/ _ \ |_ / _ \| '__| '_ ' _ \ / _' | __| __/ _ \ '__|
- // | _| ___) | | | | (_| | | | |_) | |__| (_) | (_| | __/ _| (_) | | | | | | | | (_| | |_| || __/ |
- // |_| |____/|_| |_|\__,_|_| | .__/ \____\___/ \__,_|\___|_| \___/|_| |_| |_| |_|\__,_|\__|\__\___|_| v9999.99.9
- // |_|
- //*****************************************************************************************************************//
- // Luca Vinci 868166
- // Lorenzo Rubin 870036
- module FSharpCodeFormatter.Formatter
- open Lib
- /// Controlla l'ultimo elemento
- let rec end_with (s : string) token =
- let rec aux = function
- | x :: [] when x = token -> true
- | x :: xs -> aux xs
- | _ -> false
- in aux (tokenize_line s)
- /// Controlla il primo elemento
- let start_with (s : string) token =
- match tokenize_line s with
- | x :: xs when x = token -> true
- | _ -> false
- /// Restituisce il primo elemento dello stack
- let first (i : int list) =
- match i with
- | [] -> failwith "LISTA_VUOTA"
- | [s] -> s
- | s :: ss -> s
- /// Elimina il primo elemento dello stack
- let rm_first (i : int list) =
- match i with
- | [] -> failwith "LISTA_VUOTA"
- | s :: ss -> ss
- /// Funzione indent che data in input una lista che rappresenta le linee di un programma indentato in
- /// modo errato, restituisce la rappresentazione del programma indentato correttamente.
- let indent (lines : string list) =
- let rec aux lines n l =
- match lines with
- | [] -> []
- | s :: ss when s = "" -> (0, s) :: aux ss 0 [0] // Quando c'è una stringa vuota azzera le tabulazioni e lo stack
- | s :: ss when start_with s "let" && end_with s "=" -> (n, s) :: aux ss (n+1) ((n+1) :: l)
- | s :: ss when start_with s "if" && end_with s "then" -> (n, s) :: aux ss (n+1) ((n+1) :: l)
- | s :: ss when start_with s "let" && not (end_with s "=") -> (n, s) :: aux ss n l
- | s :: ss when start_with s "if" && not (end_with s "then") -> (n, s) :: aux ss n l
- | s :: ss when s = "else" -> (first l, s) :: aux ss (first l + 1) (first l + 1 :: rm_first l)
- | s :: ss when start_with s "fun" && end_with s "->" -> (first l, s) :: aux ss (first l + 1) (first l + 1 :: rm_first l)
- | 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)
- | s :: ss when start_with s "in" -> (first l, s) :: aux ss (first l - 1) (rm_first l)
- | s :: ss when start_with s "else" -> (first l, s) :: aux ss n (rm_first l)
- | s :: ss when start_with s "elif" && not (end_with s "then") -> (first l, s) :: aux ss n (first l :: rm_first l)
- | s :: ss when start_with s "match" -> (first l, s) :: aux ss n l
- | s :: ss when start_with s "|" && end_with s "->" ->
- if (tokenize_line ss.Head).Head = "match" then
- (first l, s) :: aux ss (first l + 1) (first l + 1 :: rm_first l)
- else (first l, s) :: aux ss (first l + 1) (first l + 1 :: l)
- | s :: ss when start_with s "|" && not (end_with s "->") ->
- if (ss.Head <> "") && (tokenize_line ss.Head).Head = "|" then
- (first l, s) :: aux ss (first l) (first l :: rm_first l)
- else (first l, s) :: aux ss (first l - 1) (rm_first l)
- | s :: ss -> (first l, s) :: aux ss (first l - 1) (rm_first l)
- in aux lines 0 [0]
- // se non vuoi realizzare la versione avanzata, non modificarla
- let split (w : int) (s : string) = split_lines s
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement