{-# LANGUAGE TypeFamilies #-}
-- | The logic to render MATLAB code is contained in this module.
module Drasil.GProc.LanguageRenderer.MatlabRenderer (
  -- * MATLAB Code Configuration -- defines syntax of all MATLAB code
  MatlabCode(..), mlName, mlVersion
) where

import Drasil.Shared.InterfaceCommon (Label, Value, SValue, Variable, SVariable,
  getCodeType, UnRepr(..), Body, Block, BodySym(..), BlockSym(..), TypeSym(..),
  TypeElim(..), VariableSym(..), VariableElim(..), ValueSym(..), Argument(..),
  Literal(..), MathConstant(..), VariableValue(..), CommandLineArgs(..),
  NumericExpression(..), BooleanExpression(..), Comparison(..),
  ValueExpression(..), IndexTranslator(..), Reference(..), Array(..), List(..),
  ListStatement(..), Set(..), NativeVector(..), InternalList(..),
  EmptyStatement(..), MultiStatement(..), ValueStatement(..),
  AssignStatement(..), DeclStatement(..), PrintConsole(..), ReadConsole(..),
  FileHandling(..), PrintFile(..), ReadFile(..), StringStatement(..),
  FunctionSym, FuncAppStatement(..), CommentStatement(..), ControlStatement(..),
  switchAsIf, VisibilitySym(..), ScopeSym(..), ParameterSym(..), BinderSym(..),
  BinderElim(..), MethodSym(..), funcApp, (&=), bodyStatements)
import Drasil.GProc.InterfaceProc (ProcProg, ProgramSym(..),
  FileSym(..), ModuleSym(..))

import Drasil.Shared.RendererClassesCommon (CommonRenderSym, ImportSym(..),
  RenderBody(..), BodyElim, RenderBlock(..), BlockElim, RenderType(..),
  UnaryOpSym(..), BinaryOpSym(..), OpElim(uOpPrec, bOpPrec), RenderVariable(..),
  InternalVarElim(variableBind), RenderValue(..), ValueElim(..),
  InternalListFunc(..), RenderFunction(..), FunctionElim(functionType),
  InternalAssignStmt(..), InternalIOStmt(..), InternalControlStmt(..),
  RenderStatement(..), StatementElim(statementTerm), RenderVisibility(..),
  VisibilityElim, MethodTypeSym(..), RenderParam(..),
  ParamElim(parameterName, parameterType), RenderMethod(..), MethodElim,
  BlockCommentSym(..), BlockCommentElim, ScopeElim(..), InternalBinderElim(..))
import qualified Drasil.Shared.RendererClassesCommon as RC (body, block, uOp,
  bOp, variable, value, function, statement, visibility, parameter, method,
  blockComment')
import Drasil.GProc.RendererClassesProc (ProcRenderSym, RenderFile(..),
  RenderMod(..), ModuleElim, ProcRenderMethod(..))
import qualified Drasil.GProc.LanguageRenderer.AbstractProc as A (fileDoc,
  docMod, fileFromData, modFromData, function, funcDecDef,
  innerType)
import qualified Drasil.Shared.LanguageRenderer as R (commentedMod,
  commentedItem, parameterList, body, addComments, multiStmt, sqrt, abs, log10,
  log, exp, sin, cos, tan, asin, acos, atan, floor, ceil, break, continue)
import qualified Drasil.GProc.RendererClassesProc as RC (module')
import qualified Drasil.Shared.LanguageRenderer.LanguagePolymorphic as G (
  comment, param, docFunc, var, multiBody, block, multiBlock, stmt, loopStmt,
  negateOp, plusOp, minusOp, multOp, divideOp, equalOp, greaterOp,
  greaterEqualOp, lessOp, lessEqualOp, csc, sec, cot, valueOf, litChar,
  litDouble, litInt, litString, valStmt, emptyStmt, assign,
  funcAppMixedArgs, call, print, ifCond, tryCatch, listAccess, smartAdd)
import qualified Drasil.Shared.LanguageRenderer.CommonPseudoOO as CP (mainBody,
  functionDoc, docInOutFunc', inOutCall, multiAssign, intToIndex', indexToInt',
  listDecDef, litSet)
import qualified Drasil.Shared.LanguageRenderer.CLike as C (andOp, orOp, litTrue,
  litFalse, while)
import qualified Drasil.Shared.LanguageRenderer.Common as CS (varDecDef,
  funcType, listSize, forEach')
import qualified Drasil.Shared.LanguageRenderer.Macros as M (ifExists,
  increment1, decrement1, listSlice, stringListVals, stringListLists)
import Drasil.Shared.AST (Terminator(..), FileType(Combined), fileD, md,
  updateMod, MethodData, mthd, mthdName, updateMthd, ParamData, paramVar, paramDoc, pd,
  ProgData, TypeData, cType, vd, val, valPrec, valInt, valType, opDoc, opPrec,
  varName, varType, varBind, varDoc, vard, progD, mthdDoc, modDoc,
  FuncData(fType, funcDoc), fd, ScopeData, FileData, ModData)
import Drasil.Shared.CodeType (CodeType(..))
import Drasil.Shared.LanguageRenderer.Constructors (typeFromData, unOpPrec,
  powerPrec, multPrec, unExpr, unExpr', binExpr, binExpr', mkStateVal, mkVal,
  mkStateVar, mkStmtNoEnd, compEqualPrec, typeUnExpr, typeBinExpr)
import Drasil.Shared.LanguageRenderer (listSep', valueList, intValue)
import Drasil.Shared.LanguageRenderer.LanguagePolymorphic (OptionalSpace(..))
import Drasil.Shared.Helpers (toCode, toState, onCodeValue, onStateValue,
  onCodeList, onStateList, on2CodeValues, on2StateValues, emptyIfEmpty, vibcat)
import Drasil.Shared.State (MS, VS, FS, lensGStoFS, lensFStoMS, lensMStoVS,
  revFiles, setFileType, setModuleName, getMainDoc)

import Control.Lens.Zoom (zoom)
import Control.Monad.State (modify)

import Drasil.FileHandling.Legacy (indent)
import Prelude hiding (break,print,sin,cos,tan,floor,(<>))
import Text.PrettyPrint.HughesPJ (Doc, empty, isEmpty, text, (<>), (<+>), vcat,
  hcat, parens, brackets, braces, equals, quotes, punctuate, render)

newtype MatlabCode a = MLC {forall a. MatlabCode a -> a
unMLC :: a} deriving (forall a b. (a -> b) -> MatlabCode a -> MatlabCode b)
-> (forall a b. a -> MatlabCode b -> MatlabCode a)
-> Functor MatlabCode
forall a b. a -> MatlabCode b -> MatlabCode a
forall a b. (a -> b) -> MatlabCode a -> MatlabCode b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
$cfmap :: forall a b. (a -> b) -> MatlabCode a -> MatlabCode b
fmap :: forall a b. (a -> b) -> MatlabCode a -> MatlabCode b
$c<$ :: forall a b. a -> MatlabCode b -> MatlabCode a
<$ :: forall a b. a -> MatlabCode b -> MatlabCode a
Functor

instance Applicative MatlabCode where
  pure :: forall a. a -> MatlabCode a
pure = a -> MatlabCode a
forall a. a -> MatlabCode a
MLC
  (MLC a -> b
f) <*> :: forall a b. MatlabCode (a -> b) -> MatlabCode a -> MatlabCode b
<*> (MLC a
x) = b -> MatlabCode b
forall a. a -> MatlabCode a
MLC (a -> b
f a
x)

instance Monad MatlabCode where
  MLC a
x >>= :: forall a b. MatlabCode a -> (a -> MatlabCode b) -> MatlabCode b
>>= a -> MatlabCode b
f = a -> MatlabCode b
f a
x

instance ProcProg MatlabCode Doc (Doc, Terminator) MethodData ProgData FileData ModData Body Block

instance ProgramSym MatlabCode ProgData FileData where
  prog :: Label
-> Label
-> [FS (MatlabCode FileData)]
-> GSProgram MatlabCode ProgData
prog Label
n Label
st [FS (MatlabCode FileData)]
files = do
    fs <- (FS (MatlabCode FileData)
 -> StateT GOOLState Identity (MatlabCode FileData))
-> [FS (MatlabCode FileData)]
-> StateT GOOLState Identity [MatlabCode FileData]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> [a] -> m [b]
mapM (LensLike'
  (Zoomed (StateT FileState Identity) (MatlabCode FileData))
  GOOLState
  FileState
-> FS (MatlabCode FileData)
-> StateT GOOLState Identity (MatlabCode FileData)
forall c.
LensLike'
  (Zoomed (StateT FileState Identity) c) GOOLState FileState
-> StateT FileState Identity c -> StateT GOOLState Identity c
forall (m :: * -> *) (n :: * -> *) s t c.
Zoom m n s t =>
LensLike' (Zoomed m c) t s -> m c -> n c
zoom LensLike'
  (Zoomed (StateT FileState Identity) (MatlabCode FileData))
  GOOLState
  FileState
(FileState -> Focusing Identity (MatlabCode FileData) FileState)
-> GOOLState -> Focusing Identity (MatlabCode FileData) GOOLState
Lens' GOOLState FileState
lensGStoFS) [FS (MatlabCode FileData)]
files
    modify revFiles
    pure $ onCodeList (progD n st) fs

instance CommonRenderSym MatlabCode Doc (Doc, Terminator) MethodData Body Block
instance ProcRenderSym MatlabCode Doc (Doc, Terminator) MethodData FileData ModData Body Block

instance UnRepr MatlabCode inner where
  unRepr :: MatlabCode inner -> inner
unRepr = MatlabCode inner -> inner
forall a. MatlabCode a -> a
unMLC

instance FileSym MatlabCode FileData ModData where
  fileDoc :: FS (MatlabCode ModData) -> FS (MatlabCode FileData)
fileDoc FS (MatlabCode ModData)
m = do
    (FileState -> FileState) -> StateT FileState Identity ()
forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
modify (FileType -> FileState -> FileState
setFileType FileType
Combined)
    Label -> FS (MatlabCode ModData) -> FS (MatlabCode FileData)
forall (r :: * -> *) file mod.
RenderFile r file mod =>
Label -> FS (r mod) -> FS (r file)
A.fileDoc Label
mlExt FS (MatlabCode ModData)
m
  docMod :: Label
-> Label
-> [Label]
-> Label
-> FS (MatlabCode FileData)
-> FS (MatlabCode FileData)
docMod = Label
-> Label
-> Label
-> [Label]
-> Label
-> FS (MatlabCode FileData)
-> FS (MatlabCode FileData)
forall (r :: * -> *) file mod.
RenderFile r file mod =>
Label
-> Label -> Label -> [Label] -> Label -> FS (r file) -> FS (r file)
A.docMod Label
mlExt

instance RenderFile MatlabCode FileData ModData where
  top :: MatlabCode ModData -> MatlabCode Doc
top MatlabCode ModData
_ = Doc -> MatlabCode Doc
forall (r :: * -> *) a. Monad r => a -> r a
toCode Doc
empty
  bottom :: MatlabCode Doc
bottom = Doc -> MatlabCode Doc
forall (r :: * -> *) a. Monad r => a -> r a
toCode Doc
empty
  commentedMod :: FS (MatlabCode FileData)
-> FS (MatlabCode Doc) -> FS (MatlabCode FileData)
commentedMod = (MatlabCode FileData -> MatlabCode Doc -> MatlabCode FileData)
-> FS (MatlabCode FileData)
-> FS (MatlabCode Doc)
-> FS (MatlabCode FileData)
forall a b c s.
(a -> b -> c) -> State s a -> State s b -> State s c
on2StateValues ((FileData -> Doc -> FileData)
-> MatlabCode FileData -> MatlabCode Doc -> MatlabCode FileData
forall (r :: * -> *) a b c.
Applicative r =>
(a -> b -> c) -> r a -> r b -> r c
on2CodeValues FileData -> Doc -> FileData
R.commentedMod)
  fileFromData :: Label -> FS (MatlabCode ModData) -> FS (MatlabCode FileData)
fileFromData = (Label -> MatlabCode ModData -> MatlabCode FileData)
-> Label -> FS (MatlabCode ModData) -> FS (MatlabCode FileData)
forall {k} (r :: k -> *) (mod :: k) (file :: k).
ModuleElim r mod =>
(Label -> r mod -> r file) -> Label -> FS (r mod) -> FS (r file)
A.fileFromData ((ModData -> FileData) -> MatlabCode ModData -> MatlabCode FileData
forall (r :: * -> *) a b. Functor r => (a -> b) -> r a -> r b
onCodeValue ((ModData -> FileData)
 -> MatlabCode ModData -> MatlabCode FileData)
-> (Label -> ModData -> FileData)
-> Label
-> MatlabCode ModData
-> MatlabCode FileData
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Label -> ModData -> FileData
fileD)

instance ImportSym MatlabCode where
  langImport :: Label -> MatlabCode Doc
langImport = Label -> MatlabCode Doc
forall a. HasCallStack => a
undefined
  modImport :: Label -> MatlabCode Doc
modImport = Label -> MatlabCode Doc
forall a. HasCallStack => a
undefined

instance BodySym MatlabCode Body Block where
  body :: [MS (MatlabCode Doc)] -> MS (MatlabCode Doc)
body = ([MatlabCode Doc] -> MatlabCode Doc)
-> [MS (MatlabCode Doc)] -> MS (MatlabCode Doc)
forall a b s. ([a] -> b) -> [State s a] -> State s b
onStateList (([Doc] -> Doc) -> [MatlabCode Doc] -> MatlabCode Doc
forall (m :: * -> *) a b. Monad m => ([a] -> b) -> [m a] -> m b
onCodeList [Doc] -> Doc
R.body)
  addComments :: Label -> MS (MatlabCode Doc) -> MS (MatlabCode Doc)
addComments Label
s = (MatlabCode Doc -> MatlabCode Doc)
-> MS (MatlabCode Doc) -> MS (MatlabCode Doc)
forall a b s. (a -> b) -> State s a -> State s b
onStateValue ((Doc -> Doc) -> MatlabCode Doc -> MatlabCode Doc
forall (r :: * -> *) a b. Functor r => (a -> b) -> r a -> r b
onCodeValue (Label -> Doc -> Doc -> Doc
R.addComments Label
s Doc
mlCmtStart))

instance RenderBody MatlabCode Body where
  multiBody :: [MS (MatlabCode Doc)] -> MS (MatlabCode Doc)
multiBody = [MS (MatlabCode Doc)] -> MS (MatlabCode Doc)
forall (r :: * -> *) bod.
(BodyElim r bod, Monad r) =>
[MS (r bod)] -> MS (r Doc)
G.multiBody

instance BodyElim MatlabCode Body where
  body :: MatlabCode Doc -> Doc
body = MatlabCode Doc -> Doc
forall a. MatlabCode a -> a
unMLC

instance BlockSym MatlabCode Block (Doc, Terminator) where
  block :: [MS (MatlabCode (Doc, Terminator))] -> MS (MatlabCode Doc)
block = [MS (MatlabCode (Doc, Terminator))] -> MS (MatlabCode Doc)
forall (r :: * -> *) stmt.
(Monad r, RenderStatement r stmt, StatementElim r stmt) =>
[MS (r stmt)] -> MS (r Doc)
G.block

instance RenderBlock MatlabCode Block where
  multiBlock :: [MS (MatlabCode Doc)] -> MS (MatlabCode Doc)
multiBlock = [MS (MatlabCode Doc)] -> MS (MatlabCode Doc)
forall (r :: * -> *) block.
(BlockElim r block, Monad r) =>
[MS (r block)] -> MS (r Doc)
G.multiBlock

instance BlockElim MatlabCode Block where
  block :: MatlabCode Doc -> Doc
block = MatlabCode Doc -> Doc
forall a. MatlabCode a -> a
unMLC

instance TypeSym MatlabCode where
  bool :: VS (MatlabCode TypeData)
bool = CodeType -> Label -> VS (MatlabCode TypeData)
mlTy CodeType
Boolean Label
"logical"
  int :: VS (MatlabCode TypeData)
int = CodeType -> Label -> VS (MatlabCode TypeData)
mlTy CodeType
Integer Label
"int"
  float :: VS (MatlabCode TypeData)
float = CodeType -> Label -> VS (MatlabCode TypeData)
mlTy CodeType
Float Label
"double"
  double :: VS (MatlabCode TypeData)
double = CodeType -> Label -> VS (MatlabCode TypeData)
mlTy CodeType
Double Label
"double"
  char :: VS (MatlabCode TypeData)
char = CodeType -> Label -> VS (MatlabCode TypeData)
mlTy CodeType
Char Label
"char"
  string :: VS (MatlabCode TypeData)
string = CodeType -> Label -> VS (MatlabCode TypeData)
mlTy CodeType
String Label
"string"
  infile :: VS (MatlabCode TypeData)
infile = CodeType -> Label -> VS (MatlabCode TypeData)
mlTy CodeType
InFile Label
"file"
  outfile :: VS (MatlabCode TypeData)
outfile = CodeType -> Label -> VS (MatlabCode TypeData)
mlTy CodeType
OutFile Label
"file"
  referenceType :: VS (MatlabCode TypeData) -> VS (MatlabCode TypeData)
referenceType = VS (MatlabCode TypeData) -> VS (MatlabCode TypeData)
forall a. a -> a
id -- Ignore reference types in "high-level" langauges for now; later on think about using boxed/unboxed types
  listType :: VS (MatlabCode TypeData) -> VS (MatlabCode TypeData)
listType = VS (MatlabCode TypeData) -> VS (MatlabCode TypeData)
mlListType
  setType :: VS (MatlabCode TypeData) -> VS (MatlabCode TypeData)
setType = VS (MatlabCode TypeData) -> VS (MatlabCode TypeData)
forall (r :: * -> *).
TypeSym r =>
VS (r TypeData) -> VS (r TypeData)
listType
  arrayType :: VS (MatlabCode TypeData) -> VS (MatlabCode TypeData)
arrayType = VS (MatlabCode TypeData) -> VS (MatlabCode TypeData)
forall (r :: * -> *).
TypeSym r =>
VS (r TypeData) -> VS (r TypeData)
listType -- Treat arrays and lists the same, as in Julia/Python
  innerType :: VS (MatlabCode TypeData) -> VS (MatlabCode TypeData)
innerType = VS (MatlabCode TypeData) -> VS (MatlabCode TypeData)
forall (r :: * -> *).
TypeElim r =>
VS (r TypeData) -> VS (r TypeData)
A.innerType
  funcType :: [VS (MatlabCode TypeData)]
-> VS (MatlabCode TypeData) -> VS (MatlabCode TypeData)
funcType = [VS (MatlabCode TypeData)]
-> VS (MatlabCode TypeData) -> VS (MatlabCode TypeData)
forall (r :: * -> *).
(Monad r, TypeElim r) =>
[VS (r TypeData)] -> VS (r TypeData) -> VS (r TypeData)
CS.funcType
  void :: VS (MatlabCode TypeData)
void = CodeType -> Label -> VS (MatlabCode TypeData)
mlTy CodeType
Void Label
"void"

instance TypeElim MatlabCode where
  getCodeType :: MatlabCode TypeData -> CodeType
getCodeType = TypeData -> CodeType
cType (TypeData -> CodeType)
-> (MatlabCode TypeData -> TypeData)
-> MatlabCode TypeData
-> CodeType
forall b c a. (b -> c) -> (a -> b) -> a -> c
. MatlabCode TypeData -> TypeData
forall a. MatlabCode a -> a
unMLC

instance RenderType MatlabCode where
  multiType :: [VS (MatlabCode TypeData)] -> VS (MatlabCode TypeData)
multiType = [VS (MatlabCode TypeData)] -> VS (MatlabCode TypeData)
forall a. HasCallStack => a
undefined

instance UnaryOpSym MatlabCode where
  notOp :: VSUnOp MatlabCode
notOp = Label -> VSUnOp MatlabCode
forall (r :: * -> *). Monad r => Label -> VSOp r
unOpPrec Label
"~"
  negateOp :: VSUnOp MatlabCode
negateOp = VSUnOp MatlabCode
forall (r :: * -> *). Monad r => VSOp r
G.negateOp
  sqrtOp :: VSUnOp MatlabCode
sqrtOp = Label -> VSUnOp MatlabCode
forall (r :: * -> *). Monad r => Label -> VSOp r
unOpPrec Label
R.sqrt
  absOp :: VSUnOp MatlabCode
absOp = Label -> VSUnOp MatlabCode
forall (r :: * -> *). Monad r => Label -> VSOp r
unOpPrec Label
R.abs
  logOp :: VSUnOp MatlabCode
logOp = Label -> VSUnOp MatlabCode
forall (r :: * -> *). Monad r => Label -> VSOp r
unOpPrec Label
R.log10
  lnOp :: VSUnOp MatlabCode
lnOp = Label -> VSUnOp MatlabCode
forall (r :: * -> *). Monad r => Label -> VSOp r
unOpPrec Label
R.log
  expOp :: VSUnOp MatlabCode
expOp = Label -> VSUnOp MatlabCode
forall (r :: * -> *). Monad r => Label -> VSOp r
unOpPrec Label
R.exp
  sinOp :: VSUnOp MatlabCode
sinOp = Label -> VSUnOp MatlabCode
forall (r :: * -> *). Monad r => Label -> VSOp r
unOpPrec Label
R.sin
  cosOp :: VSUnOp MatlabCode
cosOp = Label -> VSUnOp MatlabCode
forall (r :: * -> *). Monad r => Label -> VSOp r
unOpPrec Label
R.cos
  tanOp :: VSUnOp MatlabCode
tanOp = Label -> VSUnOp MatlabCode
forall (r :: * -> *). Monad r => Label -> VSOp r
unOpPrec Label
R.tan
  asinOp :: VSUnOp MatlabCode
asinOp = Label -> VSUnOp MatlabCode
forall (r :: * -> *). Monad r => Label -> VSOp r
unOpPrec Label
R.asin
  acosOp :: VSUnOp MatlabCode
acosOp = Label -> VSUnOp MatlabCode
forall (r :: * -> *). Monad r => Label -> VSOp r
unOpPrec Label
R.acos
  atanOp :: VSUnOp MatlabCode
atanOp = Label -> VSUnOp MatlabCode
forall (r :: * -> *). Monad r => Label -> VSOp r
unOpPrec Label
R.atan
  floorOp :: VSUnOp MatlabCode
floorOp = Label -> VSUnOp MatlabCode
forall (r :: * -> *). Monad r => Label -> VSOp r
unOpPrec Label
R.floor
  ceilOp :: VSUnOp MatlabCode
ceilOp = Label -> VSUnOp MatlabCode
forall (r :: * -> *). Monad r => Label -> VSOp r
unOpPrec Label
R.ceil

instance BinaryOpSym MatlabCode where
  equalOp :: VSUnOp MatlabCode
equalOp = VSUnOp MatlabCode
forall (r :: * -> *). Monad r => VSOp r
G.equalOp
  notEqualOp :: VSUnOp MatlabCode
notEqualOp = Label -> VSUnOp MatlabCode
forall (r :: * -> *). Monad r => Label -> VSOp r
compEqualPrec Label
"~="
  greaterOp :: VSUnOp MatlabCode
greaterOp = VSUnOp MatlabCode
forall (r :: * -> *). Monad r => VSOp r
G.greaterOp
  greaterEqualOp :: VSUnOp MatlabCode
greaterEqualOp = VSUnOp MatlabCode
forall (r :: * -> *). Monad r => VSOp r
G.greaterEqualOp
  lessOp :: VSUnOp MatlabCode
lessOp = VSUnOp MatlabCode
forall (r :: * -> *). Monad r => VSOp r
G.lessOp
  lessEqualOp :: VSUnOp MatlabCode
lessEqualOp = VSUnOp MatlabCode
forall (r :: * -> *). Monad r => VSOp r
G.lessEqualOp
  plusOp :: VSUnOp MatlabCode
plusOp = VSUnOp MatlabCode
forall (r :: * -> *). Monad r => VSOp r
G.plusOp
  minusOp :: VSUnOp MatlabCode
minusOp = VSUnOp MatlabCode
forall (r :: * -> *). Monad r => VSOp r
G.minusOp
  multOp :: VSUnOp MatlabCode
multOp = VSUnOp MatlabCode
forall (r :: * -> *). Monad r => VSOp r
G.multOp
  divideOp :: VSUnOp MatlabCode
divideOp = VSUnOp MatlabCode
forall (r :: * -> *). Monad r => VSOp r
G.divideOp
  powerOp :: VSUnOp MatlabCode
powerOp = Label -> VSUnOp MatlabCode
forall (r :: * -> *). Monad r => Label -> VSOp r
powerPrec Label
"^"
  moduloOp :: VSUnOp MatlabCode
moduloOp = Label -> VSUnOp MatlabCode
forall (r :: * -> *). Monad r => Label -> VSOp r
multPrec Label
"mod"
  andOp :: VSUnOp MatlabCode
andOp = VSUnOp MatlabCode
forall (r :: * -> *). Monad r => VSOp r
C.andOp
  orOp :: VSUnOp MatlabCode
orOp = VSUnOp MatlabCode
forall (r :: * -> *). Monad r => VSOp r
C.orOp

instance OpElim MatlabCode where
  uOp :: MatlabCode OpData -> Doc
uOp = OpData -> Doc
opDoc (OpData -> Doc)
-> (MatlabCode OpData -> OpData) -> MatlabCode OpData -> Doc
forall b c a. (b -> c) -> (a -> b) -> a -> c
. MatlabCode OpData -> OpData
forall a. MatlabCode a -> a
unMLC
  bOp :: MatlabCode OpData -> Doc
bOp = OpData -> Doc
opDoc (OpData -> Doc)
-> (MatlabCode OpData -> OpData) -> MatlabCode OpData -> Doc
forall b c a. (b -> c) -> (a -> b) -> a -> c
. MatlabCode OpData -> OpData
forall a. MatlabCode a -> a
unMLC
  uOpPrec :: MatlabCode OpData -> Int
uOpPrec = OpData -> Int
opPrec (OpData -> Int)
-> (MatlabCode OpData -> OpData) -> MatlabCode OpData -> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. MatlabCode OpData -> OpData
forall a. MatlabCode a -> a
unMLC
  bOpPrec :: MatlabCode OpData -> Int
bOpPrec = OpData -> Int
opPrec (OpData -> Int)
-> (MatlabCode OpData -> OpData) -> MatlabCode OpData -> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. MatlabCode OpData -> OpData
forall a. MatlabCode a -> a
unMLC

instance ScopeSym MatlabCode where
  global :: MatlabCode ScopeData
global = MatlabCode ScopeData
forall a. HasCallStack => a
undefined
  mainFn :: MatlabCode ScopeData
mainFn = MatlabCode ScopeData
forall a. HasCallStack => a
undefined
  local :: MatlabCode ScopeData
local = MatlabCode ScopeData
forall a. HasCallStack => a
undefined

instance ScopeElim MatlabCode where
  scopeData :: MatlabCode ScopeData -> ScopeData
scopeData = MatlabCode ScopeData -> ScopeData
forall a. MatlabCode a -> a
unMLC

instance VariableSym MatlabCode where
  var :: Label -> VS (MatlabCode TypeData) -> SVariable MatlabCode
var = Label -> VS (MatlabCode TypeData) -> SVariable MatlabCode
forall (r :: * -> *).
RenderVariable r =>
Label -> VS (r TypeData) -> SVariable r
G.var
  constant :: Label -> VS (MatlabCode TypeData) -> SVariable MatlabCode
constant = Label -> VS (MatlabCode TypeData) -> SVariable MatlabCode
forall (r :: * -> *).
VariableSym r =>
Label -> VS (r TypeData) -> SVariable r
var
  extVar :: Label -> Label -> VS (MatlabCode TypeData) -> SVariable MatlabCode
extVar = Label -> Label -> VS (MatlabCode TypeData) -> SVariable MatlabCode
forall a. HasCallStack => a
undefined

instance VariableElim MatlabCode where
  variableName :: MatlabCode Variable -> Label
variableName = Variable -> Label
varName (Variable -> Label)
-> (MatlabCode Variable -> Variable)
-> MatlabCode Variable
-> Label
forall b c a. (b -> c) -> (a -> b) -> a -> c
. MatlabCode Variable -> Variable
forall a. MatlabCode a -> a
unMLC
  variableType :: MatlabCode Variable -> MatlabCode TypeData
variableType = (Variable -> TypeData)
-> MatlabCode Variable -> MatlabCode TypeData
forall (r :: * -> *) a b. Functor r => (a -> b) -> r a -> r b
onCodeValue Variable -> TypeData
varType

instance InternalVarElim MatlabCode where
  variableBind :: MatlabCode Variable -> AttachmentTag
variableBind = Variable -> AttachmentTag
varBind (Variable -> AttachmentTag)
-> (MatlabCode Variable -> Variable)
-> MatlabCode Variable
-> AttachmentTag
forall b c a. (b -> c) -> (a -> b) -> a -> c
. MatlabCode Variable -> Variable
forall a. MatlabCode a -> a
unMLC
  variable :: MatlabCode Variable -> Doc
variable = Variable -> Doc
varDoc (Variable -> Doc)
-> (MatlabCode Variable -> Variable) -> MatlabCode Variable -> Doc
forall b c a. (b -> c) -> (a -> b) -> a -> c
. MatlabCode Variable -> Variable
forall a. MatlabCode a -> a
unMLC

instance RenderVariable MatlabCode where
  varFromData :: AttachmentTag
-> Label -> VS (MatlabCode TypeData) -> Doc -> SVariable MatlabCode
varFromData AttachmentTag
b Label
n VS (MatlabCode TypeData)
t' Doc
d = do
    t <- VS (MatlabCode TypeData)
t'
    toState $ on2CodeValues (vard b n) t (toCode d)

instance ValueSym MatlabCode where
  valueType :: MatlabCode Value -> MatlabCode TypeData
valueType MatlabCode Value
v = Value -> TypeData
valType (Value -> TypeData) -> MatlabCode Value -> MatlabCode TypeData
forall (r :: * -> *) a b. Functor r => (a -> b) -> r a -> r b
<$> MatlabCode Value
v

instance Argument MatlabCode where
  pointerArg :: SValue MatlabCode -> SValue MatlabCode
pointerArg = SValue MatlabCode -> SValue MatlabCode
forall a. a -> a
id

instance Literal MatlabCode where
  litTrue :: SValue MatlabCode
litTrue = SValue MatlabCode
forall (r :: * -> *). (RenderValue r, TypeSym r) => SValue r
C.litTrue
  litFalse :: SValue MatlabCode
litFalse = SValue MatlabCode
forall (r :: * -> *). (RenderValue r, TypeSym r) => SValue r
C.litFalse
  litChar :: Char -> SValue MatlabCode
litChar = (Doc -> Doc) -> Char -> SValue MatlabCode
forall (r :: * -> *).
(RenderValue r, TypeSym r) =>
(Doc -> Doc) -> Char -> SValue r
G.litChar Doc -> Doc
quotes
  litDouble :: Double -> SValue MatlabCode
litDouble = Double -> SValue MatlabCode
forall (r :: * -> *).
(RenderValue r, TypeSym r) =>
Double -> SValue r
G.litDouble
  litFloat :: Float -> SValue MatlabCode
litFloat Float
f = VS (MatlabCode TypeData) -> Doc -> SValue MatlabCode
forall (r :: * -> *).
RenderValue r =>
VS (r TypeData) -> Doc -> SValue r
mkStateVal VS (MatlabCode TypeData)
forall (r :: * -> *). TypeSym r => VS (r TypeData)
float (Label -> Doc
text (Label -> Doc) -> Label -> Doc
forall a b. (a -> b) -> a -> b
$ Double -> Label
forall a. Show a => a -> Label
show (Float -> Double
forall a b. (Real a, Fractional b) => a -> b
realToFrac Float
f :: Double))
  litInt :: Integer -> SValue MatlabCode
litInt = Integer -> SValue MatlabCode
forall (r :: * -> *).
(RenderValue r, TypeSym r) =>
Integer -> SValue r
G.litInt
  litString :: Label -> SValue MatlabCode
litString = Label -> SValue MatlabCode
forall (r :: * -> *).
(RenderValue r, TypeSym r) =>
Label -> SValue r
G.litString
  litArray :: VS (MatlabCode TypeData)
-> [SValue MatlabCode] -> SValue MatlabCode
litArray = VS (MatlabCode TypeData)
-> [SValue MatlabCode] -> SValue MatlabCode
forall (r :: * -> *).
Literal r =>
VS (r TypeData) -> [SValue r] -> SValue r
litList
  litList :: VS (MatlabCode TypeData)
-> [SValue MatlabCode] -> SValue MatlabCode
litList = VS (MatlabCode TypeData)
-> [SValue MatlabCode] -> SValue MatlabCode
mlLitList
  litSet :: VS (MatlabCode TypeData)
-> [SValue MatlabCode] -> SValue MatlabCode
litSet = (Doc -> Doc)
-> (Doc -> Doc)
-> VS (MatlabCode TypeData)
-> [SValue MatlabCode]
-> SValue MatlabCode
forall (r :: * -> *).
(RenderValue r, TypeSym r, ValueElim r) =>
(Doc -> Doc)
-> (Doc -> Doc) -> VS (r TypeData) -> [SValue r] -> SValue r
CP.litSet Doc -> Doc
forall a. a -> a
id Doc -> Doc
brackets

instance MathConstant MatlabCode where
  pi :: SValue MatlabCode
pi = VS (MatlabCode TypeData) -> Doc -> SValue MatlabCode
forall (r :: * -> *).
RenderValue r =>
VS (r TypeData) -> Doc -> SValue r
mkStateVal VS (MatlabCode TypeData)
forall (r :: * -> *). TypeSym r => VS (r TypeData)
double (Label -> Doc
text Label
"pi")

instance VariableValue MatlabCode where
  valueOf :: SVariable MatlabCode -> SValue MatlabCode
valueOf = SVariable MatlabCode -> SValue MatlabCode
forall (r :: * -> *).
(InternalVarElim r, RenderValue r, VariableElim r) =>
SVariable r -> SValue r
G.valueOf

instance CommandLineArgs MatlabCode where
  -- Args come in through the entry function's varargin (1-based, cell-indexed).
  arg :: Integer -> SValue MatlabCode
arg Integer
n = SValue MatlabCode -> SValue MatlabCode
mlArg (Integer -> SValue MatlabCode
forall (r :: * -> *). Literal r => Integer -> SValue r
litInt (Integer
n Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
+ Integer
1))
  argsList :: SValue MatlabCode
argsList = VS (MatlabCode TypeData) -> Doc -> SValue MatlabCode
forall (r :: * -> *).
RenderValue r =>
VS (r TypeData) -> Doc -> SValue r
mkStateVal (VS (MatlabCode TypeData) -> VS (MatlabCode TypeData)
forall (r :: * -> *).
TypeSym r =>
VS (r TypeData) -> VS (r TypeData)
arrayType VS (MatlabCode TypeData)
forall (r :: * -> *). TypeSym r => VS (r TypeData)
string) (Label -> Doc
text Label
"varargin")
  argExists :: Integer -> SValue MatlabCode
argExists = Integer -> SValue MatlabCode
forall a. HasCallStack => a
undefined

instance NumericExpression MatlabCode where
  #~ :: SValue MatlabCode -> SValue MatlabCode
(#~) = VSUnOp MatlabCode -> SValue MatlabCode -> SValue MatlabCode
forall (r :: * -> *).
(OpElim r, RenderValue r, ValueElim r, ValueSym r) =>
VSUnOp r -> SValue r -> SValue r
unExpr' VSUnOp MatlabCode
forall (r :: * -> *). UnaryOpSym r => VSUnOp r
negateOp
  #/^ :: SValue MatlabCode -> SValue MatlabCode
(#/^) = VSUnOp MatlabCode -> SValue MatlabCode -> SValue MatlabCode
forall (r :: * -> *).
(OpElim r, RenderValue r, ValueElim r, ValueSym r) =>
VSUnOp r -> SValue r -> SValue r
unExpr VSUnOp MatlabCode
forall (r :: * -> *). UnaryOpSym r => VSUnOp r
sqrtOp
  #| :: SValue MatlabCode -> SValue MatlabCode
(#|) = VSUnOp MatlabCode -> SValue MatlabCode -> SValue MatlabCode
forall (r :: * -> *).
(OpElim r, RenderValue r, ValueElim r, ValueSym r) =>
VSUnOp r -> SValue r -> SValue r
unExpr VSUnOp MatlabCode
forall (r :: * -> *). UnaryOpSym r => VSUnOp r
absOp
  #+ :: SValue MatlabCode -> SValue MatlabCode -> SValue MatlabCode
(#+) = VSUnOp MatlabCode
-> SValue MatlabCode -> SValue MatlabCode -> SValue MatlabCode
forall (r :: * -> *).
(OpElim r, RenderValue r, TypeElim r, ValueElim r, ValueSym r) =>
VSBinOp r -> SValue r -> SValue r -> SValue r
binExpr VSUnOp MatlabCode
forall (r :: * -> *). BinaryOpSym r => VSBinOp r
plusOp
  #- :: SValue MatlabCode -> SValue MatlabCode -> SValue MatlabCode
(#-) = VSUnOp MatlabCode
-> SValue MatlabCode -> SValue MatlabCode -> SValue MatlabCode
forall (r :: * -> *).
(OpElim r, RenderValue r, TypeElim r, ValueElim r, ValueSym r) =>
VSBinOp r -> SValue r -> SValue r -> SValue r
binExpr VSUnOp MatlabCode
forall (r :: * -> *). BinaryOpSym r => VSBinOp r
minusOp
  #* :: SValue MatlabCode -> SValue MatlabCode -> SValue MatlabCode
(#*) = VSUnOp MatlabCode
-> SValue MatlabCode -> SValue MatlabCode -> SValue MatlabCode
forall (r :: * -> *).
(OpElim r, RenderValue r, TypeElim r, ValueElim r, ValueSym r) =>
VSBinOp r -> SValue r -> SValue r -> SValue r
binExpr VSUnOp MatlabCode
forall (r :: * -> *). BinaryOpSym r => VSBinOp r
multOp
  #/ :: SValue MatlabCode -> SValue MatlabCode -> SValue MatlabCode
(#/) = VSUnOp MatlabCode
-> SValue MatlabCode -> SValue MatlabCode -> SValue MatlabCode
forall (r :: * -> *).
(OpElim r, RenderValue r, TypeElim r, ValueElim r, ValueSym r) =>
VSBinOp r -> SValue r -> SValue r -> SValue r
binExpr VSUnOp MatlabCode
forall (r :: * -> *). BinaryOpSym r => VSBinOp r
divideOp
  #% :: SValue MatlabCode -> SValue MatlabCode -> SValue MatlabCode
(#%) = VSUnOp MatlabCode
-> SValue MatlabCode -> SValue MatlabCode -> SValue MatlabCode
forall (r :: * -> *).
(OpElim r, RenderValue r, TypeElim r, ValueElim r, ValueSym r) =>
VSBinOp r -> SValue r -> SValue r -> SValue r
binExpr' VSUnOp MatlabCode
forall (r :: * -> *). BinaryOpSym r => VSBinOp r
moduloOp
  #^ :: SValue MatlabCode -> SValue MatlabCode -> SValue MatlabCode
(#^) = VSUnOp MatlabCode
-> SValue MatlabCode -> SValue MatlabCode -> SValue MatlabCode
forall (r :: * -> *).
(OpElim r, RenderValue r, TypeElim r, ValueElim r, ValueSym r) =>
VSBinOp r -> SValue r -> SValue r -> SValue r
binExpr VSUnOp MatlabCode
forall (r :: * -> *). BinaryOpSym r => VSBinOp r
powerOp
  log :: SValue MatlabCode -> SValue MatlabCode
log = VSUnOp MatlabCode -> SValue MatlabCode -> SValue MatlabCode
forall (r :: * -> *).
(OpElim r, RenderValue r, ValueElim r, ValueSym r) =>
VSUnOp r -> SValue r -> SValue r
unExpr VSUnOp MatlabCode
forall (r :: * -> *). UnaryOpSym r => VSUnOp r
logOp
  ln :: SValue MatlabCode -> SValue MatlabCode
ln = VSUnOp MatlabCode -> SValue MatlabCode -> SValue MatlabCode
forall (r :: * -> *).
(OpElim r, RenderValue r, ValueElim r, ValueSym r) =>
VSUnOp r -> SValue r -> SValue r
unExpr VSUnOp MatlabCode
forall (r :: * -> *). UnaryOpSym r => VSUnOp r
lnOp
  exp :: SValue MatlabCode -> SValue MatlabCode
exp = VSUnOp MatlabCode -> SValue MatlabCode -> SValue MatlabCode
forall (r :: * -> *).
(OpElim r, RenderValue r, ValueElim r, ValueSym r) =>
VSUnOp r -> SValue r -> SValue r
unExpr VSUnOp MatlabCode
forall (r :: * -> *). UnaryOpSym r => VSUnOp r
expOp
  sin :: SValue MatlabCode -> SValue MatlabCode
sin = VSUnOp MatlabCode -> SValue MatlabCode -> SValue MatlabCode
forall (r :: * -> *).
(OpElim r, RenderValue r, ValueElim r, ValueSym r) =>
VSUnOp r -> SValue r -> SValue r
unExpr VSUnOp MatlabCode
forall (r :: * -> *). UnaryOpSym r => VSUnOp r
sinOp
  cos :: SValue MatlabCode -> SValue MatlabCode
cos = VSUnOp MatlabCode -> SValue MatlabCode -> SValue MatlabCode
forall (r :: * -> *).
(OpElim r, RenderValue r, ValueElim r, ValueSym r) =>
VSUnOp r -> SValue r -> SValue r
unExpr VSUnOp MatlabCode
forall (r :: * -> *). UnaryOpSym r => VSUnOp r
cosOp
  tan :: SValue MatlabCode -> SValue MatlabCode
tan = VSUnOp MatlabCode -> SValue MatlabCode -> SValue MatlabCode
forall (r :: * -> *).
(OpElim r, RenderValue r, ValueElim r, ValueSym r) =>
VSUnOp r -> SValue r -> SValue r
unExpr VSUnOp MatlabCode
forall (r :: * -> *). UnaryOpSym r => VSUnOp r
tanOp
  csc :: SValue MatlabCode -> SValue MatlabCode
csc = SValue MatlabCode -> SValue MatlabCode
forall (r :: * -> *).
(Literal r, NumericExpression r, TypeElim r) =>
SValue r -> SValue r
G.csc
  sec :: SValue MatlabCode -> SValue MatlabCode
sec = SValue MatlabCode -> SValue MatlabCode
forall (r :: * -> *).
(Literal r, NumericExpression r, TypeElim r) =>
SValue r -> SValue r
G.sec
  cot :: SValue MatlabCode -> SValue MatlabCode
cot = SValue MatlabCode -> SValue MatlabCode
forall (r :: * -> *).
(Literal r, NumericExpression r, TypeElim r) =>
SValue r -> SValue r
G.cot
  arcsin :: SValue MatlabCode -> SValue MatlabCode
arcsin = VSUnOp MatlabCode -> SValue MatlabCode -> SValue MatlabCode
forall (r :: * -> *).
(OpElim r, RenderValue r, ValueElim r, ValueSym r) =>
VSUnOp r -> SValue r -> SValue r
unExpr VSUnOp MatlabCode
forall (r :: * -> *). UnaryOpSym r => VSUnOp r
asinOp
  arccos :: SValue MatlabCode -> SValue MatlabCode
arccos = VSUnOp MatlabCode -> SValue MatlabCode -> SValue MatlabCode
forall (r :: * -> *).
(OpElim r, RenderValue r, ValueElim r, ValueSym r) =>
VSUnOp r -> SValue r -> SValue r
unExpr VSUnOp MatlabCode
forall (r :: * -> *). UnaryOpSym r => VSUnOp r
acosOp
  arctan :: SValue MatlabCode -> SValue MatlabCode
arctan = VSUnOp MatlabCode -> SValue MatlabCode -> SValue MatlabCode
forall (r :: * -> *).
(OpElim r, RenderValue r, ValueElim r, ValueSym r) =>
VSUnOp r -> SValue r -> SValue r
unExpr VSUnOp MatlabCode
forall (r :: * -> *). UnaryOpSym r => VSUnOp r
atanOp
  floor :: SValue MatlabCode -> SValue MatlabCode
floor = VSUnOp MatlabCode -> SValue MatlabCode -> SValue MatlabCode
forall (r :: * -> *).
(OpElim r, RenderValue r, ValueElim r, ValueSym r) =>
VSUnOp r -> SValue r -> SValue r
unExpr VSUnOp MatlabCode
forall (r :: * -> *). UnaryOpSym r => VSUnOp r
floorOp
  ceil :: SValue MatlabCode -> SValue MatlabCode
ceil = VSUnOp MatlabCode -> SValue MatlabCode -> SValue MatlabCode
forall (r :: * -> *).
(OpElim r, RenderValue r, ValueElim r, ValueSym r) =>
VSUnOp r -> SValue r -> SValue r
unExpr VSUnOp MatlabCode
forall (r :: * -> *). UnaryOpSym r => VSUnOp r
ceilOp

instance BooleanExpression MatlabCode where
  ?! :: SValue MatlabCode -> SValue MatlabCode
(?!) = VSUnOp MatlabCode
-> VS (MatlabCode TypeData)
-> SValue MatlabCode
-> SValue MatlabCode
forall (r :: * -> *).
(OpElim r, RenderValue r, ValueElim r) =>
VSUnOp r -> VS (r TypeData) -> SValue r -> SValue r
typeUnExpr VSUnOp MatlabCode
forall (r :: * -> *). UnaryOpSym r => VSUnOp r
notOp VS (MatlabCode TypeData)
forall (r :: * -> *). TypeSym r => VS (r TypeData)
bool
  ?&& :: SValue MatlabCode -> SValue MatlabCode -> SValue MatlabCode
(?&&) = VSUnOp MatlabCode
-> VS (MatlabCode TypeData)
-> SValue MatlabCode
-> SValue MatlabCode
-> SValue MatlabCode
forall (r :: * -> *).
(OpElim r, RenderValue r, ValueElim r) =>
VSBinOp r -> VS (r TypeData) -> SValue r -> SValue r -> SValue r
typeBinExpr VSUnOp MatlabCode
forall (r :: * -> *). BinaryOpSym r => VSBinOp r
andOp VS (MatlabCode TypeData)
forall (r :: * -> *). TypeSym r => VS (r TypeData)
bool
  ?|| :: SValue MatlabCode -> SValue MatlabCode -> SValue MatlabCode
(?||) = VSUnOp MatlabCode
-> VS (MatlabCode TypeData)
-> SValue MatlabCode
-> SValue MatlabCode
-> SValue MatlabCode
forall (r :: * -> *).
(OpElim r, RenderValue r, ValueElim r) =>
VSBinOp r -> VS (r TypeData) -> SValue r -> SValue r -> SValue r
typeBinExpr VSUnOp MatlabCode
forall (r :: * -> *). BinaryOpSym r => VSBinOp r
orOp VS (MatlabCode TypeData)
forall (r :: * -> *). TypeSym r => VS (r TypeData)
bool

instance Comparison MatlabCode where
  ?< :: SValue MatlabCode -> SValue MatlabCode -> SValue MatlabCode
(?<) = VSUnOp MatlabCode
-> VS (MatlabCode TypeData)
-> SValue MatlabCode
-> SValue MatlabCode
-> SValue MatlabCode
forall (r :: * -> *).
(OpElim r, RenderValue r, ValueElim r) =>
VSBinOp r -> VS (r TypeData) -> SValue r -> SValue r -> SValue r
typeBinExpr VSUnOp MatlabCode
forall (r :: * -> *). BinaryOpSym r => VSBinOp r
lessOp VS (MatlabCode TypeData)
forall (r :: * -> *). TypeSym r => VS (r TypeData)
bool
  ?<= :: SValue MatlabCode -> SValue MatlabCode -> SValue MatlabCode
(?<=) = VSUnOp MatlabCode
-> VS (MatlabCode TypeData)
-> SValue MatlabCode
-> SValue MatlabCode
-> SValue MatlabCode
forall (r :: * -> *).
(OpElim r, RenderValue r, ValueElim r) =>
VSBinOp r -> VS (r TypeData) -> SValue r -> SValue r -> SValue r
typeBinExpr VSUnOp MatlabCode
forall (r :: * -> *). BinaryOpSym r => VSBinOp r
lessEqualOp VS (MatlabCode TypeData)
forall (r :: * -> *). TypeSym r => VS (r TypeData)
bool
  ?> :: SValue MatlabCode -> SValue MatlabCode -> SValue MatlabCode
(?>) = VSUnOp MatlabCode
-> VS (MatlabCode TypeData)
-> SValue MatlabCode
-> SValue MatlabCode
-> SValue MatlabCode
forall (r :: * -> *).
(OpElim r, RenderValue r, ValueElim r) =>
VSBinOp r -> VS (r TypeData) -> SValue r -> SValue r -> SValue r
typeBinExpr VSUnOp MatlabCode
forall (r :: * -> *). BinaryOpSym r => VSBinOp r
greaterOp VS (MatlabCode TypeData)
forall (r :: * -> *). TypeSym r => VS (r TypeData)
bool
  ?>= :: SValue MatlabCode -> SValue MatlabCode -> SValue MatlabCode
(?>=) = VSUnOp MatlabCode
-> VS (MatlabCode TypeData)
-> SValue MatlabCode
-> SValue MatlabCode
-> SValue MatlabCode
forall (r :: * -> *).
(OpElim r, RenderValue r, ValueElim r) =>
VSBinOp r -> VS (r TypeData) -> SValue r -> SValue r -> SValue r
typeBinExpr VSUnOp MatlabCode
forall (r :: * -> *). BinaryOpSym r => VSBinOp r
greaterEqualOp VS (MatlabCode TypeData)
forall (r :: * -> *). TypeSym r => VS (r TypeData)
bool
  ?== :: SValue MatlabCode -> SValue MatlabCode -> SValue MatlabCode
(?==) = Bool -> SValue MatlabCode -> SValue MatlabCode -> SValue MatlabCode
mlEqOp Bool
False
  ?!= :: SValue MatlabCode -> SValue MatlabCode -> SValue MatlabCode
(?!=) = Bool -> SValue MatlabCode -> SValue MatlabCode -> SValue MatlabCode
mlEqOp Bool
True

instance ValueExpression MatlabCode where
  inlineIf :: SValue MatlabCode
-> SValue MatlabCode -> SValue MatlabCode -> SValue MatlabCode
inlineIf = SValue MatlabCode
-> SValue MatlabCode -> SValue MatlabCode -> SValue MatlabCode
mlInlineIf
  funcAppMixedArgs :: MixedCall MatlabCode
funcAppMixedArgs = MixedCall MatlabCode
forall (r :: * -> *). RenderValue r => MixedCall r
G.funcAppMixedArgs
  extFuncAppMixedArgs :: Label -> MixedCall MatlabCode
extFuncAppMixedArgs Label
_ = MixedCall MatlabCode
forall (r :: * -> *). RenderValue r => MixedCall r
G.funcAppMixedArgs
  libFuncAppMixedArgs :: Label -> MixedCall MatlabCode
libFuncAppMixedArgs Label
_ = MixedCall MatlabCode
forall (r :: * -> *). RenderValue r => MixedCall r
G.funcAppMixedArgs
  lambda :: [VSBinder MatlabCode] -> SValue MatlabCode -> SValue MatlabCode
lambda = [VSBinder MatlabCode] -> SValue MatlabCode -> SValue MatlabCode
forall a. HasCallStack => a
undefined
  notNull :: SValue MatlabCode -> SValue MatlabCode
notNull SValue MatlabCode
v = SValue MatlabCode -> SValue MatlabCode
forall (r :: * -> *). BooleanExpression r => SValue r -> SValue r
(?!) (SValue MatlabCode -> SValue MatlabCode)
-> SValue MatlabCode -> SValue MatlabCode
forall a b. (a -> b) -> a -> b
$ PosCall MatlabCode
forall (r :: * -> *). ValueExpression r => PosCall r
funcApp Label
"isempty" VS (MatlabCode TypeData)
forall (r :: * -> *). TypeSym r => VS (r TypeData)
bool [SValue MatlabCode
v]

instance RenderValue MatlabCode where
  inputFunc :: SValue MatlabCode
inputFunc = PosCall MatlabCode
forall (r :: * -> *). ValueExpression r => PosCall r
funcApp Label
"input" VS (MatlabCode TypeData)
forall (r :: * -> *). TypeSym r => VS (r TypeData)
string [Label -> SValue MatlabCode
forall (r :: * -> *). Literal r => Label -> SValue r
litString Label
"", Label -> SValue MatlabCode
forall (r :: * -> *). Literal r => Label -> SValue r
litString Label
"s"]
  printFunc :: SValue MatlabCode
printFunc = SValue MatlabCode
mlPrintFunc
  printLnFunc :: SValue MatlabCode
printLnFunc = SValue MatlabCode
mlPrintFunc
  printFileFunc :: SValue MatlabCode -> SValue MatlabCode
printFileFunc SValue MatlabCode
_ = SValue MatlabCode
mlPrintFunc
  printFileLnFunc :: SValue MatlabCode -> SValue MatlabCode
printFileLnFunc SValue MatlabCode
_ = SValue MatlabCode
mlPrintFunc
  cast :: VS (MatlabCode TypeData) -> SValue MatlabCode -> SValue MatlabCode
cast = VS (MatlabCode TypeData) -> SValue MatlabCode -> SValue MatlabCode
mlCast
  call :: Maybe Label -> Maybe Doc -> MixedCall MatlabCode
call = Doc -> Maybe Label -> Maybe Doc -> MixedCall MatlabCode
forall (r :: * -> *).
(InternalVarElim r, RenderValue r, ValueElim r) =>
Doc -> Maybe Label -> Maybe Doc -> MixedCall r
G.call Doc
equals
  valFromData :: Maybe Int
-> Maybe Integer
-> VS (MatlabCode TypeData)
-> Doc
-> SValue MatlabCode
valFromData Maybe Int
p Maybe Integer
i VS (MatlabCode TypeData)
t' Doc
d = do
    t <- VS (MatlabCode TypeData)
t'
    toState $ on2CodeValues (vd p i) t (toCode d)

instance ValueElim MatlabCode where
  valuePrec :: MatlabCode Value -> Maybe Int
valuePrec = Value -> Maybe Int
valPrec (Value -> Maybe Int)
-> (MatlabCode Value -> Value) -> MatlabCode Value -> Maybe Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. MatlabCode Value -> Value
forall a. MatlabCode a -> a
unMLC
  valueInt :: MatlabCode Value -> Maybe Integer
valueInt = Value -> Maybe Integer
valInt (Value -> Maybe Integer)
-> (MatlabCode Value -> Value) -> MatlabCode Value -> Maybe Integer
forall b c a. (b -> c) -> (a -> b) -> a -> c
. MatlabCode Value -> Value
forall a. MatlabCode a -> a
unMLC
  value :: MatlabCode Value -> Doc
value = Value -> Doc
val (Value -> Doc)
-> (MatlabCode Value -> Value) -> MatlabCode Value -> Doc
forall b c a. (b -> c) -> (a -> b) -> a -> c
. MatlabCode Value -> Value
forall a. MatlabCode a -> a
unMLC

instance IndexTranslator MatlabCode where
  intToIndex :: SValue MatlabCode -> SValue MatlabCode
intToIndex = SValue MatlabCode -> SValue MatlabCode
forall (r :: * -> *).
(Literal r, NumericExpression r, RenderValue r, ValueElim r) =>
SValue r -> SValue r
CP.intToIndex'
  indexToInt :: SValue MatlabCode -> SValue MatlabCode
indexToInt = SValue MatlabCode -> SValue MatlabCode
forall (r :: * -> *).
(Literal r, NumericExpression r, RenderValue r, ValueElim r) =>
SValue r -> SValue r
CP.indexToInt'

instance Reference MatlabCode where
  makeRef :: SValue MatlabCode -> SValue MatlabCode
makeRef = SValue MatlabCode -> SValue MatlabCode
forall a. a -> a
id
  maybeDeref :: SValue MatlabCode -> SValue MatlabCode
maybeDeref = SValue MatlabCode -> SValue MatlabCode
forall a. a -> a
id

instance Array MatlabCode where
  arrayElem :: SValue MatlabCode -> SValue MatlabCode -> SVariable MatlabCode
arrayElem = SValue MatlabCode -> SValue MatlabCode -> SVariable MatlabCode
mlArrayElem
  arrayLength :: SValue MatlabCode -> SValue MatlabCode
arrayLength = SValue MatlabCode -> SValue MatlabCode
forall (r :: * -> *). List r => SValue r -> SValue r
listSize
  arrayCopy :: SValue MatlabCode -> SValue MatlabCode
arrayCopy = SValue MatlabCode -> SValue MatlabCode
forall a. a -> a
id

instance List MatlabCode where
  listSize :: SValue MatlabCode -> SValue MatlabCode
listSize = Label -> SValue MatlabCode -> SValue MatlabCode
forall (r :: * -> *).
ValueExpression r =>
Label -> SValue r -> SValue r
CS.listSize Label
"length"   -- length(v)
  listAccess :: SValue MatlabCode -> SValue MatlabCode -> SValue MatlabCode
listAccess = SValue MatlabCode -> SValue MatlabCode -> SValue MatlabCode
forall (r :: * -> *).
(IndexTranslator r, InternalListFunc r, FunctionElim r,
 RenderFunction r, RenderValue r, TypeElim r, ValueElim r) =>
SValue r -> SValue r -> SValue r
G.listAccess
  indexOf :: SValue MatlabCode -> SValue MatlabCode -> SValue MatlabCode
indexOf SValue MatlabCode
lst SValue MatlabCode
v = PosCall MatlabCode
forall (r :: * -> *). ValueExpression r => PosCall r
funcApp Label
"find" VS (MatlabCode TypeData)
forall (r :: * -> *). TypeSym r => VS (r TypeData)
int [SValue MatlabCode
lst SValue MatlabCode -> SValue MatlabCode -> SValue MatlabCode
forall (r :: * -> *).
Comparison r =>
SValue r -> SValue r -> SValue r
?== SValue MatlabCode
v, Integer -> SValue MatlabCode
forall (r :: * -> *). Literal r => Integer -> SValue r
litInt Integer
1] SValue MatlabCode -> SValue MatlabCode -> SValue MatlabCode
forall (r :: * -> *).
NumericExpression r =>
SValue r -> SValue r -> SValue r
#- Integer -> SValue MatlabCode
forall (r :: * -> *). Literal r => Integer -> SValue r
litInt Integer
1

instance ListStatement MatlabCode (Doc, Terminator) where
  listAdd :: SValue MatlabCode
-> SValue MatlabCode
-> SValue MatlabCode
-> MS (MatlabCode (Doc, Terminator))
listAdd = SValue MatlabCode
-> SValue MatlabCode
-> SValue MatlabCode
-> MS (MatlabCode (Doc, Terminator))
mlListAdd
  listAppend :: SValue MatlabCode
-> SValue MatlabCode -> MS (MatlabCode (Doc, Terminator))
listAppend SValue MatlabCode
lst = SValue MatlabCode
-> SValue MatlabCode
-> SValue MatlabCode
-> MS (MatlabCode (Doc, Terminator))
forall (r :: * -> *) stmt.
ListStatement r stmt =>
SValue r -> SValue r -> SValue r -> MS (r stmt)
listSet SValue MatlabCode
lst (SValue MatlabCode -> SValue MatlabCode
forall (r :: * -> *). List r => SValue r -> SValue r
listSize SValue MatlabCode
lst)
  listSet :: SValue MatlabCode
-> SValue MatlabCode
-> SValue MatlabCode
-> MS (MatlabCode (Doc, Terminator))
listSet = SValue MatlabCode
-> SValue MatlabCode
-> SValue MatlabCode
-> MS (MatlabCode (Doc, Terminator))
mlListSet

instance Set MatlabCode where
  contains :: SValue MatlabCode -> SValue MatlabCode -> SValue MatlabCode
contains SValue MatlabCode
s SValue MatlabCode
e = PosCall MatlabCode
forall (r :: * -> *). ValueExpression r => PosCall r
funcApp Label
"ismember" VS (MatlabCode TypeData)
forall (r :: * -> *). TypeSym r => VS (r TypeData)
bool [SValue MatlabCode
e, SValue MatlabCode
s]
  setAdd :: SValue MatlabCode -> SValue MatlabCode -> SValue MatlabCode
setAdd = SValue MatlabCode -> SValue MatlabCode -> SValue MatlabCode
forall a. HasCallStack => a
undefined
  setRemove :: SValue MatlabCode -> SValue MatlabCode -> SValue MatlabCode
setRemove = SValue MatlabCode -> SValue MatlabCode -> SValue MatlabCode
forall a. HasCallStack => a
undefined
  setUnion :: SValue MatlabCode -> SValue MatlabCode -> SValue MatlabCode
setUnion = SValue MatlabCode -> SValue MatlabCode -> SValue MatlabCode
forall a. HasCallStack => a
undefined

instance NativeVector MatlabCode where
  vecScale :: SValue MatlabCode -> SValue MatlabCode -> SValue MatlabCode
vecScale = VSUnOp MatlabCode
-> SValue MatlabCode -> SValue MatlabCode -> SValue MatlabCode
forall (r :: * -> *).
(OpElim r, RenderValue r, TypeElim r, ValueElim r, ValueSym r) =>
VSBinOp r -> SValue r -> SValue r -> SValue r
binExpr VSUnOp MatlabCode
forall (r :: * -> *). BinaryOpSym r => VSBinOp r
multOp           -- s * v
  vecAdd :: SValue MatlabCode -> SValue MatlabCode -> SValue MatlabCode
vecAdd   = VSUnOp MatlabCode
-> SValue MatlabCode -> SValue MatlabCode -> SValue MatlabCode
forall (r :: * -> *).
(OpElim r, RenderValue r, TypeElim r, ValueElim r, ValueSym r) =>
VSBinOp r -> SValue r -> SValue r -> SValue r
binExpr VSUnOp MatlabCode
forall (r :: * -> *). BinaryOpSym r => VSBinOp r
plusOp           -- a + b
  vecIndex :: SValue MatlabCode -> SValue MatlabCode -> SValue MatlabCode
vecIndex = SValue MatlabCode -> SValue MatlabCode -> SValue MatlabCode
mlVecIndex               -- a(1) / a(i + 1)
  vecDot :: SValue MatlabCode -> SValue MatlabCode -> SValue MatlabCode
vecDot SValue MatlabCode
a SValue MatlabCode
b = PosCall MatlabCode
forall (r :: * -> *). ValueExpression r => PosCall r
funcApp Label
"dot" VS (MatlabCode TypeData)
forall (r :: * -> *). TypeSym r => VS (r TypeData)
double [SValue MatlabCode
a, SValue MatlabCode
b]   -- dot(a, b)
  vecMag :: SValue MatlabCode -> SValue MatlabCode
vecMag SValue MatlabCode
a = PosCall MatlabCode
forall (r :: * -> *). ValueExpression r => PosCall r
funcApp Label
"norm" VS (MatlabCode TypeData)
forall (r :: * -> *). TypeSym r => VS (r TypeData)
double [SValue MatlabCode
a]       -- norm(a)
  vecUnit :: SValue MatlabCode -> SValue MatlabCode
vecUnit SValue MatlabCode
a = SValue MatlabCode
a SValue MatlabCode -> SValue MatlabCode -> SValue MatlabCode
forall (r :: * -> *).
NumericExpression r =>
SValue r -> SValue r -> SValue r
#/ SValue MatlabCode -> SValue MatlabCode
forall (r :: * -> *). NativeVector r => SValue r -> SValue r
vecMag SValue MatlabCode
a                  -- a / norm(a)

instance InternalList MatlabCode Block where
  listSlice' :: Maybe (SValue MatlabCode)
-> Maybe (SValue MatlabCode)
-> Maybe (SValue MatlabCode)
-> SVariable MatlabCode
-> SValue MatlabCode
-> MS (MatlabCode Doc)
listSlice' = Maybe (SValue MatlabCode)
-> Maybe (SValue MatlabCode)
-> Maybe (SValue MatlabCode)
-> SVariable MatlabCode
-> SValue MatlabCode
-> MS (MatlabCode Doc)
forall (r :: * -> *) bod block stmt.
(BodySym r bod block, BlockSym r block stmt, EmptyStatement r stmt,
 DeclStatement r stmt bod, AssignStatement r stmt,
 ControlStatement r stmt bod, Literal r, BooleanExpression r,
 Comparison r, NumericExpression r, ValueExpression r,
 VariableValue r, List r, ListStatement r stmt, ValueElim r,
 VariableElim r) =>
Maybe (SValue r)
-> Maybe (SValue r)
-> Maybe (SValue r)
-> SVariable r
-> SValue r
-> MS (r block)
M.listSlice

instance InternalListFunc MatlabCode where
  listAccessFunc :: VS (MatlabCode TypeData)
-> SValue MatlabCode -> VS (MatlabCode FuncData)
listAccessFunc VS (MatlabCode TypeData)
t SValue MatlabCode
v = do
    t' <- VS (MatlabCode TypeData)
t
    iv <- intValue v
    funcFromData (mlListAccessFunc (cType (unMLC t')) iv) (return t')

mlListAccessFunc :: CodeType -> MatlabCode Value -> Doc
mlListAccessFunc :: CodeType -> MatlabCode Value -> Doc
mlListAccessFunc CodeType
ct MatlabCode Value
v = CodeType -> Doc -> Doc
mlCellWrap CodeType
ct (Doc -> Doc) -> Doc -> Doc
forall a b. (a -> b) -> a -> b
$ MatlabCode Value -> Doc
forall (r :: * -> *). ValueElim r => r Value -> Doc
RC.value MatlabCode Value
v

mlCellWrap :: CodeType -> Doc -> Doc
mlCellWrap :: CodeType -> Doc -> Doc
mlCellWrap CodeType
String = Doc -> Doc
braces
mlCellWrap CodeType
_      = Doc -> Doc
parens

instance BinderSym MatlabCode where
  binder :: Label -> VS (MatlabCode TypeData) -> VSBinder MatlabCode
binder = Label -> VS (MatlabCode TypeData) -> VSBinder MatlabCode
forall a. HasCallStack => a
undefined

instance BinderElim MatlabCode where
  binderName :: MatlabCode BinderD -> Label
binderName = MatlabCode BinderD -> Label
forall a. HasCallStack => a
undefined
  binderType :: MatlabCode BinderD -> MatlabCode TypeData
binderType = MatlabCode BinderD -> MatlabCode TypeData
forall a. HasCallStack => a
undefined

instance InternalBinderElim MatlabCode where
  binderElim :: MatlabCode BinderD -> Doc
binderElim = MatlabCode BinderD -> Doc
forall a. HasCallStack => a
undefined

instance RenderFunction MatlabCode where
  funcFromData :: Doc -> VS (MatlabCode TypeData) -> VS (MatlabCode FuncData)
funcFromData Doc
d = (MatlabCode TypeData -> MatlabCode FuncData)
-> VS (MatlabCode TypeData) -> VS (MatlabCode FuncData)
forall a b s. (a -> b) -> State s a -> State s b
onStateValue ((MatlabCode TypeData -> MatlabCode FuncData)
 -> VS (MatlabCode TypeData) -> VS (MatlabCode FuncData))
-> (MatlabCode TypeData -> MatlabCode FuncData)
-> VS (MatlabCode TypeData)
-> VS (MatlabCode FuncData)
forall a b. (a -> b) -> a -> b
$ (TypeData -> FuncData)
-> MatlabCode TypeData -> MatlabCode FuncData
forall (r :: * -> *) a b. Functor r => (a -> b) -> r a -> r b
onCodeValue (TypeData -> Doc -> FuncData
`fd` Doc
d)

instance FunctionElim MatlabCode where
  functionType :: MatlabCode FuncData -> MatlabCode TypeData
functionType = (FuncData -> TypeData)
-> MatlabCode FuncData -> MatlabCode TypeData
forall (r :: * -> *) a b. Functor r => (a -> b) -> r a -> r b
onCodeValue FuncData -> TypeData
fType
  function :: MatlabCode FuncData -> Doc
function = FuncData -> Doc
funcDoc (FuncData -> Doc)
-> (MatlabCode FuncData -> FuncData) -> MatlabCode FuncData -> Doc
forall b c a. (b -> c) -> (a -> b) -> a -> c
. MatlabCode FuncData -> FuncData
forall a. MatlabCode a -> a
unMLC

instance InternalAssignStmt MatlabCode (Doc, Terminator) where
  multiAssign :: [SVariable MatlabCode]
-> [SValue MatlabCode] -> MS (MatlabCode (Doc, Terminator))
multiAssign = (Doc -> Doc)
-> [SVariable MatlabCode]
-> [SValue MatlabCode]
-> MS (MatlabCode (Doc, Terminator))
forall (r :: * -> *) stmt.
(AssignStatement r stmt, InternalVarElim r, RenderValue r,
 RenderVariable r, ValueElim r) =>
(Doc -> Doc) -> [SVariable r] -> [SValue r] -> MS (r stmt)
CP.multiAssign Doc -> Doc
brackets

instance InternalIOStmt MatlabCode (Doc, Terminator) where
  printSt :: Bool
-> Maybe (SValue MatlabCode)
-> SValue MatlabCode
-> SValue MatlabCode
-> MS (MatlabCode (Doc, Terminator))
printSt = Bool
-> Maybe (SValue MatlabCode)
-> SValue MatlabCode
-> SValue MatlabCode
-> MS (MatlabCode (Doc, Terminator))
mlPrint

instance InternalControlStmt MatlabCode (Doc, Terminator) where
  multiReturn :: [SValue MatlabCode] -> MS (MatlabCode (Doc, Terminator))
multiReturn = [SValue MatlabCode] -> MS (MatlabCode (Doc, Terminator))
forall a. HasCallStack => a
undefined

instance RenderStatement MatlabCode (Doc, Terminator) where
  stmt :: MS (MatlabCode (Doc, Terminator))
-> MS (MatlabCode (Doc, Terminator))
stmt = MS (MatlabCode (Doc, Terminator))
-> MS (MatlabCode (Doc, Terminator))
forall {k} (r :: k -> *) (stmt :: k).
(RenderStatement r stmt, StatementElim r stmt) =>
MS (r stmt) -> MS (r stmt)
G.stmt
  loopStmt :: MS (MatlabCode (Doc, Terminator))
-> MS (MatlabCode (Doc, Terminator))
loopStmt = MS (MatlabCode (Doc, Terminator))
-> MS (MatlabCode (Doc, Terminator))
forall {k} (r :: k -> *) (stmt :: k).
(RenderStatement r stmt, StatementElim r stmt) =>
MS (r stmt) -> MS (r stmt)
G.loopStmt
  stmtFromData :: Doc -> Terminator -> MS (MatlabCode (Doc, Terminator))
stmtFromData Doc
d Terminator
t = MatlabCode (Doc, Terminator) -> MS (MatlabCode (Doc, Terminator))
forall a s. a -> State s a
toState (MatlabCode (Doc, Terminator) -> MS (MatlabCode (Doc, Terminator)))
-> MatlabCode (Doc, Terminator)
-> MS (MatlabCode (Doc, Terminator))
forall a b. (a -> b) -> a -> b
$ (Doc, Terminator) -> MatlabCode (Doc, Terminator)
forall (r :: * -> *) a. Monad r => a -> r a
toCode (Doc
d, Terminator
t)

instance StatementElim MatlabCode (Doc, Terminator) where
  statement :: MatlabCode (Doc, Terminator) -> Doc
statement = (Doc, Terminator) -> Doc
forall a b. (a, b) -> a
fst ((Doc, Terminator) -> Doc)
-> (MatlabCode (Doc, Terminator) -> (Doc, Terminator))
-> MatlabCode (Doc, Terminator)
-> Doc
forall b c a. (b -> c) -> (a -> b) -> a -> c
. MatlabCode (Doc, Terminator) -> (Doc, Terminator)
forall a. MatlabCode a -> a
unMLC
  statementTerm :: MatlabCode (Doc, Terminator) -> Terminator
statementTerm = (Doc, Terminator) -> Terminator
forall a b. (a, b) -> b
snd ((Doc, Terminator) -> Terminator)
-> (MatlabCode (Doc, Terminator) -> (Doc, Terminator))
-> MatlabCode (Doc, Terminator)
-> Terminator
forall b c a. (b -> c) -> (a -> b) -> a -> c
. MatlabCode (Doc, Terminator) -> (Doc, Terminator)
forall a. MatlabCode a -> a
unMLC

instance EmptyStatement MatlabCode (Doc, Terminator) where
  emptyStmt :: MS (MatlabCode (Doc, Terminator))
emptyStmt = MS (MatlabCode (Doc, Terminator))
forall {k} (r :: k -> *) (stmt :: k).
RenderStatement r stmt =>
MS (r stmt)
G.emptyStmt

instance MultiStatement MatlabCode (Doc, Terminator) where
  multi :: [MS (MatlabCode (Doc, Terminator))]
-> MS (MatlabCode (Doc, Terminator))
multi = ([MatlabCode (Doc, Terminator)] -> MatlabCode (Doc, Terminator))
-> [MS (MatlabCode (Doc, Terminator))]
-> MS (MatlabCode (Doc, Terminator))
forall a b s. ([a] -> b) -> [State s a] -> State s b
onStateList (([(Doc, Terminator)] -> (Doc, Terminator))
-> [MatlabCode (Doc, Terminator)] -> MatlabCode (Doc, Terminator)
forall (m :: * -> *) a b. Monad m => ([a] -> b) -> [m a] -> m b
onCodeList [(Doc, Terminator)] -> (Doc, Terminator)
R.multiStmt)

instance ValueStatement MatlabCode (Doc, Terminator) where
  valStmt :: SValue MatlabCode -> MS (MatlabCode (Doc, Terminator))
valStmt = Terminator
-> SValue MatlabCode -> MS (MatlabCode (Doc, Terminator))
forall (r :: * -> *) stmt.
(RenderStatement r stmt, ValueElim r) =>
Terminator -> SValue r -> MS (r stmt)
G.valStmt Terminator
Semi

instance AssignStatement MatlabCode (Doc, Terminator) where
  assign :: SVariable MatlabCode
-> SValue MatlabCode -> MS (MatlabCode (Doc, Terminator))
assign = Terminator
-> SVariable MatlabCode
-> SValue MatlabCode
-> MS (MatlabCode (Doc, Terminator))
forall (r :: * -> *) stmt.
(InternalVarElim r, RenderStatement r stmt, ValueElim r) =>
Terminator -> SVariable r -> SValue r -> MS (r stmt)
G.assign Terminator
Semi
  &-= :: SVariable MatlabCode
-> SValue MatlabCode -> MS (MatlabCode (Doc, Terminator))
(&-=) SVariable MatlabCode
vr SValue MatlabCode
v = SVariable MatlabCode
vr SVariable MatlabCode
-> SValue MatlabCode -> MS (MatlabCode (Doc, Terminator))
forall (r :: * -> *) stmt.
AssignStatement r stmt =>
SVariable r -> SValue r -> MS (r stmt)
&= (SVariable MatlabCode -> SValue MatlabCode
forall (r :: * -> *). VariableValue r => SVariable r -> SValue r
valueOf SVariable MatlabCode
vr SValue MatlabCode -> SValue MatlabCode -> SValue MatlabCode
forall (r :: * -> *).
NumericExpression r =>
SValue r -> SValue r -> SValue r
#- SValue MatlabCode
v)
  &+= :: SVariable MatlabCode
-> SValue MatlabCode -> MS (MatlabCode (Doc, Terminator))
(&+=) SVariable MatlabCode
vr SValue MatlabCode
v = SVariable MatlabCode
vr SVariable MatlabCode
-> SValue MatlabCode -> MS (MatlabCode (Doc, Terminator))
forall (r :: * -> *) stmt.
AssignStatement r stmt =>
SVariable r -> SValue r -> MS (r stmt)
&= SValue MatlabCode -> SValue MatlabCode -> SValue MatlabCode
forall (r :: * -> *).
(Literal r, NumericExpression r, RenderValue r, ValueElim r) =>
SValue r -> SValue r -> SValue r
G.smartAdd (SVariable MatlabCode -> SValue MatlabCode
forall (r :: * -> *). VariableValue r => SVariable r -> SValue r
valueOf SVariable MatlabCode
vr) SValue MatlabCode
v
  &++ :: SVariable MatlabCode -> MS (MatlabCode (Doc, Terminator))
(&++) = SVariable MatlabCode -> MS (MatlabCode (Doc, Terminator))
forall (r :: * -> *) stmt.
(AssignStatement r stmt, Literal r) =>
SVariable r -> MS (r stmt)
M.increment1
  &-- :: SVariable MatlabCode -> MS (MatlabCode (Doc, Terminator))
(&--) = SVariable MatlabCode -> MS (MatlabCode (Doc, Terminator))
forall (r :: * -> *) stmt.
(AssignStatement r stmt, Literal r) =>
SVariable r -> MS (r stmt)
M.decrement1

instance DeclStatement MatlabCode (Doc, Terminator) Body where
  varDec :: SVariable MatlabCode
-> MatlabCode ScopeData -> MS (MatlabCode (Doc, Terminator))
varDec SVariable MatlabCode
v MatlabCode ScopeData
scp = SVariable MatlabCode
-> MatlabCode ScopeData
-> Maybe (SValue MatlabCode)
-> MS (MatlabCode (Doc, Terminator))
forall (r :: * -> *) stmt.
(EmptyStatement r stmt, AssignStatement r stmt, ScopeElim r,
 VariableElim r) =>
SVariable r -> r ScopeData -> Maybe (SValue r) -> MS (r stmt)
CS.varDecDef SVariable MatlabCode
v MatlabCode ScopeData
scp Maybe (SValue MatlabCode)
forall a. Maybe a
Nothing
  varDecDef :: SVariable MatlabCode
-> MatlabCode ScopeData
-> SValue MatlabCode
-> MS (MatlabCode (Doc, Terminator))
varDecDef SVariable MatlabCode
v MatlabCode ScopeData
scp SValue MatlabCode
e = SVariable MatlabCode
-> MatlabCode ScopeData
-> Maybe (SValue MatlabCode)
-> MS (MatlabCode (Doc, Terminator))
forall (r :: * -> *) stmt.
(EmptyStatement r stmt, AssignStatement r stmt, ScopeElim r,
 VariableElim r) =>
SVariable r -> r ScopeData -> Maybe (SValue r) -> MS (r stmt)
CS.varDecDef SVariable MatlabCode
v MatlabCode ScopeData
scp (SValue MatlabCode -> Maybe (SValue MatlabCode)
forall a. a -> Maybe a
Just SValue MatlabCode
e)
  setDec :: SVariable MatlabCode
-> MatlabCode ScopeData -> MS (MatlabCode (Doc, Terminator))
setDec = SVariable MatlabCode
-> MatlabCode ScopeData -> MS (MatlabCode (Doc, Terminator))
forall (r :: * -> *) stmt bod.
DeclStatement r stmt bod =>
SVariable r -> r ScopeData -> MS (r stmt)
varDec
  setDecDef :: SVariable MatlabCode
-> MatlabCode ScopeData
-> SValue MatlabCode
-> MS (MatlabCode (Doc, Terminator))
setDecDef = SVariable MatlabCode
-> MatlabCode ScopeData
-> SValue MatlabCode
-> MS (MatlabCode (Doc, Terminator))
forall (r :: * -> *) stmt bod.
DeclStatement r stmt bod =>
SVariable r -> r ScopeData -> SValue r -> MS (r stmt)
varDecDef
  listDec :: Integer
-> SVariable MatlabCode
-> MatlabCode ScopeData
-> MS (MatlabCode (Doc, Terminator))
listDec Integer
_ = SVariable MatlabCode
-> MatlabCode ScopeData -> MS (MatlabCode (Doc, Terminator))
mlListDec
  listDecDef :: SVariable MatlabCode
-> MatlabCode ScopeData
-> [SValue MatlabCode]
-> MS (MatlabCode (Doc, Terminator))
listDecDef = SVariable MatlabCode
-> MatlabCode ScopeData
-> [SValue MatlabCode]
-> MS (MatlabCode (Doc, Terminator))
forall (r :: * -> *) stmt bod.
(DeclStatement r stmt bod, Literal r, VariableElim r) =>
SVariable r -> r ScopeData -> [SValue r] -> MS (r stmt)
CP.listDecDef
  arrayDec :: Integer
-> SValue MatlabCode
-> SVariable MatlabCode
-> MatlabCode ScopeData
-> MS (MatlabCode (Doc, Terminator))
arrayDec Integer
_ SValue MatlabCode
_ = SVariable MatlabCode
-> MatlabCode ScopeData -> MS (MatlabCode (Doc, Terminator))
forall (r :: * -> *) stmt bod.
DeclStatement r stmt bod =>
SVariable r -> r ScopeData -> MS (r stmt)
varDec
  arrayDecDef :: SVariable MatlabCode
-> MatlabCode ScopeData
-> [SValue MatlabCode]
-> MS (MatlabCode (Doc, Terminator))
arrayDecDef = SVariable MatlabCode
-> MatlabCode ScopeData
-> [SValue MatlabCode]
-> MS (MatlabCode (Doc, Terminator))
forall (r :: * -> *) stmt bod.
DeclStatement r stmt bod =>
SVariable r -> r ScopeData -> [SValue r] -> MS (r stmt)
listDecDef
  constDecDef :: SVariable MatlabCode
-> MatlabCode ScopeData
-> SValue MatlabCode
-> MS (MatlabCode (Doc, Terminator))
constDecDef = SVariable MatlabCode
-> MatlabCode ScopeData
-> SValue MatlabCode
-> MS (MatlabCode (Doc, Terminator))
forall (r :: * -> *) stmt bod.
DeclStatement r stmt bod =>
SVariable r -> r ScopeData -> SValue r -> MS (r stmt)
varDecDef
  funcDecDef :: SVariable MatlabCode
-> MatlabCode ScopeData
-> [SVariable MatlabCode]
-> MS (MatlabCode Doc)
-> MS (MatlabCode (Doc, Terminator))
funcDecDef = SVariable MatlabCode
-> MatlabCode ScopeData
-> [SVariable MatlabCode]
-> MS (MatlabCode Doc)
-> MS (MatlabCode (Doc, Terminator))
forall (r :: * -> *) vis stmt mthd file mod bod block.
ProcRenderSym r vis stmt mthd file mod bod block =>
SVariable r
-> r ScopeData -> [SVariable r] -> MS (r bod) -> MS (r stmt)
A.funcDecDef

instance PrintConsole MatlabCode (Doc, Terminator) where
  print :: SValue MatlabCode -> MS (MatlabCode (Doc, Terminator))
print = Bool
-> Maybe (SValue MatlabCode)
-> SValue MatlabCode
-> SValue MatlabCode
-> MS (MatlabCode (Doc, Terminator))
forall (r :: * -> *) block stmt bod.
(BlockSym r block stmt, BodySym r bod block, MultiStatement r stmt,
 PrintConsole r stmt, PrintFile r stmt, DeclStatement r stmt bod,
 AssignStatement r stmt, ControlStatement r stmt bod, Literal r,
 NumericExpression r, Comparison r, VariableValue r, List r,
 TypeElim r, InternalIOStmt r stmt) =>
Bool -> Maybe (SValue r) -> SValue r -> SValue r -> MS (r stmt)
G.print Bool
False Maybe (SValue MatlabCode)
forall a. Maybe a
Nothing SValue MatlabCode
forall (r :: * -> *). RenderValue r => SValue r
printFunc
  printLn :: SValue MatlabCode -> MS (MatlabCode (Doc, Terminator))
printLn = Bool
-> Maybe (SValue MatlabCode)
-> SValue MatlabCode
-> SValue MatlabCode
-> MS (MatlabCode (Doc, Terminator))
forall (r :: * -> *) block stmt bod.
(BlockSym r block stmt, BodySym r bod block, MultiStatement r stmt,
 PrintConsole r stmt, PrintFile r stmt, DeclStatement r stmt bod,
 AssignStatement r stmt, ControlStatement r stmt bod, Literal r,
 NumericExpression r, Comparison r, VariableValue r, List r,
 TypeElim r, InternalIOStmt r stmt) =>
Bool -> Maybe (SValue r) -> SValue r -> SValue r -> MS (r stmt)
G.print Bool
True Maybe (SValue MatlabCode)
forall a. Maybe a
Nothing SValue MatlabCode
forall (r :: * -> *). RenderValue r => SValue r
printLnFunc
  printStr :: Label -> MS (MatlabCode (Doc, Terminator))
printStr = Bool
-> Maybe (SValue MatlabCode)
-> SValue MatlabCode
-> SValue MatlabCode
-> MS (MatlabCode (Doc, Terminator))
forall (r :: * -> *) block stmt bod.
(BlockSym r block stmt, BodySym r bod block, MultiStatement r stmt,
 PrintConsole r stmt, PrintFile r stmt, DeclStatement r stmt bod,
 AssignStatement r stmt, ControlStatement r stmt bod, Literal r,
 NumericExpression r, Comparison r, VariableValue r, List r,
 TypeElim r, InternalIOStmt r stmt) =>
Bool -> Maybe (SValue r) -> SValue r -> SValue r -> MS (r stmt)
G.print Bool
False Maybe (SValue MatlabCode)
forall a. Maybe a
Nothing SValue MatlabCode
forall (r :: * -> *). RenderValue r => SValue r
printFunc (SValue MatlabCode -> MS (MatlabCode (Doc, Terminator)))
-> (Label -> SValue MatlabCode)
-> Label
-> MS (MatlabCode (Doc, Terminator))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Label -> SValue MatlabCode
forall (r :: * -> *). Literal r => Label -> SValue r
litString
  printStrLn :: Label -> MS (MatlabCode (Doc, Terminator))
printStrLn = Bool
-> Maybe (SValue MatlabCode)
-> SValue MatlabCode
-> SValue MatlabCode
-> MS (MatlabCode (Doc, Terminator))
forall (r :: * -> *) block stmt bod.
(BlockSym r block stmt, BodySym r bod block, MultiStatement r stmt,
 PrintConsole r stmt, PrintFile r stmt, DeclStatement r stmt bod,
 AssignStatement r stmt, ControlStatement r stmt bod, Literal r,
 NumericExpression r, Comparison r, VariableValue r, List r,
 TypeElim r, InternalIOStmt r stmt) =>
Bool -> Maybe (SValue r) -> SValue r -> SValue r -> MS (r stmt)
G.print Bool
True Maybe (SValue MatlabCode)
forall a. Maybe a
Nothing SValue MatlabCode
forall (r :: * -> *). RenderValue r => SValue r
printLnFunc (SValue MatlabCode -> MS (MatlabCode (Doc, Terminator)))
-> (Label -> SValue MatlabCode)
-> Label
-> MS (MatlabCode (Doc, Terminator))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Label -> SValue MatlabCode
forall (r :: * -> *). Literal r => Label -> SValue r
litString

instance ReadConsole MatlabCode (Doc, Terminator) where
  getInput :: SVariable MatlabCode -> MS (MatlabCode (Doc, Terminator))
getInput = SValue MatlabCode
-> SVariable MatlabCode -> MS (MatlabCode (Doc, Terminator))
mlInput SValue MatlabCode
forall (r :: * -> *). RenderValue r => SValue r
inputFunc
  discardInput :: MS (MatlabCode (Doc, Terminator))
discardInput = SValue MatlabCode -> MS (MatlabCode (Doc, Terminator))
forall (r :: * -> *) stmt.
ValueStatement r stmt =>
SValue r -> MS (r stmt)
valStmt SValue MatlabCode
forall (r :: * -> *). RenderValue r => SValue r
inputFunc

instance FileHandling MatlabCode (Doc, Terminator) where
  openFileR :: SVariable MatlabCode
-> SValue MatlabCode -> MS (MatlabCode (Doc, Terminator))
openFileR SVariable MatlabCode
f SValue MatlabCode
n = SVariable MatlabCode
f SVariable MatlabCode
-> SValue MatlabCode -> MS (MatlabCode (Doc, Terminator))
forall (r :: * -> *) stmt.
AssignStatement r stmt =>
SVariable r -> SValue r -> MS (r stmt)
&= PosCall MatlabCode
forall (r :: * -> *). ValueExpression r => PosCall r
funcApp Label
"fopen" VS (MatlabCode TypeData)
forall (r :: * -> *). TypeSym r => VS (r TypeData)
infile [SValue MatlabCode
n, Label -> SValue MatlabCode
forall (r :: * -> *). Literal r => Label -> SValue r
litString Label
"r"]
  openFileW :: SVariable MatlabCode
-> SValue MatlabCode -> MS (MatlabCode (Doc, Terminator))
openFileW SVariable MatlabCode
f SValue MatlabCode
n = SVariable MatlabCode
f SVariable MatlabCode
-> SValue MatlabCode -> MS (MatlabCode (Doc, Terminator))
forall (r :: * -> *) stmt.
AssignStatement r stmt =>
SVariable r -> SValue r -> MS (r stmt)
&= PosCall MatlabCode
forall (r :: * -> *). ValueExpression r => PosCall r
funcApp Label
"fopen" VS (MatlabCode TypeData)
forall (r :: * -> *). TypeSym r => VS (r TypeData)
outfile [SValue MatlabCode
n, Label -> SValue MatlabCode
forall (r :: * -> *). Literal r => Label -> SValue r
litString Label
"w"]
  openFileA :: SVariable MatlabCode
-> SValue MatlabCode -> MS (MatlabCode (Doc, Terminator))
openFileA SVariable MatlabCode
f SValue MatlabCode
n = SVariable MatlabCode
f SVariable MatlabCode
-> SValue MatlabCode -> MS (MatlabCode (Doc, Terminator))
forall (r :: * -> *) stmt.
AssignStatement r stmt =>
SVariable r -> SValue r -> MS (r stmt)
&= PosCall MatlabCode
forall (r :: * -> *). ValueExpression r => PosCall r
funcApp Label
"fopen" VS (MatlabCode TypeData)
forall (r :: * -> *). TypeSym r => VS (r TypeData)
outfile [SValue MatlabCode
n, Label -> SValue MatlabCode
forall (r :: * -> *). Literal r => Label -> SValue r
litString Label
"a"]
  closeFile :: SValue MatlabCode -> MS (MatlabCode (Doc, Terminator))
closeFile SValue MatlabCode
f = SValue MatlabCode -> MS (MatlabCode (Doc, Terminator))
forall (r :: * -> *) stmt.
ValueStatement r stmt =>
SValue r -> MS (r stmt)
valStmt (SValue MatlabCode -> MS (MatlabCode (Doc, Terminator)))
-> SValue MatlabCode -> MS (MatlabCode (Doc, Terminator))
forall a b. (a -> b) -> a -> b
$ PosCall MatlabCode
forall (r :: * -> *). ValueExpression r => PosCall r
funcApp Label
"fclose" VS (MatlabCode TypeData)
forall (r :: * -> *). TypeSym r => VS (r TypeData)
void [SValue MatlabCode
f]

instance PrintFile MatlabCode (Doc, Terminator) where
  printFile :: SValue MatlabCode
-> SValue MatlabCode -> MS (MatlabCode (Doc, Terminator))
printFile SValue MatlabCode
f = Bool
-> Maybe (SValue MatlabCode)
-> SValue MatlabCode
-> SValue MatlabCode
-> MS (MatlabCode (Doc, Terminator))
forall (r :: * -> *) block stmt bod.
(BlockSym r block stmt, BodySym r bod block, MultiStatement r stmt,
 PrintConsole r stmt, PrintFile r stmt, DeclStatement r stmt bod,
 AssignStatement r stmt, ControlStatement r stmt bod, Literal r,
 NumericExpression r, Comparison r, VariableValue r, List r,
 TypeElim r, InternalIOStmt r stmt) =>
Bool -> Maybe (SValue r) -> SValue r -> SValue r -> MS (r stmt)
G.print Bool
False (SValue MatlabCode -> Maybe (SValue MatlabCode)
forall a. a -> Maybe a
Just SValue MatlabCode
f) SValue MatlabCode
forall (r :: * -> *). RenderValue r => SValue r
printFunc
  printFileLn :: SValue MatlabCode
-> SValue MatlabCode -> MS (MatlabCode (Doc, Terminator))
printFileLn SValue MatlabCode
f = Bool
-> Maybe (SValue MatlabCode)
-> SValue MatlabCode
-> SValue MatlabCode
-> MS (MatlabCode (Doc, Terminator))
forall (r :: * -> *) block stmt bod.
(BlockSym r block stmt, BodySym r bod block, MultiStatement r stmt,
 PrintConsole r stmt, PrintFile r stmt, DeclStatement r stmt bod,
 AssignStatement r stmt, ControlStatement r stmt bod, Literal r,
 NumericExpression r, Comparison r, VariableValue r, List r,
 TypeElim r, InternalIOStmt r stmt) =>
Bool -> Maybe (SValue r) -> SValue r -> SValue r -> MS (r stmt)
G.print Bool
True (SValue MatlabCode -> Maybe (SValue MatlabCode)
forall a. a -> Maybe a
Just SValue MatlabCode
f) SValue MatlabCode
forall (r :: * -> *). RenderValue r => SValue r
printLnFunc
  printFileStr :: SValue MatlabCode -> Label -> MS (MatlabCode (Doc, Terminator))
printFileStr SValue MatlabCode
f = SValue MatlabCode
-> SValue MatlabCode -> MS (MatlabCode (Doc, Terminator))
forall (r :: * -> *) stmt.
PrintFile r stmt =>
SValue r -> SValue r -> MS (r stmt)
printFile SValue MatlabCode
f (SValue MatlabCode -> MS (MatlabCode (Doc, Terminator)))
-> (Label -> SValue MatlabCode)
-> Label
-> MS (MatlabCode (Doc, Terminator))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Label -> SValue MatlabCode
forall (r :: * -> *). Literal r => Label -> SValue r
litString
  printFileStrLn :: SValue MatlabCode -> Label -> MS (MatlabCode (Doc, Terminator))
printFileStrLn SValue MatlabCode
f = SValue MatlabCode
-> SValue MatlabCode -> MS (MatlabCode (Doc, Terminator))
forall (r :: * -> *) stmt.
PrintFile r stmt =>
SValue r -> SValue r -> MS (r stmt)
printFileLn SValue MatlabCode
f (SValue MatlabCode -> MS (MatlabCode (Doc, Terminator)))
-> (Label -> SValue MatlabCode)
-> Label
-> MS (MatlabCode (Doc, Terminator))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Label -> SValue MatlabCode
forall (r :: * -> *). Literal r => Label -> SValue r
litString

instance ReadFile MatlabCode (Doc, Terminator) where
  getFileInput :: SValue MatlabCode
-> SVariable MatlabCode -> MS (MatlabCode (Doc, Terminator))
getFileInput SValue MatlabCode
f = SValue MatlabCode
-> SVariable MatlabCode -> MS (MatlabCode (Doc, Terminator))
mlInput (SValue MatlabCode -> SValue MatlabCode
mlReadLine SValue MatlabCode
f)
  discardFileInput :: SValue MatlabCode -> MS (MatlabCode (Doc, Terminator))
discardFileInput SValue MatlabCode
f = SValue MatlabCode -> MS (MatlabCode (Doc, Terminator))
forall (r :: * -> *) stmt.
ValueStatement r stmt =>
SValue r -> MS (r stmt)
valStmt (SValue MatlabCode -> SValue MatlabCode
mlReadLine SValue MatlabCode
f)
  getFileInputLine :: SValue MatlabCode
-> SVariable MatlabCode -> MS (MatlabCode (Doc, Terminator))
getFileInputLine = SValue MatlabCode
-> SVariable MatlabCode -> MS (MatlabCode (Doc, Terminator))
forall (r :: * -> *) stmt.
ReadFile r stmt =>
SValue r -> SVariable r -> MS (r stmt)
getFileInput
  discardFileLine :: SValue MatlabCode -> MS (MatlabCode (Doc, Terminator))
discardFileLine = SValue MatlabCode -> MS (MatlabCode (Doc, Terminator))
forall (r :: * -> *) stmt.
ReadFile r stmt =>
SValue r -> MS (r stmt)
discardFileInput
  getFileInputAll :: SValue MatlabCode
-> SVariable MatlabCode -> MS (MatlabCode (Doc, Terminator))
getFileInputAll = SValue MatlabCode
-> SVariable MatlabCode -> MS (MatlabCode (Doc, Terminator))
mlReadAllLines

instance StringStatement MatlabCode (Doc, Terminator) where
  stringSplit :: Char
-> SVariable MatlabCode
-> SValue MatlabCode
-> MS (MatlabCode (Doc, Terminator))
stringSplit Char
d SVariable MatlabCode
vnew SValue MatlabCode
s = SVariable MatlabCode
vnew SVariable MatlabCode
-> SValue MatlabCode -> MS (MatlabCode (Doc, Terminator))
forall (r :: * -> *) stmt.
AssignStatement r stmt =>
SVariable r -> SValue r -> MS (r stmt)
&= PosCall MatlabCode
forall (r :: * -> *). ValueExpression r => PosCall r
funcApp Label
"strsplit" (VS (MatlabCode TypeData) -> VS (MatlabCode TypeData)
forall (r :: * -> *).
TypeSym r =>
VS (r TypeData) -> VS (r TypeData)
listType VS (MatlabCode TypeData)
forall (r :: * -> *). TypeSym r => VS (r TypeData)
string) [SValue MatlabCode
s, Label -> SValue MatlabCode
forall (r :: * -> *). Literal r => Label -> SValue r
litString [Char
d]]
  stringListVals :: [SVariable MatlabCode]
-> SValue MatlabCode -> MS (MatlabCode (Doc, Terminator))
stringListVals = [SVariable MatlabCode]
-> SValue MatlabCode -> MS (MatlabCode (Doc, Terminator))
forall (r :: * -> *) stmt.
(MultiStatement r stmt, AssignStatement r stmt, List r, Literal r,
 RenderValue r, TypeElim r, VariableElim r) =>
[SVariable r] -> SValue r -> MS (r stmt)
M.stringListVals
  stringListLists :: [SVariable MatlabCode]
-> SValue MatlabCode -> MS (MatlabCode (Doc, Terminator))
stringListLists = [SVariable MatlabCode]
-> SValue MatlabCode -> MS (MatlabCode (Doc, Terminator))
forall (r :: * -> *) bod block stmt.
(BodySym r bod block, BlockSym r block stmt,
 ControlStatement r stmt bod, Literal r, NumericExpression r,
 VariableValue r, List r, ListStatement r stmt, TypeElim r,
 VariableElim r, RenderValue r) =>
[SVariable r] -> SValue r -> MS (r stmt)
M.stringListLists

instance FunctionSym MatlabCode where

instance FuncAppStatement MatlabCode (Doc, Terminator) where
  inOutCall :: InOutCall MatlabCode (Doc, Terminator)
inOutCall = PosCall MatlabCode -> InOutCall MatlabCode (Doc, Terminator)
forall (r :: * -> *) stmt.
(InternalAssignStmt r stmt, ValueStatement r stmt,
 VariableValue r) =>
(Label -> VS (r TypeData) -> [SValue r] -> SValue r)
-> Label
-> [SValue r]
-> [SVariable r]
-> [SVariable r]
-> MS (r stmt)
CP.inOutCall PosCall MatlabCode
forall (r :: * -> *). ValueExpression r => PosCall r
funcApp
  extInOutCall :: Label -> InOutCall MatlabCode (Doc, Terminator)
extInOutCall = Label -> InOutCall MatlabCode (Doc, Terminator)
forall a. HasCallStack => a
undefined

instance CommentStatement MatlabCode (Doc, Terminator) where
  comment :: Label -> MS (MatlabCode (Doc, Terminator))
comment = Doc -> Label -> MS (MatlabCode (Doc, Terminator))
forall {k} (r :: k -> *) (stmt :: k).
RenderStatement r stmt =>
Doc -> Label -> MS (r stmt)
G.comment Doc
mlCmtStart

instance ControlStatement MatlabCode (Doc, Terminator) Body where
  break :: MS (MatlabCode (Doc, Terminator))
break = Doc -> MS (MatlabCode (Doc, Terminator))
forall {k} (r :: k -> *) (stmt :: k).
RenderStatement r stmt =>
Doc -> MS (r stmt)
mkStmtNoEnd Doc
R.break
  continue :: MS (MatlabCode (Doc, Terminator))
continue = Doc -> MS (MatlabCode (Doc, Terminator))
forall {k} (r :: k -> *) (stmt :: k).
RenderStatement r stmt =>
Doc -> MS (r stmt)
mkStmtNoEnd Doc
R.continue
  -- MATLAB has no `return <expr>`: a function returns by assigning its named
  -- output, so a return becomes `result = <value>;`.
  returnStmt :: SValue MatlabCode -> MS (MatlabCode (Doc, Terminator))
returnStmt SValue MatlabCode
v' = do
    v <- LensLike'
  (Zoomed (StateT ValueState Identity) (MatlabCode Value))
  MethodState
  ValueState
-> SValue MatlabCode
-> StateT MethodState Identity (MatlabCode Value)
forall c.
LensLike'
  (Zoomed (StateT ValueState Identity) c) MethodState ValueState
-> StateT ValueState Identity c -> StateT MethodState Identity c
forall (m :: * -> *) (n :: * -> *) s t c.
Zoom m n s t =>
LensLike' (Zoomed m c) t s -> m c -> n c
zoom LensLike'
  (Zoomed (StateT ValueState Identity) (MatlabCode Value))
  MethodState
  ValueState
(ValueState -> Focusing Identity (MatlabCode Value) ValueState)
-> MethodState -> Focusing Identity (MatlabCode Value) MethodState
Lens' MethodState ValueState
lensMStoVS SValue MatlabCode
v'
    var mlRet (toState (valueType v)) &= v'
  throw :: Label -> MS (MatlabCode (Doc, Terminator))
throw Label
errMsg = SValue MatlabCode -> MS (MatlabCode (Doc, Terminator))
forall (r :: * -> *) stmt.
ValueStatement r stmt =>
SValue r -> MS (r stmt)
valStmt (SValue MatlabCode -> MS (MatlabCode (Doc, Terminator)))
-> SValue MatlabCode -> MS (MatlabCode (Doc, Terminator))
forall a b. (a -> b) -> a -> b
$ PosCall MatlabCode
forall (r :: * -> *). ValueExpression r => PosCall r
funcApp Label
"error" VS (MatlabCode TypeData)
forall (r :: * -> *). TypeSym r => VS (r TypeData)
void [Label -> SValue MatlabCode
forall (r :: * -> *). Literal r => Label -> SValue r
litString Label
errMsg]
  ifCond :: [(SValue MatlabCode, MS (MatlabCode Doc))]
-> MS (MatlabCode Doc) -> MS (MatlabCode (Doc, Terminator))
ifCond = (Doc -> Doc)
-> Doc
-> OptionalSpace
-> Doc
-> Doc
-> Doc
-> [(SValue MatlabCode, MS (MatlabCode Doc))]
-> MS (MatlabCode Doc)
-> MS (MatlabCode (Doc, Terminator))
forall (r :: * -> *) bod stmt.
(BodyElim r bod, RenderStatement r stmt, ValueElim r) =>
(Doc -> Doc)
-> Doc
-> OptionalSpace
-> Doc
-> Doc
-> Doc
-> [(SValue r, MS (r bod))]
-> MS (r bod)
-> MS (r stmt)
G.ifCond Doc -> Doc
forall a. a -> a
id Doc
empty (Doc -> OptionalSpace
OSpace Doc
empty) Doc
mlElseIf Doc
empty Doc
mlEnd
  switch :: SValue MatlabCode
-> [(SValue MatlabCode, MS (MatlabCode Doc))]
-> MS (MatlabCode Doc)
-> MS (MatlabCode (Doc, Terminator))
switch = SValue MatlabCode
-> [(SValue MatlabCode, MS (MatlabCode Doc))]
-> MS (MatlabCode Doc)
-> MS (MatlabCode (Doc, Terminator))
forall (r :: * -> *) stmt bod.
(ControlStatement r stmt bod, Comparison r) =>
SValue r -> [(SValue r, MS (r bod))] -> MS (r bod) -> MS (r stmt)
switchAsIf
  ifExists :: SValue MatlabCode
-> MS (MatlabCode Doc)
-> MS (MatlabCode Doc)
-> MS (MatlabCode (Doc, Terminator))
ifExists = SValue MatlabCode
-> MS (MatlabCode Doc)
-> MS (MatlabCode Doc)
-> MS (MatlabCode (Doc, Terminator))
forall (r :: * -> *) stmt bod.
(ControlStatement r stmt bod, ValueExpression r) =>
SValue r -> MS (r bod) -> MS (r bod) -> MS (r stmt)
M.ifExists
  for :: MS (MatlabCode (Doc, Terminator))
-> SValue MatlabCode
-> MS (MatlabCode (Doc, Terminator))
-> MS (MatlabCode Doc)
-> MS (MatlabCode (Doc, Terminator))
for MS (MatlabCode (Doc, Terminator))
initS SValue MatlabCode
cond MS (MatlabCode (Doc, Terminator))
updateS MS (MatlabCode Doc)
b = do
    i <- MS (MatlabCode (Doc, Terminator))
initS
    c <- zoom lensMStoVS cond
    u <- updateS
    bd <- b
    mkStmtNoEnd (vcat [
      RC.statement i <> text ";",
      text "while" <+> RC.value c,
      indent (RC.body bd),
      indent (RC.statement u <> text ";"),
      mlEnd])
  forRange :: SVariable MatlabCode
-> SValue MatlabCode
-> SValue MatlabCode
-> SValue MatlabCode
-> MS (MatlabCode Doc)
-> MS (MatlabCode (Doc, Terminator))
forRange SVariable MatlabCode
i SValue MatlabCode
initv SValue MatlabCode
finalv SValue MatlabCode
stepv = SVariable MatlabCode
-> SValue MatlabCode
-> MS (MatlabCode Doc)
-> MS (MatlabCode (Doc, Terminator))
forall (r :: * -> *) stmt bod.
ControlStatement r stmt bod =>
SVariable r -> SValue r -> MS (r bod) -> MS (r stmt)
forEach SVariable MatlabCode
i (SValue MatlabCode
-> SValue MatlabCode -> SValue MatlabCode -> SValue MatlabCode
forall (r :: * -> *) vis stmt mthd bod block.
CommonRenderSym r vis stmt mthd bod block =>
SValue r -> SValue r -> SValue r -> SValue r
mlRange SValue MatlabCode
initv SValue MatlabCode
finalv SValue MatlabCode
stepv)
  forEach :: SVariable MatlabCode
-> SValue MatlabCode
-> MS (MatlabCode Doc)
-> MS (MatlabCode (Doc, Terminator))
forEach = (MatlabCode Variable -> MatlabCode Value -> MatlabCode Doc -> Doc)
-> SVariable MatlabCode
-> SValue MatlabCode
-> MS (MatlabCode Doc)
-> MS (MatlabCode (Doc, Terminator))
forall (r :: * -> *) stmt.
RenderStatement r stmt =>
(r Variable -> r Value -> r Doc -> Doc)
-> SVariable r -> SValue r -> MS (r Doc) -> MS (r stmt)
CS.forEach' MatlabCode Variable -> MatlabCode Value -> MatlabCode Doc -> Doc
forall (r :: * -> *) vis stmt mthd bod block.
CommonRenderSym r vis stmt mthd bod block =>
r Variable -> r Value -> r bod -> Doc
mlForEach
  while :: SValue MatlabCode
-> MS (MatlabCode Doc) -> MS (MatlabCode (Doc, Terminator))
while = (Doc -> Doc)
-> Doc
-> Doc
-> SValue MatlabCode
-> MS (MatlabCode Doc)
-> MS (MatlabCode (Doc, Terminator))
forall (r :: * -> *) bod stmt.
(BodyElim r bod, RenderStatement r stmt, ValueElim r) =>
(Doc -> Doc) -> Doc -> Doc -> SValue r -> MS (r bod) -> MS (r stmt)
C.while Doc -> Doc
forall a. a -> a
id Doc
empty Doc
mlEnd
  tryCatch :: MS (MatlabCode Doc)
-> MS (MatlabCode Doc) -> MS (MatlabCode (Doc, Terminator))
tryCatch = (MatlabCode Doc -> MatlabCode Doc -> Doc)
-> MS (MatlabCode Doc)
-> MS (MatlabCode Doc)
-> MS (MatlabCode (Doc, Terminator))
forall {k} (r :: k -> *) (stmt :: k) (bod :: k).
RenderStatement r stmt =>
(r bod -> r bod -> Doc) -> MS (r bod) -> MS (r bod) -> MS (r stmt)
G.tryCatch MatlabCode Doc -> MatlabCode Doc -> Doc
forall (r :: * -> *) vis stmt mthd bod block.
CommonRenderSym r vis stmt mthd bod block =>
r bod -> r bod -> Doc
mlTryCatch
  assert :: SValue MatlabCode
-> SValue MatlabCode -> MS (MatlabCode (Doc, Terminator))
assert SValue MatlabCode
cond SValue MatlabCode
errMsg = SValue MatlabCode -> MS (MatlabCode (Doc, Terminator))
forall (r :: * -> *) stmt.
ValueStatement r stmt =>
SValue r -> MS (r stmt)
valStmt (SValue MatlabCode -> MS (MatlabCode (Doc, Terminator)))
-> SValue MatlabCode -> MS (MatlabCode (Doc, Terminator))
forall a b. (a -> b) -> a -> b
$ PosCall MatlabCode
forall (r :: * -> *). ValueExpression r => PosCall r
funcApp Label
"assert" VS (MatlabCode TypeData)
forall (r :: * -> *). TypeSym r => VS (r TypeData)
void [SValue MatlabCode
cond, SValue MatlabCode
errMsg]

instance VisibilitySym MatlabCode Doc where
  private :: MatlabCode Doc
private = Doc -> MatlabCode Doc
forall (r :: * -> *) a. Monad r => a -> r a
toCode Doc
empty
  public :: MatlabCode Doc
public = Doc -> MatlabCode Doc
forall (r :: * -> *) a. Monad r => a -> r a
toCode Doc
empty

instance RenderVisibility MatlabCode Doc where
  visibilityFromData :: VisibilityTag -> Doc -> MatlabCode Doc
visibilityFromData = VisibilityTag -> Doc -> MatlabCode Doc
forall a. HasCallStack => a
undefined

instance VisibilityElim MatlabCode Doc where
  visibility :: MatlabCode Doc -> Doc
visibility = MatlabCode Doc -> Doc
forall a. MatlabCode a -> a
unMLC

instance MethodTypeSym MatlabCode where
  mType :: VS (MatlabCode TypeData) -> MSMthdType MatlabCode
mType = LensLike'
  (Zoomed (StateT ValueState Identity) (MatlabCode TypeData))
  MethodState
  ValueState
-> VS (MatlabCode TypeData) -> MSMthdType MatlabCode
forall c.
LensLike'
  (Zoomed (StateT ValueState Identity) c) MethodState ValueState
-> StateT ValueState Identity c -> StateT MethodState Identity c
forall (m :: * -> *) (n :: * -> *) s t c.
Zoom m n s t =>
LensLike' (Zoomed m c) t s -> m c -> n c
zoom LensLike'
  (Zoomed (StateT ValueState Identity) (MatlabCode TypeData))
  MethodState
  ValueState
(ValueState -> Focusing Identity (MatlabCode TypeData) ValueState)
-> MethodState
-> Focusing Identity (MatlabCode TypeData) MethodState
Lens' MethodState ValueState
lensMStoVS

instance ParameterSym MatlabCode where
  -- A MATLAB parameter is just the variable name.
  param :: SVariable MatlabCode -> MS (MatlabCode ParamData)
param = (MatlabCode Variable -> Doc)
-> SVariable MatlabCode -> MS (MatlabCode ParamData)
forall (r :: * -> *).
(RenderParam r, VariableElim r) =>
(r Variable -> Doc) -> SVariable r -> MS (r ParamData)
G.param MatlabCode Variable -> Doc
mlParam
  pointerParam :: SVariable MatlabCode -> MS (MatlabCode ParamData)
pointerParam = SVariable MatlabCode -> MS (MatlabCode ParamData)
forall (r :: * -> *).
ParameterSym r =>
SVariable r -> MS (r ParamData)
param

instance RenderParam MatlabCode where
  paramFromData :: SVariable MatlabCode -> Doc -> MS (MatlabCode ParamData)
paramFromData SVariable MatlabCode
v' Doc
d = do
    v <- LensLike'
  (Zoomed (StateT ValueState Identity) (MatlabCode Variable))
  MethodState
  ValueState
-> SVariable MatlabCode
-> StateT MethodState Identity (MatlabCode Variable)
forall c.
LensLike'
  (Zoomed (StateT ValueState Identity) c) MethodState ValueState
-> StateT ValueState Identity c -> StateT MethodState Identity c
forall (m :: * -> *) (n :: * -> *) s t c.
Zoom m n s t =>
LensLike' (Zoomed m c) t s -> m c -> n c
zoom LensLike'
  (Zoomed (StateT ValueState Identity) (MatlabCode Variable))
  MethodState
  ValueState
(ValueState -> Focusing Identity (MatlabCode Variable) ValueState)
-> MethodState
-> Focusing Identity (MatlabCode Variable) MethodState
Lens' MethodState ValueState
lensMStoVS SVariable MatlabCode
v'
    toState $ on2CodeValues pd v (toCode d)

instance ParamElim MatlabCode where
  parameterName :: MatlabCode ParamData -> Label
parameterName = MatlabCode Variable -> Label
forall (r :: * -> *). VariableElim r => r Variable -> Label
variableName (MatlabCode Variable -> Label)
-> (MatlabCode ParamData -> MatlabCode Variable)
-> MatlabCode ParamData
-> Label
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (ParamData -> Variable)
-> MatlabCode ParamData -> MatlabCode Variable
forall (r :: * -> *) a b. Functor r => (a -> b) -> r a -> r b
onCodeValue ParamData -> Variable
paramVar
  parameterType :: MatlabCode ParamData -> MatlabCode TypeData
parameterType = MatlabCode Variable -> MatlabCode TypeData
forall (r :: * -> *). VariableElim r => r Variable -> r TypeData
variableType (MatlabCode Variable -> MatlabCode TypeData)
-> (MatlabCode ParamData -> MatlabCode Variable)
-> MatlabCode ParamData
-> MatlabCode TypeData
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (ParamData -> Variable)
-> MatlabCode ParamData -> MatlabCode Variable
forall (r :: * -> *) a b. Functor r => (a -> b) -> r a -> r b
onCodeValue ParamData -> Variable
paramVar
  parameter :: MatlabCode ParamData -> Doc
parameter = ParamData -> Doc
paramDoc (ParamData -> Doc)
-> (MatlabCode ParamData -> ParamData)
-> MatlabCode ParamData
-> Doc
forall b c a. (b -> c) -> (a -> b) -> a -> c
. MatlabCode ParamData -> ParamData
forall a. MatlabCode a -> a
unMLC

instance MethodSym MatlabCode Doc MethodData Body where
  docMain :: MS (MatlabCode Doc) -> MS (MatlabCode MethodData)
docMain = MS (MatlabCode Doc) -> MS (MatlabCode MethodData)
forall (r :: * -> *) vis mthd bod.
MethodSym r vis mthd bod =>
MS (r bod) -> MS (r mthd)
mainFunction
  function :: Label
-> MatlabCode Doc
-> VS (MatlabCode TypeData)
-> [MS (MatlabCode ParamData)]
-> MS (MatlabCode Doc)
-> MS (MatlabCode MethodData)
function = Label
-> MatlabCode Doc
-> VS (MatlabCode TypeData)
-> [MS (MatlabCode ParamData)]
-> MS (MatlabCode Doc)
-> MS (MatlabCode MethodData)
forall (r :: * -> *) vis mthd bod.
ProcRenderMethod r vis mthd bod =>
Label
-> r vis
-> VS (r TypeData)
-> [MS (r ParamData)]
-> MS (r bod)
-> MS (r mthd)
A.function
  mainFunction :: MS (MatlabCode Doc) -> MS (MatlabCode MethodData)
mainFunction = MS (MatlabCode Doc) -> MS (MatlabCode MethodData)
forall (r :: * -> *) bod mthd.
(BodyElim r bod, RenderMethod r mthd) =>
MS (r bod) -> MS (r mthd)
CP.mainBody
  docFunc :: Label
-> [Label]
-> Maybe Label
-> MS (MatlabCode MethodData)
-> MS (MatlabCode MethodData)
docFunc = FuncDocRenderer
-> Label
-> [Label]
-> Maybe Label
-> MS (MatlabCode MethodData)
-> MS (MatlabCode MethodData)
forall (r :: * -> *) mthd.
RenderMethod r mthd =>
FuncDocRenderer
-> Label -> [Label] -> Maybe Label -> MS (r mthd) -> MS (r mthd)
G.docFunc FuncDocRenderer
CP.functionDoc

  -- MATLAB returns values through the output list in the header
  -- (function [outs] = name(ins)), not through a return statement, so we build
  -- the method directly instead of reusing the shared inOutFunc machinery.
  inOutFunc :: Label -> MatlabCode Doc -> InOutFunc MatlabCode MethodData Doc
inOutFunc Label
n MatlabCode Doc
_ [SVariable MatlabCode]
ins [SVariable MatlabCode]
outs [SVariable MatlabCode]
both MS (MatlabCode Doc)
b = do
    pms  <- (SVariable MatlabCode -> MS (MatlabCode ParamData))
-> [SVariable MatlabCode]
-> StateT MethodState Identity [MatlabCode ParamData]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> [a] -> m [b]
mapM SVariable MatlabCode -> MS (MatlabCode ParamData)
forall (r :: * -> *).
ParameterSym r =>
SVariable r -> MS (r ParamData)
param ([SVariable MatlabCode]
both [SVariable MatlabCode]
-> [SVariable MatlabCode] -> [SVariable MatlabCode]
forall a. [a] -> [a] -> [a]
++ [SVariable MatlabCode]
ins)
    rets <- mapM (zoom lensMStoVS) (both ++ outs)
    bod  <- b
    pure $ toCode $ mthd n $ mlFuncDoc n (map RC.variable rets) pms (RC.body bod)
  docInOutFunc :: Label -> MatlabCode Doc -> DocInOutFunc MatlabCode MethodData Doc
docInOutFunc Label
n MatlabCode Doc
s = FuncDocRenderer
-> InOutFunc MatlabCode MethodData Doc
-> DocInOutFunc MatlabCode MethodData Doc
forall (r :: * -> *) mthd bod.
RenderMethod r mthd =>
FuncDocRenderer
-> ([SVariable r]
    -> [SVariable r] -> [SVariable r] -> MS (r bod) -> MS (r mthd))
-> Label
-> [(Label, SVariable r)]
-> [(Label, SVariable r)]
-> [(Label, SVariable r)]
-> MS (r bod)
-> MS (r mthd)
CP.docInOutFunc' FuncDocRenderer
CP.functionDoc (Label -> MatlabCode Doc -> InOutFunc MatlabCode MethodData Doc
forall (r :: * -> *) vis mthd bod.
MethodSym r vis mthd bod =>
Label -> r vis -> InOutFunc r mthd bod
inOutFunc Label
n MatlabCode Doc
s)

instance RenderMethod MatlabCode MethodData where
  commentedFunc :: MS (MatlabCode Doc)
-> MS (MatlabCode MethodData) -> MS (MatlabCode MethodData)
commentedFunc MS (MatlabCode Doc)
cmt MS (MatlabCode MethodData)
m = (MatlabCode MethodData
 -> MatlabCode (Doc -> Doc) -> MatlabCode MethodData)
-> MS (MatlabCode MethodData)
-> State MethodState (MatlabCode (Doc -> Doc))
-> MS (MatlabCode MethodData)
forall a b c s.
(a -> b -> c) -> State s a -> State s b -> State s c
on2StateValues ((MethodData -> (Doc -> Doc) -> MethodData)
-> MatlabCode MethodData
-> MatlabCode (Doc -> Doc)
-> MatlabCode MethodData
forall (r :: * -> *) a b c.
Applicative r =>
(a -> b -> c) -> r a -> r b -> r c
on2CodeValues MethodData -> (Doc -> Doc) -> MethodData
updateMthd) MS (MatlabCode MethodData)
m
    ((MatlabCode Doc -> MatlabCode (Doc -> Doc))
-> MS (MatlabCode Doc)
-> State MethodState (MatlabCode (Doc -> Doc))
forall a b s. (a -> b) -> State s a -> State s b
onStateValue ((Doc -> Doc -> Doc) -> MatlabCode Doc -> MatlabCode (Doc -> Doc)
forall (r :: * -> *) a b. Functor r => (a -> b) -> r a -> r b
onCodeValue Doc -> Doc -> Doc
R.commentedItem) MS (MatlabCode Doc)
cmt)
  mthdFromData :: VisibilityTag -> Doc -> MS (MatlabCode MethodData)
mthdFromData VisibilityTag
_ Doc
d = MatlabCode MethodData -> MS (MatlabCode MethodData)
forall a s. a -> State s a
toState (MatlabCode MethodData -> MS (MatlabCode MethodData))
-> MatlabCode MethodData -> MS (MatlabCode MethodData)
forall a b. (a -> b) -> a -> b
$ MethodData -> MatlabCode MethodData
forall (r :: * -> *) a. Monad r => a -> r a
toCode (MethodData -> MatlabCode MethodData)
-> MethodData -> MatlabCode MethodData
forall a b. (a -> b) -> a -> b
$ Label -> Doc -> MethodData
mthd Label
"" Doc
d

instance ProcRenderMethod MatlabCode Doc MethodData Body where
  intFunc :: Bool
-> Label
-> MatlabCode Doc
-> MSMthdType MatlabCode
-> [MS (MatlabCode ParamData)]
-> MS (MatlabCode Doc)
-> MS (MatlabCode MethodData)
intFunc Bool
_ Label
n MatlabCode Doc
_ MSMthdType MatlabCode
t [MS (MatlabCode ParamData)]
ps MS (MatlabCode Doc)
b = do
    pms <- [MS (MatlabCode ParamData)]
-> StateT MethodState Identity [MatlabCode ParamData]
forall (t :: * -> *) (m :: * -> *) a.
(Traversable t, Monad m) =>
t (m a) -> m (t a)
forall (m :: * -> *) a. Monad m => [m a] -> m [a]
sequence [MS (MatlabCode ParamData)]
ps
    tp  <- t
    bod <- b
    let outs = [Label -> Doc
text Label
mlRet | TypeData -> CodeType
cType (MatlabCode TypeData -> TypeData
forall a. MatlabCode a -> a
unMLC MatlabCode TypeData
tp) CodeType -> CodeType -> Bool
forall a. Eq a => a -> a -> Bool
/= CodeType
Void]
    pure $ toCode $ mthd n $ mlFuncDoc n outs pms (RC.body bod)

instance MethodElim MatlabCode MethodData where
  method :: MatlabCode MethodData -> Doc
method = MethodData -> Doc
mthdDoc (MethodData -> Doc)
-> (MatlabCode MethodData -> MethodData)
-> MatlabCode MethodData
-> Doc
forall b c a. (b -> c) -> (a -> b) -> a -> c
. MatlabCode MethodData -> MethodData
forall a. MatlabCode a -> a
unMLC

instance ModuleSym MatlabCode ModData MethodData where
  buildModule :: Label
-> [Label]
-> [MS (MatlabCode MethodData)]
-> FS (MatlabCode ModData)
buildModule Label
n [Label]
_ [MS (MatlabCode MethodData)]
fs = Label -> FS Doc -> FS (MatlabCode ModData)
forall {k} (r :: k -> *) (mod :: k).
RenderMod r mod =>
Label -> FS Doc -> FS (r mod)
modFromData Label
n (do
    fns <- (MS (MatlabCode MethodData)
 -> StateT FileState Identity (MatlabCode MethodData))
-> [MS (MatlabCode MethodData)]
-> StateT FileState Identity [MatlabCode MethodData]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> [a] -> m [b]
mapM (LensLike'
  (Zoomed (StateT MethodState Identity) (MatlabCode MethodData))
  FileState
  MethodState
-> MS (MatlabCode MethodData)
-> StateT FileState Identity (MatlabCode MethodData)
forall c.
LensLike'
  (Zoomed (StateT MethodState Identity) c) FileState MethodState
-> StateT MethodState Identity c -> StateT FileState Identity c
forall (m :: * -> *) (n :: * -> *) s t c.
Zoom m n s t =>
LensLike' (Zoomed m c) t s -> m c -> n c
zoom LensLike'
  (Zoomed (StateT MethodState Identity) (MatlabCode MethodData))
  FileState
  MethodState
(MethodState
 -> Focusing Identity (MatlabCode MethodData) MethodState)
-> FileState -> Focusing Identity (MatlabCode MethodData) FileState
Lens' FileState MethodState
lensFStoMS) [MS (MatlabCode MethodData)]
fs
    entryFn <- mlMainFunc n
    let fnDocs = [Doc] -> Doc
vibcat ((MatlabCode MethodData -> Doc) -> [MatlabCode MethodData] -> [Doc]
forall a b. (a -> b) -> [a] -> [b]
map MatlabCode MethodData -> Doc
forall {k} (r :: k -> *) (mthd :: k).
MethodElim r mthd =>
r mthd -> Doc
RC.method [MatlabCode MethodData]
fns)
        content = [Doc] -> Doc
vibcat ((Doc -> Bool) -> [Doc] -> [Doc]
forall a. (a -> Bool) -> [a] -> [a]
filter (Bool -> Bool
not (Bool -> Bool) -> (Doc -> Bool) -> Doc -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Doc -> Bool
isEmpty) [Doc
entryFn, Doc
fnDocs])
    case fns of
      (MatlabCode MethodData
f:[MatlabCode MethodData]
_) | Doc -> Bool
isEmpty Doc
entryFn -> (FileState -> FileState) -> StateT FileState Identity ()
forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
modify (Label -> FileState -> FileState
setModuleName (MethodData -> Label
mthdName (MatlabCode MethodData -> MethodData
forall a. MatlabCode a -> a
unMLC MatlabCode MethodData
f)))
      [MatlabCode MethodData]
_                       -> () -> StateT FileState Identity ()
forall a. a -> StateT FileState Identity a
forall (m :: * -> *) a. Monad m => a -> m a
return ()
    return $ emptyIfEmpty content content)

instance RenderMod MatlabCode ModData where
  modFromData :: Label -> FS Doc -> FS (MatlabCode ModData)
modFromData Label
n = Label
-> (Doc -> MatlabCode ModData) -> FS Doc -> FS (MatlabCode ModData)
forall {k} (r :: k -> *) (mod :: k).
Label -> (Doc -> r mod) -> FS Doc -> FS (r mod)
A.modFromData Label
n (ModData -> MatlabCode ModData
forall (r :: * -> *) a. Monad r => a -> r a
toCode (ModData -> MatlabCode ModData)
-> (Doc -> ModData) -> Doc -> MatlabCode ModData
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Label -> Doc -> ModData
md Label
n)
  updateModuleDoc :: (Doc -> Doc) -> MatlabCode ModData -> MatlabCode ModData
updateModuleDoc Doc -> Doc
f = (ModData -> ModData) -> MatlabCode ModData -> MatlabCode ModData
forall (r :: * -> *) a b. Functor r => (a -> b) -> r a -> r b
onCodeValue ((Doc -> Doc) -> ModData -> ModData
updateMod Doc -> Doc
f)

instance ModuleElim MatlabCode ModData where
  module' :: MatlabCode ModData -> Doc
module' = ModData -> Doc
modDoc (ModData -> Doc)
-> (MatlabCode ModData -> ModData) -> MatlabCode ModData -> Doc
forall b c a. (b -> c) -> (a -> b) -> a -> c
. MatlabCode ModData -> ModData
forall a. MatlabCode a -> a
unMLC

instance BlockCommentSym MatlabCode where
  blockComment :: [Label] -> MatlabCode Doc
blockComment [Label]
lns = Doc -> MatlabCode Doc
forall (r :: * -> *) a. Monad r => a -> r a
toCode (Doc -> MatlabCode Doc) -> Doc -> MatlabCode Doc
forall a b. (a -> b) -> a -> b
$ [Label] -> Doc
mlLineCmt [Label]
lns
  docComment :: forall a. State a [Label] -> State a (MatlabCode Doc)
docComment = ([Label] -> MatlabCode Doc)
-> State a [Label] -> State a (MatlabCode Doc)
forall a b s. (a -> b) -> State s a -> State s b
onStateValue (Doc -> MatlabCode Doc
forall (r :: * -> *) a. Monad r => a -> r a
toCode (Doc -> MatlabCode Doc)
-> ([Label] -> Doc) -> [Label] -> MatlabCode Doc
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Label] -> Doc
mlLineCmt)

instance BlockCommentElim MatlabCode where
  blockComment' :: MatlabCode Doc -> Doc
blockComment' = MatlabCode Doc -> Doc
forall a. MatlabCode a -> a
unMLC

-- convenience
mlName, mlVersion :: String
mlName :: Label
mlName = Label
"MATLAB"
mlVersion :: Label
mlVersion = Label
"R2024b"

-- | MATLAB source file extension.
mlExt :: String
mlExt :: Label
mlExt = Label
"m"

-- | Name of the implicit output variable a value-returning function assigns to.
mlRet :: String
mlRet :: Label
mlRet = Label
"result"

-- | Makes a MATLAB type. Only the 'CodeType' tag is used (internally); the
--   name is never written out, since MATLAB code has no type annotations.
mlTy :: CodeType -> String -> VS (MatlabCode TypeData)
mlTy :: CodeType -> Label -> VS (MatlabCode TypeData)
mlTy CodeType
c Label
s = CodeType -> Label -> Doc -> VS (MatlabCode TypeData)
forall (r :: * -> *).
Monad r =>
CodeType -> Label -> Doc -> VS (r TypeData)
typeFromData CodeType
c Label
s (Label -> Doc
text Label
s)

-- | A MATLAB parameter renders as just the variable name.
mlParam :: MatlabCode Variable -> Doc
mlParam :: MatlabCode Variable -> Doc
mlParam = MatlabCode Variable -> Doc
forall (r :: * -> *). InternalVarElim r => r Variable -> Doc
RC.variable

-- | Renders a MATLAB function: @function [outs] = name(ins) ... end@.
--   With no outputs the @[outs] =@ part is dropped; with a single output the
--   brackets are dropped (@function out = name(ins)@).
mlFuncDoc :: (ParamElim r) => Label -> [Doc] ->
  [r ParamData] -> Doc -> Doc
mlFuncDoc :: forall (r :: * -> *).
ParamElim r =>
Label -> [Doc] -> [r ParamData] -> Doc -> Doc
mlFuncDoc Label
n [Doc]
outs [r ParamData]
pms Doc
bod =
  [Doc] -> Doc
vcat [Label -> Doc
text Label
"function" Doc -> Doc -> Doc
<+> (Doc
retDoc Doc -> Doc -> Doc
<> Label -> Doc
text Label
n) Doc -> Doc -> Doc
<> Doc -> Doc
parens ([r ParamData] -> Doc
forall (r :: * -> *). ParamElim r => [r ParamData] -> Doc
R.parameterList [r ParamData]
pms),
        Doc -> Doc
indent Doc
bod,
        Label -> Doc
text Label
"end"]
  where retDoc :: Doc
retDoc = case [Doc]
outs of
          []  -> Doc
empty
          [Doc
o] -> Doc
o Doc -> Doc -> Doc
<+> Doc
equals Doc -> Doc -> Doc
<> Label -> Doc
text Label
" "
          [Doc]
os  -> Doc -> Doc
brackets ([Doc] -> Doc
hcat (Doc -> [Doc] -> [Doc]
punctuate (Label -> Doc
text Label
", ") [Doc]
os)) Doc -> Doc -> Doc
<+> Doc
equals Doc -> Doc -> Doc
<> Label -> Doc
text Label
" "

-- Comments
mlCmtStart :: Doc
mlCmtStart :: Doc
mlCmtStart = Label -> Doc
text Label
"%"

-- | Makes a MATLAB comment. Every line starts with %.
--   (We avoid %{ %} blocks: those need the markers alone on a line.)
mlLineCmt :: [String] -> Doc
mlLineCmt :: [Label] -> Doc
mlLineCmt = [Doc] -> Doc
vcat ([Doc] -> Doc) -> ([Label] -> [Doc]) -> [Label] -> Doc
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Label -> Doc) -> [Label] -> [Doc]
forall a b. (a -> b) -> [a] -> [b]
map ((Doc
mlCmtStart Doc -> Doc -> Doc
<+>) (Doc -> Doc) -> (Label -> Doc) -> Label -> Doc
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Label -> Doc
text)

-- | A stand-in print function. mlPrint never uses it, but it must be a real
--   value so the print methods type-check.
mlPrintFunc :: SValue MatlabCode
mlPrintFunc :: SValue MatlabCode
mlPrintFunc = VS (MatlabCode TypeData) -> Doc -> SValue MatlabCode
forall (r :: * -> *).
RenderValue r =>
VS (r TypeData) -> Doc -> SValue r
mkStateVal VS (MatlabCode TypeData)
forall (r :: * -> *). TypeSym r => VS (r TypeData)
void (Label -> Doc
text Label
"fprintf")

-- | Gets a command-line argument: argv(){n}.
--   (Octave keeps the args in a cell, so we index with {}.)
-- | Wraps the main body as the entry function: @function <name>(varargin) ...
--   end@. Empty if there is no main body.
mlMainFunc :: Label -> FS Doc
mlMainFunc :: Label -> FS Doc
mlMainFunc Label
n = do
  b <- FS Doc
getMainDoc
  pure $ emptyIfEmpty b $ vcat
    [text "function" <+> text n <> parens (text "varargin"),
     indent b,
     text "end"]

mlArg :: SValue MatlabCode -> SValue MatlabCode
mlArg :: SValue MatlabCode -> SValue MatlabCode
mlArg SValue MatlabCode
n' = do
  n <- SValue MatlabCode
n'
  s <- string
  mkVal s (text "varargin" <> braces (RC.value n))

-- | Indexes into a vector. MATLAB is 1-indexed while GOOL is 0-indexed, so the
--   index is translated with 'intToIndex' (which folds constants, e.g. @a(1)@
--   for index 0, and yields @a(i + 1)@ for a variable @i@).
mlVecIndex :: SValue MatlabCode -> SValue MatlabCode -> SValue MatlabCode
mlVecIndex :: SValue MatlabCode -> SValue MatlabCode -> SValue MatlabCode
mlVecIndex SValue MatlabCode
v' SValue MatlabCode
i' = do
  v <- SValue MatlabCode
v'
  i <- intToIndex i'
  d <- double
  mkVal d (RC.value v <> parens (RC.value i))

-- | A list/vector type. MATLAB never writes types, so only the underlying
--   'CodeType' matters; arrays and lists share this native vector type.
mlListType :: VS (MatlabCode TypeData) -> VS (MatlabCode TypeData)
mlListType :: VS (MatlabCode TypeData) -> VS (MatlabCode TypeData)
mlListType VS (MatlabCode TypeData)
t' = do
  t <- VS (MatlabCode TypeData)
t'
  mlTy (List $ getCodeType t) "vector"

-- | A vector literal, rendered as a MATLAB row vector.
mlLitList :: VS (MatlabCode TypeData) -> [SValue MatlabCode] -> SValue MatlabCode
mlLitList :: VS (MatlabCode TypeData)
-> [SValue MatlabCode] -> SValue MatlabCode
mlLitList VS (MatlabCode TypeData)
t [SValue MatlabCode]
es = do
  elems <- [SValue MatlabCode]
-> StateT ValueState Identity [MatlabCode Value]
forall (t :: * -> *) (m :: * -> *) a.
(Traversable t, Monad m) =>
t (m a) -> m (t a)
forall (m :: * -> *) a. Monad m => [m a] -> m [a]
sequence [SValue MatlabCode]
es
  mkStateVal (listType t) (brackets (valueList elems))

-- | Reads one line from a file as text: fgetl(f).
mlReadLine :: SValue MatlabCode -> SValue MatlabCode
mlReadLine :: SValue MatlabCode -> SValue MatlabCode
mlReadLine SValue MatlabCode
f = PosCall MatlabCode
forall (r :: * -> *). ValueExpression r => PosCall r
funcApp Label
"fgetl" VS (MatlabCode TypeData)
forall (r :: * -> *). TypeSym r => VS (r TypeData)
string [SValue MatlabCode
f]

-- | Reads a value into v. Numbers go through str2double; text is kept as-is.
--   The type of v says which one to use.
mlInput :: SValue MatlabCode -> SVariable MatlabCode -> MS (MatlabCode (Doc, Terminator))
mlInput :: SValue MatlabCode
-> SVariable MatlabCode -> MS (MatlabCode (Doc, Terminator))
mlInput SValue MatlabCode
inSrc SVariable MatlabCode
v = SVariable MatlabCode
v SVariable MatlabCode
-> SValue MatlabCode -> MS (MatlabCode (Doc, Terminator))
forall (r :: * -> *) stmt.
AssignStatement r stmt =>
SVariable r -> SValue r -> MS (r stmt)
&= (SVariable MatlabCode
v SVariable MatlabCode
-> (MatlabCode Variable -> SValue MatlabCode) -> SValue MatlabCode
forall a b.
StateT ValueState Identity a
-> (a -> StateT ValueState Identity b)
-> StateT ValueState Identity b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= CodeType -> SValue MatlabCode
mlInput' (CodeType -> SValue MatlabCode)
-> (MatlabCode Variable -> CodeType)
-> MatlabCode Variable
-> SValue MatlabCode
forall b c a. (b -> c) -> (a -> b) -> a -> c
. MatlabCode TypeData -> CodeType
forall (r :: * -> *). TypeElim r => r TypeData -> CodeType
getCodeType (MatlabCode TypeData -> CodeType)
-> (MatlabCode Variable -> MatlabCode TypeData)
-> MatlabCode Variable
-> CodeType
forall b c a. (b -> c) -> (a -> b) -> a -> c
. MatlabCode Variable -> MatlabCode TypeData
forall (r :: * -> *). VariableElim r => r Variable -> r TypeData
variableType)
  where mlInput' :: CodeType -> SValue MatlabCode
mlInput' CodeType
Integer = PosCall MatlabCode
forall (r :: * -> *). ValueExpression r => PosCall r
funcApp Label
"str2double" VS (MatlabCode TypeData)
forall (r :: * -> *). TypeSym r => VS (r TypeData)
int [SValue MatlabCode
inSrc]
        mlInput' CodeType
Float   = PosCall MatlabCode
forall (r :: * -> *). ValueExpression r => PosCall r
funcApp Label
"str2double" VS (MatlabCode TypeData)
forall (r :: * -> *). TypeSym r => VS (r TypeData)
float [SValue MatlabCode
inSrc]
        mlInput' CodeType
Double  = PosCall MatlabCode
forall (r :: * -> *). ValueExpression r => PosCall r
funcApp Label
"str2double" VS (MatlabCode TypeData)
forall (r :: * -> *). TypeSym r => VS (r TypeData)
double [SValue MatlabCode
inSrc]
        mlInput' CodeType
String  = SValue MatlabCode
inSrc
        mlInput' CodeType
_ = Label -> SValue MatlabCode
forall a. HasCallStack => Label -> a
error Label
"Attempt to read a value of unreadable type"

-- | Prints a value: fprintf([fid, ]'fmt', value). The format is %s for text
--   and %g for numbers. A line-print adds \n. A file handle, if given, comes
--   first. (We always use fprintf, so the print-function argument is ignored.)
mlPrint :: Bool -> Maybe (SValue MatlabCode) -> SValue MatlabCode
  -> SValue MatlabCode -> MS (MatlabCode (Doc, Terminator))
mlPrint :: Bool
-> Maybe (SValue MatlabCode)
-> SValue MatlabCode
-> SValue MatlabCode
-> MS (MatlabCode (Doc, Terminator))
mlPrint Bool
newLn Maybe (SValue MatlabCode)
f' SValue MatlabCode
_ SValue MatlabCode
v' = do
  v  <- LensLike'
  (Zoomed (StateT ValueState Identity) (MatlabCode Value))
  MethodState
  ValueState
-> SValue MatlabCode
-> StateT MethodState Identity (MatlabCode Value)
forall c.
LensLike'
  (Zoomed (StateT ValueState Identity) c) MethodState ValueState
-> StateT ValueState Identity c -> StateT MethodState Identity c
forall (m :: * -> *) (n :: * -> *) s t c.
Zoom m n s t =>
LensLike' (Zoomed m c) t s -> m c -> n c
zoom LensLike'
  (Zoomed (StateT ValueState Identity) (MatlabCode Value))
  MethodState
  ValueState
(ValueState -> Focusing Identity (MatlabCode Value) ValueState)
-> MethodState -> Focusing Identity (MatlabCode Value) MethodState
Lens' MethodState ValueState
lensMStoVS SValue MatlabCode
v'
  mf <- traverse (zoom lensMStoVS) f'
  let fmt = case TypeData -> CodeType
cType (Value -> TypeData
valType (MatlabCode Value -> Value
forall a. MatlabCode a -> a
unMLC MatlabCode Value
v)) of
              CodeType
String -> Label
"%s"
              CodeType
Char   -> Label
"%c"
              CodeType
_      -> Label
"%g"
      nl = if Bool
newLn then Label
"\\n" else Label
""
      fileArg = Doc -> (MatlabCode Value -> Doc) -> Maybe (MatlabCode Value) -> Doc
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Doc
empty (\MatlabCode Value
fv -> MatlabCode Value -> Doc
forall (r :: * -> *). ValueElim r => r Value -> Doc
RC.value MatlabCode Value
fv Doc -> Doc -> Doc
<> Doc
listSep') Maybe (MatlabCode Value)
mf
  stmtFromData (text "fprintf" <>
    parens (fileArg <> text ("'" ++ fmt ++ nl ++ "'") <> listSep' <> RC.value v))
    Semi

mlInlineIf :: SValue MatlabCode -> SValue MatlabCode -> SValue MatlabCode
  -> SValue MatlabCode
mlInlineIf :: SValue MatlabCode
-> SValue MatlabCode -> SValue MatlabCode -> SValue MatlabCode
mlInlineIf SValue MatlabCode
c' SValue MatlabCode
v1' SValue MatlabCode
v2' = do
  c <- SValue MatlabCode
c'
  v1 <- v1'
  v2 <- v2'
  mkStateVal (toState $ valueType v1)
    (parens (parens (RC.value c) <+> text ".*" <+> parens (RC.value v1)
    <+> text "+ ~" <> parens (RC.value c) <+> text ".*" <+> parens (RC.value v2)))

mlCast :: VS (MatlabCode TypeData) -> SValue MatlabCode -> SValue MatlabCode
mlCast :: VS (MatlabCode TypeData) -> SValue MatlabCode -> SValue MatlabCode
mlCast VS (MatlabCode TypeData)
t' SValue MatlabCode
v' = do
  t <- VS (MatlabCode TypeData)
t'
  v <- v'
  let vTp = MatlabCode TypeData -> CodeType
forall (r :: * -> *). TypeElim r => r TypeData -> CodeType
getCodeType (MatlabCode TypeData -> CodeType)
-> MatlabCode TypeData -> CodeType
forall a b. (a -> b) -> a -> b
$ MatlabCode Value -> MatlabCode TypeData
forall (r :: * -> *). ValueSym r => r Value -> r TypeData
valueType MatlabCode Value
v
      tTp = MatlabCode TypeData -> CodeType
forall (r :: * -> *). TypeElim r => r TypeData -> CodeType
getCodeType MatlabCode TypeData
t
      rv  = MatlabCode Value -> SValue MatlabCode
forall a. a -> StateT ValueState Identity a
forall (m :: * -> *) a. Monad m => a -> m a
return MatlabCode Value
v
      rt  = MatlabCode TypeData -> VS (MatlabCode TypeData)
forall a. a -> StateT ValueState Identity a
forall (m :: * -> *) a. Monad m => a -> m a
return MatlabCode TypeData
t
  case (vTp, tTp) of
    (CodeType
String, CodeType
Integer) -> PosCall MatlabCode
forall (r :: * -> *). ValueExpression r => PosCall r
funcApp Label
"str2double" VS (MatlabCode TypeData)
rt [SValue MatlabCode
rv]
    (CodeType
String, CodeType
Float)   -> PosCall MatlabCode
forall (r :: * -> *). ValueExpression r => PosCall r
funcApp Label
"str2double" VS (MatlabCode TypeData)
rt [SValue MatlabCode
rv]
    (CodeType
String, CodeType
Double)  -> PosCall MatlabCode
forall (r :: * -> *). ValueExpression r => PosCall r
funcApp Label
"str2double" VS (MatlabCode TypeData)
rt [SValue MatlabCode
rv]
    (CodeType
String, CodeType
Boolean) -> PosCall MatlabCode
forall (r :: * -> *). ValueExpression r => PosCall r
funcApp Label
"logical" VS (MatlabCode TypeData)
rt [PosCall MatlabCode
forall (r :: * -> *). ValueExpression r => PosCall r
funcApp Label
"str2double" VS (MatlabCode TypeData)
forall (r :: * -> *). TypeSym r => VS (r TypeData)
double [SValue MatlabCode
rv]]
    (CodeType
_,      CodeType
String)  -> PosCall MatlabCode
forall (r :: * -> *). ValueExpression r => PosCall r
funcApp Label
"num2str" VS (MatlabCode TypeData)
rt [SValue MatlabCode
rv]
    (CodeType
_,      CodeType
Char)    -> PosCall MatlabCode
forall (r :: * -> *). ValueExpression r => PosCall r
funcApp Label
"char" VS (MatlabCode TypeData)
rt [SValue MatlabCode
rv]
    (CodeType, CodeType)
_                 -> MatlabCode Value -> SValue MatlabCode
forall a. a -> StateT ValueState Identity a
forall (m :: * -> *) a. Monad m => a -> m a
return MatlabCode Value
v

mlEqOp :: Bool -> SValue MatlabCode -> SValue MatlabCode -> SValue MatlabCode
mlEqOp :: Bool -> SValue MatlabCode -> SValue MatlabCode -> SValue MatlabCode
mlEqOp Bool
neg SValue MatlabCode
v1' SValue MatlabCode
v2' = do
  v1 <- SValue MatlabCode
v1'
  v2 <- v2'
  t  <- bool
  let tp = MatlabCode TypeData -> CodeType
forall (r :: * -> *). TypeElim r => r TypeData -> CodeType
getCodeType (MatlabCode Value -> MatlabCode TypeData
forall (r :: * -> *). ValueSym r => r Value -> r TypeData
valueType MatlabCode Value
v1)
      d  = Label -> Doc
text Label
"strcmp" Doc -> Doc -> Doc
<> Doc -> Doc
parens (MatlabCode Value -> Doc
forall (r :: * -> *). ValueElim r => r Value -> Doc
RC.value MatlabCode Value
v1 Doc -> Doc -> Doc
<> Doc
listSep' Doc -> Doc -> Doc
<> MatlabCode Value -> Doc
forall (r :: * -> *). ValueElim r => r Value -> Doc
RC.value MatlabCode Value
v2)
      strDoc = if Bool
neg then Label -> Doc
text Label
"~" Doc -> Doc -> Doc
<> Doc
d else Doc
d
  case tp of
    CodeType
String -> MatlabCode TypeData -> Doc -> SValue MatlabCode
forall (r :: * -> *).
RenderValue r =>
r TypeData -> Doc -> SValue r
mkVal MatlabCode TypeData
t Doc
strDoc
    CodeType
_      -> if Bool
neg then VSUnOp MatlabCode
-> VS (MatlabCode TypeData)
-> SValue MatlabCode
-> SValue MatlabCode
-> SValue MatlabCode
forall (r :: * -> *).
(OpElim r, RenderValue r, ValueElim r) =>
VSBinOp r -> VS (r TypeData) -> SValue r -> SValue r -> SValue r
typeBinExpr VSUnOp MatlabCode
forall (r :: * -> *). BinaryOpSym r => VSBinOp r
notEqualOp VS (MatlabCode TypeData)
forall (r :: * -> *). TypeSym r => VS (r TypeData)
bool (MatlabCode Value -> SValue MatlabCode
forall a. a -> StateT ValueState Identity a
forall (m :: * -> *) a. Monad m => a -> m a
return MatlabCode Value
v1) (MatlabCode Value -> SValue MatlabCode
forall a. a -> StateT ValueState Identity a
forall (m :: * -> *) a. Monad m => a -> m a
return MatlabCode Value
v2)
                     else VSUnOp MatlabCode
-> VS (MatlabCode TypeData)
-> SValue MatlabCode
-> SValue MatlabCode
-> SValue MatlabCode
forall (r :: * -> *).
(OpElim r, RenderValue r, ValueElim r) =>
VSBinOp r -> VS (r TypeData) -> SValue r -> SValue r -> SValue r
typeBinExpr VSUnOp MatlabCode
forall (r :: * -> *). BinaryOpSym r => VSBinOp r
equalOp VS (MatlabCode TypeData)
forall (r :: * -> *). TypeSym r => VS (r TypeData)
bool (MatlabCode Value -> SValue MatlabCode
forall a. a -> StateT ValueState Identity a
forall (m :: * -> *) a. Monad m => a -> m a
return MatlabCode Value
v1) (MatlabCode Value -> SValue MatlabCode
forall a. a -> StateT ValueState Identity a
forall (m :: * -> *) a. Monad m => a -> m a
return MatlabCode Value
v2)

mlListDec :: SVariable MatlabCode -> MatlabCode ScopeData
  -> MS (MatlabCode (Doc, Terminator))
mlListDec :: SVariable MatlabCode
-> MatlabCode ScopeData -> MS (MatlabCode (Doc, Terminator))
mlListDec SVariable MatlabCode
v MatlabCode ScopeData
scp = do
  vr <- LensLike'
  (Zoomed (StateT ValueState Identity) (MatlabCode Variable))
  MethodState
  ValueState
-> SVariable MatlabCode
-> StateT MethodState Identity (MatlabCode Variable)
forall c.
LensLike'
  (Zoomed (StateT ValueState Identity) c) MethodState ValueState
-> StateT ValueState Identity c -> StateT MethodState Identity c
forall (m :: * -> *) (n :: * -> *) s t c.
Zoom m n s t =>
LensLike' (Zoomed m c) t s -> m c -> n c
zoom LensLike'
  (Zoomed (StateT ValueState Identity) (MatlabCode Variable))
  MethodState
  ValueState
(ValueState -> Focusing Identity (MatlabCode Variable) ValueState)
-> MethodState
-> Focusing Identity (MatlabCode Variable) MethodState
Lens' MethodState ValueState
lensMStoVS SVariable MatlabCode
v
  let emptyInit = case MatlabCode TypeData -> CodeType
forall (r :: * -> *). TypeElim r => r TypeData -> CodeType
getCodeType (MatlabCode Variable -> MatlabCode TypeData
forall (r :: * -> *). VariableElim r => r Variable -> r TypeData
variableType MatlabCode Variable
vr) of
        List CodeType
String -> Doc -> Doc
braces Doc
empty
        CodeType
_           -> Doc -> Doc
brackets Doc
empty
  CS.varDecDef (return vr) scp
    (Just (mkStateVal (toState $ variableType vr) emptyInit))

mlReadAllLines :: SValue MatlabCode -> SVariable MatlabCode
  -> MS (MatlabCode (Doc, Terminator))
mlReadAllLines :: SValue MatlabCode
-> SVariable MatlabCode -> MS (MatlabCode (Doc, Terminator))
mlReadAllLines SValue MatlabCode
f SVariable MatlabCode
v = do
  let var_line :: SVariable MatlabCode
var_line = Label -> VS (MatlabCode TypeData) -> SVariable MatlabCode
forall (r :: * -> *).
VariableSym r =>
Label -> VS (r TypeData) -> SVariable r
var Label
"mlLine" VS (MatlabCode TypeData)
forall (r :: * -> *). TypeSym r => VS (r TypeData)
string
      v_line :: SValue MatlabCode
v_line   = SVariable MatlabCode -> SValue MatlabCode
forall (r :: * -> *). VariableValue r => SVariable r -> SValue r
valueOf SVariable MatlabCode
var_line
      readLine :: SValue MatlabCode
readLine = PosCall MatlabCode
forall (r :: * -> *). ValueExpression r => PosCall r
funcApp Label
"fgetl" VS (MatlabCode TypeData)
forall (r :: * -> *). TypeSym r => VS (r TypeData)
string [SValue MatlabCode
f]
      cond :: SValue MatlabCode
cond     = PosCall MatlabCode
forall (r :: * -> *). ValueExpression r => PosCall r
funcApp Label
"ischar" VS (MatlabCode TypeData)
forall (r :: * -> *). TypeSym r => VS (r TypeData)
bool [SValue MatlabCode
v_line]
  [MS (MatlabCode (Doc, Terminator))]
-> MS (MatlabCode (Doc, Terminator))
forall {k} (r :: k -> *) (stmt :: k).
MultiStatement r stmt =>
[MS (r stmt)] -> MS (r stmt)
multi [SVariable MatlabCode
var_line SVariable MatlabCode
-> SValue MatlabCode -> MS (MatlabCode (Doc, Terminator))
forall (r :: * -> *) stmt.
AssignStatement r stmt =>
SVariable r -> SValue r -> MS (r stmt)
&= SValue MatlabCode
readLine,
    SValue MatlabCode
-> MS (MatlabCode Doc) -> MS (MatlabCode (Doc, Terminator))
forall (r :: * -> *) stmt bod.
ControlStatement r stmt bod =>
SValue r -> MS (r bod) -> MS (r stmt)
while SValue MatlabCode
cond ([MS (MatlabCode (Doc, Terminator))] -> MS (MatlabCode Doc)
forall {k} (r :: k -> *) (block :: k) (stmt :: k) (bod :: k).
(BlockSym r block stmt, BodySym r bod block) =>
[MS (r stmt)] -> MS (r bod)
bodyStatements
      [SValue MatlabCode
-> SValue MatlabCode -> MS (MatlabCode (Doc, Terminator))
forall (r :: * -> *) stmt.
ListStatement r stmt =>
SValue r -> SValue r -> MS (r stmt)
listAppend (SVariable MatlabCode -> SValue MatlabCode
forall (r :: * -> *). VariableValue r => SVariable r -> SValue r
valueOf SVariable MatlabCode
v) SValue MatlabCode
v_line,
       SVariable MatlabCode
var_line SVariable MatlabCode
-> SValue MatlabCode -> MS (MatlabCode (Doc, Terminator))
forall (r :: * -> *) stmt.
AssignStatement r stmt =>
SVariable r -> SValue r -> MS (r stmt)
&= SValue MatlabCode
readLine])]

mlEnd, mlElseIf :: Doc
mlEnd :: Doc
mlEnd = Label -> Doc
text Label
"end"
mlElseIf :: Doc
mlElseIf = Label -> Doc
text Label
"elseif"

mlForEach
  :: (CommonRenderSym r vis stmt mthd bod block)
  => r Variable -> r Value -> r bod -> Doc
mlForEach :: forall (r :: * -> *) vis stmt mthd bod block.
CommonRenderSym r vis stmt mthd bod block =>
r Variable -> r Value -> r bod -> Doc
mlForEach r Variable
i r Value
lstVar r bod
b = [Doc] -> Doc
vcat [
  Label -> Doc
text Label
"for" Doc -> Doc -> Doc
<+> r Variable -> Doc
forall (r :: * -> *). InternalVarElim r => r Variable -> Doc
RC.variable r Variable
i Doc -> Doc -> Doc
<+> Doc
equals Doc -> Doc -> Doc
<+> r Value -> Doc
forall (r :: * -> *). ValueElim r => r Value -> Doc
RC.value r Value
lstVar,
  Doc -> Doc
indent (Doc -> Doc) -> Doc -> Doc
forall a b. (a -> b) -> a -> b
$ r bod -> Doc
forall {k} (r :: k -> *) (bod :: k). BodyElim r bod => r bod -> Doc
RC.body r bod
b,
  Doc
mlEnd]

mlRange
  :: (CommonRenderSym r vis stmt mthd bod block)
  => SValue r -> SValue r -> SValue r -> SValue r
mlRange :: forall (r :: * -> *) vis stmt mthd bod block.
CommonRenderSym r vis stmt mthd bod block =>
SValue r -> SValue r -> SValue r -> SValue r
mlRange SValue r
initv SValue r
finalv SValue r
stepv = do
  ini <- SValue r
initv
  fin <- finalv
  stp <- stepv
  d <- double
  mkVal d (RC.value ini <> text ":" <> RC.value stp <> text ":" <> RC.value fin)

mlTryCatch
  :: (CommonRenderSym r vis stmt mthd bod block) => r bod -> r bod -> Doc
mlTryCatch :: forall (r :: * -> *) vis stmt mthd bod block.
CommonRenderSym r vis stmt mthd bod block =>
r bod -> r bod -> Doc
mlTryCatch r bod
tryB r bod
catchB = [Doc] -> Doc
vcat [
  Label -> Doc
text Label
"try",
  Doc -> Doc
indent (Doc -> Doc) -> Doc -> Doc
forall a b. (a -> b) -> a -> b
$ r bod -> Doc
forall {k} (r :: k -> *) (bod :: k). BodyElim r bod => r bod -> Doc
RC.body r bod
tryB,
  Label -> Doc
text Label
"catch" Doc -> Doc -> Doc
<+> Label -> Doc
text Label
"e",
  Doc -> Doc
indent (Doc -> Doc) -> Doc -> Doc
forall a b. (a -> b) -> a -> b
$ r bod -> Doc
forall {k} (r :: k -> *) (bod :: k). BodyElim r bod => r bod -> Doc
RC.body r bod
catchB,
  Doc
mlEnd]

mlArrayElem :: SValue MatlabCode -> SValue MatlabCode -> SVariable MatlabCode
mlArrayElem :: SValue MatlabCode -> SValue MatlabCode -> SVariable MatlabCode
mlArrayElem SValue MatlabCode
arr' SValue MatlabCode
i' = do
  i <- SValue MatlabCode -> SValue MatlabCode
forall (r :: * -> *). IndexTranslator r => SValue r -> SValue r
intToIndex SValue MatlabCode
i'
  arr <- arr'
  mkStateVar (render $ RC.value arr) (A.innerType $ return $ valueType arr)
    (RC.value arr <> parens (RC.value i))

mlListAdd :: SValue MatlabCode -> SValue MatlabCode -> SValue MatlabCode
  -> MS (MatlabCode (Doc, Terminator))
mlListAdd :: SValue MatlabCode
-> SValue MatlabCode
-> SValue MatlabCode
-> MS (MatlabCode (Doc, Terminator))
mlListAdd SValue MatlabCode
lst' SValue MatlabCode
idx' SValue MatlabCode
val' = do
  lst <- LensLike'
  (Zoomed (StateT ValueState Identity) (MatlabCode Value))
  MethodState
  ValueState
-> SValue MatlabCode
-> StateT MethodState Identity (MatlabCode Value)
forall c.
LensLike'
  (Zoomed (StateT ValueState Identity) c) MethodState ValueState
-> StateT ValueState Identity c -> StateT MethodState Identity c
forall (m :: * -> *) (n :: * -> *) s t c.
Zoom m n s t =>
LensLike' (Zoomed m c) t s -> m c -> n c
zoom LensLike'
  (Zoomed (StateT ValueState Identity) (MatlabCode Value))
  MethodState
  ValueState
(ValueState -> Focusing Identity (MatlabCode Value) ValueState)
-> MethodState -> Focusing Identity (MatlabCode Value) MethodState
Lens' MethodState ValueState
lensMStoVS SValue MatlabCode
lst'
  idx <- zoom lensMStoVS (intToIndex idx')
  v   <- zoom lensMStoVS val'
  let lstDoc = MatlabCode Value -> Doc
forall (r :: * -> *). ValueElim r => r Value -> Doc
RC.value MatlabCode Value
lst
      idxDoc = MatlabCode Value -> Doc
forall (r :: * -> *). ValueElim r => r Value -> Doc
RC.value MatlabCode Value
idx
      valDoc = MatlabCode Value -> Doc
forall (r :: * -> *). ValueElim r => r Value -> Doc
RC.value MatlabCode Value
v
      lvar = Label -> VS (MatlabCode TypeData) -> Doc -> SVariable MatlabCode
forall (r :: * -> *).
RenderVariable r =>
Label -> VS (r TypeData) -> Doc -> SVariable r
mkStateVar (Doc -> Label
render Doc
lstDoc) (MatlabCode TypeData -> VS (MatlabCode TypeData)
forall a s. a -> State s a
toState (MatlabCode TypeData -> VS (MatlabCode TypeData))
-> MatlabCode TypeData -> VS (MatlabCode TypeData)
forall a b. (a -> b) -> a -> b
$ MatlabCode Value -> MatlabCode TypeData
forall (r :: * -> *). ValueSym r => r Value -> r TypeData
valueType MatlabCode Value
lst) Doc
lstDoc
  lvar &= mkStateVal (toState $ valueType lst)
    (brackets (lstDoc <> parens (text "1:" <> parens (idxDoc <+> text "- 1"))
     <> text "," <+> valDoc
     <> text "," <+> lstDoc <> parens (idxDoc <> text ":end")))

mlListSet :: SValue MatlabCode -> SValue MatlabCode -> SValue MatlabCode
  -> MS (MatlabCode (Doc, Terminator))
mlListSet :: SValue MatlabCode
-> SValue MatlabCode
-> SValue MatlabCode
-> MS (MatlabCode (Doc, Terminator))
mlListSet SValue MatlabCode
lst' SValue MatlabCode
idx' SValue MatlabCode
val' = do
  lst <- LensLike'
  (Zoomed (StateT ValueState Identity) (MatlabCode Value))
  MethodState
  ValueState
-> SValue MatlabCode
-> StateT MethodState Identity (MatlabCode Value)
forall c.
LensLike'
  (Zoomed (StateT ValueState Identity) c) MethodState ValueState
-> StateT ValueState Identity c -> StateT MethodState Identity c
forall (m :: * -> *) (n :: * -> *) s t c.
Zoom m n s t =>
LensLike' (Zoomed m c) t s -> m c -> n c
zoom LensLike'
  (Zoomed (StateT ValueState Identity) (MatlabCode Value))
  MethodState
  ValueState
(ValueState -> Focusing Identity (MatlabCode Value) ValueState)
-> MethodState -> Focusing Identity (MatlabCode Value) MethodState
Lens' MethodState ValueState
lensMStoVS SValue MatlabCode
lst'
  idx <- zoom lensMStoVS (intToIndex idx')
  let wrap = case MatlabCode TypeData -> CodeType
forall (r :: * -> *). TypeElim r => r TypeData -> CodeType
getCodeType (MatlabCode Value -> MatlabCode TypeData
forall (r :: * -> *). ValueSym r => r Value -> r TypeData
valueType MatlabCode Value
lst) of
        List CodeType
t -> CodeType -> Doc -> Doc
mlCellWrap CodeType
t
        CodeType
_      -> Doc -> Doc
parens
      lvar = Label -> VS (MatlabCode TypeData) -> Doc -> SVariable MatlabCode
forall (r :: * -> *).
RenderVariable r =>
Label -> VS (r TypeData) -> Doc -> SVariable r
mkStateVar (Doc -> Label
render (Doc -> Label) -> Doc -> Label
forall a b. (a -> b) -> a -> b
$ MatlabCode Value -> Doc
forall (r :: * -> *). ValueElim r => r Value -> Doc
RC.value MatlabCode Value
lst)
               (VS (MatlabCode TypeData) -> VS (MatlabCode TypeData)
forall (r :: * -> *).
TypeElim r =>
VS (r TypeData) -> VS (r TypeData)
A.innerType (VS (MatlabCode TypeData) -> VS (MatlabCode TypeData))
-> VS (MatlabCode TypeData) -> VS (MatlabCode TypeData)
forall a b. (a -> b) -> a -> b
$ MatlabCode TypeData -> VS (MatlabCode TypeData)
forall a. a -> StateT ValueState Identity a
forall (m :: * -> *) a. Monad m => a -> m a
return (MatlabCode TypeData -> VS (MatlabCode TypeData))
-> MatlabCode TypeData -> VS (MatlabCode TypeData)
forall a b. (a -> b) -> a -> b
$ MatlabCode Value -> MatlabCode TypeData
forall (r :: * -> *). ValueSym r => r Value -> r TypeData
valueType MatlabCode Value
lst)
               (MatlabCode Value -> Doc
forall (r :: * -> *). ValueElim r => r Value -> Doc
RC.value MatlabCode Value
lst Doc -> Doc -> Doc
<> Doc -> Doc
wrap (MatlabCode Value -> Doc
forall (r :: * -> *). ValueElim r => r Value -> Doc
RC.value MatlabCode Value
idx))
  lvar &= val'