diff --git a/cstdlib/stdio.c b/cstdlib/stdio.c index d416d99..756883a 100644 --- a/cstdlib/stdio.c +++ b/cstdlib/stdio.c @@ -9,6 +9,8 @@ FILE *CStdOut; +static int ZeroValue = 0; +static int TRUEValue = 1; static int EOFValue = EOF; static int SEEK_SETValue = SEEK_SET; static int SEEK_CURValue = SEEK_CUR; @@ -251,7 +253,7 @@ int StdioBasePrintf(struct ParseState *Parser, FILE *Stream, char *StrOut, int S if (ShowType == &IntType) { /* show a signed integer */ - if (IS_INTEGER_NUMERIC(ThisArg)) + if (IS_NUMERIC_COERCIBLE(ThisArg)) StdioFprintfWord(&SOStream, OneFormatBuf, ExpressionCoerceUnsignedInteger(ThisArg)); else StdioOutPuts("XXX", &SOStream); @@ -259,7 +261,7 @@ int StdioBasePrintf(struct ParseState *Parser, FILE *Stream, char *StrOut, int S else if (ShowType == &FPType) { /* show a floating point number */ - if (IS_FP(ThisArg)) + if (IS_NUMERIC_COERCIBLE(ThisArg)) StdioFprintfFP(&SOStream, OneFormatBuf, ExpressionCoerceFP(ThisArg)); else StdioOutPuts("XXX", &SOStream); @@ -681,6 +683,11 @@ void StdioSetupFunc(void) VariableDefinePlatformVar(NULL, "stdin", FilePtrType, (union AnyValue *)&stdinValue, FALSE); VariableDefinePlatformVar(NULL, "stdout", FilePtrType, (union AnyValue *)&stdoutValue, FALSE); VariableDefinePlatformVar(NULL, "stderr", FilePtrType, (union AnyValue *)&stderrValue, FALSE); + + /* define NULL, TRUE and FALSE */ + VariableDefinePlatformVar(NULL, "NULL", &IntType, (union AnyValue *)&ZeroValue, FALSE); + VariableDefinePlatformVar(NULL, "TRUE", &IntType, (union AnyValue *)&TRUEValue, FALSE); + VariableDefinePlatformVar(NULL, "FALSE", &IntType, (union AnyValue *)&ZeroValue, FALSE); } /* portability-related I/O calls */ diff --git a/tests/00_assignment.c b/tests/00_assignment.c index b174ab0..62adcec 100644 --- a/tests/00_assignment.c +++ b/tests/00_assignment.c @@ -1,3 +1,5 @@ +#include + int a; a = 42; printf("%d\n", a); diff --git a/tests/01_comment.c b/tests/01_comment.c index 6919b9e..d2b3ad9 100644 --- a/tests/01_comment.c +++ b/tests/01_comment.c @@ -1,3 +1,5 @@ +#include + printf("Hello\n"); printf("Hello\n"); /* this is a comment */ printf("Hello\n"); printf("Hello\n"); diff --git a/tests/02_printf.c b/tests/02_printf.c index 4523acb..66eed98 100644 --- a/tests/02_printf.c +++ b/tests/02_printf.c @@ -1,3 +1,5 @@ +#include + printf("Hello world\n"); int Count; diff --git a/tests/03_struct.c b/tests/03_struct.c index e7e7c1a..db62cc2 100644 --- a/tests/03_struct.c +++ b/tests/03_struct.c @@ -1,3 +1,5 @@ +#include + struct fred { int boris; diff --git a/tests/04_for.c b/tests/04_for.c index 4ce2257..046e473 100644 --- a/tests/04_for.c +++ b/tests/04_for.c @@ -1,3 +1,5 @@ +#include + int Count; for (Count = 1; Count <= 10; Count++) diff --git a/tests/05_array.c b/tests/05_array.c index 02a2883..a70ac0a 100644 --- a/tests/05_array.c +++ b/tests/05_array.c @@ -1,3 +1,5 @@ +#include + int Count; int Array[10]; diff --git a/tests/06_case.c b/tests/06_case.c index 9791ea1..d7f883d 100644 --- a/tests/06_case.c +++ b/tests/06_case.c @@ -1,3 +1,5 @@ +#include + int Count; for (Count = 0; Count < 4; Count++) diff --git a/tests/07_function.c b/tests/07_function.c index 7538945..e7c4fa7 100644 --- a/tests/07_function.c +++ b/tests/07_function.c @@ -1,3 +1,5 @@ +#include + int myfunc(int x) { return x * x; diff --git a/tests/08_while.c b/tests/08_while.c index 19fae8c..bb990b3 100644 --- a/tests/08_while.c +++ b/tests/08_while.c @@ -1,3 +1,5 @@ +#include + int a; int p; int t; diff --git a/tests/09_do_while.c b/tests/09_do_while.c index d9f63a7..adc4cae 100644 --- a/tests/09_do_while.c +++ b/tests/09_do_while.c @@ -1,3 +1,5 @@ +#include + int a; int p; int t; diff --git a/tests/10_pointer.c b/tests/10_pointer.c index 4206356..0e99787 100644 --- a/tests/10_pointer.c +++ b/tests/10_pointer.c @@ -1,3 +1,5 @@ +#include + int a; int *b; int c; diff --git a/tests/11_precedence.c b/tests/11_precedence.c index 954b7ef..7360312 100644 --- a/tests/11_precedence.c +++ b/tests/11_precedence.c @@ -1,3 +1,5 @@ +#include + int a; int b; int c; diff --git a/tests/12_hashdefine.c b/tests/12_hashdefine.c index e6278d9..680b87d 100644 --- a/tests/12_hashdefine.c +++ b/tests/12_hashdefine.c @@ -1,3 +1,5 @@ +#include + #define FRED 12 #define BLOGGS(x) (12*(x)) diff --git a/tests/13_integer_literals.c b/tests/13_integer_literals.c index be96c79..c0e99a1 100644 --- a/tests/13_integer_literals.c +++ b/tests/13_integer_literals.c @@ -1,3 +1,5 @@ +#include + int a = 24680; int b = 01234567; int c = 0x2468ac; diff --git a/tests/14_if.c b/tests/14_if.c index 6a1d43a..58dbed1 100644 --- a/tests/14_if.c +++ b/tests/14_if.c @@ -1,3 +1,5 @@ +#include + int a = 1; if (a) diff --git a/tests/15_recursion.c b/tests/15_recursion.c index d4926bb..766aa87 100644 --- a/tests/15_recursion.c +++ b/tests/15_recursion.c @@ -1,3 +1,5 @@ +#include + int factorial(int i) { if (i < 2) diff --git a/tests/16_nesting.c b/tests/16_nesting.c index c239aa3..520777e 100644 --- a/tests/16_nesting.c +++ b/tests/16_nesting.c @@ -1,12 +1,14 @@ -int x, y, z; - -for (x = 0; x < 2; x++) -{ - for (y = 0; y < 3; y++) - { - for (z = 0; z < 3; z++) - { - printf("%d %d %d\n", x, y, z); - } - } -} +#include + +int x, y, z; + +for (x = 0; x < 2; x++) +{ + for (y = 0; y < 3; y++) + { + for (z = 0; z < 3; z++) + { + printf("%d %d %d\n", x, y, z); + } + } +} diff --git a/tests/17_enum.c b/tests/17_enum.c index 08c865b..7d8cb61 100644 --- a/tests/17_enum.c +++ b/tests/17_enum.c @@ -1,20 +1,22 @@ -enum fred -{ - a, - b, - c, - d, - e = 54, - f = 73, - g, - h -}; +#include + +enum fred +{ + a, + b, + c, + d, + e = 54, + f = 73, + g, + h +}; enum fred frod; - -printf("%d %d %d %d %d %d %d %d\n", a, b, c, d, e, f, g, h); -printf("%d\n", frod); -frod = 12; -printf("%d\n", frod); -frod = e; -printf("%d\n", frod); + +printf("%d %d %d %d %d %d %d %d\n", a, b, c, d, e, f, g, h); +printf("%d\n", frod); +frod = 12; +printf("%d\n", frod); +frod = e; +printf("%d\n", frod); diff --git a/tests/18_include.c b/tests/18_include.c index 3438b07..0f14569 100644 --- a/tests/18_include.c +++ b/tests/18_include.c @@ -1,3 +1,5 @@ -printf("including\n"); -#include "18_include.h" -printf("done\n"); +#include + +printf("including\n"); +#include "18_include.h" +printf("done\n"); diff --git a/tests/19_pointer_arithmetic.c b/tests/19_pointer_arithmetic.c index a85d1e1..149edc4 100644 --- a/tests/19_pointer_arithmetic.c +++ b/tests/19_pointer_arithmetic.c @@ -1,3 +1,5 @@ +#include + int a; int *b; int *c; diff --git a/tests/20_pointer_comparison.c b/tests/20_pointer_comparison.c index 9561ac9..6977118 100644 --- a/tests/20_pointer_comparison.c +++ b/tests/20_pointer_comparison.c @@ -1,3 +1,5 @@ +#include + int a; int b; int *d; diff --git a/tests/21_char_array.c b/tests/21_char_array.c index c484f93..c21e5ca 100644 --- a/tests/21_char_array.c +++ b/tests/21_char_array.c @@ -1,3 +1,5 @@ +#include + int x = 'a'; char y = x; diff --git a/tests/22_floating_point.c b/tests/22_floating_point.c index 7261abd..8c6b394 100644 --- a/tests/22_floating_point.c +++ b/tests/22_floating_point.c @@ -1,40 +1,43 @@ -// variables -float a = 12.34 + 56.78; -printf("%f\n", a); - -// infix operators -printf("%f\n", 12.34 + 56.78); -printf("%f\n", 12.34 - 56.78); -printf("%f\n", 12.34 * 56.78); -printf("%f\n", 12.34 / 56.78); - -// comparison operators -printf("%d %d %d %d %d %d\n", 12.34 < 56.78, 12.34 <= 56.78, 12.34 == 56.78, 12.34 >= 56.78, 12.34 > 56.78, 12.34 != 56.78); -printf("%d %d %d %d %d %d\n", 12.34 < 12.34, 12.34 <= 12.34, 12.34 == 12.34, 12.34 >= 12.34, 12.34 > 12.34, 12.34 != 12.34); -printf("%d %d %d %d %d %d\n", 56.78 < 12.34, 56.78 <= 12.34, 56.78 == 12.34, 56.78 >= 12.34, 56.78 > 12.34, 56.78 != 12.34); - -// assignment operators -a = 12.34; -a += 56.78; -printf("%f\n", a); - -a = 12.34; -a -= 56.78; -printf("%f\n", a); - -a = 12.34; -a *= 56.78; -printf("%f\n", a); - -a = 12.34; -a /= 56.78; -printf("%f\n", a); - -// prefix operators -printf("%f\n", +12.34); -printf("%f\n", -12.34); - -// type coercion -a = 2; -printf("%f\n", a); -printf("%f\n", sin(2)); +#include +#include + +// variables +float a = 12.34 + 56.78; +printf("%f\n", a); + +// infix operators +printf("%f\n", 12.34 + 56.78); +printf("%f\n", 12.34 - 56.78); +printf("%f\n", 12.34 * 56.78); +printf("%f\n", 12.34 / 56.78); + +// comparison operators +printf("%d %d %d %d %d %d\n", 12.34 < 56.78, 12.34 <= 56.78, 12.34 == 56.78, 12.34 >= 56.78, 12.34 > 56.78, 12.34 != 56.78); +printf("%d %d %d %d %d %d\n", 12.34 < 12.34, 12.34 <= 12.34, 12.34 == 12.34, 12.34 >= 12.34, 12.34 > 12.34, 12.34 != 12.34); +printf("%d %d %d %d %d %d\n", 56.78 < 12.34, 56.78 <= 12.34, 56.78 == 12.34, 56.78 >= 12.34, 56.78 > 12.34, 56.78 != 12.34); + +// assignment operators +a = 12.34; +a += 56.78; +printf("%f\n", a); + +a = 12.34; +a -= 56.78; +printf("%f\n", a); + +a = 12.34; +a *= 56.78; +printf("%f\n", a); + +a = 12.34; +a /= 56.78; +printf("%f\n", a); + +// prefix operators +printf("%f\n", +12.34); +printf("%f\n", -12.34); + +// type coercion +a = 2; +printf("%f\n", a); +printf("%f\n", sin(2)); diff --git a/tests/22_floating_point.expect b/tests/22_floating_point.expect index 818379d..282d089 100644 --- a/tests/22_floating_point.expect +++ b/tests/22_floating_point.expect @@ -1,16 +1,16 @@ -69.12 -69.12 --44.44 -700.6652 +69.120000 +69.120000 +-44.440000 +700.665200 0.217330 1 1 0 0 0 1 0 1 1 1 0 0 0 0 0 1 1 1 -69.12 --44.44 -700.6652 +69.120000 +-44.440000 +700.665200 0.217330 -12.34 --12.34 -2.0 +12.340000 +-12.340000 +2.000000 0.909297 diff --git a/tests/23_type_coercion.c b/tests/23_type_coercion.c index 2c1ea8b..7070cc2 100644 --- a/tests/23_type_coercion.c +++ b/tests/23_type_coercion.c @@ -1,45 +1,47 @@ -void charfunc(char a) -{ - printf("char: %c\n", a); -} - -void intfunc(int a) -{ - printf("int: %d\n", a); -} - -void floatfunc(float a) -{ - printf("float: %f\n", a); -} - -charfunc('a'); -charfunc(98); -charfunc(99.0); - -intfunc('a'); -intfunc(98); -intfunc(99.0); - -floatfunc('a'); -floatfunc(98); -floatfunc(99.0); - -printf("%c %d %f\n", 'a', 'b', 'c'); -printf("%c %d %f\n", 97, 98, 99); -printf("%c %d %f\n", 97.0, 98.0, 99.0); - -char b = 97; -char c = 97.0; - -printf("%d %d\n", b, c); - -int d = 'a'; -int e = 97.0; - -printf("%d %d\n", d, e); - -float f = 'a'; -float g = 97; - -printf("%f %f\n", f, g); +#include + +void charfunc(char a) +{ + printf("char: %c\n", a); +} + +void intfunc(int a) +{ + printf("int: %d\n", a); +} + +void floatfunc(float a) +{ + printf("float: %f\n", a); +} + +charfunc('a'); +charfunc(98); +charfunc(99.0); + +intfunc('a'); +intfunc(98); +intfunc(99.0); + +floatfunc('a'); +floatfunc(98); +floatfunc(99.0); + +printf("%c %d %f\n", 'a', 'b', 'c'); +printf("%c %d %f\n", 97, 98, 99); +printf("%c %d %f\n", 97.0, 98.0, 99.0); + +char b = 97; +char c = 97.0; + +printf("%d %d\n", b, c); + +int d = 'a'; +int e = 97.0; + +printf("%d %d\n", d, e); + +float f = 'a'; +float g = 97; + +printf("%f %f\n", f, g); diff --git a/tests/23_type_coercion.expect b/tests/23_type_coercion.expect index a9fb610..a4c5b68 100644 --- a/tests/23_type_coercion.expect +++ b/tests/23_type_coercion.expect @@ -1,15 +1,15 @@ -char: a -char: b -char: c -int: 97 -int: 98 -int: 99 -float: 97.0 -float: 98.0 -float: 99.0 -a 98 99.0 -a 98 99.0 -a 98 99.0 -97 97 -97 97 -97.0 97.0 +char: a +char: b +char: c +int: 97 +int: 98 +int: 99 +float: 97.000000 +float: 98.000000 +float: 99.000000 +a 98 99.000000 +a 98 99.000000 +a 98 99.000000 +97 97 +97 97 +97.000000 97.000000 diff --git a/tests/24_math_library.c b/tests/24_math_library.c index 1f69a30..88fd817 100644 --- a/tests/24_math_library.c +++ b/tests/24_math_library.c @@ -1,3 +1,6 @@ +#include +#include + printf("%f\n", sin(0.12)); printf("%f\n", cos(0.12)); printf("%f\n", tan(0.12)); diff --git a/tests/24_math_library.expect b/tests/24_math_library.expect index c08711d..99f7299 100644 --- a/tests/24_math_library.expect +++ b/tests/24_math_library.expect @@ -1,18 +1,18 @@ 0.119712 -0.992808 +0.992809 0.120579 -0.120289 +0.120290 1.450506 -0.119428 +0.119429 0.120288 -1.007208 +1.007209 0.119427 -1.127496 -0.12 --2.120263 --0.920818 +1.127497 +0.120000 +-2.120264 +-0.920819 0.775357 0.346410 -12.0 -13.0 -12.0 +12.000000 +13.000000 +12.000000 diff --git a/tests/25_quicksort.c b/tests/25_quicksort.c index ada436d..922fd41 100644 --- a/tests/25_quicksort.c +++ b/tests/25_quicksort.c @@ -1,3 +1,5 @@ +#include + int array[16]; //Swap integer values by array indexes diff --git a/tests/26_character_constants.c b/tests/26_character_constants.c index 4f40b77..5674b88 100644 --- a/tests/26_character_constants.c +++ b/tests/26_character_constants.c @@ -1,3 +1,5 @@ +#include + printf("%d\n", '\1'); printf("%d\n", '\10'); printf("%d\n", '\100'); diff --git a/tests/27_sizeof.c b/tests/27_sizeof.c index 875e3d5..e6a20fd 100644 --- a/tests/27_sizeof.c +++ b/tests/27_sizeof.c @@ -1,3 +1,5 @@ +#include + char a; int b; double c; diff --git a/tests/28_strings.c b/tests/28_strings.c index 46f886c..8ec7fc3 100644 --- a/tests/28_strings.c +++ b/tests/28_strings.c @@ -1,3 +1,5 @@ +#include + char a[10]; strcpy(a, "hello"); diff --git a/tests/29_array_address.c b/tests/29_array_address.c index 83f63ed..b81ce89 100644 --- a/tests/29_array_address.c +++ b/tests/29_array_address.c @@ -1,3 +1,5 @@ +#include + char a[10]; strcpy(a, "abcdef"); printf("%s\n", &a[1]); diff --git a/tests/34_array_assignment.c b/tests/34_array_assignment.c index 1d2d5b0..4e096cd 100644 --- a/tests/34_array_assignment.c +++ b/tests/34_array_assignment.c @@ -1,3 +1,5 @@ +#include + int a[4]; a[0] = 12; diff --git a/tests/35_sizeof.c b/tests/35_sizeof.c index 70ae64e..7b27af0 100644 --- a/tests/35_sizeof.c +++ b/tests/35_sizeof.c @@ -1,3 +1,5 @@ +#include + char a; short b; diff --git a/tests/36_array_initialisers.c b/tests/36_array_initialisers.c index 8b84ae7..7ff386a 100644 --- a/tests/36_array_initialisers.c +++ b/tests/36_array_initialisers.c @@ -1,3 +1,5 @@ +#include + int Count; int Array[10] = { 12, 34, 56, 78, 90, 123, 456, 789, 8642, 9753 }; diff --git a/tests/37_sprintf.c b/tests/37_sprintf.c index c9ba3a5..4d8aa67 100644 --- a/tests/37_sprintf.c +++ b/tests/37_sprintf.c @@ -1,3 +1,5 @@ +#include + char Buf[100]; int Count; diff --git a/tests/38_multiple_array_index.c b/tests/38_multiple_array_index.c index d6f9092..dbb32e9 100644 --- a/tests/38_multiple_array_index.c +++ b/tests/38_multiple_array_index.c @@ -1,3 +1,5 @@ +#include + int a[4][4]; int b = 0; int x; diff --git a/tests/39_typedef.c b/tests/39_typedef.c index 541a493..60d37ec 100644 --- a/tests/39_typedef.c +++ b/tests/39_typedef.c @@ -1,3 +1,5 @@ +#include + typedef int MyInt; MyInt a = 1;