start work on interpreter

This commit is contained in:
Scott Richmond 2024-05-14 20:44:54 -04:00
parent c68d08e8b2
commit 3a8a236f01
2 changed files with 54 additions and 0 deletions

0
janet/base.janet Normal file
View File

54
janet/interpreter.janet Normal file
View File

@ -0,0 +1,54 @@
# A tree walk interpreter for ludus
(var interpret nil)
(defn- iff [ast ctx]
(def [condition then else] (ast :data))
(if (interpret condition ctx)
(interpret then ctx)
(interpret else ctx)))
(defn- script [ast ctx]
(print "interpreting script")
(def lines (ast :data))
(var result nil)
(each line lines
(print "interpreting script line")
(set result (interpret line ctx)))
result)
(defn- interpret* [ast ctx]
(print "interpreting ast node " (ast :type))
(case (ast :type)
:nil nil
:number (ast :data)
:bool (ast :data)
:string (ast :data)
:keyword (ast :data)
:if (iff ast ctx)
:script (script ast ctx)))
(set interpret interpret*)
# repl
(try (os/cd "janet") ([_] nil))
(import ./scanner :as s)
(import ./parser :as p)
(import ./validate :as v)
(var source nil)
(defn run []
(def scanned (s/scan source))
(def parsed (p/parse scanned))
(def validated (v/valid parsed))
(pp parsed)
(interpret (parsed :ast) @{}))
(do
(set source `
if false then :bar else :baz
`)
(run)
)