From 57f89f16e95749e4ee4ad98a4e3d7a7908fb9a2f Mon Sep 17 00:00:00 2001 From: Eduardo Julian Date: Sun, 8 Mar 2015 12:44:42 -0400 Subject: - The implementation of monadic macros is finally finished. --- source/lux.lux | 134 ++++++++++++++++++++++++++++++--------------------------- 1 file changed, 70 insertions(+), 64 deletions(-) (limited to 'source/lux.lux') diff --git a/source/lux.lux b/source/lux.lux index 8aefa309a..24b5cb837 100644 --- a/source/lux.lux +++ b/source/lux.lux @@ -5,48 +5,48 @@ ## Base functions & macros (def' let' (lambda' _ tokens - (lambda' _ state - (case' tokens - (#Cons [lhs (#Cons [rhs (#Cons [body #Nil])])]) - [(#Cons [(#Form (#Cons [(#Ident "case'") (#Cons [rhs (#Cons [lhs (#Cons [body #Nil])])])])) - #Nil]) - state]) - ))) + (lambda' _ state + (case' tokens + (#Cons [lhs (#Cons [rhs (#Cons [body #Nil])])]) + (#Ok [state + (#Cons [(#Form (#Cons [(#Ident "case'") (#Cons [rhs (#Cons [lhs (#Cons [body #Nil])])])])) + #Nil])])) + ))) (declare-macro let') (def' lambda (lambda' _ tokens - (lambda' _ state - (let' output (case' tokens - (#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 [(#Ident self) (#Cons [(#Tuple (#Cons [arg args'])) (#Cons [body #Nil])])]) - (#Form (#Cons [(#Ident "lambda'") - (#Cons [(#Ident self) - (#Cons [arg - (#Cons [(case' args' - #Nil - body - - _ - (#Form (#Cons [(#Ident "lux;lambda") - (#Cons [(#Tuple args') - (#Cons [body #Nil])])]))) - #Nil])])])]))) - [(#Cons [output #Nil]) state]) - ))) + (lambda' _ state + (let' output (case' tokens + (#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 [(#Ident self) (#Cons [(#Tuple (#Cons [arg args'])) (#Cons [body #Nil])])]) + (#Form (#Cons [(#Ident "lambda'") + (#Cons [(#Ident self) + (#Cons [arg + (#Cons [(case' args' + #Nil + body + + _ + (#Form (#Cons [(#Ident "lux;lambda") + (#Cons [(#Tuple args') + (#Cons [body #Nil])])]))) + #Nil])])])]))) + (#Ok [state (#Cons [output #Nil])])) + ))) (declare-macro lambda) (def' def @@ -64,7 +64,7 @@ (#Cons [(#Tuple args) (#Cons [body #Nil])])])])) #Nil])])]))) - [(#Cons [output #Nil]) state]))) + (#Ok [state (#Cons [output #Nil])])))) (declare-macro def) (def (defmacro tokens state) @@ -80,11 +80,11 @@ (#Cons [body #Nil])])]))]) (let' declaration (#Form (#Cons [(#Ident "declare-macro") (#Cons [(#Ident fn-name) #Nil])])) - [(#Cons [fn-def (#Cons [declaration #Nil])]) state]))) + (#Ok [state (#Cons [fn-def (#Cons [declaration #Nil])])])))) (declare-macro defmacro) (defmacro (comment tokens state) - [#Nil state]) + (#Ok [state #Nil])) (def (int+ x y) (jvm-iadd x y)) @@ -121,9 +121,8 @@ (#Cons [(#Tuple (#Cons [head (#Cons [tail #Nil])])) #Nil])]))) (#Tag "Nil") - xs' - ) - [(#Cons [output #Nil]) state]))) + xs') + (#Ok [state (#Cons [output #Nil])])))) (defmacro (list+ xs state) (case' (reverse xs) @@ -135,7 +134,7 @@ (#Form (list (#Tag "Cons") (#Tuple (list head tail))))) last init') - [(#Cons [output #Nil]) state]))) + (#Ok [state (#Cons [output #Nil])])))) (def (as-pairs xs) (case' xs @@ -145,16 +144,16 @@ _ #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 (#Ident "lux;let'") label value body)))) -## body -## (reverse (as-pairs bindings))) -## [(list output) state]))) +(defmacro (let tokens state) + (case' tokens + (#Cons [(#Tuple bindings) (#Cons [body #Nil])]) + (let' output (fold (lambda [body binding] + (case' binding + [label value] + (#Form (list (#Ident "lux;let'") label value body)))) + body + (reverse (as-pairs bindings))) + (#Ok [state (list output)])))) (def (++ xs ys) (case' xs @@ -216,16 +215,16 @@ )) (defmacro (` tokens state) - [(list (untemplate-list (map untemplate tokens))) - state]) + (#Ok [state + (list (untemplate-list (map untemplate tokens)))])) (defmacro (if tokens state) (case' tokens (#Cons [test (#Cons [then (#Cons [else #Nil])])]) - [(` (case' (~ test) - true (~ then) - false (~ else))) - state])) + (#Ok [state + (` (case' (~ test) + true (~ then) + false (~ else)))]))) (def (filter p xs) (case' xs @@ -239,13 +238,20 @@ (def (return val) (lambda [state] - [state val])) + (#Ok [state val]))) + +(def (fail msg) + (lambda [_] + (#Error msg))) (def (bind f v) (lambda [state] (case' (v state) - [state' x] - ((f x) state')))) + (#Ok [state' x]) + (f x state') + + (#Error msg) + (#Error msg)))) #( -- cgit v1.2.3