Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG REPORT] Does not allow single line block statements without { } #27

Closed
frankhart2018 opened this issue Sep 2, 2020 · 16 comments
Closed
Assignees
Labels
bug Something isn't working easy Easy problem KWoC'20 These issues are being listed as part of KWoC'20

Comments

@frankhart2018
Copy link
Member

Describe the bug
Error when braces not included after if or any other block statement, when it should take only the immediate line into account.

To Reproduce
simC code:-

MAIN
  var i = 0
  if(i % 2 == 0)
    print("Even")
  else
    print("Odd")
END_MAIN

C Code:-
Not generated, throws error.

Expected behavior
C Code:-

int main() {
    int i = 0;
    if(i % 2 == 0)
        print("Even")
    else
        print("Odd")

    return 0;
}
@frankhart2018 frankhart2018 added bug Something isn't working hacktoberfest Part of hacktoberfest easy Easy problem labels Sep 2, 2020
@frankhart2018 frankhart2018 removed the hacktoberfest Part of hacktoberfest label Nov 1, 2020
@Math-O5
Copy link
Collaborator

Math-O5 commented Nov 21, 2020

@frankhart2018 May I be assigned?

@frankhart2018
Copy link
Member Author

Sure @Math-O5. Thanks for showing interest to contribute.

@lolzone13
Copy link

So, in this issue, after every code block statement (like if), we have to check for spaces and enter braces accordingly right?

@frankhart2018
Copy link
Member Author

No I guess you misunderstood @lolzone13. What this issue means is C allows code without a { } for block statements like if or else or loops when there is only a single statement in that block. For eg:-

if(1 == 2)
printf("Hello World")

This is something which C allows but simC doesn't at this moment.

Hint: The change has to be made only in simc_parser.py :)

@lolzone13
Copy link

So I can check if the code block has a single statement and then I can make the changes right?

@frankhart2018
Copy link
Member Author

Yeah. Do you want to take this?

@lolzone13
Copy link

Yeah, I'll give it a shot.

@Math-O5 Math-O5 removed their assignment Dec 7, 2020
@cimplec cimplec deleted a comment from frankhart2018 Dec 8, 2020
@lolzone13 lolzone13 removed their assignment Dec 9, 2020
@shobhit10058
Copy link
Contributor

Since no one was an assignee here, I was trying this. With some modifications, I got this output -

#include <stdio.h>

int main() {
	int i = 0;
	if(i % 2 == 0) 	printf("Even");
	else 	printf("Odd");

	return 0;
}

for

MAIN
  var i = 0
  if(i % 2 == 0)
    print("Even")
  else
    print("Odd")
END_MAIN

I just remove those errors so that this works out.
But I have not included checks for scenarios like this -

MAIN
  var i = 0
  if(i % 2 == 0)
    print("Even")
    print("Even")
  else
    print("Odd")
END_MAIN

for which output is -

#include <stdio.h>

int main() {
	int i = 0;
	if(i % 2 == 0) 	printf("Even");
	printf("Even");
	else 	printf("Odd");

	return 0;
}

If this much is fine for now, then I can send a PR.

@frankhart2018
Copy link
Member Author

Hey @shobhit10058 this issue is for all types of block statements and not only for if statements. This is what I added in #341 when @Math-O5 created the PR. So if you are presenting a solution it should be for all block statements that simC currently supports and not just if else.

@shobhit10058
Copy link
Contributor

oh, ok got it!!

@Math-O5
Copy link
Collaborator

Math-O5 commented Dec 10, 2020

@shobhit10058 After you finish I can try check the case where there is more than one instruction before else without braces.

I'm planning in fact check the entire structure in cascade:

    if statment:
          if statment
             if...
          else cmd
    else cmd

So, let me know when you finish I'll submt PR in your fork.

@shobhit10058
Copy link
Contributor

So, I think the following are the block statements you are looking to adding this support to -

  1. if/else
  2. for/while/do-while
  3. functions
    I don't think switch is used as a single statement anytime.

@shobhit10058
Copy link
Contributor

I have done for

  1. if/else
  2. for/while/do-while
    for functions I think there is some error due to which I cannot test it
