Source Insight宏语法与C相似,编写好的宏以.em文件存储。要使用宏先需要把.em文件加入到当前工程或是Base工程中,然后为其分配一个快捷键或是菜单项,之后就可以使用键盘或是点击菜单来调用宏。
si的宏语法“不是”大小写敏感的,且每行语句不强制要求以分号结束,语法元素之间的空格将被自动忽略,变量名必须以字母开头。
1. 宏定义方法如下,可以传递参数,可以有返回值,但和一些脚本语言一样,在si宏中没有数据类型一说。macro my_macro(a, b, c){ ... return a+b+c}
2. 以下代码展示了si宏的控制语句,学过C语言的人都可以很快上手。while (条件){ if (条件) { ... break } else { continue } return 0}
3. 以下代码展示了变量的定义macro SomeFunction(){ var localx //定义局部变量,局部变量可以不定义而直接使用 global globalvariables; //定义全局变量 localx = 1; //局部变量赋值,作用域是当前macro globalvariables = "ison81" //全局变量赋值,作用域是整个si,其它macro也可以访问}
4. 以下代码展示了关于变量和数据类型的一些注意事项,
a. 空串
{ S = nil // s is set to the empty string S = "" // same as nil}
b. 在变量中展开字符串,使用@...@S = “Hey, @username@, don’t break the build again!”
c. 字符串当成数字来运算s = "1"x = s + 2 // x now contains the string "3"y = 2 * x + 5 // x now contains "11"但这样就是错的,s = "hello"x = s + 1 // error如果要灵活,可以先检测一个字符串是不是数字,if (IsNumber(x)) x = x / 4 // okay to do arithmetic
d. 引用字符串中的字符,s = "abc"x = s[0] // x now contains the string "a"si的字符串也是zero-terminated,s = "abc"length = strlen(s)ch = s[length] // ch now contains the empty stringif (ch == "") msg "End of string."
5. 关于结构体
定义Rec = nil // initializes as an empty stringRec.name = “Joe Smith”Rec.age = “34”Rec.experience = “guru”另一种定义方法,rec = “name=/”Joe Smith/”;age=/”34/”;experience=/”guru/””
引用结构体Filename = slr.file // get file field of slrLineNumber = slr.lnFirst // get lnFirst field of slr
6. 关于数组
si宏并没有提供数组的概念,数组是通过buf来模拟实现的,关于buf还是在介绍macro API时再讨论。
7. 运算符
.加减乘除(+, -, *, /).逻辑(&&, ||, !).自增自减(++, --),可前可后.算术比较(>, <, >=, <=, ==, !=)
.字符串比较(==, !=).字符串连接(#)与引用(@...@)
出处: