Ejemplo n.º 1
0
struct ast_node *ast_xidentifier_create(
    const struct inplocation_mark *start,
    const struct inplocation_mark *end,
    struct ast_node *id,
    bool is_constant,
    struct ast_node *genr,
    struct ast_node *arrspec
)
{
    struct ast_node *ret;
    ret = ast_node_create_marks(AST_XIDENTIFIER, start, end);
    if (!ret) {
        return NULL;
    }

    ast_node_add_child(ret, id);
    ret->xidentifier.is_constant = is_constant;
    ret->xidentifier.id = id;
    ret->xidentifier.genr = genr;
    ret->xidentifier.arrspec = arrspec;
    if (genr) {
        ast_node_add_child(ret, genr);
    }
    if (arrspec) {
        ast_node_add_child(ret, arrspec);
    }

    return ret;
}
Ejemplo n.º 2
0
}END_TEST

START_TEST(test_acc_ifexpr_2) {
    struct ast_node *n;
    static const struct RFstring s = RF_STRING_STATIC_INIT(
        "if a == 42 {\n"
        "    do_sth()\n"
        "} else { \n"
        "    55 + 2.31\n"
        "}"
    );
    front_testdriver_new_main_source(&s);

    struct ast_node *id1 = testsupport_parser_identifier_create(0, 3, 0, 3);
    testsupport_parser_constant_create(cnum1, 0, 8, 0, 9, integer, 42);
    testsupport_parser_node_create(cmp_exp, binaryop, 0, 3, 0, 9,
                                   BINARYOP_CMP_EQ, id1, cnum1);


    testsupport_parser_block_create(bnode1, 0, 11, 2, 0);
    struct ast_node *fn_name = testsupport_parser_identifier_create(
        1, 4, 1, 9);

    testsupport_parser_node_create(fc, fncall, 1, 4, 1, 11, fn_name, NULL, NULL);
    ast_node_add_child(bnode1, fc);

    testsupport_parser_node_create(cond1, condbranch, 0, 3, 2, 0,
                                   cmp_exp, bnode1);


    testsupport_parser_block_create(bnode2, 2, 7, 4, 0);
    testsupport_parser_constant_create(cnum2, 3, 4, 3, 5, integer, 55);
    testsupport_parser_constant_create(cnum3, 3, 9, 3, 12, float, 2.31);
    testsupport_parser_node_create(op1, binaryop, 3, 4, 3, 12,
                                   BINARYOP_ADD, cnum2, cnum3);
    ast_node_add_child(bnode2, op1);



    testsupport_parser_node_create(ifx, ifexpr, 0, 0, 4, 0, cond1, bnode2);

    ck_test_parse_as(n, ifexpr, "if_expression", ifx, TOKEN_KW_IF);

    ast_node_destroy(n);
    ast_node_destroy(ifx);
}END_TEST
Ejemplo n.º 3
0
enum parser_fnimpl_list_err parser_acc_fnimpl_list(struct parser *p,
                                                   struct ast_node *parent)
{
   struct ast_node *impl;
   struct token *tok;
   tok = lexer_lookahead(p->lexer, 1);

   if (!tok || tok->type != TOKEN_KW_FUNCTION) {
       return PARSER_FNIMPL_LIST_EMPTY;
   }

   while (tok && tok->type == TOKEN_KW_FUNCTION) {
       impl = parser_acc_fnimpl(p);
       if (!impl) {
           return PARSER_FNIMPL_LIST_FAILURE;
       }
       ast_node_add_child(parent, impl);
       tok = lexer_lookahead(p->lexer, 1);
   }

   return PARSER_FNIMPL_LIST_SUCCESS;
}
Ejemplo n.º 4
0
enum parser_fndecl_list_err parser_acc_fndecl_list(struct parser *p,
                                                   struct ast_node *parent,
                                                   int fndecl_position)
{
    struct ast_node *decl;
    struct token *tok;
    tok = lexer_lookahead(p->lexer, 1);

    if (!tok || tok->type != TOKEN_KW_FUNCTION) {
        return PARSER_FNDECL_LIST_EMPTY;
    }

    while (tok && tok->type == TOKEN_KW_FUNCTION) {
        decl = parser_acc_fndecl(p, fndecl_position);
        if (!decl) {
            return PARSER_FNDECL_LIST_FAILURE;
        }
        ast_node_add_child(parent, decl);
        tok = lexer_lookahead(p->lexer, 1);
    }

    return PARSER_FNDECL_LIST_SUCCESS;
}
Ejemplo n.º 5
0
}END_TEST

