view src/preprocess_test.c @ 140:a4a6169cccc0

preprocess: Update test and add one new
author Lewin Bormann <lbo@spheniscida.de>
date Tue, 03 Sep 2019 09:41:40 +0200
parents a26a197afe24
children 4e0060d95d31
line wrap: on
line source

#include "preprocess.h"

#include "eval.h"
#include "expr.h"
#include "parse.h"
#include "value.h"

#include <stdio.h>

// The very first successfully executable program!
void test_preprocess_defn(void) {
    fprintf(stderr, "test_preprocess_defn ===========\n");

    // my-func adds the two arguments plus 42.
    ystr_t input = ystr_new(
        "(defn my-func (arg1 arg2) (+ arg1 arg2) (undef) (+ arg1 arg2 41))"
        "(my-func 42 (my-func 43 (my-func 44 0)))");  // (((44 + 0 + 41) + 43 +
                                                      // 41) + 42 + 41) == 252
    yexpr_t program = yexpr_new();
    ystr_t error = ystr_new(NULL);
    assert(yparse_str(&input, &program, &error));

    ypreprocess_resolve_builtins(&program);
    yexpr_debug(&program);
    ypreprocess_refs(&program);
    yexpr_debug(&program);

    /// We can't do these anymore, as functions are now anonymous:
    /*
    fprintf(stderr, "arg ref 0: %lu\n",
            YVEC_AT(&func->value.func.args, 0, yarg_desc_t)->argref.ref.id);
    fprintf(stderr, "arg ref 1: %lu\n",
            YVEC_AT(&func->value.func.args, 1, yarg_desc_t)->argref.ref.id);
    fprintf(
        stderr, "arg name 0: %s\n",
        ystr_str(&YVEC_AT(&func->value.func.args, 0, yarg_desc_t)->argname));
    fprintf(
        stderr, "arg name 1: %s\n",
        ystr_str(&YVEC_AT(&func->value.func.args, 1, yarg_desc_t)->argname));
    */

    yeval_state_t state;
    state.call_stack = YVEC_NEW(NULL, 0, yexpr_t);
    yexpr_t result = yeval_list_return_last(&state, &program.value.list, 0);
    assert(result.typ == YEXPR_NUMBER);
    assert(252 == result.value.n);
    yexpr_debug(&result);

    ystr_destroy(&input);
    yexpr_destroy(&program);
    yvec_destroy(&state.call_stack);
    yexpr_destroy(&result);
}

void test_preprocess_refs(void) {
    fprintf(stderr, "test_preprocess_refs ===========\n");

    // my-func adds the two arguments plus 42.
    ystr_t input = ystr_new(
            "(defn my-func (arg) (+ arg 1))"
            "(let a 33)"
            "(let b 44)"
            "(+ a b)"
            "(let a b)"
            "(my-func a)");
    yexpr_t program = yexpr_new();
    ystr_t error = ystr_new(NULL);
    assert(yparse_str(&input, &program, &error));

    yexpr_debug(&program);
    ypreprocess_resolve_builtins(&program);
    ypreprocess_defn(&program);
    yexpr_debug(&program);
    ypreprocess_refs(&program);
    yexpr_debug(&program);

    yeval_state_t state;
    state.call_stack = YVEC_NEW(NULL, 0, yexpr_t);
    yexpr_t result = yeval_list_return_last(&state, &program.value.list, 0);
    assert(result.typ == YEXPR_NUMBER);
    assert(45 == result.value.n);
    yexpr_debug(&result);

    yexpr_destroy(&program);
    ystr_destroy(&input);
    ystr_destroy(&error);
    yvec_destroy(&state.call_stack);
}

int main(void) {
    test_preprocess_defn();
    test_preprocess_refs();
    fprintf(stderr, "global destroy ===========\n");
    yvalue_free_all();
    return 0;
}