第零章:为什么选择PinkShell?
PinkShell是由Exquisite开发的面向对象的Shell和编程语言,具有Shell和编译两种独立模式,本文档是编译模式的教程。
PinkShell的一些特点:
- 跨平台:Pink OS、Windows、Liunx、Mac OS等操作系统都能兼容PinkShell。
- 系统支持:Pink OS自带PinkShell环境,此系统的GUI程序和系统软件都是使用PinkShell开发的。
- 运行效率高:PinkShell的峰值运行效率仅次于C语言。
- 快速部署:PinkShell可以直接生成可执行文件,如C语言一样便捷。
第一章:环境配置
此文档假设以下条件:
操作系统版本 | Pink OS 17.7.0 |
PinkShell版本 | 8.2.0 |
PinkShell文件的后缀名是.sh
,在某个目录创建一个名为Hello.sh
,然后使用文本编辑器打开。
第二章:基础声明
如果这个文件是编译模式的程序,需要在开头加入这样的声明:
$ mode compile;
否则会以Shell模式运行。
第三章:数据类型
PinkShell严格区分数据类型,使用错误的数据类型会报错或出现逻辑错误,以下是部分数据类型:
整数
类型 | 范围 |
---|---|
auto_int | 无限 |
short_int | -32768~32767 |
long_int | 小于-32,768或大于32,767 |
浮点数
类型 | 范围 |
---|---|
auto_float | 无限 |
short_float | 2.3E-308~1.7E+308 |
long_float | 小于2.3E-308或大于1.7E+308 |
字符串
类型 | 范围 |
---|---|
auto_str | 无限 |
short_str | 0字节~1024字节 |
long_str | 大于1024字节 |
特殊类型
类型 | 含义 |
---|---|
no_type | 无类型 |
null | 空 |
第四章:函数
函数区分数据类型,如果没有返回值,通常是no_type
。
创建和调用函数的语法如下:
defined function <数据类型> <创建的函数名字>(<参数1>,<参数2>...)
<代码块>;
end
调用的函数名字(<参数1>,<参数2>...);
PinkShell需要有一个main
函数作为程序入口,不需要额外调用,大概是这样
defined function no_type main()
<代码块>;
end
第五章:输出
output
类中的echo
函数可以输出数据,语法如下:
output.echo(<参数>);
完整的Hello world程序是这样的:
$ mode compile;
defined function no_type main()
output.echo("Hello world!\n");
end
第六章:变量
变量也区分数据类型,语法如下:
defined variable <数据类型> <变量名> -> <值>;
以下是一个示例,用于连接字符串:
$ mode compile;
defined function no_type main()
defined variable auto_str a -> "Hello";
defined variable auto_str b -> "World";
output.echo(a .. b .. "\n");
end
第七章:输入
input
函数可以获取键盘在终端输入的信息,可以传达到变量,返回的类型随变量的类型而变化,语法如下:
input(<参数(通常是空的)>);
以下是一个示例,用于获取你的名字:
$ mode compile;
defined function no_type main()
output.echo("你的名字是什么:");
defined variable auto_str name -> input();
output.echo("你好," .. name .. "\n");
end
第八章:判断
if...elseif...else
语句用来进行条件判断,语法如下:
if (<条件>)
<代码块>;
elseif (<条件>)
<代码块>;
else
<代码块>;
end
这个示例可以判断密码是否正确:
$ mode compile;
defined function no_type main()
output.echo("请输入密码:");
defined variable auto_str password -> input();
if (password == "112233")
output.echo("密码正确\n");
else
output.echo("密码错误\n");
end
end
第九章:循环
while
语句可以循环执行一个代码块,在没有break
的情况不会停止,语法如下:
while
<代码块>;
end
如果需要达到某种条件跳过循环的情况,可以使用break
语句配合判断来实现:
while
if (<条件>)
break;
else
<代码块>;
end
end
第十章:库
PinkShell拥有各种好用的库,如与系统通信的system
库,简便制作GUI程序的iris
库,数学计算的math
库,管理服务器的web
库…
导入一个库很简单,只需要在开头加入这样的声明:
$ import "<库名称>";
第十一章:实战
这个程序可以循环获取键盘输入,并且判断奇偶数:
$ mode compile;
defined function no_type calc(auto_int parameter)
if (parameter % 2 == 0)
output.echo("这是偶数\n");
else
output.echo("这是奇数\n");
end
end
defined function no_type main()
output.echo("判断奇偶数,输入0退出:\n\n");
while
output.echo(">>>");
defined variable auto_int num -> input();
if (num == 0)
break;
else
calc(num);
end
end
end