Chapter 07

トイ・コードの表現

Nobuo Yamashita
Nobuo Yamashita
2021.12.06に更新
このチャプターの目次

06 Code の実装 (toy06)

プログラムの内部表現 CodeOperatorOperand の対としましょう。

type Code = (Operator, Operand)

Operator は10個の命令に対応するデータ構成子で構成します。文字列からの変換のために、Read のインスタンスにしておきます。また、値の確認を容易にするために、Show のインスタンスにもしておきます。

data Operator
    = STOP
    | GET
    | PRINT
    | LOAD
    | STORE
    | ADD
    | SUB
    | GOTO
    | IFZERO
    | IFPOS
    deriving (Show, Read)

Operand は数値あるいはラベルです。オペランドをとらないオペレータもあるので、そのようなオペレータのときは、None というオペランドをとるとしておきます。

data Operand
    = None
    | Num Int
    | Lab Label
    deriving Show

コード

ここまでの Toy モジュール
src/Toy.hs
module Toy where

type SourceCode  = String
type Interactive = [Input] -> [Output]
type Input  = String
type Output = String

drive :: Interactive -> (String -> String)
drive f = unlines . f . lines

toy :: SourceCode -> Interactive
toy prog= map output . eval . initState (load prog)

output :: ToyState -> Output
output = undefined

load :: SourceCode -> Memory
load = undefined

initState :: Memory -> [Input] -> ToyState
initState = undefined

type Memory = [(Label, Content)]
type Label  = String

data Content
    = Code Code
    | Data Int

type Code = (Operator, Operand)

data Operator
    = STOP
    | GET
    | PRINT
    | LOAD
    | STORE
    | ADD
    | SUB
    | GOTO
    | IFZERO
    | IFPOS
    deriving (Show, Read)

data Operand
    = None
    | Num Int
    | Lab Label
    deriving Show

type ToyState = (Final, Memory, Acc, [Input], Output)
type Final = Bool
type Acc   = Int

eval :: ToyState -> [ToyState]
eval state = state : rests
    where
        rests | isFinal state = []
              | otherwise     = eval (step state)

isFinal :: ToyState -> Bool
isFinal state@(flg,_,_,_,_) = flg

step :: ToyState -> ToyState
step state = execute (decode (fetch state)) state

type Instruction = ToyState -> ToyState

fetch :: ToyState -> Code
fetch = undefined

decode :: Code -> Instruction
decode = undefined

execute :: Instruction -> ToyState -> ToyState
execute = id