node.scm 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. ; Copyright (c) 1993-2008 by Richard Kelsey and Jonathan Rees. See file COPYING.
  2. ; --------------------
  3. ; Operators (= special operators and primitives)
  4. (define-record-type operator :operator
  5. (make-operator type nargs uid name)
  6. operator?
  7. (type operator-type set-operator-type!)
  8. (nargs operator-nargs)
  9. (uid operator-uid)
  10. (name operator-name))
  11. (define-record-discloser :operator
  12. (lambda (s)
  13. (list 'operator
  14. (operator-name s)
  15. (if (symbol? (operator-type s))
  16. (operator-type s)
  17. (type->sexp (operator-type s) #t)))))
  18. (define usual-operator-type
  19. (procedure-type any-arguments-type value-type #f))
  20. (define (get-operator name . type-option)
  21. (let ((type (if (null? type-option) #f (car type-option)))
  22. (probe (table-ref operators-table name)))
  23. (if (operator? probe)
  24. (let ((previous-type (operator-type probe)))
  25. (cond ((not type))
  26. ((not previous-type)
  27. (set-operator-type! probe type))
  28. ((symbol? type) ; 'leaf or 'internal
  29. (if (not (eq? type previous-type))
  30. (warn "operator type inconsistency" name type previous-type)))
  31. ((subtype? type previous-type) ;Improvement
  32. (set-operator-type! probe type))
  33. ((not (subtype? previous-type type))
  34. (warn "operator type inconsistency"
  35. name
  36. (type->sexp previous-type 'foo)
  37. (type->sexp type 'foo))))
  38. probe)
  39. (let* ((uid *operator-uid*)
  40. (op (make-operator type
  41. (if (and type
  42. (not (symbol? type))
  43. (fixed-arity-procedure-type? type))
  44. (procedure-type-arity type)
  45. #f)
  46. uid
  47. name)))
  48. (if (>= uid number-of-operators)
  49. (warn "too many operators" (operator-name op) (operator-type op)))
  50. (set! *operator-uid* (+ *operator-uid* 1))
  51. (table-set! operators-table (operator-name op) op)
  52. (vector-set! the-operators uid op)
  53. op))))
  54. (define *operator-uid* 0)
  55. (define operators-table (make-table))
  56. (define number-of-operators 400) ;Fixed-size limits bad, but speed good
  57. (define the-operators (make-vector number-of-operators #f))
  58. ; --------------------
  59. ; Operator tables (for fast dispatch)
  60. (define (make-operator-table default)
  61. (make-vector number-of-operators default))
  62. (define operator-table-ref vector-ref)
  63. (define (operator-lookup table op)
  64. (operator-table-ref table (operator-uid op)))
  65. (define (operator-define! table name type proc)
  66. (vector-set! table
  67. (operator-uid (get-operator name type))
  68. proc))
  69. ; --------------------
  70. ; Nodes
  71. ; A node is an annotated expression (or definition or other form).
  72. ; The FORM component of a node is an S-expression of the same form as
  73. ; the S-expression representation of the expression. E.g. for
  74. ; literals, the form is the literal value; for variables the form is
  75. ; the variable name; for IF expressions the form is a 4-element list
  76. ; (ignored test con alt). Nodes also have a tag identifying what kind
  77. ; of node it is (literal, variable, if, etc.) and a property list.
  78. (define-record-type node :node
  79. (really-make-node uid form plist)
  80. node?
  81. (uid node-operator-id)
  82. (form node-form)
  83. (plist node-plist set-node-plist!))
  84. (define-record-discloser :node
  85. (lambda (n) (list (operator-name (node-operator n)) (node-form n))))
  86. (define (make-node operator form)
  87. (really-make-node (operator-uid operator) form '()))
  88. (define (node-ref node key)
  89. (let ((probe (assq key (node-plist node))))
  90. (if probe (cdr probe) #f)))
  91. (define (node-set! node key value) ;gross
  92. (if value
  93. (let ((probe (assq key (node-plist node))))
  94. (if probe
  95. (set-cdr! probe value)
  96. (set-node-plist! node (cons (cons key value) (node-plist node)))))
  97. (let loop ((l (node-plist node)) (prev #f))
  98. (cond ((null? l) 'lose)
  99. ((eq? key (caar l))
  100. (if prev
  101. (set-cdr! prev (cdr l))
  102. (set-node-plist! node (cdr l))))
  103. (else (loop (cdr l) l))))))
  104. (define (node-operator node)
  105. (vector-ref the-operators (node-operator-id node)))
  106. (define (node-predicate name . type-option)
  107. (let ((id (operator-uid (apply get-operator name type-option))))
  108. (lambda (node)
  109. (= (node-operator-id node) id))))
  110. (define (make-similar-node node form)
  111. (if (equal? form (node-form node))
  112. node
  113. (make-node (node-operator node) form)))
  114. ; Top-level nodes are often delayed.
  115. (define (force-node node)
  116. (if (node? node)
  117. node
  118. (force node)))
  119. ; Node predicates and operators.
  120. (define lambda-node? (node-predicate 'lambda syntax-type))
  121. (define flat-lambda-node? (node-predicate 'flat-lambda syntax-type))
  122. (define call-node? (node-predicate 'call))
  123. (define name-node? (node-predicate 'name 'leaf))
  124. (define literal-node? (node-predicate 'literal 'leaf))
  125. (define quote-node? (node-predicate 'quote syntax-type))
  126. (define define-node? (node-predicate 'define))
  127. (define loophole-node? (node-predicate 'loophole))
  128. (define operator/flat-lambda (get-operator 'flat-lambda))
  129. (define operator/lambda (get-operator 'lambda syntax-type))
  130. (define operator/set! (get-operator 'set! syntax-type))
  131. (define operator/call (get-operator 'call 'internal))
  132. (define operator/begin (get-operator 'begin syntax-type))
  133. (define operator/name (get-operator 'name 'leaf))
  134. (define operator/letrec (get-operator 'letrec))
  135. (define operator/pure-letrec (get-operator 'pure-letrec))
  136. (define operator/literal (get-operator 'literal))
  137. (define operator/quote (get-operator 'quote syntax-type))
  138. (define operator/unassigned (get-operator 'unassigned))
  139. (define operator/unspecific (get-operator 'unspecific (proc () unspecific-type)))
  140. (define operator/define (get-operator 'define syntax-type))
  141. (define operator/define-syntax (get-operator 'define-syntax syntax-type))
  142. (define operator/primitive-procedure
  143. (get-operator 'primitive-procedure syntax-type))
  144. (define operator/structure-ref (get-operator 'structure-ref syntax-type))