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 - axtens

#1
The following NewLISP code shows me the file attributes of files under Win32. However, some of the filenames retrieved have Chinese characters in the name. When the GetFileAttributesA function encounters them, it gives me a -1 for the attribute. I looked at GetFileAttributesW but don't know how to make the contents of the fname available to the function in a form it recognises.



How does one handle this situation?


(define (get-archive-flag file-name)
    (if (not GetFileAttributesA)
        (begin
        (import "kernel32.DLL" "GetFileAttributesA")
        )
    )
    (setq fname file-name file-attrib (GetFileAttributesA (address fname)))  
    (append fname " " ( string file-attrib))    
)

; walks a disk directory and prints all path-file names
;
(define (show-tree dir)
    (if (directory dir)
        (dolist (nde (directory dir))
            (if (and (directory? (append dir "/" nde))
                (!= nde ".") (!= nde ".."))
                (show-tree (append dir "/" nde))
                (println (get-archive-flag (append dir "/" nde)))
            )
        )
    )
)

(show-tree "z:\working files\Cathy")
#2
G'day everyone



I'm writing a translator which will take a Lisp-ish way of writing a language and outputting it in a more XML-ish form.



This means taking this:
(sai (ITEFORLITLITLITLIT 999999999 1 1 (SAYOPRCAP (SAYVALFOR ...) "thing"))(sayvar goose))

and changing it into this:
<@ sai><@ ITEFORLITLITLITLIT>999999999|1|1|<@ SAYOPRCAP><@ SAYVALFOR>...</@>|thing</@></@><@ sayvar>goose</@></@>

My problem is that the code below almost works, except that it converts to this:
<@ sai><@ ITEFORLITLITLITLIT>999999999|1|1|<@ SAYOPRCAP><@ SAYVALFOR>...</@>|thing</@></@>|<@ sayvar>goose</@></@>

In case you missed it, the difference is the presence of the bar character just before the <@ sayvar>goose, which is clearly "the wrong thing"



Given the recursive nature of the 'proc' define, why am I always getting the bar appearing? The rule, BTW, is that SAI's arguments (and a few others) are never bar separated, but all others have arguments separated by bar.



Kind regards,

Bruce.

P.S. Windows version of newLisp. Development version. UTF-8 EXE.


