Browse Source

Updates again in chapter 4, on languages...

master
Ognjen Milan Robovic 5 months ago
parent
commit
ca94a621cf
3 changed files with 89 additions and 2 deletions
  1. +3
    -0
      chapter/chapter_3.h
  2. +74
    -2
      chapter/chapter_4.h
  3. +12
    -0
      xhartae.c

+ 3
- 0
chapter/chapter_3.h View File

@@ -167,6 +167,9 @@ splint -weak my_c_source_code.c

valgrind --show-leak-kinds=all --leak-check=full my_c_program
@

Then again, even if everything compiles nicely, maybe you made a mistake in your algorithm. How to solve that? Well, the answer is same as to question "How to live forever?", it's
"Just don't die."... Know what you're doing, and when you're doing it, and you'll be good to go, otherwise, use "printf" to debug it.
*/

extern void print ( char * format, ...); // Notice the "...", which means it can accept any amount of arguments after that 'format'.


+ 74
- 2
chapter/chapter_4.h View File

@@ -21,9 +21,9 @@ I believe that this chapter should be a breakpoint for you to write a simple C p
- 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.
- 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.
- chapter 3: To use standard library 'printf' function, and to implement variadic argument functions, while also covering functions and 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
From this moment onwards, some chapters will have few functions called 'program_*', which we can use to build 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.
@@ -84,6 +84,78 @@ This is how we parse it, determine where some syntax rule begins, where it ends,
^ - if it is, we return index of that rule (with function 'syntax_select') and length is 3, without changing the offset.

And we repeat that process until we reach the end of the string, that null termination, it's not a fast algorithm, but it's simple. Easy.

With all that said, learning C is great and all, but lets see some other programming languages, some minimal program in few of them, hello world for example.

@C
// I didn't use preprocessor in this example or real C comments with asterix.
extern int puts (const char *);

int main (void) {
puts ("Hello world!");

return (0);
}
@

@Ada
-- Also, 'use' keyword would apply the namespace, more on that later.
with ada.text_io;

procedure hello_world is
begin
ada.text_io.put_line ("Hello world!");
end hello_world;
@

@C++
// This is C++23 standard, they like new stuff anyway, but it can look more like C if you want.
import module std;

int main () {
std::print ("Hello world!\n");
}
@

@HolyC
// Masterpiece.
"Hello world!\n";
@

@Flat
; This will run only on 64-bit GNU/Linux OS without issues.
format ELF64 executable 3

segment readable executable
mov rax, 1
mov rdi, 1
mov rsi, string
mov rdx, [length]
syscall
mov rax, 60
mov rdi, 0
syscall

segment readable writable
string db 'Hello world!', 10, 0
length dq $-string
@

@Fortran
! Fortran is great programming language, but it can be autistic at times.

program hello_world
print *, 'Hello world!'
end program hello_world
@

@Python
# Well, they call it "second best language for everything" for some reason...

print ("Hello world!")
@

Learning C well will make learning those other programming languages, except assembly languages, a walk in the park.
*/

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.


+ 12
- 0
xhartae.c View File

@@ -121,6 +121,18 @@ One sane C program should have the following structure (please keep in mind that
7) External function then variable definition.
8) Main function.
9) You can also define functions here if you want to.

I can't bother to explain what's a compilation process right now, essentially, you want to translate one array of bytes into another array of bytes. You know what's a terminal,
file manager, file, folder, and you can find out (in the world wide web) how to navigate in your terminal using 'cd', 'ls' and other commands (aka programs). I'll assume that you
have opened terminal in your working directory, that your wrote some C code in your "main.c" file and that you want to compile it. In order to compile you source code into an
executable program, these are the steps:

@Shell
gcc -o program main.c # Compile your source code.
./program # Run your program.
@

That's enough to know for now, feel free to continue reading and enjoy writing.
*/

int main (int argc, char * * argv) {


Loading…
Cancel
Save