Skip to content

Commit

Permalink
Merge pull request #10 from lanbinshijie/dev
Browse files Browse the repository at this point in the history
Alpha 1.3.0开发
  • Loading branch information
lanbinshijie authored Apr 14, 2023
2 parents edf6d3e + eed2020 commit cb2d390
Show file tree
Hide file tree
Showing 13 changed files with 471 additions and 10 deletions.
147 changes: 147 additions & 0 deletions docs/ish语法.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
# Ish语法

ISH的语法与Bash的语法相似,但是有一些不同。

## 变量


### 变量定义
Bash中定义变量是:

```bash
var="value"
```

而在ISH中定义变量是:

```bash
$var="value"
```

### 变量调用
Bash中调用变量和ISH中调用变量是一样的:

```bash
echo $var
```

### 变量类型
ISH中的变量类型支持Python中的所有类型,包括`int``float``str``bool``list``dict``tuple``set``None`

### 变量赋值
ISH中变量不仅支持赋值,也支持加减乘除等运算。具体如下:

```bash
$a=1
$b=$a+1
$c=$a+$b
```

## 流程控制

### if语句
Bash中的if语句是:

```bash
if [ $a -eq 1 ]; then
echo "a=1"
elif [ $a -eq 2 ]; then
echo "a=2"
else
echo "a!=1 and a!=2"
fi
```

Ish沿袭了Bash的语法,但是增加了一些新的语法,如下:

```cpp
!if ($a==1)
> echo "a=1"
> echo "This is more than one line"
!elseif ($a==2)
> echo "a=2"
!else
> echo "a!=1 and a!=2"
!endif
```
您会发现,在Ish中,if语句的语法是以`!if`开始,以`!endif`结束,每一行的语句都必须以`>`开始,`!elseif``!else`也是以`>`开始。

也就是说,在ISH中的流程语句的关键字(条件语句)必须以`!`开头,如`!if``!else``!elseif``!endif`等。

### for语句
Bash中的for语句是:

```bash
for i in {1..10}
do
echo $i
done
```

基于上面提到的规则,Ish中的for语句是:

```cpp
!for ($i;1,10,1)
> echo $i
!endfor
// 输出1-10
```

我们不难发现,for循环的条件(括号内的内容)是以`;`分隔,分隔后的内容分别是变量名($i)、起始值、结束值、步长。

### while语句
Bash中的while语句是:

```bash
while [ $a -lt 10 ]
do
echo $a
a=$a+1
done
```

Ish中的while语句是:

```cpp
!while ($a<10)
> echo $a
> $a=$a+1
!endwhile
// 输出1-10
```

### 函数
Bash中的函数是:

```bash
function func()
{
echo "This is a function"
}
```

Ish中的函数是:

```cpp
!dec func()
> echo "This is a function"
!enddec
```

## 其他

### 注释
Bash中的注释是:

```bash
# This is a comment
```

Ish中的注释是:

```txt
# This is a comment
或者
; This is a comment
```
中间都要有一个空格,且注释占一整行,注释标识符要放在第一位。
14 changes: 12 additions & 2 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,27 @@
from misc.Logo import Logo
from tools.SelfCheck import SelfCheck
from misc.Info import ProgramInfo
from misc.Info import SSR_Reader
from misc.Error import Error
from tools.Phraser import PS1
from tools.Phraser import alias

ALIAS = alias()

def ExecuteModel(args, moduleName):
if not SelfCheck.CheckModel(moduleName): return
os.chdir(r"./models")
is_mutiple = False
if not os.path.exists(f"{moduleName}.py"):
if os.path.exists(f"{moduleName}.py"):
command = sys.executable + f" ./{moduleName}.py " + args
elif os.path.exists(f"./{moduleName}/main.py"):
os.chdir(rf"./{moduleName}")
command = sys.executable + f" ./main.py " + args
is_mutiple = True
else:
command = sys.executable + f" ./{moduleName}.py " + args
if ALIAS.exsist(moduleName):
command = ALIAS.get(moduleName)
command = sys.executable + f" ./{command}.py " + args
os.system(command)
if is_mutiple: os.chdir(r"..")
os.chdir(r"..")
Expand All @@ -42,6 +50,8 @@ def IceShell():
# 程序从这里开始
SelfCheck.WelcomeStart() # 显示Logo
SelfCheck.LibCheck()
ssr = SSR_Reader()
# print(ssr.paraphraser()["version"])
print(Colors.BLUE + Logo.div_line_n_m + Colors.END + "\n")
while True:
try:
Expand Down
6 changes: 3 additions & 3 deletions misc/Info.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class ProgramInfo:
author = "Lanbin"
using_libs = ["os", "sys"]
models_path = r"./models/"
registered_modules = ["print", "downlib", "dellib", "scan", "models", "shell", "*"]
registered_modules = ["print", "downlib", "dellib", "scan", "models", "shell", "alias", "bash", "*"]
debug_mode = True
ssr_path = r"./ssr/"

