Skip to content

Commit

Permalink
[ #363 ] C: fixed define with list expressions
Browse files Browse the repository at this point in the history
- NULL instead of make_[]
- make_ListFoo instead of make_(:)
  • Loading branch information
andreasabel committed May 18, 2021
1 parent 7808875 commit 8eccfdc
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
19 changes: 15 additions & 4 deletions source/src/BNFC/Backend/C/CFtoCAbs.hs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import Prelude hiding ((<>))
import Data.Char ( toLower )
import Data.Either ( lefts )
import Data.Function ( on )
import Data.List ( groupBy, intercalate, nub, sort )
import Data.List ( groupBy, intercalate, intersperse, nub, sort )
import Data.Maybe ( mapMaybe )

import BNFC.CF
Expand Down Expand Up @@ -53,6 +53,8 @@ mkHFile rp classes datas cf = unlines $ concat
[ [ "#ifndef ABSYN_HEADER"
, "#define ABSYN_HEADER"
, ""
, "#include <stddef.h> /* NULL */"
, ""
, "/* C++ Abstract Syntax Interface generated by the BNF Converter.*/"
, ""
, prTypeDefs user
Expand All @@ -75,7 +77,7 @@ mkHFile rp classes datas cf = unlines $ concat
[ "/******************** Defined Constructors ***********************/"
, ""
]
, map (prDefH user) definedConstructors
, intersperse "" $ map (prDefH user) definedConstructors

, [ ""
, "#endif"
Expand Down Expand Up @@ -116,7 +118,7 @@ prDefH
-> Define
-> String
prDefH tokenCats (Define fun args e t) =
concat [ "#define make_", f, "(", intercalate "," xs, ") ", prExp e ]
concat [ "#define make_", f, "(", intercalate "," xs, ") \\\n ", prExp e ]
where
f = funName fun
xs = map fst args
Expand All @@ -127,11 +129,20 @@ prDefH tokenCats (Define fun args e t) =
-- Token categories are just @typedef@s in C, so no constructor needed.
App g _ [e] | g `elem` tokenCats
-> prExp e
App g _ es -> concat [ "make_", g, "(", intercalate "," (map prExp es), ")" ]
App "[]" _ [] -> "NULL"
App g t es -> concat [ "make_", con g t, lparen es, intercalate ", " (map prExp es), ")" ]
LitInt i -> show i
LitDouble d -> show d
LitChar c -> show c
LitString s -> concat [ "strdup(", show s, ")" ] -- so that free() does not crash!
con g ~(FunT ts t)
| isConsFun g = identType t
| otherwise = g
-- If more than one argument, or complex argument, put space before opening parenthesis.
lparen = \case
_:_:_ -> " ("
[App _ _ (_:_)] -> " ("
_ -> "("

-- | Prints struct definitions for all categories.
prDataH :: RecordPositions -> Data -> String
Expand Down
4 changes: 4 additions & 0 deletions source/src/BNFC/CF.hs
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,10 @@ identCat :: Cat -> String
identCat (ListCat c) = "List" ++ identCat c
identCat c = catToStr c

identType :: Base -> String
identType (ListT t) = "List" ++ identType t
identType (BaseT s) = s

isList :: Cat -> Bool
isList (ListCat _) = True
isList _ = False
Expand Down

0 comments on commit 8eccfdc

Please sign in to comment.