Thread: Text printing issue

  1. #1
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,733

    Text printing issue

    So started trying to get my compiler to generate informative errors to some extent (as it is yet to understand code for now) and I ran into a confusing error where it does not print the text currently being analyzed, I'm struggling to see what I did wrong so I'm hoping someone here can, 1st the output:
    Code:
    ...
    clang	-o mitsy.elf mitsy.o memory.o
    ./mitsy.elf
    mitsy alpha
    Opening test.c...
    Allocating text buffer...
    Passing execution pool & text buffer to compiler...
    Entering compiler loop...
    Checking Type ...
    test.c:1:1: Unknown type
    Compilation Failed: ret = 1, errno = 0
    makefile:41: recipe for target 'run' failed
    make: *** [run] Error 1
    Compilation failed.
    Now the related bits of code:
    Code:
    size_t wordlen( char * const text ) {
    	size_t len = 0;
    	if ( !text ) return 0;
    	switch ( *text ) {
    		case '(': case ')':
    		case '[': case ']':
    		case '{': case '}':
    		case '=': case '%':
    		case '*': case '/':
    		case '+': case '-':
    		case '.': case ':':
    		return 0;
    	}
    	do {
    		if ( !text[len] || isspace( text[len] ) == 0 ) {
    			return len;
    		}
    		++len;
    	} while ( 1 );
    	return len;
    }
    ...
    		default:
    			len = wordlen( text );
    			c = text[len];
    			text[len] = 0;
    			printf( "Checking Type %s...\n", text );
    			block = IsKnownType( *cc_exec_content, text );
    			if ( !block ) {
    				printf( "%s:%d:%d: Unknown type %s\n", path, line, pos, text );
    				return 1;
    			}
    			text[len] = c;
    The first error should have read:
    Code:
    Checking Type int...
    test.c:1:1: Unknown type int
    As the content of test.c is just this:
    Code:
    int main( int argc, char **argc ) { return 0; }
    Last edited by awsdert; 05-17-2019 at 03:09 AM. Reason: Forgot to make clear the code is not directly next to each other

  2. #2
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,733
    Just occured to me to double check what the return value of isspace() means, turns out it doesn't follow the accepted pattern of 0 means true, anything else is an error when returning an int, corrected that and got my expected results, now onto adding the int type so it can choke on the name or bracket next (got some code for the name but not sure it will get past it)

  3. #3
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    945
    Quote Originally Posted by awsdert View Post
    Just occured to me to double check what the return value of isspace() means, turns out it doesn't follow the accepted pattern of 0 means true, anything else is an error when returning an int, corrected that and got my expected results
    isspace() returns a "boolean" value, and the accepted pattern is zero for false and non-zero for true. There are no error conditions for isspace().

  4. #4
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,733
    Quote Originally Posted by christop View Post
    isspace() returns a "boolean" value, and the accepted pattern is zero for false and non-zero for true. There are no error conditions for isspace().
    Did you not notice the "when returning an int" part? If it was to be boolean it should have at least been unsigned int or had a typedef of bool_t, boolean_t or something that was clear about its usage, declaring the function to return type int in general implies it follows the pattern of 0 = success/match and anything else being an error/mismatch of some sort

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by awsdert
    If it was to be boolean it should have at least been unsigned int or had a typedef of bool_t, boolean_t or something that was clear about its usage, declaring the function to return type int in general implies it follows the pattern of 0 = success/match and anything else being an error/mismatch of some sort
    Unfortunately, as you may recall C historically had no standard boolean type other than int, which as you rightly noted might also be used to denote a return code rather than a boolean... although in such cases it may be clearer to declare an enum. So what christop wrote is true: for isspace the int denotes a boolean value, and contrary to your assertion that this doesn't follow an accepted pattern, it in fact does follow the longstanding tradition of int being used as the boolean type in C. If we were designing isspace today, post-C99, perhaps we would make bool its return type, but alas it was designed even before C was standardised.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,733
    didn't say bool, I said bool_t as in a typedef or define, I understand what you're saying about the history thing but that doesn't change the fact that they could've used a typedef or define if typedef wasn't available to give a clear type name

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I agree. On the other hand, I note that int is also not a "clear type name" when you wrote that: "declaring the function to return type int in general implies it follows the pattern of 0 = success/match and anything else being an error/mismatch of some sort". In other words, because of the historical usage, this is not true. Just as they could have had a typedef for int to be aliased as a boolean type when first drafting the standard, likewise an enum could be used instead of just int for error return codes. The int type simply is too general, e.g., a compute_age function might return an int that means an age in years, not an error return code.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,733
    Do also note I said "in general", I also agree about the enum or at least a typedef to something like exit_code_t woulda been better for those too

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Yes, I note that, and I note that that is not true: it isn't true in general that an int return type means an error return code. In general, we have to refer to the function's documentation or code.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078
    Quote Originally Posted by awsdert View Post
    didn't say bool, I said bool_t as in a typedef or define, I understand what you're saying about the history thing but that doesn't change the fact that they could've used a typedef or define if typedef wasn't available to give a clear type name
    Neither bool or bool_t, the standard type from C99 and above is _Bool.

  11. #11
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,733
    Quote Originally Posted by flp1969 View Post
    Neither bool or bool_t, the standard type from C99 and above is _Bool.
    They only used that cause bool was taken, at thr time these functions were made I don't think even C++ existed so it woulda been perfectly fine to use bool_t or boolean_t

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. issue while printing
    By Romyo2 in forum C Programming
    Replies: 5
    Last Post: 06-17-2015, 07:48 AM
  2. printing's issue.
    By RyanC in forum C Programming
    Replies: 36
    Last Post: 06-11-2015, 08:24 AM
  3. need with debaging - printing issue
    By ilans11il in forum C Programming
    Replies: 5
    Last Post: 02-26-2013, 08:32 AM
  4. printing issue
    By ilans11il in forum C Programming
    Replies: 3
    Last Post: 02-21-2013, 12:53 AM
  5. Strange issue with printing array value
    By CodeKate in forum C Programming
    Replies: 6
    Last Post: 11-24-2010, 04:52 PM

Tags for this Thread