小编Bha*_*rni的帖子

用 Haskell 编写的解析器未按预期工作

我正在摆弄 Haskell 的parsec库。我试图将形式的十六进制字符串解析"#x[0-9A-Fa-f]*"为整数。这是我认为可行的代码:

module Main where

import Control.Monad
import Numeric
import System.Environment
import Text.ParserCombinators.Parsec hiding (spaces)

parseHex :: Parser Integer
parseHex = do
  string "#x"
  x <- many1 hexDigit
  return (fst (head (readHex x)))

testHex :: String -> String
testHex input = case parse parseHex "lisp" input of
  Left err -> "Does not match " ++ show err
  Right val -> "Matched" ++ show val

main :: IO ()
main = do
  args <- getArgs
  putStrLn …
Run Code Online (Sandbox Code Playgroud)

parsing haskell parsec

5
推荐指数
1
解决办法
125
查看次数

从 Rust 中的枚举中提取盒装枚举

我正在用 Rust 编写一个类似Scheme 的语言的解释器。我将 AST 编码为枚举:

#[derive(Debug)]
pub enum Object {
    Integer(i64),
    Boolean(bool),
    Character(char),
    String(String),
    Symbol(String),
    Cons { car: Box<Object>, cdr: Box<Object> },
    Nil,
}

pub type ObjectBox = Box<Object>;
Run Code Online (Sandbox Code Playgroud)

我想添加一个car(&self) -> ObjectBox方法到Object. 我想出了:

impl Object {
    pub fn car(&self) -> Option<ObjectBox> {
        match self {
            Object::Cons { car, cdr: _ } => Some(ObjectBox::new(**car)),
            _ => None,
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

编译时我收到此错误:

error[E0507]: cannot move out of `**car` which is behind a shared reference
  --> src/core/object.rs:94:65
   | …
Run Code Online (Sandbox Code Playgroud)

enums rust

1
推荐指数
1
解决办法
342
查看次数

标签 统计

enums ×1

haskell ×1

parsec ×1

parsing ×1

rust ×1