aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/lux/lang/reference.lux
diff options
context:
space:
mode:
Diffstat (limited to 'stdlib/source/lux/lang/reference.lux')
-rw-r--r--stdlib/source/lux/lang/reference.lux66
1 files changed, 66 insertions, 0 deletions
diff --git a/stdlib/source/lux/lang/reference.lux b/stdlib/source/lux/lang/reference.lux
new file mode 100644
index 000000000..98756aa08
--- /dev/null
+++ b/stdlib/source/lux/lang/reference.lux
@@ -0,0 +1,66 @@
+(.module:
+ lux
+ (lux (control [equality #+ Equality]
+ [hash #+ Hash]
+ pipe)))
+
+(type: #export Register Nat)
+
+(type: #export Variable
+ (#Local Register)
+ (#Foreign Register))
+
+(type: #export Reference
+ (#Variable Variable)
+ (#Constant Ident))
+
+(struct: #export _ (Equality Variable)
+ (def: (= reference sample)
+ (case [reference sample]
+ (^template [<tag>]
+ [(<tag> reference') (<tag> sample')]
+ (n/= reference' sample'))
+ ([#Local] [#Foreign])
+
+ _
+ false)))
+
+(struct: #export _ (Hash Variable)
+ (def: eq Equality<Variable>)
+ (def: (hash var)
+ (case var
+ (#Local register)
+ (n/* +1 register)
+
+ (#Foreign register)
+ (n/* +2 register))))
+
+(do-template [<name> <family> <tag>]
+ [(template: #export (<name> content)
+ (<| <family>
+ <tag>
+ content))]
+
+ [local #..Variable #..Local]
+ [foreign #..Variable #..Foreign]
+ )
+
+(do-template [<name> <tag>]
+ [(template: #export (<name> content)
+ (<| <tag>
+ content))]
+
+ [variable #..Variable]
+ [constant #..Constant]
+ )
+
+(def: #export self Reference (..local +0))
+
+(def: #export self?
+ (-> Variable Bool)
+ (|>> ..variable
+ (case> (^ (..local +0))
+ true
+
+ _
+ false)))