Изменения

Материал из Chaotic Onyx
Перейти к навигацииПерейти к поиску
54 байта добавлено ,  13:36, 14 ноября 2015
м
→‎Chapter 19: Двоеточие перед решёткой.
Строка 20: Строка 20:  
Есть две формы записи данного оператора, в зависимости от способа поиска включаемого содержимого. В первом случае компилятор будет искать файл в директории /library, а в другом случае он обратится к текущей директории и если не найдёт искомый файл, то к /library. Например
 
Есть две формы записи данного оператора, в зависимости от способа поиска включаемого содержимого. В первом случае компилятор будет искать файл в директории /library, а в другом случае он обратится к текущей директории и если не найдёт искомый файл, то к /library. Например
 
<!-- The contents of one source file may be inserted into another by using the #include command. There are two forms depending on where you want the compiler to look for the file. In one case it only looks in the library directory and in the other it looks in the current directory first and then in the library directory if necessary. Оригинал-->
 
<!-- The contents of one source file may be inserted into another by using the #include command. There are two forms depending on where you want the compiler to look for the file. In one case it only looks in the library directory and in the other it looks in the current directory first and then in the library directory if necessary. Оригинал-->
#include <libfile>
+
: #include <libfile>
#include "dmfile"
+
: #include "dmfile"
 
libfile - это включаемая библиотека. <!-- library file -->
 
libfile - это включаемая библиотека. <!-- library file -->
 
"dmfile" - это файл исходного кода. <!-- source code file -->
 
"dmfile" - это файл исходного кода. <!-- source code file -->
Строка 52: Строка 52:  
<!--command creates a preprocessor macro. Any subsequent(последующий) occurrences(случай) of the macro in the code will be replaced by the contents of the macro. Places where the macro occurs as part of another word or inside a text string do not count and will not be substituted.-->
 
<!--command creates a preprocessor macro. Any subsequent(последующий) occurrences(случай) of the macro in the code will be replaced by the contents of the macro. Places where the macro occurs as part of another word or inside a text string do not count and will not be substituted.-->
   −
#define Name Value
+
: #define Name Value
 
Name - название макроса(префикса).
 
Name - название макроса(префикса).
 
Value - значение макроса(переменной).
 
Value - значение макроса(переменной).
Строка 65: Строка 65:  
<!--It is also possible to have the macro take arguments which may then be substituted into the replacement text as desired.-->
 
<!--It is also possible to have the macro take arguments which may then be substituted into the replacement text as desired.-->
   −
#define Name(Arg1,Arg2,...) Value
+
: #define Name(Arg1,Arg2,...) Value
 
Arg1 - название первого аргумента.
 
Arg1 - название первого аргумента.
 
Arg2 - название второго аргумента, и т.д.
 
Arg2 - название второго аргумента, и т.д.
Строка 77: Строка 77:  
<!--The following code, for example, uses this technique to prevent the bitshift operator << from taking a lower order of operations when the macro is used in some larger expression.-->
 
<!--The following code, for example, uses this technique to prevent the bitshift operator << from taking a lower order of operations when the macro is used in some larger expression.-->
   −
#define FLAG1 (1<<0)
+
: #define FLAG1 (1<<0)
#define FLAG2 (1<<1)
+
: #define FLAG2 (1<<1)
#define FLAG3 (1<<2)
+
: #define FLAG3 (1<<2)
    
===Специальные макросы===
 
===Специальные макросы===
Строка 97: Строка 97:  
Как пример - задание макросов для поиска спрайтов(icons) и звуков(sounds):
 
Как пример - задание макросов для поиска спрайтов(icons) и звуков(sounds):
   −
#define FILE_DIR icons
+
: #define FILE_DIR icons
#define FILE_DIR sounds
+
: #define FILE_DIR sounds
    
====DEBUG====
 
====DEBUG====
Строка 105: Строка 105:  
<!--macro enables the inclusion of extra debugging information in the dmb file. This makes the file bigger and will result in very slightly slower execution of procedures. However, the advantage is that when a proc crashes, it will tell you the source file and line number where the problem occurred. Without the extra debugging information, only the name of the procedure is reported.-->
 
<!--macro enables the inclusion of extra debugging information in the dmb file. This makes the file bigger and will result in very slightly slower execution of procedures. However, the advantage is that when a proc crashes, it will tell you the source file and line number where the problem occurred. Without the extra debugging information, only the name of the procedure is reported.-->
   −
#define DEBUG
+
: #define DEBUG
 
Просто добавьте эту строчку в ваш код, и режим Debug будет включен.
 
Просто добавьте эту строчку в ваш код, и режим Debug будет включен.
   Строка 151: Строка 151:  
<!--command compiles the code which follows only if the specified macro has been defined. The section is terminated by the #endif command.-->
 
