Skip to content

Commit

Permalink
fix(to_c_compiler): string literal should compile to a valid c string…
Browse files Browse the repository at this point in the history
… literal
  • Loading branch information
JaDogg committed Apr 27, 2024
1 parent b8c6449 commit 0f3fb7a
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 1 deletion.
3 changes: 2 additions & 1 deletion compiler/src/compiler/to_c_compiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -674,7 +674,8 @@ void to_c_compiler::visit_literal_expr(literal_expr *obj) {
if (data_type_tok == token_type::STRING ||
data_type_tok == token_type::THREE_QUOTE_STRING) {
ykobject str_lit_object = ykobject(obj->literal_token_->token_, dt_pool_);
push("<><>", str_lit_object);
std::string string_literal = "\"" + string_utils::escape(string_utils::unescape(obj->literal_token_->token_)) + "\"";
push(string_literal, str_lit_object);
} else if (obj->literal_token_->type_ == token_type::KEYWORD_TRUE) {
push("true", ykobject(dt_pool_->create("bool")));
} else if (obj->literal_token_->type_ == token_type::KEYWORD_FALSE) {
Expand Down
8 changes: 8 additions & 0 deletions compiler/test_data/bug_fixes/easy_cstr.yaka
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import libs.c

def myfunc(d: c.CStr) -> None:
pass

def main() -> int:
myfunc(cast("c.CStr", "hello\nworld\n"))
return 0
21 changes: 21 additions & 0 deletions compiler/test_data/bug_fixes/easy_cstr.yaka.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// YK
#include "yk__lib.h"
// --forward declarations--
#define yy__c_CStr char*
void yy__myfunc(yy__c_CStr);
int32_t yy__main();
// --structs--
// --functions--
void yy__myfunc(yy__c_CStr yy__d)
{
// pass;
return;
}
int32_t yy__main()
{
yy__myfunc(((yy__c_CStr)"hello\nworld\n"));
return INT32_C(0);
}
#if defined(YK__MINIMAL_MAIN)
int main(void) { return yy__main(); }
#endif
3 changes: 3 additions & 0 deletions compiler/tests/test_compiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,9 @@ TEST_CASE("compiler: fixed arrays - structure data types") {
TEST_CASE("compiler: bug-fix - access struct str member") {
test_compile_yaka_file("../test_data/bug_fixes/struct_str_return.yaka");
}
TEST_CASE("compiler: bug-fix - cast string literal should work as expected") {
test_compile_yaka_file("../test_data/bug_fixes/easy_cstr.yaka");
}
TEST_CASE("compiler: directive - ccode") {
test_compile_yaka_file("../test_data/compiler_tests/directives/directive_ccode.yaka");
}
Expand Down

0 comments on commit 0f3fb7a

Please sign in to comment.