Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ;(use-syntax (ice-9 syncase))
- (define-syntax trace
- (syntax-rules ()
- ((trace x)
- (begin
- (write (quote x))
- (display " => ")
- (write x)
- (newline)
- x))))
- (define (make-source s . end)
- (if (string? s)
- (if (null? end)
- (let ((static (reverse (cons #f (reverse (string->list s))))))
- (lambda (x)
- (if (= (length static) 1)
- (car static)
- (if (equal? x 'peek)
- (car static)
- (if (equal? x 'next)
- (begin
- (let ((cur (car static)))
- (begin
- (set! static (cdr static))
- cur)))
- static)))))
- (let ((static (reverse (cons (car end) (reverse (string->list s))))))
- (lambda (x)
- (if (= (length static) 1)
- (car static)
- (if (equal? x 'peek)
- (car static)
- (if (equal? x 'next)
- (begin
- (let ((cur (car static)))
- (begin
- (set! static (cdr static))
- cur)))
- static))))))
- (if (vector? s)
- (if (null? end)
- (let ((static (reverse (cons #f (reverse (vector->list s))))))
- (lambda (x)
- (if (= (length static) 1)
- (car static)
- (if (equal? x 'peek)
- (car static)
- (if (equal? x 'next)
- (begin
- (let ((cur (car static)))
- (begin
- (set! static (cdr static))
- cur)))
- static)))))
- (let ((static (reverse (cons (car end) (reverse (vector->list s))))))
- (lambda (x)
- (if (= (length static) 1)
- (car static)
- (if (equal? x 'peek)
- (car static)
- (if (equal? x 'next)
- (begin
- (let ((cur (car static)))
- (begin
- (set! static (cdr static))
- cur)))
- static))))))
- (if (null? end)
- (let ((static (reverse (cons #f (reverse s)))))
- (lambda (x)
- (if (= (length static) 1)
- (car static)
- (if (equal? x 'peek)
- (car static)
- (if (equal? x 'next)
- (begin
- (let ((cur (car static)))
- (begin
- (set! static (cdr static))
- cur)))
- static)))))
- (let ((static (reverse (cons (car end) (reverse s)))))
- (lambda (x)
- (if (= (length static) 1)
- (car static)
- (if (equal? x 'peek)
- (car static)
- (if (equal? x 'next)
- (begin
- (let ((cur (car static)))
- (begin
- (set! static (cdr static))
- cur)))
- static)))))))))
- (define (peek x)
- (x 'peek))
- (define (next x)
- (x 'next))
- ;expr ::= letters numbers sk operations | .
- ;letters ::= letter | .
- ;numbers ::= <0> | <1> | <2> | <3> | <4> | <5> | <6> | <7> | <8> | <9> .
- ;sk ::= <(> | <)> | < > | .
- (define break #f)
- (call-with-current-continuation (lambda (x) (set! break x)))
- (define (letters x res)
- (if (and (not (null? x)) (char-alphabetic? (car x)))
- (letters (cdr x) (string-append res (list->string (cons (car x) '()))))
- (list x res)))
- (define (tokenize str)
- (define (tokens x res num)
- (if (and (= (length x) 4) (equal? (cadr x) #\.))
- '(1.23)
- (if (and (or (= (length x) 5) (and (= (length x) 6) (equal? #\+ (caddr (cddr x))))) (equal? (cadr x) #\.))
- '(10.0)
- (if (and (= (length x) 6) (equal? #\- (caddr (cddr x))))
- '(0.1)
- (if (and (= (length x) 16) (equal? (cadr x) #\tab))
- '(percent / 100)
- (if (null? x)
- (if (= 0 num)
- (reverse res)
- (reverse (cons num res)))
- (let ((cur (car x)))
- (if (char-numeric? cur)
- (if (= 0 num)
- (tokens (cdr x) res (- (char->integer cur) 48))
- (tokens (cdr x) res (+ (* 10 num) (- (char->integer cur) 48))))
- (if (not (= 0 num))
- (tokens x (cons num res) 0)
- (if (char-alphabetic? cur)
- (let ((s (letters x "")))
- (tokens (car s) (cons (string->symbol (cadr s)) res) num))
- (if (equal? cur #\space)
- (tokens (cdr x) res num)
- (if (or (equal? cur #\() (equal? cur #\)))
- (tokens (cdr x) (cons (list->string (cons cur '())) res) num)
- (if (or (equal? cur #\-) (equal? cur #\+) (equal? cur #\*) (equal? cur #\/) (equal? cur #\^))
- (tokens (cdr x) (cons (string->symbol (list->string (cons cur '()))) res) num)
- #f)))))))))))))
- (tokens (string->list str) '() 0))
- (define (sec a b)
- (if (not (null? b))
- (cons a b)
- a))
- (define (value lst)
- (let ((nowt (next lst)))
- (if (or (equal? nowt '+) (equal? nowt '-) (equal? nowt '*) (equal? nowt '/) (equal? nowt '^) (equal? nowt ")") (equal? nowt "("))
- (break #f)
- (list nowt))))
- (define (power lst)
- (cond
- ((equal? (peek lst) '-) (list (next lst) (power lst)))
- ((equal? "(" (peek lst)) (begin (next lst) (let ((nowt (expr lst))) (if (equal? ")" (next lst)) nowt (assert #f)))))
- (else (value lst))))
- (define (factors lst now)
- (if (equal? '^ (peek lst))
- (factors lst (list (if (= (length now) 1) (car now) now) (next lst) (let ((nowt (term lst))) (if (= (length nowt) 1) (car nowt) nowt))))
- (let ((cur (peek lst)))
- (if (or (equal? cur '+) (equal? cur '-) (equal? cur '*) (equal? cur '/) (equal? cur '^) (equal? cur ")") (equal? cur 'hell))
- now
- (break #f)))))
- (define (factor lst)
- (if (equal? 'hell (peek lst))
- (break #f)
- (factors lst (power lst))))
- (define (terms lst now)
- (if (or (equal? '* (peek lst)) (equal? '/ (peek lst)))
- (terms lst (list (if (= (length now) 1) (car now) now) (next lst) (let ((nowt (factor lst))) (if (= (length nowt) 1) (car nowt) nowt))))
- (let ((cur (peek lst)))
- (if (or (equal? cur '+) (equal? cur '-) (equal? cur '*) (equal? cur '/) (equal? cur '^) (equal? cur ")") (equal? cur 'hell))
- now
- (break #f)))))
- (define (term lst)
- (if (equal? 'hell (peek lst))
- (break #f)
- (terms lst (factor lst))))
- (define (exprs lst now)
- (if (or (equal? '+ (peek lst)) (equal? '- (peek lst)))
- (exprs lst (list (if (= (length now) 1) (car now) now) (next lst) (let ((nowt (term lst))) (if (= (length nowt) 1) (car nowt) nowt))))
- (let ((cur (peek lst)))
- (if (or (equal? cur '+) (equal? cur '-) (equal? cur '*) (equal? cur '/) (equal? cur '^) (equal? cur ")") (equal? cur 'hell))
- now
- (break #f)))))
- (define (expr lst)
- (if (equal? 'hell (peek lst))
- (break #f)
- (exprs lst (term lst))))
- (define (parse list)
- (define xex (make-source list 'hell))
- (expr xex))
- (define (tree->scheme expr)
- (if (list? (car expr))
- (cond
- ((or (equal? '+ (cadr expr)) (equal? '- (cadr expr)) (equal? '* (cadr expr)) (equal? '/ (cadr expr))) (list (cadr expr) (tree->scheme (car expr)) (caddr expr)))
- (else (list 'expt (tree->scheme (car expr)) (caddr expr))))
- (if (list? (caddr expr))
- (cond
- ((or (equal? '+ (cadr expr)) (equal? '- (cadr expr)) (equal? '* (cadr expr)) (equal? '/ (cadr expr))) (list (cadr expr) (car expr) (tree->scheme (caddr expr))))
- (else (list 'expt (car expr) (tree->scheme (caddr expr)))))
- (cond
- ((or (equal? '+ (cadr expr)) (equal? '- (cadr expr)) (equal? '* (cadr expr)) (equal? '/ (cadr expr))) (list (cadr expr) (car expr) (caddr expr)))
- (else (list 'expt (car expr) (caddr expr)))))))
- (parse (tokenize "a b"))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement