Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@ cabal run amnesia -- <program.amn>

I highly recommend watching:
- [Haskell for Imperative Programmers](https://www.youtube.com/watch?v=Vgu82wiiZ90&list=PLe7Ei6viL6jGp1Rfu0dil1JH1SHk9bgDV&index=1)
- [Megaparsec tutorial](https://markkarpov.com/tutorial/megaparsec.html)
4 changes: 3 additions & 1 deletion amnesia.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ common warnings
executable amnesia
import: warnings
main-is: Amnesia.hs
build-depends: base ^>=4.18.3.0
build-depends:
base ^>=4.18.3.0,
megaparsec,
hs-source-dirs: src
default-language: GHC2021
17 changes: 1 addition & 16 deletions src/Amnesia.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,7 @@ module Main where

import System.Environment (getArgs)
import Stacks

data Instr
= ChoA -- select stack A
| ChoB -- select stack B
| ChoC -- select stack C
| Set Int -- set selected stack to specific value
| Add -- A = A + B
| Sub -- A = A - B
| Mul -- A = A * B
| Div -- A = A / B
| Mod -- A = A % B
| Nm -- non-zero check
| Disp -- print top of stack
| Open -- ":" open bracket
| Close -- ";" close bracket
deriving(Show, Eq)
import Parser

main :: IO ()
main = do
Expand Down
36 changes: 36 additions & 0 deletions src/Parser.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
module Parser
( Instr(..)
-- , parseProgram
, cho -- fixme: for testing
) where

import Stacks (S(..))
import Text.Megaparsec
import Text.Megaparsec.Char
import Data.Void

data Instr
= Cho S -- choose stack A/B/C
| Set -- =
| Add -- +
| Sub -- -
| Mul -- *
| Div -- /
| Mod -- %
| Disp -- disp
| Nm -- conditional check
| Open -- ':'
| Close -- ';'
deriving(Show, Eq)

type Parser = Parsec Void String

cho :: Parser Instr
cho = do
_ <- string "cho"
s <- satisfy (`elem` "ABC")
return $ case s of
'A' -> Cho A
'B' -> Cho B
'C' -> Cho C
_ -> error "?"