-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHP12c_Update_handlers.elm
280 lines (216 loc) · 7.53 KB
/
HP12c_Update_handlers.elm
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
module HP12c_Update_handlers exposing (..)
import HP12c_KeyTypes exposing (..)
import HP12c_Model exposing (..)
import HP12c_Update_utils exposing (..)
--import Keyboard
type UpdateHandler
= UpdateHandler (Msg -> Model -> List UpdateHandler -> ( Model, Cmd Msg ))
chainHandler : Msg -> Model -> List UpdateHandler -> ( Model, Cmd Msg )
chainHandler msg model handlers =
let
firstHandler =
List.head handlers
remainingHandlers =
List.tail handlers
in
case ( firstHandler, remainingHandlers ) of
( Just (UpdateHandler handler), Just rest_handler ) ->
handler msg model rest_handler
( Just (UpdateHandler handler), _ ) ->
handler msg model []
( Nothing, _ ) ->
( model, Cmd.none )
handleDigitKeys : Msg -> Model -> List UpdateHandler -> ( Model, Cmd Msg )
handleDigitKeys msg model nonDigitHandlers =
let
( digit, handled ) =
case msg of
Number_7_Key ->
( 7, True )
Number_8_Key ->
( 8, True )
Number_9_Key ->
( 9, True )
Number_4_Key ->
( 4, True )
Number_5_Key ->
( 5, True )
Number_6_Key ->
( 6, True )
Number_1_Key ->
( 1, True )
Number_2_Key ->
( 2, True )
Number_3_Key ->
( 3, True )
Number_0_Key ->
( 0, True )
_ ->
( -1, False )
in
if (handled) then
let
newModel =
handleDigitInput digit model
in
( { newModel | message = Basics.toString msg }, Cmd.none )
else
chainHandler msg model nonDigitHandlers
handleSetPrecisionKeys : Msg -> Model -> List UpdateHandler -> ( Model, Cmd Msg )
handleSetPrecisionKeys msg model remainingHandlers =
let
( digit, handled ) =
case msg of
SetPrecision_7_Key ->
( 7, True )
SetPrecision_8_Key ->
( 8, True )
SetPrecision_9_Key ->
( 9, True )
SetPrecision_4_Key ->
( 4, True )
SetPrecision_5_Key ->
( 5, True )
SetPrecision_6_Key ->
( 6, True )
SetPrecision_1_Key ->
( 1, True )
SetPrecision_2_Key ->
( 2, True )
SetPrecision_3_Key ->
( 3, True )
SetPrecision_0_Key ->
( 0, True )
SetDisplayScientific_Key ->
( 10, True )
_ ->
( -1, False )
in
if (handled) then
let
newModel =
update_Display_Precision digit model
in
( { newModel | message = Basics.toString msg }, Cmd.none )
else
chainHandler msg model remainingHandlers
handleUnaryOperatorKeys : Msg -> Model -> List UpdateHandler -> ( Model, Cmd Msg )
handleUnaryOperatorKeys msg model remainingHandlers =
let
( unary_op, handled ) =
case msg of
LN_Key ->
( natural_log, True )
Square_Root_Key ->
( square_root, True )
Reciprocal_Key ->
( reciprocal, True )
E_to_the_x_Key ->
( e_to_the_x, True )
FRAC_Key ->
( fractional_part, True )
INTG_Key ->
( integral_part, True )
X_Squared_Key ->
( x_squared, True )
N_Factorial_Key ->
( n_factorial, True )
RND_Key ->
( round_function model.displayPrecision, True )
_ ->
( Basics.identity, False )
in
if (handled) then
let
newModel =
unaryOperator unary_op model
in
( { newModel | message = Basics.toString msg }, Cmd.none )
else
chainHandler msg model remainingHandlers
handleBinaryOperatorKeys : Msg -> Model -> List UpdateHandler -> ( Model, Cmd Msg )
handleBinaryOperatorKeys msg model remainingHandlers =
let
( binary_op, handled ) =
case msg of
Divide_Key ->
( y_divided_by_x, True )
Y_toThe_X_Key ->
( y_to_the_x, True )
Multiply_Key ->
( y_times_x, True )
Subtract_Key ->
( y_minus_x, True )
Sum_Key ->
( y_plus_x, True )
_ ->
( y_plus_x, False )
in
if (handled) then
let
newModel =
binaryOperator binary_op model
in
( { newModel | message = Basics.toString msg }, Cmd.none )
else
chainHandler msg model remainingHandlers
defaultHandler : Msg -> Model -> List UpdateHandler -> ( Model, Cmd Msg )
defaultHandler msg model remainingHandlers =
case msg of
_ ->
( { model | message = "UNHANDLED!!" ++ Basics.toString msg }, Cmd.none )
newUpdate : Msg -> Model -> ( Model, Cmd Msg )
newUpdate msg model =
case msg of
KeyMsg code ->
if code == 32 then
-- space bar toggles keyboard shortcuts
( { model
| keyCode = code
, shortcutVisible = not model.shortcutVisible
, message = "Space bar pressed : " ++ Basics.toString (model.shortcutVisible)
}
, Cmd.none
)
else
( { model | keyCode = code }, Cmd.none )
Roll_Down_Key ->
let
rolledStackModel =
roll_Down_Stack model
in
( { rolledStackModel | message = Basics.toString msg }, Cmd.none )
Exchange_X_Y_Key ->
let
newModel =
exchange_X_Y_Regs model
in
( { newModel | message = Basics.toString msg }, Cmd.none )
CLEAR_REG_Key ->
let
newModel =
clearAllRegisters model
in
( { newModel | message = Basics.toString msg }, Cmd.none )
CLEAR_PREFIX_Key ->
( { model
| message = Basics.toString msg
, inputMode = White
}
, Cmd.none
)
Enter_Key ->
let
newModel =
numericalInputTerminated model
in
( { newModel | message = Basics.toString msg }, Cmd.none )
BackSpace_Key ->
let
newModel =
backSpaceReg_X model
in
( { newModel | message = Basics.toString msg }, Cmd.none )
_ ->
List.map UpdateHandler [ handleDigitKeys, handleSetPrecisionKeys, handleUnaryOperatorKeys, handleBinaryOperatorKeys, defaultHandler ]
|> chainHandler msg model