tools/pas2c.hs
changeset 7069 bcf9d8e64e92
parent 7067 f98ec3aecf4e
child 7072 159616c24bb8
equal deleted inserted replaced
7068:b1b7eb9c8cc9 7069:bcf9d8e64e92
   125     --writeFile "pas2c.log" $ unlines . map (\t -> show (fst t) ++ "\n" ++ (unlines . map ((:) '\t' . show) . snd $ t)) . Map.toList $ nss
   125     --writeFile "pas2c.log" $ unlines . map (\t -> show (fst t) ++ "\n" ++ (unlines . map ((:) '\t' . show) . snd $ t)) . Map.toList $ nss
   126     mapM_ (toCFiles nss) u
   126     mapM_ (toCFiles nss) u
   127     where
   127     where
   128     toNamespace :: Map.Map String Records -> PascalUnit -> Records
   128     toNamespace :: Map.Map String Records -> PascalUnit -> Records
   129     toNamespace nss (System tvs) = 
   129     toNamespace nss (System tvs) = 
   130         currentScope $ execState (mapM_ (tvar2C True) tvs) (emptyState nss)
   130         currentScope $ execState f (emptyState nss)
       
   131         where
       
   132         f = do
       
   133             checkDuplicateFunDecls tvs
       
   134             mapM_ (tvar2C True) tvs                
   131     toNamespace _ (Program {}) = Map.empty
   135     toNamespace _ (Program {}) = Map.empty
   132     toNamespace nss (Unit (Identifier i _) interface _ _ _) = 
   136     toNamespace nss (Unit (Identifier i _) interface _ _ _) = 
   133         currentScope $ execState (interface2C interface) (emptyState nss){currentUnit = map toLower i ++ "_"}
   137         currentScope $ execState (interface2C interface) (emptyState nss){currentUnit = map toLower i ++ "_"}
   134 
   138 
   135 
   139 
   206     r <- renderStringConsts
   210     r <- renderStringConsts
   207     return (u $+$ r $+$ tv)
   211     return (u $+$ r $+$ tv)
   208 
   212 
   209 checkDuplicateFunDecls :: [TypeVarDeclaration] -> State RenderState ()
   213 checkDuplicateFunDecls :: [TypeVarDeclaration] -> State RenderState ()
   210 checkDuplicateFunDecls tvs =
   214 checkDuplicateFunDecls tvs =
   211     modify $ \s -> s{toMangle = Map.keysSet . Map.filter (> 1) . foldr ins Map.empty $ tvs}
   215     modify $ \s -> s{toMangle = Map.keysSet . Map.filter (> 1) . foldr ins initMap $ tvs}
   212     where
   216     where
       
   217         initMap = Map.empty
       
   218         --initMap = Map.fromList [("reset", 2)]
   213         ins (FunctionDeclaration (Identifier i _) _ _ _) m = Map.insertWith (+) (map toLower i) 1 m
   219         ins (FunctionDeclaration (Identifier i _) _ _ _) m = Map.insertWith (+) (map toLower i) 1 m
   214         ins _ m = m
   220         ins _ m = m
   215 
   221 
   216 typesAndVars2C :: Bool -> TypesAndVars -> State RenderState Doc
   222 typesAndVars2C :: Bool -> TypesAndVars -> State RenderState Doc
   217 typesAndVars2C b (TypesAndVars ts) = do
   223 typesAndVars2C b (TypesAndVars ts) = do
   764 expr2C (BuiltInFunCall [e] (SimpleReference (Identifier "succ" _))) = liftM (<> text " + 1") $ expr2C e
   770 expr2C (BuiltInFunCall [e] (SimpleReference (Identifier "succ" _))) = liftM (<> text " + 1") $ expr2C e
   765 expr2C (BuiltInFunCall [e] (SimpleReference (Identifier "pred" _))) = liftM (<> text " - 1") $ expr2C e
   771 expr2C (BuiltInFunCall [e] (SimpleReference (Identifier "pred" _))) = liftM (<> text " - 1") $ expr2C e
   766 expr2C (BuiltInFunCall [e] (SimpleReference (Identifier "length" _))) = do
   772 expr2C (BuiltInFunCall [e] (SimpleReference (Identifier "length" _))) = do
   767     e' <- expr2C e
   773     e' <- expr2C e
   768     lt <- gets lastType
   774     lt <- gets lastType
       
   775     modify (\s -> s{lastType = BTInt})
   769     case lt of
   776     case lt of
   770          BTString -> return $ text "length" <> parens e'
   777          BTString -> return $ text "Length" <> parens e'
   771          BTArray {} -> return $ text "length_ar" <> parens e'
   778          BTArray {} -> return $ text "length_ar" <> parens e'
   772          _ -> error $ "length() called on " ++ show lt
   779          _ -> error $ "length() called on " ++ show lt
   773 expr2C (BuiltInFunCall params ref) = do
   780 expr2C (BuiltInFunCall params ref) = do
   774     r <- ref2C ref 
   781     r <- ref2C ref 
   775     t <- gets lastType
   782     t <- gets lastType