#533 macsyma-read-string /FIX

closed
nobody
None
5
2004-03-08
2004-03-05
No

Macsyma-read-string currently doesn't work in GCL 5.9.0
(problem with vector-push-extend) and has no error
handling. Here's a fixed version with good error handling.

Example:

(meval (macsyma-read-string "diff(x^2,x)"))
=> ((MTIMES SIMP) 2 |$x|)

;;; Copyright 2004 by Stavros Macrakis
;;; Licensed under your choice of the GPL or the
modified BSD license

;;; Parse a string as a Maxima expression
;;; Return the parsed form
;;; If there's an error, it returns the symbol parse-error;
the ;;; second return value is then the error string.

;;; Usage examples:
;;;
;;; (macsyma-read-string "1/+3*x") =>
;;; ((MQUOTIENT) 1 ((MPLUS) ((MTIMES) 3 |$x|)))
;;; NIL
;;;
;;; Note that this is simply parsed -- no simplification.
;;; The simplified form is
;;; ((MTIMES SIMP) ((RAT SIMP) 1 3) ((MEXPT SIMP)
|$x| -1))
;;;
;;; (macsyma-read-string "1.2.3") =>
;;; NIL
;;; "Incorrect syntax: 0.29999999999999999 is not an
infix operator"

(defun macsyma-read-string (str)
(with-input-from-string
(stream (concatenate 'string str "$"))
(let* ((*standard-input* stream)
(*standard-output* (make-string-output-
stream))
;; catches error strings
(expr
(catch 'macsyma-quit
(list (caddr (mread stream)))))
(error-string (get-output-stream-string
*standard-output*)))
(values
(if (atom expr) 'parse-error (car expr))
(cond ((not (equal error-string "")) error-string)
((tyi stream) "Excess characters in input string")
((not (listp expr)) "Unknown read error")
(t nil))))))

_______________________________________________
Maxima mailing list
Maxima@www.math.utexas.edu
http://www.math.utexas.edu/mailman/listinfo/maxima

Discussion

  • Stavros Macrakis

    Logged In: YES
    user_id=588346

    Existing internal uses of macsyma-fill-string pass it a
    string with a fill-pointer, and it does work in that case.
    They also expect errors not to be caught. So this is not a bug.

    The above code might be useful in other cases.

    Thanks to Wolfgang Jenkner for pointing out the fill-pointer
    issue.

     
  • Stavros Macrakis

    • status: open --> closed
     

Log in to post a comment.

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:





No, thanks