From ca6e205c4d4f0ead6b82429a188301d00503d24d Mon Sep 17 00:00:00 2001 From: Eduardo Julian Date: Sun, 22 Mar 2015 02:20:30 -0400 Subject: - Fixed the bug that caused the infinite look while compiling the file. --- source/lux.lux | 2 +- src/lux.clj | 6 ++---- src/lux/analyser.clj | 7 +++---- src/lux/base.clj | 35 ++++++++++++++++++++++++++--------- src/lux/compiler.clj | 8 ++++++-- src/lux/lexer.clj | 3 +-- src/lux/reader.clj | 26 +++++++++++++++++++++++--- 7 files changed, 62 insertions(+), 25 deletions(-) diff --git a/source/lux.lux b/source/lux.lux index 9a0bab625..34b15fd49 100644 --- a/source/lux.lux +++ b/source/lux.lux @@ -25,7 +25,7 @@ ## Base functions & macros (def' _meta (lambda' _ data - (#Meta [["" -1 -1] data]))) + (#Meta [["" -1 -1] data]))) (def' let' (lambda' _ tokens diff --git a/src/lux.clj b/src/lux.clj index 3922a21d8..6d79b52bf 100644 --- a/src/lux.clj +++ b/src/lux.clj @@ -6,14 +6,12 @@ (comment ;; TODO: Finish type system. ;; TODO: Re-implement compiler in language. - ;; TODO: Adding metadata to global vars. - ;; TODO: Add column & line numbers for syntactic elements. - ;; TODO: Add text-dispenser to regulate column & line numbering. ;; TODO: Add source-file information to .class files for easier debugging. ;; TODO: Finish implementing class & interface definition ;; TODO: All optimizations + ;; TODO: Anonymous classes ;; TODO: - + ;; Finish total-locals (time (&compiler/compile-all (&/|list "lux"))) diff --git a/src/lux/analyser.clj b/src/lux/analyser.clj index 751e41923..e73423ffc 100644 --- a/src/lux/analyser.clj +++ b/src/lux/analyser.clj @@ -3,6 +3,7 @@ [clojure.core.match :as M :refer [matchv]] clojure.core.match.array (lux [base :as & :refer [exec return fail]] + [reader :as &reader] [parser :as &parser] [type :as &type] [macro :as ¯o] @@ -95,7 +96,7 @@ ["lux;Nil" _]]]]]]]]]]] (do ;; (when (= "if" ?name) ;; (prn "if" (&/show-ast ?value))) - (&&lux/analyse-def analyse ?name ?value)) + (&&lux/analyse-def analyse ?name ?value)) [["lux;Meta" [meta ["lux;Form" ["lux;Cons" [["lux;Meta" [_ ["lux;Symbol" [_ "declare-macro"]]]] ["lux;Cons" [["lux;Meta" [_ ["lux;Symbol" ?ident]]] @@ -465,7 +466,5 @@ ;; [Resources] (defn analyse [eval!] - (exec [asts &parser/parse - ;; :let [_ (prn 'analyse/asts asts)] - ] + (exec [asts &parser/parse] (&/flat-map% (analyse-ast eval!) asts))) diff --git a/src/lux/base.clj b/src/lux/base.clj index 7d0c03c86..089d1bf8a 100644 --- a/src/lux/base.clj +++ b/src/lux/base.clj @@ -185,6 +185,14 @@ [["lux;Cons" [x xs*]]] (V "lux;Cons" (T (f x) (|map f xs*))))) +(defn |empty? [xs] + (matchv ::M/objects [xs] + [["lux;Nil" _]] + true + + [["lux;Cons" [_ _]]] + false)) + (defn |filter [p xs] (matchv ::M/objects [xs] [["lux;Nil" _]] @@ -422,15 +430,24 @@ )) (defn exhaust% [step] - (try-all% (|list (exec [output-h step - output-t (exhaust% step)] - (return (|cons output-h output-t))) - ;; (return (|list)) - ;; (exec [? source-consumed?] - ;; (if ? - ;; (return (|list)) - ;; (exhaust% step))) - ))) + (fn [state] + (matchv ::M/objects [(step state)] + [["lux;Right" [state* _]]] + ((exhaust% step) state*) + + [["lux;Left" msg]] + ((exec [? source-consumed?] + (if ? + (return nil) + (fail* msg))) + state) + ;; (if (= "[Reader Error] EOF" msg) + ;; ((exec [? source-consumed? + ;; :let [_ (prn '? ?)]] + ;; (return nil)) + ;; state) + ;; (fail* msg)) + ))) (defn ^:private normalize-char [char] (case char diff --git a/src/lux/compiler.clj b/src/lux/compiler.clj index ab1a5012f..53787473b 100644 --- a/src/lux/compiler.clj +++ b/src/lux/compiler.clj @@ -364,7 +364,11 @@ (let [compiler-step (exec [analysis+ (&optimizer/optimize eval!) ;; :let [_ (prn 'analysis+ analysis+)] ] - (&/map% compile-statement analysis+))] + (&/map% compile-statement analysis+) + ;; (if (&/|empty? analysis+) + ;; (fail "[Compiler Error] No more to compile.") + ;; (&/map% compile-statement analysis+)) + )] (defn ^:private compile-module [name] (fn [state] (if (->> state (&/get$ "lux;modules") (&/|contains? name)) @@ -377,7 +381,7 @@ (&/set$ "lux;global-env" (&/V "lux;Some" (&/env name))) (&/set$ "lux;writer" (&/V "lux;Some" =class)) (&/update$ "lux;modules" #(&/|put name &a-def/init-module %))))] - [["lux;Right" [?state ?vals]]] + [["lux;Right" [?state _]]] (do (.visitEnd =class) ;; (prn 'compile-module 'DONE name) ;; (prn 'compile-module/?vals ?vals) diff --git a/src/lux/lexer.clj b/src/lux/lexer.clj index f53a01665..918ddc7d9 100644 --- a/src/lux/lexer.clj +++ b/src/lux/lexer.clj @@ -34,8 +34,7 @@ (def ^:private lex-single-line-comment (exec [[_ [meta _]] (&reader/read-text "##") - [_ [_ comment]] (&reader/read-regex #"^([^\n]*)") - _ (&reader/read-regex #"^(\n?)")] + [_ [_ comment]] (&reader/read-regex #"^(.*)$")] (return (&/V "lux;Meta" (&/T meta (&/V "Comment" comment)))))) (defn ^:private lex-multi-line-comment [___] diff --git a/src/lux/reader.clj b/src/lux/reader.clj index b4824261a..a1e447669 100644 --- a/src/lux/reader.clj +++ b/src/lux/reader.clj @@ -35,7 +35,9 @@ (fn [file-name line-num column-num line] (if-let [[match] (re-find regex line)] (let [match-length (.length match) - line* (.substring line match-length)] + line* (.substring line match-length) + ;; _ (prn 'with-line line*) + ] (&/V "Yes" (&/T (&/V "lux;Meta" (&/T (&/T file-name line-num column-num) match)) (if (empty? line*) (&/V "lux;None" nil) @@ -47,7 +49,9 @@ (fn [file-name line-num column-num line] (if-let [[match tok1 tok2] (re-find regex line)] (let [match-length (.length match) - line* (.substring line match-length)] + line* (.substring line match-length) + ;; _ (prn 'with-line line*) + ] (&/V "Yes" (&/T (&/V "lux;Meta" (&/T (&/T file-name line-num column-num) [tok1 tok2])) (if (empty? line*) (&/V "lux;None" nil) @@ -60,7 +64,9 @@ ;; (prn 'read-text text line) (if (.startsWith line text) (let [match-length (.length text) - line* (.substring line match-length)] + line* (.substring line match-length) + ;; _ (prn 'with-line line*) + ] (&/V "Yes" (&/T (&/V "lux;Meta" (&/T (&/T file-name line-num column-num) text)) (if (empty? line*) (&/V "lux;None" nil) @@ -78,3 +84,17 @@ (not (empty? line)))) (&/zip2 lines (&/|range (&/|length lines))))))) + +(def current-line + (fn [state] + (matchv ::M/objects [(&/get$ "lux;source" state)] + [["lux;None" _]] + (fail* "[Reader Error] No source code.") + + [["lux;Some" ["lux;Nil" _]]] + (fail* "[Reader Error] EOF") + + [["lux;Some" ["lux;Cons" [["lux;Meta" [_ line]] + more]]]] + (return* state line) + ))) -- cgit v1.2.3