<commit_before>(cl:in-package #:cleavir-environment)

;;;; This file contains classes that can be used to augment an
;;;; environment with local information.  Existing implementations can
;;;; not use these classes, because the existing macro expander must
;;;; know how to deal with them.  They can be used in new
;;;; implementations where the creator does not have any particular
;;;; opinion about how to represent locally augmented environments.
;;;;
;;;; An alternative strategy for an existing implementation is to
;;;; modify its implementation of MACROEXPAND-1 so that it can handle
;;;; both the native representation of augmented environments, and
;;;; this one supplied by Cleavir.

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; Class ENVIRONMENT.

(defclass environment ()
  ((%global-environment :initarg :global-environment
			:accessor global-environment)
   (%augmentations :initarg :augmentations :initform '()
		   :accessor augmentations)))

(defun augment-environment (environment augmentation)
  (make-instance 'environment
    :global-environment (global-environment environment)
    :augmentations (cons augmentation (augmentations environment))))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; Class ENTRY. 

(defclass entry () ())

(defmethod add-lexical-variable
    (environment symbol &optional (identity (gensym)))
  (augment-environment environment
		       (make-instance 'lexical-variable
			 :name symbol
			 :identity identity)))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; Augment with a local function.

(defmethod add-local-function
    (environment function-name &optional (identity (gensym)))
  (augment-environment
   environment
   (make-instance 'local-function-info
     :name function-name
     :identity identity)))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; SYMBOL-MACRO.

;;; This class is used to augment an environment with a symbol macro.
;;; We use the expansion of the symbol macro rather than expander
;;; function.
(defclass symbol-macro (entry)
  ((%name :initarg :name :reader name)
   (%expansion :initarg :expansion :reader expansion)))

(defmethod add-local-symbol-macro (environment symbol expansion)
  (augment-environment environment
		       (make-instance 'symbol-macro
			 :name symbol<commit_msg>New method on ADD-LOCAL-MACRO.<commit_after>(cl:in-package #:cleavir-environment)

;;;; This file contains classes that can be used to augment an
;;;; environment with local information.  Existing implementations can
;;;; not use these classes, because the existing macro expander must
;;;; know how to deal with them.  They can be used in new
;;;; implementations where the creator does not have any particular
;;;; opinion about how to represent locally augmented environments.
;;;;
;;;; An alternative strategy for an existing implementation is to
;;;; modify its implementation of MACROEXPAND-1 so that it can handle
;;;; both the native representation of augmented environments, and
;;;; this one supplied by Cleavir.

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; Class ENVIRONMENT.

(defclass environment ()
  ((%global-environment :initarg :global-environment
			:accessor global-environment)
   (%augmentations :initarg :augmentations :initform '()
		   :accessor augmentations)))

(defun augment-environment (environment augmentation)
  (make-instance 'environment
    :global-environment (global-environment environment)
    :augmentations (cons augmentation (augmentations environment))))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; Class ENTRY. 

(defclass entry () ())

(defmethod add-lexical-variable
    (environment symbol &optional (identity (gensym)))
  (augment-environment environment
		       (make-instance 'lexical-variable
			 :name symbol
			 :identity identity)))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; Augment with a local function.

(defmethod add-local-function
    (environment function-name &optional (identity (gensym)))
  (augment-environment
   environment
   (make-instance 'local-function-info
     :name function-name
     :identity identity)))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; Augment with a local macro.

(defmethod add-local-macro (environment symbol expander)
  (augment-environment
   environment
   (make-instance 'local-macro
     :name symbol
     :expander expander)))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; SYMBOL-MACRO.

;;; This class is used to augment an environment with a symbol macro.
;;; We use the expansion of the symbol macro rather than expander
;;; function.
(defclass symbol-macro (entry)
  ((%name :initarg :name :reader name)
   (%expansion :initarg :expansion :reader expansion)))

(defmethod add-local-symbol-macro (environment symbol expansion)
  (augment-environment environment
		       (make-instance 'symbol-macro
			 :name symbol
{'fraction_non_alphanumeric': 0.21799647128014116, 'fraction_numerical': 0.00039207998431680063, 'mean_word_length': 5.252450980392157, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4888082', 'n_tokens_mistral': 1556, 'n_tokens_neox': 1320, 'n_words': 436}