aboutsummaryrefslogtreecommitdiff
path: root/test2.lang
diff options
context:
space:
mode:
authorEduardo Julian2014-12-26 01:00:30 -0400
committerEduardo Julian2014-12-26 01:00:30 -0400
commit0a0300b129df4499782cbe47aeaee581f57cc3db (patch)
treed7f3fd5f5cecb5b7a7a8703516e98624b5901039 /test2.lang
parent4dc96027d8e90734aea3a5e109b8786fc112e88b (diff)
Reimplemented basic pattern-matching.
Diffstat (limited to 'test2.lang')
-rw-r--r--test2.lang94
1 files changed, 58 insertions, 36 deletions
diff --git a/test2.lang b/test2.lang
index a7892945f..cbce36ce8 100644
--- a/test2.lang
+++ b/test2.lang
@@ -1,7 +1,5 @@
(import java.lang.System)
-(require "./another" as another)
-
-(defclass Tagged [[java.lang.String tag] [java.lang.Object value]])
+## (require "./another" as another)
(definterface Function
(: apply (-> [java.lang.Object] java.lang.Object)))
@@ -10,43 +8,67 @@
(defclass Tuple1 [[java.lang.Object _0]])
(defclass Tuple2 [[java.lang.Object _0] [java.lang.Object _1]])
-(def const "IDENTITY")
-
-(def variant (#Tag "value"))
+(defclass Variant [[java.lang.String tag] [java.lang.Object value]])
-(def (constant x y)
- x)
+#( (def (++ xs ys)
+ (case xs
+ #Nil
+ ys
-(def (constant2 x)
- (lambda [y] x))
+ (#Cons x xs*)
+ (#Cons x (++ xs* ys)))) )#
(def (main args)
(if true
- (let f (lambda [x] (lambda [y] (x y)))
- (let g (lambda [x] const)
- (:: (:: System out) (println (f g "WE'VE GOT CLOSURES!")))))
+ (let variant (#Pair "Pattern" "Matching")
+ (case variant
+ (#Pair first second)
+ (do (:: (:: System out) (println first))
+ (:: (:: System out) (println second)))))
(:: (:: System out) (println "FALSE"))))
-## All of these work :D
-#( (let output "IT WORKS!"
- (_. (_.. System out) (println ((lambda [x y] output) "TRUE" "YOLO")))) )#
-#( (let output "IT WORKS!"
- (let f (lambda [x] (lambda [y] output))
- (_. (_.. System out) (println (f "TRUE" "YOLO"))))) )#
-#( (let output "IT WORKS!"
- (_. (_.. System out) (println ((lambda [x] (lambda [y] output)) "TRUE" "YOLO")))) )#
-#( (let output ((lambda [x y] x) "TRUE" "YOLO")
- (_. (_.. System out) (println output))) )#
-#( (let f (lambda [x y] x)
- (_. (_.. System out) (println (f "TRUE" "YOLO")))) )#
-## ((lambda [x y] (_. (_.. System out) (println x))) "TRUE" "YOLO")
-## (_. (_.. System out) (println ((lambda [x y] x) "TRUE" "YOLO")))
-#( (do (_. (_.. System out) (println true))
- (_. (_.. System out) (println (another/id 12345)))
- (_. (_.. System out) (println (constant "ONE" "TWO")))
- (_. (_.. System out) (println 2.3))
- (_. (_.. System out) (println #"Y"))
- (_. (_.. System out) (println "this\tis a\nstring"))
- (case (#Box "data")
- (#Box value)
- (_. (_.. System out) (println value)))) )#
+#( (def (template elems)
+ (case elems
+ #Nil
+ elems
+
+ (#Cons head tail)
+ (case head
+ (#Cons (#Symbol "~") (#Cons unquoted #Nil))
+ (#Cons unquoted (template tail))
+
+ (#Cons (#Symbol "~@") (#Cons spliced #Nil))
+ (#Cons (#Symbol "++") (#Cons spliced (template tail)))
+
+ _
+ (#Cons head (template tail)))
+ ))
+
+ (defmacro (' form)
+ (case form
+ (#Cons form* #Nil)
+ (case form*
+ (#Form elems)
+ (#Quote (#Form (template elems)))
+
+ _
+ (#Quote form*))
+ ))
+
+ (defmacro (::+ pieces)
+ (case pieces
+ (#Cons init #Nil)
+ init
+
+ (#Cons init (#Cons access others))
+ (' (::+ (:: (~ init) (~ access)) (~@ others)))
+ ))
+
+ (def (main args)
+ (if true
+ (let f (lambda [x] (lambda [y] (x y)))
+ (let g (lambda [x] const)
+ (::+ System out (println (f g "WE'VE GOT CLOSURES!")))
+ ## (:: (:: System out) (println (f g "WE'VE GOT CLOSURES!")))))
+ (:: (:: System out) (println "FALSE"))))
+ )#