;;; $Id: sym-exp.scm,v 1.9 2005/03/08 04:07:04 leavens Exp $ ;;; "sym-exp" is a form of symbol-expressions that uses the grammar ;;; ::= "symbol (symbol)" ;;; | ( {}* ) "s-list (s-list)" (module sym-exp (lib "typedscm.ss" "lib342") (provide sym-exp? s-list? symbol s-list sym-exp-symbol? sym-exp-s-list? sym-exp->symbol sym-exp->s-list parse-sym-exp parse-s-list) ;; type predicates (deftype sym-exp? (type-predicate-for sym-exp)) (deftype s-list? (type-predicate-for (list-of sym-exp))) ;; constructors (deftype symbol (-> (symbol) sym-exp)) (deftype s-list (-> ((list-of sym-exp)) sym-exp)) ;; case testers (discriminators) (deftype sym-exp-symbol? (-> (sym-exp) boolean)) (deftype sym-exp-s-list? (-> (sym-exp) boolean)) ;; observers (deftype sym-exp->symbol (-> (sym-exp) symbol)) (deftype sym-exp->s-list (-> (sym-exp) (list-of sym-exp))) ;; parsing/bless input correctness (deftype parse-sym-exp (-> (datum) sym-exp)) (deftype parse-s-list (-> (datum) (list-of sym-exp))) (defrep (sym-exp datum)) (define sym-exp? (lambda (d) (has-type-trusted boolean ;; ENSURES: result is true just when d represents a sym-exp (or (symbol? d) (s-list? d))))) (define s-list? (lambda (d) ;; ENSURES: result is true just when d represents a list of sym-exps ((list-of sym-exp?) d))) (define symbol (lambda (s) ;; ENSURES: result represents a sym-exp built from s (has-type datum s))) (define s-list (lambda (slst) ;; ENSURES: result represents a sym-exp built from slst (has-type datum slst))) (define sym-exp-symbol? (lambda (se) ;; ENSURES: result is true just when se represents a symbol (symbol? se))) (define sym-exp-s-list? (lambda (se) ;; ENSURES: result is true just when se represents a list of sym-exps (list? se))) (define sym-exp->symbol (lambda (se) ;; REQUIRES: se represents a symbol ;; ENSURES: result is the symbol represented by se (if (symbol? se) (has-type-trusted symbol se) (error "sym-exp->symbol: this is not a symbol: " se)))) (define sym-exp->s-list (lambda (se) ;; REQUIRES: se represents a s-list ;; ENSURES: result is the s-list represented by se (if (not (symbol? se)) (has-type-trusted (list-of datum) se) (error "sym-exp->s-list: this is not a s-list: " se)))) (define parse-sym-exp (lambda (exp) ;; ENSURES: if exp has the syntax of a sym-exp, ;; then result is the representation of exp ;; otherwise an error message is given. (has-type-trusted datum (cond ((symbol? exp) (symbol exp)) ((list? exp) (s-list (parse-s-list exp))) (else (error "parse-sym-exp: bad syntax: " exp)))))) (define parse-s-list (lambda (exp) ;; ENSURES: if exp has the syntax of a list of sym-exp, ;; then result is the representation of exp as a list of sym-exps, ;; otherwise an error message is given. (has-type-trusted (list-of datum) (cond ((list? exp) (map parse-sym-exp exp)) (else (error "parse-s-list: bad syntax: " exp)))))) ) ;; end module