tools/pas2c.hs
changeset 6872 0f6eef4a07c8
parent 6860 f4238c683ec7
child 6874 b9e2e509a42d
equal deleted inserted replaced
6871:5aadbfe02613 6872:0f6eef4a07c8
   341 initExpr2C (InitRecord fields) = do
   341 initExpr2C (InitRecord fields) = do
   342     (fs :: [Doc]) <- mapM (\(Identifier a _, b) -> liftM (text "." <> text a <+> equals <+>) $ initExpr2C b) fields
   342     (fs :: [Doc]) <- mapM (\(Identifier a _, b) -> liftM (text "." <> text a <+> equals <+>) $ initExpr2C b) fields
   343     return $ lbrace $+$ (nest 4 . vcat $ fs) $+$ rbrace
   343     return $ lbrace $+$ (nest 4 . vcat $ fs) $+$ rbrace
   344 initExpr2C (InitArray [value]) = initExpr2C value
   344 initExpr2C (InitArray [value]) = initExpr2C value
   345 initExpr2C (InitArray values) = liftM (braces . vcat . punctuate comma) $ mapM initExpr2C values
   345 initExpr2C (InitArray values) = liftM (braces . vcat . punctuate comma) $ mapM initExpr2C values
   346 initExpr2C (InitRange _) = return $ text "<<range expression>>"
   346 initExpr2C (InitRange (Range i)) = id2C IOLookup i
   347 initExpr2C (InitSet _) = return $ text "<<set>>"
   347 initExpr2C (InitRange a) = return $ text "<<range>>"
       
   348 initExpr2C (InitSet []) = return $ text "0"
       
   349 initExpr2C (InitSet a) = return $ text "<<set>>"
   348 initExpr2C (BuiltInFunction {}) = return $ text "<<built-in function>>"
   350 initExpr2C (BuiltInFunction {}) = return $ text "<<built-in function>>"
   349 initExpr2C a = error $ "Don't know how to render " ++ show a
   351 initExpr2C a = error $ "Don't know how to render " ++ show a
   350 
   352 
   351 
   353 
   352 type2C :: TypeDecl -> State RenderState Doc
   354 type2C :: TypeDecl -> State RenderState Doc
   456     e1 <- expr2C expr1
   458     e1 <- expr2C expr1
   457     t1 <- gets lastType
   459     t1 <- gets lastType
   458     e2 <- expr2C expr2
   460     e2 <- expr2C expr2
   459     case (op2C op, t1) of
   461     case (op2C op, t1) of
   460         ("+", BTString) -> expr2C $ BuiltInFunCall [expr1, expr2] (SimpleReference $ Identifier "_strconcat" (BTFunction BTString))
   462         ("+", BTString) -> expr2C $ BuiltInFunCall [expr1, expr2] (SimpleReference $ Identifier "_strconcat" (BTFunction BTString))
   461         --("==", BTString) -> expr2C $ BuiltInFunCall [expr1, expr2] (SimpleReference $ Identifier "_strcompare" (BTFunction BTBool))
   463         ("==", BTString) -> expr2C $ BuiltInFunCall [expr1, expr2] (SimpleReference $ Identifier "_strcompare" (BTFunction BTBool))
   462         --("!=", BTString) -> expr2C $ BuiltInFunCall [expr1, expr2] (SimpleReference $ Identifier "_strncompare" (BTFunction BTBool))
   464         ("!=", BTString) -> expr2C $ BuiltInFunCall [expr1, expr2] (SimpleReference $ Identifier "_strncompare" (BTFunction BTBool))
   463         ("&", BTBool) -> return $ parens $ e1 <+> text "&&" <+> e2
   465         ("&", BTBool) -> return $ parens $ e1 <+> text "&&" <+> e2
   464         ("|", BTBool) -> return $ parens $ e1 <+> text "||" <+> e2
   466         ("|", BTBool) -> return $ parens $ e1 <+> text "||" <+> e2
   465         (o, _) -> return $ parens $ e1 <+> text o <+> e2
   467         (o, _) -> return $ parens $ e1 <+> text o <+> e2
   466 expr2C (NumberLiteral s) = return $ text s
   468 expr2C (NumberLiteral s) = return $ text s
   467 expr2C (FloatLiteral s) = return $ text s
   469 expr2C (FloatLiteral s) = return $ text s
   495     t <- gets lastType
   497     t <- gets lastType
   496     ns <- gets currentScope
   498     ns <- gets currentScope
   497     case t of
   499     case t of
   498          (BTArray _ t') -> modify (\st -> st{lastType = t'})
   500          (BTArray _ t') -> modify (\st -> st{lastType = t'})
   499          (BTString) -> modify (\st -> st{lastType = BTChar})
   501          (BTString) -> modify (\st -> st{lastType = BTChar})
       
   502          (BTPointerTo t) -> do
       
   503                 t'' <- fromPointer (show t) =<< gets lastType
       
   504                 case t'' of
       
   505                      BTChar -> modify (\st -> st{lastType = BTChar})
       
   506                      a -> error $ "Getting element of " ++ show a ++ "\nReference: " ++ show ae ++ "\n" ++ show (take 100 ns)
   500          a -> error $ "Getting element of " ++ show a ++ "\nReference: " ++ show ae ++ "\n" ++ show (take 100 ns)
   507          a -> error $ "Getting element of " ++ show a ++ "\nReference: " ++ show ae ++ "\n" ++ show (take 100 ns)
   501     return $ r <> (brackets . hcat) (punctuate comma es)
   508     return $ r <> (brackets . hcat) (punctuate comma es)
   502 ref2C (SimpleReference name) = id2C IOLookup name
   509 ref2C (SimpleReference name) = id2C IOLookup name
   503 ref2C rf@(RecordField (Dereference ref1) ref2) = do
   510 ref2C rf@(RecordField (Dereference ref1) ref2) = do
   504     r1 <- ref2C ref1 
   511     r1 <- ref2C ref1