compiler: generate documentation for compiler-generated functions automatically

This commit is contained in:
hellerve 2019-05-16 15:51:56 -04:00
parent d7f1e36cab
commit c091b36b23
14 changed files with 282 additions and 143 deletions

View File

@ -104,7 +104,7 @@ Returns `Nothing` if the array is empty.")
(break))))
eq)))
(doc = "compares two arrays and inverts the result.")
(doc /= "compares two arrays and inverts the result.")
(defn /= [a b]
(not (= (the (Ref (Array a)) a) b)))

View File

@ -154,7 +154,8 @@
(/= a b)
</pre>
<p class="doc">
<p>compares two arrays and inverts the result.</p>
</p>
</div>
<div class="binder">
@ -173,7 +174,7 @@
(= a b)
</pre>
<p class="doc">
<p>compares two arrays and inverts the result.</p>
<p>compares two arrays.</p>
</p>
</div>
@ -213,7 +214,8 @@
</span>
<p class="doc">
<p>allocates an uninitialized array. You can initialize members using <a href="#aset-uninitialized"><code>aset-uninitialized</code></a>.</p>
</p>
</div>
<div class="binder">
@ -252,7 +254,8 @@
</span>
<p class="doc">
<p>sets an array element at the index <code>n</code> to a new value.</p>
</p>
</div>
<div class="binder">
@ -271,7 +274,8 @@
</span>
<p class="doc">
<p>sets an array element at the index <code>n</code> to a new value in place.</p>
</p>
</div>
<div class="binder">
@ -290,7 +294,8 @@
</span>
<p class="doc">
<p>sets an uninitialized array member. The old member will not be deleted.</p>
</p>
</div>
<div class="binder">
@ -369,7 +374,8 @@
</span>
<p class="doc">
<p>copies an array.</p>
</p>
</div>
<div class="binder">
@ -430,7 +436,8 @@
</span>
<p class="doc">
<p>deletes an array. This function should usually not be called manually.</p>
</p>
</div>
<div class="binder">
@ -489,7 +496,8 @@
</span>
<p class="doc">
<p>filters array members using a function. This function takes ownership.</p>
</p>
</div>
<div class="binder">
@ -508,7 +516,8 @@
</span>
<p class="doc">
<p>applies a function <code>f</code> to an array <code>a</code>. The type of the elements cannot change.</p>
</p>
</div>
<div class="binder">
@ -650,7 +659,8 @@
</span>
<p class="doc">
<p>gets the length of the array.</p>
</p>
</div>
<div class="binder">
@ -709,7 +719,8 @@
</span>
<p class="doc">
<p>gets a reference to the <code>n</code>th element from an array <code>a</code>.</p>
</p>
</div>
<div class="binder">
@ -728,7 +739,8 @@
</span>
<p class="doc">
<p>removes the last element of an array and returns the new array.</p>
</p>
</div>
<div class="binder">
@ -747,7 +759,8 @@
</span>
<p class="doc">
<p>removes an element <code>value</code> from the end of an array <code>a</code> in-place and returns it.</p>
</p>
</div>
<div class="binder">
@ -825,7 +838,8 @@
</span>
<p class="doc">
<p>adds an element <code>value</code> to the end of an array <code>a</code>.</p>
</p>
</div>
<div class="binder">
@ -844,7 +858,8 @@
</span>
<p class="doc">
<p>adds an element <code>value</code> to the end of an array <code>a</code> in-place.</p>
</p>
</div>
<div class="binder">
@ -883,7 +898,8 @@
</span>
<p class="doc">
<p>returns an array <code>a</code> as a raw pointer—useful for interacting with C.</p>
</p>
</div>
<div class="binder">
@ -1168,7 +1184,8 @@
</span>
<p class="doc">
<p>converts an array to a string.</p>
</p>
</div>
<div class="binder">

View File

@ -193,7 +193,8 @@
</span>
<p class="doc">
<p>gets the <code>buckets</code> property of a <code>Map</code>.</p>
</p>
</div>
<div class="binder">
@ -232,7 +233,8 @@
</span>
<p class="doc">
<p>copies the <code>Map</code>.</p>
</p>
</div>
<div class="binder">
@ -291,7 +293,8 @@
</span>
<p class="doc">
<p>deletes a <code>Map</code>. Should usually not be called manually.</p>
</p>
</div>
<div class="binder">
@ -469,7 +472,8 @@
</span>
<p class="doc">
<p>creates a <code>Map</code>.</p>
</p>
</div>
<div class="binder">
@ -548,7 +552,8 @@
</span>
<p class="doc">
<p>gets the <code>n-buckets</code> property of a <code>Map</code>.</p>
</p>
</div>
<div class="binder">
@ -567,7 +572,8 @@
</span>
<p class="doc">
<p>converts a <code>Map</code> to a string.</p>
</p>
</div>
<div class="binder">
@ -646,7 +652,8 @@
</span>
<p class="doc">
<p>sets the <code>buckets</code> property of a <code>Map</code>.</p>
</p>
</div>
<div class="binder">
@ -665,7 +672,8 @@
</span>
<p class="doc">
<p>sets the <code>buckets</code> property of a <code>Map</code> in place.</p>
</p>
</div>
<div class="binder">
@ -684,7 +692,8 @@
</span>
<p class="doc">
<p>sets the <code>n-buckets</code> property of a <code>Map</code>.</p>
</p>
</div>
<div class="binder">
@ -703,7 +712,8 @@
</span>
<p class="doc">
<p>sets the <code>n-buckets</code> property of a <code>Map</code> in place.</p>
</p>
</div>
<div class="binder">
@ -722,7 +732,8 @@
(str m)
</pre>
<p class="doc">
<p>converts a <code>Map</code> to a string.</p>
</p>
</div>
<div class="binder">
@ -781,7 +792,8 @@
</span>
<p class="doc">
<p>updates the <code>buckets</code> property of a <code>Map</code> using a function <code>f</code>.</p>
</p>
</div>
<div class="binder">
@ -800,7 +812,8 @@
</span>
<p class="doc">
<p>updates the <code>n-buckets</code> property of a <code>Map</code> using a function <code>f</code>.</p>
</p>
</div>
<div class="binder">

