| 1 |
;;; |
| 2 |
;;;; Simple calculator in Scheme |
| 3 |
;;; |
| 4 |
;; |
| 5 |
;; @created "Tue Jan 6 12:47:23 2004" |
| 6 |
;; @modified "Mon Oct 25 11:07:24 2004" |
| 7 |
;; @author "Dominique Boucher" |
| 8 |
;; @copyright "Dominique Boucher" |
| 9 |
;; |
| 10 |
;; Simple arithmetic calculator. |
| 11 |
;; |
| 12 |
;; This program illustrates the use of the lalr-scm parser generator |
| 13 |
;; for Scheme. It is NOT robust, since calling a function with |
| 14 |
;; the wrong number of arguments may generate an error that will |
| 15 |
;; cause the calculator to crash. |
| 16 |
|
| 17 |
|
| 18 |
;;; |
| 19 |
;;;; The LALR(1) parser |
| 20 |
;;; |
| 21 |
|
| 22 |
|
| 23 |
(import (rnrs) (rnrs mutable-pairs) (lalr)) |
| 24 |
|
| 25 |
(define calc-parser |
| 26 |
(lalr-parser |
| 27 |
|
| 28 |
;; --- Options |
| 29 |
;; output a parser, called calc-parser, in a separate file - calc.yy.scm, |
| 30 |
;(output: calc-parser "calc.yy.scm") |
| 31 |
;; output the LALR table to calc.out |
| 32 |
;(out-table: "calc.out") |
| 33 |
;; there should be no conflict |
| 34 |
(expect: 0) |
| 35 |
|
| 36 |
;; --- token definitions |
| 37 |
(ID NUM = LPAREN RPAREN NEWLINE COMMA |
| 38 |
(left: + -) |
| 39 |
(left: * /) |
| 40 |
(nonassoc: uminus)) |
| 41 |
|
| 42 |
(lines (lines line) : (display-result $2) |
| 43 |
(line) : (display-result $1)) |
| 44 |
|
| 45 |
|
| 46 |
;; --- rules |
| 47 |
(line (assign NEWLINE) : $1 |
| 48 |
(expr NEWLINE) : $1 |
| 49 |
(error NEWLINE) : #f) |
| 50 |
|
| 51 |
(assign (ID = expr) : (add-binding $1 $3)) |
| 52 |
|
| 53 |
(expr (expr + expr) : (+ $1 $3) |
| 54 |
(expr - expr) : (- $1 $3) |
| 55 |
(expr * expr) : (* $1 $3) |
| 56 |
(expr / expr) : (/ $1 $3) |
| 57 |
(- expr (prec: uminus)) : (- $2) |
| 58 |
(ID) : (get-binding $1) |
| 59 |
(ID LPAREN args RPAREN) : (invoke-proc $1 $3) |
| 60 |
(NUM) : $1 |
| 61 |
(LPAREN expr RPAREN) : $2) |
| 62 |
|
| 63 |
(args () : '() |
| 64 |
(expr arg-rest) : (cons $1 $2)) |
| 65 |
|
| 66 |
(arg-rest (COMMA expr arg-rest) : (cons $2 $3) |
| 67 |
() : '()))) |
| 68 |
|
| 69 |
|
| 70 |
(define (display-result v) |
| 71 |
(if v |
| 72 |
(begin |
| 73 |
(display "==> ") |
| 74 |
(display v) |
| 75 |
(newline)))) |
| 76 |
|
| 77 |
|
| 78 |
;;; |
| 79 |
;;;; The lexer |
| 80 |
;;; |
| 81 |
|
| 82 |
|
| 83 |
(define (make-lexer errorp) |
| 84 |
(lambda () |
| 85 |
(letrec ((skip-spaces |
| 86 |
(lambda () |
| 87 |
(let loop ((c (peek-char))) |
| 88 |
(if (and (not (eof-object? c)) |
| 89 |
(or (char=? c #\space) (char=? c #\tab))) |
| 90 |
(begin |
| 91 |
(read-char) |
| 92 |
(loop (peek-char))))))) |
| 93 |
(read-number |
| 94 |
(lambda (l) |
| 95 |
(let ((c (peek-char))) |
| 96 |
(if (char-numeric? c) |
| 97 |
(read-number (cons (read-char) l)) |
| 98 |
(string->number (apply string (reverse l))))))) |
| 99 |
(read-id |
| 100 |
(lambda (l) |
| 101 |
(let ((c (peek-char))) |
| 102 |
(if (char-alphabetic? c) |
| 103 |
(read-id (cons (read-char) l)) |
| 104 |
(string->symbol (apply string (reverse l)))))))) |
| 105 |
|
| 106 |
;; -- skip spaces |
| 107 |
(skip-spaces) |
| 108 |
;; -- read the next token |
| 109 |
(let loop ((c (read-char))) |
| 110 |
(cond |
| 111 |
((eof-object? c) '*eoi*) |
| 112 |
((char=? c #\newline) 'NEWLINE) |
| 113 |
((char=? c #\+) '+) |
| 114 |
((char=? c #\-) '-) |
| 115 |
((char=? c #\*) '*) |
| 116 |
((char=? c #\/) '/) |
| 117 |
((char=? c #\=) '=) |
| 118 |
((char=? c #\,) 'COMMA) |
| 119 |
((char=? c #\() 'LPAREN) |
| 120 |
((char=? c #\)) 'RPAREN) |
| 121 |
((char-numeric? c) (cons 'NUM (read-number (list c)))) |
| 122 |
((char-alphabetic? c) (cons 'ID (read-id (list c)))) |
| 123 |
(else |
| 124 |
(errorp "PARSE ERROR : illegal character: " c) |
| 125 |
(skip-spaces) |
| 126 |
(loop (read-char)))))))) |
| 127 |
|
| 128 |
|
| 129 |
(define (read-line) |
| 130 |
(let loop ((c (read-char))) |
| 131 |
(if (and (not (eof-object? c)) |
| 132 |
(not (char=? c #\newline))) |
| 133 |
(loop (read-char))))) |
| 134 |
|
| 135 |
|
| 136 |
;;; |
| 137 |
;;;; Environment management |
| 138 |
;;; |
| 139 |
|
| 140 |
|
| 141 |
(define *env* (list (cons '$$ 0))) |
| 142 |
|
| 143 |
|
| 144 |
(define (init-bindings) |
| 145 |
(set-cdr! *env* '()) |
| 146 |
(add-binding 'cos cos) |
| 147 |
(add-binding 'sin sin) |
| 148 |
(add-binding 'tan tan) |
| 149 |
(add-binding 'expt expt) |
| 150 |
(add-binding 'sqrt sqrt)) |
| 151 |
|
| 152 |
|
| 153 |
(define (add-binding var val) |
| 154 |
(set! *env* (cons (cons var val) *env*)) |
| 155 |
val) |
| 156 |
|
| 157 |
|
| 158 |
(define (get-binding var) |
| 159 |
(let ((p (assq var *env*))) |
| 160 |
(if p |
| 161 |
(cdr p) |
| 162 |
0))) |
| 163 |
|
| 164 |
|
| 165 |
(define (invoke-proc proc-name args) |
| 166 |
(let ((proc (get-binding proc-name))) |
| 167 |
(if (procedure? proc) |
| 168 |
(apply proc args) |
| 169 |
(begin |
| 170 |
(display "ERROR: invalid procedure:") |
| 171 |
(display proc-name) |
| 172 |
(newline) |
| 173 |
0)))) |
| 174 |
|
| 175 |
|
| 176 |
;;; |
| 177 |
;;;; The main program |
| 178 |
;;; |
| 179 |
|
| 180 |
|
| 181 |
(define calc |
| 182 |
(lambda () |
| 183 |
(call-with-current-continuation |
| 184 |
(lambda (k) |
| 185 |
(display "********************************") (newline) |
| 186 |
(display "* Mini calculator in Scheme *") (newline) |
| 187 |
(display "* *") (newline) |
| 188 |
(display "* Enter expressions followed *") (newline) |
| 189 |
(display "* by [RETURN] or 'quit()' to *") (newline) |
| 190 |
(display "* exit. *") (newline) |
| 191 |
(display "********************************") (newline) |
| 192 |
(init-bindings) |
| 193 |
(add-binding 'quit k) |
| 194 |
(letrec ((errorp |
| 195 |
(lambda args |
| 196 |
(for-each display args) (newline))) |
| 197 |
(start |
| 198 |
(lambda () |
| 199 |
(calc-parser (make-lexer errorp) errorp)))) |
| 200 |
(start)))))) |
| 201 |
|
| 202 |
(calc) |
| 203 |
|