File tree 3 files changed +44
-3
lines changed
Sources/SLispCore/builtins
3 files changed +44
-3
lines changed Original file line number Diff line number Diff line change @@ -246,7 +246,9 @@ class Core: Builtins {
246
246
throw LispError . runtime ( msg: " 'doc' requires the argument to be a function " )
247
247
}
248
248
249
- print ( docstring ?? " " )
249
+ if docstring != nil {
250
+ return . string( docstring!)
251
+ }
250
252
251
253
return . nil
252
254
}
@@ -653,5 +655,26 @@ class Core: Builtins {
653
655
return !x
654
656
}
655
657
}
658
+
659
+ // MARK: integer
660
+ addBuiltin ( " integer " , docstring: " " ) { args, parser, env throws in
661
+ if args. count != 1 {
662
+ throw LispError . runtime ( msg: " 'integer' expects 1 argument. " )
663
+ }
664
+
665
+ switch args [ 0 ] {
666
+ case . number( let num) :
667
+ return . number( . integer( num. intValue ( ) ) )
668
+ case . string( let str) :
669
+ let v = Int ( str)
670
+ if v != nil {
671
+ return . number( . integer( v!) )
672
+ } else {
673
+ return . nil
674
+ }
675
+ default :
676
+ throw LispError . runtime ( msg: " 'integer' expects a number or string argument. " )
677
+ }
678
+ }
656
679
}
657
680
}
Original file line number Diff line number Diff line change @@ -74,6 +74,19 @@ class MathBuiltins : Builtins {
74
74
return . number( . integer( n) )
75
75
}
76
76
77
+
78
+ addBuiltin ( " sqrt " , docstring: " " ) { args, parser, env throws in
79
+ if args. count != 1 {
80
+ throw LispError . runtime ( msg: " 'sqrt' requires 1 argument " )
81
+ }
82
+
83
+ guard case let . number( n) = args [ 0 ] else {
84
+ throw LispError . runtime ( msg: " 'sqrt' requires a number argument " )
85
+ }
86
+
87
+ return . number( . float( sqrt ( n. floatValue ( ) ) ) )
88
+ }
89
+
77
90
return builtins
78
91
}
79
92
}
Original file line number Diff line number Diff line change 55
55
(if (! x)
56
56
(if (|| (empty? description) (nil? description))
57
57
(throw :testAssertionError)
58
- (throw :testAssertionError (first description)))))
58
+ (do
59
+ (print (first description))
60
+ (throw :testAssertionError (first description))))))
59
61
60
62
(defn assertEqual
61
63
(x y & description)
62
- (apply assert (concat (== x y) description)))
64
+ (let (desc (if (|| (nil? description) (empty? description))
65
+ (str "Assertion failure: " x " is not equal to " y)
66
+ description))
67
+ (apply assert (concat (== x y) desc))))
63
68
64
69
(defn assertNotEqual
65
70
(x y & description)
You can’t perform that action at this time.
0 commit comments