View File

@ -174,7 +174,8 @@
</span>
<p class="doc">
<p>creates a <code>Just</code>.</p>
</p>
</div>
<div class="binder">
@ -193,7 +194,8 @@
</span>
<p class="doc">
<p>creates a <code>Nothing</code>.</p>
</p>
</div>
<div class="binder">
@ -232,7 +234,8 @@
</span>
<p class="doc">
<p>copies a <code>(Maybe a)</code>.</p>
</p>
</div>
<div class="binder">
@ -251,7 +254,8 @@
</span>
<p class="doc">
<p>deletes a <code>(Maybe a)</code>. Should usually not be called manually.</p>
</p>
</div>
<div class="binder">
@ -353,7 +357,8 @@
</span>
<p class="doc">
<p>stringifies a <code>&quot;Maybe&quot;</code>.</p>
</p>
</div>
<div class="binder">
@ -393,7 +398,8 @@
</span>
<p class="doc">
<p>stringifies a <code>&quot;Maybe&quot;</code>.</p>
</p>
</div>
<div class="binder">

View File

@ -174,7 +174,8 @@
</span>
<p class="doc">
<p>creates a <code>Error</code>.</p>
</p>
</div>
<div class="binder">
@ -193,7 +194,8 @@
</span>
<p class="doc">
<p>creates a <code>Success</code>.</p>
</p>
</div>
<div class="binder">
@ -253,7 +255,8 @@
</span>
<p class="doc">
<p>copies a <code>(Result a b)</code>.</p>
</p>
</div>
<div class="binder">
@ -272,7 +275,8 @@
</span>
<p class="doc">
<p>deletes a <code>(Result a b)</code>. Should usually not be called manually.</p>
</p>
</div>
<div class="binder">
@ -391,7 +395,8 @@
</span>
<p class="doc">
<p>stringifies a <code>&quot;Result&quot;</code>.</p>
</p>
</div>
<div class="binder">
@ -410,7 +415,8 @@
</span>
<p class="doc">
<p>stringifies a <code>&quot;Result&quot;</code>.</p>
</p>
</div>
<div class="binder">

View File

@ -154,7 +154,8 @@
</span>
<p class="doc">
<p>exits the program.</p>
</p>
</div>
<div class="binder">

View File

@ -271,7 +271,8 @@
</span>
<p class="doc">
<p>copies the <code>Vector2</code>.</p>
</p>
</div>
<div class="binder">
@ -290,7 +291,8 @@
</span>
<p class="doc">
<p>deletes a <code>Vector2</code>. Should usually not be called manually.</p>
</p>
</div>
<div class="binder">
@ -388,7 +390,8 @@
</span>
<p class="doc">
<p>creates a <code>Vector2</code>.</p>
</p>
</div>
<div class="binder">
@ -546,7 +549,8 @@
</span>
<p class="doc">
<p>converts a <code>Vector2</code> to a string.</p>
</p>
</div>
<div class="binder">
@ -604,7 +608,8 @@
</span>
<p class="doc">
<p>sets the <code>x</code> property of a <code>Vector2</code>.</p>
</p>
</div>
<div class="binder">
@ -623,7 +628,8 @@
</span>
<p class="doc">
<p>sets the <code>x</code> property of a <code>Vector2</code> in place.</p>
</p>
</div>
<div class="binder">
@ -642,7 +648,8 @@
</span>
<p class="doc">
<p>sets the <code>y</code> property of a <code>Vector2</code>.</p>
</p>
</div>
<div class="binder">
@ -661,7 +668,8 @@
</span>
<p class="doc">
<p>sets the <code>y</code> property of a <code>Vector2</code> in place.</p>
</p>
</div>
<div class="binder">
@ -680,7 +688,8 @@
</span>
<p class="doc">
<p>converts a <code>Vector2</code> to a string.</p>
</p>
</div>
<div class="binder">
@ -718,7 +727,8 @@
</span>
<p class="doc">
<p>updates the <code>x</code> property of a <code>Vector2</code> using a function <code>f</code>.</p>
</p>
</div>
<div class="binder">
@ -737,7 +747,8 @@
</span>
<p class="doc">
<p>updates the <code>y</code> property of a <code>Vector2</code> using a function <code>f</code>.</p>
</p>
</div>
<div class="binder">
@ -756,7 +767,8 @@
</span>
<p class="doc">
<p>gets the <code>x</code> property of a <code>Vector2</code>.</p>
</p>
</div>
<div class="binder">
@ -775,7 +787,8 @@
</span>
<p class="doc">
<p>gets the <code>y</code> property of a <code>Vector2</code>.</p>
</p>
</div>
<div class="binder">

View File

