From 93ff63219c7528074aae2d7f3e4f913b510a61bd Mon Sep 17 00:00:00 2001 From: Eduardo Julian Date: Thu, 29 Jan 2015 20:10:58 -0400 Subject: [Working on] - The monadic implementation of macros is almost done. - Missing error-handling. [Fixes] - The output folder is now generated on each compiler run to avoid exceptions thrown by the class-loader. --- source/lux.lux | 259 +++++++++++++++++++++++++++++++++------------------------ 1 file changed, 150 insertions(+), 109 deletions(-) (limited to 'source/lux.lux') diff --git a/source/lux.lux b/source/lux.lux index cab8a31d2..63ab93a4c 100644 --- a/source/lux.lux +++ b/source/lux.lux @@ -67,111 +67,152 @@ [java.lang.Object _7] [java.lang.Object _8]]) ## Base functions & macros -(def' id - (lambda' id x - x)) - -(def' + (lambda' + x (lambda' _ y (jvm:iadd x y)))) - -(def' fold - (lambda' fold f - (lambda' _ init - (lambda' _ values - (case values - #Nil - init - (#Cons x xs) - (fold f (f init x) xs) - ))))) - (annotate lambda Macro) (def' lambda (lambda' _ tokens (lambda' _ state - (let output (case tokens - (#Cons self (#Cons (#Tuple (#Cons arg args')) (#Cons body #Nil))) - (#Form (#Cons (#Ident "lambda'") - (#Cons self - (#Cons arg - (case args' - #Nil - (#Cons body #Nil) - - _ - (#Cons (#Ident "lux:lambda") - (#Cons (#Tuple args') - (#Cons body #Nil)))))))) - - (#Cons (#Tuple (#Cons arg args')) (#Cons body #Nil)) - (#Form (#Cons (#Ident "lambda'") - (#Cons (#Ident "_") - (#Cons arg - (case args' - #Nil - (#Cons body #Nil) - - _ - (#Cons (#Ident "lux:lambda") - (#Cons (#Tuple args') - (#Cons body #Nil))))))))) - [(#Cons output #Nil) state])))) - -(def cons - (lambda [tail head] - (#Cons head tail))) - -#( - (defmacro (lambda tokens) + (let' output (case tokens + (#Cons self (#Cons (#Tuple (#Cons arg args')) (#Cons body #Nil))) + (#Form (#Cons (#Ident "lambda'") + (#Cons self + (#Cons arg + (#Cons (case args' + #Nil + body + + _ + (#Form (#Cons (#Ident "lux:lambda") + (#Cons (#Tuple args') + (#Cons body #Nil))))) + #Nil))))) + + (#Cons (#Tuple (#Cons arg args')) (#Cons body #Nil)) + (#Form (#Cons (#Ident "lambda'") + (#Cons (#Ident "_") + (#Cons arg + (#Cons (case args' + #Nil + body + + _ + (#Form (#Cons (#Ident "lux:lambda") + (#Cons (#Tuple args') + (#Cons body #Nil))))) + #Nil)))))) + [(#Cons output #Nil) state])))) + +(annotate def Macro) +(def' def + (lambda [tokens state] + (let' output (case tokens + (#Cons (#Ident name) (#Cons body #Nil)) + (#Form (#Cons (#Ident "def'") + (#Cons (#Ident name) + (#Cons body #Nil)))) + + (#Cons (#Form (#Cons (#Ident name) args)) + (#Cons body #Nil)) + (#Form (#Cons (#Ident "def'") + (#Cons (#Ident name) + (#Cons (#Form (#Cons (#Ident "lux:lambda") + (#Cons (#Ident name) + (#Cons (#Tuple args) + (#Cons body #Nil))))) + #Nil))))) + [(#Cons output #Nil) state]))) + +(def (+ x y) + (jvm:iadd x y)) + +(def (id x) + x) + +(def (fold f init values) + (case values + #Nil + init + + (#Cons x xs) + (fold f (f init x) xs))) + +(def (reverse list) + (fold (lambda [tail head] (#Cons head tail)) + #Nil + list)) + +(annotate list Macro) +(def (list xs state) + (let' output (fold (lambda [tail head] + (#Form (#Cons (#Tag "Cons") + (#Cons head + (#Cons tail #Nil))))) + (#Tag "Nil") + (reverse xs)) + [(#Cons output #Nil) state])) + +(annotate list+ Macro) +(def (list+ xs state) + (case (reverse xs) + #Nil + [#Nil state] + + (#Cons last init') + (let' output (fold (lambda [tail head] + (#Form (#Cons (#Tag "Cons") + (#Cons head tail)))) + last + init') + [(#Cons output #Nil) state]))) + +(def (->pairs xs) + (case xs + (#Cons x (#Cons y xs')) + (#Cons [x y] (->pairs xs')) + + _ + #Nil)) + +#((annotate let Macro) + (def (let tokens state) (case tokens - (#Cons self (#Cons args (#Cons body #Nil))) - - - (#Cons args (#Cons body #Nil)) - )) - - (def' id (lambda [x] x)) - - (def' + (lambda [x y] (jvm:iadd x y))) - - (def (fold f init values) - (case values - #Nil - init - (#Cons x xs)x - (fold f (f init x) xs))) - - (def (cons tail head) - (#Cons head tail)) - - (def (reverse list) - (fold cons #Nil list)) - - (annotate list Macro) - (def (list xs) - (fold (lambda' tail - (lambda' head - (#Form (#Cons (#Tag "Cons") - (#Cons head - (#Cons tail #Nil)))))) - (#Tag "Nil") - (reverse xs))) - - (def (++ xs ys) - (case xs - #Nil - ys - - (#Cons x xs*) - (#Cons x (++ xs* ys)))) - - (def (map f xs) - (case xs - #Nil - #Nil - - (#Cons x xs*) - (#Cons (f x) (map f xs*)))) + (#Cons (#Tuple bindings) (#Cons body #Nil)) + (let' output (fold (lambda [body binding] + (case binding + [label value] + (#Form (list (#Ident "let'") label value body)))) + body + (reverse (->pairs bindings))) + [(list output) state]))))# + +(annotate let Macro) +(def (let tokens state) + (case tokens + (#Cons (#Tuple bindings) (#Cons body #Nil)) + (let' output (fold (lambda [body binding] + (case binding + [label value] + (#Form (#Cons (#Ident "let'") (#Cons label (#Cons value (#Cons body #Nil))))))) + body + (reverse (->pairs bindings))) + [(list output) state]))) + +(def (++ xs ys) + (case xs + #Nil + ys + + (#Cons x xs*) + (#Cons x (++ xs* ys)))) + +(def (map f xs) + (case xs + #Nil + #Nil + + (#Cons x xs*) + (#Cons (f x) (map f xs*)))) +#( (def (untemplate-list untemplate tokens) (case tokens #Nil @@ -265,19 +306,19 @@ (#Cons from (range (inc from) to)))) (def (text->list text) - (let length (jvm:invokevirtual String "length" [] - text []) - (map (lambda' idx - (jvm:invokevirtual String "charAt" [int] - text [idx])) - (range 0 length)))) + (let' length (jvm:invokevirtual String "length" [] + text []) + (map (lambda' idx + (jvm:invokevirtual String "charAt" [int] + text [idx])) + (range 0 length)))) (def (enumerate list) (case (fold (lambda' state - (lambda' x - (case state - [idx list'] - [(inc idx) (#Cons [idx x] list')]))) + (lambda' x + (case state + [idx list'] + [(inc idx) (#Cons [idx x] list')]))) [0 #Nil] list) [_ list'] -- cgit v1.2.3