view src/preprocess_test.c @ 131:0af2b62f49d2

preprocess: Add test
author Lewin Bormann <lbo@spheniscida.de>
date Sun, 01 Sep 2019 19:24:03 +0200
parents
children a26a197afe24
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_defn(&program);
    yexpr_debug(&program);

    yvalue_t* func = yvalue_get(yref_new_c("my-func"));
    assert(func != NULL);
    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);
    yexpr_debug(&result);

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

int main(void) {
    test_preprocess_defn();
    yvalue_free_all();
    return 0;
}