@ -251,7 +251,8 @@
</span>
<p class="doc">
<p>copies the <code>Vector3</code>.</p>
</p>
</div>
<div class="binder">
@ -290,7 +291,8 @@
</span>
<p class="doc">
<p>deletes a <code>Vector3</code>. Should usually not be called manually.</p>
</p>
</div>
<div class="binder">
@ -348,7 +350,8 @@
</span>
<p class="doc">
<p>creates a <code>Vector3</code>.</p>
</p>
</div>
<div class="binder">
@ -506,7 +509,8 @@
</span>
<p class="doc">
<p>converts a <code>Vector3</code> to a string.</p>
</p>
</div>
<div class="binder">
@ -544,7 +548,8 @@
</span>
<p class="doc">
<p>sets the <code>x</code> property of a <code>Vector3</code>.</p>
</p>
</div>
<div class="binder">
@ -563,7 +568,8 @@
</span>
<p class="doc">
<p>sets the <code>x</code> property of a <code>Vector3</code> in place.</p>
</p>
</div>
<div class="binder">
@ -582,7 +588,8 @@
</span>
<p class="doc">
<p>sets the <code>y</code> property of a <code>Vector3</code>.</p>
</p>
</div>
<div class="binder">
@ -601,7 +608,8 @@
</span>
<p class="doc">
<p>sets the <code>y</code> property of a <code>Vector3</code> in place.</p>
</p>
</div>
<div class="binder">
@ -620,7 +628,8 @@
</span>
<p class="doc">
<p>sets the <code>z</code> property of a <code>Vector3</code>.</p>
</p>
</div>
<div class="binder">
@ -639,7 +648,8 @@
</span>
<p class="doc">
<p>sets the <code>z</code> property of a <code>Vector3</code> in place.</p>
</p>
</div>
<div class="binder">
@ -658,7 +668,8 @@
</span>
<p class="doc">
<p>converts a <code>Vector3</code> to a string.</p>
</p>
</div>
<div class="binder">
@ -696,7 +707,8 @@
</span>
<p class="doc">
<p>updates the <code>x</code> property of a <code>Vector3</code> using a function <code>f</code>.</p>
</p>
</div>
<div class="binder">
@ -715,7 +727,8 @@
</span>
<p class="doc">
<p>updates the <code>y</code> property of a <code>Vector3</code> using a function <code>f</code>.</p>
</p>
</div>
<div class="binder">
@ -734,7 +747,8 @@
</span>
<p class="doc">
<p>updates the <code>z</code> property of a <code>Vector3</code> using a function <code>f</code>.</p>
</p>
</div>
<div class="binder">
@ -753,7 +767,8 @@
</span>
<p class="doc">
<p>gets the <code>x</code> property of a <code>Vector3</code>.</p>
</p>
</div>
<div class="binder">
@ -772,7 +787,8 @@
</span>
<p class="doc">
<p>gets the <code>y</code> property of a <code>Vector3</code>.</p>
</p>
</div>
<div class="binder">
@ -791,7 +807,8 @@
</span>
<p class="doc">
<p>gets the <code>z</code> property of a <code>Vector3</code>.</p>
</p>
</div>
<div class="binder">

View File

@ -251,7 +251,8 @@
</span>
<p class="doc">
<p>copies the <code>VectorN</code>.</p>
</p>
</div>
<div class="binder">
@ -270,7 +271,8 @@
</span>
<p class="doc">
<p>deletes a <code>VectorN</code>. Should usually not be called manually.</p>
</p>
</div>
<div class="binder">
@ -348,7 +350,8 @@
</span>
<p class="doc">
<p>creates a <code>VectorN</code>.</p>
</p>
</div>
<div class="binder">
@ -446,7 +449,8 @@
</span>
<p class="doc">
<p>gets the <code>n</code> property of a <code>VectorN</code>.</p>
</p>
</div>
<div class="binder">
@ -525,7 +529,8 @@
</span>
<p class="doc">
<p>converts a <code>VectorN</code> to a string.</p>
</p>
</div>
<div class="binder">
@ -563,7 +568,8 @@
</span>
<p class="doc">
<p>sets the <code>n</code> property of a <code>VectorN</code>.</p>
</p>
</div>
<div class="binder">
@ -582,7 +588,8 @@
</span>
<p class="doc">
<p>sets the <code>n</code> property of a <code>VectorN</code> in place.</p>
</p>
</div>
<div class="binder">
@ -601,7 +608,8 @@
</span>
<p class="doc">
<p>sets the <code>v</code> property of a <code>VectorN</code>.</p>
</p>
</div>
<div class="binder">
@ -620,7 +628,8 @@
</span>
<p class="doc">
<p>sets the <code>v</code> property of a <code>VectorN</code> in place.</p>
</p>
</div>
<div class="binder">
@ -639,7 +648,8 @@
</span>
<p class="doc">
<p>converts a <code>VectorN</code> to a string.</p>
</p>
</div>
<div class="binder">
@ -677,7 +687,8 @@
</span>
<p class="doc">
<p>updates the <code>n</code> property of a <code>VectorN</code> using a function <code>f</code>.</p>
</p>
</div>
<div class="binder">
@ -696,7 +707,8 @@
</span>
<p class="doc">
<p>updates the <code>v</code> property of a <code>VectorN</code> using a function <code>f</code>.</p>
</p>
</div>
<div class="binder">
@ -715,7 +727,8 @@
</span>
<p class="doc">
<p>gets the <code>v</code> property of a <code>VectorN</code>.</p>
</p>
</div>
<div class="binder">

View File

