]> gitweb.factorcode.org Git - factor.git/commitdiff
Change method parsing to validate stack effects. Fixes #236.
authorJohn Benediktsson <mrjbq7@gmail.com>
Thu, 13 Oct 2011 23:40:52 +0000 (16:40 -0700)
committerJohn Benediktsson <mrjbq7@gmail.com>
Thu, 13 Oct 2011 23:41:17 +0000 (16:41 -0700)
basis/locals/parser/parser.factor
core/generic/parser/parser.factor

index 09f75a0fa0b88bce175cdf8a7dddafba0d24ab48..4d4731048af5f0e71d32af25490f321fb1316a17 100644 (file)
@@ -68,13 +68,19 @@ M: lambda-parser parse-quotation ( -- quotation )
     dup
     in>> [ dup pair? [ first ] when ] map make-locals ;
 
-: parse-locals-definition ( word reader -- word quot effect )
-    [ parse-locals ] dip
+: (parse-locals-definition) ( effect vars assoc reader -- word quot effect )
     ((parse-lambda)) <lambda>
     [ nip "lambda" set-word-prop ]
     [ nip rewrite-closures dup length 1 = [ first ] [ bad-rewrite ] if ]
     [ drop nip ] 3tri ; inline
 
+: parse-locals-definition ( word reader -- word quot effect )
+    [ parse-locals ] dip (parse-locals-definition) ; inline
+
+: parse-locals-method-definition ( word reader -- word quot effect )
+    [ parse-locals pick check-method-effect ] dip
+    (parse-locals-definition) ; inline
+
 : (::) ( -- word def effect )
     scan-new-word
     [ parse-definition ]
@@ -83,6 +89,6 @@ M: lambda-parser parse-quotation ( -- quotation )
 : (M::) ( -- word def )
     scan-new-method
     [
-        [ parse-definition ] 
-        parse-locals-definition drop
+        [ parse-definition ]
+        parse-locals-method-definition drop
     ] with-method-definition ;
index 652fafc2e344715804fef05a79cdb5bb5f1dd36e..14b85290182fc522cf533bc5fee4fd739dead4af 100644 (file)
@@ -1,6 +1,7 @@
 ! Copyright (C) 2008, 2009 Slava Pestov.
 ! See http://factorcode.org/license.txt for BSD license.
-USING: parser kernel words generic namespaces effects.parser ;
+USING: accessors arrays combinators effects effects.parser
+generic kernel namespaces parser quotations sequences words ;
 IN: generic.parser
 
 ERROR: not-in-a-method-error ;
@@ -24,6 +25,34 @@ SYMBOL: current-method
 : with-method-definition ( method quot -- )
     over current-method set call current-method off ; inline
 
+: generic-effect ( word -- effect )
+    "method-generic" word-prop "declared-effect" word-prop ;
+
+: method-effect= ( method-effect generic-effect -- ? )
+    [ [ in>> length ] bi@ = ]
+    [
+        over terminated?>>
+        [ 2drop t ] [ [ out>> length ] bi@ = ] if
+    ] 2bi and ;
+
+ERROR: bad-method-effect ;
+
+: check-method-effect ( effect -- )
+    word generic-effect method-effect= [ bad-method-effect ] unless ;
+
+: ?execute-parsing ( word/number -- seq )
+    dup parsing-word?
+    [ V{ } clone swap execute-parsing ] [ 1array ] if ;
+
+: parse-method-definition ( -- quot )
+    scan-datum {
+        { \ ( [ ")" parse-effect check-method-effect parse-definition ] }
+        { \ ; [ [ ] ] }
+        [ ?execute-parsing \ ; parse-until append >quotation ]
+    } case ;
+
+PRIVATE>
+
 : (M:) ( -- method def )
-    scan-new-method [ parse-definition ] with-method-definition ;
+    scan-new-method [ parse-method-definition ] with-method-definition ;