diff options
-rw-r--r-- | src/example/test1.lang | 6 | ||||
-rw-r--r-- | src/lang/compiler.clj | 14 | ||||
-rw-r--r-- | src/lang/interpreter.clj | 43 | ||||
-rw-r--r-- | src/lang/lexer.clj | 4 | ||||
-rw-r--r-- | src/lang/parser.clj | 5 |
5 files changed, 36 insertions, 36 deletions
diff --git a/src/example/test1.lang b/src/example/test1.lang index f805b1085..c5d797beb 100644 --- a/src/example/test1.lang +++ b/src/example/test1.lang @@ -18,3 +18,9 @@ ## It's alive! (fold * 1 (repeat 5 5)) + +3.14 + +(def pi 3.14) + +pi diff --git a/src/lang/compiler.clj b/src/lang/compiler.clj index f189e8d25..74e0ba46e 100644 --- a/src/lang/compiler.clj +++ b/src/lang/compiler.clj @@ -51,6 +51,10 @@ [::&parser/int ?int] (return ?int)) +(defcompiler compile-float + [::&parser/float ?float] + (return ?float)) + (defcompiler compile-ident [::&parser/ident ?name] (return (symbol ?name))) @@ -106,8 +110,8 @@ (defcompiler compile-def [::&parser/def ?form ?body] (match ?form - [::&parser/fn-call ?name ?args] - (exec [:let [=name (symbol (unwrap-ident ?name)) + [::&parser/fn-call [::&parser/ident ?name] ?args] + (exec [:let [=name (symbol ?name) =args (map (comp symbol unwrap-ident) ?args) fn-env (into {} (for [a =args] [a nil]))] =body (apply-m compile-form (wrap* fn-env ?body)) @@ -118,7 +122,10 @@ `(fn ~=name ~?arg ~?body)) ;; _ (prn 'fn-def fn-def) ]] - (return fn-def)))) + (return fn-def)) + + [::&parser/ident ?name] + (apply-m compile-form (wrap ?body)))) (defcompiler compile-defdata [::&parser/defdata ?form ?cases] @@ -135,6 +142,7 @@ (def compile-form (try-all-m [compile-int + compile-float compile-ident compile-tuple compile-tagged diff --git a/src/lang/interpreter.clj b/src/lang/interpreter.clj index ba370ac2e..73148831e 100644 --- a/src/lang/interpreter.clj +++ b/src/lang/interpreter.clj @@ -74,6 +74,10 @@ [::&parser/int ?int] (return ?int)) +(defeval eval-float + [::&parser/float ?float] + (return ?float)) + (defeval eval-def [::&parser/def ?form ?body] (exec [;; :let [_ (prn 'eval-defdata ?form ?cases)] @@ -81,6 +85,9 @@ ;; :let [_ (prn 'eval-def 'DONE =value)] :let [=name (match ?form [::&parser/fn-call [::&parser/ident ?name] ?args] + ?name + + [::&parser/ident ?name] ?name) =value* (clojure.core/eval =value) ;; _ (prn '=value* =value*) @@ -96,10 +103,6 @@ ] (return nil))) -;; [:lang.parser/defdata [:lang.parser/fn-call [:lang.parser/ident "List"] ([:lang.parser/ident "x"])] -;; ([:lang.parser/tagged "Nil" [:lang.parser/tuple ()]] -;; [:lang.parser/tagged "Cons" [:lang.parser/tuple ([:lang.parser/ident "x"] [:lang.parser/fn-call [:lang.parser/ident "List"] ([:lang.parser/ident "x"])])]])] - (defeval eval-fn-call [::&parser/fn-call ?fn ?args] (exec [state &util/get-state @@ -112,18 +115,13 @@ (fn-call =fn =args))) (def eval-form - (try-all-m [eval-ident - eval-int + (try-all-m [eval-int + eval-float + eval-ident eval-def eval-defdata eval-fn-call])) -;; [::def [::fn-call [::ident "**"] ([::ident "base"] [::ident "exp"])] -;; [::fn-call [::ident "reduce"] ([::ident "*"] -;; [::int 1] -;; [::fn-call [::ident "repeat"] ([::ident "exp"] -;; [::ident "base"])])]] - (defn eval [text] (match ((repeat-m eval-form) text) [::&util/ok [?state ?forms]] @@ -144,31 +142,12 @@ (eval (update-in +state+ [:forms] concat syntax))) - - - - ;; (clojure.core/fn [base exp] (fold * 1 (repeat exp base))) - - ;; (* 5 6) - ;; (defdata (List x) ;; (#Nil []) ;; (#Cons [x] (List x))) - - ;; (def (repeat n val) - ;; (if (> v n) - ;; (#Nil []) - ;; (#Cons [val (repeat (- n 1) val)]))) - - ;; (def (fold f init inputs) - ;; (case input - ;; (#Nil _) init - ;; (#Cons [head tail]) (fold f (f init head) tail))) - + ;; (def (** base exp) ;; (fold * 1 (repeat exp base))) - ;; Syntax for single-line comments ## - ;; Syntax for multi-line comments #( YOLO )# ;; Syntax for chars: #"a" ) diff --git a/src/lang/lexer.clj b/src/lang/lexer.clj index a51330e09..927453aeb 100644 --- a/src/lang/lexer.clj +++ b/src/lang/lexer.clj @@ -50,6 +50,7 @@ (exec [token (lex-regex <regex>)] (return [<tag> token]))) + ^:private lex-float ::float #"^(0|[1-9][0-9]*)\.[0-9]+" ^:private lex-int ::int #"^(0|[1-9][0-9]*)" ^:private lex-ident ::ident +ident-re+) @@ -96,7 +97,8 @@ (def ^:private lex-form (exec [_ (try-m lex-white-space) - form (try-all-m [lex-int + form (try-all-m [lex-float + lex-int lex-ident lex-tag lex-list diff --git a/src/lang/parser.clj b/src/lang/parser.clj index 53f60941d..b3cb5cbc0 100644 --- a/src/lang/parser.clj +++ b/src/lang/parser.clj @@ -22,6 +22,10 @@ [::&lexer/int ?int] (return [::int (Long/parseLong ?int)])) +(defparser ^:private parse-float + [::&lexer/float ?float] + (return [::float (Double/parseDouble ?float)])) + (defparser ^:private parse-ident [::&lexer/ident ?ident] (return [::ident ?ident])) @@ -82,6 +86,7 @@ (def ^:private parse-form (try-all-m [parse-int + parse-float parse-ident parse-tuple parse-def |