unify
This commit is contained in:
1 parent
83e042fb81
commit
a2b73e761b
2 files changed
+53
-18
No files matched your search
+30
-18
@@ -1,4 +1,26 @@
|
||||
(ns expand.syntax-rules)
|
||||
(ns expand.syntax-rules
|
||||
(:require [clojure.core.match :refer [match]]
|
||||
[clojure.spec.alpha :as s]
|
||||
[scm]))
|
||||
|
||||
(s/def ::pattern
|
||||
(s/or :wildcard #{'_}
|
||||
:var (s/and symbol? (complement #{'... '_}))
|
||||
:lit scm/constant?
|
||||
:list (s/and seq?
|
||||
(s/cat :leading (s/* ::pattern)
|
||||
:rest (s/? (s/cat :ellipsis #{'...}
|
||||
:trailing (s/* ::pattern))))
|
||||
(s/conformer
|
||||
(fn [pat]
|
||||
(if-some [{:keys [ellipsis trailing]} (:rest pat)]
|
||||
(let [{:keys [leading]} pat]
|
||||
(if-some [variadic (last leading)]
|
||||
{:leading (vec (butlast leading))
|
||||
:ellipsis variadic
|
||||
:trailing trailing}
|
||||
::s/invalid))
|
||||
pat))))))
|
||||
|
||||
(defn- report-conflict [x y]
|
||||
(throw (ex-info "너무 정의합니다"
|
||||
@@ -48,22 +70,12 @@
|
||||
(defn unify [pattern datum]
|
||||
(unify* (s/conform ::pattern pattern) datum))
|
||||
|
||||
(defn transcribe [template env symtable scopes]
|
||||
(defn transcribe [template]
|
||||
(match template
|
||||
[:var x] (let [x* (offer-scopes x scopes)]
|
||||
(match (lookup x* env symtable)
|
||||
[:syntax [:ellipsis fuckoffmanjustshadowit]] (todo "점점점")
|
||||
[:syntax syn] syn
|
||||
:else x*))
|
||||
[:ellipsis _] (todo "점점점")
|
||||
[:lit x] x
|
||||
[:list xs]
|
||||
(do (tap> xs)
|
||||
(map #(transcribe % env symtable scopes) xs))))
|
||||
[:var x] (throw (Exception. "todo"))
|
||||
[:ellipsis _] (throw (Exception. "todo 점점점"))
|
||||
[:lit x] (throw (Exception. "todo 점점점"))
|
||||
[:list xs] (throw (Exception. "todo 점점점"))))
|
||||
|
||||
(defn resolve-template [template scopes]
|
||||
(clojure.walk/postwalk
|
||||
#(if (variable? %)
|
||||
(offer-scopes % scopes)
|
||||
%)
|
||||
template))
|
||||
(comment
|
||||
(unify 'x '(1 2 3)))
|
||||
Reference in new issue
Block a user