fun sum(a,b){
	return a + b
}

gives a error
image
for other statements -

	var i = 0
	while(i < 10)
		print(i ++)
	if(i > 1)
		print(i)
	else
		print(1)
	for i in 1 to 6 by +2 
		print(i)
	do
		print(i)
		i++;
	while(i<20)
END_MAIN

the c code is -

#include <stdio.h>

int main() {
	int i = 0;
	while(i < 10) 	printf("%d", i ++ );
	if(i > 1) 	printf("%d", i);
	else 	printf("%d", 1);
	for(int i = 1; i < 6; i+=2) 	printf("%d", i);
	do {
	printf("%d", i);
	i++;
	}
	while(i < 20);
	return 0;
}

for do-while statements now the user has to just write the body between the do and while it will automatically insert the left and right braces. so the body of do-while can contain anything.
If this is ok, then I can make a PR.
I was trying to run main.py in test folder but it gave error. Also I tried to check the original repo by cloning it elsewhere and running the main.py in test folder it gave the same errors.
Following are they -
for this repo -

................................EEEEEEEEEEEEEE.............
======================================================================
ERROR: test_lexical_analyze_address_of (test_lexical_analyzer.TestLexicalAnalyzer)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/shobhit/sim-c/test/test_lexical_analyzer.py", line 254, in test_lexical_analyze_address_of
    self.assertEqual(tokens[-3], address_of)
IndexError: tuple index out of range

======================================================================
ERROR: test_lexical_analyze_assignment_equal (test_lexical_analyzer.TestLexicalAnalyzer)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/shobhit/sim-c/test/test_lexical_analyzer.py", line 171, in test_lexical_analyze_assignment_equal
    self.assertEqual(tokens[2], assignment)
IndexError: tuple index out of range

======================================================================
ERROR: test_lexical_analyze_colon (test_lexical_analyzer.TestLexicalAnalyzer)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/shobhit/sim-c/test/test_lexical_analyzer.py", line 395, in test_lexical_analyze_colon
    self.assertEqual(tokens[0], colon)
  File "/usr/lib/python3.8/unittest/case.py", line 912, in assertEqual
    assertion_func(first, second, msg=msg)
  File "/usr/lib/python3.8/unittest/case.py", line 902, in _baseAssertEqual
    if not first == second:
  File "/home/shobhit/KWOC/sim-c/simc/token_class.py", line 46, in __eq__
    self.type == other.type
AttributeError: 'list' object has no attribute 'type'

======================================================================
ERROR: test_lexical_analyze_comma (test_lexical_analyzer.TestLexicalAnalyzer)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/shobhit/sim-c/test/test_lexical_analyzer.py", line 319, in test_lexical_analyze_comma
    self.assertEqual(tokens[1], comma)
  File "/usr/lib/python3.8/unittest/case.py", line 912, in assertEqual
    assertion_func(first, second, msg=msg)
  File "/usr/lib/python3.8/unittest/case.py", line 902, in _baseAssertEqual
    if not first == second:
  File "/home/shobhit/KWOC/sim-c/simc/token_class.py", line 46, in __eq__
    self.type == other.type
AttributeError: 'list' object has no attribute 'type'

======================================================================
ERROR: test_lexical_analyze_divide_single_line_multi_line (test_lexical_analyzer.TestLexicalAnalyzer)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/shobhit/sim-c/test/test_lexical_analyzer.py", line 283, in test_lexical_analyze_divide_single_line_multi_line
    self.assertEqual(tokens[4], divide)
IndexError: tuple index out of range

======================================================================
ERROR: test_lexical_analyze_greater_than_greater_than_equal_right_shift (test_lexical_analyzer.TestLexicalAnalyzer)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/shobhit/sim-c/test/test_lexical_analyzer.py", line 355, in test_lexical_analyze_greater_than_greater_than_equal_right_shift
    self.assertEqual(tokens[1], greater_than)
  File "/usr/lib/python3.8/unittest/case.py", line 912, in assertEqual
    assertion_func(first, second, msg=msg)
  File "/usr/lib/python3.8/unittest/case.py", line 902, in _baseAssertEqual
    if not first == second:
  File "/home/shobhit/KWOC/sim-c/simc/token_class.py", line 46, in __eq__
    self.type == other.type
