Skip to content

Commit

Permalink
add pointers support for ffi parse
Browse files Browse the repository at this point in the history
  • Loading branch information
astrophysik committed Nov 21, 2022
1 parent 5d90e2b commit 84e9e21
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 11 deletions.
1 change: 1 addition & 0 deletions Compiler/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ FetchContent_Declare(
# For Windows: Prevent overriding the parent project's compiler/linker settings
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
FetchContent_MakeAvailable(googletest)
find_package(Threads)

set(COMPILER_SRC AST/ast_nodes.cpp helpers/char_source.cpp src/lexer.cpp src/parser.cpp src/translator.cpp src/compiler.cpp)

Expand Down
2 changes: 1 addition & 1 deletion Compiler/helpers/char_source.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ void char_source::back() {
}

bool char_source::has_next() {
return _ifstream && _ifstream.peek() != EOF;
return _ifstream && !_ifstream.eof();
}

uint32_t char_source::char_pos() {
Expand Down
28 changes: 20 additions & 8 deletions Compiler/src/lexer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,21 +31,19 @@ void lexer::skip_white_space() {
}
}
}
/* */

void lexer::skip_comment() {
if (_source.next() != '/') {
_source.back();
return;
}
char ch = _source.next();
if (ch == '/') {
go_to_enter();
} else {
size_t pos = _source.lines_pos();
if (ch == '\n') {
pos--;
if (ch == '*') {
ch = _source.next();
while (!(ch == '*' && _source.next() == '/')) {
ch = _source.next();
}
throw compile_exception("\n" + std::to_string(pos) + " | One slash instead of two expected\n");
}
}

Expand Down Expand Up @@ -87,5 +85,19 @@ std::optional<token> lexer::next_token() {
if (it == _token_type_list.end()) {
throw compile_exception("\n" + std::to_string(_source.lines_pos()) + " | Invalid language token '" + current + "'\n");
}
return {token(current, it->second, _source.char_pos() - current.size() + 1)};
token tkn = token(current, it->second, _source.char_pos() - current.size() + 1);
/*hack for c type*/
if (it->second.name == "ctype") {
while (_source.has_next()) {
skip_white_space();
ch = _source.next();
if (ch == '*') {
tkn.value += ch;
} else {
_source.back();
break;
}
}
}
return {tkn};
}
7 changes: 5 additions & 2 deletions Compiler/src/parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,17 @@ std::list<std::shared_ptr<ffi_arg>> parser::parse_ffi_args() {

std::list<std::shared_ptr<ffi_arg>> parser::parse_ffi_non_empty_args() {
auto arg_type = require({_token_type_list.at("ctype")});
auto arg_name = require({_token_type_list.at("variable")});
auto arg_name = match({_token_type_list.at("variable")});
if (!arg_name) {
arg_name = token("", _token_type_list.at("variable"), arg_type.pos + 1);
}
auto comma = match({_token_type_list.at("comma")});
std::list<std::shared_ptr<ffi_arg>> tail;
if (comma) {
tail = parse_ffi_non_empty_args();
}
auto head = std::make_shared<ffi_arg>(
std::make_shared<ctype_node>(arg_type), std::make_shared<variable_node>(arg_name));
std::make_shared<ctype_node>(arg_type), std::make_shared<variable_node>(arg_name.value()));
tail.push_front(head);
return tail;
}
Expand Down
19 changes: 19 additions & 0 deletions Compiler/tests/cpp/ast/simple-ast-tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -257,4 +257,23 @@ TEST(SimpleAstTests, ast_ffi_test) {
ASSERT_EQ(args.begin()->get()->arg_name->variable.value, "c");
ASSERT_EQ((++args.begin())->get()->arg_type->ctype.value, "char");
ASSERT_EQ((++args.begin())->get()->arg_name->variable.value, "_gGggaa123");

src = "int memcpy(void ** dst, void "
"*** src, int \tn);";
root = parse_to_ast(src);

decl = dynamic_cast<ffi_func_decl *>(root->expressions.begin()->get());
ASSERT_TRUE(decl);
return_type = decl->return_type.get();
func_name = decl->func_name.get();
args = decl->args;
ASSERT_EQ(return_type->ctype.value, "int");
ASSERT_EQ(func_name->variable.value, "memcpy");
ASSERT_EQ(args.size(), 3);
ASSERT_EQ(args.begin()->get()->arg_type->ctype.value, "void**");
ASSERT_EQ(args.begin()->get()->arg_name->variable.value, "dst");
ASSERT_EQ((++args.begin())->get()->arg_type->ctype.value, "void***");
ASSERT_EQ((++args.begin())->get()->arg_name->variable.value, "src");
ASSERT_EQ((++++args.begin())->get()->arg_type->ctype.value, "int");
ASSERT_EQ((++++args.begin())->get()->arg_name->variable.value, "n");
}

0 comments on commit 84e9e21

Please sign in to comment.