This commit is contained in:
Scott Richmond 2023-05-18 16:44:14 -04:00
parent a7ab313a5f
commit 23e29fdca2

View File

@ -1,8 +1,8 @@
(ns ludus.scanner (ns ludus.scanner
(:require (:require
[ludus.token :as token] [ludus.token :as token]
;; [clojure.pprint :as pp] ;; [clojure.pprint :as pp]
[clojure.edn :as edn])) [clojure.edn :as edn]))
(def reserved-words (def reserved-words
"List of Ludus reserved words." "List of Ludus reserved words."
@ -43,10 +43,10 @@
}) })
(def literal-words { (def literal-words {
"true" true "true" true
"false" false "false" false
"nil" nil "nil" nil
}) })
(defn- new-scanner (defn- new-scanner
"Creates a new scanner." "Creates a new scanner."
@ -85,8 +85,8 @@
(defn- char-in-range? [start end char] (defn- char-in-range? [start end char]
(and char (and char
(>= (int char) (int start)) (>= (int char) (int start))
(<= (int char) (int end)))) (<= (int char) (int end))))
(defn- digit? [c] (defn- digit? [c]
(char-in-range? \0 \9 c)) (char-in-range? \0 \9 c))
@ -127,27 +127,27 @@
(add-token scanner token-type nil)) (add-token scanner token-type nil))
([scanner token-type literal] ([scanner token-type literal]
(update scanner :tokens conj (update scanner :tokens conj
(token/token (token/token
token-type token-type
(current-lexeme scanner) (current-lexeme scanner)
literal literal
(:line scanner) (:line scanner)
(:start scanner))))) (:start scanner)))))
;; TODO: errors should also be in the vector of tokens ;; TODO: errors should also be in the vector of tokens
;; The goal is to be able to be able to hand this to an LSP? ;; The goal is to be able to be able to hand this to an LSP?
;; Do we need a different structure ;; Do we need a different structure
(defn- add-error [scanner msg] (defn- add-error [scanner msg]
(let [token (token/token (let [token (token/token
:error :error
(current-lexeme scanner) (current-lexeme scanner)
nil nil
(:line scanner) (:line scanner)
(:start scanner)) (:start scanner))
err-token (assoc token :message msg)] err-token (assoc token :message msg)]
(-> scanner (-> scanner
(update :errors conj err-token) (update :errors conj err-token)
(update :tokens conj err-token)))) (update :tokens conj err-token))))
(defn- add-keyword (defn- add-keyword
[scanner] [scanner]
@ -202,8 +202,8 @@
(let [curr (current-char scanner)] (let [curr (current-char scanner)]
(cond (cond
(terminates? curr) (add-token scanner (terminates? curr) (add-token scanner
(get reserved-words word :word) (get reserved-words word :word)
(get literal-words word :none)) (get literal-words word :none))
(word-char? curr) (recur (advance scanner) (str word curr)) (word-char? curr) (recur (advance scanner) (str word curr))
:else (add-error scanner (str "Unexpected " curr " after word " word ".")))))) :else (add-error scanner (str "Unexpected " curr " after word " word "."))))))
@ -332,7 +332,7 @@
(assoc scanner :start (:current scanner))) (assoc scanner :start (:current scanner)))
(defn scan [source] (defn scan [source]
(loop [scanner (new-scanner (str source "\n"))] (loop [scanner (new-scanner source)]
(if (at-end? scanner) (if (at-end? scanner)
(let [scanner (add-token scanner :eof)] (let [scanner (add-token scanner :eof)]
{:tokens (:tokens scanner) {:tokens (:tokens scanner)