Haskell:读取文件到数据类型时的错误" ***例外:Prelude.read:没有解析"

时间:2014-06-05 07:30:17

标签: file haskell io type-conversion

我的问题可能是格式错误,但我无法找到它是什么错误,当我使用readFile然后我尝试将该字符串读入数据类型时,我收到该错误。

我正在Haskell中实现一个简单的编程语言解释器,如下文所示:

Implementing a language interpreter in haskell

然后,我试图从文件中读取一些来自用户和文件的数据类型,当它来自用户时,它正确地工作,但不适用于从文件读取的类型。我的数据类型如下:

infixr 1 :=
infix 4 :<, :>, :==, :>=, :<= 
infixl 6 :+, :-
infixl 7 :*, :/

data Valuable = I Int --Entero
                | V Variable --Variable
                | Valuable :+ Valuable  
                | Valuable :- Valuable  
                | Valuable :* Valuable  
                | Valuable :/ Valuable  
                deriving (Read, Show); 


data Boolval = B Bool --Dato booleano
               | Valuable :>= Valuable 
               | Valuable :<= Valuable 
               | Valuable :== Valuable  
               | Valuable :> Valuable  
               | Valuable :< Valuable  
               deriving (Read, Show);

data Instruccion = Valuable := Valuable 
                   | While Boolval Instruccion 
                   | Cond Boolval Instruccion  
                   | Varias [Instruccion] 
                   deriving (Read, Show);

当我尝试使用此过程获取输入时:

mst::[VarVal]->String -- Varval is [String, int]
mst [] = ""
mst[(a,b)] = "('" ++ a ++"', "++ (show b) ++ ")"
mst((a,b):xs) = "('" ++ a ++"', "++ (show b) ++ "), " ++ mst xs

main = do 
  putStrLn "Introduce el nombre del programa a cargar (Por ejemplo arch.txt)"
  file <- getLine  --Se toma el nombre de archivo
  putStrLn "Introduce el estado de las variables con comillas dobles, (ej [(`X`,3)])"
  user_state <- getLine   --Se toma la entrada
  arch <- readFile file
  let real_state = read user_state
  let my_program_file = read arch
  putStrLn arch
  let lst = ejecutar my_program_file real_state --problemas
  putStrLn (mst lst)

使用“读取文件”指令时会出现问题,因为输入格式不正确(我不确定),我收到消息:*** Exception: Prelude.read: no parse。这是输入格式

Varias [(V "Y" := (V "X"), 
              (V "R" := I 1), 
               (While  (I 0 :< V "Y")  (Varias [
                       (V "R" := (V "R" :* V "Y")), 
                       (V "Y" := (V "Y" :- I 1))
                       ])
                ) 
            ]

我做错了什么?

0 个答案:

没有答案