I have been given with the language semantics and everything i should know.It’d only support few operations and there wouldn’t be any concept of Data Types. So i can store anything in a variable and operate on them.
I’d have loops and conditions and function calls and that is it.
I am looking for a start, an example not a theory book. Has anyone ever implemented such a basic language interpreter in Haskell? I am looking for pointers and references.
Thanks !
I’m working on one right now as a practice project.
It’s a dynamically-typed language, so variables don’t have to be declared, but each value has an associated type. I implemented that using an algebraic data type in Haskell:
For execution of programs, I’m using the
StateTandErrorTmonad transformers on top ofIO:The “context” is a combination of a data stack (it’s a stack-based language) and an “environment” that holds all the variables that are currently in scope:
(Note that
Stackis just an alias for[Value].)On top of that foundation, I have a variety of helper functions to do things like manipulate the stack in the current context (held by the
StateTpart of theRPLmonad). For example, here are the functions involved in pushing a value onto the stack:getStack,putStack, andmodifyStackare modeled afterMonadState‘sget,put, andmodifyfunctions, but they operate on just one field of theRPLContextrecord.All the language’s built-in commands are just actions in the
RPLmonad, which build on top of tools likepushValue.For parsing code in my language, I’m using Parsec. It’s pretty nice.
On a separate track, unrelated to my RPL interpreter, you might find “Write Yourself a Scheme in 48 Hours” helpful.