2023-11-13 21:03:29 -05:00
# ifndef CHAPTER_4_HEADER
# define CHAPTER_4_HEADER
# include "chapter_0.h"
# include "chapter_1.h"
# include "chapter_2.h"
# include "chapter_3.h"
/*
2023-12-03 07:19:51 -05:00
In this chapter , you ' ll learn about :
2023-12-03 11:05:54 -05:00
- Programming languages
2023-12-03 07:19:51 -05:00
- Syntax highlighting
- Importance of readability
- Differences between languages
- More on memory management
- Using curses
2023-11-13 21:03:29 -05:00
I believe that this chapter should be a breakpoint for you to write a simple C program . So far , we ' ve learned in :
2023-11-16 10:13:23 -05:00
- chapter 0 : To format our code properly in order to increase readability and we ' ve implemented some core functions for memory management , strings and input / output .
2023-11-13 21:03:29 -05:00
- chapter 1 : To declare and define functions , and we ' ve covered character and file descriptor related functions , as well as ASCII table and discussed C keywords .
- chapter 2 : To use external variables , function pointers and minor part of ' libncurses ' reimplementation that doesn ' t care about portability .
- chapter 3 : To use standard library ' printf ' function , and to implement variadic argument functions , while also covering switch statement in more depth .
From this moment onwards , some chapters will have few functions called ' program_ * ' , which we can use to forge even larger programs . They ' ll each have their own dependencies , for
example , some of them will require functions from some or all previous chapter source and header files , but I ' ll make sure not to use in them functions that ' ll be in future
chapters . Instead of that , we ' ll ( re ) implement newer stuff with different approach if necessary . That way , you can be sure that if you ' re reading chapter four , for example , it ' ll
only use functions and variables defined in chapters zero to three . Lets begin .
2023-12-03 07:19:51 -05:00
I ' ll write this huge ' program_curses_view_file ' function in somewhat procedural style of programming , so to say , and in the next chapter , we ' ll use more modular way , using many more
2023-12-02 07:57:17 -05:00
functions . Learning anything , including the C programming language , is like a journey . Maybe you think it won ' t last long , and it ends up being quite long journey , or maybe you
think it ' ll be very long , that you ' ll walk miles and miles , and it ends up being short ( you rage - quit ) . The final destination you ' re going towards always depends on where you
left - off and where you ' re coming from . For example , if you wrote Ada , you ' ll like chapter four , if you wrote C + + , you ' ll like chapter five .
I ' ll also list a few " traps " right here , where most programmers get caught in :
- My program needs to be cross - platform , fully portable , to run on Windblows , Machos , Leenoocks , Raspberries and on Commodore 64.
- My program needs to be huge , multiple files and folders , everything is abstracted out , even the wrappers for some library .
- My program doesn ' t need to free used memory , my operating system will do it for me , I don ' t care about memory leaks , only nerds do .
- My compiler warns about stupid things , I don ' t want to fix all compiler warnings , it ' ll make the code look bad .
First of all , there are a lot of standards , people who don ' t have more important work to do make those . There are a lot of CPU architectures , x86 - 64 being used a lot , then ISA
( instruction set architecture ) such as CISC , RISC , MISC , OISC , and even more things that should ' t matter for you like SIMD , AVX , ST , MMX , XMM , YMM , ZMM , et fucking cetera . Then ,
we have many many GPU hardware , they each have some part of their own ISA , writing direct code for one GPU won ' t work on other GPUs , so we need to use OpenGL , Vulkan or Direct3D .
2023-12-03 07:19:51 -05:00
Do you see where this is going , adding complexity on top of complexity , abstracting the abstractions , due to standardization . Then again , we have many programming languages , some
of them have multiple standards , like C , C + + , Ada , Fortran , basically , popular programming languages . For some of those languages , there are multiple compilers , and sometimes they
support language extensions that aren ' t the part of the core language . That ' s why nothing is truly portable .
2023-12-02 07:57:17 -05:00
If every company make their own standard , thinking they ' re smartest , there ' s no standardization . Just look at the mirror , at your PC , laptop , whatever , then take a look outside
the window , and say out loud " My program will be written in C, it will run on 64-bit CPUs, it will depend only on Vulkan API, it will use XCB for display. " . Take a deep breath ,
you ' re not writing some part of the program for the company , you ' re having fun , you ' re sane . Then again , pray to Khronos , your OS maintainers or developers and your GPU vendor
that your GPU supports Vulkan , that someone there , out in the big white world wrote a driver for it .
Keep in mind that I don ' t work for any programming related company and I want to , also I don ' t have college , I learned C by writing it a lot and reading it from time to time . Now ,
hear me out , if 1000 people with some CS degree wrote a simple C program , all of those would look very similar . That ' s because they ' ve been programmed into that line of thinking ,
which is dangerous in my opinion for one reason : They think they ' re always right . I learned a lot from talking with smart people , some of them have CS degree , some not , so I don ' t
own what I know , no one owns anyones ' knowledge , but they weren ' t always right . So , if you don ' t have a CS degree , you can learn C easier , that ' s my point .
2023-12-03 07:19:51 -05:00
C is good programming language to learn first because it shows you what can you create with very simple tools . Now , lets get to syntax highlighting :
2023-11-13 21:03:29 -05:00
*/
2023-12-03 07:19:51 -05:00
extern int syntax_define ( int enrange , int derange , char * begin , char * end , char escape , int colour , int effect ) ; // This must be called before 'syntax_select' function.
extern int syntax_select ( char * string , int * length ) ; // And we're not dealing with null-terminated strings here.
2023-12-02 07:06:26 -05:00
2023-12-03 07:19:51 -05:00
// Internally use 'syntax_define' to make C (and Ada below) syntax highlighting, it will use global variables internal to 'chapter_4.c' file.
// Worth noting:
// - We'll only have one "predefined syntax highlighting" per file type, because we don't want them to clash with each other.
// - We could use several syntax highlighting rules if we made one more level of arrays for all those global variables, and a rule to switch between them.
// - If you want to add support for some other language, you can try to make, for example 'syntax_highlight_python', it'll be a good exercise.
2023-12-02 07:06:26 -05:00
extern void syntax_highlight_c ( void ) ;
extern void syntax_highlight_ada ( void ) ;
2023-12-03 07:19:51 -05:00
extern void program_curses_view_file ( char * text_file , int x , int y ) ; // This is our subprogram that'll view some textual file in terminal, using our curses "library".
2023-11-13 21:03:29 -05:00
# endif