Legibility clean-ups for Shell and Regex code.
[cslatevm.git] / src / syntax / inference.slate
blobb58019e2bcce2d594c02b8245b1cbd780c74d5ac
1 def@(nodes MethodDefinition traits) inferenceRule
3   def selector := def selector asInferenceRule.
4   def
5 ].
7 node@(nodes Node traits) inferTypesIn: namespace
8 "Assigns a type for the node as determined through propagation of
9 existing type information and hints."
11   node type
14 node@(nodes Node traits) inferTypes
16   node inferTypesIn: lobby literalType
19 _@(nodes ImplicitArgument traits) inferTypesIn: namespace
21   namespace
24 ann@(nodes Annotation traits) inferTypesIn: namespace
26   ann type := ann value inferTypesIn: namespace
29 message@(nodes Message traits) inferTypesIn: namespace
31   message type == Types Any
32     ifTrue:
33       [message type := message selector inferOn:
34          (message arguments
35             collect: #(_ inferTypesIn: namespace) `er)]
36     ifFalse:
37       [message arguments do: #(_ inferTypesIn: namespace) `er.
38        message type]
41 opts@(nodes OptionalKeywords traits) inferTypesIn: namespace
42 [| msgType |
43   msgType := opts message inferTypes. 
44   opts arguments do: #(_ inferTypesIn: namespace) `er.
45   opts type == Types Any
46     ifTrue: [opts type := msgType]
47     ifFalse: [opts type]
50 paren@(nodes Parenthesis traits) inferTypesIn: namespace
52   paren statements do: #(_ inferTypesIn: namespace) `er.
53   paren type == Types Any /\ [paren statements isNotEmpty]
54     ifTrue: [paren type := paren statements last type]
55     ifFalse: [paren type]
58 var@(nodes Variable traits) inferTypesIn: namespace
60   var type
63 block@(nodes Block traits) inferTypesIn: namespace
64 [| resultType |
65   block localVariables do: #(_ inferTypesIn: namespace) `er.
66   resultType := Types Any.
67   "TODO: handle Return with a Union operation."
68   block statements do:
69     [| :statement | resultType := statement inferTypesIn: namespace].
70   block type == Types Any
71     ifTrue: 
72       [block type := Types Block
73          from: (block inputVariables collect: #type `er)
74          to: resultType]
75     ifFalse: [block type]
78 array@(nodes Array traits) inferTypesIn: namespace
80   array statements do: #(_ inferTypesIn: namespace) `er.
81   array type == Types Any
82     ifTrue:
83       [array type := Types Array of:
84          (array statements
85             inject: Types None into: [| :x :y | x union: y type])]
86     ifFalse: [array type]
89 obj@(Root traits) literalTypeIn: _
91   Types Singleton of: obj
94 literal@(nodes Literal traits) inferTypesIn: namespace
96   literal type == Types Any
97     ifTrue: [literal type := literal value literalTypeIn: namespace]
98     ifFalse: [literal type]
101 load@(nodes LoadVariable traits) inferTypesIn: _
103   load type == Types Any
104     ifTrue: [load type := load variable type]
105     ifFalse: [load type]
108 nodes define: #TypeMismatch &parents: {Warning} &slots: {#source. #target}.
110 tm@(nodes TypeMismatch traits) from: source to: target causedBy: description
112   (tm newDescription: description) `>> [source := source. target := target. ]
115 store@(nodes StoreVariable traits) signalTypeMismatch
117   (nodes TypeMismatch
118      from: store value
119      to: store variable
120      causedBy: 'Type mismatch in assignment to local variable: '
121         ; store variable name name "; ' type:' ; store variable type printString ; ' New type: ' ; store value type printString") signal
124 store@(nodes StoreVariable traits) inferTypesIn: namespace
126   store value inferTypesIn: namespace.
127   (store variable type subsumes: store value type)
128     ifFalse: [store signalTypeMismatch].
129   store type == Types Any
130     ifTrue: [store type := store value type]
131     ifFalse: [store type] 
134 return@(nodes Return traits) inferTypesIn: namespace
136   return value inferTypesIn: namespace.
137   return type == Types Any
138     ifTrue: [return type := return value type]
139     ifFalse: [return type]