diff --git a/CHANGELOG.org b/CHANGELOG.org index 0cd38fe..b5da80a 100644 --- a/CHANGELOG.org +++ b/CHANGELOG.org @@ -22,6 +22,7 @@ Added missing license headers Added support for ~expressions Added prototype for Slow_Lisp build test Added Custom type for mes.h +Added support for octal and binary numbers ** Changed Converted M2-Planet to use GNU style error message diff --git a/cc_reader.c b/cc_reader.c index 70bd61b..ff3938e 100644 --- a/cc_reader.c +++ b/cc_reader.c @@ -20,6 +20,7 @@ FILE* input; struct token_list* token; int line; char* file; +int in_set(int c, char* s); int clearWhiteSpace(int c) { @@ -51,8 +52,7 @@ int consume_word(int c, int frequent) return fgetc(input); } - -void fixup_label() + void fixup_label() { int hold = ':'; int prev; @@ -66,16 +66,6 @@ void fixup_label() } while(0 != hold); } -int in_set(int c, char* s) -{ - while(0 != s[0]) - { - if(c == s[0]) return TRUE; - s = s + 1; - } - return FALSE; -} - int preserve_keyword(int c) { while(in_set(c, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_")) diff --git a/functions/in_set.c b/functions/in_set.c new file mode 100644 index 0000000..e7e989e --- /dev/null +++ b/functions/in_set.c @@ -0,0 +1,32 @@ +/* Copyright (C) 2016 Jeremiah Orians + * Copyright (C) 2018 Jan (janneke) Nieuwenhuizen + * This file is part of M2-Planet. + * + * M2-Planet is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * M2-Planet is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with M2-Planet. If not, see . + */ + +#define FALSE 0 +// CONSTANT FALSE 0 +#define TRUE 1 +// CONSTANT TRUE 1 + +int in_set(int c, char* s) +{ + while(0 != s[0]) + { + if(c == s[0]) return TRUE; + s = s + 1; + } + return FALSE; +} diff --git a/functions/numerate_number.c b/functions/numerate_number.c index fdacd78..6725ba4 100644 --- a/functions/numerate_number.c +++ b/functions/numerate_number.c @@ -22,6 +22,7 @@ //CONSTANT TRUE 1 #define FALSE 0 //CONSTANT FALSE 0 +int in_set(int c, char* s); char* numerate_number(int a) { @@ -87,63 +88,80 @@ int dec2char(int c) else return -1; } +int index_number(char* s, char c) +{ + int i = 0; + while(s[i] != c) + { + i = i + 1; + if(0 == s[i]) return -1; + } + return i; +} + +int toupper(int c) +{ + if(in_set(c, "abcdefghijklmnopqrstuvwxyz")) return (c & 0xDF); + return c; +} + +int set_reader(char* set, int mult, char* input) +{ + int n = 0; + int i = 0; + int hold; + int negative_p = 0; + + if(input[0] == '-') + { + negative_p = 1; + i = i + 1; + } + + while(in_set(input[i], set)) + { + n = n * mult; + hold = index_number(set, toupper(input[i])); + if(-1 == hold) return 0; + n = n + hold; + i = i + 1; + } + + if(0 != input[i]) return 0; + + if(negative_p) + { + n = 0 - n; + } + + return n; +} + int numerate_string(char *a) { - int count = 0; - int index; - int negative; - /* If NULL string */ if(0 == a[0]) { return 0; } - /* Deal with hex */ - else if (a[0] == '0' && a[1] == 'x') + /* Deal with binary*/ + else if ('0' == a[0] && 'b' == a[1]) { - if('-' == a[2]) - { - negative = TRUE; - index = 3; - } - else - { - negative = FALSE; - index = 2; - } - - while(0 != a[index]) - { - if(-1 == char2hex(a[index])) return 0; - count = (16 * count) + char2hex(a[index]); - index = index + 1; - } + return set_reader("01", 2, a+2); + } + /* Deal with hex */ + else if ('0' == a[0] && 'x' == a[1]) + { + return set_reader("0123456789ABCDEFabcdef", 16, a+2); + } + /* Deal with ocal */ + else if('0' == a[0]) + { + return set_reader("01234567", 8, a+1); } /* Deal with decimal */ else { - if('-' == a[0]) - { - negative = TRUE; - index = 1; - } - else - { - negative = FALSE; - index = 0; - } - - while(0 != a[index]) - { - if(-1 == char2dec(a[index])) return 0; - count = (10 * count) + char2dec(a[index]); - index = index + 1; - } + return set_reader("0123456789", 10, a); } - - if(negative) - { - count = count * -1; - } - return count; } diff --git a/makefile b/makefile index 9f79671..aa0603c 100644 --- a/makefile +++ b/makefile @@ -25,6 +25,7 @@ CFLAGS=-D_GNU_SOURCE -O0 -std=c99 -ggdb M2-Planet-gcc: cc_reader.c cc_strings.c cc_core.c cc.c cc_types.c cc.h | bin $(CC) $(CFLAGS) \ functions/match.c \ + functions/in_set.c \ functions/numerate_number.c \ functions/file_print.c \ functions/string.c \ diff --git a/test/test.answers b/test/test.answers index 12126f0..21c6357 100644 --- a/test/test.answers +++ b/test/test.answers @@ -9,7 +9,7 @@ a9a3e332d13ded5f80d7431f8717f26527b3722b33ea57760a9a5723dffc099c test/results/t f1c01feb865c4d552033186d9ce50dd39468a7e8aebf762886c13ad3e03b5011 test/results/test08-binary 3b39e72f3de90ed690adfaf6145af46157cef2ec5e72867ac577fa27a0229894 test/results/test09-binary 020e86020819cc4963e6185b22e534fcf8306b6cb116f12643f254a24688ff0a test/results/test10-binary -db6523217dd153dfbbf660750be5ea82ebdf7086a62e3456721e25d4479eedd4 test/results/test100-binary +89c788e3b72c32e16d851a1a00c050435dc5fc457200f0a5cd58ce02dc3fd0e1 test/results/test100-binary 3fd11bad4a426ce1ff8fd9c6d7d2b943effae9f3f5740b7376e426e9b0555851 test/results/test11-binary f98ab8e4bb35580e0dde96126d7a56aff66bda208d02c8d89390b40d6cff591c test/results/test12-binary 5051ffca2615144419f8ec1a5d4999486ae81e7781428f59e47e866af97cef92 test/results/test13-binary @@ -18,10 +18,10 @@ a8218958b628066e2fda63d3933f1bf607c358d7bdfe84fc02596393698ea5f6 test/results/t d70e072f4f1f077d10ff65e9216ca8b423b996e35d68d208025db7a78b062f50 test/results/test16-binary 9b4ba350b07cc1cf4e12dc77d0d960ded1511f13b887363b0eb33421e2f626de test/results/test17-binary 8de7384c4633b1d5c60bbbb298d7f4b738e52fbc266ef4ef9a48b3cb995e3176 test/results/test18-binary -b0d7982de3f257d6a5b51ad356c1aa09f24b8a69d862a90f41aef7dc83ec3b2f test/results/test19-binary +cefc5a53513abdb9069dc8bdb7b4529307420d5dd412a10112c3253bdcd29c46 test/results/test19-binary 365c96fb8368710d620a76facd6bebcdeeb6f6d30ceaf0a6f1567fc3fcbe9b54 test/results/test20-binary 3c7654eb26247e5f0460dad9e539220a68078cd8c56aae8457b5a97dc6eab892 test/results/test21-binary -b339c77f2b62f8fc6fa4da699dec4c16ad5f6f565c5cd6b133e3b5c4ea5c84e1 test/results/test22-binary -aedaa40380b16892578958e1c8454f4901ad77c7ee2511fad6c27012c08a6cc7 test/results/test23-binary +79309ff489e04ce7846fa27f3a852b0500a15f491e331f50bc40d64597899629 test/results/test22-binary +496741582b1d1368e3e2a6e8b2b516d468a9f858947d18e0c7bfa06936cc4bbb test/results/test23-binary c080413cdb713618896a73b834fb96ce8dd2c2176a226c0713d9a729bd1aa7f9 test/results/test24-binary 140af7fb3ef89d84b21bd3fe69f0d3260650ec0467c0ffadf9268fad573a397f test/results/test99-binary diff --git a/test/test100/hello.sh b/test/test100/hello.sh index 16916cc..2a87c13 100755 --- a/test/test100/hello.sh +++ b/test/test100/hello.sh @@ -24,6 +24,7 @@ then -f functions/calloc.c \ -f functions/exit.c \ -f functions/match.c \ + -f functions/in_set.c \ -f functions/numerate_number.c \ -f functions/file_print.c \ -f functions/string.c \ @@ -41,6 +42,7 @@ else -f functions/calloc.c \ -f functions/exit.c \ -f functions/match.c \ + -f functions/in_set.c \ -f functions/numerate_number.c \ -f functions/file_print.c \ -f functions/string.c \ @@ -84,6 +86,7 @@ then -f functions/calloc.c \ -f functions/exit.c \ -f functions/match.c \ + -f functions/in_set.c \ -f functions/numerate_number.c \ -f functions/file_print.c \ -f functions/string.c \ diff --git a/test/test100/proof.answer b/test/test100/proof.answer index 240fb91..4b10533 100644 --- a/test/test100/proof.answer +++ b/test/test100/proof.answer @@ -1 +1 @@ -621f03ddcfe25625517ccdf36002cb290047869764be270c45fdb2ab4493b3fc test/test100/proof +b2486e11d917fde17e2b4649238d77724b9fe8623c013ded63a3d991fda24926 test/test100/proof diff --git a/test/test19/hello.sh b/test/test19/hello.sh index 4a03128..db972bb 100755 --- a/test/test19/hello.sh +++ b/test/test19/hello.sh @@ -22,6 +22,7 @@ bin/M2-Planet -f functions/file.c \ -f functions/calloc.c \ -f functions/exit.c \ -f functions/match.c \ + -f functions/in_set.c \ -f functions/numerate_number.c \ -f test/test19/getopt.c \ -o test/test19/getopt.M1 || exit 1 diff --git a/test/test22/hello.sh b/test/test22/hello.sh index 4c4eaf2..bd992bb 100755 --- a/test/test22/hello.sh +++ b/test/test22/hello.sh @@ -23,6 +23,7 @@ set -x -f functions/malloc.c \ -f functions/calloc.c \ -f functions/match.c \ + -f functions/in_set.c \ -f functions/numerate_number.c \ -f functions/stat.c \ -f test/test22/hex2_linker.c \ diff --git a/test/test23/hello.sh b/test/test23/hello.sh index 8fb6aff..e5bd944 100755 --- a/test/test23/hello.sh +++ b/test/test23/hello.sh @@ -23,6 +23,7 @@ set -x -f functions/malloc.c \ -f functions/calloc.c \ -f functions/match.c \ + -f functions/in_set.c \ -f functions/numerate_number.c \ -f functions/string.c \ -f test/test23/M1-macro.c \ diff --git a/test/test25/hello.sh b/test/test25/hello.sh index 66b1ca9..9fe507b 100755 --- a/test/test25/hello.sh +++ b/test/test25/hello.sh @@ -23,6 +23,7 @@ set -x -f functions/malloc.c \ -f functions/calloc.c \ -f functions/match.c \ + -f functions/in_set.c \ -f functions/numerate_number.c \ -f functions/fork.c \ -f functions/execve.c \ diff --git a/test/test26/hello.sh b/test/test26/hello.sh index 128132f..e64e7cc 100755 --- a/test/test26/hello.sh +++ b/test/test26/hello.sh @@ -20,6 +20,7 @@ set -x ./bin/M2-Planet -f test/test26/lisp.h \ -f functions/malloc.c \ -f functions/calloc.c \ + -f functions/in_set.c \ -f functions/numerate_number.c \ -f functions/match.c \ -f functions/file.c \