Expand Down Expand Up @@ -43,7 +43,7 @@ def __init__(self):
# 读取配置文件
self.config = self.paraphraser(ProgramInfo.ssr_path + "config.ssr")
# 更新配置
self.update_config()
# self.update_config()


# 解码器:将配置文件中的格式转化成字典
Expand All @@ -53,7 +53,7 @@ def __init__(self):
# 请注意,“#”是注释符号,不会被解析,一般独占一行的开头
# 请注意,如果value中有“=”号,那么value中的“=”号不会被解析为=号而是作为普通字符

def paraphraser(self, config_file: str):
def paraphraser(self, config_file: str = ProgramInfo.ssr_path + "config.ssr"):
# 返回值:配置字典
# 读取文件
with open(config_file, "r") as f:
Expand Down
122 changes: 122 additions & 0 deletions models/alias.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
import sys,os
from argparse import ArgumentParser
pa = os.getcwd()
sys.path.append("..")

from tools.Phraser import alias

os.chdir(pa+r"\..\tools")
ALIAS = alias("alias.conf")

# parser = ArgumentParser()
# parser.add_argument("add", help="add some")
# args = parser.parse_args()
# ip = args.add
# start_port = int(args.sp)
# end_port = int(args.np)

# 获取参数
# 如果没有任何参数则输出所有别名
# 参数有三种:add, set, del
# add: 添加别名
# set: 修改别名
# del: 删除别名
# flu: 刷新别名配置文件
# 创建一个类来实现

class Aliaser:
def __init__(self, path="alias.conf"):
self.path = path
self.alias = alias(path)

def add(self, alias, command):
if self.alias.exsist(alias):
print(f"Alias {alias} already exsist!")
else:
with open(self.path, "a") as f:
f.write(f"\n{alias}={command}")

def set(self, alias, command):
if self.alias.exsist(alias): # 别名存在
config = self.alias.getAll()
config[alias] = command
with open(self.path, "w") as f:
for i in config:
f.write(f"{i}={config[i]}\n")
else:
print(f"Alias {alias} not exsist!")

def delete(self, alias):
if self.alias.exsist(alias):
config = self.alias.getAll()
del config[alias]
with open(self.path, "w") as f:
for i in config:
f.write(f"{i}={config[i]}\n")
else:
print(f"Alias {alias} not exsist!")


# 获取参数 参数可能为空

parser = ArgumentParser()
parser.add_argument("-a", "--add", help="add some")
parser.add_argument("-s", "--set", help="set some")
parser.add_argument("-d", "--delete", help="delete some")
# -f 没有参数,加上即代表刷新
# parser.add_argument("-f", "--flush", help="flush some")
parser.add_argument("-f", "--flush", action="store_true", help="flush some")

args = parser.parse_args()
add = args.add
set = args.set
delete = args.delete
flush = args.flush

# 如果参数为空则输出所有别名
if add == None and set == None and delete == None and flush == False:
aliases = ALIAS.getAll()
print("Alias\tRunable")
print('----------------')
for i in aliases:
print(f"{i}\t-> {aliases[i]}")
exit()

# 如果参数不为空则进行操作
# 如果参数为add则添加别名
# 如果参数为set则修改别名
# 如果参数为del则删除别名
# 如果参数为flu则刷新别名配置文件


if flush:
ALIAS.reRead()
exit()

# 如果参数为其他则报错
if add != None:
add = add.split("=")
aliaser = Aliaser()
aliaser.add(add[0], add[1])
ALIAS.reRead()
elif set != None:
set = set.split("=")
aliaser = Aliaser()
aliaser.set(set[0], set[1])
ALIAS.reRead()
elif delete != None:
aliaser = Aliaser()
aliaser.delete(delete)
ALIAS.reRead()
else:
print("Wrong parameter!")
exit()

aliases = ALIAS.getAll()
print("Alias\tRunable")
print('----------------')
for i in aliases:
print(f"{i}\t-> {aliases[i]}")
exit()


Loading

0 comments on commit cb2d390

Please sign in to comment.