public
Description: ioke is a new language for the JVM, based on Io and other languages.
Homepage: http://ioke.org
Clone URL: git://github.com/olabini/ioke.git
ioke / src / main / ioke / lang / TypeCheckingArgumentsDefinition.java
100644 165 lines (136 sloc) 5.161 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package ioke.lang;
 
import ioke.lang.exceptions.ControlFlow;
 
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
 
public class TypeCheckingArgumentsDefinition extends DefaultArgumentsDefinition {
    private List<Object> mustMimic = new ArrayList<Object>();
    private Object receiverMustMimic;
    private List<Argument> arguments;
 
    private TypeCheckingArgumentsDefinition(List<Argument> arguments,
            Collection<String> keywords, String rest, String krest, int min,
            int max, boolean restUneval, List<Object> mustMimic,
            Object receiverMustMimic) {
        super(arguments, keywords, rest, krest, min, max, restUneval);
        this.arguments = arguments;
        this.mustMimic = mustMimic;
        this.receiverMustMimic = receiverMustMimic;
    }
 
    private Object convertToMimic(Object mimic, Object on, IokeObject message,
            IokeObject context) throws ControlFlow {
        if (mimic == null) {
            return on;
        } else {
            return IokeObject.as(mimic).convertToThis(on, message, context);
        }
 
    }
 
    public Object getValidatedArgumentsAndReceiver(IokeObject context,
            IokeObject message, Object on,
            List<Object> argumentsWithoutKeywords,
            Map<String, Object> givenKeywords) throws ControlFlow {
 
        getEvaluatedArguments(context, message, on,
                argumentsWithoutKeywords, givenKeywords);
 
        int ix = 0;
        for (int i = 0, j = this.arguments.size(); i < j; i++) {
            Argument a = this.arguments.get(i);
 
            if (a instanceof KeywordArgument) {
                String name = a.getName() + ":";
                Object given = givenKeywords.get(name);
                if (given != null) {
                    givenKeywords.put(name, convertToMimic(
                            mustMimic.get(i), given, message, context));
                }
            } else {
                argumentsWithoutKeywords.set(ix, convertToMimic(mustMimic.get(i),
                        argumentsWithoutKeywords.get(ix), message,
                        context));
                ix++;
            }
        }
 
        return convertToMimic(receiverMustMimic, on, message, context);
    }
 
    public static TypeCheckingArgumentsDefinition empty() {
        return emptyButReceiverMustMimic(null);
    }
 
    public static TypeCheckingArgumentsDefinition emptyButReceiverMustMimic(Object mimic) {
        return new TypeCheckingArgumentsDefinition(new ArrayList<Argument>(), new ArrayList<String>(), null, null, 0, 0, false, new ArrayList<Object>(), mimic);
    }
    
    public static class Builder extends DefaultArgumentsDefinition.Builder {
        private List<Object> mustMimic = new ArrayList<Object>();
        private Object receiverMustMimic;
 
        private boolean setMimic = true;
 
        private void next() {
            if (!setMimic)
                mustMimic.add(null);
            else
                setMimic = false;
        }
 
        public Builder whichMustMimic(Object mimic) {
            mustMimic.add(mimic);
            return this;
        }
 
        public Builder receiverMustMimic(Object mimic) {
            this.receiverMustMimic = mimic;
            return this;
        }
 
        @Override
        public Builder withKeyword(String name) {
            super.withKeyword(name);
            next();
            return this;
        }
 
        @Override
        public Builder withKeywordRest(String name) {
            super.withKeywordRest(name);
            next();
            return this;
        }
 
        @Override
        public Builder withOptionalPositional(String name, String defaultValue) {
            super.withOptionalPositional(name, defaultValue);
            next();
            return this;
        }
 
        @Override
        public Builder withOptionalPositionalUnevaluated(String name) {
            super.withOptionalPositionalUnevaluated(name);
            next();
            return this;
        }
 
        @Override
        public Builder withRequiredPositional(String name) {
            super.withRequiredPositional(name);
            next();
            return this;
        }
 
        @Override
        public Builder withRequiredPositionalUnevaluated(String name) {
            super.withRequiredPositionalUnevaluated(name);
            next();
            return this;
        }
 
        @Override
        public Builder withRest(String name) {
            super.withRest(name);
            next();
            return this;
        }
 
        @Override
        public Builder withRestUnevaluated(String name) {
            super.withRestUnevaluated(name);
            next();
            return this;
        }
 
        @Override
        public TypeCheckingArgumentsDefinition getArguments() {
            next();
            return new TypeCheckingArgumentsDefinition(arguments, keywords,
                    rest, krest, min, max, restUneval, mustMimic,
                    receiverMustMimic);
        }
    }
 
    public static Builder builder() {
        return new Builder();
    }
}