Skip to content

Commit

Permalink
Merge pull request #3845 from JacquesCarette/SeparateOOProc
Browse files Browse the repository at this point in the history
Split `ClassInterface.hs` into separate files.
  • Loading branch information
JacquesCarette authored Jul 10, 2024
2 parents 1c41b15 + 2f6cd01 commit eacde82
Show file tree
Hide file tree
Showing 16 changed files with 508 additions and 474 deletions.
65 changes: 33 additions & 32 deletions code/drasil-gool/lib/GOOL/Drasil.hs
Original file line number Diff line number Diff line change
Expand Up @@ -18,39 +18,40 @@ module GOOL.Drasil (Label, GSProgram, SFile, MSBody, MSBlock, VSType,
libFuncApp, newObj, extNewObj, libNewObj, exists, objMethodCall,
objMethodCallNamedArgs, objMethodCallMixedArgs, objMethodCallNoParams,
FunctionSym(..), ($.), selfAccess, GetSet(..), List(..), listSlice,
listIndexExists, at, ObserverPattern(..),
StrategyPattern(..), ScopeSym(..), ParameterSym(..), MethodSym(..),
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
listIndexExists, at, ObserverPattern(..), StrategyPattern(..), ScopeSym(..),
ParameterSym(..), MethodSym(..), 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.ClassInterface (Label, GSProgram, SFile, MSBody, MSBlock,
VSType, SVariable, SValue, VSFunction, MSStatement, MSParameter, SMethod,
CSStateVar, SClass, FSModule, NamedArgs, Initializers, OOProg,
ProgramSym(..), FileSym(..), PermanenceSym(..), BodySym(..), bodyStatements,
oneLiner, BlockSym(..), TypeSym(..), OOTypeSym(..), TypeElim(..), ThunkSym(..),
VectorType(..), VectorDecl(..), VectorThunk(..), VectorExpression(..),
ThunkAssign(..), StatementSym(..), AssignStatement(..), (&=),
assignToListIndex, DeclStatement(..), OODeclStatement(..), objDecNewNoParams,
extObjDecNewNoParams, IOStatement(..), StringStatement(..),
FuncAppStatement(..), OOFuncAppStatement(..), CommentStatement(..),
initObserverList, addObserver, ControlStatement(..), switchAsIf, ifNoElse,
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(..),
privMethod, pubMethod, initializer, nonInitConstructor, StateVarSym(..),
privDVar, pubDVar, pubSVar, ClassSym(..), ModuleSym(..), convType, convTypeOO)
import GOOL.Drasil.InterfaceCommon (Label, GSProgram, SFile, MSBody, MSBlock,
VSType, SVariable, SValue, MSStatement, MSParameter, SMethod, CSStateVar,
SClass, FSModule, NamedArgs, Initializers, ProgramSym(..), FileSym(..),
PermanenceSym(..), BodySym(..), bodyStatements, oneLiner, BlockSym(..),
TypeSym(..), TypeElim(..), ThunkSym(..), VectorType(..), VectorDecl(..),
VectorThunk(..), VectorExpression(..), ThunkAssign(..), StatementSym(..),
AssignStatement(..), (&=), assignToListIndex, DeclStatement(..),
IOStatement(..), StringStatement(..), FuncAppStatement(..),
CommentStatement(..), ControlStatement(..), switchAsIf, ifNoElse,
VariableSym(..), VariableElim(..), listOf, listVar, ValueSym(..),
Argument(..), Literal(..), MathConstant(..), VariableValue(..),
CommandLineArgs(..), NumericExpression(..), BooleanExpression(..),
Comparison(..), ValueExpression(..), funcApp, funcAppNamedArgs, extFuncApp,
libFuncApp, exists, List(..), listSlice, listIndexExists, at, ScopeSym(..),
ParameterSym(..), MethodSym(..), privMethod, pubMethod, initializer,
nonInitConstructor, StateVarSym(..), privDVar, pubDVar, pubSVar, ClassSym(..),
ModuleSym(..), convType)
import GOOL.Drasil.InterfaceGOOL (VSFunction, OOProg, OOTypeSym(..),
OOVariableSym(..), ($->), OOVariableValue, OOValueExpression(..),
selfFuncApp, newObj, extNewObj, libNewObj, OODeclStatement(..),
objDecNewNoParams, extObjDecNewNoParams, OOFuncAppStatement(..), GetSet(..),
objMethodCall, objMethodCallNamedArgs, objMethodCallMixedArgs,
objMethodCallNoParams, FunctionSym(..), ($.), selfAccess, ObserverPattern(..),
initObserverList, addObserver, StrategyPattern(..), convTypeOO)

import GOOL.Drasil.AST (FileData(..), ModData(..), ProgData(..), ScopeTag(..))

Expand Down
35 changes: 19 additions & 16 deletions code/drasil-gool/lib/GOOL/Drasil/CodeInfo.hs
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,22 @@
-- Performs code analysis on the GOOL code
module GOOL.Drasil.CodeInfo (CodeInfo(..)) where

import GOOL.Drasil.ClassInterface (MSBody, VSType, SValue, MSStatement,
SMethod, OOProg, ProgramSym(..), FileSym(..), PermanenceSym(..), BodySym(..),
BlockSym(..), TypeSym(..), OOTypeSym(..), TypeElim(..), VariableSym(..),
OOVariableSym(..), VariableElim(..), ValueSym(..), OOValueSym, Argument(..),
Literal(..), MathConstant(..), VariableValue(..), OOVariableValue,
CommandLineArgs(..), NumericExpression(..), BooleanExpression(..), Comparison(..),
ValueExpression(..), OOValueExpression(..), InternalValueExp(..),
FunctionSym(..), GetSet(..), List(..), InternalList(..), ThunkSym(..),
VectorType(..), VectorDecl(..), VectorThunk(..), VectorExpression(..),
ThunkAssign(..), StatementSym(..), AssignStatement(..), DeclStatement(..),
OODeclStatement(..), IOStatement(..), StringStatement(..),
FuncAppStatement(..), OOFuncAppStatement(..), CommentStatement(..),
ControlStatement(..), ObserverPattern(..), StrategyPattern(..), ScopeSym(..),
ParameterSym(..), MethodSym(..), StateVarSym(..), ClassSym(..), ModuleSym(..))
import GOOL.Drasil.InterfaceCommon (MSBody, VSType, SValue, MSStatement,
SMethod, SharedProg, ProgramSym(..), FileSym(..), PermanenceSym(..),
BodySym(..), BlockSym(..), TypeSym(..), TypeElim(..), VariableSym(..),
VariableElim(..), ValueSym(..), Argument(..), Literal(..), MathConstant(..),
VariableValue(..), CommandLineArgs(..), NumericExpression(..),
BooleanExpression(..), Comparison(..), ValueExpression(..), List(..),
InternalList(..), ThunkSym(..), VectorType(..), VectorDecl(..),
VectorThunk(..), VectorExpression(..), ThunkAssign(..), StatementSym(..),
AssignStatement(..), DeclStatement(..), IOStatement(..), StringStatement(..),
FuncAppStatement(..), CommentStatement(..), ControlStatement(..),
ScopeSym(..), ParameterSym(..), MethodSym(..), StateVarSym(..), ClassSym(..),
ModuleSym(..))
import GOOL.Drasil.InterfaceGOOL (OOProg, OOTypeSym(..), OOVariableSym(..),
OOValueSym, OOVariableValue, OOValueExpression(..), InternalValueExp(..),
FunctionSym(..), GetSet(..), OODeclStatement(..), OOFuncAppStatement(..),
ObserverPattern(..), StrategyPattern(..))
import GOOL.Drasil.CodeType (CodeType(Void))
import GOOL.Drasil.AST (ScopeTag(..), qualName)
import GOOL.Drasil.CodeAnalysis (ExceptionType(..))
Expand Down Expand Up @@ -47,7 +49,8 @@ instance Applicative CodeInfo where
instance Monad CodeInfo where
CI x >>= f = f x

instance OOProg CodeInfo where
instance SharedProg CodeInfo
instance OOProg CodeInfo

instance ProgramSym CodeInfo where
type Program CodeInfo = GOOLState
Expand Down Expand Up @@ -195,7 +198,6 @@ instance Comparison CodeInfo where
instance ValueExpression CodeInfo where
inlineIf = execute3
funcAppMixedArgs n _ = currModCall n
selfFuncAppMixedArgs = funcAppMixedArgs
extFuncAppMixedArgs l n _ vs ns = do
sequence_ vs
executePairList ns
Expand All @@ -207,6 +209,7 @@ instance ValueExpression CodeInfo where
notNull = execute1

instance OOValueExpression CodeInfo where
selfFuncAppMixedArgs = funcAppMixedArgs
newObjMixedArgs ot vs ns = do
sequence_ vs
executePairList ns
Expand Down
Original file line number Diff line number Diff line change
@@ -1,38 +1,30 @@
{-# LANGUAGE TypeFamilies #-}

module GOOL.Drasil.ClassInterface (
module GOOL.Drasil.InterfaceCommon (
-- Types
Label, Library, GSProgram, SFile, MSBody, MSBlock, VSType, SVariable, SValue,
VSThunk, VSFunction, MSStatement, MSParameter, SMethod, CSStateVar, SClass,
VSThunk, MSStatement, MSParameter, SMethod, CSStateVar, SClass,
FSModule, NamedArgs, Initializers, MixedCall, MixedCtorCall, PosCall,
PosCtorCall,
PosCtorCall, InOutCall,
-- Typeclasses
OOProg, ProcProg, ProgramSym(..), FileSym(..), PermanenceSym(..), BodySym(..),
bodyStatements, oneLiner, BlockSym(..), TypeSym(..), OOTypeSym(..), TypeElim(..),
VariableSym(..), OOVariableSym(..), VariableElim(..), ($->), listOf, listVar,
ValueSym(..), OOValueSym, Argument(..), Literal(..), litZero,
MathConstant(..), VariableValue(..), OOVariableValue, CommandLineArgs(..),
NumericExpression(..), BooleanExpression(..), Comparison(..),
ValueExpression(..), OOValueExpression(..), funcApp, funcAppNamedArgs,
selfFuncApp, extFuncApp, libFuncApp, newObj, extNewObj, libNewObj, exists,
InternalValueExp(..), objMethodCall, objMethodCallNamedArgs,
objMethodCallMixedArgs, objMethodCallNoParams, FunctionSym(..), ($.),
selfAccess, GetSet(..), List(..), InternalList(..), listSlice,
listIndexExists, at, ThunkSym(..),
VectorType(..), VectorDecl(..), VectorThunk(..), VectorExpression(..),
ThunkAssign(..), StatementSym(..), AssignStatement(..), (&=),
assignToListIndex, DeclStatement(..), OODeclStatement(..), objDecNewNoParams,
extObjDecNewNoParams, IOStatement(..), StringStatement(..),
FuncAppStatement(..), OOFuncAppStatement(..), CommentStatement(..),
ControlStatement(..), ObserverPattern(..), observerListName, initObserverList,
addObserver, StrategyPattern(..), ifNoElse, switchAsIf, ScopeSym(..),
ParameterSym(..), MethodSym(..), privMethod, pubMethod, initializer,
nonInitConstructor, StateVarSym(..), privDVar, pubDVar, pubSVar, ClassSym(..),
ModuleSym(..), convType, convTypeOO
SharedProg, ProgramSym(..), FileSym(..), PermanenceSym(..), BodySym(..),
bodyStatements, oneLiner, BlockSym(..), TypeSym(..), TypeElim(..),
VariableSym(..), VariableElim(..), listOf, listVar, ValueSym(..),
Argument(..), Literal(..), litZero, MathConstant(..), VariableValue(..),
CommandLineArgs(..), NumericExpression(..), BooleanExpression(..),
Comparison(..), ValueExpression(..), funcApp, funcAppNamedArgs, extFuncApp,
libFuncApp, exists, List(..), InternalList(..), listSlice, listIndexExists,
at, ThunkSym(..), VectorType(..), VectorDecl(..), VectorThunk(..),
VectorExpression(..), ThunkAssign(..), StatementSym(..), AssignStatement(..),
(&=), assignToListIndex, DeclStatement(..), IOStatement(..),
StringStatement(..), FuncAppStatement(..), CommentStatement(..),
ControlStatement(..), ifNoElse, switchAsIf, ScopeSym(..), ParameterSym(..),
MethodSym(..), privMethod, pubMethod, initializer, nonInitConstructor,
StateVarSym(..), privDVar, pubDVar, pubSVar, ClassSym(..), ModuleSym(..),
convType
) where

import GOOL.Drasil.CodeType (CodeType(..), ClassName)
import GOOL.Drasil.Helpers (onStateValue)
import GOOL.Drasil.CodeType (CodeType(..))
import GOOL.Drasil.State (GS, FS, CS, MS, VS)

import qualified Data.Kind as K (Type)
Expand All @@ -49,22 +41,13 @@ type GSProgram a = GS (a (Program a))
-- Functions in GOOL's interface beginning with "ext" are to be used to access items from other modules in the same program/project
-- Functions in GOOL's interface beginning with "lib" are to be used to access items from different libraries/projects

class (ProgramSym r, OOVariableValue r, VectorType r, VectorDecl r, VectorThunk r,
VectorExpression r, ThunkAssign r, AssignStatement r, OODeclStatement r,
IOStatement r, StringStatement r, OOFuncAppStatement r, CommentStatement r,
ControlStatement r, InternalList r, Argument r, Literal r, MathConstant r,
VariableValue r, CommandLineArgs r, NumericExpression r, BooleanExpression r,
Comparison r, OOValueExpression r, InternalValueExp r, GetSet r, List r,
ObserverPattern r, StrategyPattern r, TypeElim r, VariableElim r
) => OOProg r

class (ProgramSym r, VectorType r, VectorDecl r, VectorThunk r,
class (VectorType r, VectorDecl r, VectorThunk r,
VectorExpression r, ThunkAssign r, AssignStatement r, DeclStatement r,
IOStatement r, StringStatement r, FuncAppStatement r, CommentStatement r,
ControlStatement r, InternalList r, Argument r, Literal r, MathConstant r,
VariableValue r, CommandLineArgs r, NumericExpression r, BooleanExpression r,
Comparison r, ValueExpression r, List r, TypeElim r, VariableElim r
) => ProcProg r
) => SharedProg r

-- Shared between OO and Procedural --

Expand Down Expand Up @@ -142,10 +125,6 @@ class (VariableSym r) => VariableElim r where
variableName :: r (Variable r) -> String
variableType :: r (Variable r) -> r (Type r)

($->) :: (OOVariableSym r) => SVariable r -> SVariable r -> SVariable r
infixl 9 $->
($->) = objVar

listVar :: (VariableSym r) => Label -> VSType r -> SVariable r
listVar n t = var n (listType t)

Expand Down Expand Up @@ -266,7 +245,6 @@ class (VariableSym r, ValueSym r) => ValueExpression r where
inlineIf :: SValue r -> SValue r -> SValue r -> SValue r

funcAppMixedArgs :: MixedCall r
selfFuncAppMixedArgs :: MixedCall r
extFuncAppMixedArgs :: Library -> MixedCall r
libFuncAppMixedArgs :: Library -> MixedCall r

Expand All @@ -281,9 +259,6 @@ funcAppNamedArgs :: (ValueExpression r) => Label -> VSType r ->
NamedArgs r -> SValue r
funcAppNamedArgs n t = funcAppMixedArgs n t []

selfFuncApp :: (ValueExpression r) => PosCall r
selfFuncApp n t vs = selfFuncAppMixedArgs n t vs []

extFuncApp :: (ValueExpression r) => Library -> PosCall r
extFuncApp l n t vs = extFuncAppMixedArgs l n t vs []

Expand Down Expand Up @@ -588,117 +563,6 @@ class (ClassSym r) => ModuleSym r where
-- Module name, import names, module functions, module classes
buildModule :: Label -> [Label] -> [SMethod r] -> [SClass r] -> FSModule r

-- OO Only --
class (TypeSym r) => OOTypeSym r where
obj :: ClassName -> VSType r

class (ValueSym r, OOTypeSym r) => OOValueSym r

class (VariableSym r, OOTypeSym r) => OOVariableSym r where
staticVar :: Label -> VSType r -> SVariable r -- I *think* this is OO-only
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

-- for values that can include expressions
class (ValueExpression r, OOVariableSym r, OOValueSym r) => OOValueExpression r where
newObjMixedArgs :: MixedCtorCall r
extNewObjMixedArgs :: Library -> MixedCtorCall r
libNewObjMixedArgs :: Library -> MixedCtorCall r

newObj :: (OOValueExpression r) => PosCtorCall r
newObj t vs = newObjMixedArgs t vs []

extNewObj :: (OOValueExpression r) => Library -> PosCtorCall r
extNewObj l t vs = extNewObjMixedArgs l t vs []

libNewObj :: (OOValueExpression r) => Library -> PosCtorCall r
libNewObj l t vs = libNewObjMixedArgs l t vs []

class (VariableValue r, OOVariableSym r) => OOVariableValue r

class (FuncAppStatement r, OOVariableSym r) => OOFuncAppStatement r where
selfInOutCall :: InOutCall r

class (DeclStatement r, OOVariableSym r) => OODeclStatement r where
objDecDef :: SVariable r -> SValue r -> MSStatement r
objDecNew :: SVariable r -> [SValue r] -> MSStatement r
extObjDecNew :: Library -> SVariable r -> [SValue r] -> MSStatement r

objDecNewNoParams :: (OODeclStatement r) => SVariable r -> MSStatement r
objDecNewNoParams v = objDecNew v []

extObjDecNewNoParams :: (OODeclStatement r) => Library -> SVariable r ->
MSStatement r
extObjDecNewNoParams l v = extObjDecNew l v []

class (StatementSym r, FunctionSym r) => ObserverPattern r where
notifyObservers :: VSFunction r -> VSType r -> MSStatement r

observerListName :: Label
observerListName = "observerList"

initObserverList :: (DeclStatement r) => VSType r -> [SValue r] -> MSStatement r
initObserverList t = listDecDef (var observerListName (listType t))

addObserver :: (StatementSym r, OOVariableValue r, List r) => SValue r ->
MSStatement r
addObserver o = valStmt $ listAdd obsList lastelem o
where obsList = valueOf $ observerListName `listOf` onStateValue valueType o
lastelem = listSize obsList

class (BodySym r, VariableSym r) => StrategyPattern r where
runStrategy :: Label -> [(Label, MSBody r)] -> Maybe (SValue r) ->
Maybe (SVariable r) -> MSBlock r

class (ValueSym r) => InternalValueExp r where
-- | Generic function for calling a method.
-- Takes the function name, the return type, the object, a list of
-- positional arguments, and a list of named arguments.
objMethodCallMixedArgs' :: Label -> VSType r -> SValue r -> [SValue r] ->
NamedArgs r -> SValue r

-- | Calling a method. t is the return type of the method, o is the
-- object, f is the method name, and ps is a list of positional arguments.
objMethodCall :: (InternalValueExp r) => VSType r -> SValue r -> Label ->
[SValue r] -> SValue r
objMethodCall t o f ps = objMethodCallMixedArgs' f t o ps []

-- | Calling a method with named arguments.
objMethodCallNamedArgs :: (InternalValueExp r) => VSType r -> SValue r -> Label
-> NamedArgs r -> SValue r
objMethodCallNamedArgs t o f = objMethodCallMixedArgs' f t o []

-- | Calling a method with a mix of positional and named arguments.
objMethodCallMixedArgs :: (InternalValueExp r) => VSType r -> SValue r -> Label
-> [SValue r] -> NamedArgs r -> SValue r
objMethodCallMixedArgs t o f = objMethodCallMixedArgs' f t o

-- | Calling a method with no parameters.
objMethodCallNoParams :: (InternalValueExp r) => VSType r -> SValue r -> Label
-> SValue r
objMethodCallNoParams t o f = objMethodCall t o f []

type VSFunction a = VS (a (Function a))

class (ValueSym r) => FunctionSym r where
type Function r
func :: Label -> VSType r -> [SValue r] -> VSFunction r
objAccess :: SValue r -> VSFunction r -> SValue r

($.) :: (FunctionSym r) => SValue r -> VSFunction r -> SValue r
infixl 9 $.
($.) = objAccess

selfAccess :: (OOVariableValue r, FunctionSym r) => VSFunction r -> SValue r
selfAccess = objAccess (valueOf self)

class (ValueSym r, VariableSym r) => GetSet r where
get :: SValue r -> SVariable r -> SValue r
set :: SValue r -> SVariable r -> SValue r -> SValue r

-- Utility

convType :: (TypeSym r) => CodeType -> VSType r
Expand All @@ -715,7 +579,3 @@ convType Void = void
convType InFile = infile
convType OutFile = outfile
convType (Object _) = error "Objects not supported"

convTypeOO :: (OOTypeSym r) => CodeType -> VSType r
convTypeOO (Object n) = obj n
convTypeOO t = convType t
Loading

0 comments on commit eacde82

Please sign in to comment.