root/lang/c/misc/mlisp/inc/mlisp.h @ 5975

Revision 5975, 2.6 kB (checked in by mokehehe, 5 years ago)

ディレクトリ分割

Line 
1//=============================================================================
2///     minimal lisp
3//=============================================================================
4
5#pragma once
6
7#include <stdio.h>
8
9#ifdef __cplusplus
10extern "C" {
11#endif
12
13/// Symbol-Expression type
14typedef int             SExp;
15
16/// constant value
17#define cNil                                                                                            MAKE_SEXP(tOther, 0)
18#define cT                                                                                                      MAKE_SEXP(tOther, 1)
19#define cEOF                                                                                            MAKE_SEXP(tOther, 2)
20#define cNL                                                     /* new line */                  MAKE_SEXP(tOther, 3)
21
22/// flag
23/**
24        use in ml_set_flag()
25*/
26typedef enum {
27        MLF_PRINT_FUNC_ARG,
28        MLF_PRINT_MACRO_REPLACE,
29
30        numofMLF
31} ML_FLAG;
32
33/// types
34typedef struct MLContext        MLContext;                                      ///< Context
35typedef SExp (*MLBuiltinFunc)(MLContext*, SExp);                ///< Builtin function type
36
37/// functions
38MLContext* ml_new(void);
39void ml_delete(MLContext* ml);
40
41void ml_set_flag(MLContext* ml, ML_FLAG flag, int value);
42
43void ml_dump(MLContext* ml);
44
45SExp ml_read(MLContext* ml, FILE* fp);
46SExp ml_eval(MLContext* ml, SExp a);
47void ml_print(MLContext* ml, FILE* fp, SExp a);
48SExp ml_apply(MLContext* ml, SExp ca, SExp cd);
49
50SExp ml_resist_builtin(MLContext* ml, const char* word, MLBuiltinFunc cfunc, int b_macro);
51SExp ml_create_lambda(MLContext* ml, SExp arg_body);
52void ml_defmacro(MLContext* ml, SExp symb, SExp arg_body);
53
54void ml_runtime_error(MLContext* ml, SExp a, const char* msg);
55
56
57SExp ml_int2sexp(int x);                                                                        ///< int -> sexp
58int ml_sexp2int(SExp a);                                                                        ///< sexp -> int
59
60SExp ml_cons(MLContext* ml, SExp car, SExp cdr);                        ///< cons
61SExp ml_car(MLContext* ml, SExp a);                                                     ///< car
62SExp ml_cdr(MLContext* ml, SExp a);                                                     ///< cdr
63void ml_rplaca(MLContext* ml, SExp cs, SExp a);                         ///< car ���u��
64void ml_rplacd(MLContext* ml, SExp cs, SExp d);                         ///< cdr ���u��
65
66void ml_setq(MLContext* ml, SExp symb, SExp arg_body);          ///< setq
67SExp ml_find_symbol(MLContext* ml, const char* str, const char* end, int b_make);               ///< �V���{���쐬
68
69const char* ml_get_string(MLContext* ml, SExp symb);            ///< string
70
71
72
73/// implementation detail
74
75/// define
76#define TYPE_BITS                       (8)
77#define VAL_MASK                        (-1 << TYPE_BITS)
78#define MAKE_SEXP(t, val)       ((t) | ((val) << TYPE_BITS))
79
80/// type of SExp
81#define tCell                           (2)             ///< cell
82#define tSymb                           (6)             ///< symbol
83#define tOther                          (10)    ///< other (nil, t, eof)
84#define tFunc                           (14)    ///< function (lambda or macro)
85#define tStr                            (18)    ///< string
86
87/// macros
88#define SEXP_IS_INT(a)          (((a) & 1) != 0)
89#define SEXP_TYPE(a)            ((a) & ~VAL_MASK)
90#define SEXP_CONSP(a)           (SEXP_TYPE(a) == tCell)
91#define SEXP_IS_ATOM(a)         (!SEXP_CONSP(a))
92
93#ifdef __cplusplus
94} //extern "C"
95#endif
Note: See TracBrowser for help on using the browser.