[②Meson]: Meson语法
前言
承接上文[①Meson]: Meson构建系统简介,在本篇文章中介绍下Meson的基本语法,也可以参考官方的网址:https://mesonbuild.com/Syntax.html。
变量
变量可以有不同的数据类型,比如整型或者字符串。不需要预先定义。
var1 = 'hello'
var2 = 102
重要的一点是Meson变量的对象是不可变的(immutable),例如:
var1 = [1, 2, 3]
var2 = var1
var2 += [4]
# var2 is now [1, 2, 3, 4]
# var1 is still [1, 2, 3]
数值类型
Meson只支持整型数据,支持基本的算术运算:
x = 1 + 2
y = 3 * 4
d = 5 % 3 # Yields 2.
在特地版本后支持16进制,2进制,Octal类型数据:
int_255 = 0xFF # since version 0.45.0
int_493 = 0o755 # since version 0.47.0
int_1365 = 0b10101010101
字符串和数字能互相转化:
string_var = '42'
num = string_var.to_int()
int_var = 42
string_var = int_var.to_string()
布尔类型
Meson支持布尔类型,也可以将布尔类型转化为字符串或者数字:
bool_var = true
string_var = bool_var.to_string()
int_var = bool_var.to_int()
字符串
Meson的字符串用单引号声明:
single_quote = 'contains a \' character'
字符串连接使用 + 号:
str1 = 'abc'
str2 = 'xyz'
combined = str1 + '_' + str2 # combined is now abc_xyz
可以用 / 号构建字符串路径,类似于用Meson命令join_paths():
joined = '/usr/share' / 'projectname' # => /usr/share/projectname
joined = '/usr/local' / '/etc/name' # => /etc/name
# res1 and res2 will have identical values
res1 = join_paths(foo, bar)
res2 = foo / bar
字符串格式化,可以用.format()创建字符串,就像当与字符串中@number@的位置会被.format()中的argument所替代:
template = 'string: @0@, number: @1@, bool: @2@'
res = template.format('text', 1, true)
# res now has value 'string: text, number: 1, bool: true'
在0.63版本后也可以用f-string命令:
n = 10
m = 'hi'
s = f'int: @n@, string: @m@'
# s now has the value 'int: 10, string: hi'
下面介绍一些字符串的操作命令:
.replace()
# Replaces all instances of one substring with another
s = 'semicolons;as;separators'
s = s.replace('as', 'are')
# 's' now has the value of 'semicolons;are;separators'
.strip()
# Similar to the Python str.strip(). Removes leading/ending spaces and newlines.
define = ' -Dsomedefine '
stripped_define = define.strip()
# 'stripped_define' now has the value '-Dsomedefine'
# You may also pass a string to strip, which specifies the set of characters to
# be removed instead of the default whitespace.
string = 'xyxHelloxyx'.strip('xy')
# 'string' now has the value 'Hello'
.to_upper(), .to_lower()
target = 'x86_FreeBSD'
upper = target.to_upper() # t now has the value 'X86_FREEBSD'
lower = target.to_lower() # t now has the value 'x86_freebsd'
.contains(), .startswith(), .endswith()
target = 'x86_FreeBSD'
is_fbsd = target.to_lower().contains('freebsd')
# is_fbsd now has the boolean value 'true'
is_x86 = target.startswith('x86') # boolean value 'true'
is_bsd = target.to_lower().endswith('bsd') # boolean value 'true'
.substring()
# Similar to the Python str[start:end] syntax
target = 'x86_FreeBSD'
platform = target.substring(0, 3) # prefix string value 'x86'
system = target.substring(4) # suffix string value 'FreeBSD'
.split(), .join()
# Similar to the Python str.split()
components = 'a b c d '.split()
# components now has the value ['a', 'b', 'c', 'd']
components = 'a b c d '.split(' ')
# components now has the value ['a', 'b', '', '', 'c', 'd', '']
# Similar to the Python str.join()
output = ' '.join(['foo', 'bar'])
# Output value is 'foo bar'
pathsep = ':'
path = pathsep.join(['/usr/bin', '/bin', '/usr/local/bin'])
# path now has the value '/usr/bin:/bin:/usr/local/bin'
.underscorify()
name = 'Meson Docs.txt#Reference-manual'
# Replaces all characters other than `a-zA-Z0-9` with `_` (underscore)
# Useful for substituting into #defines, filenames, etc.
underscored = name.underscorify()
# underscored now has the value 'Meson_Docs_txt_Reference_manual'
.version_compare()
version = '1.2.3'
# Compare version numbers semantically
is_new = version.version_compare('>=2.0')
# is_new now has the boolean value false
# Supports the following operators: '>', '<', '>=', '<=', '!=', '==', '='
数组
Meson数组由 [] 声明,数组内元素可以包括任何类型:
my_array = [1, 2, 'string', some_obj]
数组内的元素可以由index进行索引:
my_array = [1, 2, 'string', some_obj]
second_element = my_array[1]
last_element = my_array[-1]
数组添加元素:
my_array += ['foo', 3, 4, another_obj]
my_array += ['something']
# This also works
my_array += 'else'
在0.49版本后可以由下面方法判断数组中元素是否存在:
my_array = [1, 2]
if 1 in my_array
# This condition is true
endif
if 1 not in my_array
# This condition is false
endif
字典
Meson字典由 {} 声明,字典里包含任意数量的key:value pairs(键/值),键必须是字符串类型,并且不能相同:
my_dict = {'foo': 42, 'bar': 'baz'}
# This will fail
my_dict = {'foo': 42, 'foo': 43}
字典索引:
my_dict = {'foo': 42, 'bar': 'baz'}
forty_two = my_dict['foo']
# This will fail
my_dict['does_not_exist']
If statements
Meson的if判断语句:
var1 = 1
var2 = 2
if var1 == var2 # Evaluates to false
something_broke()
elif var3 == var2
something_else_broke()
else
everything_ok()
endif
opt = get_option('someoption')
if opt != 'foo'
do_something()
endif
逻辑判断
Meson中逻辑判断在If statements中的使用方法:
if a and b
# do something
endif
if c or d
# do something
endif
if not e
# do something
endif
if not (f or g)
# do something
endif
Foreach statements
Meson支持foreach命令:
progs = [['prog1', ['prog1.c', 'foo.c']],
['prog2', ['prog2.c', 'bar.c']]]
foreach p : progs
exe = executable(p[0], p[1])
test(p[0], exe)
endforeach
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。 如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网邮箱:veading@qq.com进行投诉反馈,一经查实,立即删除!