]> git.eshelyaron.com Git - emacs.git/commitdiff
Handle ==, !=, + and -.
authorRichard M. Stallman <rms@gnu.org>
Mon, 19 Sep 1994 05:29:15 +0000 (05:29 +0000)
committerRichard M. Stallman <rms@gnu.org>
Mon, 19 Sep 1994 05:29:15 +0000 (05:29 +0000)
(hif-tokenize): Handle ==, !=, + and -.
(hif-token-regexp): Handle ==, !=, + and -.
(hif-notequal, hif-plus, hif-minus, hif-mathify): New functions.
(hif-math, hif-eq-expr): New functions.
(hif-term): Call hif-eq-expr.
(hide-ifdef-define): Pass 1 to hif-set-var.

lisp/progmodes/hideif.el

index 4cd538efcf8d346461f8c9187b10ecfc0d4e8bf5..a285b98e490b00131b4ec5c9b5de5154fb23a25d 100644 (file)
 ;;; I will continue to upgrade hide-ifdef-mode
 ;;; with your contributions.
 
-;;; Change Log:
-;;;
-;;; Revision 1.7  88/02/16  03:12:58  liberte
-;;; Fixed comments and doc strings.
-;;; Added optional prefix arg for ifdef motion commands.
-;;; 
-;;; Revision 1.6  88/02/05  00:36:18  liberte
-;;; Bug fixes.
-;;; 1. A multi-line comment that starts on an #ifdef line
-;;;    now ends on that line.
-;;; 2. Fix bad function name: hide-hif-ifdef-toggle-read-only
-;;; 3. Make ifdef-block hiding work outside of ifdefs.
-;;; 
-;;; Revision 1.5  88/01/31  23:19:31  liberte
-;;; Major clean up.
-;;;   Prefix internal names with "hif-".
-;;; 
-;;; Revision 1.4  88/01/30  14:09:38  liberte
-;;; Add hide-ifdef-hiding and hide-ifdef-mode to minor-mode-alist.
-;;; 
-;;; Revision 1.3  88/01/29  00:38:19  liberte
-;;; Fix three bugs.
-;;; 1. Function "defined" is just like lookup.
-;;; 2. Skip to newline or cr in case text is hidden.
-;;; 3. Use car of token list if just one symbol.
-;;;
-;;; Revision 1.2  88/01/28  23:32:46  liberte
-;;; Use hide-ifdef-mode-prefix-key.
-;;; Copy current-local-map so other buffers do not get
-;;; hide-ifdef-mode bindings.
-;;;
-
 ;;; Code:
 
 (defvar hide-ifdef-mode-submap nil
@@ -372,7 +340,8 @@ that form should be displayed.")
   )
 
 ; pattern to match initial identifier, !, &&, ||, (, or ).
-(defconst hif-token-regexp "^\\(!\\|&&\\|||\\|[()]\\|\\w+\\)")
+; Added ==, + and -: garyo@avs.com 8/9/94
+(defconst hif-token-regexp "^\\(!\\|&&\\|||\\|[!=]=\\|[()+-]\\|\\w+\\)")
 (defconst hif-end-of-comment "\\*/")
 
 
@@ -418,13 +387,16 @@ that form should be displayed.")
                       (cond
                        ((string-equal token "||") 'or)
                        ((string-equal token "&&") 'and)
+                       ((string-equal token "==") 'equal)
+                       ((string-equal token "!=") 'hif-notequal)
                        ((string-equal token "!")  'not)
                        ((string-equal token "defined") 'hif-defined)
                        ((string-equal token "(") 'lparen)
                        ((string-equal token ")") 'rparen)
+                       ((string-equal token "+") 'hif-plus)
+                       ((string-equal token "-") 'hif-minus)
                        (t (intern token)))
                       token-list))))
-
             (t (error "Bad #if expression: %s" expr-string)))))
       (set-syntax-table current-syntax-table))
     (nreverse token-list)))
@@ -432,6 +404,7 @@ that form should be displayed.")
 ;;;-----------------------------------------------------------------
 ;;; Translate C preprocessor #if expressions using recursive descent.
 ;;; This parser is limited to the operators &&, ||, !, and "defined".
+;;; Added ==, !=, +, and -.  Gary Oberbrunner, garyo@avs.com, 8/9/94
 
 (defun hif-parse-if-exp (token-list)
   "Parse the TOKEN-LIST.  Return translated list in prefix form."
@@ -458,13 +431,35 @@ that form should be displayed.")
 
 (defun hif-term ()
   "Parse a term of the form
-       term : factor | term '&&' factor."
-  (let ((result (hif-factor)))
+       term : eq-expr | term '&&' eq-expr."
+  (let ((result (hif-eq-expr)))
     (while (eq token 'and)
       (hif-nexttoken)
-      (setq result (list 'and result (hif-factor))))
+      (setq result (list 'and result (hif-eq-expr))))
     result))
 
+(defun hif-eq-expr ()
+  "Parse a term of the form
+       eq-expr : math | eq-expr '=='|'!=' math."
+  (let ((result (hif-math))
+       (eq-token nil))
+    (while (or (eq token 'equal) (eq token 'hif-notequal))
+      (setq eq-token token)
+      (hif-nexttoken)
+      (setq result (list eq-token result (hif-math))))
+    result))
+
+(defun hif-math ()
+  "Parse an expression of the form
+       math : factor | math '+|-' factor."
+  (let ((result (hif-factor))
+       (math-op nil))
+    (while (or (eq  token 'hif-plus) (eq token 'hif-minus))
+      (setq math-op token)
+      (hif-nexttoken)
+      (setq result (list math-op result (hif-factor))))
+  result))
+  
 (defun hif-factor ()
   "Parse a factor of the form
        factor : '!' factor | '(' expr ')' | 'defined(' id ')' | id."
@@ -505,6 +500,24 @@ that form should be displayed.")
        ))
     ))
 
+(defun hif-mathify (val)
+  "Treat VAL as a number: if it's t or nil, use 1 or 0."
+  (cond ((eq val t)
+        1)
+       ((null val)
+        0)
+       (t val)))
+
+(defun hif-plus (a b)
+  "Like ordinary plus but treat t and nil as 1 and 0."
+  (+ (hif-mathify a) (hif-mathify b)))
+(defun hif-minus (a b)
+  "Like ordinary minus but treat t and nil as 1 and 0."
+  (- (hif-mathify a) (hif-mathify b)))
+(defun hif-notequal (a b)
+  "Like (not (equal A B)) but as one symbol."
+  (not (equal a b)))
+
 ;;;----------- end of parser -----------------------
 
 
@@ -898,7 +911,7 @@ is first activated.")
 (defun hide-ifdef-define (var)
   "Define a VAR so that #ifdef VAR would be included."
   (interactive "SDefine what? ")
-  (hif-set-var var t)
+  (hif-set-var var 1)
   (if hide-ifdef-hiding (hide-ifdefs)))
 
 (defun hide-ifdef-undef (var)