From 9403f4d68b62de4b936680ce21c9d2927663f4e2 Mon Sep 17 00:00:00 2001 From: Neil Brown Date: Thu, 27 Nov 2008 09:25:34 +0000 Subject: [PATCH] Filled in more expressions and corrected the generation of byte literals by borrowing code from the C backend --- backends/GenerateCHP.hs | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/backends/GenerateCHP.hs b/backends/GenerateCHP.hs index 28207e3..ad747eb 100644 --- a/backends/GenerateCHP.hs +++ b/backends/GenerateCHP.hs @@ -41,13 +41,17 @@ module GenerateCHP where import Control.Monad.State import Control.Monad.Trans +import Data.Char import Data.Generics import Data.List import System.IO +import Text.Printf import qualified AST as A import CompState import Errors +import EvalLiterals +import Metadata import Pass import Utils @@ -186,6 +190,8 @@ genExpression (A.Literal _ t repr) genType t tell [")"] genExpression (A.ExprVariable _ v) = genVariable v +genExpression (A.True _) = tell ["True"] +genExpression (A.False _) = tell ["False"] genExpression e = genMissing' "genExpression" e seqComma :: [CGen ()] -> CGen () @@ -198,9 +204,28 @@ genLiteralRepr (A.ArrayLiteral _ elems) tell ["]"] genLiteralRepr (A.IntLiteral _ str) = tell [str] genLiteralRepr (A.RealLiteral _ str) = tell [str] -genLiteralRepr (A.ByteLiteral _ str) = tell ["\'",str,"\'"] +genLiteralRepr (A.ByteLiteral m str) + = tell ["\'"] >> genByteLiteral m str >> tell ["\'"] genLiteralRepr _ = genMissing "genLiteralRepr" +genByteLiteral :: Meta -> String -> CGen () +genByteLiteral m s + = do c <- evalByte m s + tell [convByte c] + +convByte :: Char -> String +convByte '\'' = "\\'" +convByte '"' = "\\\"" +convByte '\\' = "\\\\" +convByte '\r' = "\\r" +convByte '\n' = "\\n" +convByte '\t' = "\\t" +convByte c + | o == 0 = "\\0" + | (o < 32 || o > 127) = printf "\\%03o" o + | otherwise = [c] + where o = ord c + genArrayElem :: A.ArrayElem -> CGen () genArrayElem (A.ArrayElemExpr e) = genExpression e genArrayElem _ = genMissing "genArrayElem"