Initial Commit
[packages] / xemacs-packages / semantic / wisent / wisent-calc.el
1 ;;; wisent-calc.el --- Infix notation calculator
2
3 ;; Copyright (C) 2001, 2002, 2003, 2004 David Ponce
4
5 ;; Author: David Ponce <david@dponce.com>
6 ;; Maintainer: David Ponce <david@dponce.com>
7 ;; Created: 11 Sep 2001
8 ;; Keywords: syntax
9 ;; X-RCS: $Id: wisent-calc.el,v 1.1 2007-11-26 15:12:28 michaels Exp $
10
11 ;; This file is not part of GNU Emacs.
12
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.
17
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.
22
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.
27
28 ;;; Commentary:
29 ;;
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.
34 ;;
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
37 ;; `wisent-calc':
38 ;;
39 ;;   calc: 4 + 4.5 - (34.0/(8*3+-3));
40 ;;   -> 6.880952380952381;
41 ;;   calc: -56 + 2;
42 ;;   -> -54;
43 ;;   calc: 3 ^ 2;
44 ;;   -> 9;
45 ;;   calc: 2*2*2 = 2^3;
46 ;;   -> t;
47 ;;   calc: 2*2*2; 2^3;
48 ;;   -> 8; 8;
49
50 ;;; History:
51 ;; 
52
53 ;;; Code:
54 (require 'semantic-wisent)
55 (require 'wisent-calc-wy)
56
57 (define-lex-simple-regex-analyzer wisent-calc-lex-number
58   "Detect and create number tokens."
59   semantic-lex-number-expression 'NUM)
60
61 (define-lex-simple-regex-analyzer wisent-calc-lex-punctuation
62   "Detect and create punctuation tokens."
63   "\\(\\s.\\|\\s$\\|\\s'\\)" (char-after))
64
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)
72
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 ";")))
79   (with-temp-buffer
80     (wisent-calc-setup-parser)
81     (semantic-lex-init)
82     (insert input)
83     (let ((wisent-lex-istream (semantic-lex-buffer)))
84       (message "%s -> %s" input
85                (wisent-parse semantic--parse-table 'wisent-lex))
86       )))
87
88 (provide 'wisent-calc)
89
90 ;;; wisent-calc.el ends here