.. _lexical_structure: ================= 词汇结构 ================= .. index:: single: lexical structure ----------- 标识符 ----------- .. index:: single: identifiers 标识符以字母字符(而不是符号 '_')开头,后跟任意数量的字母字符、'_' 或数字 ([0-9])。Daslang 是一种区分大小写的语言,这意味着同一字母字符的小写和大写表示形式被视为不同的字符。例如,“foo”、“Foo” 和 “fOo” 被视为 3 个不同的标识符。 ----------- 关键字 ----------- .. index:: single: keywords 以下单词保留为关键字,不能用作标识符: +------------+------------+-----------+------------+------------+-------------+ | struct | class | let | def | while | if | +------------+------------+-----------+------------+------------+-------------+ | static_if | else | for | recover | true | false | +------------+------------+-----------+------------+------------+-------------+ | new | typeinfo | type | in | is | as | +------------+------------+-----------+------------+------------+-------------+ | elif | static_elif| array | return | null | break | +------------+------------+-----------+------------+------------+-------------+ | try | options | table | expect | const | require | +------------+------------+-----------+------------+------------+-------------+ | operator | enum | finally | delete | deref | aka | +------------+------------+-----------+------------+------------+-------------+ | typedef | with | cast | override | abstract | upcast | +------------+------------+-----------+------------+------------+-------------+ | iterator | var | addr | continue | where | pass | +------------+------------+-----------+------------+------------+-------------+ | reinterpret| module | public | label | goto | implicit | +------------+------------+-----------+------------+------------+-------------+ | shared | private | smart_ptr | generator | yield | unsafe | +------------+------------+-----------+------------+------------+-------------+ | assume | explicit | sealed | static | inscope | fixed_array | +------------+------------+-----------+------------+------------+-------------+ | typedecl | capture | default | uninitialized | template | +------------+------------+-----------+------------+------------+-------------+ 以下单词保留为类型名称,不能用作标识符: +------------+------------+-----------+------------+------------+-------------+ | bool | void | string | auto | int | int2 | +------------+------------+-----------+------------+------------+-------------+ | int3 | int4 | uint | bitfield | uint2 | uint3 | +------------+------------+-----------+------------+------------+-------------+ | uint4 | float | float2 | float3 | float4 | range | +------------+------------+-----------+------------+------------+-------------+ | urange | block | int64 | uint64 | double | function | +------------+------------+-----------+------------+------------+-------------+ | lambda | int8 | uint8 | int16 | uint16 | tuple | +------------+------------+-----------+------------+------------+-------------+ | variant | range64 | urange64 | | | | +------------+------------+-----------+------------+------------+-------------+ 本文档稍后将详细介绍关键字和类型。 ----------- 运算符 ----------- .. index:: single: operators Daslang 可识别以下运算符: +----------+----------+----------+----------+----------+----------+----------+----------+ | ``+=`` | ``-=`` | ``/=`` | ``*=`` | ``%=`` | ``|=`` | ``^=`` | ``<<`` | +----------+----------+----------+----------+----------+----------+----------+----------+ | ``>>`` | ``++`` | ``--`` | ``<=`` | ``<<=`` | ``>>=`` | ``>=`` | ``==`` | +----------+----------+----------+----------+----------+----------+----------+----------+ | ``!=`` | ``->`` | ``<-`` | ``??`` | ``?.`` | ``?[`` | ``<|`` | ``|>`` | +----------+----------+----------+----------+----------+----------+----------+----------+ | ``:=`` | ``<<<`` | ``>>>`` | ``<<<=`` | ``>>>=`` | ``=>`` | ``+`` | ``@@`` | +----------+----------+----------+----------+----------+----------+----------+----------+ | ``-`` | ``*`` | ``/`` | ``%`` | ``&`` | ``|`` | ``^`` | ``>`` | +----------+----------+----------+----------+----------+----------+----------+----------+ | ``<`` | ``!`` | ``~`` | ``&&`` | ``||`` | ``^^`` | ``&&=`` | ``||=`` | +----------+----------+----------+----------+----------+----------+----------+----------+ | ``^^=`` | | | | | | | | +----------+----------+----------+----------+----------+----------+----------+----------+ ------------ 其他符号 ------------ .. index:: single: delimiters single: other tokens 其他重要的符号是: +----------+----------+----------+----------+----------+----------+ | ``{`` | ``}`` | ``[`` | ``]`` | ``.`` | ``:`` | +----------+----------+----------+----------+----------+----------+ | ``::`` | ``'`` | ``;`` | ``"`` | ``]]`` | ``[[`` | +----------+----------+----------+----------+----------+----------+ | ``[{`` | ``}]`` | ``{{`` | ``}}`` | ``@`` | ``$`` | +----------+----------+----------+----------+----------+----------+ | ``#`` | | | | | | +----------+----------+----------+----------+----------+----------+ ----------- 字面值 ----------- .. index:: single: literals single: string literals single: numeric literals Daslang 接受整数、无符号整数、浮点和双点数字以及字符串文字。 +-------------------------------+------------------------------------------+ | ``34`` | Integer number(base 10) | +-------------------------------+------------------------------------------+ | ``0xFF00A120`` | Unsigned Integer number(base 16) | +-------------------------------+------------------------------------------+ | 13l | Long Integer number(base 10) | +-------------------------------+------------------------------------------+ | ``0xFF00A120ul`` | Long Unsigned Integer number(base 16) | +-------------------------------+------------------------------------------+ | 32u8 | Unsigned 8-byte integer | +-------------------------------+------------------------------------------+ | ``'a'`` | Integer number | +-------------------------------+------------------------------------------+ | ``1.52`` | Floating point number | +-------------------------------+------------------------------------------+ | ``1.e2`` | Floating point number | +-------------------------------+------------------------------------------+ | ``1.e-2`` | Floating point number | +-------------------------------+------------------------------------------+ | ``1.52d`` | Double point number | +-------------------------------+------------------------------------------+ | ``1.e2lf`` | Double point number | +-------------------------------+------------------------------------------+ | ``1.e-2d`` | Double point number | +-------------------------------+------------------------------------------+ | ``"I'm a string"`` | String | +-------------------------------+------------------------------------------+ | ``" I'm a`` | | | ``multiline verbatim string`` | | | ``"`` | String | +-------------------------------+------------------------------------------+ 伪代码 BNF: .. productionlist:: IntegerLiteral : [1-9][0-9]* | '0x' [0-9A-Fa-f]+ | ''' [.]+ ''' | 0[0-7]+ FloatLiteral : [0-9]+ '.' [0-9]+ FloatLiteral : [0-9]+ '.' 'e'|'E' '+'|'-' [0-9]+ StringLiteral: '"'[.]* '"' VerbatimStringLiteral: '@''"'[.]* '"' ----------- 注释 ----------- .. index:: single: comments 注释是编译器忽略的文本,但对程序员很有用。 注释通常用于在代码中嵌入注释。编译器将它们视为空格。 注释可以是 ``/*`` (斜杠、星号) 字符,后跟任何字符序列 (包括换行符),后跟 ``*/`` 字符。此语法与 ANSI C 相同:: /* This is a multiline comment. This lines will be ignored by the compiler. */ 注释也可以是 ``//``(两个斜杠) 字符,后跟任何字符序列。 如果新行前面没有紧跟反斜杠,则终止这种形式的注释。 它通常被称为 *单行注释*:: // This is a single line comment. This line will be ignored by the compiler. ---------------------------------------------- 语义缩进和重要空格 ---------------------------------------------- .. index:: single: significant white space gen2 中的 Daslang 会自动将半列放在代码内,并用卷曲的护腕括起来; 除非它也包含在任何类型的括号中:: def foo { var a = 0 // das lang 将补充 ;这里 var b = 1; // redundant ; var c = ( 1 // 无自动 ;这里 + 2 ) * 3; // redundant ; } .. index:: single: indenting gen1 sytnax 中的 Daslang 遵循语义缩进(很像 Python)。 这意味着逻辑块以相同的缩进方式排列,如果控制语句需要块的嵌套(例如函数的主体、块、if、for 等),则必须再缩进一步。 缩进步骤是程序选项的一部分。 它是 2、4 或 8,但对于整个文件总是相同的。 默认缩进为 4,但可以按项目全局覆盖。