AttributeError: 'list' object has no attribute 'type'

======================================================================
ERROR: test_lexical_analyze_left_right_brace_newline (test_lexical_analyzer.TestLexicalAnalyzer)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/shobhit/sim-c/test/test_lexical_analyzer.py", line 152, in test_lexical_analyze_left_right_brace_newline
    self.assertEqual(tokens[7], left_brace)
IndexError: tuple index out of range

======================================================================
ERROR: test_lexical_analyze_left_right_paren_call_end (test_lexical_analyzer.TestLexicalAnalyzer)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/shobhit/sim-c/test/test_lexical_analyzer.py", line 131, in test_lexical_analyze_left_right_paren_call_end
    self.assertEqual(tokens[3], left_paren)
IndexError: tuple index out of range

======================================================================
ERROR: test_lexical_analyze_less_than_less_than_equal_left_shift (test_lexical_analyzer.TestLexicalAnalyzer)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/shobhit/sim-c/test/test_lexical_analyzer.py", line 378, in test_lexical_analyze_less_than_less_than_equal_left_shift
    self.assertEqual(tokens[1], less_than)
  File "/usr/lib/python3.8/unittest/case.py", line 912, in assertEqual
    assertion_func(first, second, msg=msg)
  File "/usr/lib/python3.8/unittest/case.py", line 902, in _baseAssertEqual
    if not first == second:
  File "/home/shobhit/KWOC/sim-c/simc/token_class.py", line 46, in __eq__
    self.type == other.type
AttributeError: 'list' object has no attribute 'type'

======================================================================
ERROR: test_lexical_analyze_minus_equal_decrement_minus (test_lexical_analyzer.TestLexicalAnalyzer)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/shobhit/sim-c/test/test_lexical_analyzer.py", line 216, in test_lexical_analyze_minus_equal_decrement_minus
    self.assertEqual(tokens[8], minus_equal)
IndexError: tuple index out of range

======================================================================
ERROR: test_lexical_analyze_modulus_equal_modulus (test_lexical_analyzer.TestLexicalAnalyzer)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/shobhit/sim-c/test/test_lexical_analyzer.py", line 303, in test_lexical_analyze_modulus_equal_modulus
    self.assertEqual(tokens[8], modulus_equal)
IndexError: tuple index out of range

======================================================================
ERROR: test_lexical_analyze_multiply_equal_multiply (test_lexical_analyzer.TestLexicalAnalyzer)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/shobhit/sim-c/test/test_lexical_analyzer.py", line 237, in test_lexical_analyze_multiply_equal_multiply
    self.assertEqual(tokens[8], multiply_equal)
IndexError: tuple index out of range

======================================================================
ERROR: test_lexical_analyze_not_equal (test_lexical_analyzer.TestLexicalAnalyzer)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/shobhit/sim-c/test/test_lexical_analyzer.py", line 334, in test_lexical_analyze_not_equal
    self.assertEqual(tokens[1], not_equal)
  File "/usr/lib/python3.8/unittest/case.py", line 912, in assertEqual
    assertion_func(first, second, msg=msg)
  File "/usr/lib/python3.8/unittest/case.py", line 902, in _baseAssertEqual
    if not first == second:
  File "/home/shobhit/KWOC/sim-c/simc/token_class.py", line 46, in __eq__
    self.type == other.type
AttributeError: 'list' object has no attribute 'type'

======================================================================
ERROR: test_lexical_analyze_plus_equal_increment_plus (test_lexical_analyzer.TestLexicalAnalyzer)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/shobhit/sim-c/test/test_lexical_analyzer.py", line 193, in test_lexical_analyze_plus_equal_increment_plus
    self.assertEqual(tokens[8], plus_equal)
IndexError: tuple index out of range

----------------------------------------------------------------------
Ran 59 tests in 0.016s

FAILED (errors=14)

with my changes -

................................EEEEEEEEEEEEEE.............
======================================================================
ERROR: test_lexical_analyze_address_of (test_lexical_analyzer.TestLexicalAnalyzer)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/shobhit/KWOC/sim-c/test/test_lexical_analyzer.py", line 254, in test_lexical_analyze_address_of
    self.assertEqual(tokens[-3], address_of)
