Skip to content
Find file
Fetching contributors…
Cannot retrieve contributors at this time
executable file 97 lines (97 sloc) 4.16 KB
(define make-token (lambda (type value) (cons type (cons value '())))) (define type (lambda (token) (first token))) (define value (lambda (token) (first (rest token)))) (define next-token (lambda () (decide-next-token (next-char)))) (define decide-next-token (lambda (current-char) (cond [(space? current-char) (decide-next-token (next-char))] [(symbol? current-char) (symbol-start current-char)] [(digit? current-char) (digit-start current-char '0)] [(letter? current-char) (letter-start current-char)] [else (error "illegal character " current-char)]))) (define symbol-start (lambda (current-char) (cond [(eq? current-char #\+) (make-token 'plus-token '())] [(eq? current-char #\*) (make-token 'mult-token '())] [(eq? current-char #\/) (make-token 'div-token '())] [(eq? current-char #\() (make-token 'left-p-token '())] [(eq? current-char #\)) (make-token 'right-p-token '())] [(eq? current-char #\.) (make-token 'end-token '())] [(eq? current-char #\-) (check-for-minus-token (next-char))] [(eq? current-char #\<) (check-for-assign-token (next-char))] [(eq? current-char #\|) (check-for-else-token (next-char))] [(eq? current-char #\>) (check-for-greater-token (next-char))] [(eq? current-char #\&) (make-token 'and-token '())] [(eq? current-char #\~) (make-token 'not-token '())] [(eq? current-char #\=) (make-token 'equal-token '())] [(eq? current-char #\;) (make-token 'semicolon-token '())] [else (error "Illegal character: " current-char)]))) ;the invariant is that all the digits in digit-start are the base 10 ;values of the variable num. (define digit-start (lambda (current-char num) (cond [(not (digit? current-char)) (begin (save-char current-char) (make-token 'number-token num))] [else (digit-start (next-char) (+ (* '10 num) (char->num current-char)))]))) (define check-for-minus-token (lambda (following-char) (cond [(eq? #\> following-char) (make-token 'then-token '())] [else (begin (save-char following-char) (make-token 'minus-token '()))]))) (define check-for-assign-token (lambda (following-char) (cond [(eq? #\- following-char) (make-token 'assign-token '())] [(eq? #\= following-char) (make-token 'lesseq-token '())] [(eq? #\> following-char) (make-token 'noteq-token '())] [else (begin (save-char following-char) (make-token 'less-token'()))]))) (define check-for-else-token (lambda (following-char) (cond [(eq? #\| following-char) (make-token 'else-token '())] [else (begin (save-char following-char) (make-token 'or-token '()))]))) (define check-for-greater-token (lambda (following-char) (cond [(equal? #\= following-char) (make-token 'greatereq-token '())] [else (begin (save-char following-char) (make-token 'greater-token '()))]))) (define letter-start (lambda (current-char) (cond [(eq? current-char #\i) (check-for-in-token (next-char))] [(eq? current-char #\o) (check-for-out-token (next-char))] [(eq? current-char #\d) (check-for-do-token (next-char))] [(eq? current-char #\f) (check-for-fi-token (next-char))] [else (make-token 'identifier-token current-char)]))) (define check-for-in-token (lambda (following-char) (cond [(eq? #\n following-char) (make-token 'in-token '())] [(eq? #\f following-char) (make-token 'if-token '())] [else (begin (save-char following-char) (make-token 'identifier-token #\i))]))) (define check-for-out-token (lambda (following-char) (cond [(eq? #\u following-char) (make-token 'out-token '())] [(eq? #\d following-char) (make-token 'od-token '())] [else (begin (save-char following-char) (make-token 'identifier-token #\o))]))) (define check-for-do-token (lambda (following-char) (cond [(eq? #\o following-char) (make-token 'do-token '())] [else (begin (save-char following-char) (make-token 'identifier-token #\d))]))) (define check-for-fi-token (lambda (following-char) (cond [(eq? #\i following-char) (make-token 'fi-token '())] [else (begin (save-char following-char) (make-token 'identifier-token #\f))])))
Something went wrong with that request. Please try again.