Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduced LitValue for expanded literal value handling #3945

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file not shown.
Binary file not shown.
13 changes: 10 additions & 3 deletions code/drasil-gool/lib/Drasil/GOOL/AST.hs
Original file line number Diff line number Diff line change
Expand Up @@ -137,14 +137,21 @@ data TypeData = TD {cType :: CodeType, typeString :: String, typeDoc :: Doc}
td :: CodeType -> String -> Doc -> TypeData
td = TD

-- FIXME: Currently, this is used to hold certain types of literal values.
-- It needs to be extended to support additional types.
data LitValue = LitChar Char
| LitDouble Double
| LitFloat Float
| LitInt Integer
| LitString String
-- Used as the underlying data type for Values in all renderers
-- valPrec is the precedence of the operator involved if the value is an expression,
-- valInt is the int the value is holding if the value is a litInt,
-- litVal is the literal value is holding,
-- valType is the type of the value,
-- val is the printed representation of the value.
data ValData = VD {valPrec :: Maybe Int, valInt :: Maybe Integer, valType :: TypeData, val :: Doc}
data ValData = VD {valPrec :: Maybe Int, litVal :: LitValue, valType :: TypeData, val :: Doc}

vd :: Maybe Int -> Maybe Integer -> TypeData -> Doc -> ValData
vd :: Maybe Int -> LitValue -> TypeData -> Doc -> ValData
vd = VD

updateValDoc :: (Doc -> Doc) -> ValData -> ValData
Expand Down
6 changes: 3 additions & 3 deletions code/drasil-gool/lib/Drasil/GOOL/LanguageRenderer/CLike.hs
Original file line number Diff line number Diff line change
Expand Up @@ -104,14 +104,14 @@ litFalse :: (CommonRenderSym r) => SValue r
litFalse = mkStateVal IC.bool (text "false")

litFloat :: (CommonRenderSym r) => Float -> SValue r
litFloat f = mkStateVal IC.float (D.float f <> text "f")
litFloat f = valFromData Nothing (LitFloat f) IC.float (D.float f <> text "f")

