MG Mud User | 88f1247 | 2016-06-24 23:31:02 +0200 | [diff] [blame^] | 1 | LPC Basics |
| 2 | Written by Descartes of Borg |
| 3 | first edition: 23 april 1993 |
| 4 | second edition: 17 june 1993 |
| 5 | |
| 6 | CHAPTER 3: LPC Data Types |
| 7 | |
| 8 | 3.1 What you should know by now |
| 9 | LPC object are made up of zero or more variables manipulated by one or |
| 10 | more functions. The order in which these functions appear in code is |
| 11 | irrelevant. The driver uses the LPC code you write by loading copies of |
| 12 | it into memory whenever it is first referenced and additional copies |
| 13 | through cloning. When each object is loaded into memory, all the variables |
| 14 | initially point to no value. The reset() function in compat muds, and |
| 15 | create() in native muds are used to give initial values to variables in |
| 16 | objects. The function for creation is called immediately after the object |
| 17 | is loaded into memory. However, if you are reading this textbook with no |
| 18 | prior programming experience, you may not know what a function is or how |
| 19 | it gets called. And even if you have programming experience, you may |
| 20 | be wondering how the process of functions calling each other gets started |
| 21 | in newly created objects. Before any of these questions get answered, |
| 22 | however, you need to know more about what it is the functions are |
| 23 | manipulating. You therefore should thouroughly come to know the concept |
| 24 | behind LPC data types. Certainly the most boring subject in this manual, |
| 25 | yet it is the most crucial, as 90% of all errors (excepting misplaced |
| 26 | {} and ()) involve the improper usage of LPC data types. So bear through |
| 27 | this important chapter, because it is my feeling that understanding this |
| 28 | chapter alone can help you find coding much, much easier. |
| 29 | |
| 30 | 3.2 Communicating with the computer |
| 31 | You possibly already know that computers cannot understand the letters |
| 32 | and numbers used by humans. Instead, the "language" spoken by computers |
| 33 | consists of an "alphabet" of 0's and 1's. Certainly you know computers |
| 34 | do not understand natural human languages. But in fact, they do not |
| 35 | understand the computer languages we write for them either. Computer |
| 36 | languages like BASIC, C, C++, Pascal, etc. are all intermediate |
| 37 | languages. They allow you to structure your thoughts more coherently |
| 38 | for translation into the 0's and 1's of the computer's languages. |
| 39 | |
| 40 | There are two methods in which translation is done: compilation and |
| 41 | interpretation. These simply are differences betweem when the |
| 42 | programming language is translated into computer language. With |
| 43 | compiled languages, the programmer writes the code then uses a program |
| 44 | called a compiler to translate the program into the computer's |
| 45 | language. This translation occurs before the program is run. With |
| 46 | interpreted languages however, the process of translation occurs as |
| 47 | the program is being run. Since the translation of the program is |
| 48 | occurring during the time of the program's running in interpreted |
| 49 | languages, interpreted languages make much slower programs than |
| 50 | compiled languages. |
| 51 | |
| 52 | The bottom line is, no matter what language you are writing in, at |
| 53 | some point this has to be changed into 0's and 1's which can be |
| 54 | understood by the computer. But the variables which you store in |
| 55 | memory are not simply 0's and 1's. So you have to have a way in |
| 56 | your programming languages of telling the computer whether or not |
| 57 | the 0's and 1's should be treated as decimal numbers or characters or |
| 58 | strings or anything else. You do this through the use of data types. |
| 59 | |
| 60 | For example, say you have a variable which you call 'x' and you give |
| 61 | it the decimal whole number value 65. In LPC you would do this through |
| 62 | the statement: |
| 63 | |
| 64 | ----- |
| 65 | x = 65; |
| 66 | ----- |
| 67 | |
| 68 | You can later do things like: |
| 69 | |
| 70 | _____ |
| 71 | write(x+"\n"); /* \n is symbolically represents a carriage return */ |
| 72 | y = x + 5; |
| 73 | ----- |
| 74 | |
| 75 | The first line allows you to send 65 and a carriage return to someone's screen. |
| 76 | The second line lets you set the value of y to 70. |
| 77 | The problem for the computer is that it does not know what '65' means when |
| 78 | you tell it x = 65;. What you think of 65, it might think of as: |
| 79 | 00000000000000000000000001000001 |
| 80 | But, also, to the computer, the letter 'A' is represented as: |
| 81 | 00000000000000000000000001000001 |
| 82 | So, whenever you instruct the computer write(x+"\n");, it must have some |
| 83 | way of knowing that you want to see '65' and not 'A'. |
| 84 | |
| 85 | The computer can tell the difference between '65' and 'A' through the use |
| 86 | of data types. A data types simply says what type of data is being stored |
| 87 | by the memory location pointed to by a given variable. Thus, each LPC |
| 88 | variable has a variable type which guides conversions. In the example |
| 89 | given above, you would have had the following line somewhere in the |
| 90 | code *before* the lines shown above: |
| 91 | |
| 92 | ----- |
| 93 | int x; |
| 94 | ----- |
| 95 | |
| 96 | This one line tells the driver that whatever value x points to, it will |
| 97 | be used as the data type "int", which is short for integer, or whole |
| 98 | number. So you have a basic introduction into the reason why data types |
| 99 | exist. They exist so the driver can make sense of the 0's and 1's that |
| 100 | the computer is storing in memory. |
| 101 | |
| 102 | 3.3 The data types of LPC |
| 103 | All LPMud drivers have the following data types: |
| 104 | |
| 105 | void, status, int, string, object, int *, string *, object *, mixed * |
| 106 | |
| 107 | Many drivers, but not all have the following important data types which |
| 108 | are important to discuss: |
| 109 | |
| 110 | float, mapping, float *, mapping * |
| 111 | |
| 112 | And there are a few drivers with the following rarely used data types |
| 113 | which are not important to discuss: |
| 114 | |
| 115 | function, enum, struct, char |
| 116 | |
| 117 | 3.4 Simple data types |
| 118 | This introductory textbook will deal with the data types void, status, |
| 119 | int, float, string, object, mand mixed. You can find out about the |
| 120 | more complex data types like mappings and arrays in the intermediate |
| 121 | textbook. This chapter deals with the two simplest data types (from the |
| 122 | point of view of the LPC coder), int and string. |
| 123 | |
| 124 | An int is any whole number. Thus 1, 42, -17, 0, -10000023 are all type int. |
| 125 | A string is one or more alphanumeric characters. Thus "a", "we are borg", |
| 126 | "42", "This is a string" are all strings. Note that strings are always |
| 127 | enclosed in "" to allow the driver to distinguish between the int 42 and |
| 128 | the string "42" as well as to distinguish between variable names (like x) |
| 129 | and strings by the same names (like "x"). |
| 130 | |
| 131 | When you use a variable in code, you must first let the driver know |
| 132 | what type of data to which that variable points. This process is |
| 133 | called *declaration*. You do this at the beginning of the function |
| 134 | or at the beginning of the object code (outside of functions before all |
| 135 | functions which use it). This is done by placing the name of the data type |
| 136 | before the name of the variable like in the following example: |
| 137 | |
| 138 | ----- |
| 139 | void add_two_and_two() { |
| 140 | int x; |
| 141 | int y; |
| 142 | |
| 143 | x = 2; |
| 144 | y = x + x; |
| 145 | } |
| 146 | ----- |
| 147 | |
| 148 | Now, this is a complete function. The name of the function is |
| 149 | add_two_and_two(). The function begins with the declaration of an |
| 150 | int variable named x followed by the declaration of an in variable |
| 151 | named y. So now, at this point, the driver now has two variables which |
| 152 | point to NULL values, and it expects what ever values end up there to be |
| 153 | of type int. |
| 154 | |
| 155 | A note about the data types void and status: |
| 156 | Void is a trivial data type which points to nothing. It is not used |
| 157 | with respect to variables, but instead with respect to functions. You |
| 158 | will come to understand this better later. For now, you need only |
| 159 | understand that it points to no value. |
| 160 | |
| 161 | The data type status is a boolean data type. That is, it can only have |
| 162 | 1 or 0 as a value. This is often referred to as being true or false. |
| 163 | |
| 164 | 3.5 Chapter summary |
| 165 | For variables, the driver needs to know how the 0's and 1's the computer |
| 166 | stores in memory get converted into the forms in which you intend them |
| 167 | to be used. The simplest LPC data types are void, status, int, and string. |
| 168 | You do not user variables of type void, but the data type does come |
| 169 | into play with respect to functions. In addition to being used for |
| 170 | translation from one form to the next, data types are used in determining |
| 171 | what rules the driver uses for such operations as +, -, etc. For example, |
| 172 | in the expression 5+5, the driver knows to add the values of 5 and 5 |
| 173 | together to make 10. With strings however, the rules for int addition |
| 174 | make no sense. So instead, with "a"+"b", it appends "b" to the string "a" |
| 175 | so that the final string is "ab". Errors can thus result if you mistakenly |
| 176 | try to add "5"+5. Since int addition makes no sense with strings, the |
| 177 | driver will convert the second 5 to "5" and use string addition. The final |
| 178 | result would be "55". If you were looking for 10, you would therefore |
| 179 | have ended up with erroneous code. Keep in mind, however, that in most |
| 180 | instances, the driver will not do something so useful as coming up with |
| 181 | "55". It comes up with "55" cause it has a rule for adding a string |
| 182 | to an int, namely to treat the int as a string. In most cases, if you |
| 183 | use a data type for which an operation or function is not defined |
| 184 | (like if you tried to divide "this is" by "nonsense", "this is"/"nonsense"), |
| 185 | the driver will barf and report an error to you. |