@ -24,6 +24,7 @@ templateEMap =
in defineTemplate
(SymPath ["Array"] "endo-map")
(FuncTy [RefTy fTy, aTy] bTy)
"applies a function `f` to an array `a`. The type of the elements cannot change."
(toTemplate "Array $NAME(Lambda *f, Array a)") -- Lambda used to be $(Fn [a] a)
(toTemplate $ unlines
["$DECL { "
@ -46,12 +47,13 @@ templateShrinkCheck var =
-- | Endofunctor filter, misnomer for consistency with flavors of map
templateEFilter :: (String, Binder)
templateEFilter = defineTypeParameterizedTemplate templateCreator path t
templateEFilter = defineTypeParameterizedTemplate templateCreator path t docs
where
fTy = FuncTy [RefTy (VarTy "a")] BoolTy
aTy = StructTy "Array" [VarTy "a"]
path = SymPath ["Array"] "endo-filter"
t = FuncTy [RefTy fTy, aTy] aTy
docs = "filters array members using a function. This function takes ownership."
elem = "&((($a*)a.data)[i])"
templateCreator = TemplateCreator $
\typeEnv env ->
@ -86,6 +88,7 @@ templatePushBack =
in defineTemplate
(SymPath ["Array"] "push-back")
(FuncTy [aTy, valTy] aTy)
"adds an element `value` to the end of an array `a`."
(toTemplate "Array $NAME(Array a, $a value)")
(toTemplate $ unlines
["$DECL { "
@ -107,6 +110,7 @@ templatePushBackBang =
in defineTemplate
(SymPath ["Array"] "push-back!")
(FuncTy [aTy, valTy] UnitTy)
"adds an element `value` to the end of an array `a` in-place."
(toTemplate "void $NAME(Array *aRef, $a value)")
(toTemplate $ unlines
["$DECL { "
@ -121,10 +125,11 @@ templatePushBackBang =
(\(FuncTy [arrayType, _] _) -> [])
templatePopBack :: (String, Binder)
templatePopBack = defineTypeParameterizedTemplate templateCreator path t
templatePopBack = defineTypeParameterizedTemplate templateCreator path t docs
where path = SymPath ["Array"] "pop-back"
aTy = StructTy "Array" [VarTy "a"]
t = FuncTy [aTy] aTy
docs = "removes the last element of an array and returns the new array."
templateCreator = TemplateCreator $
\typeEnv env ->
Template
@ -155,6 +160,7 @@ templatePopBackBang =
in defineTemplate
(SymPath ["Array"] "pop-back!")
(FuncTy [aTy] (VarTy "a"))
"removes an element `value` from the end of an array `a` in-place and returns it."
(toTemplate "$a $NAME(Array *aRef)")
(toTemplate $ unlines
["$DECL { "
@ -176,6 +182,7 @@ templateNth =
in defineTemplate
(SymPath ["Array"] "nth")
(FuncTy [RefTy (StructTy "Array" [t]), IntTy] (RefTy t))
"gets a reference to the `n`th element from an array `a`."
(toTemplate "$t* $NAME (Array *aRef, int n)")
(toTemplate $ unlines ["$DECL {"
," Array a = *aRef;"
@ -192,14 +199,16 @@ templateRaw :: (String, Binder)
templateRaw = defineTemplate
(SymPath ["Array"] "raw")
(FuncTy [StructTy "Array" [VarTy "t"]] (PointerTy (VarTy "t")))
"returns an array `a` as a raw pointer—useful for interacting with C."
(toTemplate "$t* $NAME (Array a)")
(toTemplate "$DECL { return a.data; }")
(\(FuncTy [arrayType] _) -> [])
templateAset :: (String, Binder)
templateAset = defineTypeParameterizedTemplate templateCreator path t
templateAset = defineTypeParameterizedTemplate templateCreator path t docs
where path = SymPath ["Array"] "aset"
t = FuncTy [StructTy "Array" [VarTy "t"], IntTy, VarTy "t"] (StructTy "Array" [VarTy "t"])
docs = "sets an array element at the index `n` to a new value."
templateCreator = TemplateCreator $
\typeEnv env ->
Template
@ -220,9 +229,10 @@ templateAset = defineTypeParameterizedTemplate templateCreator path t
depsForDeleteFunc typeEnv env insideTy)
templateAsetBang :: (String, Binder)
templateAsetBang = defineTypeParameterizedTemplate templateCreator path t
templateAsetBang = defineTypeParameterizedTemplate templateCreator path t docs
where path = SymPath ["Array"] "aset!"
t = FuncTy [RefTy (StructTy "Array" [VarTy "t"]), IntTy, VarTy "t"] UnitTy
docs = "sets an array element at the index `n` to a new value in place."
templateCreator = TemplateCreator $
\typeEnv env ->
Template
@ -245,9 +255,10 @@ templateAsetBang = defineTypeParameterizedTemplate templateCreator path t
-- | This function can set uninitialized memory in an array (used together with 'allocate').
-- | It will NOT try to free the value that is already at location 'n'.
templateAsetUninitializedBang :: (String, Binder)
templateAsetUninitializedBang = defineTypeParameterizedTemplate templateCreator path t
templateAsetUninitializedBang = defineTypeParameterizedTemplate templateCreator path t docs
where path = SymPath ["Array"] "aset-uninitialized!"
t = FuncTy [RefTy (StructTy "Array" [VarTy "t"]), IntTy, VarTy "t"] UnitTy
docs = "sets an uninitialized array member. The old member will not be deleted."
templateCreator = TemplateCreator $
\typeEnv env ->
Template
@ -264,9 +275,10 @@ templateAsetUninitializedBang = defineTypeParameterizedTemplate templateCreator
(const [])
templateLength :: (String, Binder)
templateLength = defineTypeParameterizedTemplate templateCreator path t
templateLength = defineTypeParameterizedTemplate templateCreator path t docs
where path = SymPath ["Array"] "length"
t = FuncTy [RefTy (StructTy "Array" [VarTy "t"])] IntTy
docs = "gets the length of the array."
templateCreator = TemplateCreator $
\typeEnv env ->
Template
@ -277,9 +289,10 @@ templateLength = defineTypeParameterizedTemplate templateCreator path t
depsForDeleteFunc typeEnv env arrayType)
templateAllocate :: (String, Binder)
templateAllocate = defineTypeParameterizedTemplate templateCreator path t
templateAllocate = defineTypeParameterizedTemplate templateCreator path t docs
where path = SymPath ["Array"] "allocate"
t = FuncTy [IntTy] (StructTy "Array" [VarTy "t"])
docs = "allocates an uninitialized array. You can initialize members using [`aset-uninitialized`](#aset-uninitialized)."
templateCreator = TemplateCreator $
\typeEnv env ->
Template
@ -298,9 +311,10 @@ templateAllocate = defineTypeParameterizedTemplate templateCreator path t
depsForDeleteFunc typeEnv env arrayType)
templateDeleteArray :: (String, Binder)
templateDeleteArray = defineTypeParameterizedTemplate templateCreator path t
templateDeleteArray = defineTypeParameterizedTemplate templateCreator path t docs
where path = SymPath ["Array"] "delete"
t = FuncTy [StructTy "Array" [VarTy "a"]] UnitTy
docs = "deletes an array. This function should usually not be called manually."
templateCreator = TemplateCreator $
\typeEnv env ->
Template
@ -348,9 +362,10 @@ insideArrayDeletion typeEnv env t indexer =
FunctionIgnored -> " /* Ignore non-managed type inside Array: '" ++ show t ++ "' */\n"
templateCopyArray :: (String, Binder)
templateCopyArray = defineTypeParameterizedTemplate templateCreator path t
templateCopyArray = defineTypeParameterizedTemplate templateCreator path t docs
where path = SymPath ["Array"] "copy"
t = FuncTy [RefTy (StructTy "Array" [VarTy "a"])] (StructTy "Array" [VarTy "a"])
docs = "copies an array."
templateCreator = TemplateCreator $
\typeEnv env ->
Template
@ -391,7 +406,7 @@ insideArrayCopying typeEnv env t =
" /* Ignore type inside Array when copying: '" ++ show t ++ "' (no copy function known)*/\n"
templateStrArray :: (String, Binder)
templateStrArray = defineTypeParameterizedTemplate templateCreator path t
templateStrArray = defineTypeParameterizedTemplate templateCreator path t docs
where templateCreator = TemplateCreator $
\typeEnv env ->
Template
@ -405,6 +420,7 @@ templateStrArray = defineTypeParameterizedTemplate templateCreator path t
depsForPrnFunc typeEnv env insideType)
path = SymPath ["Array"] "str"
t = FuncTy [RefTy (StructTy "Array" [VarTy "a"])] StringTy
docs = "converts an array to a string."
-- | TODO: move this into the templateStrArray function?
strTy :: TypeEnv -> Env -> Ty -> [Token]

View File

@ -77,14 +77,16 @@ templatesForSingleMember :: TypeEnv -> Env -> [String] -> Ty -> (XObj, XObj) ->
templatesForSingleMember typeEnv env insidePath p@(StructTy typeName _) (nameXObj, typeXObj) =
let Just t = xobjToTy typeXObj
memberName = getName nameXObj
in [instanceBinderWithDeps (SymPath insidePath memberName) (FuncTy [RefTy p] (RefTy t)) (templateGetter (mangle memberName) t)
in [instanceBinderWithDeps (SymPath insidePath memberName) (FuncTy [RefTy p] (RefTy t)) (templateGetter (mangle memberName) t) ("gets the `" ++ memberName ++ "` property of a `" ++ typeName ++ "`.")
, if isTypeGeneric t
then (templateGenericSetter insidePath p t memberName, [])
else instanceBinderWithDeps (SymPath insidePath ("set-" ++ memberName)) (FuncTy [p, t] p) (templateSetter typeEnv env (mangle memberName) t)
,instanceBinderWithDeps (SymPath insidePath ("set-" ++ memberName ++ "!")) (FuncTy [RefTy p, t] UnitTy) (templateMutatingSetter typeEnv env (mangle memberName) t)
else instanceBinderWithDeps (SymPath insidePath ("set-" ++ memberName)) (FuncTy [p, t] p) (templateSetter typeEnv env (mangle memberName) t) ("sets the `" ++ memberName ++ "` property of a `" ++ typeName ++ "`.")
,instanceBinderWithDeps (SymPath insidePath ("set-" ++ memberName ++ "!")) (FuncTy [RefTy p, t] UnitTy) (templateMutatingSetter typeEnv env (mangle memberName) t) ("sets the `" ++ memberName ++ "` property of a `" ++ typeName ++ "` in place.")
,instanceBinderWithDeps (SymPath insidePath ("update-" ++ memberName))
(FuncTy [p, RefTy (FuncTy [t] t)] p)
(templateUpdater (mangle memberName))]
(templateUpdater (mangle memberName))
("updates the `" ++ memberName ++ "` property of a `" ++ typeName ++ "` using a function `f`.")
]
-- | The template for getters of a deftype.
templateGetter :: String -> Ty -> Template
@ -119,10 +121,11 @@ templateSetter typeEnv env memberName memberTy =
-- | The template for setters of a generic deftype.
templateGenericSetter :: [String] -> Ty -> Ty -> String -> (String, Binder)
templateGenericSetter pathStrings originalStructTy memberTy memberName =
defineTypeParameterizedTemplate templateCreator path (FuncTy [originalStructTy, memberTy] originalStructTy)
templateGenericSetter pathStrings originalStructTy@(StructTy typeName _) memberTy memberName =
defineTypeParameterizedTemplate templateCreator path (FuncTy [originalStructTy, memberTy] originalStructTy) docs
where path = SymPath pathStrings ("set-" ++ memberName)
t = FuncTy [VarTy "p", VarTy "t"] (VarTy "p")
docs = "sets the `" ++ memberName ++ "` property of a `" ++ typeName ++ "`."
templateCreator = TemplateCreator $
\typeEnv env ->
Template
@ -175,6 +178,7 @@ binderForInit insidePath structTy@(StructTy typeName _) [XObj (Arr membersXObjs)
else Right $ instanceBinder (SymPath insidePath "init")
(FuncTy (initArgListTypes membersXObjs) structTy)
(concreteInit StackAlloc structTy membersXObjs)
("creates a `" ++ typeName ++ "`.")
-- | Generate a list of types from a deftype declaration.
initArgListTypes :: [XObj] -> [Ty]
@ -196,9 +200,10 @@ concreteInit allocationMode originalStructTy@(StructTy typeName typeVariables) m
-- | The template for the 'init' and 'new' functions for a generic deftype.
genericInit :: AllocationMode -> [String] -> Ty -> [XObj] -> (String, Binder)
genericInit allocationMode pathStrings originalStructTy@(StructTy typeName _) membersXObjs =
defineTypeParameterizedTemplate templateCreator path t
defineTypeParameterizedTemplate templateCreator path t docs
where path = SymPath pathStrings "init"
t = FuncTy (map snd (memberXObjsToPairs membersXObjs)) originalStructTy
docs = "creates a `" ++ typeName ++ "`."
templateCreator = TemplateCreator $
\typeEnv env ->
Template
@ -248,7 +253,8 @@ binderForStrOrPrn typeEnv env insidePath structTy@(StructTy typeName _) [XObj (A
then Right (genericStr insidePath structTy membersXObjs strOrPrn, [])
else Right (instanceBinderWithDeps (SymPath insidePath strOrPrn)
(FuncTy [RefTy structTy] StringTy)
(concreteStr typeEnv env structTy (memberXObjsToPairs membersXObjs) strOrPrn))
(concreteStr typeEnv env structTy (memberXObjsToPairs membersXObjs) strOrPrn)
("converts a `" ++ typeName ++ "` to a string."))
-- | The template for the 'str' function for a concrete deftype.
concreteStr :: TypeEnv -> Env -> Ty -> [(String, Ty)] -> String -> Template
@ -266,10 +272,11 @@ concreteStr typeEnv env concreteStructTy@(StructTy typeName _) memberPairs strOr
-- | The template for the 'str' function for a generic deftype.
genericStr :: [String] -> Ty -> [XObj] -> String -> (String, Binder)
genericStr pathStrings originalStructTy@(StructTy typeName varTys) membersXObjs strOrPrn =
defineTypeParameterizedTemplate templateCreator path t
defineTypeParameterizedTemplate templateCreator path t docs
where path = SymPath pathStrings strOrPrn
t = FuncTy [RefTy originalStructTy] StringTy
members = memberXObjsToPairs membersXObjs
docs = "converts a `" ++ typeName ++ "` to a string."
templateCreator = TemplateCreator $
\typeEnv env ->
Template
@ -331,14 +338,16 @@ binderForDelete typeEnv env insidePath structTy@(StructTy typeName _) [XObj (Arr
then Right (genericDelete insidePath structTy membersXObjs, [])
else Right (instanceBinderWithDeps (SymPath insidePath "delete")
(FuncTy [structTy] UnitTy)
(concreteDelete typeEnv env (memberXObjsToPairs membersXObjs)))
(concreteDelete typeEnv env (memberXObjsToPairs membersXObjs))
("deletes a `" ++ typeName ++"`."))
-- | The template for the 'delete' function of a generic deftype.
genericDelete :: [String] -> Ty -> [XObj] -> (String, Binder)
genericDelete pathStrings originalStructTy membersXObjs =
defineTypeParameterizedTemplate templateCreator path (FuncTy [originalStructTy] UnitTy)
genericDelete pathStrings originalStructTy@(StructTy typeName _) membersXObjs =
defineTypeParameterizedTemplate templateCreator path (FuncTy [originalStructTy] UnitTy) docs
where path = SymPath pathStrings "delete"
t = FuncTy [VarTy "p"] UnitTy
docs = "deletes a `" ++ typeName ++ "`. Should usually not be called manually."
templateCreator = TemplateCreator $
\typeEnv env ->
Template
@ -367,14 +376,16 @@ binderForCopy typeEnv env insidePath structTy@(StructTy typeName _) [XObj (Arr m
then Right (genericCopy insidePath structTy membersXObjs, [])
else Right (instanceBinderWithDeps (SymPath insidePath "copy")
(FuncTy [RefTy structTy] structTy)
(concreteCopy typeEnv env (memberXObjsToPairs membersXObjs)))
(concreteCopy typeEnv env (memberXObjsToPairs membersXObjs))
("copies a `" ++ typeName ++ "`."))
-- | The template for the 'copy' function of a generic deftype.
genericCopy :: [String] -> Ty -> [XObj] -> (String, Binder)
genericCopy pathStrings originalStructTy membersXObjs =
defineTypeParameterizedTemplate templateCreator path (FuncTy [RefTy originalStructTy] originalStructTy)
genericCopy pathStrings originalStructTy@(StructTy typeName _) membersXObjs =
defineTypeParameterizedTemplate templateCreator path (FuncTy [RefTy originalStructTy] originalStructTy) docs
where path = SymPath pathStrings "copy"
t = FuncTy [RefTy (VarTy "p")] (VarTy "p")
docs = "copies the `" ++ typeName ++ "`."
templateCreator = TemplateCreator $
\typeEnv env ->
Template

View File

@ -59,6 +59,7 @@ templatePointerCopy :: (String, Binder)
templatePointerCopy = defineTemplate
(SymPath ["Pointer"] "copy")
(FuncTy [RefTy (PointerTy (VarTy "p"))] (PointerTy (VarTy "p")))
"copies a pointer `p`."
(toTemplate "$p* $NAME ($p** ptrRef)")
(toTemplate $ unlines ["$DECL {"
," return *ptrRef;"
@ -68,6 +69,7 @@ templatePointerCopy = defineTemplate
templatePointerEqual = defineTemplate
(SymPath ["Pointer"] "eq")
(FuncTy [PointerTy (VarTy "p"), PointerTy (VarTy "p")] BoolTy)
"checks two pointers for equality."
(toTemplate "bool $NAME ($p *p1, $p *p2)")
(toTemplate $ unlines ["$DECL {"
," return p1 == p2;"
@ -78,6 +80,7 @@ templatePointerEqual = defineTemplate
templatePointerToRef = defineTemplate
(SymPath ["Pointer"] "to-ref")
(FuncTy [PointerTy (VarTy "p")] (RefTy (VarTy "p")))
"converts a pointer to a reference type. The user will have to ensure themselves that this is a safe operation."
(toTemplate "$p* $NAME ($p *p)")
(toTemplate $ unlines ["$DECL {"
," return p;"
@ -99,6 +102,7 @@ templateExit :: (String, Binder)
templateExit = defineTemplate
(SymPath ["System"] "exit")
(FuncTy [IntTy] (VarTy "a"))
"exits the program."
(toTemplate "$a $NAME (int code)")
(toTemplate $ unlines ["$DECL {"
," exit(code);"
@ -137,8 +141,8 @@ generateInnerFunctionModule arity =
funcTy = FuncTy (take arity (map (VarTy . charToTyName) alphabet)) (VarTy "z")
bindings = Map.fromList [ generateTemplateFuncCopy funcTy
, generateTemplateFuncDelete funcTy
, generateTemplateFuncStrOrPrn "str" funcTy
, generateTemplateFuncStrOrPrn "prn" funcTy
, generateTemplateFuncStrOrPrn "str" "converts a function to a string." funcTy
, generateTemplateFuncStrOrPrn "prn" "converts a function to a string (internal representation)." funcTy
]
@ -147,6 +151,7 @@ generateTemplateFuncCopy :: Ty -> (String, Binder)
generateTemplateFuncCopy funcTy = defineTemplate
(SymPath ["Function"] "copy")
(FuncTy [RefTy funcTy] (VarTy "a"))
"copies a function."
(toTemplate "$a $NAME ($a* ref)")
(toTemplate $ unlines ["$DECL {"
," if(ref->env) {"
@ -167,6 +172,7 @@ generateTemplateFuncDelete :: Ty -> (String, Binder)
generateTemplateFuncDelete funcTy = defineTemplate
(SymPath ["Function"] "delete")
(FuncTy [funcTy] UnitTy)
"deletes a function."
(toTemplate "void $NAME (Lambda f)")
(toTemplate $ unlines ["$DECL {"
," if(f.delete) {"
@ -177,10 +183,11 @@ generateTemplateFuncDelete funcTy = defineTemplate
(const [])
-- | A template function for generating 'str' or 'prn' functions for function pointers.
generateTemplateFuncStrOrPrn :: String -> Ty -> (String, Binder)
generateTemplateFuncStrOrPrn name funcTy = defineTemplate
generateTemplateFuncStrOrPrn :: String -> String -> Ty -> (String, Binder)
generateTemplateFuncStrOrPrn name docs funcTy = defineTemplate
(SymPath ["Function"] name)
(FuncTy [RefTy funcTy] StringTy)
docs
(toTemplate "String $NAME (Lambda *f)")
(toTemplate $ unlines ["$DECL {"
," static String lambda = \"λ\";"
@ -287,6 +294,7 @@ templateEnumToInt :: (String, Binder)
templateEnumToInt = defineTemplate
(SymPath [] "enum-to-int")
(FuncTy [VarTy "a"] IntTy)
"converts an enum `e` to an integer."
(toTemplate "int $NAME ($a e)")
(toTemplate $ unlines ["$DECL {"
," return (int)e;"

View File

@ -63,8 +63,9 @@ binderForCaseInit insidePath structTy@(StructTy typeName _) sumtypeCase =
concreteCaseInit :: AllocationMode -> [String] -> Ty -> SumtypeCase -> (String, Binder)
concreteCaseInit allocationMode insidePath structTy sumtypeCase =
instanceBinder (SymPath insidePath (caseName sumtypeCase)) (FuncTy (caseTys sumtypeCase) structTy) template
where template =
instanceBinder (SymPath insidePath (caseName sumtypeCase)) (FuncTy (caseTys sumtypeCase) structTy) template doc
where doc = "creates a `" ++ caseName sumtypeCase ++ "`."
template =
Template
(FuncTy (caseTys sumtypeCase) (VarTy "p"))
(\(FuncTy _ concreteStructTy) ->
@ -76,9 +77,10 @@ concreteCaseInit allocationMode insidePath structTy sumtypeCase =
genericCaseInit :: AllocationMode -> [String] -> Ty -> SumtypeCase -> (String, Binder)
genericCaseInit allocationMode pathStrings originalStructTy sumtypeCase =
defineTypeParameterizedTemplate templateCreator path t
defineTypeParameterizedTemplate templateCreator path t docs
where path = SymPath pathStrings (caseName sumtypeCase)
t = FuncTy (caseTys sumtypeCase) originalStructTy
docs = "creates a `" ++ caseName sumtypeCase ++ "`."
templateCreator = TemplateCreator $
\typeEnv env ->
Template
@ -123,8 +125,9 @@ binderForStrOrPrn typeEnv env insidePath structTy@(StructTy typeName _) cases st
-- | The template for the 'str' function for a concrete deftype.
concreteStr :: TypeEnv -> Env -> [String] -> Ty -> [SumtypeCase] -> String -> (String, Binder)
concreteStr typeEnv env insidePath concreteStructTy@(StructTy typeName _) cases strOrPrn =
instanceBinder (SymPath insidePath strOrPrn) (FuncTy [RefTy concreteStructTy] StringTy) template
where template =
instanceBinder (SymPath insidePath strOrPrn) (FuncTy [RefTy concreteStructTy] StringTy) template doc
where doc = "converts a `" ++ typeName ++ "` to a string."
template =
Template
(FuncTy [RefTy concreteStructTy] StringTy)
(\(FuncTy [RefTy structTy] StringTy) -> toTemplate $ "String $NAME(" ++ tyToCLambdaFix structTy ++ " *p)")
@ -138,9 +141,10 @@ concreteStr typeEnv env insidePath concreteStructTy@(StructTy typeName _) cases
-- | The template for the 'str' function for a generic deftype.
genericStr :: [String] -> Ty -> [SumtypeCase] -> String -> (String, Binder)
genericStr insidePath originalStructTy@(StructTy typeName varTys) cases strOrPrn =
defineTypeParameterizedTemplate templateCreator path t
defineTypeParameterizedTemplate templateCreator path t docs
where path = SymPath insidePath strOrPrn
t = FuncTy [RefTy originalStructTy] StringTy
docs = "stringifies a `" ++ show typeName ++ "`."
templateCreator = TemplateCreator $
\typeEnv env ->
Template
@ -221,9 +225,10 @@ binderForDelete typeEnv env insidePath structTy@(StructTy typeName _) cases =
-- | The template for the 'delete' function of a generic sumtype.
genericSumtypeDelete :: [String] -> Ty -> [SumtypeCase] -> (String, Binder)
genericSumtypeDelete pathStrings originalStructTy cases =
defineTypeParameterizedTemplate templateCreator path (FuncTy [originalStructTy] UnitTy)
defineTypeParameterizedTemplate templateCreator path (FuncTy [originalStructTy] UnitTy) docs
where path = SymPath pathStrings "delete"
t = FuncTy [VarTy "p"] UnitTy
docs = "deletes a `" ++ show originalStructTy ++ "`. Should usually not be called manually."
templateCreator = TemplateCreator $
\typeEnv env ->
Template
@ -245,9 +250,10 @@ genericSumtypeDelete pathStrings originalStructTy cases =
-- | The template for the 'delete' function of a concrete sumtype
concreteSumtypeDelete :: [String] -> TypeEnv -> Env -> Ty -> [SumtypeCase] -> (String, Binder)
concreteSumtypeDelete insidePath typeEnv env structTy cases =
instanceBinder (SymPath insidePath "delete") (FuncTy [structTy] UnitTy) template
where template = Template
concreteSumtypeDelete insidePath typeEnv env structTy@(StructTy typeName _) cases =
instanceBinder (SymPath insidePath "delete") (FuncTy [structTy] UnitTy) template doc
where doc = "deletes a `" ++ typeName ++ "`. This should usually not be called manually."
template = Template
(FuncTy [VarTy "p"] UnitTy)
(const (toTemplate "void $NAME($p p)"))
(const (toTemplate $ unlines [ "$DECL {"
@ -276,9 +282,10 @@ binderForCopy typeEnv env insidePath structTy@(StructTy typeName _) cases =
-- | The template for the 'copy' function of a generic sumtype.
genericSumtypeCopy :: [String] -> Ty -> [SumtypeCase] -> (String, Binder)
genericSumtypeCopy pathStrings originalStructTy cases =
defineTypeParameterizedTemplate templateCreator path (FuncTy [RefTy originalStructTy] originalStructTy)
defineTypeParameterizedTemplate templateCreator path (FuncTy [RefTy originalStructTy] originalStructTy) docs
where path = SymPath pathStrings "copy"
t = FuncTy [RefTy (VarTy "p")] (VarTy "p")
docs = "copies a `" ++ show originalStructTy ++ "`."
templateCreator = TemplateCreator $
\typeEnv env ->
Template
@ -298,9 +305,10 @@ genericSumtypeCopy pathStrings originalStructTy cases =
-- | The template for the 'copy' function of a concrete sumtype
concreteSumtypeCopy :: [String] -> TypeEnv -> Env -> Ty -> [SumtypeCase] -> (String, Binder)
concreteSumtypeCopy insidePath typeEnv env structTy cases =
instanceBinder (SymPath insidePath "copy") (FuncTy [RefTy structTy] structTy) template
where template = Template
concreteSumtypeCopy insidePath typeEnv env structTy@(StructTy typeName _) cases =
instanceBinder (SymPath insidePath "copy") (FuncTy [RefTy structTy] structTy) template doc
where doc = "copies a `" ++ typeName ++ "`."
template = Template
(FuncTy [RefTy (VarTy "p")] (VarTy "p"))
(const (toTemplate "$p $NAME($p* pRef)"))
(const (tokensForSumtypeCopy typeEnv env structTy cases))

View File

@ -1,5 +1,6 @@
module Template where
import qualified Data.Map as Map
import qualified Data.Set as Set
import Debug.Trace
@ -12,16 +13,20 @@ import Concretize
import ToTemplate
-- | Create a binding pair used for adding a template instantiation to an environment.
instanceBinder :: SymPath -> Ty -> Template -> (String, Binder)
instanceBinder path@(SymPath _ name) actualType template =
instanceBinder :: SymPath -> Ty -> Template -> String -> (String, Binder)
instanceBinder path@(SymPath _ name) actualType template docs =
let (x, _) = instantiateTemplate path actualType template
in (name, Binder emptyMeta x)
docObj = (XObj (Str docs) (Just dummyInfo) Nothing)
meta = MetaData (Map.insert "doc" docObj Map.empty)
in (name, Binder meta x)
-- | Create a binding pair and don't discard the dependencies
instanceBinderWithDeps :: SymPath -> Ty -> Template -> ((String, Binder), [XObj])
instanceBinderWithDeps path@(SymPath _ name) actualType template =
instanceBinderWithDeps :: SymPath -> Ty -> Template -> String -> ((String, Binder), [XObj])
instanceBinderWithDeps path@(SymPath _ name) actualType template docs =
let (x, deps) = instantiateTemplate path actualType template
in ((name, Binder emptyMeta x), deps)
docObj = (XObj (Str docs) (Just dummyInfo) Nothing)
meta = MetaData (Map.insert "doc" docObj Map.empty)
in ((name, Binder meta x), deps)
-- | Templates are instructions for the compiler to generate some C-code
-- | based on some template and the names and types to fill into the template.
@ -34,21 +39,25 @@ instanceBinderWithDeps path@(SymPath _ name) actualType template =
-- | "int length__Float(Array__Float xs) { return xs->len; }"
-- | Create a binding pair used for adding a template definition to an environment.
defineTemplate :: SymPath -> Ty -> [Token] -> [Token] -> (Ty -> [XObj]) -> (String, Binder)
defineTemplate path t declaration definition depsFunc =
defineTemplate :: SymPath -> Ty -> String -> [Token] -> [Token] -> (Ty -> [XObj]) -> (String, Binder)
defineTemplate path t docs declaration definition depsFunc =
let (SymPath _ name) = path
template = Template t (const declaration) (const definition) depsFunc
i = Info 0 0 (show path ++ ".template") Set.empty 0
defLst = [XObj (Deftemplate (TemplateCreator (\_ _ -> template))) Nothing Nothing, XObj (Sym path Symbol) Nothing Nothing]
in (name, Binder emptyMeta (XObj (Lst defLst) (Just i) (Just t)))
docObj = (XObj (Str docs) (Just dummyInfo) Nothing)
meta = MetaData (Map.insert "doc" docObj Map.empty)
in (name, Binder meta (XObj (Lst defLst) (Just i) (Just t)))
-- | The more advanced version of a template, where the code can vary depending on the type.
defineTypeParameterizedTemplate :: TemplateCreator -> SymPath -> Ty -> (String, Binder)
defineTypeParameterizedTemplate templateCreator path t =
defineTypeParameterizedTemplate :: TemplateCreator -> SymPath -> Ty -> String -> (String, Binder)
defineTypeParameterizedTemplate templateCreator path t docs =
let (SymPath _ name) = path
i = Info 0 0 (show path ++ ".parameterizedTemplate") Set.empty 0
defLst = [XObj (Deftemplate templateCreator) Nothing Nothing, XObj (Sym path Symbol) Nothing Nothing]
in (name, Binder emptyMeta (XObj (Lst defLst) (Just i) (Just t)))
docObj = (XObj (Str docs) (Just dummyInfo) Nothing)
meta = MetaData (Map.insert "doc" docObj Map.empty)
in (name, Binder meta (XObj (Lst defLst) (Just i) (Just t)))
-- | Concretizes the types used in @token
-- @cName is the name of the definition, i.e. the "foo" in "void foo() { ... }"
@ -86,6 +95,7 @@ templateNoop :: (String, Binder)
templateNoop = defineTemplate
(SymPath [] "noop")
(FuncTy [PointerTy (VarTy "a")] UnitTy)
"accepts a pointer and will do nothing with it."
(toTemplate "void $NAME ($a* a)")
(toTemplate "$DECL { }")
(const [])