inlineIf :: (CommonRenderSym r) => SValue r -> SValue r -> SValue r -> SValue r
inlineIf c' v1' v2' = do
c <- c'
v1 <- v1'
v2 <- v2'
valFromData (prec c) Nothing (toState $ valueType v1)
v2 <- v2'
valFromData (prec c) (LitInt 0) (toState $ valueType v1)
(RC.value c <+> text "?" <+> RC.value v1 <+> text ":" <+> RC.value v2)
where prec cd = valuePrec cd <|> Just 0

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import Drasil.GOOL.RendererClassesCommon (CommonRenderSym, ImportSym(..),
ImportElim, RenderBody(..), BodyElim, RenderBlock(..),
BlockElim, RenderType(..), InternalTypeElim, UnaryOpSym(..), BinaryOpSym(..),
OpElim(uOpPrec, bOpPrec), RenderVariable(..), InternalVarElim(variableBind),
RenderValue(..), ValueElim(valuePrec, valueInt), InternalListFunc(..),
RenderValue(..), ValueElim(..), InternalListFunc(..),
RenderFunction(..), FunctionElim(functionType), InternalAssignStmt(..),
InternalIOStmt(..), InternalControlStmt(..), RenderStatement(..),
StatementElim(statementTerm), RenderVisibility(..), VisibilityElim, MethodTypeSym(..),
Expand Down Expand Up @@ -413,7 +413,21 @@ instance RenderValue CSharpCode where

instance ValueElim CSharpCode where
valuePrec = valPrec . unCSC
valueInt = valInt . unCSC
valueChar sc = case litVal (unCSC sc) of
LitChar c -> Just c
_ -> Nothing
valueDouble sc = case litVal (unCSC sc) of
LitDouble d -> Just d
_ -> Nothing
valueFloat sc = case litVal (unCSC sc) of
LitFloat f -> Just f
_ -> Nothing
valueInt sc = case litVal (unCSC sc) of
LitInt i -> Just i
_ -> Nothing
valueString sc = case litVal (unCSC sc) of
LitString s -> Just s
_ -> Nothing
value = val . unCSC

instance InternalValueExp CSharpCode where
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import Drasil.GOOL.RendererClassesCommon (CommonRenderSym, VSUnOp, VSBinOp,
RenderValue(..), ValueElim(valuePrec), RenderStatement(..))
import qualified Drasil.GOOL.RendererClassesCommon as RC (uOp, bOp, value)
import Drasil.GOOL.LanguageRenderer (unOpDocD, unOpDocD', binOpDocD, binOpDocD')
import Drasil.GOOL.AST (Terminator(..), Binding(..), OpData, od)
import Drasil.GOOL.AST (Terminator(..), Binding(..), OpData, od, LitValue(..))
import Drasil.GOOL.CodeType (CodeType(..))
import Drasil.GOOL.Helpers (toCode, toState, on2StateValues)
import Drasil.GOOL.State (VS)
Expand All @@ -36,11 +36,11 @@ mkStmtNoEnd = flip stmtFromData Empty

-- | Constructs a value in a stateful context
mkStateVal :: (CommonRenderSym r) => VSType r -> Doc -> SValue r
mkStateVal = valFromData Nothing Nothing
mkStateVal = valFromData Nothing (LitInt 0)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is suspicious to me: why is the static value defaulting to 0? (same in other places in this PR)


-- | Constructs a value in a non-stateful context
mkVal :: (CommonRenderSym r) => r (Type r) -> Doc -> SValue r
mkVal t = valFromData Nothing Nothing (toState t)
mkVal t = valFromData Nothing (LitInt 0) (toState t)

-- Variables --

Expand Down Expand Up @@ -210,7 +210,7 @@ exprRender' f b' v1' v2' = do
toState $ f b v1 v2

mkExpr :: (CommonRenderSym r) => Int -> r (Type r) -> Doc -> SValue r
mkExpr p t = valFromData (Just p) Nothing (toState t)
mkExpr p t = valFromData (Just p) (LitInt 0) (toState t)

binOpDocDRend :: (CommonRenderSym r) => r (BinaryOp r) -> r (Value r) ->
r (Value r) -> Doc
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import Drasil.GOOL.RendererClassesCommon (CommonRenderSym, ImportSym(..),
ImportElim, RenderBody(..), BodyElim, RenderBlock(..), BlockElim,
RenderType(..), InternalTypeElim, UnaryOpSym(..), BinaryOpSym(..),
OpElim(uOpPrec, bOpPrec), RenderVariable(..), InternalVarElim(variableBind),
RenderValue(..), ValueElim(valuePrec, valueInt), InternalListFunc(..),
RenderValue(..), ValueElim(..), InternalListFunc(..),
RenderFunction(..), FunctionElim(functionType), InternalAssignStmt(..),
InternalIOStmt(..), InternalControlStmt(..), RenderStatement(..),
StatementElim(statementTerm), RenderVisibility(..), VisibilityElim, MSMthdType,
Expand Down Expand Up @@ -428,7 +428,11 @@ instance (Pair p) => RenderValue (p CppSrcCode CppHdrCode) where

instance (Pair p) => ValueElim (p CppSrcCode CppHdrCode) where
valuePrec v = valuePrec $ pfst v
valueChar v = valueChar $ pfst v
valueDouble v = valueDouble $ pfst v
valueFloat v = valueFloat $ pfst v
valueInt v = valueInt $ pfst v
valueString v = valueString $ pfst v
value v = RC.value $ pfst v

instance (Pair p) => InternalValueExp (p CppSrcCode CppHdrCode) where
Expand Down Expand Up @@ -1354,7 +1358,21 @@ instance RenderValue CppSrcCode where

instance ValueElim CppSrcCode where
valuePrec = valPrec . unCPPSC
valueInt = valInt . unCPPSC
valueChar sc = case litVal (unCPPSC sc) of
LitChar c -> Just c
_ -> Nothing
valueDouble sc = case litVal (unCPPSC sc) of
LitDouble d -> Just d
_ -> Nothing
valueFloat sc = case litVal (unCPPSC sc) of
LitFloat f -> Just f
_ -> Nothing
valueInt sc = case litVal (unCPPSC sc) of
LitInt i -> Just i
_ -> Nothing
valueString sc = case litVal (unCPPSC sc) of
LitString s -> Just s
_ -> Nothing
value = val . unCPPSC

instance InternalValueExp CppSrcCode where
Expand Down Expand Up @@ -2057,7 +2075,21 @@ instance RenderValue CppHdrCode where

instance ValueElim CppHdrCode where
valuePrec = valPrec . unCPPHC
valueInt = valInt . unCPPHC
valueChar sc = case litVal (unCPPHC sc) of
LitChar c -> Just c
_ -> Nothing
valueDouble sc = case litVal (unCPPHC sc) of
LitDouble d -> Just d
_ -> Nothing
valueFloat sc = case litVal (unCPPHC sc) of
LitFloat f -> Just f
_ -> Nothing
valueInt sc = case litVal (unCPPHC sc) of
LitInt i -> Just i
_ -> Nothing
valueString sc = case litVal (unCPPHC sc) of
LitString s -> Just s
_ -> Nothing
value = val . unCPPHC

instance InternalValueExp CppHdrCode where
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import Drasil.GOOL.RendererClassesCommon (CommonRenderSym, ImportSym(..),
ImportElim, RenderBody(..), BodyElim, RenderBlock(..),
BlockElim, RenderType(..), InternalTypeElim, UnaryOpSym(..), BinaryOpSym(..),
OpElim(uOpPrec, bOpPrec), RenderVariable(..), InternalVarElim(variableBind),
RenderValue(..), ValueElim(valuePrec, valueInt), InternalListFunc(..),
RenderValue(..), ValueElim(..), InternalListFunc(..),
RenderFunction(..), FunctionElim(functionType), InternalAssignStmt(..),
InternalIOStmt(..), InternalControlStmt(..), RenderStatement(..),
StatementElim(statementTerm), RenderVisibility(..), VisibilityElim, MethodTypeSym(..),
Expand Down Expand Up @@ -438,7 +438,21 @@ instance RenderValue JavaCode where

instance ValueElim JavaCode where
valuePrec = valPrec . unJC
valueInt = valInt . unJC
valueChar sc = case litVal (unJC sc) of
LitChar c -> Just c
_ -> Nothing
valueDouble sc = case litVal (unJC sc) of
LitDouble d -> Just d
_ -> Nothing
valueFloat sc = case litVal (unJC sc) of
LitFloat f -> Just f
_ -> Nothing
valueInt sc = case litVal (unJC sc) of
LitInt i -> Just i
_ -> Nothing
valueString sc = case litVal (unJC sc) of
LitString s -> Just s
_ -> Nothing
value = val . unJC

instance InternalValueExp JavaCode where
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,21 @@ instance RenderValue JuliaCode where

instance ValueElim JuliaCode where
valuePrec = valPrec . unJLC
valueInt = valInt . unJLC
valueChar sc = case litVal (unJLC sc) of
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you should be able to abstract out the

case litVal x of
   LitChar c -> Just c
   _  -> Nothing

part in a separate function so that it can be re-used across the different renderers

LitChar c -> Just c
_ -> Nothing
valueDouble sc = case litVal (unJLC sc) of
LitDouble d -> Just d
_ -> Nothing
valueFloat sc = case litVal (unJLC sc) of
LitFloat f -> Just f
_ -> Nothing
valueInt sc = case litVal (unJLC sc) of
LitInt i -> Just i
_ -> Nothing
valueString sc = case litVal (unJLC sc) of
LitString s -> Just s
_ -> Nothing
value = val . unJLC

instance List JuliaCode where
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ import qualified Drasil.GOOL.RendererClassesCommon as S (RenderValue(call),
InternalListFunc (listAddFunc, listAppendFunc, listAccessFunc, listSetFunc),
RenderStatement(stmt), InternalIOStmt(..))
import qualified Drasil.GOOL.RendererClassesCommon as RC (BodyElim(..),
BlockElim(..), InternalVarElim(variable), ValueElim(value, valueInt),
BlockElim(..), InternalVarElim(variable), ValueElim(..),
FunctionElim(..), StatementElim(statement), BlockCommentElim(..))
import Drasil.GOOL.RendererClassesOO (OORenderSym, RenderFile(commentedMod),
OORenderMethod(intMethod), RenderClass(inherit, implements),
Expand Down Expand Up @@ -134,16 +134,26 @@ smartAdd v1 v2 = do
v1' <- v1
v2' <- v2
case (RC.valueInt v1', RC.valueInt v2') of
(Just i1, Just i2) -> litInt (i1 + i2)
_ -> v1 #+ v2
(Just i1,Just i2) -> litInt (i1 + i2)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if smartAdd should be "more typed", so that this cascade of checking doesn't happen.

_ -> case (RC.valueDouble v1', RC.valueDouble v2') of
(Just d1, Just d2) -> litDouble (d1 + d2)
_ -> case (RC.valueFloat v1', RC.valueFloat v2') of
(Just f1, Just f2) -> IC.litFloat (f1 + f2)
_ -> case (RC.valueString v1', RC.valueString v2') of
(Just s1, Just s2) -> litString (s1 ++ s2)
_ -> v1 #+ v2

smartSub :: (CommonRenderSym r) => SValue r -> SValue r -> SValue r
smartSub v1 v2 = do
v1' <- v1
v2' <- v2
case (RC.valueInt v1', RC.valueInt v2') of
(Just i1, Just i2) -> litInt (i1 - i2)
_ -> v1 #- v2
(Just i1,Just i2) -> litInt (i1 - i2)
_ -> case (RC.valueDouble v1', RC.valueDouble v2') of
(Just d1, Just d2) -> litDouble (d1 - d2)
_ -> case (RC.valueFloat v1', RC.valueFloat v2') of
(Just f1, Just f2) -> IC.litFloat (f1 - f2)
_ -> v1 #- v2

equalOp :: (Monad r) => VSOp r
equalOp = compEqualPrec "=="
Expand Down Expand Up @@ -220,16 +230,16 @@ local = toCode $ sd Local
-- Values --

litChar :: (CommonRenderSym r) => (Doc -> Doc) -> Char -> SValue r
litChar f c = mkStateVal IC.char (f $ if c == '\n' then text "\\n" else D.char c)
litChar f c = valFromData Nothing (LitChar c) IC.char (f $ if c == '\n' then text "\\n" else D.char c)

litDouble :: (CommonRenderSym r) => Double -> SValue r
litDouble d = mkStateVal IC.double (D.double d)
litDouble d = valFromData Nothing (LitDouble d) IC.double (D.double d)

litInt :: (CommonRenderSym r) => Integer -> SValue r
litInt i = valFromData Nothing (Just i) IC.int (integer i)
litInt i = valFromData Nothing (LitInt i) IC.int (integer i)

litString :: (CommonRenderSym r) => String -> SValue r
litString s = mkStateVal IC.string (doubleQuotedText s)
litString s = valFromData Nothing (LitString s) IC.string (doubleQuotedText s)

valueOf :: (CommonRenderSym r) => SVariable r -> SValue r
valueOf v' = do
Expand Down Expand Up @@ -278,7 +288,7 @@ lambda f ps' ex' = do
ps <- sequence ps'
ex <- ex'
let ft = IC.funcType (map (return . variableType) ps) (return $ valueType ex)
valFromData (Just 0) Nothing ft (f ps ex)
valFromData (Just 0) (LitInt 0) ft (f ps ex)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here again, why default to 0?


objAccess :: (CommonRenderSym r) => SValue r -> VSFunction r -> SValue r
objAccess = on2StateWrapped (\v f-> mkVal (functionType f)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import Drasil.GOOL.RendererClassesCommon (CommonRenderSym, ImportSym(..),
ImportElim, RenderBody(..), BodyElim, RenderBlock(..),
BlockElim, RenderType(..), InternalTypeElim, UnaryOpSym(..), BinaryOpSym(..),
OpElim(uOpPrec, bOpPrec), RenderVariable(..), InternalVarElim(variableBind),
RenderValue(..), ValueElim(valuePrec, valueInt), InternalListFunc(..),
RenderValue(..), ValueElim(..), InternalListFunc(..),
RenderFunction(..), FunctionElim(functionType), InternalAssignStmt(..),
InternalIOStmt(..), InternalControlStmt(..), RenderStatement(..),
StatementElim(statementTerm), RenderVisibility(..), VisibilityElim,
Expand Down Expand Up @@ -426,7 +426,21 @@ instance RenderValue PythonCode where

instance ValueElim PythonCode where
valuePrec = valPrec . unPC
valueInt = valInt . unPC
valueChar sc = case litVal (unPC sc) of
LitChar c -> Just c
_ -> Nothing
valueDouble sc = case litVal (unPC sc) of
LitDouble d -> Just d
_ -> Nothing
valueFloat sc = case litVal (unPC sc) of
LitFloat f -> Just f
_ -> Nothing
valueInt sc = case litVal (unPC sc) of
LitInt i -> Just i
_ -> Nothing
valueString sc = case litVal (unPC sc) of
LitString s -> Just s
_ -> Nothing
value = val . unPC

instance InternalValueExp PythonCode where
Expand Down Expand Up @@ -932,7 +946,7 @@ pyInlineIf c' v1' v2' = do
c <- c'
v1 <- v1'
v2 <- v2'
valFromData (valuePrec c) (valueInt c) (toState $ valueType v1)
valFromData (valuePrec c) (LitInt 0) (toState $ valueType v1)
(RC.value v1 <+> ifLabel <+> RC.value c <+> elseLabel <+> RC.value v2)

pyLambda :: (CommonRenderSym r) => [r (Variable r)] -> r (Value r) -> Doc
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import Drasil.GOOL.RendererClassesCommon (MSMthdType, CommonRenderSym,
ImportSym(..), ImportElim, RenderBody(..), BodyElim, RenderBlock(..),
BlockElim, RenderType(..), InternalTypeElim, UnaryOpSym(..), BinaryOpSym(..),
OpElim(uOpPrec, bOpPrec), RenderVariable(..), InternalVarElim(variableBind),
RenderValue(..), ValueElim(valuePrec, valueInt), InternalListFunc(..),
RenderValue(..), ValueElim(..), InternalListFunc(..),
RenderFunction(..), FunctionElim(functionType), InternalAssignStmt(..),
InternalIOStmt(..), InternalControlStmt(..), RenderStatement(..),
StatementElim(statementTerm), RenderVisibility(..), VisibilityElim,
Expand Down Expand Up @@ -431,7 +431,21 @@ instance RenderValue SwiftCode where

instance ValueElim SwiftCode where
valuePrec = valPrec . unSC
valueInt = valInt . unSC
valueChar sc = case litVal (unSC sc) of
LitChar c -> Just c
_ -> Nothing
valueDouble sc = case litVal (unSC sc) of
LitDouble d -> Just d
_ -> Nothing
valueFloat sc = case litVal (unSC sc) of
LitFloat f -> Just f
_ -> Nothing
valueInt sc = case litVal (unSC sc) of
LitInt i -> Just i
_ -> Nothing
valueString sc = case litVal (unSC sc) of
LitString s -> Just s
_ -> Nothing
value = val . unSC

instance InternalValueExp SwiftCode where
Expand Down
8 changes: 6 additions & 2 deletions code/drasil-gool/lib/Drasil/GOOL/RendererClassesCommon.hs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import Drasil.GOOL.InterfaceCommon (Label, Library, MSBody, MSBlock, VSFunction,
FuncAppStatement(..), CommentStatement(..), ControlStatement(..),
VisibilitySym(..), ParameterSym(..), MethodSym(..), ScopeSym(..))
import Drasil.GOOL.CodeType (CodeType)
import Drasil.GOOL.AST (Binding, Terminator, VisibilityTag, ScopeData)
import Drasil.GOOL.AST (Binding, Terminator, VisibilityTag, LitValue(..), ScopeData)
import Drasil.GOOL.State (MS, VS)

import Control.Monad.State (State)
Expand Down Expand Up @@ -148,11 +148,15 @@ class RenderValue r where
-- calls.
call :: Maybe Library -> Maybe Doc -> MixedCall r

valFromData :: Maybe Int -> Maybe Integer -> VSType r -> Doc -> SValue r
valFromData :: Maybe Int -> LitValue -> VSType r -> Doc -> SValue r
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The previous type was more precise! Perhaps LitValue is not the best way to go, but instead a synonym for Maybe a would be, where the type could be more precise.


class ValueElim r where
valuePrec :: r (Value r) -> Maybe Int
valueChar :: r (Value r) -> Maybe Char
valueDouble :: r (Value r) -> Maybe Double
valueFloat :: r (Value r) -> Maybe Float
valueInt :: r (Value r) -> Maybe Integer
valueString :: r (Value r) -> Maybe String
value :: r (Value r) -> Doc

class InternalListFunc r where
Expand Down
Loading
Loading