(setq res "")
(define (proc procode)
(local (codelen code separator elider)
(begin
(setq codelen (length procode))
(for (i 0 (- codelen 1) 1)
(begin
(setq code (procode i))
(when (= i 0) (setq res (append res "<@ " (string code) ">" )))
;at this point, check to see whether OMT and SAI things are used
;if so, set the separator to empty string
(setq elider (find (upper-case (string code)) '("OMT" "SAO" "OSA" "SAI" "OSI" "SAW" "OSW")))
(if (nil? elider)
(setq separator "|")
(setq separator "")
)
(when (> i 0 )
(if
(list? code) (proc code)
(atom? code) (setq res (append res (string code) )  )
)
(if (< i (- codelen 1))
(begin
(setq res (append res separator ))
)
)
)
(when (= i (- codelen 1))
(setq res (append res "</@>" ))
)
)
)
)
)
)

(setq code "(sai (ITEFORLITLITLITLIT 999999999 1 1 (SAYOPRCAP (SAYVALFOR ...) "thing"))(sayvar goose))")
(setq parsed (read-expr code))(println $0)
(proc parsed)
(print res)
(exit)
#3
G'day everyone



W.r.t. complex numbers, how easy would it be to have

  (sqrt -1)

return

  (0.0 1.0)



and on the question of multiple answers, there are two correct answers to the square root of 4

  (sqrt 4)

-->

  (2 -2)



Could there be a way in future newLISPs to be able to do complexes and also some system setting that would return both answers from things like (sqrt)?



Kind regards,

Bruce.
#4
G'day everyone



With this code

;margs.lsp
(dolist (i (main-args)) (println $idx ": " i))
(exit)


If I evaluate it using the interpreter I get the following

>newlisp margs.lsp 1 2 3
0: newlisp
1: margs.lsp
2: 1
3: 2
4: 3


but if I link it using link.lsp I get the similar stuff but in different places

>emargs 1 2 3
0: emargs
1: 1
2: 2
3: 3


This is problematic, particularly as I tried to link the twitter.lsp code yesterday and had to tweak the code to make the main-args work properly. It would be better, I reckon, to be able to write into the script itself a means of detecting whether it's running interpreted or linked.



I suppose one could check for the "newlisp" in (main-args 0) but v10.3.4 could have a predicate (linked?), viz

(if (linked?) (println (uppercase (main-args 1))) (println (uppercase (main-args 2))))


Am I missing something? It happens all the time.



Kind regards,

Bruce.
#5
Whither newLISP? / need an enhanced (for)
April 12, 2009, 02:49:44 AM
G'day everyone



I'm not sure how I'd handle this, as I'm just thinking about it now, but how would one implement a (for) that had two indices rather than the usual one?



For example (pseudopseudocode):

  for i = 1 to 10 with j=20 to 2 step -2 do

    println i,j

  next



The usual thing is to have the for j inside the for i and to get about 100 results. I was thinking more of getting as many results as it takes for either i or j to terminate. That is, if i gets to 10 before j gets to 2 then terminate or vice versa.



I may come up with something soon, but if anyone wants to offer a solution, I'd be very interested.



Kind regards,

Bruce.
#6
G'day everyone



Pardon my naivete, but I'm having trouble with (join (args)) in that I'm expecting to get a string out of it, but it fails when items in the args are non-string.



For example
(define (tracer)
(begin
(set 'str (join (args)))
(append-file "trace.log" str)
(print str)
)
)

(tracer 1 0.2 "do" " " "not")


Out of that I get the following error message
ERR: string expected : 1
called from user defined function tracer


How do I make (tracer) sufficiently generic such that it can handle anything (or almost anything) that it receives in the args list?



Kind regards,

Bruce.
#7
G'day everyone



Here's something I just cooked up as part of a test frame. It's a way of evaluating VBScript code from newLISP.



(define (vbscript text)
(begin
(set 'aFile (open "c:/temp/temp.vbs" "write"))
(write-line aFile (string text))
(close aFile)
(exec "cmd.exe /C c:\windows\system32\cscript.exe c:\temp\temp.vbs >c:\temp\temp.out" )
(set 'aFile (open "c:/temp/temp.out" "read"))
(set 'result (string (read-line aFile)))
(close aFile)
result
)
)


And here's an example of its use:



(set 'foo "dim a
a = "hello world"
msgbox a
")

(vbscript foo)


Kind regards,

Bruce.
#8
newLISP and the O.S. / newLISP on Windows 7
April 08, 2009, 07:00:53 AM
Just in case anyone's curious, newLISP runs fine under Windows 7. I did have to install Java to run the IDE, but apart from that everything worked straight out of the box.



I'm getting closer to giving up on XP completely and going for W7, beta status notwithstanding. It's really is a very nice environment to work in, and I haven't met a lot of rough edges.



Kind regards,

Bruce.
#9
G'day everyone



(find) is like VBScript's Instr(). What is the equivalent for VBScript's RevInstr() wherein one searches for the first occurrence from the other end of the string? (find (reverse))?



Kind regards,

Bruce.
#10
newLISP in the real world / A better way to do this?
April 08, 2009, 12:36:06 AM
G'day everyone



I'm starting to like this language. But I'm still approaching it as one who has been 'polluted' by other languages.



Is there a better way to do this?


(dolist
  (i
    (parse
      (read-file "c:\temp\data") "rn"
    )
  )
(begin
(set 'j (reverse (parse i "t")))
(if (not (empty? j))
(begin
;(print j "n")
(set 'k (j 0))
(set 'l (j 1))
;(print (string (cons 'find (list k l 1))))
    (set 'm (eval-string (string (cons 'find (list k l 1)))))
(when (true? m)
;(print m "n")
(print "afti(" l ", " k ")=" (slice l m) "n" )
)
)
)
)
)


Kind regards,

Bruce.
#11
newLISP in the real world / NEWBIE: bug in (count)?
April 08, 2009, 12:02:43 AM
I've often thought it would be good to learn Lisp and so I'm trying newLISP (despite what some purists might argue).



I have problem already. In the following code I tests whether 'j' is a list, and if so I ask for a count of it and print the result. Every time I run it, it fails with a "ERR: list expected in function count : nil".



c:tempdata is a text file containing crlf-delimited lines of two tab-delimited items.



Here's the code


(dolist
  (i
    (parse
      (read-file "c:\temp\data") "rn"
    )
  )
(begin
(set 'j (reverse (parse i "t")))
(print j "n")
(when (list? j)
(print (count j) "n")
)
(set 'k (j 0))
(set 'l (j 1))
)
)


What is my non-Lisp mind not seeing?



Kind regards,

Bruce.



P.S. Is there a way of assigning (j 0) to k and (j 1) to l in one statement, i.e. does newLISP permit double (or more) assignment?