Skip to content

Commit

Permalink
Add commands set_base and set_ascii for pretty-printing options.
Browse files Browse the repository at this point in the history
This is one step towards fixing issues #28 and #29. So far, users
can set these values but the pretty printer does not use them yet.
  • Loading branch information
Brian Huffman committed Aug 3, 2015
1 parent 5fbd78f commit d72c220
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
20 changes: 20 additions & 0 deletions src/SAWScript/Interpreter.hs
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,7 @@ buildTopLevelEnv opts =
, rwTypes = tm0
, rwDocs = primDocEnv
, rwCryptol = ce0
, rwPPOpts = defaultPPOpts
}
return (bic, ro0, rw0)

Expand All @@ -326,6 +327,16 @@ include_value file = do
rw' <- io $ interpretFile ro rw file
putTopLevelRW rw'

set_ascii :: Bool -> TopLevel ()
set_ascii b = do
rw <- getTopLevelRW
putTopLevelRW rw { rwPPOpts = (rwPPOpts rw) { ppOptsAscii = b } }

set_base :: Int -> TopLevel ()
set_base b = do
rw <- getTopLevelRW
putTopLevelRW rw { rwPPOpts = (rwPPOpts rw) { ppOptsBase = b } }

print_value :: Value -> TopLevel ()
print_value (VString s) = io $ putStrLn s
print_value (VTerm t) = do
Expand Down Expand Up @@ -422,6 +433,15 @@ primitives = Map.fromList
(pureVal envCmd)
[ "Print all sawscript values in scope." ]

, prim "set_ascii" "Bool -> TopLevel ()"
(pureVal set_ascii)
[ "Select whether to pretty-print arrays of 8-bit numbers as ascii strings." ]

, prim "set_base" "Int -> TopLevel ()"
(pureVal set_base)
[ "Set the number base for pretty-printing numeric literals."
, "Permissible values include 2, 8, 10, and 16." ]

, prim "print" "{a} a -> TopLevel ()"
(pureVal print_value)
[ "Print the value of the given expression." ]
Expand Down
5 changes: 4 additions & 1 deletion src/SAWScript/Value.hs
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,12 @@ isVUnit _ = False

data PPOpts = PPOpts
{ ppOptsAnnotate :: Bool
, ppOptsAscii :: Bool
, ppOptsBase :: Int
}

defaultPPOpts :: PPOpts
defaultPPOpts = PPOpts False
defaultPPOpts = PPOpts False False 10

commaSep :: [ShowS] -> ShowS
commaSep ss = foldr (.) id (intersperse (showString ",") ss)
Expand Down Expand Up @@ -232,6 +234,7 @@ data TopLevelRW =
, rwTypes :: Map SS.LName SS.Schema
, rwDocs :: Map SS.Name String
, rwCryptol :: CEnv.CryptolEnv SAWCtx
, rwPPOpts :: PPOpts
}

newtype TopLevel a = TopLevel (ReaderT TopLevelRO (StateT TopLevelRW IO) a)
Expand Down

0 comments on commit d72c220

Please sign in to comment.