@@ -128,6 +128,8 @@ instance PrettyPrint DiagnosticData where
128
128
pure [" unit $" ++ show un ++ " " ++ stat ++ " $" ++ show tn]
129
129
pprint (UnitDs units) =
130
130
pure [" units" ++ foldMap (\ u -> " $" ++ show (nonce u)) units]
131
+ pprint (ReadFromD filename vars) =
132
+ pure [" readfrom" ++ filename ++ " " ++ show vars]
131
133
132
134
instance PrettyPrint Frame where
133
135
pprint (Declaration n UserDecl {.. }) = do
@@ -165,6 +167,7 @@ data DiagnosticData
165
167
Expr {- e, t = elab e -}
166
168
| UnitD Nonce BOOL Nonce
167
169
| UnitDs [WithSource String ]
170
+ | ReadFromD String [Either String (String , TYPE )]
168
171
deriving Show
169
172
170
173
data Frame where
@@ -1966,6 +1969,15 @@ runDirective rl (dir :<=: src, body) = do
1966
1969
InputFormat name | Just (InputFormatBody body) <- body -> do
1967
1970
newProb $ InputFormatAction name body rl
1968
1971
run
1972
+ ReadFrom filename variables -> do
1973
+ vs <- for variables $ \ v -> findDeclaration (UserDecl {varTy = Nothing , currentName = v, seen = False , newNames = [] , capturable = False , whereAmI = MatLab }) >>= \ case
1974
+ Just (name, ty) -> do
1975
+ pushDefinition name (E $^ M (name, Zy ) $^ S0 $^ U :^ Ze )
1976
+ pure $ Right (v, ty)
1977
+ Nothing -> pure $ Left $ " Unknown variable " ++ v
1978
+ push $ Diagnostic rl (ReadFromD filename vs)
1979
+ newProb $ Done nil
1980
+ run
1969
1981
Declare xs ty -> do
1970
1982
(sol, prob) <- elab " declType" emptyContext (atom SType ) (TensorTask <$> ty)
1971
1983
pushProblems (Sourced . fmap (DeclareAction sol) <$> xs)
@@ -2871,6 +2883,10 @@ normaliseFrame = \case
2871
2883
stat <- normalise VN (atom STwo ) stat
2872
2884
pure $ UnitD un stat tn
2873
2885
normaliseDiagnostic d@ (UnitDs _) = pure d
2886
+ normaliseDiagnostic d@ (ReadFromD filename vs) = do
2887
+ vs <- traverse (either (pure . Left )
2888
+ (\ (s, ty) -> (Right . (s,)) <$> normalise emptyContext (atom SType ) ty)) vs
2889
+ pure $ ReadFromD filename vs
2874
2890
2875
2891
cleanup :: Mood -> Elab ()
2876
2892
cleanup mood = do
@@ -2952,7 +2968,53 @@ diagnosticRun = llup >>= \case
2952
2968
units ++
2953
2969
[ spc dent, sym " %<}"
2954
2970
]
2955
-
2971
+ ReadFromD filename vars -> go vars >>= \ case
2972
+ Left err -> pure $ [ Tok " \n " Ret dump, spc dent, sym " %<" , spc 1 ] ++ err
2973
+ Right code -> pure $ [ Tok " \n " Ret dump, spc dent, sym " %<{" ]
2974
+ ++ concatMap (\ x -> Tok " \n " Ret dump: spc dent: x)
2975
+ (preamble ++ code) ++ [ Tok " \n " Ret dump, spc dent, sym " %<}" ]
2976
+ where
2977
+ preamble = [[sym (" h=fopen(\' " ++ filename ++ " \' );" )]
2978
+ ,[sym " c=textscan(h,\' %f\' );" ]
2979
+ ,[sym " fclose(h);" ]
2980
+ ,[sym " src = c{1};" ]
2981
+ ,[sym " readPtr = 1;" ]
2982
+ ]
2983
+ go :: [Either String (String , TYPE )] -> Elab (Either [Tok ] [[Tok ]])
2984
+ go [] = pure $ Right []
2985
+ go (Left err: vs) = pure $ Left [ sym err ]
2986
+ go (Right (v, ty): vs) = readFromType emptyContext v ty >>= \ case
2987
+ Right code -> go vs >>= \ case
2988
+ Left err -> pure $ Left $ err
2989
+ Right codes -> pure $ Right $ code ++ codes
2990
+ Left err -> pure $ Left err
2991
+
2992
+ readFromType :: NATTY n => Context n -> String -> Typ ^ n -> Elab (Either [Tok ] [[Tok ]])
2993
+ readFromType ctx var ty = do
2994
+ ty <- normalise ctx (atom SType ) ty
2995
+ case tagEh ty of
2996
+ _ | ty == doubleType -< no (scopeOf ty) -> pure $ Right $ readSingle
2997
+ Just (SAbel , [ty]) | isUnitType ty -> pure $ Right readSingle
2998
+ Just (SMatrix , [rowGenTy, colGenTy, cellTy, rs, cs])
2999
+ | Just (i, cellTy) <- lamNameEh cellTy, Just (j, cellTy) <- lamNameEh cellTy
3000
+ , rs <- listView rs, cs <- listView cs, all isRight rs, all isRight cs -> do
3001
+ let ctx' = ctx \\\ (i, mk SAbel rowGenTy) \\\ (j, wk $ mk SAbel colGenTy)
3002
+ cellTy <- normalise ctx' (atom SType ) cellTy
3003
+ case (length rs, length cs) of
3004
+ (1 , 1 ) -> readFromType ctx' var cellTy
3005
+ (1 , c) -> fmap (\ s -> [[sym (" for i = 1:" ++ show c)]] ++ map (spc 2 : ) s ++ [[sym " end" ]])
3006
+ <$> (readFromType ctx' (var ++ " (i)" ) cellTy)
3007
+ (r, 1 ) -> fmap (\ s -> [[sym (" for i = 1:" ++ show r)]] ++ map (spc 2 : ) s ++ [[sym " end" ]])
3008
+ <$> (readFromType ctx' (var ++ " (i,1)" ) cellTy)
3009
+ (r, c) -> fmap (\ s -> [[sym (" for i = 1:" ++ show r)], [spc 2 , sym (" for j = 1:" ++ show c)]]
3010
+ ++ map (spc 4 : ) s ++ [[spc 4 , sym " end" ], [sym " end" ]])
3011
+ <$> (readFromType ctx' (var ++ " (i,j)" ) cellTy)
3012
+ Just (SQuantity , [enumTy, exp ]) -> pure $ Right $ readSingle -- TODO: require unit
3013
+ _ -> do
3014
+ ty <- unelabType ctx ty
3015
+ pure $ Left ([sym " I do not know how to generate code to read from type " ] ++ ty)
3016
+ where
3017
+ readSingle = [[sym (var ++ " = src(readPtr);" )], [sym " readPtr = readPtr + 1;" ]]
2956
3018
2957
3019
metaStatus :: Name -> Elab Status
2958
3020
metaStatus x = do
0 commit comments