Skip to content
This repository
tree: a9146a9025
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 78 lines (60 sloc) 2.192 kb
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 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
module Ocram.Ruab where

import Text.JSON

import qualified Data.ByteString.Char8 as BS

data TLocation = TLocation {
    tlocRow :: Int
  , tlocCol :: Int
  , tlocLen :: Int
  } deriving (Show, Read)

data ELocation = ELocation {
    elocRow :: Int
  , elocCol :: Int
  , elocTidd :: Maybe Int
  }

data Location = Location TLocation ELocation

instance JSON Location where
  readJSON val = do
    (tr:tc:tl:er:ec:tid) <- readJSON val
    case tid of
      [] -> return $ Location (TLocation tr tc tl) (ELocation er ec Nothing)
      [tid'] -> return $ Location (TLocation tr tc tl) (ELocation er ec (Just tid'))
      _ -> Error "wrong number of values in JSON array of Location type"

  showJSON (Location (TLocation tr tc tl) (ELocation er ec Nothing)) = showJSON [tr, tc, tl, er, ec]
  showJSON (Location (TLocation tr tc tl) (ELocation er ec (Just tid))) = showJSON [tr, tc, tl, er, ec, tid]

type LocMap = [Location]

type Variable = String
type VarMap = [(Variable, Variable)]

data File = File {
    fileName :: FilePath
  , fileChecksum :: String
  }

instance JSON File where
  readJSON val = readJSON val >>= \[n,c] -> return $ File n c
  showJSON (File n c) = showJSON [n, c]

data DebugInfo = DebugInfo {
    diTcode :: File
  , diPcode :: BS.ByteString
  , diEcode :: File
  , diLocMap :: LocMap
  , diVarMap :: VarMap
  }

instance JSON DebugInfo where
  showJSON (DebugInfo tcode pcode ecode lm vm) = (JSObject . toJSObject) [
      ("tcode", showJSON tcode)
    , ("pcode", showJSON pcode)
    , ("ecode", showJSON ecode)
    , ("locmap", showJSON lm)
    , ("varmap", showJSON vm)
    ]

  readJSON (JSObject obj) = do
    let [tcode, pcode, ecode, lm, vm] = map snd $ fromJSObject obj
    [tcode', ecode'] <- mapM readJSON [tcode, ecode]
    pcode' <- readJSON pcode
    lm' <- readJSON lm
    vm' <- readJSON vm
    return $ DebugInfo tcode' pcode' ecode' lm' vm'

  readJSON _ = Error "unexpected JSON value for DebugInfo type"

encode_debug_info :: DebugInfo -> BS.ByteString
encode_debug_info = BS.pack . encodeStrict

decode_debug_info :: BS.ByteString -> Either String DebugInfo
decode_debug_info string = (resultToEither . decodeStrict . BS.unpack) string
Something went wrong with that request. Please try again.