haskell - Handling nested variable scopes when implementing an interpreter -
i'm writing interpreter simple programming language , wanted ask on best approach tackle it.
the environment program follows:
type env = [[(var, int)]]
so i've coded lookup
, update
i'm bit stuck on how deal the scope each begin block. example shown below:
begin [a,b,c] read n = 1 while < 0 begin n = 2 * n = - 1 end; write n end
from understanding scope of first begin [a,b,c,i,n] , second begin contain [i, n]
therefore env
[ [ ("a",0), (b",0), ("c",0), ("i",3), ("n",2) ], [("n",8), ("i",0) ] ]`
currently lookup function returns first occurrence of variable, i'm having problems 2nd scope (2nd begin).
i'm not quite sure how can make update
, lookup
function return value associated particular scope.
basically have code working 1 begin
statement, having issues 2 or more statements in sample program.
Comments
Post a Comment