Skip to content

Commit e596ef9

Browse files
authored
Unchecked arithmetic (#452)
* Unchecked arithmetic * Fix line and column metadata * Fetch metadata from various places as needed * More * Docstrings * And the rest * Pin coverage since coverage>=5.0 is not supported by coveralls. TheKevJames/coveralls-python#203
1 parent f02c3db commit e596ef9

File tree

2 files changed

+151
-18
lines changed

2 files changed

+151
-18
lines changed

src/basilisp/core.lpy

Lines changed: 149 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -865,37 +865,41 @@
865865
"Sum the arguments together. If no arguments given, returns 0."
866866
([] 0)
867867
([x] x)
868-
([x & args]
869-
(if (seq (rest args))
870-
(recur (operator/add x (first args)) (rest args))
871-
(operator/add x (first args)))))
868+
([x y] (operator/add x y))
869+
([x y & args]
870+
(if (seq args)
871+
(recur (operator/add x y) (first args) (rest args))
872+
(operator/add x y))))
872873

873874
(defn -
874875
"Subtract the arguments. If one argument given, returns the negation
875876
of that argument."
876877
([x] (operator/neg x))
877-
([x & args]
878-
(if (seq (rest args))
879-
(recur (operator/sub x (first args)) (rest args))
880-
(operator/sub x (first args)))))
878+
([x y] (operator/sub x y))
879+
([x y & args]
880+
(if (seq args)
881+
(recur (operator/sub x y) (first args) (rest args))
882+
(operator/sub x y))))
881883

882884
(defn *
883885
"Multiply the arguments. If no arguments given, returns 1."
884886
([] 1)
885887
([x] x)
886-
([x & args]
887-
(if (seq (rest args))
888-
(recur (operator/mul x (first args)) (rest args))
889-
(operator/mul x (first args)))))
888+
([x y] (operator/mul x y))
889+
([x y & args]
890+
(if (seq args)
891+
(recur (operator/mul x y) (first args) (rest args))
892+
(operator/mul x y))))
890893

891894
(defn /
892895
"Divide the arguments. If no arguments given, returns the inverse of
893896
the argument."
894897
([x] (basilisp.lang.runtime/divide 1 x))
895-
([x & args]
896-
(if (seq (rest args))
897-
(recur (basilisp.lang.runtime/divide x (first args)) (rest args))
898-
(basilisp.lang.runtime/divide x (first args)))))
898+
([x y] (basilisp.lang.runtime/divide x y))
899+
([x y & args]
900+
(if (seq args)
901+
(recur (basilisp.lang.runtime/divide x y) (first args) (rest args))
902+
(basilisp.lang.runtime/divide x y))))
899903

900904
(defn mod
901905
"Returns the modulo of num and div."
@@ -1325,6 +1329,135 @@
13251329
[x]
13261330
(python/int x))
13271331

