Source

arana-main / test.c

#include <stdio.h>
#include "arana/arana.h"


void
_try_type_error(AR_SIG, void *closure)
{
    printf("Trying to raise TypeError...\n");
    AR_RAISE(AR_TYPE_ERROR("Hello World"));
}

void
_catch_type_error(AR_SIG, AR error, void *closure)
{
    ArError *e = (ArError*)error;
    struct _ArTraceback *tb = (struct _ArTraceback*)AI->traceback;
    printf("%s: %s in %s:%d (%s)\n", AR_TYPE_NAME(e), AR_AS_CHARP(e->message),
           tb->filename, tb->lineno, tb->function);
}

void
_print_tuple_item(AR_SIG, AR value, void *closure)
{
    int *index = (int*)closure;
    printf("%d: %s\n", ++(*index), AR_AS_CHARP(AR_TO_STRING(value)));
}

int
main(void)
{
    /* the root is here */
    AR_ROOT_SCOPE;

    /* create the interpreter in the root scope */
    AR_INTERPRETER();

    arana_repl_loop(AR_ISIG);
    return 0;

#if 0
    /* create some basic types */
    AR i = AR_INTEGER(42);
    AR b = AR_BOOLEAN(1);
    AR s = AR_STRING("Hello World");
    AR sm = AR_STRINGMAP();
    AR tup = AR_TUPLE_PACK(i, s);
    AR l = AR_LIST();

    /* test the basic types a bit */
    printf("Integer: %ld (%s)\n", AR_AS_LONG(i), AR_IS_INTEGER(i) ? "yes" : "no");
    printf("Boolean: %d (%s)\n", AR_TEST(b), AR_IS_BOOLEAN(b) ? "yes" : "no");
    printf("Integer is Boolean: %s\n", AR_IS_BOOLEAN(i) ? "yes" : "no");
    printf("Boolean is Integer: %s\n", AR_IS_INTEGER(b) ? "yes" : "no");
    printf("Boolean as Integer: %ld\n", AR_AS_LONG(b));
    printf("String: %s\n", AR_AS_CHARP(s));
    printf("Integer type: %s\n", AR_TYPE_NAME(i));
    printf("Boolean type: %s\n", AR_TYPE_NAME(b));
    printf("String type: %s\n", AR_TYPE_NAME(s));
    printf("NULL type: %s\n", AR_TYPE_NAME(AR_NULL));

    /* test the float type */
    AR f1 = AR_FLOAT(3.14);
    AR f2 = AR_FLOAT(1.0);
    AR f3 = AR_CALL_METHOD(f1, ":add", f2);
    AR f4 = AR_CALL_METHOD(AR_INTEGER(6), ":div", AR_INTEGER(5));
    printf("Float repr: %s\n", AR_AS_CHARP(AR_REPR(f3)));
    printf("Int division result: %s\n", AR_AS_CHARP(AR_REPR(f4)));

    /* put something into the stringmap */
    AR_STRINGMAP_SET(sm, "key1", i);
    AR_STRINGMAP_SET(sm, "key2", s);

    /* and read it again */
    printf("Key1: %ld\n", AR_AS_LONG(AR_STRINGMAP_GET(sm, "key1")));
    printf("Key2: %s\n", AR_AS_CHARP(AR_STRINGMAP_GET(sm, "key2")));
    printf("Missing key type: %s\n", AR_TYPE_NAME(AR_STRINGMAP_GET(sm, "missing")));
    printf("Existing key1 (%s) and missing (%s)\n",
           AR_STRINGMAP_HAS_KEY(sm, "key1") ? "yes" : "no",
           AR_STRINGMAP_HAS_KEY(sm, "missing") ? "yes" : "no");

    /* get some types */
    printf("Type of type: %s\n", AR_TYPE_NAME(AR_GET_MEMBER(AR_BUILTINS, "Type")));
    printf("Another type: %s\n", AR_TYPE_NAME(AR_INTEGER(42)));

    /* count and get items from the tuple */
    printf("Items in tuple: %d\n", (int)AR_TUPLE_SIZE(tup));
    printf("First item: %ld\n", AR_AS_LONG(AR_TUPLE_GET(tup, 0)));
    printf("Second item: %s\n", AR_AS_CHARP(AR_TUPLE_GET(tup, 1)));

    /* manually invoking functions */
    printf("1 + 2 = %ld\n", AR_AS_LONG(AR_CALL_METHOD(AR_INTEGER(1), ":add", AR_INTEGER(2))));

    /* test lists */
    AR_LIST_APPEND(l, AR_INTEGER(100));
    AR_LIST_APPEND(l, AR_INTEGER(200));
    AR_LIST_APPEND(l, AR_INTEGER(300));
    AR_LIST_APPEND(l, AR_STRING("Hello New World"));
    printf("1. item in list: %ld\n", AR_AS_LONG(AR_LIST_GET(l, 0)));
    printf("2. item in list: %ld\n", AR_AS_LONG(AR_LIST_GET(l, 1)));
    printf("3. item in list: %ld\n", AR_AS_LONG(AR_LIST_GET(l, 2)));
    printf("4. item in list: %s\n", AR_AS_CHARP(AR_LIST_GET(l, 3)));

    /* test string printing */
    printf("The value of the first list item self printed: %s\n",
           AR_AS_CHARP(AR_TO_STRING(AR_LIST_GET(l, 0))));

    printf("String value of True: %s\n",
           AR_AS_CHARP(AR_TO_STRING(AR_TRUE)));

    /* some reprs repr */
    AR intmap = AR_OBJECT_MAP(AR_GET_TYPE_OBJECT(AR_INTEGER(42)));
    printf("%s\n", AR_TYPE_NAME(intmap));
    printf("%s\n", AR_AS_CHARP(AR_REPR(intmap)));
    printf("Tuple repr: %s\n", AR_AS_CHARP(AR_REPR(tup)));
    printf("Integer repr: %s\n", AR_AS_CHARP(AR_REPR(AR_INTEGER(42))));

    /* iterate over a tuple */
    printf("Iter over tuple:\n");
    int tuple_index = 0;
    AR_EACH(tup, _print_tuple_item, &tuple_index);

    /* try the exception handling */
    AR_RESCUE(_try_type_error, _catch_type_error, NULL);
#endif
    /* create an ast for testing */
    ArAstNode *test;
    #if 0
    test = AR_AST_STMTS(
        AR_AST(binary, AR_AST_TYPE_ASSIGN,
               AR_AST(name, "seq", AR_ID_CTX_STORE),
               AR_AST(literal, AR_TUPLE_PACK(
                          AR_INTEGER(42),
                          AR_INTEGER(2),
                          AR_STRING("Hello World")
                          ))
            ),
        AR_AST(for_stmt,
               AR_AST(name, "item", AR_ID_CTX_STORE),
               AR_AST(name, "seq", AR_ID_CTX_LOAD),
               AR_AST_STMTS(
                   AR_AST(send,
                          AR_AST(name, "item", AR_ID_CTX_LOAD),
                          AR_AST(literal, AR_STRING("print")),
                          AR_AST(literal, AR_TUPLE(0))
                       ),
                   AR_AST(send,
                          AR_AST(literal, AR_STRING("\n")),
                          AR_AST(literal, AR_STRING("print")),
                          AR_AST(literal, AR_TUPLE(0))
                       )
                   )
            )
	);
    #endif
#if 0
    /* try parsing something */
    //AR codestr = AR_STRING("\"#{1+2}de#{0}g\"");
    AR codestr = AR_STRING("x += 15; y += 23 \nif 1:\n   if 2: a; b\n   else:");
    test = arana_parse(AR_ISIG, codestr, AR_STRING("<unknown>"));

    /* set a dummy filename */
    arana_ast_set_filename(AR_ISIG, test, "<unknown>");

    /* and compile and evaluate */
    arana_ast_optimize(AR_ISIG, test);
    ArCode *code = AR_COMPILE(test);
    AR_DEBUG_CODE(code);
    ArFrame *frame = AR_FRAME(code);
    AR_SET_LOCAL(frame, "foo", AR_INTEGER(2));
    AR val = AR_EVALUATE(frame);
    printf("result = ");
    AR_PRINT(AR_REPR(val));
    printf("\n");

    /* default exception handling. */
    printf("Expected exception:\n");
    AR_CALL_METHOD(AR_INTEGER(1), ":add", AR_STRING("foo"));

    printf("This is never executed\n");
#endif
    return 0;
}