<!--command compiles the code which follows only if the specified macro has been defined. The section is terminated by the #endif command.-->
   −
# #ifdef Macro
+
: #ifdef Macro
 
//Условие для дальнейшего выполнения кода.
 
//Условие для дальнейшего выполнения кода.
# #endif
+
: #endif
 
Также существует команда #ifndef - она срабатывает если значение определённого макроса не было задано.
 
Также существует команда #ifndef - она срабатывает если значение определённого макроса не было задано.
 
<!--There is also a #ifndef command which has the opposite effect. The code that follows is only compiled if the macro is not defined.-->
 
<!--There is also a #ifndef command which has the opposite effect. The code that follows is only compiled if the macro is not defined.-->
Строка 160: Строка 160:  
<!--The DEBUG macro is sometimes used to turn on certain debugging features in the code. The following example demonstrates this technique.-->
 
<!--The DEBUG macro is sometimes used to turn on certain debugging features in the code. The following example demonstrates this technique.-->
   −
#ifdef DEBUG
+
: #ifdef DEBUG
 
mob/verb/GotoMob(mob/M in world)
 
mob/verb/GotoMob(mob/M in world)
 
   set category = "Debugging"
 
   set category = "Debugging"
 
   usr.loc = M.loc
 
   usr.loc = M.loc
#endif
+
: #endif
    
====#if====
 
====#if====
Строка 170: Строка 170:  
The #if command is a more general version of the #ifdef command because it can take any expression involving other macros and constants. If the expression is true, the code which follows is compiled. Otherwise it is skipped. Alternate conditions can be supplied with the #elif command and a final section to be compiled if all else fails may follow the #else command.
 
The #if command is a more general version of the #ifdef command because it can take any expression involving other macros and constants. If the expression is true, the code which follows is compiled. Otherwise it is skipped. Alternate conditions can be supplied with the #elif command and a final section to be compiled if all else fails may follow the #else command.
   −
#if Condition
+
: #if Condition
 
//Conditional code.
 
//Conditional code.
#elif Condition
+
: #elif Condition
 
//Conditional code.
 
//Conditional code.
#else
+
: #else
 
//Conditional code.
 
//Conditional code.
#endif
+
: #endif
 
The condition may involve any of the basic operators but usually only uses the boolean operators. One addition is the defined instruction which tests if the specified macro has been defined.
 
The condition may involve any of the basic operators but usually only uses the boolean operators. One addition is the defined instruction which tests if the specified macro has been defined.
   Строка 184: Строка 184:  
One common use of the #if command is to block out a section of code. This is sometimes done in the course of debugging or possibly to turn off a feature without throwing away the code. The following example demonstrates this technique.
 
One common use of the #if command is to block out a section of code. This is sometimes done in the course of debugging or possibly to turn off a feature without throwing away the code. The following example demonstrates this technique.
   −
#if 0
+
: #if 0
 
   //Disabled code.
 
   //Disabled code.
#endif
+
: #endif
 
Since DM allows comments to be nested (one inside another) it is also possible to accomplish the same thing by putting /* */ around the disabled code. It is a C programmer's habit to use the #if command because many C compilers get confused by nested comments.
 
Since DM allows comments to be nested (one inside another) it is also possible to accomplish the same thing by putting /* */ around the disabled code. It is a C programmer's habit to use the #if command because many C compilers get confused by nested comments.
   Строка 193: Строка 193:  
The #error command stops compilation and displays the specified error message. Library writers can use this to tell the user of the library if something is wrong.
 
The #error command stops compilation and displays the specified error message. Library writers can use this to tell the user of the library if something is wrong.
   −
#error Message
+
: #error Message
 
The following example will refuse to compile if the DM macro is not defined.
 
The following example will refuse to compile if the DM macro is not defined.
   −
#ifndef DM
+
: #ifndef DM
#error You need to define DM as the name of your key!
+
: #error You need to define DM as the name of your key!
#endif
+
: #endif
    
==Some Code Management Issues==
 
==Some Code Management Issues==
Строка 270: Строка 270:  
Sometimes debugging output such as this is simply removed after fixing the problem, but sometimes you may want diagnostic information to appear whenever you are testing the code. In this case, a macro such as the following may be useful.
 
Sometimes debugging output such as this is simply removed after fixing the problem, but sometimes you may want diagnostic information to appear whenever you are testing the code. In this case, a macro such as the following may be useful.
   −
#ifdef DEBUG
+
: #ifdef DEBUG
#define debug world.log
+
: #define debug world.log
#else
+
: #else
#define debug null
+
: #define debug null
#endif
+
: #endif
 
You can then send output to debug and it will be ignored when not in DEBUG mode.
 
You can then send output to debug and it will be ignored when not in DEBUG mode.
  
470

правок

Навигация