Compare commits
2 Commits
6a4e2ccd17
...
20cb689d12
Author | SHA1 | Date | |
---|---|---|---|
|
20cb689d12 | ||
|
5874a56090 |
|
@ -44,7 +44,7 @@
|
|||
:dict (dict-str value)
|
||||
:set
|
||||
(string/join (map stringify (keys value)) ", ")
|
||||
:ref (stringify (value :^value))
|
||||
:box (stringify (value :^value))
|
||||
:fn (string "fn " (value :name))
|
||||
:applied (string "fn " (value :name))
|
||||
:function (string "builtin " (string value))
|
||||
|
@ -68,7 +68,7 @@
|
|||
:list (string "[" (stringify x) "]")
|
||||
:dict (string "#{" (stringify x) "}")
|
||||
:set (string "${" (stringify x) "}")
|
||||
:ref (string "box " (x :name) " [ " (stringify x) " ]")
|
||||
:box (string "box " (x :name) " [ " (stringify x) " ]")
|
||||
:pkg (show-pkg x)
|
||||
(stringify x)))
|
||||
|
||||
|
|
|
@ -1,10 +1,34 @@
|
|||
(import spork/json :as j)
|
||||
(import /base :as b)
|
||||
|
||||
(defn- get-line [source line]
|
||||
((string/split "\n" source) (dec line)))
|
||||
|
||||
(defn scan-error [e out] (set (out :errors) e) (j/encode out))
|
||||
|
||||
(defn parse-error [e out] (set (out :errors) e) (j/encode out))
|
||||
(defn parse-error [e]
|
||||
(def msg (e :msg))
|
||||
(def line-num (get-in e [:token :line]))
|
||||
(def source (get-in e [:token :source]))
|
||||
(def source-line (get-line source line-num))
|
||||
(print "Parsing error: " msg)
|
||||
(print "On line " line-num ":")
|
||||
(print source-line))
|
||||
|
||||
(defn validation-error [e out] (set (out :errors) e) (j/encode out))
|
||||
|
||||
(defn validation-error [e]
|
||||
(def msg (e :msg))
|
||||
(def line-num (get-in e [:node :token :line]))
|
||||
(def source (get-in e [:node :token :source]))
|
||||
(def source-line (get-line source line-num))
|
||||
(case msg
|
||||
"unbound name"
|
||||
(do
|
||||
(print "Validation error: " msg " " (get-in e [:node :data]))
|
||||
(print "on line " line-num)
|
||||
(print source-line)
|
||||
)
|
||||
)
|
||||
)
|
||||
|
||||
(defn runtime-error [e out] (set (out :errors) e) (j/encode out))
|
||||
|
|
|
@ -340,7 +340,7 @@
|
|||
(defn- ref [ast ctx]
|
||||
(def {:data value-ast :name name} ast)
|
||||
(def value (interpret value-ast ctx))
|
||||
(def box @{:^type :ref :^value value :name name})
|
||||
(def box @{:^type :box :^value value :name name})
|
||||
(set (ctx name) box)
|
||||
box)
|
||||
|
||||
|
|
|
@ -16,16 +16,16 @@ The API from the old Clojure Ludus interpreter returns an object with four field
|
|||
* `errors`: an array of errors, which are just strings
|
||||
|
||||
This new scene will have to return a JSON POJSO:
|
||||
{:console [...] :result "..." :draw [...] :errors [...]}
|
||||
{:console "..." :result "..." :draw [...] :errors [...]}
|
||||
)
|
||||
|
||||
(def console @"")
|
||||
(setdyn :out console)
|
||||
(print "foo")
|
||||
(pp {:a 1 :b 2})
|
||||
(setdyn :out stdout)
|
||||
(print "collected out")
|
||||
(print console)
|
||||
(def prelude-src (slurp "prelude.ld"))
|
||||
(def prelude-scanned (s/scan prelude-src))
|
||||
(def prelude-parsed (p/parse prelude-scanned))
|
||||
(def parse-errors (prelude-parsed :errors))
|
||||
(when (any? parse-errors) (each err parse-errors (e/parse-error err)))
|
||||
(def prelude-validated (v/valid prelude-parsed @{"base" b/base}))
|
||||
(each err (prelude-validated :errors) (e/validation-error err))
|
||||
|
||||
(defn run [source]
|
||||
(def errors @[])
|
||||
|
@ -44,7 +44,7 @@ This new scene will have to return a JSON POJSO:
|
|||
(break (-> :errors validated (e/validation-error out))))
|
||||
(setdyn :out console)
|
||||
(try
|
||||
(set result (b/show (i/interpret (parsed :ast) @{})))
|
||||
(set result (b/show (i/interpret (parsed :ast) @{:^parent b/base})))
|
||||
([err] (e/runtime-error err out)))
|
||||
(set (out :result) result)
|
||||
(j/encode out))
|
||||
|
@ -56,7 +56,7 @@ This new scene will have to return a JSON POJSO:
|
|||
(run source))
|
||||
|
||||
(def source `
|
||||
fn foo () -> :foo
|
||||
let foo = fn () -> :bar
|
||||
foo ()
|
||||
`)
|
||||
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
### A recursive descent parser for Ludus
|
||||
|
||||
### We still need to scan some things
|
||||
# (try (os/cd "janet") ([_] nil)) # when in repl to do relative imports
|
||||
(try (os/cd "janet") ([_] nil)) # when in repl to do relative imports
|
||||
(import ./scanner :as s)
|
||||
|
||||
(defmacro declare
|
||||
|
@ -773,7 +773,7 @@
|
|||
(defn- lambda [parser]
|
||||
(def origin (current parser))
|
||||
(expect parser :fn) (advance parser)
|
||||
@{:type :fn :data (fn-simple parser) :token origin})
|
||||
@{:type :fn :data ((fn-simple parser) :clauses) :token origin})
|
||||
|
||||
(defn- fnn [parser]
|
||||
(if (= :lparen (-> parser peek type)) (break (lambda parser)))
|
||||
|
@ -1104,6 +1104,7 @@
|
|||
(def origin (current parser))
|
||||
(def lines @[])
|
||||
(while (not (check parser :eof))
|
||||
(accept-many parser :newline)
|
||||
(array/push lines (capture toplevel parser))
|
||||
(capture terminator parser))
|
||||
{:type :script :data lines :token origin})
|
||||
|
@ -1116,10 +1117,10 @@
|
|||
|
||||
# (do
|
||||
(comment
|
||||
(def source `pkg Foo {}
|
||||
(def source `fn () -> 42
|
||||
`)
|
||||
(def scanned (s/scan source))
|
||||
# (print "\n***NEW PARSE***\n")
|
||||
(def a-parser (new-parser scanned))
|
||||
(def parsed (pkg a-parser))
|
||||
(def parsed (fnn a-parser))
|
||||
)
|
||||
|
|
1405
janet/prelude.ld
Normal file
1405
janet/prelude.ld
Normal file
File diff suppressed because it is too large
Load Diff
|
@ -173,7 +173,7 @@ Deferred until a later iteration of Ludus:
|
|||
:dict
|
||||
:list
|
||||
:fn
|
||||
:ref
|
||||
:box
|
||||
:pkg
|
||||
])
|
||||
|
||||
|
@ -296,7 +296,7 @@ Deferred until a later iteration of Ludus:
|
|||
(defn- fnn [validator]
|
||||
(def ast (validator :ast))
|
||||
(def name (ast :name))
|
||||
# (print "function name: " name)
|
||||
(print "function name: " name)
|
||||
(def status (validator :status))
|
||||
(def tail? (status :tail))
|
||||
(set (status :tail) true)
|
||||
|
|
Loading…
Reference in New Issue
Block a user