xlang 中的空指针是nilptr 而非 null, 与 java 类似,xlang中的所有对象均为指针引用。
xlang 的语法非常简单, 下面演示了一个最简单的 Hello World 程序示例。
// 如同 C 和 java 一样 需要一个入口方法来执行
int main(String [] args){
_system_.consoleWrite("hello World!");
return 0;
}
_system_ 是由 xlang 内部提供的一个基本应用程序接口, 通过其 consoleWrite 方法打印字符串到控制台。
进阶的 Hello Wlord 程序示例
/***
声明包 System
在其中定义类 out
out 类的静态方法 print
**/
package System{
public class out{
public static int print(String text){
return _system_.consoleWrite(text);
}
};
}; /*** package 和 class 语法末尾的分号不能少 ***/
/***
System 包需要声明被使用, 如 using { System; };
使用被声明后的包成员,可以省略包路径 如 out.println
否则, 使用包成员需要使用完整包路径 如 System.out.println
***/
using { System; };
/*** 使用包 System 的成员out类来打印字符串 ***/
int main(String [] args){
out.print("hello World!");
return 0;
}
void foo(){
var map = new Map<int, String>();
//效果等同于 Map<int, String> map = new Map<int, String>();
//既定类型, 编译器会自动推导
var iterator = map.iterator();
}
int main(String [] args) {
int n = 0;
//创建线程
Thread t = new Thread() {
void run()override {
//实现 Thread 类的run方法, 可在线程方法内使对象 n 自增并打印
System.out.println("n = " + n++);
}
};
//使线程开始工作
t.start();
//等待线程返回
t.join();
return 0;
}
// JRPCData 重载 . (小数点)的方法,参数或被作为字符串输入
class JRPCData{
// 取值操作 xx = object.key;
public JRPCData operator . (String key) {
...
}
// 赋值操作 object.key = xx;
public JRPCData operator . (String key, Object value) {
...
}
};
//xlang 重载操作费
class cout{
// 重载 << 操作符
public static cout operator << (String txt){
_system_.consoleWrite(txt);
return new cout();
}
};
int main(String [] args) {
// 使用 << 操作符输出字符串
cout << "hello" << " " << "world!";
return 0;
}
/*** END ***/
//xlang 模板
//定义模板类
class MyArray{
private T [] _array;
public MyArray(int presize){
_array = new T[presize];
}
// 重载[] set操作符, [] 两个参数为set
public void operator [] (int n, T o){
if (n >= 0 && n < _array.length){
_array[n] = o;
}else{
throw new IndexOutOfBoundsException();
}
}
// 重载[] get操作符, [] 一个参数为get
public T operator [](int n){
if (n >= 0 && n < _array.length){
return _array[n];
}else{
throw new IndexOutOfBoundsException();
}
return nilptr;
}
};
int main(String [] args) {
// 定义对象MyArray, T = String
MyArray _ma = new MyArray(10);
// 设置第三个元素的值
_ma[3] = "hi!";
//获取并打印第三个元素的值
_system_.consoleWrite(_ma[3]);
return 0;
}
/*** END ***/
//定义类
class MyArray{
private Object [] _array;
public MyArray(int presize){
_array = new Object[presize];
}
// 重载[] set操作符, [] 两个参数为set
public void operator [] (int n, Object o){
if (n >= 0 && n < _array.length){
_array[n] = o;
}else{
throw new IndexOutOfBoundsException();
}
}
// 重载[] get操作符, [] 一个参数为get
public Object operator [](int n){
if (n >= 0 && n < _array.length){
return _array[n];
}else{
throw new IndexOutOfBoundsException();
}
return nilptr;
}
};
int main(String [] args) {
// 定义对象MyArray, T = String
MyArray _ma = new MyArray(10);
// 设置第三个元素的值
_ma[3] = "hi!";
//获取并打印第三个元素的值
Object s = _ma[3];
//判断是否String类的实例
if (s.instanceOf(String)){
_system_.consoleWrite((String)s);
}
return 0;
}
/*** END ***/
更多示例请见XStudio示例代码和文档
#### http://xlang.link
### calex gramar for xlang
####基础类####
#数字字符和数字字符串
[define]
dig=[0~9]
number=[$dig][$number,]
#空格类的字符和字符串
blank=[\x01~\x20]
space=[$blank][$space,]
#可用英文字母
ch=[A~Z,a~z,_]
equ=[=]
#16进制字符串
hch=[A~F,a~f,$dig]
hexc=[$hch][$hexc,]
#可用英文字母和数字
char=[$ch,$dig]
#命名字符串规则
postfix=([$char][$postfix,],)
#命名规范-必须以英文字母开头
wd=[$ch][$postfix]
####元素类####
#关键字
strcont=(\\?,<\">?)
charscont=(\\?,<\'>?)
[meta]
#关键字识别
k_space=($space)
k_to=to<$char>
k_import=import<$char>
k_cdecl=cdecl<$char>
k_stdcall=stdcall<$char>
k_fastcall=fastcall<$char>
k_pascal=pascal<$char>
k_operator=operator<$char>
k_for=for<$char>
k_while=while<$char>
k_do=do<$char>
k_try=try<$char>
k_catch=catch<$char>
k_if=if<$char>
k_break=break<$char>
k_continue=continue<$char>
k_default=default<$char>
k_interface=interface<$char>
k_class=class<$char>
k_enum=enum<$char>
k_package=package<$char>
k_static=static<$char>
k_const=const<$char>
k_throws=throws<$char>
k_throw=throw<$char>
k_switch=switch<$char>
k_case=case<$char>
k_else=else<$char>
k_return=return<$char>
k_new=new<$char>
k_include=include<$char>
k_using=using<$char>
k_require=require<$char>
k_finally=finally<$char>
k_final=final<$char>
k_synchronized_read=synchronized_read<$char>
k_synchronized_write=synchronized_write<$char>
k_synchronized=synchronized<$char>
k_declare=@Declare<$char>
k_public=public<$char>
k_private=private<$char>
k_protected=protected<$char>
k_override=override<$char>
#十六进制数
hex=0x($hexc)[l,L,]
#浮点数 三种形式 0.6f .6f 0.6 识别优先级高于long
float=(($number[.]($number,)([e,E][+,-,]($number),)[f,F,]),([.]($number)([e,E][+,-,]($number),)[f,F,]),([.,]($number)([e,E][+,-,]($number))[f,F,]),([.,]($number)([e,E][+,-,]($number),)[f,F]))
#长整数 识别优先级高于int
longnum=($number)[l,L]
#整数
intnum=($number)
word=($wd)
####符号类####
colon=[\:]
qm=[\?]
#注释
comment=[/\**\*/,//*\n]
#复合赋值运算 结合性:从右到左
compset_ops=[\>\>=,\<\<=,|=,^=,&=,%=,/=,\*=,-=,+=]
#逻辑或运算
logicor_ops=[||]
#逻辑与运算
logicand_ops=[&&]
#位或运算
bitor_ops=[|]
#位异或运算
bitxor_ops=[^]
#位并运算
bitand_ops=[&]
#等于运算
equals_ops=[==,!=]
#位移运算
bitmov_ops=[\<\<]
#比较运算
meq=[\>=]
leq=[\<=]
lc=[\<]
rc=[\>]
#注解符号
at=[@]
#自操作运算
self_ops=[++,--]
#一元运算
unary_ops=[!,\~]
#加减运算
addsub_ops=[+,-]
#乘除取余运算
muldivmod_ops=[\*,/,%]
mov=[=]
indete=[...]
dot=[.]
lb=[\(]
rb=[\)]
ll=[\{]
lr=[\}]
ls=[\[]
rs=[\]]
le=[;]
comma=[\,]
string="({$strcont},)"
chars='({$charscont},)'
[ignore]
comment=true
k_space=true
[syntax]
#############################数学运算#############################
#复合赋值运算 自右向左
compset_symbol=[$compset_ops,$mov]
compset_operation=($normal_object)($compset_symbol)($object)
#三元运算 自右向左
binary_expression=($object)($qm)($object)($colon\e:24)($object)
#逻辑或运算
logicor_symbol=[$logicor_ops]
logicor_operator=($logicand_operation,$logicand_operator)
logicor_operation=([$logicor_operation],$logicor_operator)($logicor_symbol)($logicor_operator)
#逻辑与运算
logicand_symbol=[$logicand_ops]
logicand_operator=($bitor_operation,$bitor_operator)
logicand_operation=([$logicand_operation],$logicand_operator)($logicand_symbol)($logicand_operator)
#位或运算
bitor_symbol=[$bitor_ops]
bitor_operator=($bitxor_operation,$bitxor_operator)
bitor_operation=([$bitor_operation],$bitor_operator)($bitor_symbol)($bitor_operator)
#位异或运算
bitxor_symbol=[$bitxor_ops]
bitxor_operator=($bitand_operation,$bitand_operator)
bitxor_operation=([$bitxor_operation],$bitxor_operator)($bitxor_symbol)($bitxor_operator)
#位与运算
bitand_symbol=[$bitand_ops]
bitand_operator=($equals_operation,$equals_operator)
bitand_operation=([$bitand_operation],$bitand_operator)($bitand_symbol)($bitand_operator)
#等于运算
equals_symbol=[$equals_ops]
equals_operator=($compare_operation,$compare_operator)
equals_operation=([$equals_operation],$equals_operator)($equals_symbol)($equals_operator)
#比较运算
compare_symbol=[$meq,$rc,$leq,$lc]
compare_operator=($bitmov_operation,$bitmov_operator)
compare_operation=([$compare_operation],$compare_operator)($compare_symbol)($compare_operator)
#位移运算 由于和模板的>>冲突 于是这里改成两个rc
bitmov_symbol=[$bitmov_ops,[$rc][$rc]]
bitmov_operator=($addsub_operation,$addsub_operator)
bitmov_operation=([$bitmov_operation],$bitmov_operator)($bitmov_symbol)($bitmov_operator)
#加减法运算
addsub_symbol=[$addsub_ops]
addsub_operator=($muldivmod_operation,$muldivmod_operator)
addsub_operation=([$addsub_operation],$addsub_operator)($addsub_symbol)($addsub_operator)
#乘除取余运算
muldivmod_symbol=[$muldivmod_ops]
muldivmod_operator=($unary_operation,$unary_operator)
muldivmod_operation=([$muldivmod_operation],$muldivmod_operator)($muldivmod_symbol)($muldivmod_operator)
#一元运算 自右向左
unary_symbol=[$unary_ops,$self_ops,$addsub_ops]
unary_operator=($normal_object)
unary_operation=(($unary_symbol)([$unary_operation],$unary_operator),([$unary_operation],$unary_operator)($self_ops))
#############################语法定义#############################
#算子
#顶级元素 常量
const_object=[$hex,$longnum,$intnum,$float,$string,$chars]
word_exp=($word)
meta_object=[($k_operator)($compset_symbol,$logicor_ops,$logicand_ops,$bitor_ops,$bitxor_ops,$bitand_ops,$equals_ops,$bitmov_symbol,$compare_symbol,$self_ops,$unary_ops,$addsub_ops,$muldivmod_ops,$dot,($ls)($rs)),($word_exp)]
#顶级元素 对象
#静态的取属性表达式 只能用于new 对象的时候使用
static_ownner=([$static_ownner],$template_object,$object_block,$word_exp,$const_object)[$dot]($meta_object)
static_element=([$static_element],($static_ownner),$object_block,($word_exp))[$ls\e:20]($object)[$rs\e:21]
template_object=($static_ownner,$object_block,$word_exp)[$lc\e:22]($template_params)[$rc\e:23]
new_expression=[$k_new]($template_object,$static_ownner,$object_block,$word_exp)($lb\e:2)($array,)($rb\e:4)
array_type=($array_type,$template_object,$static_ownner,$object_block,$word_exp)[$ls\e:20][$rs\e:21]
new_array=([$new_array],$template_object,$static_ownner,$object_block,$word_exp)[$ls\e:20]($object)[$rs\e:21]
newarray_expression=[$k_new]($new_array)
array_init_list=[$k_new]($array_type)($init_list)
dynamic_ownner=([$dynamic_ownner],$array_init_list,$newarray_expression,$new_expression,$dynamic_element,$static_element,$call_expression,$temporary_class)[$dot]($meta_object)
dynamic_element=([$dynamic_element],$call_expression,$dynamic_ownner)[$ls\e:20]($object)[$rs\e:21]
call_expression=($dynamic_ownner,$static_ownner,$meta_object,$template_object)($lb\e:2)($array,)($rb\e:4)
static_type=($array_type,$static_ownner,$template_object,$word_exp)
#表达式定义
object_block=($lb\e:2)([$object_block],$array_type,$object)($rb\e:4)
cast_object=($object_block)($normal_object)
return_expression=($k_return)($object,)
throw_expression=($k_throw)($object)
#
normal_expression=(($synchronized_expression,$if_expression,$fastfor_expression,$fastfor_step_expression,$for_expression,$while_expression,$dowhile_expression,$switch_expression,$exception_expression,$def_variable_state,$function),($class,$object,$k_break,$k_continue,$throw_expression,$return_expression)($le\e:3))
#############################对象定义#############################
#语法对象
##############################集合
#这些是没有任何优先级的
normal_object=($cast_object,$init_list,$call_expression,$dynamic_element,$dynamic_ownner,$temporary_class,$array_init_list,$newarray_expression,$new_expression,$template_object,$static_element,$static_ownner,$object_block,$word_exp,$const_object)
#数学运算表达式 这些是带有优先级的
math_object:=($compset_operation,$binary_expression,$logicor_operation,$logicand_operation,$bitor_operation,$bitxor_operation,$bitand_operation,$equals_operation,$compare_operation,$bitmov_operation,$addsub_operation,$muldivmod_operation,$unary_operation)
#带括号的对象
object=($math_object,$normal_object)
#############################函数运算#############################
#调用函数 #参与数学和逻辑运算
#
temporary_class=($new_expression)($ll\e:5)($class_body,)($lr\e:6)
array=:([$array][$comma],)($bitand_ops,)($object)
template_params=([$template_params][$comma],)(($word_exp)($colon)($static_type),$static_type)
init_list=($ll\e:5)($array)($lr\e:6)
#############################特殊表达式#############################
#special
synchronized_expression=($k_synchronized,$k_synchronized_read,$k_synchronized_write)(($lb\e:2)($static_ownner,$word_exp)($rb\e:4))($ll\e:5)($statements,)($lr\e:6)
if_expression=:($k_if)($lb\e:2)($object)($rb\e:4)($statement)(($k_else)($statement),)
for_first_expression=[[$for_first_expression][$comma],]($variable_def,$object)
for_last_expression=[[$for_last_expression][$comma],]($object)
for_in_expression=$object
#for表达式
for_expression=($k_for)($lb\e:2)($for_first_expression,)($le\e:3)($for_in_expression,)($le\e:3)($for_last_expression,)($rb\e:4)($statement)
fastfor_expression=($k_for)($lb\e:2)(($static_type)($word_exp,))($colon\e:24)($object)($rb\e:4)($statement)
fastfor_step_expression=($k_for)($lb\e:2)(($static_type)($word_exp,))($mov)($object)($k_to)($object)($rb\e:4)($statement)
#while表达式
while_expression=($k_while)($lb\e:2)($object)($rb\e:4)($statement)
#dowhile表达式
dowhile_expression=($k_do)($statement)($k_while)($lb\e:2)($object)($rb\e:4)($le\e:3)
#case表达式
case_expression=[$case_expression,](($k_case)($object),$k_default)($colon\e:24)($statements,)
#switch表达式
switch_expression=($k_switch)($lb\e:2)($object)($rb\e:4)($ll\e:5)($case_expression)($lr\e:6)
#try catch表达式
catch_body=[$catch_body,]($k_catch)($lb\e:2)($object)($word_exp)($rb\e:4)($ll\e:5)($statements,)($lr\e:6)
exception_expression=($k_try)($ll\e:5)($statements,)($lr\e:6)($catch_body,)(($k_finally)($ll\e:5)($statements,)($lr\e:6),)
#############################语法结构#############################
statement=([$ll\e:5]($statements,)[$lr\e:6],$normal_expression)
statements=[$statements,]($statement)
variable_init=:[$variable_init($comma),]($word_exp)[($mov)($object),]
throw_list=:[$throw_list($comma),]($static_type)
throw_declare=($k_throws)($throw_list)
enum_list=:[$enum_list($comma),]($word_exp\e:18)[($mov)($object),]
#变量定义
variable_def=($k_static,)($k_const,)($static_type)($variable_init)
def_variable_state=($variable_def)($le\e:3)
#参数定义
params_def=([$params_def][$comma],)($annotate,)($static_type)($word_exp,)
#调用本地函数的参数类型
nativeparams_def=([$nativeparams_def][$comma],)((($word_exp)($muldivmod_ops,$bitand_ops,)($word_exp,)),$indete)
#include语法
filearray=([$filearray][$comma],)($string)
require=($k_require)($lb\e:2)($filearray)($rb\e:4)
include=($k_include)($lb\e:2)($filearray)($rb\e:4)
package_name=[[$package_name][$dot]($word_exp),($word_exp)]
package_array=[[$package_array],]($package_name)[$le]
using=[$k_using][$ll]($package_array)[$lr][$le]
#函数定义
function=(($k_final)($k_static),($k_static)($k_final),$k_final,$k_static,$k_cdecl,$k_stdcall,$k_fastcall,$k_pascal,)([$lc\e:22]($template_params)[$rc\e:23],)($static_type)($word_exp)($lb\e:2)($params_def,)($rb\e:4)($k_override,)($throw_declare,)($ll\e:5)($statements,)($lr\e:6)
function_ctor=($word_exp)($lb\e:2)($params_def,)($rb\e:4)($throw_declare,)($ll\e:5)($statements,)($lr\e:6)
function_ctor_declare=($word_exp)($lb\e:2)($params_def,)($rb\e:4)($throw_declare,)($le\e:3)
operator=(($k_final)($k_static),($k_static)($k_final),$k_final,$k_static,)([$lc\e:22]($template_params)[$rc\e:23],)($static_type)($k_operator)($compset_symbol,$logicor_ops,$logicand_ops,$bitor_ops,$bitxor_ops,$bitand_ops,$equals_ops,$bitmov_symbol,$compare_symbol,$self_ops,$unary_ops,$addsub_ops,$muldivmod_ops,$dot,($ls)($rs))($lb\e:2)($params_def,)($rb\e:4)($k_override,)($throw_declare,)($ll\e:5)($statements,)($lr\e:6)
dispatch=($function_ctor,$function_ctor_declare,$function,$operator)
baseclass=[$colon\e:24]($domainlimit,)([$lc\e:22]($template_params)[$rc\e:23],$static_type\e:15)
baseenum=[$colon\e:24]($domainlimit,)($static_type)
nativefunction_def=($static_type)($k_cdecl,$k_stdcall,$k_fastcall,$k_pascal,)($word_exp)($lb\e:2)($nativeparams_def,)($rb\e:4)($le\e:3)
native_import_body=:[$native_import_body,]($nativefunction_def,)
native_import=($k_import)($string,)($ll\e:5)($native_import_body,)($lr\e:6)($le\e:3)
#类定义
domainlimit=($k_public,$k_protected,$k_private)
class_body=:[$class_body,]($annotate,)($domainlimit,)($dispatch,$interface_dispatch,$native_import,$nativefunction_def,$class,$enum,$interface,$def_variable_state)
#包括模板类
class=(($k_final)($k_static),($k_static)($k_final),$k_final,$k_static,)($k_class)($word_exp)([$lc\e:22]($template_params)[$rc\e:23],)($baseclass,)($ll\e:5)($class_body,)($lr\e:6)($le\e:3)
enum=($k_final,)($k_enum)($word_exp)($baseenum,)($ll\e:5)($enum_list,)($lr\e:6)($le\e:3)
baseinterface=[$colon\e:24]($domainlimit,)($static_type)
function_declare=(($k_final)($k_static),($k_static)($k_final),$k_final,$k_static,)($static_type)($word_exp)($lb\e:2)($params_def,)($rb\e:4)($throw_declare,)($le\e:3)
operator_declare=(($k_final)($k_static),($k_static)($k_final),$k_final,$k_static,)($static_type)($k_operator)($compset_symbol,$logicor_ops,$logicand_ops,$bitor_ops,$bitxor_ops,$bitand_ops,$equals_ops,$bitmov_symbol,$compare_symbol,$self_ops,$unary_ops,$addsub_ops,$muldivmod_ops,$dot,($ls)($rs))($lb\e:2)($params_def,)($rb\e:4)($throw_declare,)($le\e:3)
interface_dispatch=($function_declare,$operator_declare,$interface)
interface_body=:[$interface_body,]($annotate,)($interface_dispatch)
interface=($k_interface)($word_exp)($baseinterface,)($ll\e:5)($interface_body,)($lr\e:6)($le\e:3)
package_body=[$package_body,]($annotate,)($domainlimit,)($package,$class,$enum,$interface)
package=[$k_package]($word_exp)[$ll]($package_body,)[$lr][$le]
declare=($k_declare)($package,$class,$enum,$interface)
### 注解符号
annotate_params=([$annotate_params][$comma],)($word_exp)([$mov]($word_exp,$const_object),)
annotateitem=($at)($word_exp)((($lb\e:2)($annotate_params)($rb\e:4)),)
annotate=([$annotate],)($annotateitem)
#############################语言架构#############################
global=:[$global,]($annotate,)($declare,$function,$using,$include,$require,$native_import,$package,$def_variable_state,$class,$enum,$interface,($object)($le\e:3))
[scan]
scan:=($global,)
[error]