forked from lcompilers/lpython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcwrapper.cpp
More file actions
91 lines (68 loc) · 2.21 KB
/
cwrapper.cpp
File metadata and controls
91 lines (68 loc) · 2.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include <cstdlib>
#include <cstring>
#include <lpython/cwrapper.h>
#include <lpython/ast.h>
#include <libasr/alloc.h>
#include <lpython/parser/parser.h>
#include <lpython/pickle.h>
extern "C" {
#define CWRAPPER_BEGIN try {
#define CWRAPPER_END \
return LFORTRAN_NO_EXCEPTION; \
} \
catch (LFortran::LFortranException & e) \
{ \
return e.error_code(); \
} \
catch (...) \
{ \
return LFORTRAN_RUNTIME_ERROR; \
}
struct LFortranCParser {
Allocator al;
LFortranCParser() : al{1024*1024} {}
};
struct lfortran_ast_t {
LFortran::AST::ast_t m;
};
LFortranCParser *lfortran_parser_new()
{
return new LFortranCParser;
}
void lfortran_parser_free(LFortranCParser *self)
{
delete self;
}
lfortran_exceptions_t lfortran_parser_parse(LFortranCParser *self,
const char *input, lfortran_ast_t **ast)
{
CWRAPPER_BEGIN
LFortran::AST::ast_t* result;
LFortran::diag::Diagnostics diagnostics;
LFortran::Result<LFortran::AST::TranslationUnit_t*> res
= LFortran::parse(self->al, input, diagnostics);
if (res.ok) {
result = res.result->m_items[0];
} else {
return LFORTRAN_PARSER_ERROR;
}
lfortran_ast_t* result2 = (lfortran_ast_t*)result;
*ast = result2;
CWRAPPER_END
}
lfortran_exceptions_t lfortran_parser_pickle(lfortran_ast_t* ast,
char **str)
{
CWRAPPER_BEGIN
std::string p = LFortran::pickle(ast->m);
*str = new char[p.length()+1];
std::strcpy(*str, p.c_str());
CWRAPPER_END
}
lfortran_exceptions_t lfortran_str_free(char *str)
{
CWRAPPER_BEGIN
delete[] str;
CWRAPPER_END
}
} // extern "C"