1 ! Copyright (C) 2004, 2010 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors arrays byte-arrays byte-vectors classes
4 classes.algebra.private classes.builtin classes.error
5 classes.intersection classes.maybe classes.mixin classes.parser
6 classes.predicate classes.singleton classes.tuple
7 classes.tuple.parser classes.union combinators compiler.units
8 definitions effects effects.parser fry generic generic.hook
9 generic.math generic.parser generic.standard hash-sets
10 hashtables hashtables.identity io.pathnames kernel lexer
11 locals.errors locals.parser macros math memoize namespaces
12 parser quotations sbufs sequences slots source-files splitting
13 strings strings.parser vectors vocabs.parser words words.alias
14 words.constant words.symbol ;
17 ! These words are defined as a top-level form, instead of with
18 ! defining parsing words, because during stage1 bootstrap, the
19 ! "syntax" vocabulary is copied from the host. When stage1
20 ! bootstrap completes, the host's syntax vocabulary is deleted
21 ! from the target, then this top-level form creates the
22 ! target's "syntax" vocabulary as one of the first things done
25 : define-delimiter ( name -- )
26 "syntax" lookup-word t "delimiter" set-word-prop ;
28 : define-core-syntax ( name quot -- )
29 [ dup "syntax" lookup-word [ ] [ no-word-error ] ?if ] dip
33 { "]" "}" ";" ">>" } [ define-delimiter ] each
37 scan-word scan-effect ensure-primitive
41 "Call stack literals are not supported" throw
44 "IN:" [ scan-token set-current-vocab ] define-core-syntax
46 "<PRIVATE" [ begin-private ] define-core-syntax
48 "PRIVATE>" [ end-private ] define-core-syntax
50 "USE:" [ scan-token use-vocab ] define-core-syntax
52 "UNUSE:" [ scan-token unuse-vocab ] define-core-syntax
54 "USING:" [ ";" [ use-vocab ] each-token ] define-core-syntax
56 "QUALIFIED:" [ scan-token dup add-qualified ] define-core-syntax
58 "QUALIFIED-WITH:" [ scan-token scan-token add-qualified ] define-core-syntax
61 scan-token "=>" expect ";" parse-tokens add-words-from
65 scan-token "=>" expect ";" parse-tokens add-words-excluding
69 scan-token scan-token "=>" expect scan-token add-renamed-word
72 "NAN:" [ 16 scan-base <fp-nan> suffix! ] define-core-syntax
74 "f" [ f suffix! ] define-core-syntax
77 lexer get parse-raw [ "token" throw-unexpected-eof ] unless* {
78 { [ dup length 1 = ] [ first ] }
79 { [ "\\" ?head ] [ next-escape >string "" assert= ] }
80 [ name>char-hook get call( name -- char ) ]
84 "\"" [ parse-string suffix! ] define-core-syntax
87 lexer get skip-blank parse-string >sbuf suffix!
91 lexer get skip-blank parse-string <pathname> suffix!
94 "[" [ parse-quotation suffix! ] define-core-syntax
95 "{" [ \ } [ >array ] parse-literal ] define-core-syntax
96 "V{" [ \ } [ >vector ] parse-literal ] define-core-syntax
97 "B{" [ \ } [ >byte-array ] parse-literal ] define-core-syntax
98 "BV{" [ \ } [ >byte-vector ] parse-literal ] define-core-syntax
99 "H{" [ \ } [ parse-hashtable ] parse-literal ] define-core-syntax
100 "IH{" [ \ } [ >identity-hashtable ] parse-literal ] define-core-syntax
101 "T{" [ parse-tuple-literal suffix! ] define-core-syntax
102 "W{" [ \ } [ first <wrapper> ] parse-literal ] define-core-syntax
103 "HS{" [ \ } [ >hash-set ] parse-literal ] define-core-syntax
105 "POSTPONE:" [ scan-word suffix! ] define-core-syntax
106 "\\" [ scan-word <wrapper> suffix! ] define-core-syntax
107 "M\\" [ scan-word scan-word lookup-method <wrapper> suffix! ] define-core-syntax
108 "inline" [ last-word make-inline ] define-core-syntax
109 "recursive" [ last-word make-recursive ] define-core-syntax
110 "foldable" [ last-word make-foldable ] define-core-syntax
111 "flushable" [ last-word make-flushable ] define-core-syntax
112 "delimiter" [ last-word t "delimiter" set-word-prop ] define-core-syntax
113 "deprecated" [ last-word make-deprecated ] define-core-syntax
116 scan-new-word parse-definition define-syntax
121 current-vocab lookup-word
122 (parse-tuple-definition)
123 2drop builtin-class check-instance drop
127 scan-new-word define-symbol
131 ";" [ create-word-in [ reset-generic ] [ define-symbol ] bi ] each-token
135 ";" [ create-class-in define-singleton-class ] each-token
139 scan-token current-vocab create-word
140 [ fake-definition ] [ set-last-word ] [ undefined-def define ] tri
144 scan-new-word scan-word define-alias
148 scan-new-word scan-object define-constant
156 [ simple-combination ] (GENERIC:)
160 [ scan-number <standard-combination> ] (GENERIC:)
164 [ math-combination ] (GENERIC:)
168 [ scan-word <hook-combination> ] (GENERIC:)
176 scan-new-class parse-array-def define-union-class
180 scan-new-class parse-array-def define-intersection-class
184 scan-new-class define-mixin-class
189 scan-word scan-word 2dup add-mixin-instance
191 ] dip remember-definition
198 parse-definition define-predicate-class
202 scan-new-class define-singleton-class
206 parse-tuple-definition define-tuple-class
214 scan-token define-protocol-slot
218 scan-new-word scan-word define-boa-word
222 parse-tuple-definition
232 ")" parse-effect suffix!
236 scan-word dup \ [ = [
237 drop "( main )" <uninterned-word> dup
238 parse-quotation ( -- ) define-declared
239 ] when dup ( -- ) check-stack-effect
240 [ current-vocab main<< ]
241 [ current-source-file get [ main<< ] [ drop ] if* ] bi
246 \ >> parse-until >quotation
247 ] with-nested-compilation-unit call( -- )
253 \ (call-next-method) suffix!
255 not-in-a-method-error
260 \ } [ <anonymous-union> <maybe> ] parse-literal
264 \ } [ <anonymous-union> <anonymous-complement> ] parse-literal
268 \ } [ <anonymous-intersection> ] parse-literal
272 \ } [ <anonymous-union> ] parse-literal
275 "initial:" "syntax" lookup-word define-symbol
277 "read-only" "syntax" lookup-word define-symbol
279 "call(" [ \ call-effect parse-call-paren ] define-core-syntax
281 "execute(" [ \ execute-effect parse-call-paren ] define-core-syntax
283 "<<<<<<<" [ version-control-merge-conflict ] define-core-syntax
284 "=======" [ version-control-merge-conflict ] define-core-syntax
285 ">>>>>>>" [ version-control-merge-conflict ] define-core-syntax
287 "<<<<<<" [ version-control-merge-conflict ] define-core-syntax
288 "======" [ version-control-merge-conflict ] define-core-syntax
289 ">>>>>>" [ version-control-merge-conflict ] define-core-syntax
292 t in-fry? [ parse-quotation ] with-variable fry append!
296 t in-fry? [ \ } parse-until >array ] with-variable fry append!
300 t in-fry? [ \ } parse-until >array ] with-variable fry
301 [ >hash-set ] compose append!
305 t in-fry? [ \ } parse-until >array ] with-variable fry
306 [ parse-hashtable ] compose append!
310 in-fry? get [ \ _ suffix! ] [ not-in-a-fry ] if
314 in-fry? get [ \ @ suffix! ] [ not-in-a-fry ] if
317 "MACRO:" [ (:) define-macro ] define-core-syntax
319 "MEMO:" [ (:) define-memoized ] define-core-syntax
320 "IDENTITY-MEMO:" [ (:) define-identity-memoized ] define-core-syntax
323 in-lambda? get [ :>-outside-lambda-error ] unless
324 scan-token parse-def suffix!
326 "[|" [ parse-lambda append! ] define-core-syntax
327 "[let" [ parse-let append! ] define-core-syntax
329 "::" [ (::) define-declared ] define-core-syntax
330 "M::" [ (M::) define ] define-core-syntax
331 "MACRO::" [ (::) define-macro ] define-core-syntax
332 "MEMO::" [ (::) define-memoized ] define-core-syntax
333 "IDENTITY-MEMO::" [ (::) define-identity-memoized ] define-core-syntax
334 ] with-compilation-unit