I'm trying to implement some language with x = 4 and pritn x, constructions using haskell happy So far I've defined grammar like this
terms
: term { [$1] }
| term terms { $1 : $2 }
term
: var '=' int { Assign $1 $3 }
| print var { Print $2 }
When I run it over something like
x = 4
print x
y = 5
print y
I get
[Assign "x" 4, Print "x", Assign "y" 5, Print "y"]
Now I want to do actual implementation, but I don't know how to implement "assign"
Though I'm not good at haskell, from happy docs I saw "let" implementation and got the idea of some environment p passed and evaluated in
Exp : let var '=' Exp in Exp { \p -> $6 (($2,$4 p):p) }
| Exp1 { $1 }
Exp1 : Exp1 '+' Term { \p -> $1 p + $3 p }
| Exp1 '-' Term { \p -> $1 p - $3 p }
| Term { $1 }
Term : Term '*' Factor { \p -> $1 p * $3 p }
| Term '/' Factor { \p -> $1 p `div` $3 p }
| Factor { $1 }
Factor
: int { \p -> $1 }
| var { \p -> case lookup $1 p of
Nothing -> error "no var"
Just i -> i }
| '(' Exp ')' { $2 }
I guess "assign" implementation has to do something with this env, but I couldn't find any example. How can I implement assign and print or where can I find information or example of it?