Arc Forumnew | comments | leaders | submit | mpr's commentslogin
3 points by mpr 3286 days ago | link | parent | on: Execute code in the scope of a hash table

Hey, could you expand on what we might do to hack local macros into arc? I'm really keen on writing macrolet

-----

2 points by rocketnia 3286 days ago | link

It would take a bit of refactoring: Changing the `env` list to a table, adding it as a parameter of `ac-macro?` (which should now look things up from that table first), and finally adding an `env` parameter to Arc's `eval`.

-----

3 points by mpr 3287 days ago | link | parent | on: IDE?

What's the benefit of running vim-slime as opposed to just an arc repl from the command line? I've been doing that w/ the same vim/tmux setup you mentioned. I enjoy it.

Edit: I see that you can execute code from the vim window. That is a nice feature. Anything else significant?

-----

3 points by akkartik 3286 days ago | link

No, that's pretty much it. I sometimes find it useful, but yes, mostly I forget it exists. Occasionally it'll bother me when I hit ctrl-c for unrelated reasons :)

-----

2 points by mpr 3288 days ago | link | parent | on: Execute code in the scope of a hash table

I read the link you suggested in my previous post about defvar, and I see that it can be used to set dynamic behavior when a variable is referenced (??). Is this the correct interpretation, and what might be some uses of that?

-----

3 points by akkartik 3288 days ago | link

Yeah it lets you decide what to do when getting or setting a variable. The original link has an example at the bottom, but here's another one kinda related to what you seem to be trying to do:

  arc> (= h (obj a 1 b 2))
  #hash((a . 1) (b . 2))
  arc> (defvar a
               (fn args
                 (if args
                   ; write
                   (= h!a car.args)
                   ; read
                   h!a)))
  arc> a
  1
  arc> (= a 3)
  3
  arc> a
  3
  arc> h
  #hash((a . 3) (b . 2))

-----

2 points by mpr 3288 days ago | link

Oh I see now. That is pretty cool

-----

4 points by mpr 3288 days ago | link | parent | on: Execute code in the scope of a hash table

Wow great post, thanks for the reply. I didn't know eval was always executed at global scope, and I was not thinking about the ht variable being executed multiple times at different scope, so thanks for pointing that out. The rest of your explanation about how to get it working is very clear and I will be a better arc hacker for it!

The occasion for this macro is an object system built around closures and hash tables. I will post more about it when I've made more progress.

-----

2 points by mpr 3289 days ago | link | parent | on: ASK: How to read user input?

I was using defvar where I should have been using =. But the idea in my post was just to store the result of (prompt) in the variable x, which is meant to be a string.

And yes, now that I am moving past the experimental phase of my script, I will be running things in batch mode.

Thanks for the advice.

-----

1 point by mpr 3289 days ago | link | parent | on: ASK: How to read user input?

Yes, but I'm using the msg arg as the prompt text. Example:

    arc> (= x (prompt "> "))
    > this is the user text
    arc> x
    "this is the user text"
I ended up hacking the ac.scm file to throw away the first newline after an expr is (read). It works for now.

-----

2 points by kinnard 3289 days ago | link

I think I understand. You want a function that prints arbitrary user prompts and then takes in user inputs?

You should share your hack!

-----

3 points by mpr 3289 days ago | link

Yep, thats the idea. Anyway, here is my hack, in all its hackish glory:

    (define (trash-line c)
      (if (equal? c #\newline)
        '()
        (trash-line (read-char))))

    (define (tl2 interactive?)
      (when interactive? (display "arc> "))
      (on-err (lambda (c)
                (set! last-condition* c)
                (parameterize ((current-output-port (current-error-port)))
                  ((error-display-handler) (exn-message c) c)
                  (newline))
                (tl2 interactive?))
        (lambda ()
          (let ((expr (read)))

            ;; HACK located here
            (trash-line (read-char)) ; throw away until we hit the newline


            (if (eof-object? expr)
                 (begin (when interactive? (newline))
                        (exit)))
            (if (eqv? expr ':a)
                'done
                (let ((val (arc-eval expr)))
                  (when interactive?
                    (write (ac-denil val))
                    (newline))
                  (parameterize ((current-namespace (main-namespace)))
                    (namespace-set-variable-value! '_that val)
                    (namespace-set-variable-value! '_thatexpr expr))
                  (tl2 interactive?)))))))
So I call the trash-line function after the expr is read, but before it is eval'd by arc, so that there is not leading #\newline in the input buffer.

This does seem to work for the readline'ing I was doing yesterday. Probably doesn't handle all cases.

As akkartik mentioned above, this hack is obviated by running scripts in batch mode.

-mpr

Edit: this change is in my ac.scm file around line 1250

-----

2 points by akkartik 3289 days ago | link

I like it! I don't think it'll break anything; can you send a pull request? Then we'll be able to run such code reliably at the repl! That would be sweet.

Edit 38 minutes later: hmm, there's one issue. Right now you can type multiple expressions in on a single line, but this change would drop everything after the end of the first expression. A better approach would be to drop only whitespace and stop at the very first non-whitespace character.

-----

2 points by mpr 3289 days ago | link

Thanks! Yeah, I am aware of that bug, and was kind of ignoring it ;) I'll implement your suggested fix then send a pull request.

-----

3 points by mpr 3289 days ago | link

Pull request submitted

-----

2 points by mpr 3290 days ago | link | parent | on: ASK: Arc Language Slack?

Yes, count my vote in favor of an arclang slack

-----