Skip to content

Commit

Permalink
Port "Fcdr()" and "cdr()" to Rust
Browse files Browse the repository at this point in the history
  • Loading branch information
TheKK committed Jan 14, 2017
1 parent 9364e97 commit d3ad1e3
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 14 deletions.
34 changes: 32 additions & 2 deletions rust_src/src/cons.rs
Expand Up @@ -113,12 +113,16 @@ unsafe fn XCAR(object: LispObject) -> LispObject {
(*XCONS(object)).car
}

/// Take the car of a cons cell, or signal an error if it's a
unsafe fn XCDR(object: LispObject) -> LispObject {
(*XCONS(object)).cdr
}

/// Take the car/cdr of a cons cell, or signal an error if it's a
/// different type.
///
/// # Porting Notes
///
/// This is equivalent to `CAR` in C code.
/// This is equivalent to `CAR`/`CDR` in C code.
fn car(object: LispObject) -> LispObject {
if CONSP(object) {
unsafe {
Expand All @@ -132,6 +136,19 @@ fn car(object: LispObject) -> LispObject {
}
}
}
fn cdr(object: LispObject) -> LispObject {
if CONSP(object) {
unsafe {
XCDR(object)
}
} else if NILP(object) {
Qnil
} else {
unsafe {
wrong_type_argument(Qlistp, object)
}
}
}

#[no_mangle]
pub extern "C" fn Fcar(list: LispObject) -> LispObject {
Expand All @@ -145,3 +162,16 @@ See Info node `(elisp)Cons Cells' for a discussion of related basic
Lisp concepts such as car, cdr, cons cell and list.
(fn LIST)");

#[no_mangle]
pub extern "C" fn Fcdr(list: LispObject) -> LispObject {
cdr(list)
}

defun!("cdr", Fcdr, Scdr, 1, 1, ptr::null(), " Return the cdr of LIST. If arg is nil, return nil.
Error if arg is not nil and not a cons cell. See also `cdr-safe'.
See Info node `(elisp)Cons Cells' for a discussion of related basic
Lisp concepts such as cdr, car, cons cell and list.
(fn LIST)");
2 changes: 2 additions & 0 deletions rust_src/src/lib.rs
Expand Up @@ -31,6 +31,7 @@ pub use math::Fquo;
pub use cons::Fsetcar;
pub use cons::Fsetcdr;
pub use cons::Fcar;
pub use cons::Fcdr;

extern "C" {
fn defsubr(sname: *const LispSubr);
Expand All @@ -55,6 +56,7 @@ pub extern "C" fn rust_init_syms() {
defsubr(&*cons::Ssetcar);
defsubr(&*cons::Ssetcdr);
defsubr(&*cons::Scar);
defsubr(&*cons::Scdr);
defsubr(&*strings::Sstringp);
defsubr(&*strings::Seq);
defsubr(&*strings::Snull);
Expand Down
12 changes: 0 additions & 12 deletions src/data.c
Expand Up @@ -479,17 +479,6 @@ DEFUN ("car-safe", Fcar_safe, Scar_safe, 1, 1, 0,
return CAR_SAFE (object);
}

DEFUN ("cdr", Fcdr, Scdr, 1, 1, 0,
doc: /* Return the cdr of LIST. If arg is nil, return nil.
Error if arg is not nil and not a cons cell. See also `cdr-safe'.
See Info node `(elisp)Cons Cells' for a discussion of related basic
Lisp concepts such as cdr, car, cons cell and list. */)
(register Lisp_Object list)
{
return CDR (list);
}

DEFUN ("cdr-safe", Fcdr_safe, Scdr_safe, 1, 1, 0,
doc: /* Return the cdr of OBJECT if it is a cons cell, or else nil. */)
(Lisp_Object object)
Expand Down Expand Up @@ -3341,7 +3330,6 @@ syms_of_data (void)
defsubr (&Ssubrp);
defsubr (&Sbyte_code_function_p);
defsubr (&Schar_or_string_p);
defsubr (&Scdr);
defsubr (&Scar_safe);
defsubr (&Scdr_safe);
defsubr (&Ssymbol_function);
Expand Down
1 change: 1 addition & 0 deletions src/lisp.h
Expand Up @@ -1276,6 +1276,7 @@ CDR_SAFE (Lisp_Object c)
Lisp_Object Fsetcar(Lisp_Object, Lisp_Object);
Lisp_Object Fsetcdr(Lisp_Object, Lisp_Object);
Lisp_Object Fcar(Lisp_Object);
Lisp_Object Fcdr(Lisp_Object);
Lisp_Object Fatom(Lisp_Object);

/* In a string or vector, the sign bit of the `size' is the gc mark bit. */
Expand Down

0 comments on commit d3ad1e3

Please sign in to comment.