Carp/core/Dynamic.carp
Veit Heller dacc13560b
feat: add dynamic Map type (#1168)
* feat: add dynamic map prototype

* feat: feature parity for dynamic map

* docs: document dynamic map

* test: add dynamic map tests

* fix: defdynamics are handled in getBinderDescription

* test: i forgot to add dynamic tests, whoops
2021-02-11 09:12:58 +01:00

95 lines
2.6 KiB
Plaintext

(doc Dynamic "This module contains dynamic functions which are used in the Carp repl and during compilation. They are not available in compiled code. Read more about dynamic functions in the [Language Guide](https://github.com/carp-lang/Carp/blob/master/docs/LanguageGuide.md#dynamic-functions).")
(defmodule Dynamic
;; Functions for doing things at the REPL and during compile time.
(doc nil "is the value `nil`, i.e. the empty list.")
(defdynamic nil '())
(doc nil? "checks whether a value is nil, i.e. the empty list.")
(defndynamic nil? [value]
(= value nil))
(defndynamic inc [x]
(+ x 1))
(defndynamic dec [x]
(- x 1))
(defndynamic neg [x]
(* -1 x))
(defndynamic mod [x y]
(let-do [a (if (< x 0) (neg x) x)
b (if (< y 0) (neg y) y)
m a]
(while (or (> m b) (= m b))
(set! m (- m b)))
(if (< a 0) (neg m) m)))
(doc imod "implements the modulo on integers, and is much faster than
[`mod](#mod).")
(defndynamic imod [x y]
(- x (* y (/ x y))))
(doc even? "checks whether the number `n` is even.")
(defndynamic even? [n] (= 0 (mod n 2)))
(doc odd? "checks whether the number `n` is odd.")
(defndynamic odd? [n] (= 1 (mod n 2)))
(defmodule Project
(doc no-echo "Turn off debug printing in the compiler.")
(defndynamic no-echo []
(do
(Project.config "print-ast" false)
(Project.config "echo-compiler-cmd" false))))
(defmodule String
(defndynamic prefix [s to]
(String.slice s 0 to))
(defndynamic suffix [s from]
(String.slice s from (String.length s)))
(defndynamic head [s]
(String.prefix s 1))
(defndynamic tail [s]
(String.suffix s 1))
)
)
;; The following functions are not put into a module for now:
(defndynamic add-cflag [flag]
(eval (list 'Project.config "cflag" flag)))
(defndynamic add-lib [lib]
(eval (list 'Project.config "libflag" lib)))
(defndynamic pkg-config [pkg flags]
(Dynamic.String.concat (Dynamic.append
(Dynamic.append
["`pkg-config " pkg " "]
(Project.get-config "pkgconfigflag")
)
[flags "`"])))
(defndynamic add-pkg [pkg]
(do
(add-cflag (pkg-config pkg "--cflags"))
(add-lib (pkg-config pkg "--libs"))))
(defndynamic current-file []
(car (Project.get-config "load-stack")))
(defndynamic relative-to [path relpath]
(Dynamic.String.concat [(Dynamic.Path.directory path)
"/"
relpath]))
(defndynamic add-c [relpath]
(Project.config "cmod" (relative-to (current-file) relpath)))