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

Made constDecDef capitalize constants in Python #3858

Merged
merged 10 commits into from
Jul 24, 2024
18 changes: 9 additions & 9 deletions code/drasil-code/lib/Language/Drasil/Code/Imperative/Import.hs
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,14 @@ import qualified Language.Drasil.Mod as M (Class(..))
import GOOL.Drasil (Label, SFile, MSBody, MSBlock, VSType, SVariable, SValue,
MSStatement, MSParameter, SMethod, CSStateVar, SClass, NamedArgs,
Initializers, OOProg, PermanenceSym(..), bodyStatements, BlockSym(..),
TypeSym(..), VariableSym(..), OOVariableSym(..), VariableElim(..), ($->), ValueSym(..),
Literal(..), VariableValue(..), NumericExpression(..), BooleanExpression(..),
Comparison(..), ValueExpression(..), OOValueExpression(..),
objMethodCallMixedArgs, List(..), StatementSym(..), AssignStatement(..),
DeclStatement(..), IOStatement(..), StringStatement(..), ControlStatement(..),
ifNoElse, ScopeSym(..), ParameterSym(..), MethodSym(..), OOMethodSym(..),
pubDVar, privDVar, nonInitConstructor, convTypeOO, ScopeTag(..), CodeType(..),
onStateValue)
TypeSym(..), VariableSym(..), OOVariableSym(..), staticConst,
VariableElim(..), ($->), ValueSym(..), Literal(..), VariableValue(..),
NumericExpression(..), BooleanExpression(..), Comparison(..),
ValueExpression(..), OOValueExpression(..), objMethodCallMixedArgs, List(..),
StatementSym(..), AssignStatement(..), DeclStatement(..), IOStatement(..),
StringStatement(..), ControlStatement(..), ifNoElse, ScopeSym(..),
ParameterSym(..), MethodSym(..), OOMethodSym(..), pubDVar, privDVar,
nonInitConstructor, convTypeOO, ScopeTag(..), CodeType(..), onStateValue)
import qualified GOOL.Drasil as C (CodeType(List, Array))