IndexError: tuple index out of range

======================================================================
ERROR: test_lexical_analyze_assignment_equal (test_lexical_analyzer.TestLexicalAnalyzer)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/shobhit/KWOC/sim-c/test/test_lexical_analyzer.py", line 171, in test_lexical_analyze_assignment_equal
    self.assertEqual(tokens[2], assignment)
IndexError: tuple index out of range

======================================================================
ERROR: test_lexical_analyze_colon (test_lexical_analyzer.TestLexicalAnalyzer)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/shobhit/KWOC/sim-c/test/test_lexical_analyzer.py", line 395, in test_lexical_analyze_colon
    self.assertEqual(tokens[0], colon)
  File "/usr/lib/python3.8/unittest/case.py", line 912, in assertEqual
    assertion_func(first, second, msg=msg)
  File "/usr/lib/python3.8/unittest/case.py", line 902, in _baseAssertEqual
    if not first == second:
  File "/home/shobhit/KWOC/sim-c/simc/token_class.py", line 46, in __eq__
    self.type == other.type
AttributeError: 'list' object has no attribute 'type'

======================================================================
ERROR: test_lexical_analyze_comma (test_lexical_analyzer.TestLexicalAnalyzer)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/shobhit/KWOC/sim-c/test/test_lexical_analyzer.py", line 319, in test_lexical_analyze_comma
    self.assertEqual(tokens[1], comma)
  File "/usr/lib/python3.8/unittest/case.py", line 912, in assertEqual
    assertion_func(first, second, msg=msg)
  File "/usr/lib/python3.8/unittest/case.py", line 902, in _baseAssertEqual
    if not first == second:
  File "/home/shobhit/KWOC/sim-c/simc/token_class.py", line 46, in __eq__
    self.type == other.type
AttributeError: 'list' object has no attribute 'type'

======================================================================
ERROR: test_lexical_analyze_divide_single_line_multi_line (test_lexical_analyzer.TestLexicalAnalyzer)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/shobhit/KWOC/sim-c/test/test_lexical_analyzer.py", line 283, in test_lexical_analyze_divide_single_line_multi_line
    self.assertEqual(tokens[4], divide)
IndexError: tuple index out of range

======================================================================
ERROR: test_lexical_analyze_greater_than_greater_than_equal_right_shift (test_lexical_analyzer.TestLexicalAnalyzer)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/shobhit/KWOC/sim-c/test/test_lexical_analyzer.py", line 355, in test_lexical_analyze_greater_than_greater_than_equal_right_shift
    self.assertEqual(tokens[1], greater_than)
  File "/usr/lib/python3.8/unittest/case.py", line 912, in assertEqual
    assertion_func(first, second, msg=msg)
  File "/usr/lib/python3.8/unittest/case.py", line 902, in _baseAssertEqual
    if not first == second:
  File "/home/shobhit/KWOC/sim-c/simc/token_class.py", line 46, in __eq__
    self.type == other.type
AttributeError: 'list' object has no attribute 'type'

======================================================================
ERROR: test_lexical_analyze_left_right_brace_newline (test_lexical_analyzer.TestLexicalAnalyzer)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/shobhit/KWOC/sim-c/test/test_lexical_analyzer.py", line 152, in test_lexical_analyze_left_right_brace_newline
    self.assertEqual(tokens[7], left_brace)
IndexError: tuple index out of range

======================================================================
ERROR: test_lexical_analyze_left_right_paren_call_end (test_lexical_analyzer.TestLexicalAnalyzer)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/shobhit/KWOC/sim-c/test/test_lexical_analyzer.py", line 131, in test_lexical_analyze_left_right_paren_call_end
    self.assertEqual(tokens[3], left_paren)
IndexError: tuple index out of range

