MG Mud User | 88f1247 | 2016-06-24 23:31:02 +0200 | [diff] [blame^] | 1 | * What is LPC? |
| 2 | |
| 3 | LPC is the language in which LPmud objects are written. |
| 4 | LPC stands for Lars Pensj| C. As one might surmise from the name, |
| 5 | LPC is based on the syntax of C. LPC provides the C while loop, for loop, |
| 6 | if statement, switch statement, a variant of sscanf, and integer data type, |
| 7 | (LPC also provides other data types not in C such as the object and the |
| 8 | mapping). LPC uses C's syntax for defining and calling functions and for |
| 9 | declaring variables. Note that LPC's version of the string datatype is |
| 10 | much different from that provided by C. See the LPC tutorial on syntax |
| 11 | and language constructs for more information. |
| 12 | |
| 13 | Here are some differences between LPC and C: |
| 14 | |
| 15 | There is no need for a function named "main" in LPC objects (although there |
| 16 | is one called "create"). |
| 17 | |
| 18 | The efuns (or system calls) provided by the gamedriver are different than |
| 19 | those typically found in the C library (libc.a). |
| 20 | |
| 21 | There is no malloc(). However, there is an allocate(int value) efun that |
| 22 | lets space be allocated for arrays. Note that the argument to 'allocate' |
| 23 | is not in units of bytes, but rather in units of elements. |
| 24 | |
| 25 | Memory is never explicitly deallocated. The gamedriver keeps track of |
| 26 | how many times a given piece of data has been referenced. When the |
| 27 | reference count goes to zero (when no object has a copy of that variable), |
| 28 | then the space used by the variable is reclaimed (garbage collected). |
| 29 | |
| 30 | The string data type in LPC is closer to that provided by BASIC than that |
| 31 | provided by C. Strings are not declared as arrays of characters but rather |
| 32 | as a basic intrinsic type. Strings may be concatenated using the '+' operator. |
| 33 | |
| 34 | For example, the LPC statements: |
| 35 | |
| 36 | string ack; |
| 37 | |
| 38 | ack = foo + bar; |
| 39 | |
| 40 | are equivalent to the C statements: |
| 41 | |
| 42 | char *ack; |
| 43 | |
| 44 | ack = (char *)malloc(strlen(foo) + 1); |
| 45 | strcpy(ack,foo); |
| 46 | ack = (char *)realloc(strlen(ack) + strlen(bar) + 1); |
| 47 | strcat(ack,bar); |
| 48 | |
| 49 | Note: ack[i] may not appear as an lvalue (i.e. ack[i] = 'a'; will not |
| 50 | work as expected). |
| 51 | |
| 52 | LPC is an interpreted language (however it is compiled into an internal |
| 53 | compact tokenized form before being interpreted). |
| 54 | |
| 55 | LPC has no structures or unions. In fact, the -> operator is used to |
| 56 | indicate a call to another object. The mapping datatype can serve |
| 57 | as an effective substitute for structures in some situations. |
| 58 | |
| 59 | sscanf does not work in the same way as in C. arguments to sscanf need not |
| 60 | be pointers (since LPC does not have the explicit pointer data type). Also, |
| 61 | sscanf(arg,"%s %s",str1,str2) does not operate as the C programmer would |
| 62 | expect. In C, the first word of arg would be copied into str1 and the |
| 63 | second word of arg into str2. In LPC, the first word is copied into str1 |
| 64 | and the _remainder_ of arg is copied into str2. |