diff options
Diffstat (limited to 'source')
-rw-r--r-- | source/lux.lux | 455 |
1 files changed, 229 insertions, 226 deletions
diff --git a/source/lux.lux b/source/lux.lux index 9e5885e97..ccc1476de 100644 --- a/source/lux.lux +++ b/source/lux.lux @@ -80,237 +80,237 @@ (#Right [state (#Cons [fn-def (#Cons [declaration #Nil])])])))) (declare-macro defmacro) -## (defmacro (comment tokens state) -## (#Right [state #Nil])) +(defmacro (comment tokens state) + (#Right [state #Nil])) -## (def (int+ x y) -## (jvm-ladd x y)) +(def (int+ x y) + (jvm-ladd x y)) -## (def (id x) -## x) +(def (id x) + x) -## (def (print x) -## (jvm-invokevirtual java.io.PrintStream "print" [java.lang.Object] -## (jvm-getstatic java.lang.System "out") [x])) +(def (print x) + (jvm-invokevirtual java.io.PrintStream "print" [java.lang.Object] + (jvm-getstatic java.lang.System "out") [x])) -## (def (println x) -## (jvm-invokevirtual java.io.PrintStream "println" [java.lang.Object] -## (jvm-getstatic java.lang.System "out") [x])) +(def (println x) + (jvm-invokevirtual java.io.PrintStream "println" [java.lang.Object] + (jvm-getstatic java.lang.System "out") [x])) -## (def (fold f init xs) -## (case' xs -## #Nil -## init +(def (fold f init xs) + (case' xs + #Nil + init -## (#Cons [x xs']) -## (fold f (f init x) xs'))) - -## (def (reverse list) -## (fold (lambda [tail head] -## (#Cons [head tail])) -## #Nil -## list)) - -## (defmacro (list xs state) -## (let' xs' (reverse xs) -## (let' output (fold (lambda [tail head] -## (#Form (#Cons [(#Tag "Cons") -## (#Cons [(#Tuple (#Cons [head (#Cons [tail #Nil])])) -## #Nil])]))) -## (#Tag "Nil") -## xs') -## (#Right [state (#Cons [output #Nil])])))) - -## (defmacro (list+ xs state) -## (case' (reverse xs) -## #Nil -## [#Nil state] - -## (#Cons [last init']) -## (let' output (fold (lambda [tail head] -## (#Form (list (#Tag "Cons") (#Tuple (list head tail))))) -## last -## init') -## (#Right [state (#Cons [output #Nil])])))) - -## (def (as-pairs xs) -## (case' xs -## (#Cons [x (#Cons [y xs'])]) -## (#Cons [[x y] (as-pairs xs')]) - -## _ -## #Nil)) - -## (defmacro (let tokens state) -## (case' tokens -## (#Cons [(#Tuple bindings) (#Cons [body #Nil])]) -## (let' output (fold (lambda [body binding] -## (case' binding -## [label value] -## (#Form (list (#Symbol "lux;let'") label value body)))) -## body -## (reverse (as-pairs bindings))) -## (#Right [state (list output)])))) - -## (def (. f g) -## (lambda [x] (f (g x)))) - -## (def (++ xs ys) -## (case' xs -## #Nil -## ys - -## (#Cons [x xs*]) -## (#Cons [x (++ xs* ys)]))) - -## (def concat -## (fold ++ #Nil)) - -## (def (map f xs) -## (case' xs -## #Nil -## #Nil - -## (#Cons [x xs']) -## (#Cons [(f x) (map f xs')]))) - -## (def flat-map (. concat map)) - -## (def (untemplate-list tokens) -## (case' tokens -## #Nil -## (#Tag "Nil") - -## (#Cons [token tokens']) -## (#Form (#Cons [(#Tag "Cons") -## (#Cons [(#Tuple (#Cons [token (#Cons [(untemplate-list tokens') #Nil])])) -## #Nil])])))) - -## (def (untemplate token) -## (case' token -## (#Bool value) -## (#Form (list (#Tag "Bool") (#Bool value))) - -## (#Int value) -## (#Form (list (#Tag "Int") (#Int value))) - -## (#Real value) -## (#Form (list (#Tag "Real") (#Real value))) - -## (#Char value) -## (#Form (list (#Tag "Char") (#Char value))) - -## (#Text value) -## (#Form (list (#Tag "Text") (#Text value))) - -## (#Tag value) -## (#Form (list (#Tag "Tag") (#Text value))) - -## (#Symbol value) -## (#Form (list (#Tag "Symbol") (#Text value))) + (#Cons [x xs']) + (fold f (f init x) xs'))) + +(def (reverse list) + (fold (lambda [tail head] + (#Cons [head tail])) + #Nil + list)) + +(defmacro (list xs state) + (let' xs' (reverse xs) + (let' output (fold (lambda [tail head] + (#Form (#Cons [(#Tag "Cons") + (#Cons [(#Tuple (#Cons [head (#Cons [tail #Nil])])) + #Nil])]))) + (#Tag "Nil") + xs') + (#Right [state (#Cons [output #Nil])])))) + +(defmacro (list+ xs state) + (case' (reverse xs) + #Nil + [#Nil state] + + (#Cons [last init']) + (let' output (fold (lambda [tail head] + (#Form (list (#Tag "Cons") (#Tuple (list head tail))))) + last + init') + (#Right [state (#Cons [output #Nil])])))) + +(def (as-pairs xs) + (case' xs + (#Cons [x (#Cons [y xs'])]) + (#Cons [[x y] (as-pairs xs')]) + + _ + #Nil)) + +(defmacro (let tokens state) + (case' tokens + (#Cons [(#Tuple bindings) (#Cons [body #Nil])]) + (let' output (fold (lambda [body binding] + (case' binding + [label value] + (#Form (list (#Symbol "lux;let'") label value body)))) + body + (reverse (as-pairs bindings))) + (#Right [state (list output)])))) + +(def (. f g) + (lambda [x] (f (g x)))) + +(def (++ xs ys) + (case' xs + #Nil + ys + + (#Cons [x xs*]) + (#Cons [x (++ xs* ys)]))) + +(def concat + (fold ++ #Nil)) + +(def (map f xs) + (case' xs + #Nil + #Nil + + (#Cons [x xs']) + (#Cons [(f x) (map f xs')]))) + +(def flat-map (. concat map)) + +(def (untemplate-list tokens) + (case' tokens + #Nil + (#Tag "Nil") + + (#Cons [token tokens']) + (#Form (#Cons [(#Tag "Cons") + (#Cons [(#Tuple (#Cons [token (#Cons [(untemplate-list tokens') #Nil])])) + #Nil])])))) + +(def (untemplate token) + (case' token + (#Bool value) + (#Form (list (#Tag "Bool") (#Bool value))) + + (#Int value) + (#Form (list (#Tag "Int") (#Int value))) + + (#Real value) + (#Form (list (#Tag "Real") (#Real value))) + + (#Char value) + (#Form (list (#Tag "Char") (#Char value))) + + (#Text value) + (#Form (list (#Tag "Text") (#Text value))) + + (#Tag value) + (#Form (list (#Tag "Tag") (#Text value))) + + (#Symbol value) + (#Form (list (#Tag "Symbol") (#Text value))) + + (#Tuple elems) + (#Form (list (#Tag "Tuple") (untemplate-list (map untemplate elems)))) + + (#Form (#Cons [(#Symbol "~") (#Cons [unquoted #Nil])])) + unquoted + + (#Form elems) + (#Form (list (#Tag "Form") (untemplate-list (map untemplate elems)))) + )) + +(defmacro (` tokens state) + (case' tokens + (#Cons [template #Nil]) + (#Right [state (list (untemplate template))]))) + +(defmacro (if tokens state) + (case' tokens + (#Cons [test (#Cons [then (#Cons [else #Nil])])]) + (#Right [state + (list (` (case' (~ test) + true (~ then) + false (~ else))))]))) + +(def (filter p xs) + (case' xs + #Nil + #Nil + + (#Cons [x xs*]) + (if (p x) + (filter p xs*) + (#Cons [x (filter p xs*)])))) + +(def (return val) + (lambda [state] + (#Right [state val]))) + +(def (fail msg) + (lambda [_] + (#Left msg))) -## (#Tuple elems) -## (#Form (list (#Tag "Tuple") (untemplate-list (map untemplate elems)))) - -## (#Form (#Cons [(#Symbol "~") (#Cons [unquoted #Nil])])) -## unquoted - -## (#Form elems) -## (#Form (list (#Tag "Form") (untemplate-list (map untemplate elems)))) -## )) - -## (defmacro (` tokens state) -## (case' tokens -## (#Cons [template #Nil]) -## (#Right [state (list (untemplate template))]))) - -## (defmacro (if tokens state) -## (case' tokens -## (#Cons [test (#Cons [then (#Cons [else #Nil])])]) -## (#Right [state -## (list (` (case' (~ test) -## true (~ then) -## false (~ else))))]))) - -## (def (filter p xs) -## (case' xs -## #Nil -## #Nil - -## (#Cons [x xs*]) -## (if (p x) -## (filter p xs*) -## (#Cons [x (filter p xs*)])))) - -## (def (return val) -## (lambda [state] -## (#Right [state val]))) - -## (def (fail msg) -## (lambda [_] -## (#Left msg))) - -## (def (bind f v) -## (lambda [state] -## (case' (v state) -## (#Right [state' x]) -## (f x state') +(def (bind f v) + (lambda [state] + (case' (v state) + (#Right [state' x]) + (f x state') -## (#Left msg) -## (#Left msg)))) - -## (def (first pair) -## (case' pair -## [f s] -## f)) - -## (def (second pair) -## (case' pair -## [f s] -## s)) - -## (defmacro (loop tokens) -## (case' tokens -## (#Cons [bindings (#Cons [body #Nil])]) -## (let [pairs (as-pairs bindings)] -## (return (list (#Form (#Cons [(` (lambda (~ (#Symbol "recur")) (~ (#Tuple (map first pairs))) -## (~ body))) -## (map second pairs)]))))))) - -## (defmacro (export tokens) -## (return (map (lambda [t] (` (export' (~ t)))) -## tokens))) - -## (defmacro (and tokens) -## (let [as-if (case' tokens -## #Nil -## (` true) - -## (#Cons [init tests]) -## (fold (lambda [prev next] -## (` (if (~ prev) (~ next) false))) -## init -## tokens) -## )] -## (return (list as-if)))) - -## (defmacro (or tokens) -## (let [as-if (case' tokens -## #Nil -## (` false) - -## (#Cons [init tests]) -## (fold (lambda [prev next] -## (` (if (~ prev) true (~ next)))) -## init -## tokens) -## )] -## (return (list as-if)))) - -## (def (not x) -## (case' x -## true false -## false true)) + (#Left msg) + (#Left msg)))) + +(def (first pair) + (case' pair + [f s] + f)) + +(def (second pair) + (case' pair + [f s] + s)) + +(defmacro (loop tokens) + (case' tokens + (#Cons [bindings (#Cons [body #Nil])]) + (let [pairs (as-pairs bindings)] + (return (list (#Form (#Cons [(` (lambda (~ (#Symbol "recur")) (~ (#Tuple (map first pairs))) + (~ body))) + (map second pairs)]))))))) + +(defmacro (export tokens) + (return (map (lambda [t] (` (export' (~ t)))) + tokens))) + +(defmacro (and tokens) + (let [as-if (case' tokens + #Nil + (` true) + + (#Cons [init tests]) + (fold (lambda [prev next] + (` (if (~ prev) (~ next) false))) + init + tokens) + )] + (return (list as-if)))) + +(defmacro (or tokens) + (let [as-if (case' tokens + #Nil + (` false) + + (#Cons [init tests]) + (fold (lambda [prev next] + (` (if (~ prev) true (~ next)))) + init + tokens) + )] + (return (list as-if)))) + +(def (not x) + (case' x + true false + false true)) ## (defmacro (|> tokens) ## (case' tokens @@ -756,10 +756,13 @@ ## (#Cons [args (#Cons [body #Nil])]) ## (return (list (` (All (~ args) (~ body))))))) -## (def Any (| #Any)) -## (def Nothing (| #Nothing)) -## (def Text (^ java.lang.String)) +## (def Any #Any) +## (def Nothing #Nothing) +## (def Bool (^ java.lang.Boolean)) ## (def Int (^ java.lang.Long)) +## (def Real (^ java.lang.Double)) +## (def Char (^ java.lang.Character)) +## (def Text (^ java.lang.String)) ## (deftype (List a) ## (| #Nil |