Main Menu

READ

Started by plugge, June 23, 2005, 12:39:52 PM

Previous topic - Next topic

plugge

Hi, I'm new to newLisp, but not to lisp.

In the lisp I used most often muLISP (a looong time ago) there was a READ function, to read a normal LISP expression.

Why is that not present in newLISP? Does anyone have ready made READ function?



TIA

-Leo-

eddier

#1
If you are reading from a file:

(eval-string (read-file "filename"))

If you just want to evaluate something from the console

(eval-string (read-line))

Hope this helps.



Eddie

newdep

#2
Welcome :-) The more Dutch the better ;-)

Enjoy newlisp.. !



Norman.
-- (define? (Cornflakes))

HPW

#3
I used this read-emulation:

(define (read readstr   readret)
(cond
((float readstr)
(if (find "." readstr)
(setq readret (float readstr))
(setq readret (integer readstr))
)
)
((=(slice readstr 0 1)"(") ;)
(setq readret(eval-string(append "'" readstr)))
)
(true
(setq readret (symbol readstr))
)
)
)
Hans-Peter

plugge

#4
Quote from: "eddier"If you are reading from a file:

(eval-string (read-file "filename"))

If you just want to evaluate something from the console

(eval-string (read-line))

Hope this helps.



Eddie


No, this is not what READ does. READ reads one atom or a list at a time, but does not evaluate. Quoted text is read in as strings, number as numers etc.

and there is no cr-lf.

plugge

#5
Thanx, but this is also not what I'm looking for.

It should not evaluate and read an atom or a list at the time.


Quote from: "HPW"I used this read-emulation:

(define (read readstr   readret)
(cond
((float readstr)
(if (find "." readstr)
(setq readret (float readstr))
(setq readret (integer readstr))
)
)
((=(slice readstr 0 1)"(") ;)
(setq readret(eval-string(append "'" readstr)))
)
(true
(setq readret (symbol readstr))
)
)
)

HPW

#6
Maybe it is not like muLISP-read, but I use it in code ported from autolisp.



So it should be possible to code the wanted behaviour.



You can list all test-cases of input and wanted output and then it should be possible.
Hans-Peter

plugge

#7
Quote from: "HPW"Maybe it is not like muLISP-read, but I use it in code ported from autolisp.



So it should be possible to code the wanted behaviour.



You can list all test-cases of input and wanted output and then it should be possible.


Yes, I could. Thanx for helping.

I'm a little suprised that such a basic function is not available. Lisp is a read-eval loop. There's a read-char, read-line,  read-key etc, but not the basic READ.

plugge

#8
Hi, here's my version of READ:



It is used to read a text file one atom or list at a time.

I'm out of practise since I haven't programmed in Lisp since 1993, and I'm new to newLisp

If someone likes to make an elegant version, be my guest!


(define (read fHandle, i result)
(setq result "")
(do-while (not (= i nil))
 (setq i (read-char fHandle))
 (cond ((= i nil) (setq result "EOF"))
((not (member i '(32 13 10 40))) ;legal character
;; not a list
                     (setq result (append (char i) (read-atom fHandle)))
                     (setq i nil))
                    ((= i 40)
;; opening parethesis - read in the list
                     (setq result (append (char i) (read-list fHandle)))
(setq i nil))
 )
)
   (if (= result "EOF") nil (sym result))
)

(define (read-atom fHandle, x y)
(setq y "")
(do-while (not (= x nil))
 (setq x (read-char fHandle))
     (cond ((not (member x '(32 13 10 40)))
                     (setq y (append y (char x))))
                    ((member x '(32 13 10 40)) (setq x nil))
              )
    )
y)

(define (read-list fHandle, x y flag)
(setq flag 1)
(setq y "")
(do-while (not (= x nil))
 (setq x (read-char fHandle))
 (if (= x 40) (inc 'flag 1))
 (if (= x 41) (dec 'flag 1))
     (cond ((and (= x 41) (= flag 0))
                     (setq y (append y (char x)))
    (setq x nil))
              ((not (member x '(13 10)))
                     (setq y (append y (char x))))
              )
    )
y)

HPW

#9
I do not get the point what you want to reach.

Can you give an example file which you want to read

and a explanation what you want to have in memory after read.
Hans-Peter

plugge

#10
Simple:

(from xLisp)

read an expression

(read [<stream> [<eofp> [<eof> [<rflag>]]]])

<stream>   the input stream (default, or NIL, is *standard-input*, T is *terminal-io*)

<eofp>   When T, signal an error on end of file, when NIL return <eof> (default is T)

<eof>   the value to return on end of file (default is NIL)

<rflag>   recursive read flag. The value is ignored

returns   the expression read



Actually the basic READ doesn't need any parameter, it uses the *standard-input*



READ is for getting a syntactically valid S-expresseion. Only that value is returned. The S-expression is not evaluated.



Its practical for reading in S-expression from a file, without evaluation.



HTH

-Leo-

newdep

#11
Isnt 'load doing that?
-- (define? (Cornflakes))

plugge

#12
BTW



An S-expression in a file may run over a line

read-line only reads until a cr-lf



Do you guys think READ is obsolete?? How do you then perform this type of READing?



-Leo-

plugge

#13
Quote from: "newdep"Isnt 'load doing that?


Nope.



Quote from the newLisp manual:
QuoteLoads and translates newLISP from a source file specified in one or more str-file-name and evaluates the expressions contained in the file(s).


It should not evaluate and READ doesn't



Besides: I want a controlled one by one READ of S-expressions, for example to operate on values sequentially.



-Leo-

HPW

#14
I think I get the point now.

Maybe Lutz can tell his point of view or made that function.


Quote
Do you guys think READ is obsolete?? How do you then perform this type of READing?


My S-expressions are bound to symbols.

I store them with:



(save "stuff.lsp" 'aContext 'myFunc 'otherVar 'Acontext)



Then I easily load them with:



(load "stuff.lsp")



Also source and eval-string can be used:

> (set 'a '(+ 1 2 3))
(+ 1 2 3)
> (source 'a)
"(set 'a '(+ 1 2 3))rnrn"
> (eval-string(source 'a))
(+ 1 2 3)
>
Hans-Peter