START_TEST(test_acc_ifexpr_4) {
    struct ast_node *n;
    static const struct RFstring s = RF_STRING_STATIC_INIT(
        "if a == 42 {\n"
        "    do_sth()\n"
        "} elif (a == 50 && is_good()) {\n"
        "    \"foo\"\n"
        "} elif (5 < something) {\n"
        "    283.23\n"
        "} else { \n"
        "    55 + 2.31\n"
        "}"
    );
    front_testdriver_new_main_source(&s);

    struct ast_node *id1 = testsupport_parser_identifier_create(0, 3, 0, 3);
    testsupport_parser_constant_create(cnum1, 0, 8, 0, 9, integer, 42);
    testsupport_parser_node_create(cmp_exp, binaryop, 0, 3, 0, 9,
                                   BINARYOP_CMP_EQ, id1, cnum1);


    testsupport_parser_block_create(bnode1, 0, 11, 2, 0);
    struct ast_node *fn_name = testsupport_parser_identifier_create(
        1, 4, 1, 9);

    testsupport_parser_node_create(fc, fncall, 1, 4, 1, 11, fn_name, NULL, NULL);
    ast_node_add_child(bnode1, fc);

    testsupport_parser_node_create(cond1, condbranch, 0, 3, 2, 0,
                                   cmp_exp, bnode1);



    struct ast_node *id2 = testsupport_parser_identifier_create(2, 8, 2, 8);
    testsupport_parser_constant_create(cnum2, 2, 13, 2, 14, integer, 50);
    testsupport_parser_node_create(op1, binaryop, 2, 8, 2, 14,
                                   BINARYOP_CMP_EQ, id2, cnum2);
    struct ast_node *fn_name2 = testsupport_parser_identifier_create(
        2, 19, 2, 25);
    testsupport_parser_node_create(fc2, fncall, 2, 19, 2, 27,
                                   fn_name2, NULL, NULL);
    testsupport_parser_node_create(cmp_exp2, binaryop, 2, 8, 2, 27,
                                   BINARYOP_LOGIC_AND, op1, fc2);

    testsupport_parser_block_create(bnode2, 2, 30, 4, 0);
    testsupport_parser_string_literal_create(sliteral1, 3, 4, 3, 8);
    ast_node_add_child(bnode2, sliteral1);
    testsupport_parser_node_create(cond2, condbranch, 2, 8, 4, 0,
                                   cmp_exp2, bnode2);


    testsupport_parser_constant_create(cnum3, 4, 8, 4, 8, integer, 5);
    struct ast_node *id3 = testsupport_parser_identifier_create(4, 12, 4, 20);
    testsupport_parser_node_create(cmp_exp3, binaryop, 4, 8, 4, 20,
                                   BINARYOP_CMP_LT, cnum3, id3);
    testsupport_parser_block_create(bnode3, 4, 23, 6, 0);
    testsupport_parser_constant_create(cnum4, 5, 4, 5, 9, float, 283.23);
    ast_node_add_child(bnode3, cnum4);
    testsupport_parser_node_create(cond3, condbranch, 4, 8, 6, 0,
                                   cmp_exp3, bnode3);


    testsupport_parser_block_create(bnode4, 6, 7, 8, 0);
    testsupport_parser_constant_create(cnum5, 7, 4, 7, 5, integer, 55);
    testsupport_parser_constant_create(cnum6, 7, 9, 7, 12, float, 2.31);
    testsupport_parser_node_create(op3, binaryop, 7, 4, 7, 12,
                                   BINARYOP_ADD, cnum5, cnum6);
    ast_node_add_child(bnode4, op3);

    // the second elif
    testsupport_parser_node_create(if3, ifexpr, 4, 2, 8, 0, cond3, bnode4);
    // the first elif
    testsupport_parser_node_create(if2, ifexpr, 2, 2, 8, 0, cond2, if3);
    // the first if
    testsupport_parser_node_create(ifx, ifexpr, 0, 0, 8, 0, cond1, if2);


    ck_test_parse_as(n, ifexpr, "if_expression", ifx, TOKEN_KW_IF);

    ast_node_destroy(n);
    ast_node_destroy(ifx);
}END_TEST