Browse files

simulator prototype

  • Loading branch information...
1 parent 158380b commit afa8cc93cfb50fdfe5c24567a39d9a8dee6bcbcf Alexander Bernauer committed Oct 20, 2011
Showing with 16 additions and 15 deletions.
  1. +1 −0 examples/fail.bcs
  2. +11 −11 src/Language/Bitcoin/Main.hs
  3. +4 −4 src/Language/Bitcoin/Parser.hs
View
1 examples/fail.bcs
@@ -0,0 +1 @@
+OP_FALSE
View
22 src/Language/Bitcoin/Main.hs
@@ -19,24 +19,24 @@ main = do
output <- exitOnError $ runAction opts input
if (optOutput opts /= optOutput defaultOptions)
then B.writeFile (optOutput opts) output
- else return ()
+ else putStrLn $ B.unpack output
runAction :: Options -> B.ByteString -> Either String B.ByteString
runAction opts input
- | optAssembler opts = assembler input
- | optDisassembler opts = disassembler input
- | optSimulator opts = simulator input
+ | optAssembler opts = Right $ assembler input
+ | optDisassembler opts = Right $ disassembler input
+ | optSimulator opts = simulator opts input
| otherwise = error "internal error"
-assembler :: B.ByteString -> Either String B.ByteString
-assembler code = Right $ run_assembler code
+assembler :: B.ByteString -> B.ByteString
+assembler = run_assembler
-disassembler :: B.ByteString -> Either String B.ByteString
-disassembler binary = Right $ run_disassembler binary
+disassembler :: B.ByteString -> B.ByteString
+disassembler = run_disassembler
-simulator :: B.ByteString -> Either String B.ByteString
-simulator code =
- case run_parser code of
+simulator :: Options -> B.ByteString -> Either String B.ByteString
+simulator opts code =
+ case run_parser (optInput opts) code of
Left parseError -> Left $ show parseError
Right script ->
case run_simulator script of
View
8 src/Language/Bitcoin/Parser.hs
@@ -10,12 +10,12 @@ import Text.ParserCombinators.Parsec
import qualified Data.ByteString.Char8 as B
-- run_parser :: Code -> Script {{{1
-run_parser :: Code -> Either ParseError Script
-run_parser code = parse script "(unknown)" (B.unpack code)
+run_parser :: String -> Code -> Either ParseError Script
+run_parser source code = parse script source (B.unpack code)
script :: Parser Script
-script = sepBy operation separator
+script = endBy operation separator
operation :: Parser Opcode
operation = opcode -- <|> paste
@@ -25,7 +25,7 @@ opcode = string "OP_FALSE" >> return OP_FALSE
--paste = string "PASTE" >> bytes >>= (\bs -> return PASTE DATA (pack bs))
separator :: Parser Char
-separator = char '\n' -- <|> char ';'
+separator = char '\n' <|> char ';'
-- run_printer :: Script -> Code {{{1

0 comments on commit afa8cc9

Please sign in to comment.