-
-
Notifications
You must be signed in to change notification settings - Fork 126
/
Main.purs
1053 lines (891 loc) · 30.2 KB
/
Main.purs
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
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
module Test.Main (main) where
import Prelude hiding (degree)
import Effect (Effect)
import Effect.Aff (Aff, launchAff_)
import Data.Decimal (fromNumber)
import Data.Either (Either(..))
import Data.Foldable (traverse_, for_, intercalate)
import Data.List (List(..), (:))
import Data.NonEmpty ((:|))
import Data.Map (insert, keys)
import Test.Spec (describe, it)
import Test.Spec.Assertions (shouldEqual, fail)
import Test.Spec.Reporter (consoleReporter)
import Test.Spec.Runner (runSpec)
import Parsing (Position(..), parseErrorMessage, parseErrorPosition)
import Quantities ((./), (.*), milli, nano, meter, inch, hour, minute, kilo,
mile, gram, second, deci, tera, hertz, degree, radian,
day, tonne, euro)
import Insect.Language (BinOp(..), Expression(..), Statement(..))
import Insect.Parser (Dictionary(..), DictEntry, (==>), prefixDict,
normalUnitDict, imperialUnitDict, parseInsect)
import Insect.Environment (StorageType(..), StoredValue(..), Environment,
initialEnvironment)
import Insect.Format (format, fmtPlain)
import Insect.PrettyPrint (pretty)
import Insect (repl)
shouldParseAs ∷ Statement → String → Aff Unit
shouldParseAs expected input =
case parseInsect initialEnvironment input of
Left err →
case parseErrorPosition err of
Position pos →
fail $ "Parse error for input '" <> input <> "': "
<> parseErrorMessage err
<> " at position "
<> show pos.column
Right output →
unless (output == expected) do
fail $ "Unexpected result:\n" <>
"Input: '" <> input <> "'\n" <>
"Output: " <> show output <> "\n" <>
"Expected: " <> show expected <> "\n"
allParseAs ∷ Statement → Array String → Aff Unit
allParseAs expected = traverse_ (shouldParseAs expected)
shouldFail ∷ String → Aff Unit
shouldFail input =
case parseInsect initialEnvironment input of
Left _ → pure unit
Right _ → fail $ "input is expected to throw a parse error: '" <> input <> "'"
expectOutput ∷ Environment → String → String → Aff Unit
expectOutput env expected inp =
let { msg: out } = repl fmtPlain env inp
in
unless (out == expected) do
fail $ "Unexpected result:\n" <>
"Input: '" <> inp <> "'\n" <>
"Output: '" <> out <> "'\n" <>
"Expected: '" <> expected <> "'\n"
prettyPrintCheck ∷ String → Aff Unit
prettyPrintCheck input =
case parseInsect initialEnvironment input of
Left err →
case parseErrorPosition err of
Position pos →
fail $ "Parse error for input '" <> input <> "': "
<> parseErrorMessage err
<> " at position "
<> show pos.column
Right output@(Expression expr) →
shouldParseAs output (format fmtPlain (pretty expr))
_ → fail "Input is not an expression"
main ∷ Effect Unit
main = launchAff_ $ runSpec [consoleReporter] do
-- Helper to construct scalars
let scalar n = Scalar (fromNumber n)
-- Helper to construct quantities
let q s u = BinOp Mul (scalar s) (Unit u)
-- The way we use it, `test` makes more sense than `it`.
let test = it
describe "Parser - Numbers" do
test "Simple numbers" do
allParseAs (Expression (scalar 1.0))
[ "1"
, "1.0"
, " 1 "
, " 1.0000 "
, " +1.0 "
, "+1"
]
allParseAs (Expression (scalar 3.5))
[ "3.5"
, " 3.5 "
, "3.50"
, "+3.50"
]
allParseAs (Expression (scalar 0.2))
[ "0.2"
, " 0.2 "
, "+0.2 "
, ".2"
, "+.2"
]
allParseAs (Expression (Negate (scalar 0.61)))
[ "-0.61"
, "-.61"
, "- .61"
]
shouldParseAs (Expression (scalar 0.05)) ".05"
shouldFail "123.."
shouldFail "0.."
shouldFail ".0."
shouldFail "."
shouldFail ". 2"
shouldFail "..2"
test "Large numbers" do
allParseAs (Expression (scalar 1234567890000000.0))
[ "1234567890000000"
, "1234567890000000.0"
, "+1234567890000000.0"
]
test "Negative numbers" do
shouldParseAs (Expression (Negate (scalar 123.45)))
"-123.45"
test "Exponential notation" do
shouldParseAs (Expression (Negate (scalar 1.3e13)))
"-1.3e13"
shouldParseAs (Expression (scalar 2.7e-3))
"2.7e-3"
shouldFail "123e+"
shouldFail "123e-"
test "Hexadecimal, octal and binary numbers" do
allParseAs (Expression (scalar 106.0))
[ "0x6A"
, "0X6a"
, "0b1101010"
, "0B1101010"
, "0o152"
, "0O152"
]
allParseAs (Expression (scalar 256.0))
[ "0x4p6"
, "0b100p6"
, "0o10p5"
]
allParseAs (Expression (Negate (scalar 3.0)))
[ "-0x3"
, "-0b11"
, "-0o3"
]
allParseAs (Expression (scalar 1.0))
[ "0x2p-1"
, "0b10p-1"
, "0o2p-1"
]
shouldParseAs (Expression (BinOp Add (scalar 12.75) (BinOp Sub (scalar 13.125) (scalar 18.5))))
"0xC.C + 0o15.1 - 0b10.01010p3"
shouldParseAs (Expression (scalar 481.65625)) "0x1E1.a8"
shouldFail "0xG"
shouldFail "0oG"
shouldFail "0oA"
shouldFail "0b2"
shouldFail "0be"
shouldFail "0x1p+"
shouldFail "0x1p-"
shouldFail "0x5.."
describe "Parser - Units (this may take some time)" do
test "Simple" do
allParseAs (Expression (Unit meter))
[ "m"
, " m "
, "meter"
, "meters"
, "metre"
, "metres"
]
allParseAs (Expression (Unit inch))
[ "in"
, " in "
, "inch"
, "inches"
]
allParseAs (Expression (Unit degree))
[ "°"
, " ° "
, "deg"
, "degree"
, "degrees"
]
let
unp ∷ ∀ a. Dictionary a → Array (DictEntry a)
unp (Dictionary dict) = dict
test "All imperial units" do
for_ (unp imperialUnitDict) \(unit ==> unitStrs) →
allParseAs (Expression (Unit unit))
unitStrs
test "All normal units" do
for_ (unp normalUnitDict) \(unit ==> unitStrs) →
allParseAs (Expression (Unit unit))
unitStrs
for_ (unp prefixDict) \(prefix ==> prefixStrs) →
test ("Testing all units with prefix: " <> intercalate ", " prefixStrs) do
for_ (unp normalUnitDict) \(unit ==> unitStrs) →
allParseAs (Expression (Unit (prefix unit)))
((<>) <$> prefixStrs <*> unitStrs)
test "Special cases" do
allParseAs (Expression (Unit day))
[ "d"
, " d "
]
allParseAs (Expression (Unit tonne))
[ "t"
, " t "
]
describe "Parser - Quantities" do
test "Simple" do
allParseAs (Expression (q 2.3 meter))
[ "2.3*m"
, " 2.3 * m "
, "2.3m"
, " 2.3 m "
, "2.3meter"
, "2.3 meter"
, "2.3meters"
, "2.3 metre"
, "2.3metres"
]
allParseAs (Expression (q 5.0 second))
[ "5s"
, "5second"
, "5 seconds"
]
allParseAs (Expression (q 5.0 gram))
[ "5g"
, "5gram"
, "5 grams"
]
allParseAs (Expression (q 10.0 minute))
[ "10minutes"
, "10minute"
, "10min"
]
allParseAs (Expression (q 10.0 mile))
[ "10miles"
, "10mile"
]
allParseAs (Expression (q 10.0 inch))
[ "10inches"
, "10inch"
, "10in"
]
allParseAs (Expression (q 360.0 degree))
[ "360degrees"
, "360degree"
, "360deg"
, "360°"
, "360.0°"
]
allParseAs (Expression (q 1.0 radian))
[ "1.0rad"
, "1.0 radian"
, "1.0 radians"
]
test "SI prefixes" do
allParseAs (Expression (q 2.3 (kilo meter)))
[ "2.3km"
, " 2.3 km "
, " 2.3 kmeter "
, " 2.3 kmeters "
, " 2.3 kmetre "
, " 2.3 kmetres "
]
allParseAs (Expression (q 2.3 (milli meter)))
[ "2.3mm"
, " 2.3 mm "
, " 2.3 mmeter "
, " 2.3 mmeters "
, " 2.3 mmetre "
, " 2.3 mmetres "
]
allParseAs (Expression (q 2.3 (nano gram)))
[ "2.3ng"
, " 2.3 ng "
]
allParseAs (Expression (q 2.3 (kilo minute)))
[ "2.3kmin"
, " 2.3 kmin "
]
allParseAs (Expression (q 2.3 (deci meter)))
[ "2.3dm"
, " 2.3 dm "
]
allParseAs (Expression (q 42.3 (tera hertz)))
[ "42.3THz"
, "42.3Thertz"
]
test "Divisions" do
allParseAs (Expression (BinOp Div (q 2.3 (kilo meter)) (Unit hour)))
[ "2.3km/h"
, "2.30km/h"
, "2.30km÷h"
, "2.30 kilometer per hour"
, "2.30 kilometre per hour"
, "2.30km per h"
]
describe "Parser - Operators" do
test "Factorial" do
allParseAs (Expression (Factorial (scalar 4.0)))
[ "4!"
, "4.0!"
, "4 !"
, " 4 ! "
, "(4)!"
]
allParseAs (Expression (BinOp Pow (Factorial (scalar 5.0)) (scalar 3.0)))
[ "5!^3"
, "5!³"
, "(5!)^3"
]
allParseAs (Expression (Negate (Factorial (scalar 5.0))))
[ "-5!"
, "-(5!)"
]
test "Exponentiation" do
allParseAs (Expression (BinOp Pow (scalar 5.0) (scalar 3.0)))
[ "5^3"
, " 5 ^ 3 "
, " ( 5 ) ^ ( 3 ) "
, " ( ( 5 ) ^ ( 3 ) ) "
, " ( 5 ^ 3 ) "
, "5^(+3)"
, "+5^3"
]
shouldParseAs (Expression (BinOp Pow (scalar 2.0) (BinOp Pow (scalar 3.0) (BinOp Pow (scalar 4.0) (scalar 5.0)))))
"2^3^4^5"
allParseAs (Expression (BinOp Pow (scalar 4.0) (scalar 3.0)))
[ "4^3"
, "4 ^ 3"
, "4**3"
, "4 ** 3"
, "4³"
, "4 ³"
]
allParseAs (Expression (BinOp Pow (Variable "pi") (scalar 2.0)))
[ "pi^2"
, "pi ^ 2"
, "pi**2"
, "pi ** 2"
, "pi²"
, "(pi)²"
]
allParseAs (Expression (Negate (BinOp Pow (scalar 3.0) (scalar 4.0))))
[ "-3^4"
, "-3 ^ 4"
, "-3**4"
, "-3 ** 4"
, "-(3^4)"
]
allParseAs (Expression (BinOp Pow (scalar 3.0) (Negate (scalar 1.4))))
[ "3 ^ (-1.4)"
, "3 ** (-1.4)"
]
shouldFail "³"
shouldFail "³2"
shouldFail "2^"
shouldFail "^2"
test "Multiplication" do
allParseAs (Expression (BinOp Mul (scalar 5.0) (scalar 3.0)))
[ "5*3"
, " 5 * 3 "
, " ( 5 ) * ( 3 ) "
, " ( 5 ) ( 3 ) "
, " ( ( 5 ) * ( 3 ) ) "
, " ( 5 * 3 ) "
, "5(3)"
, "(5)3"
, "5(+3)"
, "+5*3"
]
allParseAs (Expression (BinOp Mul (scalar 5.0) (Negate $ scalar 3.0)))
[ "5*(-3)"
, " 5 * (-3) "
, " ( 5 ) * ( -3 ) "
, " ( ( 5 ) * (-( 3 )) ) "
, " ( 5 * (-3) ) "
, "+5*(-3)"
]
shouldFail "5*"
test "Division" do
allParseAs (Expression (BinOp Div (scalar 5.0) (scalar 3.0)))
[ "5/3"
, "5 per 3"
, " 5 / 3 "
, " ( 5 ) / ( 3 ) "
, " ( ( 5 ) / ( 3 ) ) "
, " ( 5 / 3 ) "
]
shouldFail "5/"
shouldFail "5 per"
test "Modulo" do
allParseAs (Expression (BinOp Mod (scalar 5.0) (scalar 3.0)))
[ "5%3"
, "5 % 3"
, " ( 5 ) % ( 3 ) "
, " ( ( 5 ) % ( 3 ) ) "
, " ( 5 % 3 ) "
]
shouldFail "5%"
shouldFail "%2"
test "Addition" do
allParseAs (Expression (BinOp Add (scalar 5.0) (scalar 3.0)))
[ "5+3"
, " 5 + 3 "
, " ( 5 ) + ( 3 ) "
, " ( ( 5 ) + ( 3 ) ) "
, " ( 5 + 3 ) "
]
shouldFail "3 + "
shouldFail "3 + @"
test "Subtraction" do
allParseAs (Expression (BinOp Sub (scalar 5.0) (scalar 3.0)))
[ "5-3"
, " 5 - 3 "
, " ( 5 ) - ( 3 ) "
, " ( ( 5 ) - ( 3 ) ) "
, " ( 5 - 3 ) "
]
shouldFail "3 - "
test "Unary operators" do
shouldParseAs (Expression (Negate (Variable "x")))
"-x"
shouldParseAs (Expression (Variable "x"))
"+x"
shouldParseAs (Expression (Negate (Negate (Variable "x"))))
"--x"
shouldParseAs (Expression (Negate (Variable "x")))
"++-x"
shouldParseAs (Expression (BinOp Mul (Negate $ scalar 1.0) (Negate $ scalar 2.0)))
"-1 * -2"
shouldParseAs (Expression (BinOp Mul (scalar 1.0) (scalar 2.0)))
"+1 * +2"
shouldParseAs (Expression (BinOp Sub (Negate $ scalar 1.0) (Negate $ scalar 2.0)))
"-1 - -2"
shouldParseAs (Expression (BinOp Sub (scalar 1.0) (scalar 2.0)))
"+1 - +2"
shouldParseAs (Expression (Negate (BinOp Pow (scalar 2.0) (scalar 3.0))))
"-2^3"
shouldParseAs (Expression (Negate (BinOp Pow (scalar 2.0) (scalar 3.0))))
"-2³"
shouldParseAs (Expression (BinOp Pow (scalar 2.0) (Negate $ scalar 3.0)))
"2^-3"
shouldParseAs (Expression (BinOp Pow (scalar 2.0) (scalar 3.0)))
"2^+3"
shouldParseAs (Expression (BinOp Pow (scalar 2.0)
(Negate (BinOp Pow (scalar 3.0)
(Negate (scalar 4.0))))))
"2^-3^-4"
test "Precedence" do
allParseAs (Expression (BinOp Add (q 5.0 meter) (BinOp Mul (q 3.0 inch) (scalar 7.0))))
[ "5m+3in*7"
, "5m+(3in*7)"
, "5m+(3in·7)"
, "5m+(3in⋅7)"
, "5m+3in×7"
, " 5 m + 3 in * 7 "
]
allParseAs (Expression (BinOp Mul (BinOp Add (q 5.0 meter) (q 3.0 inch)) (scalar 7.0)))
[ "(5m+3in)*7"
, "((5m+3in))*7"
, "(((((((5m))+((3in)))))))*((7))"
, " ( 5 m + ( 3 in ) ) * 7 "
]
allParseAs (Expression (BinOp Mul (BinOp Div (scalar 5.0) (scalar 3.0)) (scalar 2.0)))
[ "5/3*2"
, "(5/3)*2"
]
shouldFail "3+*4"
shouldFail "3*/4"
shouldFail "(3+4"
shouldFail "3+4)"
shouldFail "3+("
shouldFail "()"
shouldFail "(3+)4"
test "Multiple division" do
allParseAs (Expression (BinOp Div (BinOp Div (scalar 42.0) (scalar 7.0)) (scalar 3.0)))
[ "42/7/3"
, "(42/7)/3"
, "42 per 7 per 3"
]
test "Multiple division - per precedence" do
allParseAs (Expression (BinOp Div (Unit euro) (BinOp Div (Unit meter) (Unit second))))
[ "euro / (meter / second)"
, "euro / meter per second"
, "euro per (meter per second)"
]
test "Involving units" do
allParseAs (Expression (BinOp Pow (q 3.0 meter) (scalar 2.0)))
[ "(3m)^2"
, "(3.0m)^(2.0)"
]
allParseAs (Expression (BinOp Mul (scalar 3.0) (BinOp Pow (Unit meter) (scalar 2.0))))
[ "3m^2"
, "3.0(m)^(2.0)"
, "3m²"
, "3 m²"
, "3·m²"
, "3⋅m²"
, "3×m²"
, "3·m^(2.0)"
]
allParseAs (Expression (BinOp Div (q 3.0 meter) (Unit second)))
[ "3m/s"
, "3 meter / second"
, "3 meter per second"
, "3 metre / second"
, "3 metre per second"
, "(3m)/s"
]
allParseAs (Expression (BinOp Mul (scalar 3.0) (BinOp Div (Unit meter) (Unit second))))
[ "3·m/s"
, "3*meter / second"
, "3*meter / sec"
, "3*metre / second"
, "3*metre / sec"
]
allParseAs (Expression (BinOp Pow (Unit meter) (Negate $ scalar 1.0)))
[ "m^(-1)"
, "m^(-1.0)"
, "meter^(-1.0)"
, "metre^(-1.0)"
]
describe "Parser - Conversions" do
test "Simple" do
allParseAs (Expression (BinOp ConvertTo (q (2.3) meter) (Unit inch)))
[ "2.3m -> in"
, " 2.3 meters->inches "
, " 2.3 m -> in "
, "2.3m→in"
, " 2.3 m → in "
, "2.3m to in"
, " 2.3 m to in "
]
allParseAs (Expression (BinOp ConvertTo (q 120.0 minute) (Unit hour)))
[ "120min -> h"
, "120minutes -> hours"
]
shouldFail "2.3m->"
shouldFail "-> km"
shouldFail "2.3m →"
shouldFail "2.3m to"
shouldFail "to km"
shouldFail "to"
test "Complex units" do
allParseAs (Expression (BinOp ConvertTo (BinOp Mul (scalar 36.0) (BinOp Div (Unit (kilo meter)) (Unit hour))) (Unit (mile ./ hour))))
[ "36·km/h -> mph"
, " 36 · km / hour->mph"
]
allParseAs (Expression (BinOp ConvertTo (BinOp Div (q 36.0 (kilo meter)) (Unit hour)) (Unit (mile ./ hour))))
[ "36km/h -> mph"
, " 36km / hour->mph "
]
describe "Parser - Identifiers" do
test "Valid and invalid names" do
shouldParseAs (Expression (Variable "x")) "x"
shouldParseAs (Expression (Variable "µ")) "µ"
shouldParseAs (Expression (Variable "pi")) "pi"
shouldParseAs (Expression (Variable "tau")) "tau"
shouldParseAs (Expression (Variable "x_2")) "x_2"
shouldParseAs (Expression (Variable "länge")) "länge"
shouldParseAs (Expression (Variable "_prefixed")) "_prefixed"
shouldParseAs (Expression (Variable "x'")) "x'"
shouldParseAs (Expression (Variable "t''")) "t''"
shouldParseAs (Expression (Variable "to_")) "to_"
shouldFail "xs,as"
shouldFail "hello`"
test "Variables which begin like units" do
shouldParseAs (Expression (Variable "myVariable")) "myVariable"
shouldParseAs (Expression (Variable "density")) "density"
shouldParseAs (Expression (Variable "µg2")) "µg2"
shouldParseAs (Expression (Variable "in2")) "in2"
test "Variables which begin with 'e'" do
allParseAs (Expression (BinOp Mul (scalar 2.0) (Variable "ex")))
[ "2ex"
, "2.0ex"
, "2 ex"
]
allParseAs (Expression (BinOp Mul (scalar 2.0) (Variable "e")))
[ "2e"
, "2.0e"
, "2 e"
]
test "Variables which begin with 'p'" do
allParseAs (Expression (BinOp Mul (scalar 2.0) (Variable "pi")))
[ "0x2pi"
, "0o2.0pi"
, "0b10 pi"
]
allParseAs (Expression (BinOp Mul (scalar 2.0) (Variable "p")))
[ "0b10p"
, "0x2.0p"
, "0o2 p"
]
test "Variables before parenthesis" do
allParseAs (Expression (BinOp Mul (Variable "pi") (scalar 2.0)))
[ "pi(2)"
, "pi*(2)"
, "(pi)(2)"
]
test "Initial environment" do
for_ (keys initialEnvironment.values) \ident →
shouldParseAs (Expression (Variable ident)) ident
describe "Parser - Functions" do
test "Simple" do
allParseAs (Expression (Apply "sin" (q 30.0 degree :| Nil)))
[ "sin(30°)"
, " sin( 30° ) "
, " sin( +30° ) "
]
allParseAs (Expression (Apply "sqrt" (scalar 2.0 :| Nil)))
[ "sqrt(2)"
, " sqrt( 2.0 ) "
, " sqrt( +2.0 ) "
]
allParseAs (Expression (Apply "exp" (Negate (scalar 10.0) :| Nil)))
[ "exp(-10)"
, " exp( -10 ) "
]
allParseAs (Expression (Apply "exp" (scalar 1.0 :| scalar 2.0 : scalar 3.0 : Nil)))
[ "exp(1,2,3)"
, " exp( 1 , 2 , 3 ) "
]
allParseAs (Expression (BinOp Mul (Apply "exp" (scalar 1.0 :| Nil))
(Apply "exp" (scalar 1.0 :| Nil))))
[ "exp(1)exp(1)"
, " exp( 1 ) exp( 1 ) "
]
shouldFail "exp(,)"
shouldFail "exp(1,)"
shouldFail "exp()"
-- The first four are reserved units, 'list' is a reserved keyword, and '_'
-- and 'ans' are reserved variables that refer to the value of the last
-- evaluated line.
let reservedNames = ["m", "meter", "kg", "kilogram", "list", "ans", "_"]
describe "Parser - Variable Assignments" do
test "Simple" do
allParseAs (VariableAssignment "xyz_123" (scalar 1.0))
[ "xyz_123 = 1"
, "xyz_123=1"
, " xyz_123 = 1 "
]
shouldFail "x²=3"
shouldFail "x+y=3"
shouldFail "x+2=3"
shouldFail "3=5"
shouldFail "x="
shouldFail "x=3+"
test "Reserved names" do
for_ reservedNames \name -> shouldFail (name <> "=2")
describe "Parser - Function Assignments" do
test "Simple" do
allParseAs (FunctionAssignment "xyz_123" ("x" :| Nil) (scalar 1.0))
[ "xyz_123(x) = 1"
, "xyz_123(x)=1"
, " xyz_123(x) = 1 "
]
shouldFail "f()=2"
shouldFail "f(x,)=3"
shouldFail "f(2)=3"
shouldFail "f(x)="
test "Multiple arguments" do
allParseAs (FunctionAssignment "f'" ("x" :| "y" : "z" : Nil) (Variable "x"))
[ "f'(x,y,z)=x"
, " f'( x , y , z ) = x "
]
test "Reserved names" do
for_ reservedNames \name -> shouldFail (name <> "(x)=2")
describe "Parser - Pretty print function" do
test "Simple" do
allParseAs (PrettyPrintFunction "cos")
[ "cos"
, " cos"
, "cos "
]
let pretty' str =
case parseInsect initialEnvironment str of
Right (Expression expr) → format fmtPlain (pretty expr)
_ → "Error"
let equalPretty out inp =
out `shouldEqual` (pretty' inp)
describe "Pretty printer" do
test "Consistency" do
prettyPrintCheck "-2.3e-12387"
prettyPrintCheck "2.3e-12387"
prettyPrintCheck "18379173"
prettyPrintCheck "2+3"
prettyPrintCheck "2+3*5"
prettyPrintCheck "-3^4+2/(4+2*3)"
prettyPrintCheck "1-2-3-4-(5-6-7)"
prettyPrintCheck "1/2/3/4/(5/6/7)"
prettyPrintCheck "kg"
prettyPrintCheck "2m/s"
prettyPrintCheck "a+b*c^d-e*f"
prettyPrintCheck "sin(x)^3"
prettyPrintCheck "sin(cos(atanh(x)+2))^3"
prettyPrintCheck "2^3^4^5"
prettyPrintCheck "(2^3)^(4^5)"
prettyPrintCheck "sqrt(1.4^2 + 1.5^2) * cos(pi/3)^2"
prettyPrintCheck "40kg * 9.8m/s^2 * 150cm"
prettyPrintCheck "4/3 * pi * r³"
prettyPrintCheck "vol * density -> kg"
prettyPrintCheck "atan(30cm / 2m)"
prettyPrintCheck "500km/day -> km/h"
prettyPrintCheck "1mrad -> °"
prettyPrintCheck "52weeks -> days"
prettyPrintCheck "5in + 2ft -> cm"
prettyPrintCheck "6Mbit/s * 1.5h -> GB"
prettyPrintCheck "2d"
prettyPrintCheck "5t"
prettyPrintCheck "länge * x_2 * µ * _prefixed"
prettyPrintCheck "2m^3"
prettyPrintCheck "(2m)^3"
prettyPrintCheck "(2m)^(3kg)"
prettyPrintCheck "(2m)^(3kg)^((4in)^(5ft))^(6s)"
prettyPrintCheck "-sqrt(-30m^3)"
prettyPrintCheck "-3^4"
prettyPrintCheck "(-3)^4"
prettyPrintCheck "5!³"
prettyPrintCheck "2^3!"
prettyPrintCheck "-3!"
prettyPrintCheck "(-3)!"
prettyPrintCheck "sin(2,3,4)"
test "Format" do
equalPretty "2 + 3" "2+3"
equalPretty "2 × 3" "2*3"
equalPretty "2^3" "2^3"
equalPretty "2 km" "2km"
equalPretty "sin(30°)" "sin(30°)"
equalPretty "2 × 3 × 4" "2*3*4"
equalPretty "2 × 3 × 4" "2*(3*4)"
equalPretty "2 + 3 + 4" "2+3+4"
equalPretty "2 + 3 + 4" "2+(3+4)"
equalPretty "atan(30 cm / 2 m)" "atan(30cm / 2m)"
equalPretty "1 mrad ➞ °" "1mrad -> °"
equalPretty "2 km + 2 cm ➞ in" "2km+2cm -> in"
equalPretty "2^3 + 4^5" "2^3 + 4^5"
equalPretty "2^3 - 4^5" "2^3 - 4^5"
equalPretty "2^3 × 4^5" "2^3 * 4^5"
equalPretty "2 × 3 + 4 × 5" "2 * 3 + 4 * 5"
equalPretty "2 × (3 / 4)" "2 * 3 / 4"
equalPretty "123.123 × km^2 / s^2" "123.123 km^2 / s^2"
equalPretty "sin(2, 3, 4)" " sin( 2 , 3 , 4 ) "
let expectOutput' = expectOutput initialEnvironment
describe "Integration tests" do
test "Simple input" do
expectOutput' "3 m" "3m"
expectOutput' "3 m" " 3.0 meter "
test "Function using current environment" do
let env1 = initialEnvironment
env2 = (repl fmtPlain env1 "a = 2").newEnv
env3 = (repl fmtPlain env2 "f(x) = a * x").newEnv
env4 = (repl fmtPlain env3 "a = 3").newEnv
expectOutput env4 "3" "f(1)"
test "Square, cube and other exponentiation operators" do
expectOutput' "18" "3²*2"
expectOutput' "18" "3² 2"
expectOutput' "18" "3²·2"
expectOutput' "54" "3³*2"
expectOutput' "54" "3³(2)"
expectOutput' "9" "(1+2)²"
expectOutput' "12.5664" "2²pi"
expectOutput' "12.5664" "2² pi"
expectOutput' "12.5664" "2²·pi"
expectOutput' "500 cm·m" "5m² to cm·m"
expectOutput' "32" "2⁵"
expectOutput' "-4" "-4¹"
expectOutput' "0.5" "2⁻¹"
expectOutput' "0.25" "2⁻²"
expectOutput' "0.00001" "10⁻⁵"
shouldFail "32⁻"
test "Conversions" do
expectOutput' "5.08 cm" "2in to cm"
expectOutput' "500 m·cm" "5m^2 -> m*cm"
expectOutput' "500 cm·m" "5m^2 -> cm*m"
expectOutput' "0.1 MB/s" "1 kB / 10 ms -> MB/s"
test "Implicit multiplication" do
let myEnv =
{ values: insert "x" (StoredValue UserDefined (5.0 .* meter)) initialEnvironment.values
, functions: initialEnvironment.functions
}
expectOutput myEnv "5 m" "x"
expectOutput myEnv "10 m" "2x"
expectOutput myEnv "10 m" "2 x"
expectOutput myEnv "25 m²" "x x"
expectOutput myEnv "25 m²" "x²"
expectOutput myEnv "Unknown identifier: x2" "x2"
test "Function inverses" do
expectOutput' "0.1234" "sin(asin(0.1234))"
expectOutput' "0.1234" "cos(acos(0.1234))"
expectOutput' "0.1234" "tan(atan(0.1234))"
expectOutput' "0.1234" "sinh(asinh(0.1234))"
expectOutput' "1.1234" "cosh(acosh(1.1234))"
expectOutput' "0.1234" "tanh(atanh(0.1234))"
expectOutput' "0.1234" "log(exp(0.1234))"
expectOutput' "0.1234" "asin(sin(0.1234))"
expectOutput' "0.1234" "acos(cos(0.1234))"
expectOutput' "0.1234" "atan(tan(0.1234))"
expectOutput' "0.1234" "asinh(sinh(0.1234))"
expectOutput' "1.1234" "acosh(cosh(1.1234))"
expectOutput' "0.1234" "atanh(tanh(0.1234))"
expectOutput' "0.1234" "exp(log(0.1234))"
test "Temperature conversions" do
expectOutput' "284.65 K" "fromCelsius(11.5)"
expectOutput' "304.983 K" "fromFahrenheit(89.3)"
expectOutput' "-273.15" "toCelsius(0 K)"
expectOutput' "-405.67" "toFahrenheit(30 K)"
expectOutput' "100" "toCelsius(fromCelsius(100))"
expectOutput' "100" "toFahrenheit(fromFahrenheit(100))"
expectOutput' "123 K" "fromCelsius(toCelsius(123 K))"
expectOutput' "123 K" "fromFahrenheit(toFahrenheit(123 K))"
test "Other functions" do
expectOutput' "2" "sqrt(4)"
expectOutput' "5" "log10(100000)"
expectOutput' "15" "log(e^15)"
expectOutput' "15" "ln(e^15)"
expectOutput' "4" "ceil(3.1)"
expectOutput' "3" "floor(3.9)"
expectOutput' "4" "round(3.9)"
expectOutput' "3" "round(3.1)"
test "Unit simplification" do
expectOutput' "36000 Mbit" "5Mbit/s * 2h"
expectOutput' "2500 cm²" "5cm · 5m"
expectOutput' "0.2 km" "120km/h*6s"
test "'ans' and '_'" do
let env1 = initialEnvironment
env2 = (repl fmtPlain env1 "5").newEnv
env3 = (repl fmtPlain env2 "_ * 20").newEnv
expectOutput env3 "1" "ans / 100"
test "Examples" do
expectOutput' "1080" "1920/16*9"
expectOutput' "4294967296" "2^32"
expectOutput' "0.512957" "sqrt(1.4^2 + 1.5^2) * cos(pi/3)^2"
expectOutput' "2.5 min" "2min + 30s"
expectOutput' "150 s" "2min + 30s -> sec"
expectOutput' "904779000000 km³" "4/3 * pi * (6000km)³"
expectOutput' "588 m²·kg/s²" "40kg * 9.8m/s^2 * 150cm"
expectOutput' "0.5" "sin(30°)"
expectOutput' "26.8224 m/s" "60mph -> m/s"