All pastes #250300 Raw Edit

Phi lexer

public text v1 · immutable
#250300 ·published 2006-11-16 21:07 UTC
rendered paste body
;Load the common functions
(load "common.scm")

;Wrapped token format
;((unlexed newlinenumber) (tokentype, tokencontents, linenumber))
;Token format
;(tokentype, tokencontents, linenumber)

;Some aliases for laziness
(define s->l string->list)
(define l->s list->string)
;I probably won't use these - but i do think they're good names
;(define tail cdr)
;(define head car)

;Tails and converts a list to a string
(define (tail-l->s lst)
  (l->s (cdr lst)))

;Checks if a character is in printable range
(define (printable? ch)
  (and (> ch 32) (not (= ch 127))))

;Makes a token
(define (make-token tokentype token linenum)
  (list tokentype token linenum))

;Wraps a token up with extra info
(define (wrap-token unlexed newlinenum token)
  (list (list (tail-l->s unlexed) newlinenum) token))

;Unwraps a token
(define (unwrap-token wrapped-token)
  (cdr wrapped-token))

;Returns the wrapper of a wrapped token
(define (wrapper wrapped-token)
  (car wrapped-token))

;Wraps a list token and returns
(define (return-list ch unlexed line)
  (wrap-token unlexed line (make-token ch ch line)))

;Gets the next token
(define (next-token unlexed line)
  (let temp ((lst (s->l unlexed))
	     (ch (car lst)))
    (cond
      ;listy stuff
      ((in? ch "[]()")
       (return-list ch unlexed-list linenumber)))))

;Returns a list of all tokens
(define (collect-tokens tolex)
  (let collect ((tokens '())
		(unlexed tolex)
		(linenumber 0))
    (if (null? unlexed)
      tokens
      (let temp ((wrappedtoken (next-token unlexed linenumber))
		 (wrap (wrapper wrappedtoken)))
	(collect (append tokens (unwrap-token wrappedtoken))
		 (car wrap)
		 (cadr wrap))))))