1 ;; eval_string.lisp -- parse a string as an expression and evaluate it, or just parse it
3 ;; Copyright (C) 2005 Robert Dodier
5 ;; This program is free software; you can redistribute it and/or modify
6 ;; it under the terms of the GNU General Public License as published by
7 ;; the Free Software Foundation; either version 2 of the License, or
8 ;; (at your option) any later version.
10 ;; This program is distributed in the hope that it will be useful,
11 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 ;; GNU General Public License for more details.
15 ;; eval_string (s) -- parse the string s as a Maxima expression and evaluate it.
16 ;; It may or may not have a terminator (dollar sign `$' or semicolon `;').
17 ;; Only the first expression is parsed and evaluated, if there is more than one.
19 ;; eval_string ("foo: 42; bar: foo^2 + baz") => 42
20 ;; eval_string ("(foo: 42, bar: foo^2 + baz)") => baz + 1764
21 ;; Complain if s is not a string.
25 (defun $eval_string
(s)
28 (meval (parse-string s
)))
30 (merror "eval_string: ~M is not a string." s
))))
32 ;; parse_string (s) -- parse the string s as a Maxima expression (do not evaluate it).
33 ;; It may or may not have a terminator (dollar sign `$' or semicolon `;').
34 ;; Only the first expression is parsed, if there is more than one.
36 ;; parse_string ("foo: 42; bar: foo^2 + baz") => foo : 42
37 ;; parse_string ("(foo: 42, bar: foo^2 + baz)") => (foo : 42, bar : foo^2 + baz)
38 ;; Complain if s is not a string.
40 (defun $parse_string
(s)
45 (merror "parse_string: ~M is not a string." s
))))
47 ;; (PARSE-STRING S) -- parse the string as a Maxima expression.
48 ;; Do not evaluate the parsed expression.
50 (defun parse-string (s)
51 (declare (special *mread-prompt
* *parse-string-input-stream
*))
52 (setq *parse-string-input-stream
*
53 (make-string-input-stream (ensure-terminator s
)))
54 (let ((*mread-prompt
* ""))
55 (third (mread *parse-string-input-stream
*)) ))
57 ;; (ENSURE-TERMINATOR S) -- always concatenate a dollar sign `$' to the end of S.
58 ;; Always concatenating a terminator is not a problem for PARSE-STRING since it only
59 ;; parses the first expression in a string.
61 (defun ensure-terminator (s)
62 (concatenate 'string s
"$"))