mirror of
https://github.com/janet-lang/janet
synced 2025-01-13 09:00:26 +00:00
Simplify eval-string implementation.
This commit is contained in:
parent
a3a42eebea
commit
8d1cfe0c56
@ -2542,31 +2542,6 @@
|
|||||||
(setdyn :exit-value value)
|
(setdyn :exit-value value)
|
||||||
nil)
|
nil)
|
||||||
|
|
||||||
(defn eval-string
|
|
||||||
``Evaluates a string in the current environment. If more control over the
|
|
||||||
environment is needed, use `run-context`.``
|
|
||||||
[str]
|
|
||||||
(var state (string str))
|
|
||||||
(defn chunks [buf _]
|
|
||||||
(def ret state)
|
|
||||||
(set state nil)
|
|
||||||
(when ret
|
|
||||||
(buffer/push-string buf str)
|
|
||||||
(buffer/push-string buf "\n")))
|
|
||||||
(var returnval nil)
|
|
||||||
(run-context {:chunks chunks
|
|
||||||
:on-compile-error (fn compile-error [msg errf &]
|
|
||||||
(error (string "compile error: " msg)))
|
|
||||||
:on-parse-error (fn parse-error [p x]
|
|
||||||
(error (string "parse error: " (:error p))))
|
|
||||||
:fiber-flags :i
|
|
||||||
:on-status (fn on-status [f val]
|
|
||||||
(if-not (= (fiber/status f) :dead)
|
|
||||||
(error val))
|
|
||||||
(set returnval val))
|
|
||||||
:source :eval-string})
|
|
||||||
returnval)
|
|
||||||
|
|
||||||
(defn eval
|
(defn eval
|
||||||
``Evaluates a form in the current environment. If more control over the
|
``Evaluates a form in the current environment. If more control over the
|
||||||
environment is needed, use `run-context`.``
|
environment is needed, use `run-context`.``
|
||||||
@ -2582,6 +2557,8 @@
|
|||||||
[str]
|
[str]
|
||||||
(let [p (parser/new)]
|
(let [p (parser/new)]
|
||||||
(parser/consume p str)
|
(parser/consume p str)
|
||||||
|
(if (= :error (parser/status p))
|
||||||
|
(error (parser/error p)))
|
||||||
(parser/eof p)
|
(parser/eof p)
|
||||||
(if (parser/has-more p)
|
(if (parser/has-more p)
|
||||||
(parser/produce p)
|
(parser/produce p)
|
||||||
@ -2596,6 +2573,8 @@
|
|||||||
(let [p (parser/new)
|
(let [p (parser/new)
|
||||||
ret @[]]
|
ret @[]]
|
||||||
(parser/consume p str)
|
(parser/consume p str)
|
||||||
|
(if (= :error (parser/status p))
|
||||||
|
(error (parser/error p)))
|
||||||
(parser/eof p)
|
(parser/eof p)
|
||||||
(while (parser/has-more p)
|
(while (parser/has-more p)
|
||||||
(array/push ret (parser/produce p)))
|
(array/push ret (parser/produce p)))
|
||||||
@ -2603,6 +2582,14 @@
|
|||||||
(error (parser/error p))
|
(error (parser/error p))
|
||||||
ret)))
|
ret)))
|
||||||
|
|
||||||
|
(defn eval-string
|
||||||
|
``Evaluates a string in the current environment. If more control over the
|
||||||
|
environment is needed, use `run-context`.``
|
||||||
|
[str]
|
||||||
|
(var ret nil)
|
||||||
|
(each x (parse-all str) (set ret (eval x)))
|
||||||
|
ret)
|
||||||
|
|
||||||
(def load-image-dict
|
(def load-image-dict
|
||||||
``A table used in combination with `unmarshal` to unmarshal byte sequences created
|
``A table used in combination with `unmarshal` to unmarshal byte sequences created
|
||||||
by `make-image`, such that `(load-image bytes)` is the same as `(unmarshal bytes load-image-dict)`.``
|
by `make-image`, such that `(load-image bytes)` is the same as `(unmarshal bytes load-image-dict)`.``
|
||||||
|
Loading…
Reference in New Issue
Block a user