blob: cb1d38b85aabab5d58ab6b42de9cce983681edb6 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|
#lang racket
(provide full-parse)
; + - * / ^ ( ) [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 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 current-term other-terms remaining-expression)
(cond
[(null? remaining-expression)
(reverse (add-operation-to-list current-term other-terms))]
[(eqv? (car remaining-expression) operator)
(reverse (inner '() (add-operation-to-list current-term other-terms) (cdr remaining-expression)))]
[else
(reverse (inner (cons (car remaining-expression) current-term) other-terms (cdr remaining-expression)))]))
(if (not (contains operator expr))
(next-operation expr)
(cons (char->wanted operator) (inner '() '() expr))))
(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)))
|