- Timestamp:
- 03/09/08 12:59:51 (17 years ago)
- Location:
- hh2008/hayamiz
- Files:
-
- 2 added
- 1 modified
Legend:
- Unmodified
- Added
- Removed
-
hh2008/hayamiz/trunk/hascheme/parser.scm
r15 r23 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))) 8 10 (define-class <ast-type-decl> (<ast-decl>) 9 11 ((left :init-keyword :left :getter type-decl-left) … … 14 16 (define-class <ast-single-type> (<ast-type>) 15 17 ((type-name :init-keyword :name :getter type-name) 16 (type-arity :init-keyword :arity :accessor type-arity )18 (type-arity :init-keyword :arity :accessor type-arity :init-value #f) 17 19 (type-vars :init-keyword :vars :accessor type-vars :init-value #f) 18 20 (type-module :init-keyword :module :getter type-module :init-value #f))) 19 21 (define-class <ast-multi-type> (<ast-type>) 20 22 ((types :init-keyword :name :accessor types))) 21 (define-class <ast-simple-type> (<ast>) ()) 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 22 27 (define-method make-type-decl ((left <ast-simple-type>) (right <ast-type>)) 23 ; (make <type-decl> :left left :right right) 24 `(:type-decl ,left ,right)) 28 (make <ast-type-decl> :left left :right right)) 25 29 26 30 (define (hascheme:parse str) … … 32 36 (Body <- "{" (:decls TopDecls) "}" :return decls) 33 37 (TopDecls <- Spaces* (:decl TopDecl) (:rest-decls TopDecls%) Spaces* 34 :return `(:decls ,decl ,@rest-decls)38 :return (make <ast-decls> :decls (cons decl rest-decls)) 35 39 / Spaces* :return ()) 36 40 (TopDecls% <- Spaces* ";" Spaces* TopDecl TopDecls% … … 75 79 ) 76 80 77 (FuncApplyExpression <- AtomExpression +) 81 (FuncApplyExpression 82 <- (:atom AtomExpression) 83 (:rest-atoms ((Spaces (:inatm AtomExpression) :return inatm) *)) 84 Spaces* :return `(:funapply ,atom ,@rest-atoms)) 78 85 (AtomExpression 79 86 <- QVar … … 109 116 / "~" Spaces* AtomPattern) 110 117 111 (QVar <- LexQVarId / "(" Spaces* LexQVarSym Spaces* ")") 118 (QVar <- LexQVarId 119 / "(" Spaces* (:sym LexQVarSym) Spaces* ")" :return sym) 112 120 (GCon <- "(" Spaces* ")" 113 121 / "[" Spaces* "]" … … 122 130 / LexQtyCls "(" LexTyVar AType + ")") 123 131 (SimpleType <- (:con LexTyCon) (:vars SimpleTypeVars) 124 :return `(:type-name ,con :vars ,vars))132 :return (make <ast-simple-type> :name con :vars vars)) 125 133 (SimpleTypeVars <- Spaces (:var LexTyVar) (:vars SimpleTypeVars) 126 134 :return (cons var vars) 127 135 / :return ()) 128 136 (Type <- (:type BType) Spaces* (:rest-types Type%) 129 :return `(:type ,type ,@rest-types)) 137 :return 138 (if (null? rest-types) 139 type 140 (make <ast-multi-type> :types (cons type rest-types)))) 130 141 (Type% <- "->" Spaces* (:type Type) (:rest-types Type%) 131 142 :return (cons type rest-types) 132 143 / :return ()) 133 144 (BType <- (:type AType) (:rest-types BType%) 134 :return (cons type rest-types)) 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")))) 135 153 (BType% <- Spaces (:type AType) (:rest-types BType%) 136 154 :return (cons type rest-types) … … 140 158 / "(" Spaces* (:t1 Type) Spaces* "," 141 159 Spaces* (:t2 Type) Spaces* (:rest AType%) ")" 142 :return (make <ast-si mple-type> :name '$Tuple160 :return (make <ast-single-type> :name '$Tuple 143 161 :arity (+ 2 (length rest)) 144 162 :vars `(,t1 ,t2 ,@rest)) 145 163 / "[" Spaces* Type Spaces* "]" 146 / "(" Spaces* Type Spaces* ")")164 / "(" Spaces* (:t Type) Spaces* ")" :return t) 147 165 (AType% <- "," Spaces* (:t Type) Spaces* (:rest AType%) 148 166 :return (cons t rest) 149 167 / :return ()) 150 (GtyCon <- LexQtyCon 168 (GtyCon <- (:tycon LexQtyCon) 169 :return (make <ast-single-type> :name (cadr tycon) 170 :module (car tycon)) 151 171 / "(" Spaces* ")" 152 :return (make <ast-single-type> :name '$Unit :arity 0 :vars ())172 :return (make <ast-single-type> :name '$Unit :arity 0) 153 173 / "[" Spaces* "]" 154 174 :return '(make <ast-single-type> :name '$Unit :arity 0) … … 204 224 (LexQConSym <- (LexModId ".") ? LexConSym) 205 225 (LexQtyCon <- (:mod ((LexModId ".") ?)) (:tycon LexTyCon) 206 :return (if mod `( :module ,(car mod) ,@tycon)207 `( :module #f ,@tycon)))226 :return (if mod `(,(car mod) ,tycon) 227 `(#f ,tycon))) 208 228 (LexQtyCls <- (LexModId ".") ? LexTyCls) 209 229 (LexGConSym <- ":" / LexQConSym ) … … 229 249 230 250 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 (cond 267 ((null? vars) (display (type-name ast))) 268 (else 269 (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 (cond 276 ((null? vars) (display (type-name ast))) 277 (else 278 (display (type-name ast)) 279 (for-each (lambda (var) 280 (cond 281 ((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 231 295 (define (hascheme:parser-test) 232 296 (test* "Simplest body" '(())