Marist-CMPT331-TOPL / adder

Adder is a small but usable subset of the Python language. It is named for the Blackadder comedy series, much as the Python language is named for Monty Python.
MIT License
0 stars 2 forks source link

Marsh suite #52

Closed IanMarsh1 closed 1 year ago

IanMarsh1 commented 1 year ago

Sorry about that last push did it by accident. Also kind of shooting from the hip with this. I understand that I am trying to return [Statement], and stmtList is just a Statement. I think that adding block before it makes stmtList return a list, but I am still a little confused.

Tientuine commented 1 year ago

Good question, let's break it down a little at a time:

Here's a simple, but kind of analogous, Haskell example showing how to get the data encapsulated in one type so that we can pass it along. In this example, the IO type is taking the place of IParser.

-- Imagine a data type that can encapsulate different kinds of values, includes itself
data Result = AResult Int | ManyResults [Result]

-- It can sometimes be helpful to have a function that can extract an encapsulated value
getResults :: Result -> [Result]; getResults r = case r of (ManyResults rs) -> rs; _ -> [r]

-- Now suppose we have a way of generating an instance of our data type
readResults :: IO Result
readResults = (ManyResults . map (AResult . read) . words) <$> getLine

-- But perhaps we also need a way to generate something like what's encapsulated in our type
-- One way we can do so is to extract the encapsulated data using our helper function
readResultList :: IO [Result]
readResultList = getResults <$> readResults

In the example above, the Result type is kind of like our Statement type, the readResults is a bit likestmtListandreadResultListplays the role ofsuite` (or at least the part of that function where you are trying to parse a statement list).

IanMarsh1 commented 1 year ago

A bit lost in the sauce. Can you tell me if I am on the right path or not?