Re: How to eval one "step" of a form?
Thank you, that's very helpful. I'm wondering if you know of a ClojureScript debugging library (something on the level of xlib for php) that I can use to watch the evaluation of forms programmatically with breakpoints. I'm trying to develop a tool that runs a Clojure program by stepping through evaluation for each form. On Thursday, July 20, 2017 at 5:38:47 PM UTC-6, tbc++ wrote: > > The answer lies in the term "REPL". You start by reading the string, you > can use `clojure.core/read-string` for this case. This will convert your > string into Clojure data: > > "(+ 1 (* 2 2))" => (+ 1 (* 2 2)) > > That's the "R" part of "REPL", read. > > Next step is "E" for "eval". We need to evaluate the structure. In a very > simple sort of way you can write eval thusly: > > (defn simple-eval [form] > (cond > (integer? form) form > > (symbol? form) (condp = form > '+ + > '* *) > > (seq? form) (let [[f & args] (map simple-eval form) > (apply f args > > As you see this algorithm is recursive. And it's also a (very) simple lisp > interpreter. If we get an integer we return it. If we get a symbol we > return the matching function. If we get a seq, we recursively eval all the > contents of the seq then call the first resolved item passing it the args. > You could easily insert println expressions in the above code to see how it > step-by-step evals the code from left-to-right and from > deepest-to-shallowest expression. > > This is known as an interpreter as it interprets the code more or less at > runtime. Now this sort of interpreter isn't exactly fast (although it's > very simple and easy to understand). So often language designers will > compile languages by preprocessing and transforming the input code into a > different language. C compiles to assembly. ClojureScript compiles to > JavaScript. And Clojure compiles to JVM byte code. The point of these > compilation steps is to remove the overhead of this interpreter. > > The P in REPL stands for "Print". After we eval a form we print the result. > > And then we "L"oop back to the start. > > Hopefully this helps a bit. > > Timothy > > > On Thu, Jul 20, 2017 at 4:39 PM, Lincoln Bergeson> wrote: > >> Here's my how-to question for someone with in-depth knowledge of the >> clojure/clojurescript compiler: >> >> If I have a string representing a valid Clojure form, say for example, >> "(+ 1 (* 2 (/ 6 3)))", how would I "reduce" this form to the form with one >> s-expr evaluated? That is, how would I obtain "(+ 1 (* 2 2))"? And then >> from there, how would I obtain "(+ 1 4)", and then "5"? >> >> I read through David Nolen's brief intro to the cljs compiler: >> https://github.com/swannodette/hello-cljsc and suspect the answer lies >> in ASTs. I'm just not sure where to start. >> >> Forgive me if I should be posting this somewhere else, just not sure >> where else I can find an answer. >> >> -- >> You received this message because you are subscribed to the Google >> Groups "Clojure" group. >> To post to this group, send email to clo...@googlegroups.com >> >> Note that posts from new members are moderated - please be patient with >> your first post. >> To unsubscribe from this group, send email to >> clojure+u...@googlegroups.com >> For more options, visit this group at >> http://groups.google.com/group/clojure?hl=en >> --- >> You received this message because you are subscribed to the Google Groups >> "Clojure" group. >> To unsubscribe from this group and stop receiving emails from it, send an >> email to clojure+u...@googlegroups.com . >> For more options, visit https://groups.google.com/d/optout. >> > > > > -- > “One of the main causes of the fall of the Roman Empire was that–lacking > zero–they had no way to indicate successful termination of their C > programs.” > (Robert Firth) > -- You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@googlegroups.com Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en --- You received this message because you are subscribed to the Google Groups "Clojure" group. To unsubscribe from this group and stop receiving emails from it, send an email to clojure+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.
Re: How to eval one "step" of a form?
The answer lies in the term "REPL". You start by reading the string, you can use `clojure.core/read-string` for this case. This will convert your string into Clojure data: "(+ 1 (* 2 2))" => (+ 1 (* 2 2)) That's the "R" part of "REPL", read. Next step is "E" for "eval". We need to evaluate the structure. In a very simple sort of way you can write eval thusly: (defn simple-eval [form] (cond (integer? form) form (symbol? form) (condp = form '+ + '* *) (seq? form) (let [[f & args] (map simple-eval form) (apply f args As you see this algorithm is recursive. And it's also a (very) simple lisp interpreter. If we get an integer we return it. If we get a symbol we return the matching function. If we get a seq, we recursively eval all the contents of the seq then call the first resolved item passing it the args. You could easily insert println expressions in the above code to see how it step-by-step evals the code from left-to-right and from deepest-to-shallowest expression. This is known as an interpreter as it interprets the code more or less at runtime. Now this sort of interpreter isn't exactly fast (although it's very simple and easy to understand). So often language designers will compile languages by preprocessing and transforming the input code into a different language. C compiles to assembly. ClojureScript compiles to JavaScript. And Clojure compiles to JVM byte code. The point of these compilation steps is to remove the overhead of this interpreter. The P in REPL stands for "Print". After we eval a form we print the result. And then we "L"oop back to the start. Hopefully this helps a bit. Timothy On Thu, Jul 20, 2017 at 4:39 PM, Lincoln Bergesonwrote: > Here's my how-to question for someone with in-depth knowledge of the > clojure/clojurescript compiler: > > If I have a string representing a valid Clojure form, say for example, "(+ > 1 (* 2 (/ 6 3)))", how would I "reduce" this form to the form with one > s-expr evaluated? That is, how would I obtain "(+ 1 (* 2 2))"? And then > from there, how would I obtain "(+ 1 4)", and then "5"? > > I read through David Nolen's brief intro to the cljs compiler: > https://github.com/swannodette/hello-cljsc and suspect the answer lies > in ASTs. I'm just not sure where to start. > > Forgive me if I should be posting this somewhere else, just not sure where > else I can find an answer. > > -- > You received this message because you are subscribed to the Google > Groups "Clojure" group. > To post to this group, send email to clojure@googlegroups.com > Note that posts from new members are moderated - please be patient with > your first post. > To unsubscribe from this group, send email to > clojure+unsubscr...@googlegroups.com > For more options, visit this group at > http://groups.google.com/group/clojure?hl=en > --- > You received this message because you are subscribed to the Google Groups > "Clojure" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to clojure+unsubscr...@googlegroups.com. > For more options, visit https://groups.google.com/d/optout. > -- “One of the main causes of the fall of the Roman Empire was that–lacking zero–they had no way to indicate successful termination of their C programs.” (Robert Firth) -- You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@googlegroups.com Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en --- You received this message because you are subscribed to the Google Groups "Clojure" group. To unsubscribe from this group and stop receiving emails from it, send an email to clojure+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.
How to eval one "step" of a form?
Here's my how-to question for someone with in-depth knowledge of the clojure/clojurescript compiler: If I have a string representing a valid Clojure form, say for example, "(+ 1 (* 2 (/ 6 3)))", how would I "reduce" this form to the form with one s-expr evaluated? That is, how would I obtain "(+ 1 (* 2 2))"? And then from there, how would I obtain "(+ 1 4)", and then "5"? I read through David Nolen's brief intro to the cljs compiler: https://github.com/swannodette/hello-cljsc and suspect the answer lies in ASTs. I'm just not sure where to start. Forgive me if I should be posting this somewhere else, just not sure where else I can find an answer. -- You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@googlegroups.com Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en --- You received this message because you are subscribed to the Google Groups "Clojure" group. To unsubscribe from this group and stop receiving emails from it, send an email to clojure+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.