-- | @futhark query@
module Futhark.CLI.Query (main) where

import Futhark.Compiler
import Futhark.Util.Loc
import Futhark.Util.Options
import Language.Futhark.Query
import Language.Futhark.Syntax
import Text.Read (readMaybe)

-- | Run @futhark query@.
main :: String -> [String] -> IO ()
main :: String -> [String] -> IO ()
main = forall cfg.
cfg
-> [FunOptDescr cfg]
-> String
-> ([String] -> cfg -> Maybe (IO ()))
-> String
-> [String]
-> IO ()
mainWithOptions () [] String
"program line col" forall a b. (a -> b) -> a -> b
$ \[String]
args () ->
  case [String]
args of
    [String
file, String
line, String
col] -> do
      Int
line' <- forall a. Read a => String -> Maybe a
readMaybe String
line
      Int
col' <- forall a. Read a => String -> Maybe a
readMaybe String
col
      forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ do
        (Warnings
_, Imports
imports, VNameSource
_) <- forall (m :: * -> *).
MonadIO m =>
String -> m (Warnings, Imports, VNameSource)
readProgramOrDie String
file
        -- The 'offset' part of the Pos is not used and can be arbitrary.
        case Imports -> Pos -> Maybe AtPos
atPos Imports
imports forall a b. (a -> b) -> a -> b
$ String -> Int -> Int -> Int -> Pos
Pos String
file Int
line' Int
col' Int
0 of
          Maybe AtPos
Nothing -> String -> IO ()
putStrLn String
"No information available."
          Just (AtName QualName VName
qn Maybe BoundTo
def Loc
loc) -> do
            String -> IO ()
putStrLn forall a b. (a -> b) -> a -> b
$ String
"Name: " forall a. [a] -> [a] -> [a]
++ forall a. Pretty a => a -> String
pretty QualName VName
qn
            String -> IO ()
putStrLn forall a b. (a -> b) -> a -> b
$ String
"Position: " forall a. [a] -> [a] -> [a]
++ forall a. Located a => a -> String
locStr (forall a. Located a => a -> SrcLoc
srclocOf Loc
loc)
            case Maybe BoundTo
def of
              Maybe BoundTo
Nothing -> forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
              Just (BoundTerm StructType
t Loc
defloc) -> do
                String -> IO ()
putStrLn forall a b. (a -> b) -> a -> b
$ String
"Type: " forall a. [a] -> [a] -> [a]
++ forall a. Pretty a => a -> String
pretty StructType
t
                String -> IO ()
putStrLn forall a b. (a -> b) -> a -> b
$ String
"Definition: " forall a. [a] -> [a] -> [a]
++ forall a. Located a => a -> String
locStr (forall a. Located a => a -> SrcLoc
srclocOf Loc
defloc)
              Just (BoundType Loc
defloc) ->
                String -> IO ()
putStrLn forall a b. (a -> b) -> a -> b
$ String
"Definition: " forall a. [a] -> [a] -> [a]
++ forall a. Located a => a -> String
locStr (forall a. Located a => a -> SrcLoc
srclocOf Loc
defloc)
              Just (BoundModule Loc
defloc) ->
                String -> IO ()
putStrLn forall a b. (a -> b) -> a -> b
$ String
"Definition: " forall a. [a] -> [a] -> [a]
++ forall a. Located a => a -> String
locStr (forall a. Located a => a -> SrcLoc
srclocOf Loc
defloc)
              Just (BoundModuleType Loc
defloc) ->
                String -> IO ()
putStrLn forall a b. (a -> b) -> a -> b
$ String
"Definition: " forall a. [a] -> [a] -> [a]
++ forall a. Located a => a -> String
locStr (forall a. Located a => a -> SrcLoc
srclocOf Loc
defloc)
    [String]
_ -> forall a. Maybe a
Nothing