The sixth installment

  • Task1 : Implement { and } discussed in class.

  • Task2 : Implement an array marker, ‘(‘

What the array marker does is that it allows us to create active arrays. You start by pushing the opening array marker ‘(‘ on the stack, and do all the stuff, then, when you push the ending array marker ‘)’, it collects every thing in the stack and makes that into an array.

for example

ghci> eval "1 2 ( 3 4 5 dup + swap )"
1 2 [3 10 4]

ghci> eval "1 2 ( 3 4 5 + + )"
1 2 [12]

Hint. The way to do it is almost exactly like ‘{‘ is implemented, except that we are using the stack rather than env.

Implementing scope

bigStep env (W "{":xs) ys = bigStep (("{",[]):env) xs ys
bigStep env (W "}":xs) ys = bigStep myenv xs ys
  where myenv = tail $ dropWhile (/= ("{", [])) env

bigStep env (W "(":xs) ys = bigStep env xs (W "(":ys)
bigStep env (W ")":xs) ys = bigStep env xs (Nested (reverse arr): (tail st))
  where (arr,st) = span (/= W "(") ys

If not built in, look up in the environment.

bigStep env (W x :xs) ys = bigStep env (def ++ xs) ys
  where def = case lookup x env of
          Nothing -> error ("Definition not found or is not applicable for word  {" ++ x ++ "} with stack " ++ (showStack ys))  
          Just x -> x