RPN Calculator in Chicken Scheme











up vote
0
down vote

favorite












My first attempt at Scheme (Chicken Scheme) - a simple RPN calculator. Please comment on exception handling as well as the general coding style. Here is the code:



(use stack)
(use srfi-69)
(use simple-exceptions)

(define prompt "> ")

(define stack (make-stack))
(define words (make-hash-table))

(define (init-words)
(hash-table-set! words "bye" '(quit))
(hash-table-set! words ".s" '(stack-print))
(hash-table-set! words "+" '(add))
(hash-table-set! words "-" '(subtract))
(hash-table-set! words "*" '(multiply))
(hash-table-set! words "/" '(divide))
(hash-table-set! words "." '(pop-print))
(hash-table-set! words "dup" '(dup))
(hash-table-set! words "drop" '(drop))
(hash-table-set! words "swap" '(swap)))


(define (stack-print)
(display (stack->list stack))
(newline))


(define (pop-print)
(display (stack-pop! stack))
(newline))


(define (add)
(stack-push! stack
(+ (stack-pop! stack)
(stack-pop! stack))))


(define (subtract)
(swap)
(stack-push! stack
(- (stack-pop! stack)
(stack-pop! stack))))


(define (multiply)
(stack-push! stack
(* (stack-pop! stack)
(stack-pop! stack))))


(define (divide)
(swap)
(stack-push! stack
(/ (stack-pop! stack)
(stack-pop! stack))))


(define (dup)
(let ((first (stack-pop! stack)))
(stack-push! stack first)
(stack-push! stack first)))


(define (drop)
(stack-pop! stack))


(define (swap)
(let ((first (stack-pop! stack))
(second (stack-pop! stack)))
(stack-push! stack first)
(stack-push! stack second)))


(define (f-read)
(display prompt)
(flush-output)
(read-line))


(define (f-parse line)
(string-split line))


(define (process word)
(cond
((hash-table-exists? words word)
(eval (hash-table-ref words word)))
((string->number word)
(stack-push! stack (string->number word)))))


(define (f-eval words)
(cond
((null? words) "")
(else (process (car words))
(f-eval (cdr words)))))


(define (f-print line)
(display line))

;; user-interrupt ?

(define (repl)
(guard
(exn (((exception-of? 'user-interrupt) exn)
(quit))
((exception? exn)
(display (message exn))
(newline)
(repl))
(else
(display (arguments exn))
(newline)
(repl)))
(init-words)
(f-print (f-eval (f-parse (f-read))))
(repl)))









share|improve this question







New contributor




Anonimista is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
























    up vote
    0
    down vote

    favorite












    My first attempt at Scheme (Chicken Scheme) - a simple RPN calculator. Please comment on exception handling as well as the general coding style. Here is the code:



    (use stack)
    (use srfi-69)
    (use simple-exceptions)

    (define prompt "> ")

    (define stack (make-stack))
    (define words (make-hash-table))

    (define (init-words)
    (hash-table-set! words "bye" '(quit))
    (hash-table-set! words ".s" '(stack-print))
    (hash-table-set! words "+" '(add))
    (hash-table-set! words "-" '(subtract))
    (hash-table-set! words "*" '(multiply))
    (hash-table-set! words "/" '(divide))
    (hash-table-set! words "." '(pop-print))
    (hash-table-set! words "dup" '(dup))
    (hash-table-set! words "drop" '(drop))
    (hash-table-set! words "swap" '(swap)))


    (define (stack-print)
    (display (stack->list stack))
    (newline))


    (define (pop-print)
    (display (stack-pop! stack))
    (newline))


    (define (add)
    (stack-push! stack
    (+ (stack-pop! stack)
    (stack-pop! stack))))


    (define (subtract)
    (swap)
    (stack-push! stack
    (- (stack-pop! stack)
    (stack-pop! stack))))


    (define (multiply)
    (stack-push! stack
    (* (stack-pop! stack)
    (stack-pop! stack))))


    (define (divide)
    (swap)
    (stack-push! stack
    (/ (stack-pop! stack)
    (stack-pop! stack))))


    (define (dup)
    (let ((first (stack-pop! stack)))
    (stack-push! stack first)
    (stack-push! stack first)))


    (define (drop)
    (stack-pop! stack))


    (define (swap)
    (let ((first (stack-pop! stack))
    (second (stack-pop! stack)))
    (stack-push! stack first)
    (stack-push! stack second)))


    (define (f-read)
    (display prompt)
    (flush-output)
    (read-line))


    (define (f-parse line)
    (string-split line))


    (define (process word)
    (cond
    ((hash-table-exists? words word)
    (eval (hash-table-ref words word)))
    ((string->number word)
    (stack-push! stack (string->number word)))))


    (define (f-eval words)
    (cond
    ((null? words) "")
    (else (process (car words))
    (f-eval (cdr words)))))


    (define (f-print line)
    (display line))

    ;; user-interrupt ?

    (define (repl)
    (guard
    (exn (((exception-of? 'user-interrupt) exn)
    (quit))
    ((exception? exn)
    (display (message exn))
    (newline)
    (repl))
    (else
    (display (arguments exn))
    (newline)
    (repl)))
    (init-words)
    (f-print (f-eval (f-parse (f-read))))
    (repl)))









    share|improve this question







    New contributor




    Anonimista is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.






















      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      My first attempt at Scheme (Chicken Scheme) - a simple RPN calculator. Please comment on exception handling as well as the general coding style. Here is the code:



      (use stack)
      (use srfi-69)
      (use simple-exceptions)

      (define prompt "> ")

      (define stack (make-stack))
      (define words (make-hash-table))

      (define (init-words)
      (hash-table-set! words "bye" '(quit))
      (hash-table-set! words ".s" '(stack-print))
      (hash-table-set! words "+" '(add))
      (hash-table-set! words "-" '(subtract))
      (hash-table-set! words "*" '(multiply))
      (hash-table-set! words "/" '(divide))
      (hash-table-set! words "." '(pop-print))
      (hash-table-set! words "dup" '(dup))
      (hash-table-set! words "drop" '(drop))
      (hash-table-set! words "swap" '(swap)))


      (define (stack-print)
      (display (stack->list stack))
      (newline))


      (define (pop-print)
      (display (stack-pop! stack))
      (newline))


      (define (add)
      (stack-push! stack
      (+ (stack-pop! stack)
      (stack-pop! stack))))


      (define (subtract)
      (swap)
      (stack-push! stack
      (- (stack-pop! stack)
      (stack-pop! stack))))


      (define (multiply)
      (stack-push! stack
      (* (stack-pop! stack)
      (stack-pop! stack))))


      (define (divide)
      (swap)
      (stack-push! stack
      (/ (stack-pop! stack)
      (stack-pop! stack))))


      (define (dup)
      (let ((first (stack-pop! stack)))
      (stack-push! stack first)
      (stack-push! stack first)))


      (define (drop)
      (stack-pop! stack))


      (define (swap)
      (let ((first (stack-pop! stack))
      (second (stack-pop! stack)))
      (stack-push! stack first)
      (stack-push! stack second)))


      (define (f-read)
      (display prompt)
      (flush-output)
      (read-line))


      (define (f-parse line)
      (string-split line))


      (define (process word)
      (cond
      ((hash-table-exists? words word)
      (eval (hash-table-ref words word)))
      ((string->number word)
      (stack-push! stack (string->number word)))))


      (define (f-eval words)
      (cond
      ((null? words) "")
      (else (process (car words))
      (f-eval (cdr words)))))


      (define (f-print line)
      (display line))

      ;; user-interrupt ?

      (define (repl)
      (guard
      (exn (((exception-of? 'user-interrupt) exn)
      (quit))
      ((exception? exn)
      (display (message exn))
      (newline)
      (repl))
      (else
      (display (arguments exn))
      (newline)
      (repl)))
      (init-words)
      (f-print (f-eval (f-parse (f-read))))
      (repl)))









      share|improve this question







      New contributor




      Anonimista is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      My first attempt at Scheme (Chicken Scheme) - a simple RPN calculator. Please comment on exception handling as well as the general coding style. Here is the code:



      (use stack)
      (use srfi-69)
      (use simple-exceptions)

      (define prompt "> ")

      (define stack (make-stack))
      (define words (make-hash-table))

      (define (init-words)
      (hash-table-set! words "bye" '(quit))
      (hash-table-set! words ".s" '(stack-print))
      (hash-table-set! words "+" '(add))
      (hash-table-set! words "-" '(subtract))
      (hash-table-set! words "*" '(multiply))
      (hash-table-set! words "/" '(divide))
      (hash-table-set! words "." '(pop-print))
      (hash-table-set! words "dup" '(dup))
      (hash-table-set! words "drop" '(drop))
      (hash-table-set! words "swap" '(swap)))


      (define (stack-print)
      (display (stack->list stack))
      (newline))


      (define (pop-print)
      (display (stack-pop! stack))
      (newline))


      (define (add)
      (stack-push! stack
      (+ (stack-pop! stack)
      (stack-pop! stack))))


      (define (subtract)
      (swap)
      (stack-push! stack
      (- (stack-pop! stack)
      (stack-pop! stack))))


      (define (multiply)
      (stack-push! stack
      (* (stack-pop! stack)
      (stack-pop! stack))))


      (define (divide)
      (swap)
      (stack-push! stack
      (/ (stack-pop! stack)
      (stack-pop! stack))))


      (define (dup)
      (let ((first (stack-pop! stack)))
      (stack-push! stack first)
      (stack-push! stack first)))


      (define (drop)
      (stack-pop! stack))


      (define (swap)
      (let ((first (stack-pop! stack))
      (second (stack-pop! stack)))
      (stack-push! stack first)
      (stack-push! stack second)))


      (define (f-read)
      (display prompt)
      (flush-output)
      (read-line))


      (define (f-parse line)
      (string-split line))


      (define (process word)
      (cond
      ((hash-table-exists? words word)
      (eval (hash-table-ref words word)))
      ((string->number word)
      (stack-push! stack (string->number word)))))


      (define (f-eval words)
      (cond
      ((null? words) "")
      (else (process (car words))
      (f-eval (cdr words)))))


      (define (f-print line)
      (display line))

      ;; user-interrupt ?

      (define (repl)
      (guard
      (exn (((exception-of? 'user-interrupt) exn)
      (quit))
      ((exception? exn)
      (display (message exn))
      (newline)
      (repl))
      (else
      (display (arguments exn))
      (newline)
      (repl)))
      (init-words)
      (f-print (f-eval (f-parse (f-read))))
      (repl)))






      scheme






      share|improve this question







      New contributor




      Anonimista is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      share|improve this question







      New contributor




      Anonimista is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      share|improve this question




      share|improve this question






      New contributor




      Anonimista is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked 2 hours ago









      Anonimista

      1




      1




      New contributor




      Anonimista is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      Anonimista is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      Anonimista is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.



























          active

          oldest

          votes











          Your Answer





          StackExchange.ifUsing("editor", function () {
          return StackExchange.using("mathjaxEditing", function () {
          StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
          StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
          });
          });
          }, "mathjax-editing");

          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "196"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });






          Anonimista is a new contributor. Be nice, and check out our Code of Conduct.










           

          draft saved


          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f207631%2frpn-calculator-in-chicken-scheme%23new-answer', 'question_page');
          }
          );

          Post as a guest





































          active

          oldest

          votes













          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          Anonimista is a new contributor. Be nice, and check out our Code of Conduct.










           

          draft saved


          draft discarded


















          Anonimista is a new contributor. Be nice, and check out our Code of Conduct.













          Anonimista is a new contributor. Be nice, and check out our Code of Conduct.












          Anonimista is a new contributor. Be nice, and check out our Code of Conduct.















           


          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f207631%2frpn-calculator-in-chicken-scheme%23new-answer', 'question_page');
          }
          );

          Post as a guest




















































































          Popular posts from this blog

          Morgemoulin

          Scott Moir

          Souastre