summaryrefslogtreecommitdiff
path: root/Main.hs
blob: d52fedca3a606b787339f66e8ff25bffac35fbf5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
{-# LANGUAGE LambdaCase #-}
module Main where

import Data.Text.Lazy (Text)
import qualified Data.Text.Lazy as T
import qualified Data.Text.Lazy.IO as T
import System.Environment
import System.Exit

import Pattern
import Parser
import Pretty
import SourceFile


apply :: Pattern -> SourceFile -> SourceFile
apply pat (SourceFile chunks) =
    SourceFile $ flip map chunks $ \case CSkip s -> CSkip s
                                         COcc occ -> COcc (apply' pat occ)

apply' :: Pattern -> Occurrence -> Occurrence
apply' (Pattern name items) origocc@(Occurrence name' args)
  | name == name' = Occurrence name' (travOrigsArgs items args)
  | otherwise = origocc

travOrigsArgs :: [Origin] -> [(Text, Text)] -> [(Text, Text)]
travOrigsArgs [] args = args
travOrigsArgs _ [] = []
travOrigsArgs (OCopy : items) (arg:args) = arg : travOrigsArgs items args
travOrigsArgs (ONew name : items) args = (T.pack " ", name) : travOrigsArgs items args

main :: IO ()
main = do
    getArgs >>= \case
        [fname, pat] -> do
            pat' <- case parsePattern pat of
              Left err -> die err
              Right pat' -> return pat'
            source <- readFile fname
            parsed <- case parseSourceFile fname source pat' of
              Left err -> die (show err)
              Right res -> return res
            print parsed
            T.putStrLn (pretty parsed)
            putStrLn (replicate 80 '-')
            T.putStrLn (pretty (apply pat' parsed))
        _ -> do
            die "Usage: refactor-type-parameters <file.hs> <OpenAcc env aenv *taenv t>"