1 ;;; wisent-calc.el --- Infix notation calculator
3 ;; Copyright (C) 2001, 2002, 2003, 2004 David Ponce
5 ;; Author: David Ponce <david@dponce.com>
6 ;; Maintainer: David Ponce <david@dponce.com>
7 ;; Created: 11 Sep 2001
9 ;; X-RCS: $Id: wisent-calc.el,v 1.1 2007-11-26 15:12:28 michaels Exp $
11 ;; This file is not part of GNU Emacs.
13 ;; This program is free software; you can redistribute it and/or
14 ;; modify it under the terms of the GNU General Public License as
15 ;; published by the Free Software Foundation; either version 2, or (at
16 ;; your option) any later version.
18 ;; This program is distributed in the hope that it will be useful, but
19 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 ;; General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with this program; see the file COPYING. If not, write to
25 ;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
26 ;; Boston, MA 02110-1301, USA.
30 ;; This is a port of the Bison 1.28d Infix Calc sample program to the
31 ;; elisp LALR parser Wisent. It illustrates usage of operator
32 ;; precedence and contextual precedence. The grammar is generated
33 ;; from the WY file wisent-calc.wy.
35 ;; To run the calculator use M-x wisent-calc and at "calc:" prompt
36 ;; enter expressions separated by semicolons. Here is a sample run of
39 ;; calc: 4 + 4.5 - (34.0/(8*3+-3));
40 ;; -> 6.880952380952381;
54 (require 'semantic-wisent)
55 (require 'wisent-calc-wy)
57 (define-lex-simple-regex-analyzer wisent-calc-lex-number
58 "Detect and create number tokens."
59 semantic-lex-number-expression 'NUM)
61 (define-lex-simple-regex-analyzer wisent-calc-lex-punctuation
62 "Detect and create punctuation tokens."
63 "\\(\\s.\\|\\s$\\|\\s'\\)" (char-after))
65 (define-lex wisent-calc-lexer
66 "Calc lexical analyzer."
67 semantic-lex-ignore-whitespace
68 semantic-lex-ignore-newline
69 wisent-calc-lex-number
70 wisent-calc-lex-punctuation
71 semantic-lex-default-action)
73 (defun wisent-calc (input)
74 "Infix desktop calculator.
75 Parse INPUT string and output the result of computation."
76 (interactive "scalc: ")
77 (or (string-match ";\\s-*$" input)
78 (setq input (concat input ";")))
80 (wisent-calc-setup-parser)
83 (let ((wisent-lex-istream (semantic-lex-buffer)))
84 (message "%s -> %s" input
85 (wisent-parse semantic--parse-table 'wisent-lex))
88 (provide 'wisent-calc)
90 ;;; wisent-calc.el ends here