Permalink
Browse files

main infrastructure

  • Loading branch information...
1 parent cdba169 commit 158380b5991449d214e84b2b0b68ea8783bca40c Alexander Bernauer committed Oct 20, 2011
@@ -16,15 +16,20 @@ library
ghc-options: -Wall
hs-source-dirs: src
build-depends:
- bytestring >= 0.9.1.10
+ bytestring >= 0.9.1.10,
+ parsec >= 3.1.1
exposed-modules:
- Language.Bitcoin.Machine
- Language.Bitcoin.Opcodes
+ Language.Bitcoin.Types
Language.Bitcoin.Simulator
+ Language.Bitcoin.Assembler
+ Language.Bitcoin.Parser
-executable bitcoin-script-engine
+executable bitcoin-script
hs-source-dirs: src
main-is: Main.hs
+ other-modules:
+ Language.Bitcoin.Main
+ Language.Bitcoin.Options
build-depends: base >= 4
ghc-options: -Wall
@@ -0,0 +1,138 @@
+module Language.Bitcoin.Assembler
+-- export {{{1
+(
+ run_assembler, run_disassembler
+) where
+
+-- imports
+import Language.Bitcoin.Types
+
+-- run_assembler :: Code -> Binary {{{1
+run_assembler :: Code -> Binary
+run_assembler = undefined
+
+-- run_disassembler :: Binary -> Code {{{1
+run_disassembler :: Binary -> Code
+run_disassembler = undefined
+
+--opcodes :: [(Opcode, Int)]
+--opcodes = [
+-- (OP_FALSE, 0)
+-- , (OP_0, 0)
+-- , (OP_1NEGATE, 79)
+-- , (OP_TRUE, 81)
+-- , (OP_1, 81)
+-- , (OP_2, 82)
+-- , (OP_3, 83)
+-- , (OP_4, 84)
+-- , (OP_5, 85)
+-- , (OP_6, 86)
+-- , (OP_7, 87)
+-- , (OP_8, 88)
+-- , (OP_9, 89)
+-- , (OP_10, 90)
+-- , (OP_11, 91)
+-- , (OP_12, 92)
+-- , (OP_13, 93)
+-- , (OP_14, 94)
+-- , (OP_15, 95)
+-- , (OP_16, 96)
+-- , (OP_NOP, 97)
+-- , (OP_IF, 99)
+-- , (OP_NOTIF, 100)
+-- , (OP_ELSE, 103)
+-- , (OP_ENDIF, 104)
+-- , (OP_VERIFY, 105)
+-- , (OP_RETURN, 106)
+-- , (OP_TOALTSTACK, 107)
+-- , (OP_FROMALTSTACK, 108)
+-- , (OP_IFDUP, 115)
+-- , (OP_DEPTH, 116)
+-- , (OP_DROP, 117)
+-- , (OP_DUP, 118)
+-- , (OP_NIP, 119)
+-- , (OP_OVER, 120)
+-- , (OP_PICK, 121)
+-- , (OP_ROLL, 122)
+-- , (OP_ROT, 123)
+-- , (OP_SWAP, 124)
+-- , (OP_TUCK, 125)
+-- , (OP_2DROP, 109)
+-- , (OP_2DUP, 110)
+-- , (OP_3DUP, 111)
+-- , (OP_2OVER, 112)
+-- , (OP_2ROT, 113)
+-- , (OP_2SWAP, 114)
+-- , (OP_CAT, 126)
+-- , (OP_SUBSTR, 127)
+-- , (OP_LEFT, 128)
+-- , (OP_RIGHT, 129)
+-- , (OP_SIZE, 130)
+-- , (OP_INVERT, 131)
+-- , (OP_AND, 132)
+-- , (OP_OR, 133)
+-- , (OP_XOR, 134)
+-- , (OP_EQUAL, 135)
+-- , (OP_EQUALVERIFY, 136)
+-- , (OP_1ADD, 139)
+-- , (OP_1SUB, 140)
+-- , (OP_2MUL, 141)
+-- , (OP_2DIV, 142)
+-- , (OP_NEGATE, 143)
+-- , (OP_ABS, 144)
+-- , (OP_NOT, 145)
+-- , (OP_0NOTEQUAL, 146)
+-- , (OP_ADD, 147)
+-- , (OP_SUB, 148)
+-- , (OP_MUL, 149)
+-- , (OP_DIV, 150)
+-- , (OP_MOD, 151)
+-- , (OP_LSHIFT, 152)
+-- , (OP_RSHIFT, 153)
+-- , (OP_BOOLAND, 154)
+-- , (OP_BOOLOR, 155)
+-- , (OP_NUMEQUAL, 156)
+-- , (OP_NUMEQUALVERIFY, 157)
+-- , (OP_NUMNOTEQUAL, 158)
+-- , (OP_LESSTHAN, 159)
+-- , (OP_GREATERTHAN, 160)
+-- , (OP_LESSTHANOREQUAL, 161)
+-- , (OP_GREATERTHANOREQUAL, 162)
+-- , (OP_MIN, 163)
+-- , (OP_MAX, 164)
+-- , (OP_WITHIN, 165)
+-- , (OP_RIPEMD160, 166)
+-- , (OP_SHA1, 167)
+-- , (OP_SHA256, 168)
+-- , (OP_HASH160, 169)
+-- , (OP_HASH256, 170)
+-- , (OP_CODESEPARATOR, 171)
+-- , (OP_CHECKSIG, 172)
+-- , (OP_CHECKSIGVERIFY, 173)
+-- , (OP_CHECKMULTISIG, 174)
+-- , (OP_CHECKMULTISIGVERIFY, 175)
+-- , (OP_PUBKEYHASH, 253)
+-- , (OP_PUBKEY, 254)
+-- , (OP_INVALIDOPCODE, 255)
+-- , (OP_RESERVED, 80)
+-- , (OP_VER, 98)
+-- , (OP_VERIF, 101)
+-- , (OP_VERNOTIF, 102)
+-- , (OP_RESERVED1, 137)
+-- , (OP_RESERVED2, 138)
+-- , (OP_NOP1, 176)
+-- , (OP_NOP2, 177)
+-- , (OP_NOP3, 178)
+-- , (OP_NOP4, 179)
+-- , (OP_NOP5, 180)
+-- , (OP_NOP6, 181)
+-- , (OP_NOP7, 182)
+-- , (OP_NOP8, 183)
+-- , (OP_NOP9, 184)
+-- , (OP_NOP10, 185)
+-- ]
+--
+--instance Enum Opcode where
+-- fromEnum opcode = fromJust $ lookup opcode opcodes
+-- toEnum byte = fst $ fromJust $ find ((==byte) . snd) opcodes
+--
@@ -1,28 +0,0 @@
-module Language.Bitcoin.Machine where
-
-import Language.Bitcoin.Opcodes (Opcode)
-import Data.Word
-
-type Data = [Word8]
-type Stack = [Data]
-
-data Command =
- CmdOpcode Opcode
- | CmdData Data
- deriving (Show, Eq)
-
-type Script = [Command]
-
-data Machine = Machine Script Stack Stack deriving (Show)
-
-data ResultCode =
- Success
- | Failure String
- | Error String
-
-instance Show ResultCode where
- show Success = "Bitcoin script completed successfully."
- show (Failure what) = "Bitcoin script failed: " ++ what
- show (Error what) = "Bitcoin script is illegal: " ++ what
-
-data Result = Result Machine ResultCode deriving (Show)
@@ -0,0 +1,48 @@
+module Language.Bitcoin.Main where
+
+import qualified Data.ByteString.Char8 as B
+import Language.Bitcoin.Options
+import Language.Bitcoin.Types (ResultCode(Error), Result(Result))
+import Language.Bitcoin.Simulator (run_simulator)
+import Language.Bitcoin.Assembler (run_assembler, run_disassembler)
+import Language.Bitcoin.Parser (run_parser)
+import System.Environment (getArgs, getProgName)
+import System.Exit (exitWith, ExitCode(ExitFailure))
+import System.IO (stderr, hPutStrLn)
+
+main :: IO ()
+main = do
+ prg <- getProgName
+ args <- getArgs
+ opts <- exitOnError $ options prg args
+ input <- B.readFile $ optInput opts
+ output <- exitOnError $ runAction opts input
+ if (optOutput opts /= optOutput defaultOptions)
+ then B.writeFile (optOutput opts) output
+ else return ()
+
+runAction :: Options -> B.ByteString -> Either String B.ByteString
+runAction opts input
+ | optAssembler opts = assembler input
+ | optDisassembler opts = disassembler input
+ | optSimulator opts = simulator input
+ | otherwise = error "internal error"
+
+assembler :: B.ByteString -> Either String B.ByteString
+assembler code = Right $ run_assembler code
+
+disassembler :: B.ByteString -> Either String B.ByteString
+disassembler binary = Right $ run_disassembler binary
+
+simulator :: B.ByteString -> Either String B.ByteString
+simulator code =
+ case run_parser code of
+ Left parseError -> Left $ show parseError
+ Right script ->
+ case run_simulator script of
+ result@(Result (Error _) _) -> Left $ show result
+ result -> Right $ B.pack (show result)
+
+exitOnError :: Either String a -> IO a
+exitOnError (Left e) = hPutStrLn stderr e >> exitWith (ExitFailure 1)
+exitOnError (Right x) = return x
Oops, something went wrong.

0 comments on commit 158380b

Please sign in to comment.