From 0a0300b129df4499782cbe47aeaee581f57cc3db Mon Sep 17 00:00:00 2001 From: Eduardo Julian Date: Fri, 26 Dec 2014 01:00:30 -0400 Subject: Reimplemented basic pattern-matching. --- test2.lang | 94 ++++++++++++++++++++++++++++++++++++++------------------------ 1 file changed, 58 insertions(+), 36 deletions(-) (limited to 'test2.lang') 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")))) + )# -- cgit v1.2.3