Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add list literal syntax #119

Merged
merged 23 commits into from
Dec 9, 2022
Merged

Add list literal syntax #119

merged 23 commits into from
Dec 9, 2022

Conversation

max-acebal
Copy link
Contributor

No description provided.

@j-hui j-hui changed the title Remove blink3 Add list literal syntax Oct 17, 2022
@j-hui j-hui mentioned this pull request Oct 17, 2022
@max-acebal
Copy link
Contributor Author

max-acebal commented Oct 21, 2022

Hi @EmilySillars I am still getting this error:
stack exec sslc -- tests/lists_sample.ssl > out/lists_sample.c sslc: src/IR/LowerAst.hs:(119,1)-(164,63): Non-exhaustive patterns in function lowerExpr
when trying to run my sample test. As we discussed last week I have been reading through the IR to get an idea of how App, and DConId work. I think my next steps should be adding the code for the ListExpr so that it can be generated by the IR in the way we have been discussing on slack, but unsure where that code should even go since the error is in LowerAst.

@EmilySillars
Copy link
Contributor

Hi Max, Thank you for your comment and apologies on the late reply --
Restating compiler pipeline here for context:
Scanner --> Parser --> Lowering --> Type Inference --> Optimization --> Code Generation --> C code

Lowering Stage:
The ouput of the parser gives us a representation of our input sslang program as a tree.
Nodes in the tree represents parts of the input program.
The "lowering" pass implemented in LowerAst.hs transforms every AST node into an equivalient IR node.
After performing lowering, we are still representing our input sslang program as a tree in the compiler.
The difference is that after lowering, there are fewer kinds of nodes in our tree,
and each node is augmented with space for a type, t.

// an AST Expr Node     ----- LowerAst.hs ---->     // an IR Expr Node

data Expr                                            data Expr t              
  = Id Identifier                                      = Var VarId t
  | Lit Literal                                        | Data DConId t
  | Apply Expr Expr                                    | Lit Literal t
  | Lambda [Pat] Expr                                  | App (Expr t) (Expr t) t
  | OpRegion Expr OpRegion                             | Let [(Binder, Expr t)] (Expr t) t
  | NoExpr                                             | Lambda Binder (Expr t) t
  | Let [Definition] Expr                              | Match (Expr t) [(Alt, Expr t)] t
  | While Expr Expr                                    | Prim Primitive [Expr t] t
  | Loop Expr                                          deriving (Eq, Show, Typeable, Data, Functor, Foldable, Traversable)
  | Par [Expr]
  | IfElse Expr Expr Expr
  | After Expr Expr Expr
  | Assign Expr Expr
  | Constraint Expr TypAnn
  | Wait [Expr]
  | Seq Expr Expr
  | Break
  | Match Expr [(Pat, Expr)]
  | CQuote String
  | CCall Identifier [Expr]
  | ListExpr [Expr]
  deriving (Eq, Show)

The function lowerExpr inside lowerAst.hs has the type signature
lowerExpr :: A.Expr -> Compiler.Pass (I.Expr I.Annotations)
which means it transforms something of type A.Expr to into a monadic I.Expr.
Compiler.Pass is the monad that wraps most all our compiler computations on/transformations
of our representation of a sslang prorgam.

Current Compiler Error:
You are getting a non-exhaustive pattern match warning because the function lowerExpr
does not have a case for matching on an A.Expr of instance ListExpr.

You need to add a case to LowerExpr that transforms a ListExpr instance of A.Expr into an App instance of I.Expr.

ListExpr [Expr] ---- needs to turn into ----> App (Expr t) (Expr t) t

How to do this???

Here is an exercise to get you started thinking about this.
Write down your answers and comment them on this draft PR before our next meeting.

Also comment any questions you have (or slack message me, whichever you prefer) if you get stuck!

Exercise:

  type List
    Cons Int 
    Nil

  main cin cout = 
   let y = (Cons 1 (Cons 2 (Cons 3 Nil)))
   let x = [1, 2, 3]
   ()
  1. What does y look like as an AST node?

  2. What does x look like as an AST node?

  3. What should y look like as an IR node?

  4. What should x look like as an IR node?

