on master help sign in
Added log/exp (and lib/link directives)
commit5afe67857d778017daec591d1149c3834acb9b32 authorEdwin Brady <eb@cs.st-andrews.ac.uk> dateFri 11 Nov 2011, 00:07 (15 years ago) parent2c05448
7 files changed+42−3
lib/prelude.idr+9 src/Idris/AbsSyntax.hs+1 src/Idris/Compiler.hs+1−1 src/Idris/ElabDecls.hs+1−1 src/Idris/Imports.hs+8 src/Idris/Parser.hs+9 src/Idris/Primitives.hs+13−1
lib/prelude.idr+9
@@ -85,6 +85,15 @@ instance MonadPlus Maybe where {
8585 mplus Nothing Nothing = Nothing;
8686}
8787
88+---- some mathematical operations
89+
90+%lib "m"
91+
92+exp : Float -> Float;
93+exp x = prim__floatExp x;
94+
95+log : Float -> Float;
96+log x = prim__floatLog x;
8897
8998---- some basic io
9099
src/Idris/AbsSyntax.hs+1
@@ -221,6 +221,7 @@ data PDecl' t = PFix FC Fixity [String] -- fixity declaration
221221 t -- full instance type
222222 [PDecl' t]
223223 | PSyntax FC Syntax
224+ | PDirective (Idris ())
224225 deriving Functor
225226
226227data PClause' t = PClause Name t [t] t [PDecl' t]
src/Idris/Compiler.hs+1−1
@@ -20,7 +20,7 @@ compile f = do ds <- mkDecls
2020 objs <- getObjectFiles
2121 libs <- getLibs
2222 lift $ compileObjWith [Debug] (mkProgram ds) (f ++ ".o")
23− lift $ link ((f ++ ".o") : objs) f
23+ lift $ link ((f ++ ".o") : objs ++ (map ("-l"++) libs)) f
2424
2525mkDecls :: Idris [EpicDecl]
2626mkDecls = do i <- getIState
src/Idris/ElabDecls.hs+1−1
@@ -458,7 +458,7 @@ elabDecl' info (PClass s f cs n ps ds) = do iLOG $ "Elaborating class " ++ show
458458elabDecl' info (PInstance s f cs n ps t ds)
459459 = do iLOG $ "Elaborating instance " ++ show n
460460 elabInstance info s f cs n ps t ds
461−
461+elabDecl' info (PDirective i) = i
462462
463463-- elabDecl' info (PImport i) = loadModule i
464464
src/Idris/Imports.hs+8
@@ -35,4 +35,12 @@ findImport (d:ds) fp = do let ibcp = ibcPath (d ++ "/" ++ fp)
3535 then return (IDR idrp)
3636 else findImport ds fp
3737
38+-- find a specific filename somewhere in a path
39+
40+findInPath :: [FilePath] -> FilePath -> IO FilePath
41+findInPath [] fp = fail $ "Can't find file " ++ fp
42+findInPath (d:ds) fp = do let p = d ++ "/" ++ fp
43+ e <- doesFileExist p
44+ if e then return p else findInPath ds p
45+
3846
src/Idris/Parser.hs+9
@@ -167,6 +167,7 @@ pDecl syn
167167 <|> pParams syn
168168 <|> pClass syn
169169 <|> pInstance syn
170+ <|> pDirective
170171 <|> try (do reserved "import"
171172 fp <- identifier
172173 lchar ';'
@@ -673,6 +674,14 @@ pWhereblock n syn
673674 lchar '}';
674675 return (concat ds, map (\x -> (x, decoration syn x)) dns)
675676
677+pDirective :: IParser [PDecl]
678+pDirective = do lchar '%'; reserved "lib"; lib <- strlit;
679+ return [PDirective (addLib lib)]
680+ <|> do lchar '%'; reserved "link"; obj <- strlit;
681+ return [PDirective (do datadir <- lift $ getDataDir
682+ o <- lift $ findInPath [".", datadir] obj
683+ addObjectFile o)]
684+
676685pTactic :: SyntaxInfo -> IParser PTactic
677686pTactic syn = do reserved "intro"; ns <- sepBy pName (lchar ',')
678687 return $ Intro ns
src/Idris/Primitives.hs+13−1
@@ -35,6 +35,9 @@ intToStr x = foreign_ tyString "intToStr" [(x, tyInt)]
3535strToFloat x = foreign_ tyFloat "strToFloat" [(x, tyString)]
3636floatToStr x = foreign_ tyString "floatToStr" [(x, tyFloat)]
3737
38+floatExp x = foreign_ tyFloat "exp" [(x, tyFloat)]
39+floatLog x = foreign_ tyFloat "log" [(x, tyFloat)]
40+
3841primitives =
3942 -- operators
4043 [Prim (UN ["prim__addInt"]) (ty [IType, IType] IType) 2 (iBin (+))
@@ -83,7 +86,11 @@ primitives =
8386 Prim (UN ["prim__strToFloat"]) (ty [StrType] FlType) 1 (c_strToFloat)
8487 ([E.name "x"], strToFloat (fun "x")),
8588 Prim (UN ["prim__floatToStr"]) (ty [FlType] StrType) 1 (c_floatToStr)
86− ([E.name "x"], floatToStr (fun "x"))
89+ ([E.name "x"], floatToStr (fun "x")),
90+ Prim (UN ["prim__floatExp"]) (ty [FlType] FlType) 1 (p_floatExp)
91+ ([E.name "x"], floatExp (fun "x")),
92+ Prim (UN ["prim__floatLog"]) (ty [FlType] FlType) 1 (p_floatLog)
93+ ([E.name "x"], floatLog (fun "x"))
8794 ]
8895
8996iBin op [VConstant (I x), VConstant (I y)] = Just $ VConstant (I (op x y))
@@ -111,6 +118,11 @@ c_floatToStr _ = Nothing
111118c_strToFloat [VConstant (Str x)] = Just $ VConstant (Fl (read x))
112119c_strToFloat _ = Nothing
113120
121+p_floatExp [VConstant (Fl x)] = Just $ VConstant (Fl (exp x))
122+p_floatExp _ = Nothing
123+p_floatLog [VConstant (Fl x)] = Just $ VConstant (Fl (log x))
124+p_floatLog _ = Nothing
125+
114126elabPrim :: Prim -> Idris ()
115127elabPrim (Prim n ty i def epic)
116128 = do updateContext (addOperator n ty i def)