Skip to content

Instantly share code, notes, and snippets.

@lucansky
Created December 26, 2017 19:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lucansky/12e33a71edf6eeea8be365aff9fea869 to your computer and use it in GitHub Desktop.
Save lucansky/12e33a71edf6eeea8be365aff9fea869 to your computer and use it in GitHub Desktop.
Generic interpreter in Haskell
module Data.Gerber.Interpreter where
import Data.Gerber.Types
data InterpreterState = InterpreterState
{ commandsParsed :: Integer }
deriving (Show, Eq)
initState = InterpreterState
{ commandsParsed = 0 }
eval :: Command -> InterpreterState -> InterpreterState
eval command state =
InterpreterState { commandsParsed = commandsParsed state + 1 }
evalBulk :: [Command] -> InterpreterState
evalBulk cmds = go cmds initState
where
go :: [Command] -> InterpreterState -> InterpreterState
go [] imm = imm
go (x) imm = eval x imm
go (x:xs) imm = go xs $ eval x imm
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment