import Text.ParserCombinators.Parsec
import System.Environment

{- the "words" of bitcoin script -}
data Token = Number Int
           | String String
           | Operator String

{- matches against <12345> -}
parseRawNum :: Parser Token
parseRawNum = do
    char '<'
    num <- many1 digit
    char '>' 
    return $ Number (read num :: Int)

{- matches string of alphanumeric characters. going to be used for keys, signatures, addrs -}
parseRawStr :: Parser Token
parseRawStr = do
    char '<'
    str <- many1 alphaNum
    char '>'
    return $ String str

{- matches a subset of bitcoin script's arithmetic operators -}
parseArithOp :: Parser Token
parseArithOp = do
    string "OP_"
    op <-  try (string "ADD")
       <|> try (string "1ADD")
       <|> try (string "SUB")
       <|> try (string "1SUB")
       <|> try (string "MIN")
       <|> try (string "MAX")
    eof -- Token should end here
    return $ Operator op

parseToken :: Parser Token
parseToken = try parseRawNum <|>
             try parseRawStr <|>
             parseArithOp

readExpr :: String -> String
readExpr input = case parse parseToken "Bitcoin Script" input of
    Left err -> "No match: " ++ show err
    Right val -> "Found value"

main :: IO ()
main = do
    (expr:_) <- getArgs
    putStrLn . readExpr $ expr
