2 single: General Information; Introduction
8 This chapter contains general information about the Ring programming language
12 (3) Data Representation
15 pair: General Information; Ring Architecture
20 We have the next architecture
22 (1) Ring Applications (Your Code) - Written in Ring - See folder : ring/applications
23 (2) Ring Libraries (StdLib, WebLib, GameEngine, etc) - Written in Ring - See folder : ring/ringlibs
24 (3) Ring Extensions (RingAllegro, RingQt, etc) - Written in C/C++ (may include Ring code) - See folder : ring/extensions
25 (4) Ring Virtual Machine (Ring VM) - Written in C language
26 (5) Operating System (Current Platform) - (Windows, Linux, macOS, Android, etc)
28 The extensions are just dynamic libraries (DLL, So, Dylib)
29 You can update the extensions without the need to update your code.
32 Folder (ring\libdepwin) ====> C libraries used for building Ring Extensions (written in C) on Windows platform.
34 Folder (ring\ringlibs) ====> Ring libraries written in Ring itself (StdLib, WebLib, GameEngine, etc)
36 Folder (ring\rnoteexe) ====> Just C source file to create executable file that will run rnote.ring
38 Folder (ring\visualsrc) ====> The Visual Source Code of the Ring Compiler & Ring VM developed using Programming Without Coding Technology (PWCT)
40 We use the term Ring Library ---> When the library code is written in Ring
41 We use the term Ring Extension ---> When the library code is Written in C or C++
44 pair: General Information; Memory Management
49 (1) When we call function, we get a new scope for this function, inside this scope we store variables.
51 Also we get Temp. memory for this function. Inside this temp. memory we store temp. lists
53 All of this will be deleted directly after the end of the function call (end of scope)
55 (2) In Ring to delete memory, you have the next options
57 2.1 Wait until the end of the function scope
59 2.2 Use the Assignment operator
61 2.3 Use callgc() function (delete temp. lists only)
63 (3) Ring Garbage Collector uses Escape Analysis and Reference Counting
65 In 90% of cases, the Escape Analysis is used so we don't waste time in running the garbage collector
66 We directly know what will be deleted and what will remain in the memory.
68 https://en.wikipedia.org/wiki/Escape_analysis
70 In 10% of cases (or less than that) Ring may use Reference Counting.
72 For example when we pass a list and sub list to a function
73 Ring will pass the lists by reference, but what will happens if we deleted the Parent List?
74 In this case, the Sub List will use reference counting, and when deleting the Parent List, it will stay in memory until the end of the function.
76 Remember that Ring encourage us to avoid using references, and the
77 Assignment Operator will copy lists by value, so Ring usage of reference counting is
78 very limited to special cases and in most cases the Escape Analysis is enough which is very fast.
80 Starting from Ring 1.9 we extended the Reference Counting support to Ring Extensions and low level C pointers.
81 So we don't have to care about using fclose() when we use fopen() for example. and the same for
82 other extensions like RingODBC, RingSQLite, RingMySQL, RingQt, etc.
84 All of the allocated resources will be cleaned by the Garbage Collector when we finish using it (when we lost the last reference).
88 pair: General Information; Data Representation
93 (1) In Ring, The String is just (Array of bytes)
95 Ring is 8-bit clean, Each character in the string is 8 bits (1 byte)
97 So these functions (Int2Bytes(), Float2Bytes() and Double2Bytes()) just return a string.
99 Also we can store binary data in strings
103 mystring = read("myfile.exe")
105 (2) Remember this, when you think about variables
107 * Value ---> What we have (What we are storing in the computer memory as data) - Low Level Concept
108 * Type ---> What we can do with what we have or how we do things with what we have (Just a Logical Concept)
110 Computer memory ----> Just store [Bytes] - Each byte is 8-bit - (Here we avoid the memory word concept)
112 These bytes could be grouped together when moved between the memory and the processor registers.
113 Here we have (The register size) and things like 32-bit and 64-bit for example.
114 Also we have the bytes order.
116 Programming Languages ----> Add Types (Just as a concept) to these bytes so we can determine what to do with them and how operations should be done.
118 And programming language could allow (Type Conversion) ---> Because the Type is a logical concept in most cases, What we really have is just data (Bytes, Bytes Count, Bytes Order, etc)
120 Ring Stings ----> You have these bytes (each byte is 8-bit) and Ring know the string size (stored as number in the String structure)
122 So we don't check the NULL character or add it to the end of the string (Not Required)
124 All operations inside Ring VM, will check the Ring size and deal with the string as binary data (each character is 8-bit)
126 In the C language ---> The normal case is adding NULL character (\0) to the end of each string
128 And the string functions check this character, This is not suitable for binary data.
130 Signed vs Unsigned ---> Is a logical concept which is important when you do arithmetic operations on the data, but when storing the data, if you will include all of the (8-bits) and will not ignore any of them ---> Then don't care.
132 In Ring, don't think about these details, we are hiding it from you, so you can focus on your application and what you will do.
134 Think in C when you write C code which could be (based on need) low level code to have control on everything.
135 ----> Good for performance and memory management
137 Think in Ring when you write Ring code which let you ignore a lot of details and concentrate only on the result
138 -----> Good for productivity and delivering software quickly
140 The good news (We can mix between Ring and C in our projects)
143 (3) The functions Int2Bytes(), Float2Bytes() and Double2Bytes()
145 These function take input as (Number) ---> Convert it to group of bytes based on the number type (int|float|double) ---> Then return a Ring string that contains these bytes
147 Int2Bytes() ---> Ring string (Group of bytes) and the string size = sizeof(int)
149 Float2Bytes() ---> Ring string (Group of bytes) and the string size = sizeof(float)
151 Double2Bytes() ---> Ring string (Group of bytes) and the string size = sizeof(double)
157 ? len( int2bytes(1) )
158 ? len( float2bytes(1) )
159 ? len( double2bytes(1) )
171 When we use a number, Ring always use the (Double) data type for representing these numbers in memory.
172 This is important to know when we do arithmetic operations on numbers.
174 But when we convert the number to a String using "" + number or using string(number) we get a string where each digit is represented in 1 byte (Not good idea for storage, but useful for string processing)
176 If you need the number to be represented in specific size (int|float|double) for storage then use bytes2int() , bytes2float() and bytes2double() when writing the data to binary files.
178 Ring Number (double) ----> int2bytes() - will cast the number from double to int then return the bytes ----> 4 bytes (Ring String)
180 Ring Number (double) ----> float2bytes() - will cast the number from double to float then return the bytes ----> 4 bytes (Ring String)
182 Ring Number (double) ----> double2bytes() - will use the number (double) to return the bytes ----> 8 bytes (Ring String)
184 The (int) type is used only for internal Ring operations, but Ring applications|code will use only the (double) type for numbers.
186 (5) The Unsigned() Function
188 The function unsigned() expect the first and the second parameters as numbers
192 unsigned(nNumber1,nNumber2,cOperator)
194 We can use the bytes2int() function to convert the bytes to a number
205 for kk=1 to 4 { B[k][kk]= char(60+4*k +kk) }
206 ? " B" +k +": " +B[k]
209 A12= Space(4) A12= bytes2int(B[1]) ^ bytes2int(B[2])
210 ? "A12: " +string(A12)
211 A34= Space(4) A34= bytes2int(B[3]) ^ bytes2int(B[4])
212 ? "A34: " +string(A34)
213 A12= space(4) A12= Unsigned(bytes2int(B[1]),bytes2int(B[2]),"^")
214 ? "unsigned A12: " +A12
215 A34= space(4) A34= Unsigned(bytes2int(B[3]),bytes2int(B[4]),"^")
216 ? "unsigned A34: " +A34
228 unsigned A12: 201589764
229 unsigned A34: 470025220