Login
7 branches 0 tags
Ben (X13/Arch) WIP a25bc3e 3 years ago 651 Commits
nujel / stdlib / core / core.nuj
;; Nujel - Copyright (C) 2020-2021 - Benjamin Vincent Schulenburg
;; This project uses the MIT license, a copy should be included under /LICENSE
;;
;; Contains native nujel implementations of some
;; core language constructs and essential macros

[def otherwise #t]

[defmacro comment body
  "Does nothing"
  #nil]

[defmacro += [val inc]
  `[set! ~val [+ ~val ~inc]]]

[defmacro cdr! [l]
  "[set! l [cdr l]]"
  `[set! ~l [cdr ~l]]]

[defn not [v] :inline
  "Return true if V is false"
  [if v #f #t]]

[defn identity [α] :inline
  "Returns its argument"
  α]

[defn list arguments
  "Return ARGUMENTS as a list"
  arguments]

[defn default [arg default-value]
  "Returns ARG or DEFAULT-VALUE if ARG is #nil"
  [if arg arg default-value]]

[defn caar [p] :inline
  "[car [car p]]"
  [car [car p]]]

[defn cadr [p] :inline
  "[car [cdr p]]"
  [car [cdr p]]]

[defn cdar [p] :inline
  "[cdr [car p]]"
  [cdr [car p]]]

[defn cddr [p] :inline
  "[cdr [cdr p]]"
  [cdr [cdr p]]]

[defn cadar [p]
  "[cdr [car p]]"
  [car [cdr [car p]]]]

[defn caddr [p]
  "[car [cdr [cdr p]]]"
  [car [cdr [cdr p]]]]

[defn cdddr [p]
  "[cdr [cdr [cdr p]]]"
  [cdr [cdr [cdr p]]]]

[defn cadddr [p]
  "[car [cdr [cdr [cdr p]]]]"
  [car [cdr [cdr [cdr p]]]]]

[defn cddddr [p]
  "[car [cdr [cdr [cdr p]]]]"
  [cdr [cdr [cdr [cdr p]]]]]

[defn caddddr [p]
  "[car [cdr [cdr [cdr p]]]]"
  [car [cdr [cdr [cdr [cdr p]]]]]]

[defn cdddddr [p]
  "[cdr [cdr [cdr [cdr p]]]]"
  [cdr [cdr [cdr [cdr [cdr p]]]]]]

[defn keyword->string [α]
  [when-not [keyword? α] [throw [list :type-error "[keyword->string] can only be called on keywords" α [current-lambda]]]]
  [sym->str [keyword->symbol α]]]

[defn string->keyword [α]
  [when-not [string? α] [throw [list :type-error "[string->keyword] can only be called on strings" α [current-lambda]]]]
  [symbol->keyword [str->sym α]]]

[defmacro exception [type description value]
  `[throw [list ~type ~description ~value [current-lambda]]]]