mirror of
https://github.com/carp-lang/Carp.git
synced 2024-09-11 05:25:28 +03:00
templates: add Array.allocate; core: rewrite repeat and replicate in Carp
This commit is contained in:
parent
5e777a601f
commit
0f86e15f0e
@ -115,9 +115,7 @@ repl context readSoFar =
|
||||
arrayModule :: Env
|
||||
arrayModule = Env { envBindings = bindings, envParent = Nothing, envModuleName = Just "Array", envUseModules = [], envMode = ExternalEnv }
|
||||
where bindings = Map.fromList [ templateNth
|
||||
, templateReplicate
|
||||
, templateRepeat
|
||||
, templateRepeatIndexed
|
||||
, templateAllocate
|
||||
, templateCopyingMap
|
||||
, templateEMap
|
||||
, templateFilter
|
||||
|
@ -107,12 +107,14 @@
|
||||
|
||||
; cannot use for, because we want also be able to go downwards
|
||||
(defn range [start end step]
|
||||
(let-do [x []
|
||||
(let-do [x (allocate (Int.inc (Int.abs (/ (- end start) step))))
|
||||
e start
|
||||
i 0
|
||||
op (if (< start end) <= >=)]
|
||||
(while (op e end)
|
||||
(do
|
||||
(set! &x (push-back x e))
|
||||
(aset! &x i e)
|
||||
(set! &i (Int.inc i))
|
||||
(set! &e (+ e step))))
|
||||
x))
|
||||
|
||||
@ -121,4 +123,19 @@
|
||||
|
||||
(defn sort [a]
|
||||
(sort-with a cmp))
|
||||
|
||||
(defn repeat [n f]
|
||||
(let-do [a (allocate n)]
|
||||
(for [i 0 n] (aset! &a i (f)))
|
||||
a))
|
||||
|
||||
(defn repeat-indexed [n f]
|
||||
(let-do [a (allocate n)]
|
||||
(for [i 0 n] (aset! &a i (f i)))
|
||||
a))
|
||||
|
||||
(defn replicate [n e]
|
||||
(let-do [a (allocate n)]
|
||||
(for [i 0 n] (aset! &a i @e))
|
||||
a))
|
||||
)
|
||||
|
@ -186,87 +186,6 @@ templateSort = defineTypeParameterizedTemplate templateCreator path t
|
||||
,defineArrayTypeAlias arrayType] ++
|
||||
depsForDeleteFunc typeEnv env arrayType)
|
||||
|
||||
templateReplicate :: (String, Binder)
|
||||
templateReplicate = defineTypeParameterizedTemplate templateCreator path t
|
||||
where path = SymPath ["Array"] "replicate"
|
||||
t = FuncTy [IntTy, RefTy (VarTy "t")] (StructTy "Array" [VarTy "t"])
|
||||
templateCreator = TemplateCreator $
|
||||
\typeEnv env ->
|
||||
Template
|
||||
t
|
||||
(const (toTemplate "Array $NAME(int n, $t *elem)"))
|
||||
(\(FuncTy [_, _] arrayType) ->
|
||||
let StructTy _ [insideType] = arrayType
|
||||
copierType = FuncTy [RefTy insideType] insideType
|
||||
copierPath = if isManaged typeEnv insideType -- TODO: also check if it's an external function
|
||||
then case nameOfPolymorphicFunction typeEnv env copierType "copy" of
|
||||
Just p -> Just p
|
||||
Nothing -> error ("Can't find copy function for array type: " ++ show insideType)
|
||||
else Nothing
|
||||
in
|
||||
toTemplate $ unlines [ "$DECL {"
|
||||
, " Array a; a.len = n; a.data = CARP_MALLOC(sizeof($t) * n);"
|
||||
, " for(int i = 0; i < n; ++i) {"
|
||||
, " (($t*)a.data)[i] = " ++ case copierPath of
|
||||
Just p -> pathToC p ++ "(elem);"
|
||||
Nothing -> "*elem;"
|
||||
, " }"
|
||||
, " return a;"
|
||||
, "}"])
|
||||
(\(FuncTy [_, _] arrayType) ->
|
||||
let StructTy _ [insideType] = arrayType
|
||||
in defineArrayTypeAlias arrayType :
|
||||
depsForDeleteFunc typeEnv env arrayType ++
|
||||
depsForCopyFunc typeEnv env insideType)
|
||||
|
||||
templateRepeat :: (String, Binder)
|
||||
templateRepeat = defineTypeParameterizedTemplate templateCreator path t
|
||||
where path = SymPath ["Array"] "repeat"
|
||||
t = FuncTy [IntTy, FuncTy [] (VarTy "t")] (StructTy "Array" [VarTy "t"])
|
||||
templateCreator = TemplateCreator $
|
||||
\typeEnv env ->
|
||||
Template
|
||||
t
|
||||
(const (toTemplate "Array $NAME(int n, $(Fn [] t) f)"))
|
||||
(const
|
||||
(toTemplate $ unlines
|
||||
[ "$DECL {"
|
||||
, " Array a; a.len = n; a.data = CARP_MALLOC(sizeof($t) * n);"
|
||||
, " for(int i = 0; i < n; ++i) {"
|
||||
, " (($t*)a.data)[i] = f();"
|
||||
, " }"
|
||||
, " return a;"
|
||||
, "}"]))
|
||||
(\(FuncTy [_, ft] arrayType) ->
|
||||
let StructTy _ [insideType] = arrayType
|
||||
in defineArrayTypeAlias arrayType : defineFunctionTypeAlias ft :
|
||||
depsForDeleteFunc typeEnv env arrayType)
|
||||
|
||||
|
||||
templateRepeatIndexed :: (String, Binder)
|
||||
templateRepeatIndexed = defineTypeParameterizedTemplate templateCreator path t
|
||||
where path = SymPath ["Array"] "repeat-indexed"
|
||||
t = FuncTy [IntTy, FuncTy [IntTy] (VarTy "t")] (StructTy "Array" [VarTy "t"])
|
||||
templateCreator = TemplateCreator $
|
||||
\typeEnv env ->
|
||||
Template
|
||||
t
|
||||
(const (toTemplate "Array $NAME(int n, $(Fn [int] t) f)"))
|
||||
(const
|
||||
(toTemplate $ unlines
|
||||
[ "$DECL {"
|
||||
, " Array a; a.len = n; a.data = CARP_MALLOC(sizeof($t) * n);"
|
||||
, " for(int i = 0; i < n; ++i) {"
|
||||
, " (($t*)a.data)[i] = f(i);"
|
||||
, " }"
|
||||
, " return a;"
|
||||
, "}"]))
|
||||
(\(FuncTy [_, ft] arrayType) ->
|
||||
let StructTy _ [insideType] = arrayType
|
||||
in defineArrayTypeAlias arrayType : defineFunctionTypeAlias ft :
|
||||
depsForDeleteFunc typeEnv env arrayType)
|
||||
|
||||
|
||||
templateRaw :: (String, Binder)
|
||||
templateRaw = defineTemplate
|
||||
(SymPath ["Array"] "raw")
|
||||
@ -321,6 +240,25 @@ templateCount = defineTypeParameterizedTemplate templateCreator path t
|
||||
[defineArrayTypeAlias arrayType] ++
|
||||
depsForDeleteFunc typeEnv env arrayType)
|
||||
|
||||
templateAllocate :: (String, Binder)
|
||||
templateAllocate = defineTypeParameterizedTemplate templateCreator path t
|
||||
where path = (SymPath ["Array"] "allocate")
|
||||
t = (FuncTy [IntTy] (StructTy "Array" [VarTy "t"]))
|
||||
templateCreator = TemplateCreator $
|
||||
\typeEnv env ->
|
||||
Template
|
||||
t
|
||||
(const (toTemplate "Array $NAME (int n)"))
|
||||
(const (toTemplate $ unlines ["$DECL {"
|
||||
," Array a;"
|
||||
," a.len = n;"
|
||||
," a.data = CARP_MALLOC(n*sizeof($t));"
|
||||
," return a;"
|
||||
,"}"]))
|
||||
(\(FuncTy [_] arrayType) ->
|
||||
[defineArrayTypeAlias arrayType] ++
|
||||
depsForDeleteFunc typeEnv env arrayType)
|
||||
|
||||
templateDeleteArray :: (String, Binder)
|
||||
templateDeleteArray = defineTypeParameterizedTemplate templateCreator path t
|
||||
where templateCreator = TemplateCreator $
|
||||
|
@ -12,6 +12,8 @@
|
||||
|
||||
(defn inc-ref [x] (+ @x 1))
|
||||
|
||||
(defn make-zero [] 0)
|
||||
|
||||
(defn main []
|
||||
(let [a (range 0 9 1)
|
||||
b (Array.replicate 5 "Hi")]
|
||||
@ -25,6 +27,14 @@
|
||||
(assert-false test
|
||||
(= &[1 2 3] &[1 2 34])
|
||||
"= works as expected")
|
||||
(assert-equal test
|
||||
&[0 0 0 0]
|
||||
&(replicate 4 &0)
|
||||
"replicate works as expected")
|
||||
(assert-equal test
|
||||
&[0 0 0 0]
|
||||
&(repeat 4 make-zero)
|
||||
"repeat works as expected")
|
||||
(assert-equal test
|
||||
1
|
||||
(first &[1 2 3])
|
||||
|
Loading…
Reference in New Issue
Block a user