aboutsummaryrefslogtreecommitdiff
path: root/test2.lux
diff options
context:
space:
mode:
Diffstat (limited to 'test2.lux')
-rw-r--r--test2.lux73
1 files changed, 57 insertions, 16 deletions
diff --git a/test2.lux b/test2.lux
index a75243191..af17b4c7a 100644
--- a/test2.lux
+++ b/test2.lux
@@ -67,6 +67,10 @@
[java.lang.Object _5] [java.lang.Object _6]
[java.lang.Object _7] [java.lang.Object _8]])
+(def (print x)
+ (jvm/invokevirtual java.io.PrintStream "print" [Object]
+ (jvm/getstatic System out) [x]))
+
(def (println x)
(jvm/invokevirtual java.io.PrintStream "println" [Object]
(jvm/getstatic System out) [x]))
@@ -123,6 +127,9 @@
(#Ident ident)
(#Form (#Cons (#Tag "Ident") (#Cons (#Text ident) #Nil)))
+ (#Tuple elems)
+ (#Form (#Cons (#Tag "Tuple") (#Cons (convert-list convert elems) #Nil)))
+
(#Form elems)
(#Form (#Cons (#Tag "Form") (#Cons (convert-list convert elems) #Nil)))
))
@@ -201,18 +208,51 @@
_
#Nil))
-#( (defmacro (exec tokens)
- (case tokens
- (#Cons (#Tuple steps) (#Cons return #Nil))
- (if (= 0 (rem (length steps) 2))
- (fold (lambda [inner pair]
- (case pair
- [label computation]
- (' (bind (~ computation)
- (lambda [(~ label)] (~ inner))))))
- return
- (as-pairs steps))
- (#Text "Oh no!")))) )#
+(defmacro (list xs)
+ (case xs
+ #Nil
+ (#Tag "Nil")
+
+ (#Cons x xs*)
+ (#Form (#Cons (#Tag "Cons") (#Cons x (#Cons (list xs*) #Nil))))))
+
+(defmacro (exec tokens)
+ (case tokens
+ (#Cons (#Tuple steps) (#Cons return #Nil))
+ (if (= 0 (rem (length steps) 2))
+ (fold (lambda [inner pair]
+ (case pair
+ [label computation]
+ (' (bind (~ computation)
+ (lambda [(~ label)] (~ inner))))))
+ return
+ (as-pairs steps))
+ (#Text "Oh no!"))))
+
+(def (cons tail head)
+ (#Cons head tail))
+
+(def (reverse list)
+ (fold cons #Nil list))
+
+(def (enumerate list)
+ (case (fold (lambda [state x]
+ (case state
+ [idx list']
+ [(inc idx) (#Cons [idx x] list')]))
+ [0 #Nil]
+ list)
+ [_ list']
+ (reverse list')))
+
+(def (print-enum enum)
+ (case enum
+ #Nil
+ (println "")
+
+ (#Cons [idx x] enum')
+ (do (print "[") (print idx) (print ":") (print x) (print "]") (print " ")
+ (print-enum enum'))))
## Program
(def (main args)
@@ -224,15 +264,16 @@
(println (jvm/i- 10 20))
(println (jvm/i* 10 20))
(println (jvm/i/ 10 2))
- (let list (#Cons 1 (#Cons 2 (#Cons 3 (#Cons 4 (#Cons 5 (#Cons 6 #Nil))))))
- (do (println (fold + 0 list))
- (println (length list))))
+ (let xs (list 1 2 3 4 5 6 7 8 9)
+ (do (println (fold + 0 xs))
+ (println (length xs))))
(println (rem 21 6))
(println (rem 21 7))
(println (= false false))
(println (= false true))
(println (= true false))
- (println (= true true)))
+ (println (= true true))
+ (print-enum (enumerate (list #"a" #"b" #"c" #"d" #"e"))))
))
#( (def (main args)