Menu

Show posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.

Show posts Menu

Topics - bairui

#1
Hi, newlispers,



I wrote a PEG (Parsing Expression Grammar) parser for Vim (https://github.com/dahu/Vimpeg">https://github.com/dahu/Vimpeg) a while ago and recently ported it to newLISP (https://github.com/dahu/nlpeg">https://github.com/dahu/nlpeg). I would love for you guys to review my work. I am new to lisp and expect that I have made some boo-boos along the way.



NOTE: the nlpeg-peg-parser.lsp is UNFINISHED, however the nlpeg.lsp is ready for review. Examples of using nlpeg.lsp can be seen in the test files and calc.lsp.



When nlpeg-peg-parser.lsp is finished, grammars can be specified using standard PEG grammar format in [text] strings, if preferred over the explicit API approach available through nlpeg.lsp currently.
#2
@TedWalther



Thanks for the getopts.lsp module, Ted. Here is a macro for combining the definition of short and long options into a single call. The aim here is to DRY up option specification. My newLISP-fu in general and macro-fu in particular is not strong, so please elucidate any boo-boos.


;; @syntax (opt <short-opts> <long-opt> <action> <arg?> <desc>)
;; @param <short-opts> The single letter option(s)
;; @param <long-opt> The long option
;; @param <action> The code to execute when the option is found
;; @param <arg?> 'nil' if the option doesn't take an argument, otherwise a string that describes the type of argument the option takes.
;; @param <desc> A string that describes the option.  This is used by the 'usage' function.
;; @example
;; (opt "?h" "help" (getopts:usage) nil "Print this help message")
;; ...
;; $ ./myscript.lsp -h
;; $ ./myscript.lsp -?
;; $ ./myscript.lsp --help
(define-macro (opt short-opts long-opt action arg? desc)
              (dolist (opt (explode short-opts))
                (eval (list 'shortopt opt action arg? desc)))
              (eval (list 'longopt long-opt action arg? desc)))


Also... I'm looking forward to seeing hartrock's contribution to bundle the options together in the getopts:usage output being added to the module.



Cheers,

Barry.
#3
newLISP in the real world / external filter
January 29, 2013, 08:19:25 PM
What is a more idiomatic way to do this? :


(set 'tmp-file (open "/tmp/file" "write"))
(write tmp-file (join (map markup (parse pretext "n" 0)) "n"))
(close tmp-file)
(setf posttext (join (exec "fmt -68 /tmp/file") "n"))


It feels dirty to use a /tmp/file like that. Ideally I'd like to pipe a string through `/bin/fmt` capturing its STDOUT, all within newlisp.
#4
Whither newLISP? / magic numbers
January 20, 2013, 03:18:58 PM

(error-event (fn ()
    (if (= 4 ((last-error) 0))
        (println "ERR:" (last (last-error 4)))
        (println ((last-error) 1)))))
[/quote]

For a while now I have been unconsciously squirming in my seat when reading over code like this. This morning I became aware of that discomfort and cried in alarm: magic numbers!



To the trained webber this might read transparently lucid; succinct even. But to the novice seeing this for the first time or the junior revisiting six month (week? day?!) old code, these opaque little numbers muddy the cognitive stream, making it nearly impossible to reason about the code until the manual has been consulted.



, where ((last-error) 0) and ((last-error) 1) retrieve the number-only and string-only, respectively.



Ok. That's not too bad. Two things to learn there and you're set for life:

1. newLISP's awesome implicit list indexing;

2. (last-error) returns such an ordered pair



fine, but what about those 4's...?


(if (= 4 ((last-error) 0))

Is now approachable: If the last error number is 4...

But, what is error 4?

Another quick test:


(last-error 4)

yielded:


(4 "problem accessing file")

ah... light-bulb



While the solution to the mystical zero and one numbers was trivial, the same approach will not work here -- it is not plausible to memorise the table of errors.



Error Codes[/url] handy;



2. Have constants representing error numbers:


(last-error ERR_FILE_ACCESS)
or the more cryptic:
(last-error EFACC)

Note: I am not literally suggesting either actual constant here -- I presume actual thought would be given to such a scheme.



[size=120]tl;dr[/size]



What is the recommended way to reduce magic numbers in our newLISP code?
#5
I have a deeply nested tree of lists (which came from json (the   i3-tag -t get_tree   command from the i3 window manager, specifically)).



What is the best way in newLISP to drill down into various parts of this structure?



I have so far been using code like:


((assoc "nodes" (((assoc "nodes" ((get_tree) (0 -1 (ref '("name" "VGA1") (get_tree) match)))) 1) 1)) 1))

but that feels horrible. I must be doing something very wrong. I am hoping for something beautiful and simple like XPath.
#6
newLISP Graphics & Sound / seesaw for newlisp?
October 08, 2012, 06:15:45 PM
Has anyone played with the http://darevay.com/talks/clojurewest2012/#/title-slide">seesaw swing library for clojure? I have only dabbled so far, but it looks pretty sexy. How does it stack up against newlisp's guiserver? Would a port of seesaw to newlisp be:



1. possible

2. worthwhile

3. desirable



?
#7
newLISP in the real world / Request for critique
September 30, 2012, 09:36:57 PM
Hi, guys,



I'm very new to lisp (in general, and newLISP in particular). I'm also fairly... GUI challenged - I spend all my time in command-line or text-only environments. Most of my coding experience has been limited to this domain.



As such... I suck at building GUIs and would LOVE for some constructive criticism on a little app I built:

https://github.com/dahu/Marathon-Wow">https://github.com/dahu/Marathon-Wow



Some questions I have:



1. Is the   (set-locale "C")   necessary? What effect does that have?

2. I don't really like the way my contexts at the bottom (WordEngine, Score and Timer) "touch" the GUI directly. Perhaps this is unavoidable. Perhaps this is the right way to do it. Perhaps I've offended the GUI gods. Either way, I'd like to know.



I guess I should have split the contexts out into separate files, but with their interdependence on the GUI, I felt they should reside in the same file (at least for now, until someone can assuage that fear.)



I have tested this on Windows (which sadly happens to be my target platform, despite me living almost purely in linux land), and it *seems* to be coping so far.



Any and all feedback welcome.



Thanks,

Barry
#8
I dev on a linux box and only get to test on a windows box periodically. I have noticed failures on the windows machine that are solved by simply ensuring that the newlisp script is moved to a directory that doesn't contain chinese characters in the path (like, C:foo). I saw an error at one stage along the lines of "read-file expected a string" if that helps in setting up a test.
#9

(setq file (open {foo} {read}))
(println (seek file))
(read-line file)
(println (seek file))


The first seek prints 0 as expected, however the second one prints the EOF byte position. I couldn't find this behaviour documented in the read-line section of the manual. So... is this a bug in newLISP or me?



If this is normal expected behaviour, how do you suggest I:
[list=1]
  • [*]search to a place within a file (search file {something})

  • [*]skip a few lines (read-line file)(read-line file) <-- currently fails here

  • [*]read a chunk of text (read file buf 1000)
  • [/list]


    Thanks in advance. :-)