summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHugo <hugo.hornquist@gmail.com>2016-04-26 22:53:13 +0200
committerHugo <hugo.hornquist@gmail.com>2016-04-26 22:53:13 +0200
commit345d10e2450570a9cc425781e546ea56c5a265d3 (patch)
tree1d4f01bb4ae43a0f2edcf5d0ba02a3db2a9b2e99
parentInitial commit (diff)
downloadmath-parse-345d10e2450570a9cc425781e546ea56c5a265d3.tar.gz
math-parse-345d10e2450570a9cc425781e546ea56c5a265d3.tar.xz
parser now handles the four basic '(+ - * /)
-rw-r--r--full-parse.rkt42
1 files changed, 27 insertions, 15 deletions
diff --git a/full-parse.rkt b/full-parse.rkt
index 25239a2..ae80640 100644
--- a/full-parse.rkt
+++ b/full-parse.rkt
@@ -4,31 +4,43 @@
; + - * / ^ ( ) [0-9]
+; 5+3*x
(define (full-parse str)
(define (get-general expr operator next-operation)
(define (char->wanted c)
((if (char-numeric? c)
- string->number string->symbol)
+ string->number string->symbol)
(string c)))
+ ; used to add a completed part of the operation to the list of operations
+ ; 5 is one of those in the addition iteration
+ ; 3 and x are in the product iteration
(define (add-operation-to-list operation seq)
+ ;(display (list operator "|" operation "|" seq))
+ ;(newline)
(cons (if (= (length operation) 1)
(char->wanted (car operation))
(next-operation operation))
seq))
+ (define (contains value seq)
+ (memq value seq))
(define (inner cfac facts rexpr)
(cond
- ((null? rexpr)
- (reverse (add-operation-to-list cfac facts)))
- ((eqv? (car rexpr) operator)
- (inner '() (add-operation-to-list cfac facts) (cdr rexpr)))
- (else
- (inner (cons (car rexpr) cfac) facts (cdr rexpr)))))
- (cons (char->wanted operator) (inner '() '() expr)))
+ [(null? rexpr)
+ (reverse (add-operation-to-list cfac facts))]
+ [(eqv? (car rexpr) operator)
+ (inner '() (add-operation-to-list cfac facts) (cdr rexpr))]
+ [else
+ (inner (cons (car rexpr) cfac) facts (cdr rexpr))]))
+ (if (not (contains operator expr))
+ (next-operation expr)
+ (cons (char->wanted operator) (inner '() '() expr))))
- (define (get-parts expr)
- (get-general
- expr
- #\+
- (lambda (expr)
- (get-general expr #\* (lambda (x) x)))))
- (get-parts (string->list str)))
+ (define (create-trace expr ops)
+ (lambda (expr)
+ (get-general expr (car ops)
+ (if (null? (cdr ops))
+ (lambda (x) x)
+ (create-trace expr (cdr ops))))))
+
+ (let ((expr (string->list str)))
+ ((create-trace expr '(#\+ #\- #\* #\/)) expr)))