BNFC / bnfc

BNF Converter
http://bnfc.digitalgrammars.com/
586 stars 165 forks source link

C: Properly implement defined rules using the empty list. #365

Closed shlevy closed 3 years ago

shlevy commented 3 years ago

Part of #363.

andreasabel commented 3 years ago

Thanks. My suggestion would be to generalize Exp in CF.hs to

type Exp = Exp' String
data Exp' f
  = App f [Exp]       -- ^ (Possibly defined) label applied to expressions.
  | Var String        -- ^ Function parameter.
  | LitInt Integer
  | LitDouble Double
  | LitChar Char
  | LitString String
  deriving (Eq, Functor)

Then replace, in the C backend, [] by Null and (:) by the respective make_ListXXX, using fmap. (Btw. there is isNilFun and isConsFun to check for these special labels.) This is likely still not enough, we need to have the list type XXX. This could be saved in the TypeChecker.hs, producing a Exp' (String,Cat) remembering the target type of the App f es. Spinning this further, the Cat is only needed for list constructors, so maybe Exp' Label would be sufficient where

data Label
  = OrdinaryLabel String
  | NilLabel  Cat    -- ^ []
  | ConsLabel Cat   -- ^ (:)
  | OneLabel  Cat    -- ^ (:[])
shlevy commented 3 years ago

Makes sense, will try to carve out some time!

andreasabel commented 3 years ago

I fixed this problem in a way similar as I wrote in the comment. Mainly, we remember the typing information from the type checker, so we

shlevy commented 3 years ago

Awesome! Sorry this sat for so long, I only have a little time here and there to work on the project using BNFC at the moment. Hopefully will be able to pick up the rust backend soon!

andreasabel commented 3 years ago

Yeah, looking forward to the Rust backend!

If you find any more bugs, do not hesitate to report them! (Trying to understand how things work is the best way to discover bugs...)