Skip to content

Allow symbols in data contructors#2384

Merged
paf31 merged 4 commits intopurescript:masterfrom
brandonhamilton:data-constructors
Oct 12, 2016
Merged

Allow symbols in data contructors#2384
paf31 merged 4 commits intopurescript:masterfrom
brandonhamilton:data-constructors

Conversation

@brandonhamilton
Copy link
Copy Markdown
Contributor

I would like to attempt to contribute a solution to resolve #2208

As I am new to the Purescript compiler, I am not sure if there is anything that I have overlooked here or if there is perhaps a better way to achieve this.

In essence, the changes I have made are:

  • Modify the Lexer to accept the same range of tokens for data constructors as those for type constructors.
  • Modify the Codegen to pass data constructor strings through the logic for generating appropriate javascript names from identifiers.
  • Added an example to use as a test case.


data Baz'' = Baz''

main = log "Done"
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Could you please add an example where you case on one of these new constructors? Thanks.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks, this has caught the case where the parser for the constructor binding in case statements did not deal with the symbols. I have updated the parser and example.

return $ JSApp Nothing (JSFunction Nothing Nothing [] (JSBlock Nothing (ds' ++ [JSReturn Nothing ret]))) []
valueToJs' (Constructor (_, _, _, Just IsNewtype) _ (ProperName ctor) _) =
return $ JSVariableIntroduction Nothing ctor (Just $
return $ JSVariableIntroduction Nothing (identToJs . Ident $ ctor) (Just $
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

It's probably worth pulling this out into its own small function.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

properNameToJs?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Great, I have created a function for this

@paf31
Copy link
Copy Markdown
Contributor

paf31 commented Oct 11, 2016

Looks great so far, thanks!

properToJs :: String -> String
properToJs name
| nameIsJsReserved name || nameIsJsBuiltIn name = "$$" ++ name
| otherwise = concatMap identCharToString name
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Might be worth refactoring identToJs to use this in the (Ident name) case - as the implementation is the same.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks !

g Baz'' = 0
g Baz' = 1

h :: Bar' -> Int
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Could you please make sure these cases actually get run? You could do this by adding additional top-level declarations of type Int for example, where you apply each function.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the feedback. Would this do ?

f' ∷ Boolean
f' = f $ Foo' $ Bar' 0

g' ∷ Int
g' = g Baz''

h' ∷ Int
h' = h $ Bar' 4

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Yes, thanks!

@paf31 paf31 merged commit a70b5dd into purescript:master Oct 12, 2016
@paf31
Copy link
Copy Markdown
Contributor

paf31 commented Oct 12, 2016

Thanks!

@brandonhamilton brandonhamilton deleted the data-constructors branch October 12, 2016 07:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Allow prime symbols in data constructors

3 participants