From 83b320a301aebe16bb91a5ae32e7196d781c0114 Mon Sep 17 00:00:00 2001 From: Alex Shinn Date: Fri, 3 May 2013 00:12:44 +0900 Subject: [PATCH] Adding parse-binary-op utility to (chibi parse common). --- lib/chibi/parse/common.scm | 55 ++++++++++++++++++++++++++++++++++++++ lib/chibi/parse/common.sld | 1 + tests/parse-tests.scm | 21 ++++++++++++++- 3 files changed, 76 insertions(+), 1 deletion(-) diff --git a/lib/chibi/parse/common.scm b/lib/chibi/parse/common.scm index 3620d113..e5a0307d 100644 --- a/lib/chibi/parse/common.scm +++ b/lib/chibi/parse/common.scm @@ -162,6 +162,61 @@ (apply parse-or parse-end (map parse-string terms))) car)))) +(define parse-space (parse-char char-whitespace?)) + +(define (op-value op) (car op)) +(define (op-prec op) (cadr op)) +(define (op-assoc op) + (let ((tail (cddr op))) (if (pair? tail) (car tail) 'left))) +(define (op e1 ,expr) ")") e1)) + (op + ("+" '+) ("-" '-) ("*" '*) ("/" '/) ("^" '^)) + (expr + (,(parse-binary-op op `((+ 5) (- 5) (* 3) (/ 3) (^ 1 right)) simple))))) + +(test 42 (parse prec-calc "42")) +(test '(+ 2 2) (parse prec-calc "2 + 2")) +(test '(+ (+ 2 2) 2) (parse prec-calc "2 + 2 + 2")) +(test '(+ (+ 2 (* 2 10)) 1) (parse prec-calc "2 + 2*10 + 1")) +(test '(+ (+ 2 (* 2 10)) (* 1 3)) (parse prec-calc "2+2 * 10+1 * 3")) +(test '(+ (* (+ 2 2) 10) 1) (parse prec-calc "(2 + 2) * 10 + 1")) +(test '(^ 2 (^ 2 2)) (parse prec-calc "2 ^ 2 ^ 2")) +(test '(+ (+ (+ 1 (* (* 2 (^ 3 (^ 4 5))) 6)) (^ 7 8)) 9) + (parse prec-calc "1 + 2 * 3 ^ 4 ^ 5 * 6 + 7 ^ 8 + 9")) + ;; this takes exponential time without memoization (define explode (grammar start