├── .gitignore ├── LICENSE.md ├── Makefile ├── README.md ├── cnpython ├── cnpython.c ├── exception.c ├── help.c ├── include │ ├── cnpy_exceptions.h │ ├── cnpy_help.h │ ├── cnpy_print.h │ ├── cnpython.h │ └── datastructures │ │ ├── linkedlist.h │ │ └── stack.h ├── linkedlist.c ├── print.c └── stack.c ├── testing ├── abs │ ├── abs │ └── abs.c ├── all │ └── all.c ├── and │ └── and.c ├── append │ └── append.c ├── arr_clear │ └── clear.c ├── arr_count │ └── count.c ├── arr_find │ └── find.c ├── bin │ └── bin.c ├── capitalize │ └── capitalize.c ├── chr │ └── chr.c ├── comments │ └── comm.c ├── copy │ └── copy.c ├── count │ └── count.c ├── elif │ └── elif.c ├── endswith │ └── endswith.c ├── find │ └── find.c ├── find_str │ └── find_str.c ├── functions │ └── func.c ├── help │ └── help.c ├── hex │ ├── hex.c │ └── id │ │ └── id.c ├── input │ └── input.c ├── int │ └── int.c ├── isin │ └── isin.c ├── islower │ └── islower.c ├── islowercase │ └── islowercase.c ├── join │ └── join.c ├── justify │ └── justify.c ├── len │ └── len.c ├── linked_list │ └── linked_list.c ├── list │ └── list.c ├── oct │ └── oct.c ├── open │ └── open.c ├── ord │ └── ord.c ├── pop │ └── pop.c ├── pow │ └── pow.c ├── print │ └── print.c ├── range │ └── range.c ├── read_file │ ├── read.c │ └── test.txt ├── replace │ └── replace.c ├── reversed │ └── reversed.c ├── round │ └── round.c ├── split │ └── split.c ├── startswith │ └── starts.c ├── str │ └── str.c ├── str_check │ └── str_check.c ├── strip │ └── strip.c ├── sum │ └── sum.c ├── title │ └── title.c ├── tuples │ └── tuple.c ├── type │ └── type.c ├── upper │ └── upper.c └── var │ └── var.c └── tutorials ├── functions └── str_islower │ ├── Makefile │ └── str_islower.c ├── helloWorld ├── Makefile └── helloWorld.c └── variables ├── Makefile └── variables.c /.gitignore: -------------------------------------------------------------------------------- 1 | *.o 2 | github 3 | .vscode/ 4 | .vscode/settings.json 5 | main-tests.c 6 | src/ 7 | build/ -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | Copyright 2022 Grigoras Arin 2 | 3 | Licensed under the Apache License, Version 2.0 (the "License"); 4 | you may not use this file except in compliance with the License. 5 | You may obtain a copy of the License at 6 | 7 | http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | Unless required by applicable law or agreed to in writing, software 10 | distributed under the License is distributed on an "AS IS" BASIS, 11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | See the License for the specific language governing permissions and 13 | limitations under the License. -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | #insert your own filename here 2 | 3 | CC = gcc 4 | SRC = ${wildcard ./cnpython/*.c} 5 | CFLAGS = -lm 6 | 7 | 8 | #Uncomment and add your own file here if you need it 9 | 10 | BUILD = ./build/something 11 | 12 | all: 13 | ${CC} src/main.c ${SRC} -lm -o ${BUILD} 14 | 15 | test: ./template.c 16 | gcc ./template.c cnpython.c -o ./template.o -lm 17 | 18 | 19 | clear: *.o 20 | rm *.o 21 | 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # CnPython 2 | 3 | Implementing most of pythons builtin functions in C, making it easier to read. 4 | 5 | ## Compile 6 | Go to the makefile and adjust it to your needs. 7 | 8 | 9 | ## How to contribute 10 | 11 | Just create an issue if you have an idea for a builtin function or create a new pull request if you can also write the code for that. When 12 | I will have more time to spend on this project I will definetly contribute a little bit more. 13 | 14 | ## Implemented functions 15 | - [x] type(var), 16 | - [x] bin(decimal), 17 | - [x] hex(decimal), 18 | - [x] oct(decimal), 19 | - [x] pow(base, exponent), 20 | - [x] str(number), 21 | - [x] _int(string), 22 | - [x] input(prompt), 23 | - [x] reverse(arr, size), 24 | - [x] min(arr, size), 25 | - [x] max(arr, size), 26 | - [x] print(*format, ...), 27 | - [x] sorted(arr, size), 28 | - [x] sum(arr, size), 29 | - [x] str_len(str), 30 | - [x] str_count(str, find), 31 | - [x] endswith(str, element), 32 | - [x] replace(str, find, repl), 33 | - [x] find(str, find), 34 | - [x] startswith(str, element), 35 | - [ ] str_split(arr, str, del), 36 | - [x] printSplit(str, del), 37 | - [ ] split(arr, str, del) 38 | - [x] chr(ascii) 39 | - [x] ord(char) 40 | - [x] isin(str, value) 41 | - [x] rem(a, b) 42 | - [x] percentage(p, n) 43 | - [x] iappend(arr, value, size) 44 | - [x] copy(arr, dest, size) 45 | - [x] arr_count(arr, value, size) 46 | - [x] arr_find(arr, value, size) 47 | - [x] pop(arr, size) 48 | 49 | ## License 50 | 51 | This code is under a MIT license. This means you can do whatever you want with the code but the same license has to be in your project if you want to use this in your project. 52 | 53 | 54 | -------------------------------------------------------------------------------- /cnpython/cnpython.c: -------------------------------------------------------------------------------- 1 | #include "./include/cnpython.h" 2 | 3 | 4 | 5 | //the second arguments value determines the binary notation should be printed or not 6 | long long bin(long decimal, ...){ 7 | long long bin = 0; 8 | int rem, i = 1, step = 1; 9 | va_list valist; 10 | while (decimal != 0) { 11 | rem = decimal % 2; 12 | decimal /= 2; 13 | bin += rem * i; 14 | i *= 10; 15 | } 16 | 17 | va_start(valist, decimal); 18 | 19 | if(va_arg(valist, int) == True){ 20 | print("0b%lld", bin); 21 | } 22 | 23 | va_end(valist); 24 | 25 | return bin; 26 | } 27 | 28 | 29 | 30 | 31 | 32 | 33 | /*Converts any decimal value to a hexadecimal value*/ 34 | string hex(long n, ...) 35 | { 36 | string hexaDeciNum = (char*)malloc(sizeof(char) * 100); 37 | va_list valist; 38 | 39 | if(!hexaDeciNum){ 40 | raise_exception(AllocationError, __FILE__, __LINE__); 41 | } 42 | 43 | int i = 0; 44 | while (n != 0) { 45 | int temp = 0; 46 | 47 | temp = n % 16; 48 | 49 | if (temp < 10) { 50 | hexaDeciNum[i] = temp + 48; 51 | i++; 52 | } 53 | else { 54 | hexaDeciNum[i] = temp + 55; 55 | i++; 56 | } 57 | 58 | n = n / 16; 59 | } 60 | 61 | va_start(valist, n); 62 | 63 | if(va_arg(valist, int) == True){ 64 | print("0x"); 65 | for(int j = i - 1; j >= 0; j--){ 66 | print("%c", hexaDeciNum[j]); 67 | } 68 | free(hexaDeciNum); 69 | } 70 | 71 | else{ 72 | return hexaDeciNum; 73 | } 74 | 75 | } 76 | 77 | 78 | 79 | /*Converts any decimal value to an octal value*/ 80 | long oct(long dec, ...){ 81 | long octalNumber = 0; 82 | int i = 1; 83 | va_list valist; 84 | 85 | while (dec != 0){ 86 | octalNumber += (dec % 8) * i; 87 | dec /= 8; 88 | i *= 10; 89 | } 90 | 91 | 92 | va_start(valist, dec); 93 | 94 | if(va_arg(valist, int) == True){ 95 | print("0o%ld", octalNumber); 96 | } 97 | 98 | va_end(valist); 99 | 100 | return octalNumber; 101 | } 102 | 103 | 104 | /*Power function*/ 105 | double power(double base, double exponent){ 106 | double result = 1; 107 | for(exponent; exponent > 0; exponent--){ 108 | result *= base; 109 | } 110 | 111 | return result; 112 | } 113 | 114 | 115 | 116 | int factorial(int n){ 117 | if(n == 0){ 118 | return 1; 119 | } 120 | 121 | return n * factorial(n - 1); 122 | } 123 | 124 | 125 | 126 | /*Converts a number to a string*/ 127 | string str(int number){ 128 | int size = (int)((ceil(log10(number))+1)*sizeof(char)); 129 | string buffer = (char*)malloc(sizeof(size)); 130 | 131 | if(!buffer){ 132 | raise_exception(AllocationError, __FILE__, __LINE__); 133 | } 134 | 135 | sprintf(buffer, "%d", number); 136 | 137 | return buffer; 138 | 139 | } 140 | 141 | 142 | 143 | 144 | /*Converts a string to an integer*/ 145 | int _int(string str){ return atoi(str); } 146 | //converts from int to bool 147 | int _bool(int n){ return n == 0 ? False : True; } 148 | 149 | 150 | 151 | /*Takes as a parameter the prompt for the user and reads the input byte by byte and returns it*/ 152 | string input(string str){ 153 | int bufsize = 100; 154 | int position = 0; 155 | string buffer = (char*)malloc(sizeof(char) * bufsize); 156 | int c; 157 | 158 | if (!buffer) { 159 | raise_exception(AllocationError, __FILE__, __LINE__); 160 | } 161 | 162 | printf("%s", str); 163 | 164 | while (True) { 165 | 166 | c = getchar(); 167 | 168 | if (c == EOF or c == '\n') { 169 | buffer[position] = '\0'; 170 | return buffer; 171 | } else { 172 | buffer[position] = c; 173 | 174 | } 175 | 176 | position++; 177 | 178 | if (position >= bufsize) { 179 | bufsize += 100; 180 | buffer = (char*)realloc(buffer, bufsize); 181 | if (!buffer) { 182 | raise_exception(ReallocationError, __FILE__, __LINE__); 183 | } 184 | } 185 | } 186 | 187 | return buffer; 188 | } 189 | 190 | 191 | 192 | /*Reverses an int array*/ 193 | int* reversed(int* arr, int size){ 194 | int *return_arr = calloc(size, sizeof(int)); 195 | 196 | if(!return_arr){ 197 | raise_exception(AllocationError, __FILE__, __LINE__); 198 | } 199 | 200 | for(int c = size - 1, d = 0; c >= 0; c--, d++){ 201 | return_arr[d] = arr[c]; 202 | } 203 | 204 | return return_arr; 205 | 206 | } 207 | 208 | 209 | 210 | 211 | /*Returns the biggest element in an array*/ 212 | double max (double numbers[], int arrlen) { 213 | double maxitem = numbers[0]; 214 | for(int i=1; i maxitem) { 216 | maxitem = numbers[i]; 217 | } 218 | } 219 | return maxitem; 220 | } 221 | 222 | 223 | 224 | /*Returns the smallest element in an array*/ 225 | double min(double numbers[], int arrlen) { 226 | double minitem = numbers[0]; 227 | for(int i=1; i < arrlen; i++) { 228 | if (numbers[i] < minitem) { 229 | minitem = numbers[i]; 230 | } 231 | } 232 | return minitem; 233 | } 234 | 235 | 236 | 237 | /*Used in tim sort algorithm*/ 238 | void insertionSort(int arr[], int left, int right) 239 | { 240 | for (int i = left + 1; i <= right; i++) 241 | { 242 | int temp = arr[i]; 243 | int j = i - 1; 244 | while (j >= left and arr[j] > temp) 245 | { 246 | arr[j+1] = arr[j]; 247 | j--; 248 | } 249 | arr[j+1] = temp; 250 | } 251 | } 252 | 253 | /*Merge function is used in the tim sort algorithm*/ 254 | void merge(int arr[], int l, int m, int r){ 255 | int len1 = m - l + 1, len2 = r - m; 256 | int left[len1], right[len2]; 257 | for (int i = 0; i < len1; i++) 258 | left[i] = arr[l + i]; 259 | for (int i = 0; i < len2; i++) 260 | right[i] = arr[m + 1 + i]; 261 | 262 | int i = 0; 263 | int j = 0; 264 | int k = l; 265 | 266 | while (i < len1 and j < len2) 267 | { 268 | if (left[i] <= right[j]) 269 | { 270 | arr[k] = left[i]; 271 | i++; 272 | } 273 | else 274 | { 275 | arr[k] = right[j]; 276 | j++; 277 | } 278 | k++; 279 | } 280 | 281 | 282 | while (i < len1) 283 | { 284 | arr[k] = left[i]; 285 | k++; 286 | i++; 287 | } 288 | 289 | 290 | while (j < len2) 291 | { 292 | arr[k] = right[j]; 293 | k++; 294 | j++; 295 | } 296 | } 297 | 298 | 299 | /*Tim Sort algorithm*/ 300 | void timSort(int arr[], int n){ 301 | 302 | for (int i = 0; i < n; i+=RUN) 303 | insertionSort(arr, i, MIN((i+RUN-1), 304 | (n-1))); 305 | 306 | for (int size = RUN; size < n; 307 | size = 2*size) 308 | { 309 | 310 | for (int left = 0; left < n; 311 | left += 2*size) 312 | { 313 | int mid = left + size - 1; 314 | int right = MIN((left + 2*size - 1), 315 | (n-1)); 316 | 317 | 318 | if(mid < right) 319 | merge(arr, left, mid, right); 320 | } 321 | } 322 | } 323 | 324 | 325 | 326 | /*uses timSort algorithm to sort an array*/ 327 | int* sorted(int *arr, int size){ 328 | timSort(arr, size); 329 | 330 | return arr; 331 | } 332 | 333 | 334 | 335 | /*Gets the sum of all the elements in an array*/ 336 | long sum(int *arr, int size){ 337 | long sum = 0; 338 | 339 | for(int i = 0; i < size; i++){ 340 | sum += arr[i]; 341 | } 342 | 343 | return sum; 344 | } 345 | 346 | 347 | 348 | int str_len(string str){ 349 | //exists already but decided to put this here cuz why not 350 | return strlen(str); 351 | } 352 | 353 | 354 | /*Counts how many times the program found a character in a given string*/ 355 | int str_count(string str, char find){ 356 | 357 | int count = 0; 358 | 359 | for(int i = 0; i < strlen(str); i++){ 360 | if(str[i] == find){ 361 | count++; 362 | } 363 | } 364 | 365 | return count; 366 | } 367 | 368 | 369 | 370 | int endswith(string str, char element){ 371 | if(str[strlen(str)-1] == element){ 372 | return 0; 373 | } 374 | 375 | return -1; 376 | } 377 | 378 | 379 | 380 | string join(string str1, string str2){ 381 | string str3 = (char*)malloc(sizeof(str1) + sizeof(str2)); 382 | 383 | if(!str3){ 384 | raise_exception(AllocationError, __FILE__, __LINE__); 385 | } 386 | 387 | strcpy(str3, str1); 388 | strcat(str3, str2); 389 | 390 | return str3; 391 | } 392 | 393 | 394 | 395 | string replace(string str, char find, char repl){ 396 | string ret_string = (char*)malloc(sizeof(str)); 397 | 398 | if(!ret_string){ 399 | raise_exception(AllocationError, __FILE__, __LINE__); 400 | } 401 | 402 | strcpy(ret_string, str); 403 | 404 | 405 | for(int i = 0; i < strlen(ret_string); i++){ 406 | if(ret_string[i] == find){ 407 | ret_string[i] = repl; 408 | } 409 | } 410 | 411 | return ret_string; 412 | } 413 | 414 | 415 | 416 | int find(string str, char find){ 417 | for(int i = 0; i < strlen(str); i++){ 418 | if(str[i] == find){ 419 | return i; 420 | } 421 | } 422 | 423 | return -1; 424 | } 425 | 426 | 427 | 428 | int startswith(string str, char element){ 429 | if(str[0] == element){ 430 | return 0; 431 | } 432 | return -1; 433 | } 434 | 435 | 436 | 437 | string read_file(string filename){ 438 | FILE *fptr = fopen(filename, "rb"); 439 | 440 | if(!fptr){ 441 | raise_exception(FileOpenError, __FILE__, __LINE__); 442 | } 443 | 444 | fseek(fptr, 0, SEEK_END); 445 | long fptr_size = ftell(fptr); 446 | fseek(fptr, 0, SEEK_SET); 447 | 448 | string buffer = (char*)malloc(fptr_size + 1); 449 | 450 | if(!buffer){ 451 | raise_exception(AllocationError, __FILE__, __LINE__); 452 | } 453 | 454 | fread(buffer, 1, fptr_size, fptr); 455 | fclose(fptr); 456 | 457 | buffer[fptr_size] = 0; 458 | 459 | return buffer; 460 | } 461 | 462 | 463 | /* 464 | size_t str_split(string **array, string str, const string del) { 465 | string token; 466 | size_t i = 0; 467 | 468 | token = strtok(str, del); 469 | 470 | while(token != NULL) { 471 | *array = realloc(*array, sizeof(string ) * (i + 1)); 472 | 473 | if(!array){ 474 | raise_exception(ReallocationError, __FILE__, __LINE__); 475 | } 476 | 477 | (*array)[i++] = token; 478 | token = strtok(NULL, del); 479 | } 480 | 481 | return i; 482 | }*/ 483 | 484 | 485 | /* 486 | void printSplit(string **split, string str, const char del){ 487 | size_t count = str_split(&split, del); 488 | 489 | for(size_t i = 0; i < count; i++) { 490 | printf("[%s], ", split[i]); 491 | } 492 | 493 | free(split); 494 | } 495 | */ 496 | 497 | 498 | 499 | 500 | void str_split(string str, const string del) 501 | { 502 | string token; 503 | 504 | token = strtok(str, del); 505 | 506 | while(token != None){ 507 | print(token); 508 | 509 | token = strtok(None, del); 510 | } 511 | } 512 | 513 | 514 | 515 | int ord(char c) { 516 | return (int) c; 517 | } 518 | 519 | 520 | 521 | int isin(string str, char value){ 522 | for(int i = 0; i < strlen(str); i++){ 523 | if(str[i] == value){ 524 | return 0; 525 | } 526 | } 527 | 528 | return -1; 529 | } 530 | 531 | 532 | 533 | int rem(int a, int b){ 534 | return a % b; 535 | } 536 | 537 | 538 | 539 | double percentage(float p, float n){ 540 | return p * n / 100; 541 | } 542 | 543 | 544 | void iappend(int *arr, int value, int size){ 545 | arr[size] = value; 546 | } 547 | 548 | 549 | int *copy(int *arr, int *dest, int size){ 550 | for(int i = 0; i < size; i++){ 551 | dest[i] = arr[i]; 552 | } 553 | 554 | return dest; 555 | } 556 | 557 | 558 | int arr_count(int *arr, int value, int size){ 559 | int count = 0; 560 | 561 | for(int i = 0; i < size; i++){ 562 | if(arr[i] == value){ 563 | count++; 564 | } 565 | } 566 | 567 | return count; 568 | } 569 | 570 | 571 | int arr_find(int *arr, int value, int size){ 572 | for(int i = 0; i < size; i++){ 573 | if(arr[i] == value){ 574 | return i; 575 | } 576 | } 577 | 578 | return -1; 579 | } 580 | 581 | 582 | void pop(int *arr, int size){ 583 | arr[size-1] = 0; 584 | } 585 | 586 | 587 | char chr(int ascii) { 588 | return (char) ascii; 589 | } 590 | 591 | /*returns absolute value*/ 592 | int i_abs(int number){ 593 | return (((number) < (0)) ? (-number) : (number)); 594 | } 595 | 596 | double d_abs(double number){ 597 | return (((number) < (0)) ? (-number) : (number)); 598 | } 599 | 600 | 601 | int all(int *arr, int size){ 602 | //int total_nulls = 0; 603 | int total_trues = 0; 604 | 605 | for(int i = 0; i < size; i++){ 606 | /*if(arr[i] == NULL){ 607 | total_nulls++; 608 | }*/ 609 | 610 | if(arr[i] != 0){ 611 | total_trues++; 612 | } 613 | } 614 | 615 | /*if(total_trues == size && total_nulls == size){ 616 | return True; 617 | }*/ 618 | 619 | if(total_trues == size){ 620 | return True; 621 | } 622 | 623 | return False; 624 | } 625 | 626 | 627 | int d_all(double *arr, int size){ 628 | int total_trues = 0; 629 | 630 | for(int i = 0; i < size; i++){ 631 | if(arr[i] != 0){ 632 | total_trues++; 633 | } 634 | } 635 | 636 | if(total_trues == size){ 637 | return True; 638 | } 639 | 640 | return False; 641 | } 642 | 643 | 644 | int any(int *arr, int size){ 645 | for(int i = 0; i < size; i++){ 646 | if(arr[i] != 0){ 647 | return True; 648 | } 649 | } 650 | 651 | 652 | return False; 653 | } 654 | 655 | int d_any(double *arr, int size){ 656 | for(int i = 0; i < size; i++){ 657 | if(arr[i] != 0){ 658 | return True; 659 | } 660 | } 661 | 662 | return False; 663 | } 664 | 665 | 666 | int str_islower(string str){ 667 | int total = 0; 668 | int whitespaces = 0; 669 | int str_size = strlen(str); 670 | 671 | for(int i = 0; i < str_size; i++){ 672 | if(islower(str[i])){ 673 | total++; 674 | }elif(isupper(str[i])){ 675 | return False; 676 | }elif(isspace(str[i])){ 677 | whitespaces++; 678 | } 679 | } 680 | 681 | if(total == str_size - whitespaces){ 682 | return True; 683 | } 684 | 685 | return False; 686 | } 687 | 688 | 689 | int str_isupper(string str){ 690 | int total = 0; 691 | int whitespaces = 0; 692 | int str_size = strlen(str); 693 | 694 | for(int i = 0; i < str_size; i++){ 695 | if(isupper(str[i])){ 696 | total++; 697 | }elif(islower(str[i])){ 698 | return False; 699 | }elif(isspace(str[i])){ 700 | whitespaces++; 701 | } 702 | } 703 | 704 | if(total == str_size - whitespaces){ 705 | return True; 706 | } 707 | 708 | return False; 709 | } 710 | 711 | 712 | int str_isspace(string str){ 713 | int whitespaces = 0; 714 | int str_size = strlen(str); 715 | 716 | for(int i = 0; i < str_size; i++){ 717 | if(isspace(str[i])){ 718 | whitespaces++; 719 | }elif(isspace(str[i] == 0)){ 720 | return False; 721 | } 722 | } 723 | 724 | if(whitespaces == str_size){ 725 | return True; 726 | } 727 | } 728 | 729 | 730 | int str_isalpha(string str) 731 | { 732 | int total = 0; 733 | int whitespaces = 0; 734 | int str_size = strlen(str); 735 | 736 | for(int i = 0; i < str_size; i++) 737 | { 738 | if(isalpha(str[i])) 739 | { 740 | total++; 741 | } 742 | elif(isspace(str[i])) 743 | { 744 | whitespaces++; 745 | } 746 | else 747 | { 748 | return False; 749 | } 750 | } 751 | 752 | 753 | if(total == str_size - whitespaces){ 754 | return True; 755 | } 756 | 757 | return False; 758 | } 759 | 760 | 761 | int str_isdigit(string str) 762 | { 763 | int total = 0; 764 | int whitespaces = 0; 765 | int str_size = strlen(str); 766 | 767 | for(int i = 0; i < str_size; i++) 768 | { 769 | if(isdigit(str[i])) 770 | { 771 | total++; 772 | } 773 | elif(isspace(str[i])) 774 | { 775 | whitespaces++; 776 | } 777 | else 778 | { 779 | return False; 780 | } 781 | } 782 | 783 | if(total == str_size - whitespaces){ 784 | return True; 785 | } 786 | 787 | 788 | return False; 789 | } 790 | 791 | 792 | void str_splitlines(string str) 793 | { 794 | str_split(str, " "); 795 | } 796 | 797 | 798 | 799 | float CNPY_rsqrt(float number){ 800 | long i; 801 | float x2, y; 802 | const float threehalfs = 1.5F; 803 | 804 | x2 = number * 0.5F; 805 | y = number; 806 | i = *(long*)&y; 807 | i = 0x5f3759df - (i >> 1); 808 | y = *(float*)&i; 809 | y = y * (threehalfs - (x2 * y * y)); 810 | y = y * (threehalfs) - (x2 * y * y); 811 | 812 | 813 | return y; 814 | } 815 | 816 | 817 | /** 818 | * @brief Find the substring inside a string. Supporting search zone feature. Set negative value at end offset for end of the string 819 | * 820 | * @param str The given string 821 | * @param value The substring to be found inside str 822 | * @param start The offset of when to start 823 | * @param end The end range 824 | * @return ptrdiff_t Return the index of the first character of the substring when found. Else, -1 is returned 825 | */ 826 | ptrdiff_t find_str(string str, string value, const unsigned int start, const int end){ 827 | if (end > 0){ 828 | size_t strLen = strlen(str); 829 | string copy = malloc(strLen); 830 | 831 | strncpy(copy, str, strLen); 832 | //Since we only find until "end" index 833 | copy[end] = '\0'; 834 | 835 | char* strstrRet = strstr(copy + start, value); 836 | return strstrRet == NULL ? -1 : strstrRet - copy; 837 | } 838 | 839 | char* strstrRet = strstr(str + start, value); 840 | return strstrRet == NULL ? -1 : strstrRet - str; 841 | } 842 | 843 | /** 844 | * @brief Works just like the find method. However, an error is thrown when not found then given value 845 | */ 846 | ptrdiff_t index_str(string str, string value, const unsigned int start, const int end){ 847 | char* strstrRet; 848 | if (end > 0){ 849 | size_t strLen = strlen(str); 850 | string copy = malloc(strLen); 851 | 852 | strncpy(copy, str, strLen); 853 | //Since we only find until "end" index 854 | copy[end] = '\0'; 855 | 856 | strstrRet = strstr(copy + start, value); 857 | if (strstrRet == NULL){ 858 | fprintf(stderr, "ValueError: substring not found, given: \"%s\"\n", value); 859 | exit(1); 860 | } 861 | }else{ 862 | strstrRet = strstr(str + start, value); 863 | if (strstrRet == NULL){ 864 | fprintf(stderr, "ValueError: substring not found, given: \"%s\"\n", value); 865 | exit(1); 866 | } 867 | } 868 | 869 | return strstrRet - str; 870 | } 871 | 872 | /** 873 | * @brief Justify the given string to the right of a new string 874 | * 875 | * @param str The string to be justified 876 | * @param len The len of the fill offset and the string itself 877 | * @param fill The character to be filled 878 | * @return string The new string that right-justified the old string 879 | */ 880 | string rjust(string str, const size_t len, const char fill){ 881 | size_t strLen = strlen(str); 882 | if (len < strLen) 883 | return NULL; 884 | 885 | string ret = malloc(len + 1); 886 | 887 | if (!ret){ 888 | fprintf(stderr, "ERROR: rjust function: Can't allocating memory"); 889 | exit(1); 890 | } 891 | memset(ret, fill, len - strLen); 892 | memcpy(ret + (len - strLen), str, strLen); 893 | 894 | return ret; 895 | } 896 | 897 | 898 | /** 899 | * @brief Justify the given string to the left of a new string 900 | * 901 | * @param str The string to be justified 902 | * @param len The len of string and the fill offset 903 | * @param fill The character to be filled 904 | * @return string The new string that left-justified the old string 905 | */ 906 | string ljust(string str, const size_t len, const char fill){ 907 | size_t strLen = strlen(str); 908 | if (len < strLen) 909 | return NULL; 910 | 911 | string ret = malloc(len + 1); 912 | 913 | if (!ret){ 914 | fprintf(stderr, "ERROR: ljust function: Can't allocating memory"); 915 | exit(1); 916 | } 917 | 918 | memcpy(ret, str, strLen); 919 | memset(ret + strLen, fill, len - strLen); 920 | 921 | return ret; 922 | } 923 | 924 | 925 | /** 926 | * @brief Basically right-justified but auto fill with zero (ascii value) instead 927 | * 928 | * @param str The string to be justified 929 | * @param len The len of string and the fill offset 930 | * @return char* The new string that right-justified and fills with zero 931 | */ 932 | char* zfill(string str, size_t len){ 933 | string ret = rjust(str, len, '0'); 934 | 935 | return ret; 936 | } 937 | 938 | 939 | //Some more checking method for string 940 | 941 | bool isspace_str(string str){ 942 | bool ret = true; 943 | size_t i = 0; 944 | 945 | while (str[i] != '\0'){ 946 | if (str[i] != ' ') 947 | ret = false; 948 | i++; 949 | } 950 | 951 | return ret; 952 | } 953 | 954 | bool isprintable_str(string str){ 955 | size_t strLen = strlen(str); 956 | bool ret = true; 957 | 958 | for (int i = 0; i < strLen; i++) 959 | if (!isprint(str[i])) 960 | ret = false; 961 | 962 | return ret; 963 | } 964 | 965 | bool isdigit_str(string str){ 966 | size_t strLen = strlen(str); 967 | bool ret = true; 968 | 969 | for (int i = 0; i < strLen; i++) 970 | if (!isdigit(str[i])) 971 | ret = false; 972 | 973 | return ret; 974 | } 975 | 976 | bool isidentifier_str(string str){ 977 | size_t strLen = strlen(str); 978 | bool ret = true; 979 | 980 | if (isdigit(str[0])) 981 | return false; 982 | else{ 983 | for (int i = 0; i < strLen; i++){ 984 | if (str[i] == ' ') 985 | return false; 986 | 987 | if (!isalnum(str[i]) && str[i] != '_') 988 | ret = false; 989 | } 990 | } 991 | return ret; 992 | } 993 | 994 | 995 | /** 996 | * @brief Trim of the heading and the trailing of any character that met inside the strip_content 997 | * 998 | * @param str The string to be trimmed 999 | * @param strip_content The delimiter for trimming 1000 | * @return string The new string that got trimmed the heading and the trailing 1001 | */ 1002 | string strip(string str, string strip_content){ 1003 | size_t start = 0; 1004 | size_t strLen = strlen(str); 1005 | size_t end = strLen - 1; 1006 | string ret = calloc(strLen, 1); 1007 | int i; 1008 | 1009 | //Trim the leading 1010 | while (strchr(strip_content, str[end])) 1011 | end--; 1012 | 1013 | //Trim the trailing 1014 | while (strchr(strip_content, str[start])) 1015 | start++; 1016 | 1017 | //Start copying data 1018 | for (i = 0; i <= end - start; i++) 1019 | ret[i] = str[i + start]; 1020 | 1021 | ret[i] = '\0'; 1022 | 1023 | ret = realloc(ret, strlen(ret) + 1); 1024 | 1025 | return ret; 1026 | } 1027 | 1028 | 1029 | /** 1030 | * @brief Check whether the string is of title type (First character of all word must be uppercase) 1031 | * 1032 | * @param str The string to be checked 1033 | * @return true When the string obeys the rule, false when the string not obeys the rule or only contains number 1034 | */ 1035 | bool istitle(string str){ 1036 | bool ret = true; 1037 | bool char_flag = false; //Check the case where there's no character and only number 1038 | size_t len = strlen(str); 1039 | size_t offsetS = 0; 1040 | size_t offsetE = 0; 1041 | string token; 1042 | 1043 | while (offsetE < len){ 1044 | while (isalpha(str[offsetE])){ 1045 | offsetE++; 1046 | char_flag = true; 1047 | } 1048 | 1049 | // Reach here when the delimiter was found 1050 | for (int i = 0; i < offsetE - offsetS; i++){ 1051 | if (isalpha((str + offsetS)[i])){ 1052 | if (i > 0){ 1053 | if (!islower((str + offsetS)[i])) 1054 | return false; 1055 | } 1056 | else{ 1057 | if (!isupper((str + offsetS)[i])) 1058 | return false; 1059 | } 1060 | } 1061 | } 1062 | 1063 | while (!isalpha(str[offsetE])) 1064 | offsetE++; 1065 | 1066 | offsetS = offsetE; 1067 | } 1068 | 1069 | return char_flag ? ret : false; 1070 | } 1071 | 1072 | /** 1073 | * @brief Turn the string to become title 1074 | * 1075 | * @param str The string to be transform 1076 | * @return string The new string that is obeys title form 1077 | */ 1078 | string title(string str){ 1079 | size_t len = strlen(str); 1080 | size_t i = 0; 1081 | string ret = malloc(len + 1); 1082 | 1083 | if (!ret){ 1084 | fprintf(stderr, "ERROR: title function: Can't allocating memory"); 1085 | exit(1); 1086 | } 1087 | strncpy(ret, str, len); 1088 | 1089 | while (i < len){ 1090 | //Cycle through the non-alpha character 1091 | while (!isalpha(str[i])){ 1092 | i++; 1093 | 1094 | if (i >= len) 1095 | goto CHECK_DONE; 1096 | } 1097 | 1098 | //And upper case the first one 1099 | ret[i] = toupper(ret[i]); 1100 | 1101 | //Then cycle through until meeting a non-alpha again 1102 | while (isalpha(str[i])){ 1103 | i++; 1104 | 1105 | if (i >= len) 1106 | goto CHECK_DONE; 1107 | } 1108 | i++; 1109 | } 1110 | 1111 | 1112 | CHECK_DONE: 1113 | return ret; 1114 | } 1115 | 1116 | 1117 | 1118 | float rsqrt(float number){ 1119 | float result = CNPY_rsqrt(number); 1120 | return result; 1121 | } 1122 | -------------------------------------------------------------------------------- /cnpython/exception.c: -------------------------------------------------------------------------------- 1 | #include "./include/cnpy_exceptions.h" 2 | 3 | 4 | 5 | void raise_exception(int except, char *file, int line){ 6 | if(except == AllocationError){ 7 | print("AllocationError: %s %d", file, line); 8 | exit(AllocationError); 9 | } 10 | else if(except == ReallocationError){ 11 | print("ReallocationError %s %d", file, line); 12 | exit(ReallocationError); 13 | } 14 | else if(except == MemoryError){ 15 | print("MemoryError %s %d", file, line); 16 | exit(MemoryError); 17 | } 18 | else if(except == FileOpenError){ 19 | print("FileOpenError %s %d", file, line); 20 | exit(FileOpenError); 21 | } 22 | 23 | } -------------------------------------------------------------------------------- /cnpython/help.c: -------------------------------------------------------------------------------- 1 | #include "./include/cnpy_help.h" 2 | 3 | 4 | void help(void){ 5 | print("\n\t\tCNPYTHON HELP\n\n"); 6 | print("\ntype -> type(var) returns a value from -1 to 10\n"); 7 | print("\nbin -> bin(decimal) converts a decimal number into binary form\n"); 8 | print("\nhex -> hex(decimal) converts a decimal number into hexadecimal form\n"); 9 | print("\noct -> oct(decimal) converts a decimal number into an octal\n"); 10 | print("\npow -> pow(base, exponent), multiplies base by itself as many times as the exponent says\n"); 11 | print("\nstr -> str(number) converts from an int to a string\n"); 12 | print("\n_int -> _int(string) converts from a string to an int\n"); 13 | print("\ninput -> input(prompt) prints out the prompt and takes input from the user\n"); 14 | print("\nreverse -> reverse(arr, size) reverses an array\n"); 15 | print("\nmin -> min(arr, size) gets the smallest element in an array\n"); 16 | print("\nmax -> max(arr, size) gets the biggest element in an array\n"); 17 | print("\nprint -> print(format, ...) printf but with the ability to print arrays built in\n"); 18 | print("\nsorted -> sorted(arr, size) sorts an array using timsort\n"); 19 | print("\nsum -> sum(arr, size) gets the sum of all the element in an array\n"); 20 | print("\nstr_len -> str_len(str) returns the length of a string\n"); 21 | print("\nstr_count -> str_count(str, find) returns how many times a character was found in a string\n"); 22 | print("\nendswith -> endswith(str, element) returns 0 if the string ends with the specified element\n"); 23 | print("\nreplace -> replace(str, find, repl) replaces all occurences of a character in a string\n"); 24 | print("\nfind -> find(str, element) returns the index of the element if it found it.\n"); 25 | print("\nstartswith -> startswith(str, element) returns 0 if it starts with the specified element\n"); 26 | print("\nstr_split -> str_split(arr, str, del) isn't really useful on it's own\n"); 27 | print("\nprintSplit -> printSplit(str, del) prints the array that is created after splitting a string\n"); 28 | print("\nchr -> chr(ascii) returns the character represented by a number\n"); 29 | print("\nord -> ord(char) returns the number represented by a character(opposite of chr)\n"); 30 | print("\nisin -> isin(str, value) returns 0 if the value specified was in the array\n"); 31 | print("\nrem - > rem(a, b) returns the remainder of a division between 2 numbers\n"); 32 | print("\npercentage -> percentage(p, n) returns the percentage\n"); 33 | print("\niappend -> iappend(arr, value, size) adds a value at the end of an array\n"); 34 | print("\ncopy -> copy(arr, dest, size) copies an array to another array\n"); 35 | print("\narr_count -> arr_count(arr, value, size) counts how many times a specified value was found\n"); 36 | print("\narr_find -> arr_find(arr, value, size) returns the index of the specified value\n"); 37 | print("\npop -> pop(arr, size) pops the last element from the array\n"); 38 | } 39 | -------------------------------------------------------------------------------- /cnpython/include/cnpy_exceptions.h: -------------------------------------------------------------------------------- 1 | #ifndef CNPY_EXCEPTIONS_H 2 | #define CNPY_EXCEPTIONS_H 3 | 4 | 5 | #include "./cnpython.h" 6 | 7 | 8 | /*Exceptions*/ 9 | #define AllocationError 1 10 | #define ReallocationError 2 11 | #define MemoryError 3 12 | #define ZeroDivisionError 4 13 | #define FileOpenError 5 14 | 15 | 16 | 17 | 18 | void raise_exception(int except, char *file, int line); 19 | 20 | 21 | #endif //CNPY_EXCEPTIONS_H -------------------------------------------------------------------------------- /cnpython/include/cnpy_help.h: -------------------------------------------------------------------------------- 1 | #ifndef CNPY_HELP_H 2 | #define CNPY_HELP_H 3 | 4 | #include "./cnpy_print.h" 5 | 6 | 7 | /*This functions gives a summary of all the functions that are in this project*/ 8 | void help(); 9 | 10 | #endif //CNPY_HELP_H -------------------------------------------------------------------------------- /cnpython/include/cnpy_print.h: -------------------------------------------------------------------------------- 1 | //Header guard start 2 | 3 | #ifndef CNPY_PRINT_H 4 | #define CNPY_PRINT_H 5 | 6 | 7 | #include "./cnpython.h" 8 | 9 | 10 | 11 | /*Prints integers*/ 12 | void print_int(int int_to_print); 13 | 14 | 15 | /*Prints an integer array*/ 16 | void print_int_arr(int *arr, int size); 17 | 18 | 19 | /*Prints a double array*/ 20 | void print_double_arr(double *arr, int size); 21 | 22 | /*Prints a long array*/ 23 | void print_long_arr(long *arr, int size); 24 | 25 | /*Prints string array*/ 26 | void print_str_arr(char **arr, int size); 27 | 28 | /*Basically printf but has array printing built in*/ 29 | void print(char *format, ...); 30 | 31 | 32 | #endif //CNPY_PRINT_H 33 | 34 | 35 | //Header guard end -------------------------------------------------------------------------------- /cnpython/include/cnpython.h: -------------------------------------------------------------------------------- 1 | 2 | 3 | /*Header guards*/ 4 | #ifndef CYTHON_H 5 | #define CYTHON_H 6 | 7 | /*Standard includes*/ 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | //not previously included for `ptrdiff_t` 18 | #include 19 | #include 20 | 21 | 22 | 23 | //Own includes 24 | 25 | #include "./cnpy_exceptions.h" 26 | #include "./cnpy_print.h" 27 | #include "./cnpy_help.h" 28 | 29 | 30 | 31 | 32 | //#define tuple(...) 33 | 34 | 35 | /*DEFINES*/ 36 | 37 | //python 'and' logical operator implemented in C 38 | #ifndef and 39 | #define and && 40 | #endif //and 41 | 42 | //python 'or' logical operator implemented in C 43 | #ifndef or 44 | #define or || 45 | #endif //or 46 | 47 | //python 'not' logical operator implemented in C 48 | #ifndef not 49 | #define not ! 50 | #endif //not 51 | 52 | //python 'is' logical operator implemented in C 53 | #ifndef is 54 | #define is == 55 | #endif //is 56 | 57 | //python 'isnt' logical operator implemented in C 58 | #ifndef isnt 59 | #define isnt != 60 | #endif //isnt 61 | 62 | //supposed to be pythons 'in' operator but it's not really good implemented 63 | #ifndef in 64 | #define in , 65 | #endif //in 66 | 67 | //else if in python 68 | #ifndef elif 69 | #define elif else if 70 | #endif //elif 71 | 72 | //if the True macro is not defined we define it and if it is we skip 73 | #ifndef True 74 | #define True 1 75 | #endif 76 | 77 | //if the False macro is not defined we define it and if it is we skip 78 | #ifndef False 79 | #define False 0 80 | #endif 81 | 82 | //None = NULL 83 | #ifndef None 84 | #define None ((void*)0) //basically NULL 85 | #endif //None 86 | 87 | #ifndef class 88 | #define class struct 89 | #endif 90 | 91 | 92 | //used in timsort function in 'cnpython.c' 93 | #ifndef RUN 94 | #define RUN 32 95 | #endif //RUN 96 | 97 | //MAX and MIN macro functions 98 | 99 | #define MAX(x, y) (((x) > (y)) ? (x) : (y)) 100 | #define MIN(x, y) (((x) < (y)) ? (x) : (y)) 101 | 102 | 103 | 104 | /*STRUCTS*/ 105 | 106 | 107 | /* 108 | typedef void* var; 109 | */ 110 | 111 | //typedefs the char array datatype to a custom one called 'string' 112 | typedef char* string; 113 | 114 | 115 | /*extern var Int; 116 | extern var String; 117 | extern var Double; 118 | extern var Float; 119 | extern var Char; 120 | extern var Long; 121 | extern var Function; 122 | //extern var Tuple; 123 | */ 124 | 125 | /*struct Int{ 126 | int val; 127 | }; 128 | //typedef Int inte; 129 | 130 | 131 | 132 | struct String{ 133 | char *val; 134 | }; 135 | //typedef String string; 136 | 137 | 138 | 139 | struct Double{ 140 | double val; 141 | }; 142 | //typedef Double dbl; 143 | 144 | 145 | 146 | struct Float{ 147 | float val; 148 | }; 149 | //typedef Float flt; 150 | 151 | 152 | 153 | struct Char{ 154 | char val; 155 | }; 156 | //typedef Char character; 157 | 158 | 159 | 160 | struct Long{ 161 | long val; 162 | }; 163 | //typedef Long lng; 164 | 165 | 166 | 167 | struct Function{ 168 | var (*func)(var); 169 | };*/ 170 | 171 | 172 | 173 | 174 | 175 | 176 | /*struct Tuple{ 177 | const var* val; 178 | };*/ 179 | 180 | 181 | 182 | 183 | 184 | 185 | 186 | /*Converts any decimal value to a binary value*/ 187 | extern long long bin(long decimal, ...); 188 | 189 | /*Converts any decimal value to a hexadecimal value*/ 190 | extern string hex(long n, ...); 191 | 192 | /*Converts any decimal value to an octal value*/ 193 | extern long oct(long dec, ...); 194 | 195 | /*Power function*/ 196 | extern double power(double base, double exponent); 197 | 198 | /*Factorial function*/ 199 | extern int factorial(int n); 200 | 201 | 202 | /*Converts a number to a string*/ 203 | extern string str(int number); 204 | 205 | /*Converts a string to an integer*/ 206 | extern int _int(string str); 207 | 208 | /*Converts a number into a boolean*/ 209 | extern int _bool(int n); 210 | 211 | 212 | /*Takes as a parameter the prompt for the user and reads the input byte by byte and returns it*/ 213 | extern string input(string str); 214 | 215 | /*Reverses an int array*/ 216 | extern int* reversed(int* arr, int size); 217 | 218 | //! TODO: Make reversed functions for other array data types as well 219 | 220 | 221 | 222 | 223 | /*Returns the biggest element in an array*/ 224 | extern double max (double numbers[], int arrlen); 225 | 226 | /*Returns the smallest element in an array*/ 227 | extern double min(double numbers[], int arrlen); 228 | 229 | 230 | /*Used in tim sort algorithm*/ 231 | extern void insertionSort(int arr[], int left, int right); 232 | 233 | /*Merge function is used in the tim sort algorithm*/ 234 | extern void merge(int arr[], int l, int m, int r); 235 | 236 | /*Tim Sort algorithm*/ 237 | extern void timSort(int arr[], int n); 238 | 239 | /*uses timSort algorithm to sort an array*/ 240 | extern int* sorted(int *arr, int size); 241 | 242 | 243 | 244 | /*Gets the sum of all the elements in an array*/ 245 | extern long sum(int *arr, int size); 246 | 247 | /*Returns the length of a string*/ 248 | extern int str_len(string str); 249 | 250 | /*Counts how many times the program found a character in a given string*/ 251 | extern int str_count(string str, char find); 252 | 253 | /*Returns 0 if the certain string ends with the element provided*/ 254 | extern int str_endswith(string str, char element); 255 | 256 | /*Joins 2 strings togheter and returns a third string*/ 257 | extern string str_join(string str1, string str2); 258 | 259 | /*Replaces all found occurunces of a character with another character 260 | * and returns a new string*/ 261 | extern string str_replace(string str, char find, char repl); 262 | 263 | /*Returns the index of the character if it was found in the string, if not it returns -1*/ 264 | extern int str_find(string str, char find); 265 | 266 | /*Returns 0 if the string starts with the specified element*/ 267 | extern int str_startswith(string str, char element); 268 | 269 | 270 | /*Idk what this was*/ 271 | //extern size_t str_split(string **array, string str, const string del); 272 | 273 | 274 | /*Reads the whole entire file you pass it*/ 275 | extern string read_file(string filename); 276 | 277 | 278 | /*Prints the splited string*/ 279 | //extern void printSplit(string str, const char del); 280 | 281 | /*Splits a string and returns an array*/ 282 | extern void str_split(string str, const string del); 283 | 284 | 285 | /* Returns the char for the ASCII code */ 286 | extern char chr(int ascii); 287 | 288 | /* Returns the ASCII code for the char */ 289 | extern int ord(char c); 290 | 291 | /*Returns 0 of the value was found it the specified string*/ 292 | extern int isin(string str, char value); 293 | 294 | /*Returns the remainder of the division of 2 numbers*/ 295 | extern int rem(int a, int b); 296 | 297 | /*Returns the percentage*/ 298 | extern double percentage(float p, float n); 299 | 300 | /*Adds a new value to the end of a string*/ 301 | extern void iappend(int *arr, int value, int size); 302 | 303 | /*Copies the elements of an array to another array*/ 304 | extern int *copy(int *arr, int *dest, int size); 305 | 306 | /*Returns how many times a value was found inside an array*/ 307 | extern int arr_count(int *arr, int value, int size); 308 | 309 | 310 | /*Returns the index at which it found a specified value, if it didn't find it, returns -1*/ 311 | extern int arr_find(int *arr, int value, int size); 312 | 313 | /*Pops out the last element of an array*/ 314 | extern void pop(int *arr, int size); 315 | 316 | 317 | /*Returns the absolute value of an integer number*/ 318 | extern int i_abs(int number); 319 | 320 | /*Returns the absolute value of a double number*/ 321 | extern double d_abs(double number); 322 | 323 | /*Returns True if all the elements in the array are True*/ 324 | extern int all(int *arr, int size); 325 | 326 | /*Returns True if all the elements in the array are True. For double type arrays*/ 327 | extern int d_all(double *arr, int size); 328 | 329 | /*Returns True if one of the elements in the array is True for int arrays*/ 330 | extern int any(int *arr, int size); 331 | 332 | /*Returns true if one of the elements in the array is True for double arrays*/ 333 | extern int d_any(double *arr, int size); 334 | 335 | /*Returns True if all the elements in a string are lowercase*/ 336 | extern int str_islower(string str); 337 | 338 | /*Returns True if all the elemenets in a string are uppercase*/ 339 | extern int str_isupper(string str); 340 | 341 | /*Returns True if all the elements in the string are whitespaces*/ 342 | extern int str_isspace(string str); 343 | 344 | /*Returns True if all the elements in the string are from the alphabet*/ 345 | extern int str_isalpha(string str); 346 | 347 | /*Returns True if all the elements in the string are digits*/ 348 | extern int str_isdigit(string str); 349 | 350 | /*Splits the string at line breaks and returns an array*/ 351 | extern void str_splitlines(string str); 352 | 353 | /** 354 | * @brief Find the substring inside a string. Supporting search zone feature. Set negative value at end offset for end of the string 355 | * 356 | * @param str The given string 357 | * @param value The substring to be found inside str 358 | * @param start The offset of when to start 359 | * @param end The end range 360 | * @return ptrdiff_t Return the index of the first character of the substring when found. Else, -1 is returned 361 | */ 362 | extern ptrdiff_t find_str(string str, string value, const unsigned int start, const int end); 363 | 364 | /** 365 | * @brief Works just like the find method. However, an error is thrown when not found then given value 366 | */ 367 | extern ptrdiff_t index_str(string str, string value, const unsigned int start, const int end); 368 | 369 | /** 370 | * @brief Justify the given string to the right of a new string 371 | * 372 | * @param str The string to be justified 373 | * @param len The len of the fill offset and the string itself 374 | * @param fill The character to be filled 375 | * @return string The new string that right-justified the old string 376 | */ 377 | extern string rjust(string str, const size_t len, const char fill); 378 | 379 | 380 | /** 381 | * @brief Justify the given string to the right of a new string 382 | * 383 | * @param str The string to be justified 384 | * @param len The len of the fill offset and the string itself 385 | * @param fill The character to be filled 386 | * @return string The new string that right-justified the old string 387 | */ 388 | extern string rjust(string str, const size_t len, const char fill); 389 | 390 | /** 391 | * @brief Justify the given string to the left of a new string 392 | * 393 | * @param str The string to be justified 394 | * @param len The len of string and the fill offset 395 | * @param fill The character to be filled 396 | * @return string The new string that left-justified the old string 397 | */ 398 | extern string ljust(string str, const size_t len, const char fill); 399 | 400 | /** 401 | * @brief Basically right-justified but auto fill with zero (ascii value) instead 402 | * 403 | * @param str The string to be justified 404 | * @param len The len of string and the fill offset 405 | * @return char* The new string that right-justified and fills with zero 406 | */ 407 | extern char* zfill(string str, size_t len); 408 | 409 | 410 | //Some more checking method for string 411 | 412 | extern bool isspace_str(string str); 413 | extern bool isprintable_str(string str); 414 | extern bool isdigit_str(string str); 415 | extern bool isidentifier_str(string str); 416 | 417 | /** 418 | * @brief Trim of the heading and the trailing of any character that met inside the strip_content 419 | * 420 | * @param str The string to be trimmed 421 | * @param strip_content The delimiter for trimming 422 | * @return string The new string that got trimmed the heading and the trailing 423 | */ 424 | extern string strip(string str, string strip_content); 425 | 426 | /** 427 | * @brief Check whether the string is of title type (First character of all word must be uppercase) 428 | * 429 | * @param str The string to be checked 430 | * @return true When the string obeys the rule, false when the string not obeys the rule or only contains number 431 | */ 432 | extern bool istitle(string str); 433 | 434 | /** 435 | * @brief Turn the string to become title 436 | * 437 | * @param str The string to be transform 438 | * @return string The new string that is obeys title form 439 | */ 440 | extern string title(string str); 441 | 442 | /*Returns the inverse square root*/ 443 | extern float rsqrt(float number); 444 | 445 | 446 | #endif /*CYTHON_H*/ 447 | /*End of header guard*/ 448 | -------------------------------------------------------------------------------- /cnpython/include/datastructures/linkedlist.h: -------------------------------------------------------------------------------- 1 | #ifndef LINKEDLIST_H 2 | #define LINKEDLIST_H 3 | 4 | 5 | 6 | #include "../cnpython.h" 7 | 8 | /*struct*/ 9 | typedef class node{ 10 | int data; 11 | struct node* next; 12 | }node_t; 13 | 14 | 15 | void ll_print(node_t *head); 16 | void ll_push_end(node_t *head, int data); 17 | void ll_push_beg(node_t **head, int data); 18 | int ll_pop_first(node_t **head); 19 | int ll_pop_last(node_t *head); 20 | int ll_remove_by_index(node_t **head, int n); 21 | void ll_remove_by_value(node_t **head, int val); 22 | 23 | 24 | 25 | #endif //LINKEDLIST_H -------------------------------------------------------------------------------- /cnpython/include/datastructures/stack.h: -------------------------------------------------------------------------------- 1 | #ifndef STACK_H 2 | #define STACK_H 3 | 4 | 5 | #include "../cnpython.h" 6 | 7 | 8 | typedef struct StackNode{ 9 | int data; 10 | struct StackNode *next; 11 | }snode_t; 12 | 13 | 14 | struct StackNode *stack_newNode(int data); 15 | int stack_isEmpty(snode_t *root); 16 | void stack_push(snode_t **root, int data); 17 | int stack_pop(snode_t **root); 18 | int stack_peek(snode_t *root); 19 | 20 | 21 | 22 | 23 | 24 | 25 | #endif //STACK_H -------------------------------------------------------------------------------- /cnpython/linkedlist.c: -------------------------------------------------------------------------------- 1 | #include "./include/datastructures/linkedlist.h" 2 | 3 | 4 | /*print the linked list*/ 5 | void ll_print(node_t *head){ 6 | node_t *c = head; 7 | //looping through the linked list 8 | while(c isnt None){ 9 | //printing every node's data 10 | print("%d", c->data); 11 | c = c->next; 12 | } 13 | } 14 | 15 | 16 | 17 | /*push to the linked list a value*/ 18 | void ll_push_end(node_t *head, int data){ 19 | node_t *c = head; 20 | //looping to the end of the linked list 21 | while(c->next isnt None){ 22 | c = c->next; 23 | } 24 | 25 | //creating a new node at the end of the list 26 | c->next = (node_t*)malloc(sizeof(node_t)); 27 | //sets the new node's value 28 | c->next->data = data; 29 | c->next->next = None; 30 | } 31 | 32 | 33 | void ll_push_beg(node_t **head, int data){ 34 | //creating a new node 35 | node_t *new_node = (node_t*)malloc(sizeof(node_t)); 36 | 37 | //setting it's value 38 | new_node->data = data; 39 | //setting it's position at the beggining 40 | new_node->next = *head; 41 | *head = new_node; 42 | } 43 | 44 | 45 | 46 | /*pop from the linked list*/ 47 | int ll_pop_first(node_t **head){ 48 | int retval = -1; 49 | node_t *next_node = None; 50 | 51 | //if head is None 52 | if(not *head){ 53 | return -1; 54 | } 55 | 56 | next_node = (*head)->next; 57 | retval = (*head)->data; 58 | free(*head); 59 | *head = next_node; 60 | 61 | return retval; 62 | } 63 | 64 | int ll_pop_last(node_t *head) 65 | { 66 | int retval = 0; 67 | 68 | // if there's only one element in the list it removes it 69 | if (head->next is None){ 70 | retval = head->data; 71 | free(head); 72 | return retval; 73 | } 74 | 75 | /* get to the second to last node in the list */ 76 | 77 | node_t *c = head; 78 | while(c->next->next isnt None){ 79 | c = c->next; 80 | } 81 | 82 | /* now current points to the second to last item of the list, so let's remove current->next */ 83 | retval = c->next->data; 84 | free(c->next); 85 | c->next = None; 86 | return retval; 87 | } 88 | 89 | 90 | int remove_by_index(node_t **head, int n){ 91 | int retval = -1; 92 | node_t *c = *head; 93 | node_t *temp = None; 94 | 95 | if(n is 0){ 96 | return ll_pop_first(head); 97 | } 98 | 99 | for(int i = 0; i < n-1; i++){ 100 | if(c->next is None){ 101 | return -1; 102 | } 103 | c = c->next; 104 | } 105 | 106 | if(c->next is None){ 107 | return -1; 108 | } 109 | 110 | temp = c->next; 111 | retval = temp->data; 112 | c->next = temp->next; 113 | free(temp); 114 | 115 | 116 | return retval; 117 | } 118 | 119 | void ll_remove_by_value(node_t **head, int val){ 120 | node_t *temp = *head, *prev; 121 | 122 | if (temp isnt None && temp->data is val){ 123 | *head = temp->next; 124 | free(temp); 125 | } 126 | 127 | while (temp isnt None && temp->data isnt val){ 128 | prev = temp; 129 | temp = temp->next; 130 | } 131 | 132 | if (temp is None){ 133 | return; 134 | } 135 | 136 | prev->next = temp->next; 137 | 138 | free(temp); 139 | } -------------------------------------------------------------------------------- /cnpython/print.c: -------------------------------------------------------------------------------- 1 | #include "./include/cnpy_print.h" 2 | 3 | 4 | 5 | /*Prints integers*/ 6 | void print_int(int int_to_print){ 7 | int a = int_to_print; 8 | 9 | if (int_to_print < 0){ 10 | putchar('-'); 11 | int_to_print = -int_to_print; 12 | } 13 | 14 | if (int_to_print > 9) print_int(int_to_print/10); 15 | 16 | putchar('0'+ (a%10)); 17 | } 18 | 19 | 20 | /*Prints an integer array*/ 21 | void print_int_arr(int *arr, int size){ 22 | for(int i = 0; i < size; i++){ 23 | printf("[%d], ", arr[i]); 24 | } 25 | } 26 | 27 | 28 | /*Prints a double array*/ 29 | void print_double_arr(double *arr, int size){ 30 | for(int i = 0; i < size; i++){ 31 | printf("[%f], ", arr[i]); 32 | } 33 | } 34 | 35 | /*Prints a long array*/ 36 | void print_long_arr(long *arr, int size){ 37 | for(int i = 0; i < size; i++){ 38 | printf("[%ld], ", arr[i]); 39 | } 40 | } 41 | 42 | 43 | 44 | /*Prints a string array*/ 45 | void print_str_arr(char **arr, int size){ 46 | for(int i = 0; i < size+1; i++){ 47 | printf("[%s], ", arr[i]); 48 | } 49 | } 50 | 51 | 52 | 53 | 54 | /*Basically printf but has array printing built in*/ 55 | void print(char *format, ...){ 56 | va_list argp; 57 | va_start(argp, format); 58 | 59 | while (*format != '\0') { 60 | if (*format == '%') { 61 | format++; 62 | 63 | if (*format == '%') { 64 | putchar('%'); 65 | } 66 | 67 | else if (*format == 'c') { //this is for a character 68 | char char_to_print = va_arg(argp, int); 69 | putchar(char_to_print); 70 | } 71 | 72 | else if(*format == 's'){ //this is for a string 73 | char* string_to_print = va_arg(argp, char*); 74 | 75 | for(int i = 0; i < strlen(string_to_print); i++){ 76 | putchar(string_to_print[i]); 77 | } 78 | } 79 | else if(*format == 'd'){ //this is for an integer 80 | int int_to_print = va_arg(argp, int); 81 | print_int(int_to_print); 82 | } 83 | 84 | else if(*format == 't'){ //this is for an integer array 85 | int *int_arr_to_print = va_arg(argp, int*); 86 | int size = sizeof(int_arr_to_print)/sizeof(int_arr_to_print[0]); 87 | print_int_arr(int_arr_to_print, size); 88 | } 89 | 90 | else if(*format == 'e'){ //for double arrays 91 | double *double_arr_to_print = va_arg(argp, double*); 92 | int size = sizeof(double_arr_to_print)/sizeof(double_arr_to_print[0]); 93 | print_double_arr(double_arr_to_print, size); 94 | } 95 | 96 | else if(*format == 'g'){ //for long arrays 97 | long *long_arr_to_print = va_arg(argp, long*); 98 | int size = sizeof(long_arr_to_print)/sizeof(long_arr_to_print[0]); 99 | print_long_arr(long_arr_to_print, size); 100 | } 101 | 102 | else if(*format == 'r'){ 103 | char **str_arr_to_print = va_arg(argp, char**); 104 | int size = sizeof(str_arr_to_print)/sizeof(str_arr_to_print[0]); 105 | 106 | print_str_arr(str_arr_to_print, size); 107 | } 108 | 109 | 110 | else { 111 | fputs("Not implemented", stdout); 112 | } 113 | 114 | } else { 115 | putchar(*format); 116 | } 117 | format++; 118 | } 119 | putchar('\n'); 120 | va_end(argp); 121 | } 122 | -------------------------------------------------------------------------------- /cnpython/stack.c: -------------------------------------------------------------------------------- 1 | #include "./include/datastructures/stack.h" 2 | 3 | 4 | 5 | snode_t *stack_newNode(int data){ 6 | snode_t *stack_node = (snode_t *)malloc(sizeof(snode_t)); 7 | stack_node->data = data; 8 | stack_node->next = None; 9 | 10 | return stack_node; 11 | } 12 | 13 | 14 | int stack_isEmpty(snode_t *root){ 15 | return !root; 16 | } 17 | 18 | void stack_push(snode_t **root, int data){ 19 | snode_t *stack_node = stack_newNode(data); 20 | stack_node->next = *root; 21 | *root = stack_node; 22 | 23 | //print("%d pushed to the stack", data); 24 | } 25 | 26 | int stack_pop(snode_t **root){ 27 | if(stack_isEmpty(*root)){ 28 | return INT_MIN; 29 | } 30 | snode_t *temp = *root; 31 | *root = (*root)->next; 32 | int popped = temp->data; 33 | free(temp); 34 | 35 | return popped; 36 | } 37 | 38 | 39 | int peek(snode_t *root){ 40 | if(stack_isEmpty(root)){ 41 | return INT_MIN; 42 | } 43 | return root->data; 44 | } -------------------------------------------------------------------------------- /testing/abs/abs: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Arin-Grigoras/CnPython/074c2f94b3fe29d04ea584743e33a23923ec75a4/testing/abs/abs -------------------------------------------------------------------------------- /testing/abs/abs.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | 4 | int int_abs(int); 5 | double double_abs(double); 6 | 7 | 8 | int main(void){ 9 | int int_x = 10; 10 | int int_y = -10; 11 | 12 | printf("%d - and - %d\n", int_abs(int_x), int_abs(int_y)); 13 | 14 | double double_x = 10.05; 15 | double double_y = -10.05; 16 | 17 | printf("%.2f - and - %.2f\n", double_abs(double_x), double_abs(double_y)); 18 | 19 | 20 | return 0; 21 | } 22 | 23 | int int_abs(int number) 24 | { 25 | return (((number) < (0)) ? (-number) : (number)); 26 | } 27 | 28 | double double_abs(double number) 29 | { 30 | return (((number) < (0)) ? (-number) : (number)); 31 | } 32 | -------------------------------------------------------------------------------- /testing/all/all.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | 4 | int all(int *arr, int size){ 5 | //int total_nulls = 0; 6 | int total_trues = 0; 7 | 8 | for(int i = 0; i < size; i++){ 9 | /*if(arr[i] == NULL){ 10 | total_nulls++; 11 | }*/ 12 | if(arr[i] != 0){ 13 | total_trues++; 14 | } 15 | } 16 | 17 | /*if(total_trues == size && total_nulls == size){ 18 | return 1; 19 | }*/ 20 | 21 | if(total_trues == size){ 22 | return 1; 23 | } 24 | 25 | return 0; 26 | } 27 | 28 | 29 | int main(void){ 30 | 31 | int arr2[5] = {1, 1, 1, 1, 2}; 32 | int arr3[5] = {2, 0, 0, 2, 0}; 33 | 34 | if(all(arr2, 5) == 1){ 35 | printf("arr2: True\n"); 36 | } 37 | 38 | else{ 39 | printf("arr2: False\n"); 40 | } 41 | 42 | if(all(arr3, 5) == 1){ 43 | printf("arr3: True\n"); 44 | } 45 | else{ 46 | printf("arr3: False\n"); 47 | } 48 | } -------------------------------------------------------------------------------- /testing/and/and.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define and && 4 | 5 | int main(void){ 6 | int a, b; 7 | a = 10; 8 | b = 10; 9 | if(a == b and b == a){ 10 | printf("Works"); 11 | } 12 | } -------------------------------------------------------------------------------- /testing/append/append.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "../../cnpython/print.h" 3 | 4 | char **arrs_append(char **arr, char *value, int size){ 5 | arr[size] = value; 6 | 7 | return arr; 8 | } 9 | 10 | 11 | int *arri_append(int *arr, int value, int size){ 12 | arr[size] = value; 13 | 14 | return arr; 15 | } 16 | 17 | void iappend(int *arr, int value, int size){ 18 | arr[size] = value; 19 | } 20 | 21 | 22 | int main(void){ 23 | char *str_arr[] = {"Hello"}; 24 | int int_arr[] = {1}; 25 | 26 | iappend(int_arr, 2, 1); 27 | 28 | } -------------------------------------------------------------------------------- /testing/arr_clear/clear.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "../../cnpython/print.h" 4 | 5 | /*Doesn't */ 6 | 7 | void clear(int *arr){ 8 | memset(arr, 0, sizeof(arr)); 9 | } 10 | 11 | int main(void){ 12 | int arr[4] = {1, 2, 3, 4}; 13 | 14 | print_int_arr(arr, 4); 15 | print("\n"); 16 | 17 | clear(arr); 18 | 19 | print("%d\n", arr[0]); 20 | } -------------------------------------------------------------------------------- /testing/arr_count/count.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int arr_count(int *arr, int value, int size){ 4 | int count = 0; 5 | 6 | for(int i = 0; i < size; i++){ 7 | if(arr[i] == value){ 8 | count++; 9 | } 10 | } 11 | 12 | return count; 13 | } 14 | 15 | int main(void){ 16 | int arr[] = {1, 1, 2}; 17 | 18 | printf("%d\n", arr_count(arr, 1, 3)); 19 | } -------------------------------------------------------------------------------- /testing/arr_find/find.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int arr_find(int *arr, int value, int size){ 4 | for(int i = 0; i < size; i++){ 5 | if(arr[i] == value){ 6 | return i; 7 | } 8 | } 9 | 10 | return -1; 11 | } 12 | 13 | int main(void){ 14 | int arr[] = {1, 2, 3}; 15 | printf("%d\n", arr_find(arr, 2, 3)); 16 | } -------------------------------------------------------------------------------- /testing/bin/bin.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | 6 | #define true 1 7 | #define false 0 8 | 9 | 10 | long long dec_to_bin(long decimal, ...){ 11 | long long bin = 0; 12 | int rem, i = 1, step = 1; 13 | va_list valist; 14 | while (decimal != 0) { 15 | rem = decimal % 2; 16 | decimal /= 2; 17 | bin += rem * i; 18 | i *= 10; 19 | } 20 | 21 | va_start(valist, decimal); 22 | 23 | if(va_arg(valist, int) == 1){ 24 | printf("0b%lld\n", bin); 25 | } 26 | 27 | 28 | va_end(valist); 29 | 30 | 31 | 32 | return bin; 33 | } 34 | 35 | 36 | int main(void){ 37 | printf("%lld\n", dec_to_bin(10, false)); 38 | long long test = dec_to_bin(10, false); 39 | 40 | printf("%lld\n", test); 41 | 42 | 43 | } 44 | -------------------------------------------------------------------------------- /testing/capitalize/capitalize.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | /* 6 | * Failed Capitalize function 7 | * */ 8 | 9 | void capitalize(char *str){ 10 | toupper(str[0]); 11 | } 12 | 13 | int main(void){ 14 | 15 | char *str = "hello world"; 16 | 17 | 18 | 19 | printf("%s", str); 20 | } 21 | -------------------------------------------------------------------------------- /testing/chr/chr.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "../../src/cnpython.h" 3 | 4 | int main(void){ 5 | print("<%c>", chr(65)); 6 | } 7 | -------------------------------------------------------------------------------- /testing/comments/comm.c: -------------------------------------------------------------------------------- 1 | #include 2 | #define $ // 3 | 4 | //$ hello world -------------------------------------------------------------------------------- /testing/copy/copy.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "../../cnpython/print.h" 3 | 4 | int *copy(int *arr, int *dest, int size){ 5 | for(int i = 0; i < size; i++){ 6 | dest[i] = arr[i]; 7 | } 8 | 9 | return dest; 10 | } 11 | 12 | int main(void){ 13 | int arr[] = {1, 2, 3, 4}; 14 | int dest[4]; 15 | 16 | copy(arr, dest, 4); 17 | 18 | print_int_arr(arr, 4); 19 | print("\n"); 20 | print_int_arr(dest, 4); 21 | } -------------------------------------------------------------------------------- /testing/count/count.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int str_count(char *str, char find){ 5 | 6 | int count = 0; 7 | 8 | for(int i = 0; i < strlen(str); i++){ 9 | if(str[i] == find){ 10 | count++; 11 | } 12 | } 13 | 14 | return count; 15 | } 16 | 17 | int main(void){ 18 | char *str = "test"; 19 | printf("Found 't' %d times", str_count(str, 't')); 20 | 21 | } 22 | -------------------------------------------------------------------------------- /testing/elif/elif.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define elif else if 4 | 5 | int main(void){ 6 | if(1 < 0){ 7 | printf("something"); 8 | } 9 | elif(0 < 1){ 10 | printf("Elif works"); 11 | } 12 | } -------------------------------------------------------------------------------- /testing/endswith/endswith.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int endswith(char *str, char element){ 5 | if(str[strlen(str)-1] == element){ 6 | return 0; 7 | } 8 | 9 | return -1; 10 | } 11 | 12 | int main(void){ 13 | char *str = "hello worlo"; 14 | if(endswith(str, 'd') == 0){ 15 | printf("%s ends with 'd'\n", str); 16 | } 17 | else{ 18 | printf("%s doesn't end with 'd'\n", str); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /testing/find/find.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | 5 | int find(char *str, char find){ 6 | for(int i = 0; i < strlen(str); i++){ 7 | if(str[i] == find){ 8 | return i; 9 | } 10 | } 11 | 12 | return -1; 13 | } 14 | 15 | 16 | int main(void){ 17 | char *str = "Hello world"; 18 | printf("%d", find(str, 'e')); 19 | } 20 | -------------------------------------------------------------------------------- /testing/find_str/find_str.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "../../cnpython/include/cnpython.h" 4 | #include 5 | #include 6 | #include 7 | 8 | /** 9 | * @brief Find the substring inside a string. Supporting search zone feature. Set negative value at end offset for end of the string 10 | * 11 | * @param str The given string 12 | * @param value The substring to be found inside str 13 | * @param start The offset of when to start 14 | * @param end The end range 15 | * @return ptrdiff_t Return the index of the first character of the substring when found. Else, -1 is returned 16 | */ 17 | ptrdiff_t find_str(string str, string value, const unsigned int start, const int end){ 18 | if (end > 0){ 19 | size_t strLen = strlen(str); 20 | string copy = malloc(strLen); 21 | 22 | strncpy(copy, str, strLen); 23 | //Since we only find until "end" index 24 | copy[end] = '\0'; 25 | 26 | char* strstrRet = strstr(copy + start, value); 27 | return strstrRet == NULL ? -1 : strstrRet - copy; 28 | } 29 | 30 | char* strstrRet = strstr(str + start, value); 31 | return strstrRet == NULL ? -1 : strstrRet - str; 32 | } 33 | 34 | /** 35 | * @brief Works just like the find method. However, an error is thrown when not found then given value 36 | */ 37 | ptrdiff_t index_str(string str, string value, const unsigned int start, const int end){ 38 | char* strstrRet; 39 | if (end > 0){ 40 | size_t strLen = strlen(str); 41 | string copy = malloc(strLen); 42 | 43 | strncpy(copy, str, strLen); 44 | //Since we only find until "end" index 45 | copy[end] = '\0'; 46 | 47 | strstrRet = strstr(copy + start, value); 48 | if (strstrRet == NULL){ 49 | fprintf(stderr, "ValueError: substring not found, given: \"%s\"\n", value); 50 | exit(1); 51 | } 52 | }else{ 53 | strstrRet = strstr(str + start, value); 54 | if (strstrRet == NULL){ 55 | fprintf(stderr, "ValueError: substring not found, given: \"%s\"\n", value); 56 | exit(1); 57 | } 58 | } 59 | 60 | return strstrRet - str; 61 | } 62 | 63 | int main(){ 64 | int pos = find_str("This is a string", "is", 5, -1); 65 | printf("The first postion of the string \"is\" is %d\n", pos); 66 | 67 | int not_real_pos = find_str("This is a string", "zer", 5, -1); 68 | printf("The first postion of the string \"zer\" is %d\n", not_real_pos); 69 | 70 | pos = index_str("This is a string", "is", 5, -1); 71 | printf("The first postion of the string \"is\" is %d\n", pos); 72 | 73 | not_real_pos = index_str("This is a string", "zer", 5, -1); 74 | printf("The first postion of the string \"zer\" is %d\n", not_real_pos); 75 | 76 | return 0; 77 | } -------------------------------------------------------------------------------- /testing/functions/func.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "../../cnpython/cnpython.h" 3 | 4 | 5 | var name(var name){ 6 | return name; 7 | } 8 | 9 | var add(int a, int b){ 10 | return a + b; 11 | } 12 | 13 | 14 | var main(void){ 15 | 16 | print("Hello world"); 17 | 18 | printf("%d\n", add(1, 2)); 19 | 20 | return 0; 21 | } -------------------------------------------------------------------------------- /testing/help/help.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void help(){ 4 | printf("\n\t\tCYTHON HELP\n\n"); 5 | printf("*bin -> bin(decimal) converts a decimal number into binary form\n"); 6 | 7 | } 8 | 9 | int main(){ 10 | //INSERT FUNCTION HERE 11 | } 12 | -------------------------------------------------------------------------------- /testing/hex/hex.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | void hex(int n) 5 | { 6 | char *hexaDeciNum = (char*)malloc(sizeof(char) * 100); 7 | 8 | if(!hexaDeciNum){ 9 | printf("\n\nERROR: Hex() function error\nCouldn't allocate memory\n\n"); 10 | exit(1); 11 | } 12 | 13 | int i = 0; 14 | while (n != 0) { 15 | int temp = 0; 16 | 17 | temp = n % 16; 18 | 19 | if (temp < 10) { 20 | hexaDeciNum[i] = temp + 48; 21 | i++; 22 | } 23 | else { 24 | hexaDeciNum[i] = temp + 55; 25 | i++; 26 | } 27 | 28 | n = n / 16; 29 | } 30 | 31 | printf("0x"); 32 | 33 | for(int j = i - 1; j >= 0; j--){ 34 | printf("%c", hexaDeciNum[j]); 35 | } 36 | 37 | } 38 | 39 | 40 | int main(void){ 41 | hex(2545); 42 | } 43 | -------------------------------------------------------------------------------- /testing/hex/id/id.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /* 4 | *Code does not work, please create PR if you know the answer. 5 | * */ 6 | 7 | 8 | /* 9 | *26/2/2022 EDIT: This is basically useless because you can just print out '&var' 10 | */ 11 | 12 | int* id(int var){ 13 | return (&var); 14 | } 15 | 16 | int main(void){ 17 | int a = 10; 18 | printf("%ls", id(a)); 19 | printf("%p", &a); 20 | } 21 | -------------------------------------------------------------------------------- /testing/input/input.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | char *input(char *str){ 5 | int bufsize = 100; 6 | int position = 0; 7 | char *buffer = malloc(sizeof(char) * bufsize); 8 | int c; 9 | 10 | if (!buffer) { 11 | printf("\n\nERROR: input() function error\nCouldn't alocate memory\n\n"); 12 | exit(1); 13 | } 14 | 15 | printf("%s", str); 16 | 17 | while (1) { 18 | 19 | c = getchar(); 20 | 21 | if (c == EOF || c == '\n') { 22 | buffer[position] = '\0'; 23 | return buffer; 24 | } else { 25 | buffer[position] = c; 26 | 27 | } 28 | 29 | position++; 30 | 31 | if (position >= bufsize) { 32 | bufsize += 100; 33 | buffer = realloc(buffer, bufsize); 34 | if (!buffer) { 35 | printf("\n\nError: REALLOCATION error\n\n"); 36 | exit(EXIT_FAILURE); 37 | } 38 | } 39 | } 40 | 41 | return buffer; 42 | } 43 | 44 | int main(void){ 45 | char *test = input("enter input here -> "); 46 | printf("%s", test); 47 | } 48 | -------------------------------------------------------------------------------- /testing/int/int.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int _int(char *str){ 6 | return atoi(str); 7 | } 8 | 9 | int main(void){ 10 | //INSERT FUNCTION HERE 11 | int number = _int("20"); 12 | printf("%d", number); 13 | } 14 | -------------------------------------------------------------------------------- /testing/isin/isin.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int isin(char *str, char value){ 5 | for(int i = 0; i < strlen(str); i++){ 6 | if(str[0] == value){ 7 | return 0; 8 | } 9 | } 10 | 11 | return -1; 12 | } 13 | 14 | int main(void){ 15 | char *str = "hello world"; 16 | printf("%d", isin(str, 'h')); 17 | } -------------------------------------------------------------------------------- /testing/islower/islower.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int islower(char *str){ 5 | int 6 | } 7 | 8 | int main(void){ 9 | 10 | } 11 | -------------------------------------------------------------------------------- /testing/islowercase/islowercase.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | 7 | 8 | /* 9 | 10 | This approach doesn't work at all 11 | 12 | int str_islower(char* str){ 13 | int total = 0; 14 | int whitespaces = 0; 15 | int str_size = strlen(str); 16 | 17 | for(int i = 0; i < str_size; i++){ 18 | if(str[i] >= 'a' && str[i] <= 'z'){ 19 | total++; 20 | } 21 | else{ 22 | whitespaces++; 23 | } 24 | } 25 | 26 | if(total == str_size - whitespaces){ 27 | return 1; 28 | } 29 | 30 | return 0; 31 | }*/ 32 | 33 | 34 | int str_islower(char *str){ 35 | int total = 0; 36 | int whitespaces = 0; 37 | int str_size = strlen(str); 38 | 39 | for(int i = 0; i < str_size; i++){ 40 | if(islower(str[i])){ 41 | total++; 42 | }else if(isupper(str[i])){ 43 | return 0; 44 | }else{ 45 | whitespaces++; 46 | } 47 | } 48 | 49 | if(total = str_size - whitespaces){ 50 | return 1; 51 | } 52 | 53 | return 0; 54 | } 55 | 56 | 57 | int main(void){ 58 | char *str1 = malloc(sizeof(char) * 15); 59 | char *str2 = malloc(sizeof(char) * 15); 60 | 61 | if(!str1){ 62 | printf("Error\n"); 63 | return 1; 64 | } 65 | 66 | if(!str2){ 67 | printf("Error\n"); 68 | return 1; 69 | } 70 | 71 | strcpy(str1, "hello, there!"); 72 | strcpy(str2, "HELLO THERE!"); 73 | 74 | if(str_islower(str1) == 1){ 75 | printf("str1: True\n"); 76 | } 77 | 78 | else{ 79 | printf("str1: False\n"); 80 | } 81 | 82 | if(str_islower(str2) == 1){ 83 | printf("str2: True\n"); 84 | } 85 | 86 | else{ 87 | printf("str2: False\n"); 88 | } 89 | 90 | 91 | free(str1); 92 | free(str2); 93 | 94 | return 0; 95 | } -------------------------------------------------------------------------------- /testing/join/join.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | char *join(char *str1, char *str2){ 6 | char *str3 = (char*)malloc(sizeof(str1) + sizeof(str2)); 7 | 8 | if(!str3){ 9 | printf("\n\nERROR: join() function error\nCouldn't allocate memory\n\n"); 10 | exit(1); 11 | } 12 | 13 | strcpy(str3, str1); 14 | strcat(str3, str2); 15 | 16 | return str3; 17 | } 18 | 19 | int main(void){ 20 | char *str = "Hello "; 21 | char *str1 = "World"; 22 | 23 | char *str2 = join(str, str1); 24 | 25 | printf("%s\n", str2); 26 | } 27 | -------------------------------------------------------------------------------- /testing/justify/justify.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "../../cnpython/include/cnpython.h" 4 | #include 5 | #include 6 | #include 7 | 8 | /** 9 | * @brief Justify the given string to the right of a new string 10 | * 11 | * @param str The string to be justified 12 | * @param len The len of the fill offset and the string itself 13 | * @param fill The character to be filled 14 | * @return string The new string that right-justified the old string 15 | */ 16 | string rjust(string str, const size_t len, const char fill){ 17 | size_t strLen = strlen(str); 18 | if (len < strLen) 19 | return NULL; 20 | 21 | string ret = malloc(len + 1); 22 | 23 | if (!ret){ 24 | fprintf(stderr, "ERROR: rjust function: Can't allocating memory"); 25 | exit(1); 26 | } 27 | memset(ret, fill, len - strLen); 28 | memcpy(ret + (len - strLen), str, strLen); 29 | 30 | return ret; 31 | } 32 | 33 | 34 | /** 35 | * @brief Justify the given string to the left of a new string 36 | * 37 | * @param str The string to be justified 38 | * @param len The len of string and the fill offset 39 | * @param fill The character to be filled 40 | * @return string The new string that left-justified the old string 41 | */ 42 | string ljust(string str, const size_t len, const char fill){ 43 | size_t strLen = strlen(str); 44 | if (len < strLen) 45 | return NULL; 46 | 47 | string ret = malloc(len + 1); 48 | 49 | if (!ret){ 50 | fprintf(stderr, "ERROR: ljust function: Can't allocating memory"); 51 | exit(1); 52 | } 53 | 54 | memcpy(ret, str, strLen); 55 | memset(ret + strLen, fill, len - strLen); 56 | 57 | return ret; 58 | } 59 | 60 | 61 | /** 62 | * @brief Basically right-justified but auto fill with zero (ascii value) instead 63 | * 64 | * @param str The string to be justified 65 | * @param len The len of string and the fill offset 66 | * @return char* The new string that right-justified and fills with zero 67 | */ 68 | char* zfill(string str, size_t len){ 69 | string ret = rjust(str, len, '0'); 70 | 71 | return ret; 72 | } 73 | 74 | int main(){ 75 | string strL = "To The Left"; 76 | string strR = "To The Right"; 77 | string strZ = "Put Zero"; 78 | 79 | string strL_just = ljust(strL, 20, '-'); 80 | string strR_just = rjust(strR, 20, '-'); 81 | string strZ_just = zfill(strR, 20); 82 | 83 | printf("%s\n%s\n%s\n", strL_just, strR_just, strZ_just); 84 | 85 | return 0; 86 | } -------------------------------------------------------------------------------- /testing/len/len.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define len(arr) size_t n = sizeof(arr)/sizeof(sizeof(arr[0]);return n; 4 | 5 | /* 6 | *This is yet another function I cannot implement, PR if you got any ideas. 7 | * */ 8 | 9 | int main(void){ 10 | int arr[] = {1, 2, 3}; 11 | int n = len(arr); 12 | printf("%d", n); 13 | } 14 | -------------------------------------------------------------------------------- /testing/linked_list/linked_list.c: -------------------------------------------------------------------------------- 1 | #include "../../cnpython/include/datastructures/linkedlist.h" 2 | 3 | 4 | 5 | void ll_remove_by_value(node_t **head, int val){ 6 | node_t *temp = *head, *prev; 7 | 8 | if(temp != NULL && temp->data == val){ 9 | *head = temp->next; 10 | free(temp); 11 | } 12 | 13 | while(temp != NULL && temp->data != val){ 14 | prev = temp; 15 | temp = temp->next; 16 | } 17 | 18 | if(temp == NULL){ 19 | return; 20 | } 21 | 22 | prev->next = temp->next; 23 | 24 | free(temp); 25 | } 26 | 27 | 28 | 29 | int main(void){ 30 | 31 | node_t *head = NULL; 32 | 33 | 34 | ll_push_beg(&head, 7); 35 | ll_push_beg(&head, 2); 36 | ll_push_beg(&head, 3); 37 | ll_push_beg(&head, 8); 38 | ll_push_beg(&head, 10); 39 | 40 | print("Created linked list: "); 41 | ll_print(head); 42 | ll_remove_by_value(&head, 3); 43 | print("Linked list after deletion of '3'"); 44 | ll_print(head); 45 | 46 | return 0; 47 | } -------------------------------------------------------------------------------- /testing/list/list.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | 6 | typedef char* string; 7 | 8 | 9 | /* 10 | 11 | How about no for now 12 | 13 | struct List 14 | { 15 | int type; 16 | 17 | string *str; 18 | int strlen; 19 | 20 | double *num; 21 | int doublelen; 22 | 23 | }; 24 | */ 25 | 26 | int main(void) 27 | { 28 | 29 | } -------------------------------------------------------------------------------- /testing/oct/oct.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | long oct(long dec){ 4 | long octalNumber = 0; 5 | int i = 1; 6 | 7 | while (dec != 0){ 8 | octalNumber += (dec % 8) * i; 9 | dec /= 8; 10 | i *= 10; 11 | } 12 | 13 | return octalNumber; 14 | } 15 | 16 | int main(void){ 17 | printf("%ld", oct(78)); 18 | } 19 | -------------------------------------------------------------------------------- /testing/open/open.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | /* 6 | *This ones scraped cuz just now i realized that fopen does the same exact thing. 7 | I have brain damage. 8 | * */ 9 | 10 | FILE* open(char *filename, char mode[2]){ 11 | FILE *fptr; 12 | fptr = fopen(filename, mode); 13 | 14 | if(!fptr){ 15 | printf("\n\nERROR: open()\nCouldn't open file\n\n"); 16 | exit(1); 17 | } 18 | 19 | return fptr; 20 | } 21 | 22 | int main(void){ 23 | FILE* fptr = open("test.txt", "r"); 24 | } 25 | -------------------------------------------------------------------------------- /testing/ord/ord.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "../../src/cnpython.h" 3 | 4 | int main(void){ 5 | print("<%d>", ord('A')); 6 | } 7 | -------------------------------------------------------------------------------- /testing/pop/pop.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void pop(int *arr, int size){ 4 | arr[size-1] = 0; 5 | } 6 | 7 | int main(void){ 8 | int arr[] = {1, 2}; 9 | pop(arr, 2); 10 | 11 | for(int i = 0; i < 2; i++){ 12 | printf("%d\n", arr[i]); 13 | } 14 | } -------------------------------------------------------------------------------- /testing/pow/pow.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | double pow(double base, double exponent){ 4 | double result = 1; 5 | for(exponent; exponent > 0; exponent--){ 6 | result *= base; 7 | } 8 | 9 | return result; 10 | } 11 | 12 | int main(void){ 13 | 14 | printf("%f", pow(2, 3)); 15 | } 16 | -------------------------------------------------------------------------------- /testing/print/print.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | /* 6 | *The print function is very tricky for me and I couldn't find the actual code for this function so I have to do this myself 7 | * */ 8 | 9 | 10 | //PROTOTYPES 11 | void print_int(int int_to_print); 12 | void print_int_arr(int *arr, int size); 13 | void print_double_arr(double *arr, int size); 14 | void print_long_arr(long *arr, int size); 15 | void print(char *format, ...); 16 | 17 | 18 | void print_int(int int_to_print){ 19 | int a = int_to_print; 20 | 21 | if (int_to_print < 0){ 22 | putchar('-'); 23 | int_to_print = -int_to_print; 24 | } 25 | 26 | if (int_to_print > 9) print_int(int_to_print/10); 27 | 28 | putchar('0'+ (a%10)); 29 | } 30 | 31 | 32 | void print_int_arr(int *arr, int size){ 33 | for(int i = 0; i < size; i++){ 34 | printf("[%d], ", arr[i]); 35 | } 36 | } 37 | 38 | 39 | void print_double_arr(double *arr, int size){ 40 | for(int i = 0; i < size; i++){ 41 | printf("[%f], ", arr[i]); 42 | } 43 | } 44 | 45 | 46 | void print_long_arr(long *arr, int size){ 47 | for(int i = 0; i < size; i++){ 48 | printf("[%ld], ", arr[i]); 49 | } 50 | } 51 | 52 | 53 | void print(char *format, ...){ 54 | va_list argp; 55 | va_start(argp, format); 56 | while (*format != '\0') { 57 | if (*format == '%') { 58 | format++; 59 | if (*format == '%') { 60 | putchar('%'); 61 | } else if (*format == 'c') { //this is for a character 62 | char char_to_print = va_arg(argp, int); 63 | putchar(char_to_print); 64 | }else if(*format == 's'){ //this is for a string 65 | char* string_to_print = va_arg(argp, char*); 66 | for(int i = 0; i < strlen(string_to_print); i++){ 67 | putchar(string_to_print[i]); 68 | } 69 | }else if(*format == 'd'){ //this is for an integer 70 | int int_to_print = va_arg(argp, int); 71 | print_int(int_to_print); 72 | }else if(*format == 't'){ //this is for an integer array 73 | int *int_arr_to_print = va_arg(argp, int*); 74 | int size = sizeof(int_arr_to_print)/sizeof(int_arr_to_print[0]); 75 | print_int_arr(int_arr_to_print, size); 76 | }else if(*format == 'e'){ 77 | double *double_arr_to_print = va_arg(argp, double*); 78 | int size = sizeof(double_arr_to_print)/sizeof(double_arr_to_print[0]); 79 | print_double_arr(double_arr_to_print, size); 80 | }else if(*format == 'g'){ //for long arrays 81 | long *long_arr_to_print = va_arg(argp, long*); 82 | int size = sizeof(long_arr_to_print)/sizeof(long_arr_to_print[0]); 83 | print_long_arr(long_arr_to_print, size); 84 | } 85 | 86 | 87 | else { 88 | fputs("Not implemented", stdout); 89 | } 90 | 91 | } else { 92 | putchar(*format); 93 | } 94 | format++; 95 | } 96 | va_end(argp); 97 | } 98 | 99 | 100 | -------------------------------------------------------------------------------- /testing/range/range.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "../../cnpython.h" 3 | 4 | 5 | /*This doesn't work lmao, PR if you can fix it pls*/ 6 | int* range(int start, int stop, int step){ 7 | int *arr = calloc(stop, sizeof(int)); 8 | for(int i = start; i < stop; i+=step){ 9 | arr[i] = i; 10 | } 11 | 12 | return arr; 13 | } 14 | 15 | int main(void){ 16 | print("%t", range(1, 10, 1)); 17 | } 18 | -------------------------------------------------------------------------------- /testing/read_file/read.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | char *read_file(char *filename){ 5 | FILE *fptr = fopen(filename, "rb"); 6 | 7 | if(!fptr){ 8 | printf("\n\nERROR: read_file() function error\nCouldn't open file\n\n"); 9 | exit(1); 10 | } 11 | 12 | fseek(fptr, 0, SEEK_END); 13 | long fptr_size = ftell(fptr); 14 | fseek(fptr, 0, SEEK_SET); 15 | 16 | char *buffer = malloc(fptr_size + 1); 17 | fread(buffer, 1, fptr_size, fptr); 18 | fclose(fptr); 19 | 20 | buffer[fptr_size] = 0; 21 | 22 | return buffer; 23 | } 24 | 25 | 26 | int main(void){ 27 | printf("%s", read_file("test.txt")); 28 | } 29 | -------------------------------------------------------------------------------- /testing/read_file/test.txt: -------------------------------------------------------------------------------- 1 | test lmao 2 | this should work 3 | please work 4 | -------------------------------------------------------------------------------- /testing/replace/replace.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | char *replace(char *str, char find, char repl){ 6 | char *ret_string = (char*)malloc(sizeof(str)); 7 | 8 | if(!ret_string){ 9 | printf("\nERROR: replace() function error\nCouldn't allocate memory\n\n"); 10 | exit(1); 11 | } 12 | 13 | strcpy(ret_string, str); 14 | 15 | 16 | for(int i = 0; i < strlen(ret_string); i++){ 17 | if(ret_string[i] == find){ 18 | ret_string[i] = repl; 19 | } 20 | } 21 | 22 | return ret_string; 23 | } 24 | 25 | 26 | 27 | int main(void){ 28 | char *str = "hello worls"; 29 | str = replace(str, 's', 'd'); 30 | 31 | printf("%s", str); 32 | } 33 | -------------------------------------------------------------------------------- /testing/reversed/reversed.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "../../cnpython.h" 3 | 4 | int* reversed(int* arr, int size){ 5 | int *return_arr = calloc(size, sizeof(int)); 6 | for(int c = size - 1, d = 0; c >= 0; c--, d++){ 7 | return_arr[d] = arr[c]; 8 | } 9 | 10 | return return_arr; 11 | 12 | } 13 | 14 | int main(void){ 15 | int arr[] = {1, 2}; 16 | print("%t", reversed(arr, 2)); 17 | } 18 | -------------------------------------------------------------------------------- /testing/round/round.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | 4 | double round(double number, int ndigits){ 5 | 6 | } 7 | 8 | 9 | int main(void){ 10 | 11 | } -------------------------------------------------------------------------------- /testing/split/split.c: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | #include "../../src/cnpython.h" 4 | #include 5 | 6 | size_t str_split(char ***array, char *str, const char *del) { 7 | char *token; 8 | size_t i = 0; 9 | 10 | token = strtok(str, del); 11 | 12 | while(token != NULL) { 13 | *array = realloc(*array, sizeof(char *) * (i + 1)); 14 | (*array)[i++] = token; 15 | token = strtok(NULL, del); 16 | } 17 | 18 | return i; 19 | } 20 | 21 | 22 | 23 | void printSplit(char *str, const char *del){ 24 | char **split = NULL; 25 | size_t count = str_split(&split, str, del); 26 | 27 | for(size_t i = 0; i < count; i++) { 28 | printf("[%s], ", split[i]); 29 | } 30 | 31 | free(split); 32 | } 33 | 34 | 35 | 36 | char **split(char **array, char *str, const char *del){ 37 | char *token; 38 | size_t i = 0; 39 | 40 | token = strtok(str, del); 41 | 42 | while(token != NULL) { 43 | *array = realloc(*array, sizeof(char *) * (i + 1)); 44 | (*array)[i++] = token; 45 | token = strtok(NULL, del); 46 | } 47 | 48 | return array; 49 | } 50 | 51 | 52 | int main(void){ 53 | char *str = "this-is-cool"; 54 | char *arr[3]; 55 | 56 | printSplit(str, "-"); 57 | } 58 | -------------------------------------------------------------------------------- /testing/startswith/starts.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int startswith(char *str, char element){ 4 | if(str[0] == element){ 5 | return 0; 6 | } 7 | return -1; 8 | } 9 | 10 | int main(void){ 11 | char *str = "Hello world"; 12 | 13 | printf("%d", startswith(str, 'H')); 14 | 15 | } 16 | -------------------------------------------------------------------------------- /testing/str/str.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | char *_str(int number){ 7 | int size = (int)((ceil(log10(number))+1)*sizeof(char)); 8 | char *buffer = (char*)malloc(sizeof(size)); 9 | 10 | sprintf(buffer, "%d", number); 11 | 12 | return buffer; 13 | 14 | } 15 | 16 | 17 | int main(void){ 18 | int number = 200; 19 | printf("%s", _str(number)); 20 | } 21 | -------------------------------------------------------------------------------- /testing/str_check/str_check.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "../../cnpython/include/cnpython.h" 4 | #include 5 | #include 6 | #include 7 | 8 | //Some more checking method for string 9 | 10 | bool isspace_str(string str){ 11 | bool ret = true; 12 | size_t i = 0; 13 | 14 | while (str[i] != '\0'){ 15 | if (str[i] != ' ') 16 | ret = false; 17 | i++; 18 | } 19 | 20 | return ret; 21 | } 22 | 23 | bool isprintable_str(string str){ 24 | size_t strLen = strlen(str); 25 | bool ret = true; 26 | 27 | for (int i = 0; i < strLen; i++) 28 | if (!isprint(str[i])) 29 | ret = false; 30 | 31 | return ret; 32 | } 33 | 34 | bool isdigit_str(string str){ 35 | size_t strLen = strlen(str); 36 | bool ret = true; 37 | 38 | for (int i = 0; i < strLen; i++) 39 | if (!isdigit(str[i])) 40 | ret = false; 41 | 42 | return ret; 43 | } 44 | 45 | bool isidentifier_str(string str){ 46 | size_t strLen = strlen(str); 47 | bool ret = true; 48 | 49 | if (isdigit(str[0])) 50 | return false; 51 | else{ 52 | for (int i = 0; i < strLen; i++){ 53 | if (str[i] == ' ') 54 | return false; 55 | 56 | if (!isalnum(str[i]) && str[i] != '_') 57 | ret = false; 58 | } 59 | } 60 | return ret; 61 | } 62 | 63 | int main(){ 64 | bool space_check = isspace_str(" "); 65 | bool printable_check = isprintable_str("AllIsPrintable"); 66 | bool digit_check = isdigit_str("09084823217"); 67 | bool identifier_check = isidentifier_str("THIS2_"); 68 | 69 | printf("%s\n%s\n%s\n%s\n", space_check ? "True" : "False", printable_check ? "True" : "False", digit_check ? "True" : "False", identifier_check ? "True" : "False" ); 70 | return 0; 71 | } -------------------------------------------------------------------------------- /testing/strip/strip.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "../../cnpython/include/cnpython.h" 4 | #include 5 | #include 6 | #include 7 | 8 | /** 9 | * @brief Trim of the heading and the trailing of any character that met inside the strip_content 10 | * 11 | * @param str The string to be trimmed 12 | * @param strip_content The delimiter for trimming 13 | * @return string The new string that got trimmed the heading and the trailing 14 | */ 15 | string strip(string str, string strip_content){ 16 | size_t start = 0; 17 | size_t strLen = strlen(str); 18 | size_t end = strLen - 1; 19 | string ret = calloc(strLen, 1); 20 | int i; 21 | 22 | //Trim the leading 23 | while (strchr(strip_content, str[end])) 24 | end--; 25 | 26 | //Trim the trailing 27 | while (strchr(strip_content, str[start])) 28 | start++; 29 | 30 | //Start copying data 31 | for (i = 0; i <= end - start; i++) 32 | ret[i] = str[i + start]; 33 | 34 | ret[i] = '\0'; 35 | 36 | ret = realloc(ret, strlen(ret) + 1); 37 | 38 | return ret; 39 | } 40 | 41 | int main(){ 42 | string str = "......,..:::,.!^^Trimmed all but this . (dot) right here..,...^^,,,::."; 43 | 44 | string trimmed_str = strip(str, ".,:!^"); 45 | 46 | printf("%s\n", trimmed_str); 47 | return 0; 48 | } -------------------------------------------------------------------------------- /testing/sum/sum.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | long sum(int *arr, int size){ 4 | long sum = 0; 5 | 6 | for(int i = 0; i < size; i++){ 7 | sum += arr[i]; 8 | } 9 | 10 | return sum; 11 | } 12 | 13 | int main(void){ 14 | int arr[] = {1, 2}; 15 | printf("%ld", sum(arr, 2)); 16 | } 17 | -------------------------------------------------------------------------------- /testing/title/title.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "../../cnpython/include/cnpython.h" 4 | #include 5 | #include 6 | #include 7 | 8 | 9 | /** 10 | * @brief Check whether the string is of title type (First character of all word must be uppercase) 11 | * 12 | * @param str The string to be checked 13 | * @return true When the string obeys the rule, false when the string not obeys the rule or only contains number 14 | */ 15 | bool istitle(string str){ 16 | bool ret = true; 17 | bool char_flag = false; //Check the case where there's no character and only number 18 | size_t len = strlen(str); 19 | size_t offsetS = 0; 20 | size_t offsetE = 0; 21 | string token; 22 | 23 | while (offsetE < len){ 24 | while (isalpha(str[offsetE])){ 25 | offsetE++; 26 | char_flag = true; 27 | } 28 | 29 | // Reach here when the delimiter was found 30 | for (int i = 0; i < offsetE - offsetS; i++){ 31 | if (isalpha((str + offsetS)[i])){ 32 | if (i > 0){ 33 | if (!islower((str + offsetS)[i])) 34 | return false; 35 | } 36 | else{ 37 | if (!isupper((str + offsetS)[i])) 38 | return false; 39 | } 40 | } 41 | } 42 | 43 | while (!isalpha(str[offsetE])) 44 | offsetE++; 45 | 46 | offsetS = offsetE; 47 | } 48 | 49 | return char_flag ? ret : false; 50 | } 51 | 52 | /** 53 | * @brief Turn the string to become title 54 | * 55 | * @param str The string to be transform 56 | * @return string The new string that is obeys title form 57 | */ 58 | string title(string str){ 59 | size_t len = strlen(str); 60 | size_t i = 0; 61 | string ret = malloc(len + 1); 62 | 63 | if (!ret){ 64 | fprintf(stderr, "ERROR: title function: Can't allocating memory"); 65 | exit(1); 66 | } 67 | strncpy(ret, str, len); 68 | 69 | while (i < len){ 70 | //Cycle through the non-alpha character 71 | while (!isalpha(str[i])){ 72 | i++; 73 | 74 | if (i >= len) 75 | goto CHECK_DONE; 76 | } 77 | 78 | //And upper case the first one 79 | ret[i] = toupper(ret[i]); 80 | 81 | //Then cycle through until meeting a non-alpha again 82 | while (isalpha(str[i])){ 83 | i++; 84 | 85 | if (i >= len) 86 | goto CHECK_DONE; 87 | } 88 | i++; 89 | } 90 | 91 | 92 | CHECK_DONE: 93 | return ret; 94 | } 95 | 96 | int main(){ 97 | string str = "hello b2b2b2 and 3g3g3g"; 98 | 99 | string title_str = title(str); 100 | 101 | bool is_title_form = istitle(title_str); 102 | 103 | printf("%s\n", is_title_form ? "True" : "False"); 104 | 105 | return 0; 106 | } -------------------------------------------------------------------------------- /testing/tuples/tuple.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | 7 | struct Tuple 8 | { 9 | void *values; 10 | }; 11 | 12 | 13 | extern const struct Tuple new_Tuple(void *value, ...) 14 | { 15 | 16 | } 17 | 18 | 19 | extern void del_Tuple(struct Tuple *tuple) 20 | { 21 | free(tuple->values); 22 | free(tuple); 23 | 24 | } 25 | 26 | 27 | 28 | int main(void){ 29 | 30 | } -------------------------------------------------------------------------------- /testing/type/type.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | enum types{ 4 | TYPE_INT = 0, 5 | TYPE_CHAR = 1, 6 | TYPE_STRING = 2, 7 | TYPE_LONG = 3, 8 | TYPE_DOUBLE = 4, 9 | TYPE_FLOAT = 5, 10 | TYPE_INT_ARR = 6, 11 | TYPE_STRING_ARR = 7, 12 | TYPE_LONG_ARR = 8, 13 | TYPE_DOUBLE_ARR = 9, 14 | TYPE_FLOAT_ARR = 10, 15 | TYPE_UNKNOWN = -1 16 | }; 17 | 18 | #define type(X) \ 19 | _Generic(X, \ 20 | int: TYPE_INT, \ 21 | char: TYPE_CHAR, \ 22 | char*: TYPE_STRING, \ 23 | char**: TYPE_STRING_ARR, \ 24 | long: TYPE_LONG, \ 25 | double: TYPE_DOUBLE, \ 26 | float: TYPE_FLOAT, \ 27 | int*: TYPE_INT_ARR, \ 28 | long*: TYPE_LONG_ARR, \ 29 | double*: TYPE_DOUBLE_ARR, \ 30 | float*: TYPE_FLOAT_ARR, \ 31 | default: TYPE_UNKNOWN ) 32 | 33 | 34 | 35 | int main(void){ 36 | printf("%d", type(2)); 37 | } 38 | -------------------------------------------------------------------------------- /testing/upper/upper.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include "../../cnpython/include/cnpython.h" 6 | 7 | /* 8 | Worked now :))) 9 | */ 10 | 11 | string upper(string str){ 12 | size_t strLen = strlen(str); 13 | string upperStr = malloc(strLen); 14 | //Or: char upperStr[strLen]; 15 | 16 | strcpy(upperStr, str); 17 | 18 | for (size_t i = 0; i < strLen; i++) 19 | upperStr[i] = toupper(upperStr[i]); 20 | 21 | return upperStr; 22 | } 23 | 24 | int main(void){ 25 | string str = "hello world"; 26 | printf("%s", upper(str)); 27 | } 28 | -------------------------------------------------------------------------------- /testing/var/var.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "../../cnpython/headers/cnpython.h" 4 | 5 | typedef void* var; 6 | 7 | extern var Int; 8 | extern var String; 9 | extern var Double; 10 | extern var Float; 11 | extern var Char; 12 | extern var Long; 13 | extern var Int_Arr; 14 | extern var Function; 15 | 16 | struct Int{ 17 | int64_t val; 18 | }; 19 | 20 | struct String{ 21 | char *val; 22 | }; 23 | 24 | struct Double{ 25 | double val; 26 | }; 27 | 28 | struct Float{ 29 | float val; 30 | }; 31 | 32 | struct Char{ 33 | char val; 34 | }; 35 | 36 | struct Long{ 37 | long val; 38 | }; 39 | 40 | 41 | struct Function{ 42 | var (*func)(var); 43 | }; 44 | 45 | var hello(){ 46 | print("Hello world"); 47 | } 48 | 49 | /* 50 | Doesn't really work 51 | */ 52 | var add(var a, var b){ 53 | var result = a + b; 54 | return result; 55 | } 56 | 57 | int main(void){ 58 | print("%d", add(1, 2)); 59 | } 60 | -------------------------------------------------------------------------------- /tutorials/functions/str_islower/Makefile: -------------------------------------------------------------------------------- 1 | CC = gcc 2 | SRC = ${wildcard ../../../cnpython/*.c} 3 | CLAGS = -lm 4 | BUILD = ./str_islower.o 5 | 6 | all ./helloWorld.c: 7 | ${CC} str_islower.c ${SRC} ${CLAGS} -o ${BUILD} -------------------------------------------------------------------------------- /tutorials/functions/str_islower/str_islower.c: -------------------------------------------------------------------------------- 1 | #include "../../../cnpython/headers/cnpython.h" 2 | 3 | 4 | int main(void){ 5 | //Allocates memory on the heap 6 | string str = new_String("hello, world!"); 7 | 8 | print(str); 9 | 10 | if(str_islower(str) == True){ 11 | print("Variable 'str' is all lowercase"); 12 | } 13 | else{ 14 | print("Variable 'str' isn't all lowercase"); 15 | } 16 | 17 | return 0; 18 | } -------------------------------------------------------------------------------- /tutorials/helloWorld/Makefile: -------------------------------------------------------------------------------- 1 | CC = gcc 2 | SRC = ${wildcard ../../cnpython/*.c} 3 | #Necessary -lm flag for the library to work 4 | CLAGS = -lm 5 | BUILD = ./helloWorld.o 6 | 7 | all ./helloWorld.c: 8 | ${CC} ./helloWorld.c ${SRC} ${CLAGS} -o ${BUILD} -------------------------------------------------------------------------------- /tutorials/helloWorld/helloWorld.c: -------------------------------------------------------------------------------- 1 | //includes the cnpython.h header file which includes all of the other necessary header files 2 | 3 | #include "../../cnpython/headers/cnpython.h" 4 | 5 | 6 | int main(void){ 7 | 8 | //uses the cnpython.h print() function to say hello world 9 | print("Hello, world!"); 10 | 11 | return 0; 12 | } -------------------------------------------------------------------------------- /tutorials/variables/Makefile: -------------------------------------------------------------------------------- 1 | CC = gcc 2 | SRC = ${wildcard ../../cnpython/*.c} 3 | CFLAGS = -lm 4 | BUILD = ./variables.o 5 | 6 | all ./variables.c: 7 | ${CC} ./variables.c ${SRC} ${CFLAGS} -o ${BUILD} -------------------------------------------------------------------------------- /tutorials/variables/variables.c: -------------------------------------------------------------------------------- 1 | #include "../../cnpython/headers/cnpython.h" 2 | 3 | 4 | 5 | int main() 6 | { 7 | string str = "Hello, world!"; 8 | 9 | print(str); 10 | 11 | return 0; 12 | } 13 | --------------------------------------------------------------------------------