home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Tools / Languages / MacHaskell 2.2 / ast / modules.scm < prev    next >
Encoding:
Text File  |  1994-09-27  |  8.9 KB  |  264 lines  |  [TEXT/CCL2]

  1. ;;;  File: ast/module-structs   Author: John
  2.  
  3. ;;; This contains AST structures which define the basic module structure.
  4. ;;; This is just the skeleton module structure: module, imports, exports,
  5. ;;; fixity, and default decls.
  6.  
  7. ;;; AST nodes defined in the file:
  8. ;;;  module  import-decl  entity  entity-module  entity-var  entity-con
  9. ;;;  entity-class  entity-abbreviated  entity-datatype  fixity-decl
  10.  
  11.  
  12.  
  13. ;;; All AST structs inherit from ast-node.  Not instantiated directly.
  14. ;;; The line-number is a back pointer to the source code.
  15.  
  16. (define-struct ast-node
  17.   (type-template ast-td)
  18.   (slots
  19.    (line-number (type (maybe source-pointer)) (default '#f))))
  20.  
  21. (define-struct source-pointer
  22.   (slots
  23.    (line (type int))
  24.    (file (type string))))
  25.  
  26. ;;; <module> -> module <modid> [<exports>] where <body>
  27. ;;;          -> <body>
  28. ;;;
  29. ;;; <exports> -> ( <export>, ... <export> )
  30. ;;;
  31. ;;; <body>   -> { [<impdecls>;] [[<fixdecls>;] <topdecls> [;]] }
  32. ;;;          -> { <impdecls> [;] }
  33. ;;;
  34. ;;; <impdecls> -> <impdecl> ; ... ; <impdecl>
  35. ;;;
  36. ;;; <fixdecls> -> <fix> ; ... ; <fix>
  37. ;;;
  38. ;;; <topdecls> -> <topdecl> ; ... ; <topdecl>
  39. ;;;
  40. ;;; <topdecl> -> <synonym-decl>
  41. ;;;           -> <algdata-decl>
  42. ;;;           -> <class-decl>
  43. ;;;           -> <instance-decl>
  44. ;;;           -> <default-decl>
  45. ;;;           -> <sign-decl>
  46. ;;;           -> <valdef>
  47.  
  48. ;;; The module struct is used to represent the program internally.  Binary
  49. ;;; files containing interface information contain these structures.
  50. ;;; Most compiler passes operate on this structure.  A table maps module
  51. ;;; names to this structure.  Within the module structure, local names are
  52. ;;; mapped to definitions.
  53.  
  54. ;;; Modules are also used to represent interfaces & primitives.
  55. ;;; Some of the module fields may be blank for non-standard modules.
  56.  
  57. (define-struct module
  58.   (include ast-node)
  59.   (slots
  60.  
  61.     ;; These slots are required.
  62.  
  63.     (name (type symbol))
  64.     (type (type (enum standard interface psuedo-interface extension)))
  65.     (prelude? (type bool) (default '#f))  ; True when symbols define the core
  66.     (interface-module (type (maybe module)) (default '#f))
  67.         ; link to previously compiled interface
  68.  
  69.     ;; The unit is filled in by the compilation system
  70.  
  71.     (unit (type symbol) (default '*undefined*))
  72.  
  73.     ;; The following slots are defined at parse time.
  74.     ;; After a module is dumped, these are all empty.
  75.  
  76.     ;; <exports>, list of exported names
  77.     (exports (type (list entity)) (default '()))
  78.     ;; <impdecls>, local import decls
  79.     (imports (type (list import-decl)) (default '()))
  80.     ;; <fixdecls>, local fixity decls
  81.     (fixities (type (list fixity-decl)) (default '()))
  82.     ;; <synonym-decl>, local type synonym decls
  83.     (synonyms (type (list synonym-decl)) (default '()))
  84.     ;; <algdata-decl>, local data decls
  85.     (algdatas (type (list data-decl)) (default '()))
  86.     ;; <class-decl>, local class decls
  87.     (classes (type (list class-decl)) (default '()))
  88.     ;; <instance-decl>, local instance decls
  89.     (instances (type (list instance-decl)) (default '()))
  90.     ;; <default-decl>, default types
  91.     (derivings (type (list deriving-decl)) (default '()))
  92.     (annotations (type (list annotation)) (default '()))
  93.     (default (type (maybe default-decl)) (default '#f))
  94.     ;; signatures, pattern, function bindings
  95.     (decls (type (list decl)) (default '()))
  96.  
  97.     ;; These slots are filled in by the type-declaration-analysis phase
  98.     ;; after conversion to definition form
  99.  
  100.     (synonym-defs (type (list synonym)) (default '()))
  101.     (alg-defs (type (list algdata)) (default '()))
  102.     (class-defs (type (list class)) (default '()))
  103.     (instance-defs (type (list instance)) (default '()))
  104.  
  105.  
  106.     ;; The import-export stage creates a set of tables which are used for
  107.     ;; imports and exports and local name resolution.  All of these tables
  108.     ;; are indexed by names.  These tables always deal with definitions.
  109.     ;; Every variable, type, class, instance, and synonym is converted into
  110.     ;; a definition.  Blank definitions are created early (in import/export)
  111.     ;; and different aspects of the definitions are filled in as compilation
  112.     ;; progresses.  The type-related definitions are filled in during
  113.     ;; declaration analysis.  Only definitions are saved when a module is
  114.     ;; written to a file; the ast information is not retained.
  115.  
  116.     ;; Used to avoid copy of Prelude symbols.
  117.     (uses-standard-prelude? (type bool) (default '#f))
  118.     ;; maps symbols in scope to definitions
  119.     (symbol-table (type (table symbol def)) (default (make-table)))
  120.     ;; maps names onto groups.
  121.     (export-table (type (table symbol (list (tuple symbol def))))
  122.           (default (make-table)))
  123.     ;; Note: symbol groups are found in classes and data decls.  An
  124.     ;; entire group is denoted by the (..) abbreviation in an entity.
  125.     ;; maps local names onto declared fixities
  126.     (fixity-table (type (table symbol fixity)) (default (make-table)))
  127.     ;; maps defs to local names
  128.     (inverted-symbol-table (type (table symbol symbol)) (default (make-table)))
  129.     ;; Used internally during import-export
  130.     (fresh-exports (type (list (list (tuple symbol def)))) (default '()))
  131.     (exported-modules (type (list module)) (default '()))
  132.  
  133.     ;; These slots are used to support incremental compilation.
  134.  
  135.     ;; vars defined in the module -- not used anymore
  136.     ;; (vars (type (list var)) (default '()))
  137.     ;; for incremental compilation
  138.     (inherited-env (type (maybe module)) (default '#f))
  139.  
  140.     ;; The following slots are for interfaces only
  141.     ;; These store renaming mappings defined in the import decls of
  142.     ;; the interface.  Maps local name onto (module, original name).
  143.     (interface-imports (type (list (tuple symbol (typle symbol symbol))))
  144.                (default '()))
  145.     ;; This is a list of the definitions created within an interface
  146.     ;; file - this is checked against actual definitions later.
  147.     (interface-definitions (type (list (tuple symbol (list def))))
  148.                (default '()))
  149.     ;; This holds dangling references from the module.
  150.     (unresolved-symbols (type (list def)) (default '()))
  151.     ;; True when no implementation is required (C / Lisp interfaces)
  152.     (stand-alone? (type bool) (default '#f))
  153.     ))
  154.  
  155. (define (interface-module? mod)
  156.   (eq? (module-type mod) 'interface))
  157.  
  158. ;;; <impdecl> -> import <modid> [<impspec>] [renaming <renamings>]
  159. ;;;
  160. ;;; <impspec> -> ( <import> , ... , <import> )
  161. ;;;           -> hiding ( <import> , ... , <import> )
  162. ;;;
  163. ;;; <import>  -> <entity>
  164. ;;;
  165. ;;; <renamings> -> ( <renaming>, ... , <renaming> )
  166. ;;;
  167. ;;; <renaming>  -> <varid> to <varid>
  168. ;;;             -> <conid> to <conid>
  169.  
  170. (define-struct import-decl
  171.   (include ast-node)
  172.   (slots
  173.    ;; <modid>, module imported from
  174.    (module-name (type symbol))
  175.    ;; all: import Foo; by-name: import Foo(x) import Foo()
  176.    (mode (type (enum all by-name)))
  177.    ;; <impspec>, for mode = all this is the hiding list
  178.    (specs (type (list entity)))
  179.    ;; <renamings>, alist maps symbol -> symbol
  180.    (renamings (type (list renaming)))
  181.    ;; place to put corresponding module-ast; filled in by import/export.
  182.    (module (type module) (uninitialized? #t))
  183.    ))
  184.  
  185.  
  186. ;;; <entity> -> <modid> ..                              entity-module
  187. ;;           -> <varid>                                 entity-var
  188. ;;;          -> <tycon>                                 entity-con
  189. ;;;          -> <tycon> (..)                            entity-abbreviated
  190. ;;;          -> <tycon> ( <conid> , ... , <conid>)      entity-datatype
  191. ;;;          -> <tycls> (..)                            entity-abbreviated
  192. ;;;                note: this is indistinguishable from tycon (..)
  193. ;;;          -> <tycls> ( <varid> , ... , <varid>)      entity-class
  194.  
  195. (define-struct entity
  196.   (include ast-node)
  197.   (slots
  198.     (name (type symbol))))
  199.  
  200. (define-struct entity-module
  201.   (include entity)
  202.   (predicate entity-module?)
  203.   (slots
  204.     ;; a direct pointer to the referenced module added later
  205.     (module (type module) (uninitialized? #t))
  206.     ))
  207.  
  208. (define-struct entity-var
  209.   (include entity)
  210.   (predicate entity-var?))
  211.  
  212. (define-struct entity-con
  213.   (include entity)
  214.   (predicate entity-con?))
  215.  
  216. (define-struct entity-abbreviated
  217.   (include entity)
  218.   (predicate entity-abbreviated?))
  219.  
  220. (define-struct entity-class
  221.   (include entity)
  222.   (predicate entity-class?)
  223.   (slots
  224.     (methods (type (list symbol)))))
  225.  
  226. (define-struct entity-datatype
  227.   (include entity)
  228.   (predicate entity-datatype?)
  229.   (slots
  230.     (constructors (type (list symbol)))))
  231.  
  232. (define-struct renaming
  233.   (include ast-node)
  234.   (slots
  235.     (from (type symbol))
  236.     (to (type symbol))
  237.     (referenced? (type bool))))
  238.         
  239.  
  240. ;;; <fix> -> infixl [<digit>] <ops>
  241. ;;;       -> infixr [<digit>] <ops>
  242. ;;;       -> infix  [<digit>] <ops>
  243. ;;;
  244. ;;; <ops> -> <op> , ... , <op>
  245. ;;;
  246. ;;; <op>  -> <varop>
  247. ;;;       -> <conop>
  248.  
  249. ;;; Not sure where to put this decl - jcp
  250. (define-struct fixity
  251.   (include ast-node)
  252.   (slots
  253.     (associativity (type (enum l n r)))
  254.     (precedence (type int))))
  255.  
  256. (define-struct fixity-decl
  257.   (include ast-node)
  258.   (slots
  259.     (fixity (type fixity))
  260.     ;; <ops>
  261.     (names (type (list symbol)))
  262.     ))
  263.  
  264.