1332+
;;;;;;;;;;;;;;;;;;;;;;;;;;
1333+
;; Unchecked Arithmetic ;;
1334+
;;;;;;;;;;;;;;;;;;;;;;;;;;
1335+
1336+
(defmacro ^:private defcopy
1337+
"Copy the Var `var` into a new Var as `name`.
1338+
1339+
Append the given docstring `doc` to the end of the new Var's `doc`."
1340+
([name var]
1341+
`(defcopy ~name nil ~var))
1342+
([name doc var]
1343+
(let [orig-var (basilisp.lang.runtime/resolve-var var *ns*)
1344+
orig-var-meta (meta orig-var)
1345+
var-doc (:doc orig-var-meta)
1346+
new-doc (if doc
1347+
(str var-doc "\n\n" doc)
1348+
var-doc)
1349+
vname (vary-meta name
1350+
assoc
1351+
:doc new-doc
1352+
:arglists (list 'quote (:arglists orig-var-meta)))]
1353+
`(def ~vname ~var))))
1354+
1355+
(defcopy unchecked-add
1356+
"Same as '+. Python integers are unlimited precision, so unchecked arithmetic
1357+
is only provided for compatibility with platforms without unlimited precision
1358+
integers."
1359+
+)
1360+
(defcopy unchecked-add-int
1361+
"Same as '+. Python integers are unlimited precision, so unchecked arithmetic
1362+
is only provided for compatibility with platforms without unlimited precision
1363+
integers."
1364+
+)
1365+
(defcopy unchecked-subtract
1366+
"Same as '-. Python integers are unlimited precision, so unchecked arithmetic
1367+
is only provided for compatibility with platforms without unlimited precision
1368+
integers."
1369+
-)
1370+
(defcopy unchecked-subtract-int
1371+
"Same as '-. Python integers are unlimited precision, so unchecked arithmetic
1372+
is only provided for compatibility with platforms without unlimited precision
1373+
integers."
1374+
-)
1375+
(defcopy unchecked-multiply
1376+
"Same as '*. Python integers are unlimited precision, so unchecked arithmetic
1377+
is only provided for compatibility with platforms without unlimited precision
1378+
integers."
1379+
*)
1380+
(defcopy unchecked-multiply-int
1381+
"Same as '*. Python integers are unlimited precision, so unchecked arithmetic
1382+
is only provided for compatibility with platforms without unlimited precision
1383+
integers."
1384+
*)
1385+
(defcopy unchecked-divide-int
1386+
"Same as '/. Python integers are unlimited precision, so unchecked arithmetic
1387+
is only provided for compatibility with platforms without unlimited precision
1388+
integers."
1389+
/)
1390+
1391+
(defcopy unchecked-inc
1392+
"Same as 'inc. Python integers are unlimited precision, so unchecked arithmetic
1393+
is only provided for compatibility with platforms without unlimited precision
1394+
integers."
1395+
inc)
1396+
(defcopy unchecked-inc-int
1397+
"Same as 'inc. Python integers are unlimited precision, so unchecked arithmetic
1398+
is only provided for compatibility with platforms without unlimited precision
1399+
integers."
1400+
inc)
1401+
(defcopy unchecked-dec
1402+
"Same as 'dec. Python integers are unlimited precision, so unchecked arithmetic
1403+
is only provided for compatibility with platforms without unlimited precision
1404+
integers."
1405+
dec)
1406+
(defcopy unchecked-dec-int
1407+
"Same as 'dec. Python integers are unlimited precision, so unchecked arithmetic
1408+
is only provided for compatibility with platforms without unlimited precision
1409+
integers."
1410+
dec)
1411+
1412+
(defn unchecked-negate
1413+
"Return the negation of x.
1414+
1415+
Same as (- x). Python integers are unlimited precision so unchecked arithmetic
1416+
is only provided for compatibility with platforms without unlimited precision
1417+
integers."
1418+
[x]
1419+
(- x))
1420+
(defcopy unchecked-negate-int unchecked-negate)
1421+
1422+
(defn unchecked-byte
1423+
"Coerce x to a byte. Value may be truncated or rounded."
1424+
[x]
1425+
(byte (mod x 256)))
1426+
1427+
(defn unchecked-char
1428+
"Coerce x to a char. Value may be truncated or rounded."
1429+
[x]
1430+
(cond
1431+
(instance? python/int x) (char (mod x sys/maxunicode))
1432+
:else (char x)))
1433+
1434+
(defcopy unchecked-double
1435+
"Same as 'double. Python integers are unlimited precision, so unchecked arithmetic
1436+
is only provided for compatibility with platforms without unlimited precision
1437+
integers."
1438+
double)
1439+
(defcopy unchecked-float
1440+
"Same as 'float. Python integers are unlimited precision, so unchecked arithmetic
1441+
is only provided for compatibility with platforms without unlimited precision
1442+
integers."
1443+
float)
1444+
1445+
(defcopy unchecked-int
1446+
"Same as 'int. Python integers are unlimited precision, so unchecked arithmetic
1447+
is only provided for compatibility with platforms without unlimited precision
1448+
integers."
1449+
int)
1450+
(defcopy unchecked-long
1451+
"Same as 'long. Python integers are unlimited precision, so unchecked arithmetic
1452+
is only provided for compatibility with platforms without unlimited precision
1453+
integers."
1454+
long)
1455+
(defcopy unchecked-short
1456+
"Same as 'short. Python integers are unlimited precision, so unchecked arithmetic
1457+
is only provided for compatibility with platforms without unlimited precision
1458+
integers."
1459+
short)
1460+
13281461
;;;;;;;;;;;;;;;;
13291462
;; Exceptions ;;
13301463
;;;;;;;;;;;;;;;;

tox.ini

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ parallel_show_output = {env:TOX_SHOW_OUTPUT:true}
66
setenv =
77
BASILISP_DO_NOT_CACHE_NAMESPACES = true
88
deps =
9-
coverage
9+
coverage==4.5.4
1010
six==1.10.0
1111
commands =
1212
coverage run \
@@ -20,7 +20,7 @@ commands =
2020
depends = py36, py37, py38
2121
deps =
2222
coveralls
23-
coverage
23+
coverage==4.5.4
2424
six==1.10.0
2525
passenv =
2626
COVERALLS_REPO_TOKEN

0 commit comments

Comments
 (0)