0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Common Lisp風のLISPを作ってみる(12.条件)

Posted at

今回は、なんと、ifだけです。なぜかというと、and, or, condはスペシャルオペレーターではなくマクロだからですね。大丈夫です。もうmy-lisp2の中にはand, or, condをマクロとして入れることができましたので、今回はサラッと流していきましょう。
ソースコード

built_in_func.c

op_if関数はスペシャルオペレーターifの実体です。

built_in_func.c
void *op_if(void *args, void *env_func, void *env_var) {
    void *test_form = car(args);
    void *then_form = car(cdr(args));
    void *else_form = car(cdr(cdr(args)));
    void *val;
    size_t len = list_length(args);
    if (len <= 1) {
        fprintf(stderr, "IF: 引数が少なすぎます\n");
        state = STATE_ERROR;
        return 0;
    } else if (len >= 4) {
        fprintf(stderr, "IF: 引数が多すぎます\n"); 
        state = STATE_ERROR;
        return 0;
    }   
    val = eval(test_form, env_func, env_var);
    if (!val) return 0;
    if (val != NIL) {
        return eval(then_form, env_func, env_var);
    } else {
        return eval(else_form, env_func, env_var); 
    }   
}

動かしてみよう

ifだけ増やしても、大したことはできないです...

> (if 1 2 3)
2
> (if nil 2 3)
3
> (if (> 2 1) "おめでとう" "残念")
"おめでとう"
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?