I'm writing up a worked example + solution for a similar exercise, and will post that here as soon as I'm finished with it.

@EmilySillars
Copy link
Contributor

EmilySillars commented Oct 27, 2022

@max-acebal max-acebal marked this pull request as ready for review October 31, 2022 21:24
@EmilySillars
Copy link
Contributor

Hi @j-hui ,

  • The changes in Max's PR here add a ListExpr node to the AST.
  • For the cases of ListExpr in Desugar.hs and Anomaly.hs, should its elements be recursed on, or ignored? My gut says for each function that takes a ListExpr, that function should be called on each of the ListExpr's elements. Is this correct? If it is, I think a one line change needs to be made in Anomaly.hs.
  • Any other changes you think need to be made before we merge?

@EmilySillars
Copy link
Contributor

EmilySillars commented Nov 1, 2022

I believe Max's lists_sample2 test case is failing because of a bug on main #125

@EmilySillars
Copy link
Contributor

Hi Max,
Great work so far! We're in the home stretch!

  1. merge main into your branch
  2. once your branch is up to date with main, replace this line in front.hs with
    let astTuple = insertTypeDef pairDef astL to make sure the string and list desugaring gets threaded through.
  3. replace lines 19 and 20 desugarLists.hs with
    desugarDef (DefFn v bs t e) = DefFn v bs t $ everywhere (mkT desugarExpr) e
    desugarDef (DefPat b e    ) = DefPat b $ everywhere (mkT desugarExpr) e

You will likely need to import Data.Generics for this to work. This change allows you to bypass matching on all the different instance of an Expr when you only care about the ListExpr instance - it uses generics from a haskell library called SYBP.

  1. At this point your lists_sample.ssl test should pass, but doesn't output anything meaningful. Change this test case to print out the contents of your list so that it's outputting something meaningful (instead of nothing). Change the corresponding lists_sample.out file to match your new output. Don't worry about the unescaped string error from lists_sample2.ssl. Delete that test or change it so it doesn't use character literals.
  2. resolve all your build warnings/ redundant pattern matches etc.

After you complete these steps I will take a final pass over your code for style/ other nits.

Copy link
Contributor

@EmilySillars EmilySillars left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great Work, Max!

  • I added some final nits.
    EDITED:
    In the interest of time, just fix the nits, don't worry about adding additional test cases!

    - Could you also add two more list test cases, one that is for list of lists, and one that is for lists of tuples?
    Something like printing out x, where x is [[57,56,55],[54]]
    and printing out y, where y is [(57,57),(52,53)]?
    You will need to change the definition of List from being integer specific to being polymorphic:
type List a
  Cons a (List a)
  Nil

For printing the lists of lists, I would add another function

 puts2DList cout lst = ...

and for printing a list of tuples, I would add two functions,

putsTuple cout tup = ...      // prints out a tuple

putsListOfTuples cout lst = ...
// ^ where this func calls putsTuple for a single elt,
// ^ and calls putsListOfTuples (recurses) on rest of list

Adding these two cases shouldn't take too long. Message me if you get stuck.

regression-tests/tests/lists_sample.out Outdated Show resolved Hide resolved
regression-tests/tests/lists_sample2.ssl Show resolved Hide resolved
src/Common/Identifiers.hs Outdated Show resolved Hide resolved
src/Front/DesugarLists.hs Outdated Show resolved Hide resolved
src/Front/DesugarLists.hs Outdated Show resolved Hide resolved
src/Front/DesugarLists.hs Outdated Show resolved Hide resolved
src/Front/DesugarLists.hs Outdated Show resolved Hide resolved
src/Front/Pattern/Anomaly.hs Outdated Show resolved Hide resolved
src/Front/Pattern/Desugar.hs Outdated Show resolved Hide resolved
src/Front/Parser.y Outdated Show resolved Hide resolved
helper [] = (Id nil)
helper (h:t) = Apply (Apply (Id cons) h) (helper t)
desugarExpr e = e
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add newline!

Copy link
Contributor

@EmilySillars EmilySillars left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@max-acebal max-acebal merged commit 4d018c1 into main Dec 9, 2022
@max-acebal max-acebal deleted the Remove_Blink3 branch December 9, 2022 19:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants