Changes in hh2008/hayamiz/trunk/hascheme/parser.scm [23:15]
- Files:
-
- 1 modified
Legend:
- Unmodified
- Added
- Removed
-
hh2008/hayamiz/trunk/hascheme/parser.scm
r23 r15 6 6 (define-class <ast> () ()) 7 7 (define-class <ast-decl> (<ast>) ()) 8 (define-class <ast-decls> (<ast>)9 ((delcs :init-keyword :decls :getter decls)))10 8 (define-class <ast-type-decl> (<ast-decl>) 11 9 ((left :init-keyword :left :getter type-decl-left) … … 16 14 (define-class <ast-single-type> (<ast-type>) 17 15 ((type-name :init-keyword :name :getter type-name) 18 (type-arity :init-keyword :arity :accessor type-arity :init-value #f)16 (type-arity :init-keyword :arity :accessor type-arity) 19 17 (type-vars :init-keyword :vars :accessor type-vars :init-value #f) 20 18 (type-module :init-keyword :module :getter type-module :init-value #f))) 21 19 (define-class <ast-multi-type> (<ast-type>) 22 20 ((types :init-keyword :name :accessor types))) 23 (define-class <ast-simple-type> (<ast>) 24 ((type-name :init-keyword :name :getter type-name) 25 (type-vars :init-keyword :vars :getter type-vars))) 26 21 (define-class <ast-simple-type> (<ast>) ()) 27 22 (define-method make-type-decl ((left <ast-simple-type>) (right <ast-type>)) 28 (make <ast-type-decl> :left left :right right)) 23 ; (make <type-decl> :left left :right right) 24 `(:type-decl ,left ,right)) 29 25 30 26 (define (hascheme:parse str) … … 36 32 (Body <- "{" (:decls TopDecls) "}" :return decls) 37 33 (TopDecls <- Spaces* (:decl TopDecl) (:rest-decls TopDecls%) Spaces* 38 :return (make <ast-decls> :decls (cons decl rest-decls))34 :return `(:decls ,decl ,@rest-decls) 39 35 / Spaces* :return ()) 40 36 (TopDecls% <- Spaces* ";" Spaces* TopDecl TopDecls% … … 79 75 ) 80 76 81 (FuncApplyExpression 82 <- (:atom AtomExpression) 83 (:rest-atoms ((Spaces (:inatm AtomExpression) :return inatm) *)) 84 Spaces* :return `(:funapply ,atom ,@rest-atoms)) 77 (FuncApplyExpression <- AtomExpression +) 85 78 (AtomExpression 86 79 <- QVar … … 116 109 / "~" Spaces* AtomPattern) 117 110 118 (QVar <- LexQVarId 119 / "(" Spaces* (:sym LexQVarSym) Spaces* ")" :return sym) 111 (QVar <- LexQVarId / "(" Spaces* LexQVarSym Spaces* ")") 120 112 (GCon <- "(" Spaces* ")" 121 113 / "[" Spaces* "]" … … 130 122 / LexQtyCls "(" LexTyVar AType + ")") 131 123 (SimpleType <- (:con LexTyCon) (:vars SimpleTypeVars) 132 :return (make <ast-simple-type> :name con :varsvars))124 :return `(:type-name ,con :vars ,vars)) 133 125 (SimpleTypeVars <- Spaces (:var LexTyVar) (:vars SimpleTypeVars) 134 126 :return (cons var vars) 135 127 / :return ()) 136 128 (Type <- (:type BType) Spaces* (:rest-types Type%) 137 :return 138 (if (null? rest-types) 139 type 140 (make <ast-multi-type> :types (cons type rest-types)))) 129 :return `(:type ,type ,@rest-types)) 141 130 (Type% <- "->" Spaces* (:type Type) (:rest-types Type%) 142 131 :return (cons type rest-types) 143 132 / :return ()) 144 133 (BType <- (:type AType) (:rest-types BType%) 145 :return (cond 146 ((is-a? type <ast-single-type>) 147 (begin (set! (type-vars type) rest-types) 148 type)) 149 ((and (is-a? #?=type <ast-multi-type>) 150 (null? #?=rest-types)) 151 type) 152 (else (error "Type error")))) 134 :return (cons type rest-types)) 153 135 (BType% <- Spaces (:type AType) (:rest-types BType%) 154 136 :return (cons type rest-types) … … 158 140 / "(" Spaces* (:t1 Type) Spaces* "," 159 141 Spaces* (:t2 Type) Spaces* (:rest AType%) ")" 160 :return (make <ast-si ngle-type> :name '$Tuple142 :return (make <ast-simple-type> :name '$Tuple 161 143 :arity (+ 2 (length rest)) 162 144 :vars `(,t1 ,t2 ,@rest)) 163 145 / "[" Spaces* Type Spaces* "]" 164 / "(" Spaces* (:t Type) Spaces* ")" :return t)146 / "(" Spaces* Type Spaces* ")") 165 147 (AType% <- "," Spaces* (:t Type) Spaces* (:rest AType%) 166 148 :return (cons t rest) 167 149 / :return ()) 168 (GtyCon <- (:tycon LexQtyCon) 169 :return (make <ast-single-type> :name (cadr tycon) 170 :module (car tycon)) 150 (GtyCon <- LexQtyCon 171 151 / "(" Spaces* ")" 172 :return (make <ast-single-type> :name '$Unit :arity 0 )152 :return (make <ast-single-type> :name '$Unit :arity 0 :vars ()) 173 153 / "[" Spaces* "]" 174 154 :return '(make <ast-single-type> :name '$Unit :arity 0) … … 224 204 (LexQConSym <- (LexModId ".") ? LexConSym) 225 205 (LexQtyCon <- (:mod ((LexModId ".") ?)) (:tycon LexTyCon) 226 :return (if mod `( ,(car mod) ,tycon)227 `( #f ,tycon)))206 :return (if mod `(:module ,(car mod) ,@tycon) 207 `(:module #f ,@tycon))) 228 208 (LexQtyCls <- (LexModId ".") ? LexTyCls) 229 209 (LexGConSym <- ":" / LexQConSym ) … … 249 229 250 230 251 (define-method hascheme:ast-print ((ast <ast-decls>))252 (let1 ast-decls (decls ast)253 (unless (null? ast-decls)254 (hascheme:ast-print (car ast-decls))255 (unless (null? (cdr ast-decls))256 (display "; ")257 (for-each hascheme:ast-print (cdr ast-decls))))))258 259 (define-method hascheme:ast-print ((ast <ast-type-decl>))260 (hascheme:ast-print (type-decl-left ast))261 (display " = ")262 (hascheme:ast-print (type-decl-right ast)))263 264 (define-method hascheme:ast-print ((ast <ast-simple-type>))265 (let1 vars (type-vars ast)266 (cond267 ((null? vars) (display (type-name ast)))268 (else269 (display (type-name ast))270 (for-each (lambda (sym) (format #t " ~a" sym))271 vars)))))272 273 (define-method hascheme:ast-print ((ast <ast-single-type>))274 (let1 vars (type-vars ast)275 (cond276 ((null? vars) (display (type-name ast)))277 (else278 (display (type-name ast))279 (for-each (lambda (var)280 (cond281 ((symbol? var) (format #t " ~a" var))282 ((is-a? var <ast-type-var>)283 (display " ")284 (hascheme:ast-print var))285 ((is-a? var <ast>)286 (display " (")287 (hascheme:ast-print var)288 (display ")"))289 (error "Invalid type")))290 vars)))))291 292 (define-method hascheme:ast-print ((ast <ast-type-var>))293 (display (type-var ast)))294 295 231 (define (hascheme:parser-test) 296 232 (test* "Simplest body" '(())