import Prelude hiding (sin, cos, tan, log, exp)
Expand Down Expand Up @@ -100,7 +100,7 @@ variable s t = do
g <- get
let cs = codeSpec g
defFunc Var = var
defFunc Const = staticVar
defFunc Const = staticConst
if s `elem` map codeName (inputs cs)
then inputVariable (inStruct g) Var (var s t)
else if s `elem` map codeName (constants $ codeSpec g)
Expand Down
1 change: 1 addition & 0 deletions code/drasil-code/test/HelloWorld.hs
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ helloIfBody = addComments "If body" (body [
objDecDef (var "myObj" char) (litChar 'o'),
constDecDef (constant "myConst" string) (litString "Imconstant"),

printLn (valueOf $ constant "myConst" string),
printLn (valueOf $ var "a" int),
printLn (valueOf $ var "b" int),
printLn (valueOf $ var "c" int),
Expand Down
43 changes: 22 additions & 21 deletions code/drasil-gool/lib/GOOL/Drasil.hs
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,23 @@ module GOOL.Drasil (Label, GSProgram, SFile, MSBody, MSBlock, VSType,
extObjDecNewNoParams, IOStatement(..), StringStatement(..),
FuncAppStatement(..), OOFuncAppStatement(..), CommentStatement(..),
initObserverList, addObserver, ControlStatement(..), ifNoElse, switchAsIf,
VariableSym(..), OOVariableSym(..), VariableElim(..), ($->), listOf, listVar,
ValueSym(..), Argument(..), Literal(..), MathConstant(..), VariableValue(..),
OOVariableValue, CommandLineArgs(..), NumericExpression(..),
BooleanExpression(..), Comparison(..), ValueExpression(..),
OOValueExpression(..), funcApp, funcAppNamedArgs, selfFuncApp, extFuncApp,
libFuncApp, newObj, extNewObj, libNewObj, exists, objMethodCall,
objMethodCallNamedArgs, objMethodCallMixedArgs, objMethodCallNoParams,
FunctionSym(..), ($.), selfAccess, GetSet(..), List(..), listSlice,
listIndexExists, at, ObserverPattern(..), StrategyPattern(..), ScopeSym(..),
ParameterSym(..), MethodSym(..), OOMethodSym(..), privMethod, pubMethod,
initializer, nonInitConstructor, StateVarSym(..), privDVar, pubDVar, pubSVar,
ClassSym(..), ModuleSym(..), convType, convTypeOO, ProgData(..), FileData(..),
ModData(..), ScopeTag(..), CodeType(..), GOOLState(..), lensMStoVS, headers,
sources, mainMod, initialState, onStateValue, onCodeList, unCI, unPC, unJC,
unCSC, unCPPC, unSC, pyName, pyVersion, jName, jVersion, csName, csVersion,
cppName, cppVersion, swiftName, swiftVersion
VariableSym(..), OOVariableSym(..), staticVar, staticConst, VariableElim(..),
($->), listOf, listVar, ValueSym(..), Argument(..), Literal(..),
MathConstant(..), VariableValue(..), OOVariableValue, CommandLineArgs(..),
NumericExpression(..), BooleanExpression(..), Comparison(..),
ValueExpression(..), OOValueExpression(..), funcApp, funcAppNamedArgs,
selfFuncApp, extFuncApp, libFuncApp, newObj, extNewObj, libNewObj, exists,
objMethodCall, objMethodCallNamedArgs, objMethodCallMixedArgs,
objMethodCallNoParams, FunctionSym(..), ($.), selfAccess, GetSet(..),
List(..), listSlice, listIndexExists, at, ObserverPattern(..),
StrategyPattern(..), ScopeSym(..), ParameterSym(..), MethodSym(..),
OOMethodSym(..), privMethod, pubMethod, initializer, nonInitConstructor,
StateVarSym(..), privDVar, pubDVar, pubSVar, ClassSym(..), ModuleSym(..),
convType, convTypeOO, ProgData(..), FileData(..), ModData(..), ScopeTag(..),
CodeType(..), GOOLState(..), lensMStoVS, headers, sources, mainMod,
initialState, onStateValue, onCodeList, unCI, unPC, unJC, unCSC, unCPPC, unSC,
pyName, pyVersion, jName, jVersion, csName, csVersion, cppName, cppVersion,
swiftName, swiftVersion
) where

import GOOL.Drasil.InterfaceCommon (Label, MSBody, MSBlock, VSType, SVariable,
Expand All @@ -43,11 +44,11 @@ import GOOL.Drasil.InterfaceCommon (Label, MSBody, MSBlock, VSType, SVariable,
listIndexExists, at, ScopeSym(..), ParameterSym(..), MethodSym(..), convType)
import GOOL.Drasil.InterfaceGOOL (GSProgram, SFile, FSModule, SClass,
CSStateVar, VSFunction, OOProg, ProgramSym(..), FileSym(..), ModuleSym(..),
ClassSym(..), OOMethodSym(..), OOTypeSym(..), OOVariableSym(..), ($->),
PermanenceSym(..), privMethod, pubMethod, initializer, nonInitConstructor,
StateVarSym(..), privDVar, pubDVar, pubSVar, OOVariableValue,
OOValueExpression(..), selfFuncApp, newObj, extNewObj, libNewObj,
OODeclStatement(..), objDecNewNoParams, extObjDecNewNoParams,
ClassSym(..), OOMethodSym(..), OOTypeSym(..), OOVariableSym(..), staticVar,
staticConst, ($->), PermanenceSym(..), privMethod, pubMethod, initializer,
nonInitConstructor, StateVarSym(..), privDVar, pubDVar, pubSVar,
OOVariableValue, OOValueExpression(..), selfFuncApp, newObj, extNewObj,
libNewObj, OODeclStatement(..), objDecNewNoParams, extObjDecNewNoParams,
OOFuncAppStatement(..), GetSet(..), objMethodCall, objMethodCallNamedArgs,
objMethodCallMixedArgs, objMethodCallNoParams, FunctionSym(..), ($.),
selfAccess, ObserverPattern(..), initObserverList, addObserver,
Expand Down
2 changes: 1 addition & 1 deletion code/drasil-gool/lib/GOOL/Drasil/CodeInfo.hs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ instance VariableSym CodeInfo where
arrayElem _ _ = noInfo

instance OOVariableSym CodeInfo where
staticVar _ _ = noInfo
staticVar' _ _ _ = noInfo
self = noInfo
classVar _ _ = noInfo
extClassVar _ _ = noInfo
Expand Down
15 changes: 11 additions & 4 deletions code/drasil-gool/lib/GOOL/Drasil/InterfaceGOOL.hs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ module GOOL.Drasil.InterfaceGOOL (
GSProgram, SFile, FSModule, SClass, VSFunction, CSStateVar,
-- Typeclasses
OOProg, ProgramSym(..), FileSym(..), ModuleSym(..), ClassSym(..),
OOTypeSym(..), OOVariableSym(..), ($->), OOValueSym, OOVariableValue,
OOValueExpression(..), selfFuncApp, newObj, extNewObj, libNewObj,
OODeclStatement(..), objDecNewNoParams, extObjDecNewNoParams,
OOTypeSym(..), OOVariableSym(..), staticVar, staticConst, ($->), OOValueSym,
OOVariableValue, OOValueExpression(..), selfFuncApp, newObj, extNewObj,
libNewObj, OODeclStatement(..), objDecNewNoParams, extObjDecNewNoParams,
OOFuncAppStatement(..), GetSet(..), InternalValueExp(..), objMethodCall,
objMethodCallNamedArgs, objMethodCallMixedArgs, objMethodCallNoParams,
OOMethodSym(..), privMethod, pubMethod, initializer, nonInitConstructor,
Expand Down Expand Up @@ -130,13 +130,20 @@ class (TypeSym r) => OOTypeSym r where
class (ValueSym r, OOTypeSym r) => OOValueSym r

class (VariableSym r, OOTypeSym r) => OOVariableSym r where
staticVar :: Label -> VSType r -> SVariable r
-- Bool: False for variable, True for constant. Required by the Python renderer.
staticVar' :: Bool -> Label -> VSType r -> SVariable r
self :: SVariable r
classVar :: VSType r -> SVariable r -> SVariable r
extClassVar :: VSType r -> SVariable r -> SVariable r
objVar :: SVariable r -> SVariable r -> SVariable r
objVarSelf :: SVariable r -> SVariable r

staticVar :: (OOVariableSym r) => Label -> VSType r -> SVariable r
staticVar = staticVar' False

staticConst :: (OOVariableSym r) => Label -> VSType r -> SVariable r
staticConst = staticVar' True

($->) :: (OOVariableSym r) => SVariable r -> SVariable r -> SVariable r
infixl 9 $->
($->) = objVar
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ instance VariableSym CSharpCode where
arrayElem i = G.arrayElem (litInt i)

instance OOVariableSym CSharpCode where
staticVar = G.staticVar
staticVar' _ = G.staticVar
self = C.self
classVar = CP.classVar R.classVar
extClassVar = classVar
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ instance (Pair p) => VariableSym (p CppSrcCode CppHdrCode) where
arrayElem i = pair1 (arrayElem i) (arrayElem i)

instance (Pair p) => OOVariableSym (p CppSrcCode CppHdrCode) where
staticVar n = pair1 (staticVar n) (staticVar n)
staticVar' c n = pair1 (staticVar' c n) (staticVar' c n)
self = on2StateValues pair self self
classVar = pair2 classVar classVar
extClassVar = pair2 extClassVar extClassVar
Expand Down Expand Up @@ -1163,7 +1163,7 @@ instance VariableSym CppSrcCode where
arrayElem i = G.arrayElem (litInt i)

instance OOVariableSym CppSrcCode where
staticVar = G.staticVar
staticVar' _ = G.staticVar
self = C.self
classVar c' v'= do
c <- c'
Expand Down Expand Up @@ -1862,7 +1862,7 @@ instance VariableSym CppHdrCode where
arrayElem _ _ = mkStateVar "" void empty

instance OOVariableSym CppHdrCode where
staticVar = G.staticVar
staticVar' _ = G.staticVar
self = mkStateVar "" void empty
classVar _ _ = mkStateVar "" void empty
extClassVar _ _ = mkStateVar "" void empty
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ instance VariableSym JavaCode where
arrayElem i = G.arrayElem (litInt i)

instance OOVariableSym JavaCode where
staticVar = G.staticVar
staticVar' _ = G.staticVar
self = C.self
classVar = CP.classVar R.classVar
extClassVar = classVar
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@ import GOOL.Drasil.LanguageRenderer (classDec, dot, ifLabel, elseLabel,
import qualified GOOL.Drasil.LanguageRenderer as R (sqrt, fabs, log10,
log, exp, sin, cos, tan, asin, acos, atan, floor, ceil, multiStmt, body,
classVar, listSetFunc, castObj, dynamic, break, continue, addComments,
commentedMod, commentedItem)
commentedMod, commentedItem, var)
import GOOL.Drasil.LanguageRenderer.Constructors (mkStmtNoEnd, mkStateVal,
mkVal, mkStateVar, VSOp, unOpPrec, powerPrec, multPrec, andPrec, orPrec,
unExpr, unExpr', typeUnExpr, binExpr, typeBinExpr)
unExpr, unExpr', typeUnExpr, binExpr, typeBinExpr, mkStaticVar)
import qualified GOOL.Drasil.LanguageRenderer.LanguagePolymorphic as G (
multiBody, block, multiBlock, listInnerType, obj, negateOp, csc, sec, cot,
equalOp, notEqualOp, greaterOp, greaterEqualOp, lessOp, lessEqualOp, plusOp,
Expand Down Expand Up @@ -82,11 +82,11 @@ import GOOL.Drasil.AST (Terminator(..), FileType(..), FileData(..), fileD,
vectorize2, sumComponents, commonVecIndex, commonThunkElim, commonThunkDim)
import GOOL.Drasil.Helpers (vibcat, emptyIfEmpty, toCode, toState, onCodeValue,
onStateValue, on2CodeValues, on2StateValues, onCodeList, onStateList, on2StateWrapped)
import GOOL.Drasil.State (MS, VS, lensGStoFS, lensMStoVS, lensVStoMS,
revFiles, addLangImportVS, getLangImports, addLibImportVS,
getLibImports, addModuleImport, addModuleImportVS, getModuleImports,
import GOOL.Drasil.State (MS, VS, lensGStoFS, lensMStoVS, lensVStoMS,
revFiles, addLangImportVS, getLangImports, addLibImportVS,
getLibImports, addModuleImport, addModuleImportVS, getModuleImports,
setFileType, getClassName, setCurrMain, getClassMap, getMainDoc, useVarName,
genLoopIndex)
genLoopIndex, varNameAvailable)

import Prelude hiding (break,print,sin,cos,tan,floor,(<>))
import Data.Maybe (fromMaybe)
Expand All @@ -95,6 +95,7 @@ import Control.Lens.Zoom (zoom)
import Control.Monad (join)
import Control.Monad.State (modify)
import Data.List (intercalate, sort)
import Data.Char (toUpper)
import qualified Data.Map as Map (lookup)
import Text.PrettyPrint.HughesPJ (Doc, text, (<>), (<+>), parens, empty, equals,
vcat, colon, brackets, isEmpty, quotes)
Expand Down Expand Up @@ -256,12 +257,13 @@ instance OpElim PythonCode where
instance VariableSym PythonCode where
type Variable PythonCode = VarData
var = G.var
constant = var
constant n = var (map toUpper n)
extVar l n t = modify (addModuleImportVS l) >> CP.extVar l n t
arrayElem i = G.arrayElem (litInt i)

instance OOVariableSym PythonCode where
staticVar = G.staticVar
staticVar' c n t = if c then mkStaticVar n t (R.var (map toUpper n))
else G.staticVar n t
self = zoom lensVStoMS getClassName >>= (\l -> mkStateVar pySelf (obj l) (text pySelf))
classVar = CP.classVar R.classVar
extClassVar c v = join $ on2StateValues (\t cm -> maybe id ((>>) . modify .
Expand Down Expand Up @@ -535,7 +537,14 @@ instance DeclStatement PythonCode where
listDecDef = CP.listDecDef
arrayDec = listDec
arrayDecDef = listDecDef
constDecDef = varDecDef
constDecDef v e = do
v' <- zoom lensMStoVS v
let n = map toUpper (variableName v')
newConst = constant n (pure (variableType v'))
available <- varNameAvailable n
if available
then varDecDef newConst e
else error "Cannot safely capitalize constant."
Copy link
Collaborator

Choose a reason for hiding this comment

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

Do we want this to be an error? We could just warn the user and potentially just use the lowercase version of it (assuming that it is available)…

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

That's a good point. It would be better to do it that way. The only thing I'm not sure about is, how do we keep track of whether or not the renderer used the modified name? When the constant function shows up later, it needs to know whether or not to modify the variable name.

  • We could have it infer whether the modified or unmodified name was used by checking varNameAvailable (variableName v') - if the original name was used, we can probably assume that it was used for the constant in question, else we must have modified it. This would work, but it feels a little sketchy to me.
  • A better way might be to have a mapping from GOOL variable name to rendered variable name floating around in the State. Then in the constant function, we could look up the name to use, and not have to worry about any misplaced assumptions. This would be a bit of extra work to implement, though.

What are your thoughts? I'd rather not do option 2 until @JacquesCarette can take a look. Would it be better to do option 1 for now, as it should be a small change?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

As a side note, what's the best way to print a warning in Haskell? I'm not actually sure if there's anywhere else in GOOL that currently prints warnings. It seems like a shame to wrap all of GOOL in IO String just for this, is there a better way?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Honestly, I'm thinking that "error vs. warning" may be a variability we'd want to introduce at some point. Since this is likely going to be a bigger change, I would create a new issue for tracking our design decisions and working on it in a separate PR (if at all!)

No idea about your warning question 😅

funcDecDef = CP.funcDecDef

instance OODeclStatement PythonCode where
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ instance VariableSym SwiftCode where
arrayElem i = G.arrayElem (litInt i)

instance OOVariableSym SwiftCode where
staticVar = G.staticVar
staticVar' _ = G.staticVar
self = CP.self
classVar = CP.classVar R.classVar
extClassVar = classVar
Expand Down
7 changes: 6 additions & 1 deletion code/drasil-gool/lib/GOOL/Drasil/State.hs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ module GOOL.Drasil.State (
setMainDoc, getMainDoc, setScope, getScope, setCurrMainFunc, getCurrMainFunc,
setThrowUsed, getThrowUsed, setErrorDefined, getErrorDefined, addIter,
getIter, resetIter, incrementLine, incrementWord, getLineIndex, getWordIndex,
resetIndices, useVarName, genVarName, genLoopIndex
resetIndices, useVarName, genVarName, genLoopIndex, varNameAvailable
) where

import GOOL.Drasil.AST (FileType(..), ScopeTag(..), QualifiedName, qualName)
Expand Down Expand Up @@ -569,6 +569,11 @@ genVarName candidates backup = do
genLoopIndex :: MS String
genLoopIndex = genVarName ["i", "j", "k"] "i"

varNameAvailable :: String -> MS Bool
varNameAvailable n = do
used <- gets (^. varNames)
return $ isNothing $ Map.lookup n used

-- Helpers

ifElemError :: (Eq a) => a -> [a] -> String -> [a]
Expand Down
2 changes: 1 addition & 1 deletion code/stable/dblpend/src/python/Constants.py

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions code/stable/gooltest/cpp/HelloWorld/HelloWorld.cpp

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions code/stable/gooltest/csharp/HelloWorld/HelloWorld.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion code/stable/gooltest/python/HelloWorld/HelloWorld.py

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions code/stable/gooltest/swift/HelloWorld/main.swift

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion code/stable/pdcontroller/src/python/Calculations.py

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions code/stable/pdcontroller/src/python/Constants.py

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading