File: builtin-complex.c

package info (click to toggle)
nickle 2.107
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 3,756 kB
  • sloc: ansic: 27,954; yacc: 1,874; lex: 954; sh: 204; makefile: 13; lisp: 1
file content (57 lines) | stat: -rw-r--r-- 1,307 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/*
 * Copyright © 2025 Keith Packard and Bart Massey.
 * All Rights Reserved.  See the file COPYING in this directory
 * for licensing information.
 */

#include	"builtin.h"

NamespacePtr	ComplexNamespace;

Value do_Complex_creal (Value z);
Value do_Complex_cimag (Value z);

void
import_Complex_namespace()
{
    ENTER();
    static const struct fbuiltin_1 funcs_1[] = {
	{ do_Complex_creal, "creal", "N", "C", "\n"
	  " real creal(complex c)\n"
	  "\n"
	  " Returns the real component of a complex value\n" },
	{ do_Complex_cimag, "cimag", "N", "C", "\n"
	  " real cimag(complex c)\n"
	  "\n"
	  " Returns the imaginary component of a complex value\n" },
	{ 0 }
    };

    static const struct fbuiltin_2 funcs_2[] = {
	{ NewValueComplex, "cmplx", "C", "NN", "\n"
	  " complex cmplx(real r, real i)\n"
	  "\n"
	  " Returns a new complex value given real and imaginary components\n" },
	{ 0 },
    };

    ComplexNamespace = BuiltinNamespace(/*parent*/ 0, "Complex")->namespace.namespace;

    BuiltinFuncs1 (&ComplexNamespace, funcs_1);
    BuiltinFuncs2 (&ComplexNamespace, funcs_2);
    EXIT();
}

Value do_Complex_creal (Value z)
{
    if (ValueIsComplex(z))
	return z->complex.r;
    return z;
}

Value do_Complex_cimag (Value z)
{
    if (ValueIsComplex(z))
	return z->complex.i;
    return Zero;
}