Skip to content

Commit

Permalink
fix(compiler): fix autocasting augment assignment always using =
Browse files Browse the repository at this point in the history
  • Loading branch information
JaDogg committed Nov 4, 2023
1 parent 5b5f323 commit a0d69a4
Show file tree
Hide file tree
Showing 6 changed files with 188 additions and 2 deletions.
2 changes: 1 addition & 1 deletion compiler/src/compiler/compiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ void compiler::perform_assign(std::pair<std::string, ykobject> &lhs,
(lhs.second.datatype_->const_unwrap()->is_a_number() ||
lhs.second.datatype_->const_unwrap()->is_bool())) {
// Number cast that we can do here
body_ << lhs.first << " = ";
body_ << lhs.first << operator_token->token_;
cast_numbers(castable, lhs, rhs);
if (lhs.second.is_primitive_or_obj() &&
lhs.second.datatype_->const_unwrap()->is_f32() &&
Expand Down
2 changes: 1 addition & 1 deletion compiler/src/compiler/type_checker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1144,7 +1144,7 @@ void type_checker::visit_cfor_stmt(cfor_stmt *obj) {
obj->comparison_->accept(this);
auto comp = pop();
if (!comp.datatype_->const_unwrap()->is_bool()) {
error(obj->semi1_, "Comparision must be a boolean operation");
error(obj->semi1_, "Comparison must be a boolean operation");
}
}
if (obj->operation_ != nullptr) { obj->operation_->accept(this); }
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
def main() -> int:
a = 0
b = True
for (x = 0; x < 10; x = x + 1):
a += b
println(a)
0
154 changes: 154 additions & 0 deletions compiler/test_data/compiler_tests/output/autocastboolcount.tokens
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
[
["output.c",0,1,"//","INT_DIV"],
["output.c",0,4,"YK","NAME"],
["output.c",0,6,"\n","NEW_LINE"],
["output.c",1,1,"include \"yk__lib.h\"","COMMENT"],
["output.c",1,21,"\n","NEW_LINE"],
["output.c",2,1,"//","INT_DIV"],
["output.c",2,4,"-","SUB"],
["output.c",2,5,"-","SUB"],
["output.c",2,6,"forward","NAME"],
["output.c",2,14,"declarations","NAME"],
["output.c",2,26,"-","SUB"],
["output.c",2,27,"-","SUB"],
["output.c",2,29,"\n","NEW_LINE"],
["output.c",3,1,"int32_t","NAME"],
["output.c",3,9,"yy__main","NAME"],
["output.c",3,17,"(","PAREN_OPEN"],
["output.c",3,18,")","PAREN_CLOSE"],
["output.c",3,19,";","SEMICOLON"],
["output.c",3,20,"\n","NEW_LINE"],
["output.c",4,1,"//","INT_DIV"],
["output.c",4,4,"-","SUB"],
["output.c",4,5,"-","SUB"],
["output.c",4,6,"structs","NAME"],
["output.c",4,13,"-","SUB"],
["output.c",4,14,"-","SUB"],
["output.c",4,16,"\n","NEW_LINE"],
["output.c",5,1,"//","INT_DIV"],
["output.c",5,4,"-","SUB"],
["output.c",5,5,"-","SUB"],
["output.c",5,6,"functions","NAME"],
["output.c",5,15,"-","SUB"],
["output.c",5,16,"-","SUB"],
["output.c",5,18,"\n","NEW_LINE"],
["output.c",6,1,"int32_t","NAME"],
["output.c",6,9,"yy__main","NAME"],
["output.c",6,17,"(","PAREN_OPEN"],
["output.c",6,18,")","PAREN_CLOSE"],
["output.c",6,20,"\n","NEW_LINE"],
["output.c",7,1,"{","CURLY_BRACKET_OPEN"],
["output.c",7,2,"\n","NEW_LINE"],
["output.c",8,1," ","INDENT"],
["output.c",8,5,"int32_t","NAME"],
["output.c",8,13,"yy__a","NAME"],
["output.c",8,19,"=","EQ"],
["output.c",8,21,"INT32_C","NAME"],
["output.c",8,28,"(","PAREN_OPEN"],
["output.c",8,29,"0","INTEGER_DECIMAL"],
["output.c",8,30,")","PAREN_CLOSE"],
["output.c",8,31,";","SEMICOLON"],
["output.c",8,32,"\n","NEW_LINE"],
["output.c",9,1," ","INDENT"],
["output.c",9,5,"bool","NAME"],
["output.c",9,10,"yy__b","NAME"],
["output.c",9,16,"=","EQ"],
["output.c",9,18,"true","NAME"],
["output.c",9,22,";","SEMICOLON"],
["output.c",9,23,"\n","NEW_LINE"],
["output.c",10,1," ","INDENT"],
["output.c",10,5,"for","KEYWORD_FOR"],
["output.c",10,9,"(","PAREN_OPEN"],
["output.c",10,10,"int32_t","NAME"],
["output.c",10,18,"yy__x","NAME"],
["output.c",10,24,"=","EQ"],
["output.c",10,26,"INT32_C","NAME"],
["output.c",10,33,"(","PAREN_OPEN"],
["output.c",10,34,"0","INTEGER_DECIMAL"],
["output.c",10,35,")","PAREN_CLOSE"],
["output.c",10,36,";","SEMICOLON"],
["output.c",10,37,"(","PAREN_OPEN"],
["output.c",10,38,"yy__x","NAME"],
["output.c",10,44,"<","LESS"],
["output.c",10,46,"INT32_C","NAME"],
["output.c",10,53,"(","PAREN_OPEN"],
["output.c",10,54,"10","INTEGER_DECIMAL"],
["output.c",10,56,")","PAREN_CLOSE"],
["output.c",10,57,")","PAREN_CLOSE"],
["output.c",10,58,";","SEMICOLON"],
["output.c",10,59,"yy__x","NAME"],
["output.c",10,65,"=","EQ"],
["output.c",10,67,"(","PAREN_OPEN"],
["output.c",10,68,"yy__x","NAME"],
["output.c",10,74,"+","PLUS"],
["output.c",10,76,"INT32_C","NAME"],
["output.c",10,83,"(","PAREN_OPEN"],
["output.c",10,84,"1","INTEGER_DECIMAL"],
["output.c",10,85,")","PAREN_CLOSE"],
["output.c",10,86,")","PAREN_CLOSE"],
["output.c",10,87,")","PAREN_CLOSE"],
["output.c",10,88,"\n","NEW_LINE"],
["output.c",11,1," ","INDENT"],
["output.c",11,5,"{","CURLY_BRACKET_OPEN"],
["output.c",11,6,"\n","NEW_LINE"],
["output.c",12,1," ","INDENT"],
["output.c",12,9,"yy__a","NAME"],
["output.c",12,14,"+=","PLUS_EQ"],
["output.c",12,16,"(","PAREN_OPEN"],
["output.c",12,17,"(","PAREN_OPEN"],
["output.c",12,18,"int32_t","NAME"],
["output.c",12,25,")","PAREN_CLOSE"],
["output.c",12,26,"(","PAREN_OPEN"],
["output.c",12,27,"(","PAREN_OPEN"],
["output.c",12,28,"(","PAREN_OPEN"],
["output.c",12,29,"yy__b","NAME"],
["output.c",12,34,")","PAREN_CLOSE"],
["output.c",12,38,"1","INTEGER_DECIMAL"],
["output.c",12,40,":","COLON"],
["output.c",12,42,"0","INTEGER_DECIMAL"],
["output.c",12,43,")","PAREN_CLOSE"],
["output.c",12,44,")","PAREN_CLOSE"],
["output.c",12,45,")","PAREN_CLOSE"],
["output.c",12,46,";","SEMICOLON"],
["output.c",12,47,"\n","NEW_LINE"],
["output.c",13,1," ","INDENT"],
["output.c",13,5,"}","CURLY_BRACKET_CLOSE"],
["output.c",13,6,"\n","NEW_LINE"],
["output.c",14,1," ","INDENT"],
["output.c",14,5,"yk__printlnint","NAME"],
["output.c",14,19,"(","PAREN_OPEN"],
["output.c",14,20,"(","PAREN_OPEN"],
["output.c",14,21,"intmax_t","NAME"],
["output.c",14,29,")","PAREN_CLOSE"],
["output.c",14,30,"yy__a","NAME"],
["output.c",14,35,")","PAREN_CLOSE"],
["output.c",14,36,";","SEMICOLON"],
["output.c",14,37,"\n","NEW_LINE"],
["output.c",15,1," ","INDENT"],
["output.c",15,5,"return","KEYWORD_RETURN"],
["output.c",15,12,"INT32_C","NAME"],
["output.c",15,19,"(","PAREN_OPEN"],
["output.c",15,20,"0","INTEGER_DECIMAL"],
["output.c",15,21,")","PAREN_CLOSE"],
["output.c",15,22,";","SEMICOLON"],
["output.c",15,23,"\n","NEW_LINE"],
["output.c",16,1,"}","CURLY_BRACKET_CLOSE"],
["output.c",16,2,"\n","NEW_LINE"],
["output.c",17,1,"if defined(YK__MINIMAL_MAIN)","COMMENT"],
["output.c",17,30,"\n","NEW_LINE"],
["output.c",18,1,"int","NAME"],
["output.c",18,5,"main","NAME"],
["output.c",18,9,"(","PAREN_OPEN"],
["output.c",18,10,"void","NAME"],
["output.c",18,14,")","PAREN_CLOSE"],
["output.c",18,16,"{","CURLY_BRACKET_OPEN"],
["output.c",18,18,"return","KEYWORD_RETURN"],
["output.c",18,25,"yy__main","NAME"],
["output.c",18,33,"(","PAREN_OPEN"],
["output.c",18,34,")","PAREN_CLOSE"],
["output.c",18,35,";","SEMICOLON"],
["output.c",18,37,"}","CURLY_BRACKET_CLOSE"],
["output.c",18,38,"\n","NEW_LINE"],
["output.c",19,1,"endif","COMMENT"],
["output.c",19,8,"","END_OF_FILE"]
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// YK
#include "yk__lib.h"
// --forward declarations--
int32_t yy__main();
// --structs--
// --functions--
int32_t yy__main()
{
int32_t yy__a = INT32_C(0);
bool yy__b = true;
for (int32_t yy__x = INT32_C(0);(yy__x < INT32_C(10));yy__x = (yy__x + INT32_C(1)))
{
yy__a+=((int32_t)(((yy__b) ? 1 : 0)));
}
yk__printlnint((intmax_t)yy__a);
return INT32_C(0);
}
#if defined(YK__MINIMAL_MAIN)
int main(void) { return yy__main(); }
#endif
5 changes: 5 additions & 0 deletions compiler/tests/test_compiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -478,3 +478,8 @@ TEST_CASE("compiler: c like for loop") {
"../test_data/compiler_tests/for_loop_test.yaka", "for_loop_test.yaka",
"../test_data/compiler_tests/output/for_loop_test.tokens");
}
TEST_CASE("compiler: auto cast bool count") {
test_compile_yaka_file(
"../test_data/compiler_tests/auto_casting/autocastboolcount.yaka", "autocastboolcount.yaka",
"../test_data/compiler_tests/output/autocastboolcount.tokens");
}

0 comments on commit a0d69a4

Please sign in to comment.