Skip to content

Commit

Permalink
[ #266 C++ ] refactor: removed clone of definedRules
Browse files Browse the repository at this point in the history
  • Loading branch information
andreasabel committed Jan 3, 2020
1 parent f0ecff6 commit bbe1f51
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 48 deletions.
50 changes: 2 additions & 48 deletions source/src/BNFC/Backend/CPP/NoSTL/CFtoBison.hs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@

module BNFC.Backend.CPP.NoSTL.CFtoBison (cf2Bison) where

import Data.Char ( toLower, isUpper )
import Data.Char ( toLower )
import Data.List ( intersperse, nub )
import Data.Maybe ( fromMaybe )
import qualified Data.Map as Map
Expand All @@ -51,9 +51,8 @@ import BNFC.CF
import BNFC.Backend.Common.NamedVariables hiding (varName)
import BNFC.Backend.C.CFtoBisonC
( resultName, specialToks, startSymbol, typeName, varName )
import BNFC.Backend.CPP.STL.CFtoBisonSTL ( tokens, union )
import BNFC.Backend.CPP.STL.CFtoBisonSTL ( tokens, union, definedRules )
import BNFC.PrettyPrint
import BNFC.TypeChecker
import BNFC.Utils ( (+++) )

--This follows the basic structure of CFtoHappy.
Expand Down Expand Up @@ -117,51 +116,6 @@ header name cf = unlines
eps = allEntryPoints cf
dats = nub $ map normCat eps

definedRules :: CF -> String
definedRules cf = unlines [ rule f xs e | FunDef f xs e <- cfgPragmas cf]
where
ctx = buildContext cf

list = LC (const "[]") (\ t -> "List" ++ unBase t)
where
unBase (ListT t) = unBase t
unBase (BaseT x) = show$normCat$strToCat x

rule f xs e =
case checkDefinition' list ctx f xs e of
Left err -> error $ "Panic! This should have been caught already:\n" ++ err
Right (args,(e',t)) -> unlines
[ cppType t ++ " " ++ f ++ "_ (" ++
concat (intersperse ", " $ map cppArg args) ++ ") {"
, " return " ++ cppExp e' ++ ";"
, "}"
]
where
cppType :: Base -> String
cppType (ListT (BaseT x)) = "List" ++ show (normCat (strToCat x)) ++ " *"
cppType (ListT t) = cppType t ++ " *"
cppType (BaseT x)
| isToken x ctx = "String"
| otherwise = show (normCat (strToCat x)) ++ " *"

cppArg :: (String, Base) -> String
cppArg (x,t) = cppType t ++ " " ++ x ++ "_"

cppExp :: Exp -> String
cppExp (App "[]" []) = "0"
cppExp (App x [])
| elem x xs = x ++ "_" -- argument
cppExp (App t [e])
| isToken t ctx = cppExp e
cppExp (App x es)
| isUpper (head x) = call ("new " ++ x) es
| otherwise = call (x ++ "_") es
cppExp (LitInt n) = show n
cppExp (LitDouble x) = show x
cppExp (LitChar c) = show c
cppExp (LitString s) = show s

call x es = x ++ "(" ++ concat (intersperse ", " $ map cppExp es) ++ ")"


-- | Generates declaration and initialization of the @YY_RESULT@ for a parser.
Expand Down
1 change: 1 addition & 0 deletions source/src/BNFC/Backend/CPP/STL/CFtoBisonSTL.hs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
module BNFC.Backend.CPP.STL.CFtoBisonSTL
( cf2Bison
, tokens, union
, definedRules
) where

import Prelude'
Expand Down

0 comments on commit bbe1f51

Please sign in to comment.