1 ! Copyright (C) 2009 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: strings arrays memoize kernel sequences accessors combinators ;
6 SINGLETONS: nil self super ;
8 TUPLE: ast-comment { string string } ;
9 TUPLE: ast-block { arguments array } { temporaries array } { body array } ;
10 TUPLE: ast-message-send receiver { selector string } { arguments array } ;
11 TUPLE: ast-message { selector string } { arguments array } ;
12 TUPLE: ast-cascade receiver { messages array } ;
13 TUPLE: ast-name { name string } ;
14 TUPLE: ast-return value ;
15 TUPLE: ast-assignment { name ast-name } value ;
16 TUPLE: ast-local-variables { names array } ;
17 TUPLE: ast-method { name string } { body ast-block } ;
18 TUPLE: ast-class { name string } { superclass string } { ivars array } { methods array } ;
19 TUPLE: ast-foreign { class string } { name string } ;
20 TUPLE: ast-sequence { temporaries array } { body array } ;
22 ! We treat a sequence of statements like a block in a few places to
23 ! simplify handling of top-level forms
24 M: ast-sequence arguments>> drop { } ;
26 : unclip-temporaries ( statements -- temporaries statements' )
28 { [ dup empty? ] [ { } ] }
29 { [ dup first ast-local-variables? not ] [ { } ] }
33 : <ast-block> ( arguments body -- block )
34 unclip-temporaries ast-block boa ;
36 : <ast-sequence> ( body -- block )
37 unclip-temporaries ast-sequence boa ;
39 ! The parser parses normal message sends as cascades with one message, but
40 ! we represent them differently in the AST to simplify generated code in
42 : <ast-cascade> ( receiver messages -- ast )
44 [ first [ selector>> ] [ arguments>> ] bi ast-message-send boa ]
48 ! Methods return self by default
49 : <ast-method> ( class arguments body -- method )
50 self suffix <ast-block> ast-method boa ;
52 TUPLE: symbol { name string } ;
53 MEMO: intern ( name -- symbol ) symbol boa ;