-- Copyright (C) 2018 Jun Zhang <zhangjunphy[at]gmail[dot]com>
--
-- This file is a part of decafc.
--
-- decafc is free software: you can redistribute it and/or modify it under the
-- terms of the MIT (X11) License as described in the LICENSE file.
--
-- decafc is distributed in the hope that it will be useful, but WITHOUT ANY
-- WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
-- FOR A PARTICULAR PURPOSE.  See the X11 license for more details.

-- Types -- Common types
module Types (VID, Name, ScopeID, BBID, CompileError(..)) where

import Data.Text (Text)
import Util.SourceLoc qualified as SL
import Formatting (sformat, int, stext, (%), formatToString)

-- Variable ID
type VID = Int

-- Var/Function name
type Name = Text

-- Scope of function, basic block, etc.
type ScopeID = Int

-- Basic block ID
type BBID = Int

data CompileError = CompileError
  { CompileError -> Maybe Range
sl :: !(Maybe SL.Range)
  , CompileError -> Text
msg :: !Text
  }

instance Show CompileError where
  show :: CompileError -> String
show (CompileError (Just Range
sl) Text
msg) = Format String (Int -> Int -> Text -> String)
-> Int -> Int -> Text -> String
forall a. Format String a -> a
formatToString (Format (Int -> Text -> String) (Int -> Int -> Text -> String)
forall a r. Integral a => Format r (a -> r)
int Format (Int -> Text -> String) (Int -> Int -> Text -> String)
-> Format String (Int -> Text -> String)
-> Format String (Int -> Int -> Text -> String)
forall r a r'. Format r a -> Format r' r -> Format r' a
% Format (Int -> Text -> String) (Int -> Text -> String)
":" Format (Int -> Text -> String) (Int -> Text -> String)
-> Format String (Int -> Text -> String)
-> Format String (Int -> Text -> String)
forall r a r'. Format r a -> Format r' r -> Format r' a
% Format (Text -> String) (Int -> Text -> String)
forall a r. Integral a => Format r (a -> r)
int Format (Text -> String) (Int -> Text -> String)
-> Format String (Text -> String)
-> Format String (Int -> Text -> String)
forall r a r'. Format r a -> Format r' r -> Format r' a
% Format (Text -> String) (Text -> String)
": error: " Format (Text -> String) (Text -> String)
-> Format String (Text -> String) -> Format String (Text -> String)
forall r a r'. Format r a -> Format r' r -> Format r' a
% Format String (Text -> String)
forall r. Format r (Text -> r)
stext) (Posn -> Int
SL.row (Range -> Posn
SL.start Range
sl) Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) (Posn -> Int
SL.col (Range -> Posn
SL.start Range
sl) Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) Text
msg
  show (CompileError Maybe Range
Nothing Text
msg) = Format String (Text -> String) -> Text -> String
forall a. Format String a -> a
formatToString (Format (Text -> String) (Text -> String)
"error: " Format (Text -> String) (Text -> String)
-> Format String (Text -> String) -> Format String (Text -> String)
forall r a r'. Format r a -> Format r' r -> Format r' a
% Format String (Text -> String)
forall r. Format r (Text -> r)
stext) Text
msg