你如何在一个简单的编程语言中实现一个解释器(在Haskell中),这是一种命令式的语言

时间:2012-04-27 05:10:18

标签: haskell

我已经获得了语言语义和我应该知道的一切。它只支持很少的操作,并且不会有任何数据类型的概念。所以我可以将任何东西存储在变量中并对它们进行操作。

我有循环,条件和函数调用,就是这样。 我正在寻找一个开始,一个例子而不是一本理论书。有没有人在Haskell中实现过这样的基本语言解释器?我正在寻找指针和参考资料。

谢谢!

4 个答案:

答案 0 :(得分:6)

我现在正在做一个作为练习项目。

它是一种动态类型语言,因此不必声明变量,但每个值都有一个关联的类型。我在Haskell中使用代数数据类型实现了它:

data Value = BoolValue Bool  -- ^ A Boolean value.
           | NumberValue Double  -- ^ A numeric value.
           | StringValue String  -- ^ A string value.
           -- (several others omitted for simplicity)

为了执行程序,我在StateT之上使用ErrorTIO monad变换器:

-- | A monad representing a step in an RPL program.
--
-- This type is an instance of 'MonadState', so each action is a function that
-- takes an 'RPLContext' as input and produces a (potentially different)
-- 'RPLContext' as its result.  It is also an instance of 'MonadError', so an
-- action may fail (with 'throwRPLError').  And it is built on the 'IO' monad,
-- so 'RPL' computations can interact with the outside world.
type RPL = StateT RPLContext (ErrorT RPLError IO)

-- | Executes an 'RPL' computation.
-- The monadic result value (of type @a@) is discarded, leaving only the final
-- 'RPLContext'.
runRPL :: RPL a  -- ^ The computation to run
       -> RPLContext  -- ^ The computation's initial context
       -> IO (Either RPLError RPLContext)
       -- ^ An 'IO' action that performs the operation, producing either
       -- a modified context if it succeeds, or an error if it fails.
runRPL a = runErrorT . (execStateT a)

“context”是数据堆栈(它是基于堆栈的语言)和“环境”的组合,它包含当前范围内的所有变量:

-- | The monadic state held by an 'RPL' computation.
data RPLContext = RPLContext {
  contextStack :: Stack,  -- ^ The context's data stack.
  contextEnv :: Env  -- ^ The context's environment.
}

(请注意,Stack只是[Value]的别名。)

除此之外,我还有各种辅助函数来处理当前上下文中的堆栈(由StateT monad的RPL部分持有)。例如,以下是将值推送到堆栈所涉及的函数:

-- | Pushes a value onto the stack.
pushValue :: Value -> RPL ()
pushValue x = modifyStack (x:)

-- | Transforms the current stack by a function.
modifyStack :: (Stack -> Stack) -> RPL ()
modifyStack f = do
  stack <- getStack
  putStack $ f stack

-- | Returns the entire current stack.
getStack :: RPL Stack
getStack = fmap contextStack get

-- | Replaces the entire current stack with a new one.
putStack :: Stack -> RPL ()
putStack stack = do
  context <- get
  put $ context { contextStack = stack }

getStackputStackmodifyStack是在MonadState的{​​{1}},getput函数之后建模的,但它们仅在modify记录的一个字段上运行。

所有语言的内置命令都只是RPLContext monad中的操作,它们构建在RPL等工具之上。

为了用我的语言解析代码,我正在使用Parsec。这很不错。


在与我的RPL口译员无关的单独轨道上,您可能会发现“Write Yourself a Scheme in 48 Hours”有帮助。

答案 1 :(得分:6)

我首先要在EDSL中对整个程序进行编码。 EDSL本身就是一个monad并且类似于IO。 GADT使编码非常容易:

{-# LANGUAGE GADTs, KindSignatures #-}

module Interp where

import SomeStuff


data Expr :: * -> * where
    -- Commands
    Print   :: String -> Expr ()
    GetLine :: Expr String

    -- Variables (created on demand)
    GetVar :: Name -> Expr Value
    SetVar :: Name -> Value -> Expr ()

    -- Loop constructs
    While :: Expr Bool -> Expr a -> Expr ()
    For   :: Expr a -> Expr Bool -> Expr b -> Expr c -> Expr ()

    -- Expr is a monad
    Return :: a -> Expr a
    Bind   :: Expr a -> (a -> Expr b) -> Expr b

instance Monad Expr where
    return = Return
    (>>=)  = Bind

runExpr :: Expr a -> StateT Variables IO a
runExpr (Print str) = liftIO (putStrLn str)
runExpr GetLine     = liftIO getLine
runExpr (While p x) =
    fix $ \again -> do
        b <- runExpr p
        when b (runExpr x >> again)
runExpr ...

对于简单语言,您甚至可以在没有专用EDSL的情况下做一些简单的事情:

parseProgram :: Parser (StateT Variables IO ())
parseProgram = ...

人们经常忘记Haskell将函数式编程的概念引入其结论。让解析器返回程序本身。然后你只需要以合适的起始状态运行StateT。

答案 2 :(得分:3)

一种方法是让你的解释器在StateT monad中运行,使用Map来模拟可变变量。简单的例子:

import Control.Monad.State
import Data.Map (Map)
import qualified Data.Map as Map

type VarName = String
data Value = VInt Int
           | VString String
type InterpreterState = Map VarName Value

type InterpretM = StateT InterpreterState IO

putVar :: VarName -> Value -> InterpretM ()
putVar varname value = modify (Map.insert varname value)

getVar :: VarName -> InterpretM Value
getVar varname = do
    m <- gets (Map.lookup varname)
    case m of
        Just x  -> return x
        Nothing -> error $ "Variable " ++ varname ++ " is undefined"

然后解释器将在InterpretM monad中运行。上面的访问器使它可以访问可变变量(不支持闭包和词法范围等优点)。

答案 3 :(得分:1)

相关问题