#lang scheme/base
(require "../main.ss"
)
(define OP (tokens op <- (char-in '(#\+ #\- #\* #\/))
(return (case op
((#\+) +)
((#\-) -)
((#\*) *)
((#\/) /)))))
(define NUMBER (token real-number))
(define expr (tokens lhs <- term
(let loop ((lhs lhs))
(choice (tokens opr <- OP
rhs <- term
(loop (list opr lhs rhs)))
(return lhs)))))
(define term (tokens lhs <- factor
(let loop ((lhs lhs))
(choice (tokens opr <- OP
rhs <- factor
(loop (list opr lhs rhs)))
(return lhs)))))
(define factor (choice NUMBER (bracket #\( expr #\))))
(define (calc in)
(define (helper exp)
(cond ((number? exp) exp)
((pair? exp)
(apply (car exp)
(map helper (cdr exp))))))
(helper ((make-reader expr) in)))
(provide calc)