This repository was archived by the owner on Dec 16, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRpnInputDomain.fs
526 lines (480 loc) · 21.9 KB
/
RpnInputDomain.fs
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
namespace GraphingCalculator
// Using types and functions from Conventional Domain
// that are common to the RPN Domain.
open GraphingCalculator.ConventionalDomain
module RpnDomain =
// -------------Types
type Stack = StackContents of Number list
type StackOperation =
| Push /// Pushes a value onto the stack.
| Pop /// Pop a value from the stack and return it and the new stack as a tuple
| Drop /// Removes the top value from the stack (if it exists).
| Duplicate /// Pushes a duplicate copy of the top value onto the stack.
| ClearStack /// Clears the entire stack contents.
| Swap /// Swaps the two top values on the stack.
type CalculatorOp =
| StackOp of StackOperation
| MathOp of CalculatorMathOp
// types to describe errors
type OperationError =
| DivideByZero of Stack
| UnknownError of Stack
| EmptyStack of Stack
| TooFewArguments of Stack
type OperationResult =
| Success of Stack
| Failure of OperationError
// Data associated with each state
type ReadyStateData = {stack : Stack}
type DigitAccumulatorStateData = {digits:DigitAccumulator; stack : Stack}
type ErrorStateData = {error:OperationError}
type CalculatorInput =
| Op of CalculatorOp
| Input of ConventionalDomain.CalculatorInput
| Enter
// States
type CalculatorState =
| ReadyState of ReadyStateData
| DigitAccumulatorState of DigitAccumulatorStateData
| DecimalAccumulatorState of DigitAccumulatorStateData
| ErrorState of ErrorStateData
type Calculate = CalculatorInput * CalculatorState -> CalculatorState
// Services used by the calculator itself
type DoStackOperation = StackOperation * Number option * Stack -> OperationResult
type DoMathOperation = CalculatorMathOp * Stack -> OperationResult
type GetNumberFromAccumulator = DigitAccumulatorStateData -> Number
type GetDisplayFromStack = CalculatorState -> string
type GetDisplayFromRpnState = CalculatorState -> string
type RpnServices = {
doStackOperation :DoStackOperation
doMathOperation :DoMathOperation
accumulateZero :AccumulateZero
accumulateNonZeroDigit :AccumulateNonZeroDigit
accumulateSeparator :AccumulateSeparator
getNumberFromAccumulator :GetNumberFromAccumulator
getDisplayFromStack :GetDisplayFromStack
getDisplayFromRpnState :GetDisplayFromRpnState
}
module StackOperations =
open RpnDomain
/// Push a value on the stack
let push x (StackContents contents) = StackContents (x::contents)
/// Pop a value from the stack and return it and the new stack as a tuple
let pop (StackContents contents) =
match contents with
| top::rest ->
let newStack = StackContents rest
(top,newStack)
| [] -> (nan, (StackContents contents))
/// Removes the top value from the stack (if it exists).
let drop (StackContents contents) =
match contents with
| top::rest -> StackContents rest
| [] -> StackContents contents
/// Duplicate the top value on the stack
let duplicate (StackContents contents) =
match contents with
| top::rest -> push top (StackContents contents)
| [] -> StackContents contents
/// Swap the top two values
let swap (StackContents contents) =
match contents with
| top::next::rest -> StackContents (next::top::rest)
| [_] | [] -> StackContents contents
let clearStack = StackContents []
module RpnImplementation =
open RpnDomain
let accumulateNonZeroDigit services digit accumulatorData =
let digits = accumulatorData.digits
let newDigits = services.accumulateNonZeroDigit (digit, digits)
let newAccumulatorData = { accumulatorData with digits = newDigits }
newAccumulatorData // return
let accumulateZero services accumulatorData =
let digits = accumulatorData.digits
let newDigits = services.accumulateZero digits
let newAccumulatorData = { accumulatorData with digits = newDigits }
newAccumulatorData // return
let accumulateSeparator services accumulatorData =
let digits = accumulatorData.digits
let newDigits = services.accumulateSeparator digits
let newAccumulatorData = { accumulatorData with digits = newDigits }
newAccumulatorData // return
let doStackOperation services state input =
match state with
| ReadyState r ->
let stack = r.stack
let newStack = services.doStackOperation (input, None ,stack)
match newStack with
| Success stackData -> ReadyState {stack = stackData}
| Failure errorData -> ErrorState {error = errorData}
| DigitAccumulatorState d ->
let stack = d.stack
let number = services.getNumberFromAccumulator d
let newStack = services.doStackOperation (input, Some(number), stack)
match newStack with
| Success stackData -> ReadyState {stack = stackData}
| Failure errorData -> ErrorState {error = errorData}
| DecimalAccumulatorState d ->
let stack = d.stack
let number = services.getNumberFromAccumulator d
let newStack = services.doStackOperation (input, Some(number), stack)
match newStack with
| Success stackData -> ReadyState {stack = stackData}
| Failure errorData -> ErrorState {error = errorData}
| ErrorState e ->
let stack =
match e.error with
| DivideByZero x
| UnknownError x
| EmptyStack x
| TooFewArguments x -> x
let newStack = services.doStackOperation (input, None, stack)
match newStack with
| Success stackData -> ReadyState {stack = stackData}
| Failure errorData -> ErrorState {error = errorData}
let doMathOperation services stack input =
let result = services.doMathOperation (input,stack)
match result with
| Success x -> ReadyState {stack = x}
| Failure x -> ErrorState {error = x}
let handleReadyState services stack input =
match input with
| Op op ->
match op with
| StackOp s ->
match s with
| ClearStack -> ReadyState {stack = StackOperations.clearStack}
| Push -> ReadyState {stack = stack} // stay in ReadyState
| Pop
| Drop
| Duplicate
| Swap -> doStackOperation services (ReadyState {stack = stack}) s
| MathOp m ->
match m with
| Add
| Subtract
| Multiply
| Divide
| Inverse
| Percent
| Root
| ChangeSign -> doMathOperation services stack m
| MemoryAdd -> ReadyState {stack = stack} // not used in RPN mode
| MemorySubtract -> ReadyState {stack = stack} // not used in RPN mode
| Input i ->
match i with
| Zero -> ReadyState {stack = stack} // stay in ReadyState
| Digit d ->
let newData = (accumulateNonZeroDigit services d { digits = ""; stack = stack } )
DigitAccumulatorState newData
| DecimalSeparator ->
{digits=""; stack = stack}
|> accumulateSeparator services
|> DecimalAccumulatorState // transition to AccumulatorState
| Equals -> ReadyState {stack = stack} // not used in RPN mode
| Clear -> ReadyState {stack = stack} // not used in RPN mode
| ClearEntry -> ReadyState {stack = stack} // ToDO
| Back -> ReadyState {stack = stack} // ToDO
| ConventionalDomain.MathOp op -> ReadyState {stack = stack} // not used in RPN mode
| MemoryStore
| MemoryClear
| MemoryRecall -> ReadyState {stack = stack} // not used in RPN mode
| Enter -> ReadyState {stack = stack}
let handleDigitAccumulatorState services stateData input =
match input with
| Op op ->
match op with
| StackOp stackOp ->
let state =
match stateData with
| DigitAccumulatorState x -> DigitAccumulatorState x
| DecimalAccumulatorState x -> DecimalAccumulatorState x
| ReadyState x -> ErrorState {error = (UnknownError x.stack)}
| ErrorState e -> ErrorState e
doStackOperation services state stackOp
| MathOp mathOp ->
let stack =
match stateData with
| DigitAccumulatorState x -> x.stack
| DecimalAccumulatorState x -> x.stack
| ReadyState x -> x.stack
| ErrorState e ->
match e.error with
| DivideByZero x
| UnknownError x
| EmptyStack x
| TooFewArguments x -> x
doMathOperation services stack mathOp
| Input i ->
let digits =
match stateData with
| DigitAccumulatorState x -> {digits = x.digits; stack = x.stack}
| DecimalAccumulatorState x -> {digits = x.digits; stack = x.stack}
| ReadyState x -> {digits = "0"; stack = x.stack}
| ErrorState e ->
match e.error with
| DivideByZero x
| UnknownError x
| EmptyStack x
| TooFewArguments x -> {digits = "0"; stack = x}
match i with
| Zero -> DigitAccumulatorState (accumulateZero services digits)
| Digit d -> DigitAccumulatorState (accumulateNonZeroDigit services d digits)
| DecimalSeparator -> DecimalAccumulatorState (accumulateSeparator services digits)
// ToDO
| ClearEntry -> DigitAccumulatorState digits
| Back -> DigitAccumulatorState digits
// not used in RPN mode
| ConventionalDomain.MathOp _
| Equals
| Clear
| MemoryStore
| MemoryClear
| MemoryRecall -> DigitAccumulatorState digits
| Enter -> doStackOperation services stateData Push
let handleDecimalAccumulatorState services stateData input =
match input with
| Op op ->
match op with
| StackOp stackOp ->
let state =
match stateData with
| DigitAccumulatorState x -> DigitAccumulatorState x
| DecimalAccumulatorState x -> DecimalAccumulatorState x
| ReadyState x -> ErrorState {error = (UnknownError x.stack)}
| ErrorState e -> ErrorState e
doStackOperation services state stackOp
| MathOp mathOp ->
let stack =
match stateData with
| DigitAccumulatorState x -> x.stack
| DecimalAccumulatorState x -> x.stack
| ReadyState x -> x.stack
| ErrorState e ->
match e.error with
| DivideByZero x
| UnknownError x
| EmptyStack x
| TooFewArguments x -> x
doMathOperation services stack mathOp
| Input i ->
let digits =
match stateData with
| DigitAccumulatorState x -> {digits = x.digits; stack = x.stack}
| DecimalAccumulatorState x -> {digits = x.digits; stack = x.stack}
| ReadyState x -> {digits = ""; stack = x.stack}
| ErrorState e ->
match e.error with
| DivideByZero x
| UnknownError x
| EmptyStack x
| TooFewArguments x -> {digits = ""; stack = x}
match i with
| Zero -> DecimalAccumulatorState (accumulateZero services digits)
| Digit d -> DecimalAccumulatorState (accumulateNonZeroDigit services d digits)
| DecimalSeparator -> DecimalAccumulatorState digits
// ToDO
| ClearEntry -> DecimalAccumulatorState digits
| Back -> DecimalAccumulatorState digits
// not used in RPN mode
| ConventionalDomain.MathOp _
| Equals
| Clear
| MemoryStore
| MemoryClear
| MemoryRecall -> DecimalAccumulatorState digits
| Enter -> doStackOperation services stateData Push
let handleErrorState stateData input =
match input with
| Op _
| Input _ //-> ErrorState stateData
| Enter ->
let errorStack =
match stateData.error with
| DivideByZero e
| UnknownError e
| EmptyStack e
| TooFewArguments e-> e
ReadyState {stack = errorStack}
let createCalculate (services:RpnServices) : Calculate =
// create some local functions with partially applied services
let handleReady = handleReadyState services
let handleDigitAccumulator = handleDigitAccumulatorState services
let handleDecimalAccumulator = handleDecimalAccumulatorState services
let handleError = handleErrorState
fun (input,state) ->
match state with
| ReadyState stateData ->
handleReady stateData.stack input
| DigitAccumulatorState stateData ->
handleDigitAccumulator (DigitAccumulatorState stateData) input
| DecimalAccumulatorState stateData ->
handleDecimalAccumulator (DecimalAccumulatorState stateData) input
| ErrorState stateData ->
handleError stateData input
module RpnServices =
open RpnDomain
open StackOperations
let accumulateZero maxLen :AccumulateZero =
fun accumulator -> CalculatorServices.appendToAccumulator maxLen accumulator "0"
let accumulateNonZeroDigit maxLen :AccumulateNonZeroDigit =
fun (digit, accumulator) ->
// determine what character should be appended to the display
let appendCh =
match digit with
| One -> "1"
| Two -> "2"
| Three-> "3"
| Four -> "4"
| Five -> "5"
| Six-> "6"
| Seven-> "7"
| Eight-> "8"
| Nine-> "9"
CalculatorServices.appendToAccumulator maxLen accumulator appendCh
let accumulateSeparator maxLen :AccumulateSeparator =
fun accumulator ->
let appendCh =
if accumulator = "" then "0." else "."
CalculatorServices.appendToAccumulator maxLen accumulator appendCh
let getNumberFromAccumulator :GetNumberFromAccumulator =
fun accumulatorStateData ->
let digits = accumulatorStateData.digits
match System.Double.TryParse digits with
| true, d -> d
| false, _ -> 0.0
let doMathOperation :DoMathOperation = fun (op,stack) ->
let stackContents = match stack with | StackContents s -> s
match stackContents.Length with
| x when x = 0 -> Failure (EmptyStack stack)
| x when x = 1 ->
let f1, toStack = pop stack
match op with
| Percent ->
let calc = f1/100.
Success (push calc toStack)
| Root -> Failure (TooFewArguments stack)
| Inverse when f1 = 0. -> Failure (DivideByZero stack)
| Inverse -> Failure (EmptyStack stack)
| Add -> Failure (TooFewArguments stack)
| Subtract -> Failure (TooFewArguments stack)
| Multiply -> Failure (TooFewArguments stack)
| Divide -> Failure (TooFewArguments stack)
| ChangeSign ->
let calc = f1 * -1.
Success (push calc toStack)
| _ -> Failure (UnknownError stack)
| x ->
let f1, toStack1 = pop stack
let f2, toStack2 = pop toStack1
match op with
| Percent ->
let calc = f1/100.
Success (push calc toStack1)
| Root ->
let calc = (System.Math.Pow(f1,f2))
Success (push calc toStack2)
| Inverse when f1 = 0. -> Failure (DivideByZero stack)
| Inverse ->
let calc = f2 / f1
Success (push calc toStack2)
| Add ->
let calc = f1 + f2
Success (push calc toStack2)
| Subtract ->
let calc = f1 - f2
Success (push calc toStack2)
| Multiply ->
let calc = f1 * f2
Success (push calc toStack2)
| Divide ->
let calc = f1 / f2
Success (push calc toStack2)
| ChangeSign ->
let calc = f1 * -1.
Success (push calc toStack1)
| _ -> Failure (UnknownError stack)
let doStackOperation :DoStackOperation = fun (op,number,StackContents numberList) ->
match numberList.Length with
| x when x = 0 ->
match op with
| Push ->
match number with
| Some n -> Success (push n (StackContents numberList))
| None -> Success (push 0. (StackContents numberList))
| ClearStack -> Success (StackContents [])
| Drop -> Failure (EmptyStack (StackContents numberList))
| Duplicate -> Failure (EmptyStack (StackContents numberList))
| Swap -> Failure (TooFewArguments (StackContents numberList))
| _ -> Failure (UnknownError (StackContents numberList))
| x when x = 1 ->
let f1 = numberList.Head
match op with
| Push ->
match number with
| Some n -> Success (push n (StackContents numberList))
| None -> Success (push 0. (StackContents numberList))
| Drop -> Success (StackContents [])
| Duplicate ->
let newStack = f1::numberList
Success (StackContents newStack)
| ClearStack -> Success (StackContents [])
| Swap -> Failure (TooFewArguments (StackContents numberList))
| _ -> Failure (UnknownError (StackContents numberList))
| x ->
let f1 = numberList.[0]
let f2 = numberList.[1]
match op with
| Push ->
match number with
| Some n -> Success (push n (StackContents numberList))
| None -> Success (push 0. (StackContents numberList))
| Drop ->
let newStack = match numberList with | x::rest -> rest | [] -> []
Success (StackContents newStack)
| Duplicate ->
let newStack = f1::numberList
Success (StackContents newStack)
| Swap ->
let newStack = f2::(f1::(List.skip 2 numberList))
Success (StackContents newStack)
| ClearStack -> Success (StackContents [])
| _ -> Failure (UnknownError (StackContents numberList))
let getDisplayFromStack :GetDisplayFromStack =
// helper
let floatToString = sprintf ">>%g\n"
let stackStringFrom numberList =
match numberList with
| [] -> "Empty \nStack"
| x::rest -> List.fold (fun acc x -> (floatToString x) + acc) "" (List.rev numberList)
fun rpnState ->
match rpnState with
| ReadyState data ->
match data.stack with
| StackContents numberList -> stackStringFrom numberList
| DigitAccumulatorState stateData
| DecimalAccumulatorState stateData ->
let numberList = match stateData.stack with StackContents sc -> sc
let numberFromAccumulator = stateData |> getNumberFromAccumulator
let displayString = numberFromAccumulator.ToString() + " \n" + (stackStringFrom numberList)
displayString
| ErrorState stateData -> stateData.error.ToString()
let getDisplayFromRpnState :GetDisplayFromRpnState =
// helper
let floatToString = sprintf ">>%g"
fun rpnState ->
match rpnState with
| ReadyState data -> "Ready"
| DigitAccumulatorState stateData
| DecimalAccumulatorState stateData -> stateData.digits
| ErrorState stateData -> stateData.error.ToString()
let createServices () = {
accumulateNonZeroDigit = accumulateNonZeroDigit (10)
accumulateZero = accumulateZero (15)
accumulateSeparator = accumulateSeparator (15)
doMathOperation = doMathOperation
doStackOperation = doStackOperation
getNumberFromAccumulator = getNumberFromAccumulator
getDisplayFromStack = getDisplayFromStack
getDisplayFromRpnState = getDisplayFromRpnState}
(**)