]> gitweb.factorcode.org Git - factor.git/blob - extra/smalltalk/ast/ast.factor
factor: trim using lists
[factor.git] / extra / smalltalk / ast / ast.factor
1 ! Copyright (C) 2009 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors arrays combinators kernel sequences strings ;
4 IN: smalltalk.ast
5
6 SINGLETONS: nil self super ;
7
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 } ;
21
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 { } ;
25
26 : unclip-temporaries ( statements -- temporaries statements' )
27     {
28         { [ dup empty? ] [ { } ] }
29         { [ dup first ast-local-variables? not ] [ { } ] }
30         [ unclip names>> ]
31     } cond swap ;
32
33 : <ast-block> ( arguments body -- block )
34     unclip-temporaries ast-block boa ;
35
36 : <ast-sequence> ( body -- block )
37     unclip-temporaries ast-sequence boa ;
38
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
41 ! the common case
42 : <ast-cascade> ( receiver messages -- ast )
43     dup length 1 =
44     [ first [ selector>> ] [ arguments>> ] bi ast-message-send boa ]
45     [ ast-cascade boa ]
46     if ;
47
48 ! Methods return self by default
49 : <ast-method> ( class arguments body -- method )
50     self suffix <ast-block> ast-method boa ;
51
52 TUPLE: symbol { name string } ;
53 MEMO: intern ( name -- symbol ) symbol boa ;