1#include <myaw.h>
 2
 3uint16_t PwTypeId_MyawError = 0;
 4
 5uint16_t MweEndOfBlock = 0;
 6uint16_t MweParseError = 0;
 7
 8/****************************************************************
 9 * Basic interface methods
10 */
11
12static bool myaw_error_create(PwMethod_Basic_create* mthis, PwValuePtr result, PwCtorArgs* ctor_args)
13{
14    if (!pw_super(mthis, result, ctor_args)) {
15        return false;
16    }
17    MwExceptionData* mwe = pw_this_data(result);
18
19    MyawErrorCtorArgs* args = pw_this_ctor_args();
20    if (args) {
21        mwe->line_number = args->line_number;
22        mwe->position    = args->position;
23    }
24    return true;
25}
26
27static bool myaw_error_hash(PwMethod_Basic_hash* mthis, PwValuePtr self, PwHashContext* ctx, _PwCompoundChain* tail)
28{
29    if (!pw_super(mthis, self, ctx, tail)) {
30        return false;
31    }
32    MwExceptionData* mwe = pw_this_data(self);
33    _pw_hash_uint64(ctx, mwe->line_number);
34    _pw_hash_uint64(ctx, mwe->position);
35    return true;
36}
37
38static bool myaw_error_dump(PwMethod_Basic_dump* mthis, PwValuePtr self, FILE* fp, int indent, _PwCompoundChain* tail)
39{
40    if (!pw_super(mthis, self, fp, indent, tail)) {
41        return false;
42    }
43    MwExceptionData* mwe = pw_this_data(self);
44    _pw_print_indent(fp, indent);
45    fprintf(fp, "Line %u, position %u\n",  mwe->line_number, mwe->position);
46    return true;
47}
48
49static bool myaw_error_to_string(PwMethod_Basic_to_string* mthis, PwValuePtr self, PwValuePtr result, _PwCompoundChain* tail)
50{
51    if (!pw_super(mthis, self, result, tail)) {
52        return false;
53    }
54    MwExceptionData* mwe = pw_this_data(self);
55
56    char location[48];
57    snprintf(location, sizeof(location), " Line %u, position %u: ",
58             mwe->line_number, mwe->position);
59
60    return pw_string_append(result, location);
61}
62
63static PwInterface_Basic myaw_error_basic_interface = {
64    .create    = { .func = myaw_error_create },
65    .hash      = { .func = myaw_error_hash },
66    .dump      = { .func = myaw_error_dump },
67    .to_string = { .func = myaw_error_to_string }
68};
69
70[[ gnu::constructor ]]
71static void init_mw_status()
72{
73    _pw_init_types();
74
75    if (PwTypeId_MyawError) {
76        return;
77    }
78
79    PwTypeId_MyawError = pw_add_type2("MyawError", MwExceptionData,
80                                      PW_PARENTS,
81                                         PwTypeId_Exception,
82                                      PW_INTERFACES,
83                                         PwInterfaceId_Basic, &myaw_error_basic_interface);
84    // init status codes
85    MweEndOfBlock = pw_define_status("End of block");
86    MweParseError = pw_define_status("Parse error");
87}