======================================================================
ERROR: test_lexical_analyze_less_than_less_than_equal_left_shift (test_lexical_analyzer.TestLexicalAnalyzer)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/shobhit/KWOC/sim-c/test/test_lexical_analyzer.py", line 378, in test_lexical_analyze_less_than_less_than_equal_left_shift
    self.assertEqual(tokens[1], less_than)
  File "/usr/lib/python3.8/unittest/case.py", line 912, in assertEqual
    assertion_func(first, second, msg=msg)
  File "/usr/lib/python3.8/unittest/case.py", line 902, in _baseAssertEqual
    if not first == second:
  File "/home/shobhit/KWOC/sim-c/simc/token_class.py", line 46, in __eq__
    self.type == other.type
AttributeError: 'list' object has no attribute 'type'

======================================================================
ERROR: test_lexical_analyze_minus_equal_decrement_minus (test_lexical_analyzer.TestLexicalAnalyzer)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/shobhit/KWOC/sim-c/test/test_lexical_analyzer.py", line 216, in test_lexical_analyze_minus_equal_decrement_minus
    self.assertEqual(tokens[8], minus_equal)
IndexError: tuple index out of range

======================================================================
ERROR: test_lexical_analyze_modulus_equal_modulus (test_lexical_analyzer.TestLexicalAnalyzer)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/shobhit/KWOC/sim-c/test/test_lexical_analyzer.py", line 303, in test_lexical_analyze_modulus_equal_modulus
    self.assertEqual(tokens[8], modulus_equal)
IndexError: tuple index out of range

======================================================================
ERROR: test_lexical_analyze_multiply_equal_multiply (test_lexical_analyzer.TestLexicalAnalyzer)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/shobhit/KWOC/sim-c/test/test_lexical_analyzer.py", line 237, in test_lexical_analyze_multiply_equal_multiply
    self.assertEqual(tokens[8], multiply_equal)
IndexError: tuple index out of range

======================================================================
ERROR: test_lexical_analyze_not_equal (test_lexical_analyzer.TestLexicalAnalyzer)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/shobhit/KWOC/sim-c/test/test_lexical_analyzer.py", line 334, in test_lexical_analyze_not_equal
    self.assertEqual(tokens[1], not_equal)
  File "/usr/lib/python3.8/unittest/case.py", line 912, in assertEqual
    assertion_func(first, second, msg=msg)
  File "/usr/lib/python3.8/unittest/case.py", line 902, in _baseAssertEqual
    if not first == second:
  File "/home/shobhit/KWOC/sim-c/simc/token_class.py", line 46, in __eq__
    self.type == other.type
AttributeError: 'list' object has no attribute 'type'

======================================================================
ERROR: test_lexical_analyze_plus_equal_increment_plus (test_lexical_analyzer.TestLexicalAnalyzer)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/shobhit/KWOC/sim-c/test/test_lexical_analyzer.py", line 193, in test_lexical_analyze_plus_equal_increment_plus
    self.assertEqual(tokens[8], plus_equal)
IndexError: tuple index out of range

----------------------------------------------------------------------
Ran 59 tests in 0.063s

FAILED (errors=14)

@shobhit10058
Copy link
Contributor

@Math-O5 can you review?

@shobhit10058
Copy link
Contributor

ok, the function's code I mentioned earlier was not compiled because its parameter types and return type could not be judged as it was not used. This is working -

fun sum(a,b){
	return a + b
}
MAIN
	var i = 0
	while(i < 10)
		print(i ++)
	if(i > 1)
		print(i)
	else
		print(1)
	for i in 1 to 6 by +2 
		print(i)
	do
		print(i)
		i++;
	while(i<20)
	var c = sum(1,2)
END_MAIN

to

#include <stdio.h>

int sum(int a, int b) {

	return a + b;
}

int main() {
	int i = 0;
	while(i < 10) 	printf("%d", i ++ );
	if(i > 1) 	printf("%d", i);
	else 	printf("%d", 1);
	for(int i = 1; i < 6; i+=2) 	printf("%d", i);
	do {
	printf("%d", i);
	i++;
	}
	while(i < 20);	int c = sum(1, 2);

	return 0;
}

The function is not supported without braces in c also so I did not add for it now. But it can be extended feature.

@frankhart2018
Copy link
Member Author

Merged.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working easy Easy problem KWoC'20 These issues are being listed as part of KWoC'20
Projects
None yet
5 participants