Branswe支持

This commit is contained in:
2026-01-31 23:36:50 +08:00
parent a974c1ff0f
commit b8cabfd1c0
5 changed files with 672 additions and 1 deletions

520
Branswe.cs Normal file
View File

@@ -0,0 +1,520 @@
using Cosmos.Core;
using Cosmos.HAL;
using Cosmos.HAL.BlockDevice;
using Cosmos.System;
using Cosmos.System.FileSystem;
using Cosmos.System.FileSystem.VFS;
using Cosmos.System.Graphics;
using Cosmos.System.Network;
using System;
using System.Collections.Generic;
using System.Diagnostics.Metrics;
using System.Drawing;
using System.IO;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Xml.Linq;
using Console = System.Console;
using Sys = Cosmos.System;
public static class Branswe
{
private static string varlib = "";
private static string geted = "";
private static string runlib = "";
private static string methods = "";
public static void Run(string Code)
{
//<2F><>ʼ<EFBFBD><CABC>
string[] codelines = Code.Split("\n");
int Codelength = codelines.Length;
for (int coderun = 0; coderun < Codelength; coderun++)
{
string line = codelines[coderun];
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>к<EFBFBD><D0BA><EFBFBD> # <20><>ͷ<EFBFBD><CDB7>ע<EFBFBD><D7A2>
if (string.IsNullOrWhiteSpace(line) || line.TrimStart().StartsWith("#"))
{
continue;
}
//<2F><>ȡ<EFBFBD><C8A1><EFBFBD>ʶ<EFBFBD><CAB6><EFBFBD><EFBFBD><EFBFBD>ո<EFBFBD>ָ<EFBFBD><D6B8><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD>
switch (line.Split(" ")[0])
{
case "method":
// 与CMLeonOS不兼容删除此功能
Console.WriteLine("Error: method command is not compatible with CMLeonOS");
break;
case "diskfile":
// 与CMLeonOS不兼容删除此功能
Console.WriteLine("Error: diskfile command is not compatible with CMLeonOS");
break;
case "rstr": //rstr=Read String To Run
// 与CMLeonOS不兼容删除此功能
Console.WriteLine("Error: rstr command is not compatible with CMLeonOS");
break;
case "var()":
{
var parts = line.Split(" ", 4);
if (parts.Length == 3 && parts[2] == "rm")
{
string varName = parts[1];
string[] lines = varlib.Split('\n');
string newVarlib = "";
foreach (string varlibLine in lines)
{
if (!string.IsNullOrEmpty(varlibLine))
{
if (!varlibLine.StartsWith(varName + "\uE001"))
{
newVarlib = newVarlib + "\n" + varlibLine;
}
}
}
varlib = newVarlib;
}
else
{
switch (parts[2])
{
case "=+":
{
Branswe.Run("ref getvar " + parts[1]);
int wc0 = int.Parse(geted);
Branswe.Run("ref getvar " + parts[3]);
int wc1 = int.Parse(geted);
int calced = wc0 + wc1;
Branswe.Run("var(text) " + parts[1] + " = " + calced);
break;
}
case "=-":
{
Branswe.Run("ref getvar " + parts[1]);
int wc0 = int.Parse(geted);
Branswe.Run("ref getvar " + parts[3]);
int wc1 = int.Parse(geted);
int calced = wc0 - wc1;
Branswe.Run("var(text) " + parts[1] + " = " + calced);
break;
}
case "=*":
{
Branswe.Run("ref getvar " + parts[1]);
int wc0 = int.Parse(geted);
Branswe.Run("ref getvar " + parts[3]);
int wc1 = int.Parse(geted);
int calced = wc0 * wc1;
Branswe.Run("var(text) " + parts[1] + " = " + calced);
break;
}
case "=/":
{
Branswe.Run("ref getvar " + parts[1]);
int wc0 = int.Parse(geted);
Branswe.Run("ref getvar " + parts[3]);
int wc1 = int.Parse(geted);
int calced = wc0 / wc1;
Branswe.Run("var(text) " + parts[1] + " = " + calced);
break;
}
case "=":
{
Branswe.Run("ref getvar " + parts[3]);
Branswe.Run("var(text) " + parts[1] + " = " + geted);
break;
}
}
}
break;
}
case "var(text)":
{
var parts = line.Split(" ", 4);
switch (parts[2])
{
case "=":
{
Branswe.Run("var() " + parts[1] + " rm");
varlib = varlib + "\n" + parts[1] + "\uE001" + parts[3];
break;
}
}
break;
}
case "conshowl":
{
Branswe.Run("ref getvar " + line.Split(" ")[1]);
Console.WriteLine(geted);
break;
}
case "ref":
{
string[] parts = line.Split(" ");
if (parts.Length < 2) break;
switch (parts[1])
{
case "mousex":
// 与CMLeonOS不兼容删除此功能
Console.WriteLine("Error: mousex command is not compatible with CMLeonOS");
break;
case "mousey":
// 与CMLeonOS不兼容删除此功能
Console.WriteLine("Error: mousey command is not compatible with CMLeonOS");
break;
case "screenx":
// 与CMLeonOS不兼容删除此功能
Console.WriteLine("Error: screenx command is not compatible with CMLeonOS");
break;
case "screeny":
// 与CMLeonOS不兼容删除此功能
Console.WriteLine("Error: screeny command is not compatible with CMLeonOS");
break;
case "concolour-b":
// 与CMLeonOS不兼容删除此功能
Console.WriteLine("Error: concolour-b command is not compatible with CMLeonOS");
break;
case "concolour-f":
// 与CMLeonOS不兼容删除此功能
Console.WriteLine("Error: concolour-f command is not compatible with CMLeonOS");
break;
case "getalldisks":
{
Branswe.Run("var(text) [] = " + VFSManager.GetDisks().ToArray());
break;
}
case "to":
{
switch (parts[2])
{
case "raw":
{
Branswe.Run("ref getvar " + parts[3]);
Branswe.Run("var(text) [] = " + byte.Parse(geted).ToString());
break;
}
/*case "int":
{
int.Parse(parts[3]);
break;
}
case "long":
{
long.Parse(parts[3]);
break;
}
case "decimal":
{
decimal.Parse(parts[3]);
break;
}*/
}
break;
}
case "getvar":
{
string varName = parts[2];
string value = "";
string[] lines = varlib.Split('\n');
foreach (string lineInLib in lines)
{
if (!string.IsNullOrEmpty(lineInLib) &&
lineInLib.StartsWith(varName + "\uE001"))
{
value = lineInLib.Substring(varName.Length + 1);
}
}
//CSharp:
geted = value;
//Branswe:
Branswe.Run("var(text) [] = " + value);
break;
}
}
break;
}
case "conshow":
{
Branswe.Run("ref getvar " + line.Split(" ")[1]);
Console.Write(geted);
break;
}
case "coninput":
{
Branswe.Run("var(text) " + line.Split(" ")[1] + " = " + Console.ReadLine());
break;
}
case "conbeep":
{
Console.Beep(); //<2F><><EFBFBD><EFBFBD>C#<23><>beep
break;
}
case "sleep":
{
Branswe.Run("ref getvar " + line.Split(" ")[1]);
Thread.Sleep(TimeSpan.FromMilliseconds(long.Parse(geted)));//<2F><><EFBFBD><EFBFBD>C# Threading<6E><67>Sleep
break;
}
/*case "if":
{
if ()
break;
}*/
case "loop":
{
var parts = line.Split(" ");
if (parts[1] == "<<")
{
while (true)
{
Branswe.Run("rstr " + parts[2]);
}
}
break;
}
case "canvas":
// 与CMLeonOS不兼容删除此功能
Console.WriteLine("Error: canvas command is not compatible with CMLeonOS");
break;
case "end":
{
throw new Exception("\uE001");
}
/*case "graphics":
// 与CMLeonOS不兼容删除此功能
Console.WriteLine("Error: graphics command is not compatible with CMLeonOS");
break;*/
case "power":
// 与CMLeonOS不兼容删除此功能
Console.WriteLine("Error: power command is not compatible with CMLeonOS");
break;
case "decide":
{
var parts = line.Split(" ");
bool IsRight;
Branswe.Run("ref getvar " + parts[1]);
var left = geted;
Branswe.Run("ref getvar " + parts[3]);
var right = geted;
switch (parts[2])
{
case "==":
{
if (left == right)
{
IsRight = true;
}
else
{
IsRight = false;
}
break;
}
case "!=":
{
if (left == right)
{
IsRight = false;
}
else
{
IsRight = true;
}
break;
}
case ">":
{
if (long.Parse(left) > long.Parse(right))
{
IsRight = true;
}
else
{
IsRight = false;
}
break;
}
case "<":
{
if (long.Parse(left) < long.Parse(right))
{
IsRight = true;
}
else
{
IsRight = false;
}
break;
}
case ">=":
{
if (long.Parse(left) >= long.Parse(right))
{
IsRight = true;
}
else
{
IsRight = false;
}
break;
}
case "<=":
{
if (long.Parse(left) <= long.Parse(right))
{
IsRight = true;
}
else
{
IsRight = false;
}
break;
}
default:
{
IsRight = false;
break;
}
}
if (IsRight)
{
Branswe.Run("var(text) [] = \uE003");
}
else
{
Branswe.Run("var() [] rm");
}
break;
}
case "if":
{
//if <bool> then <true_code> else <false_code>
//Split: 0 1 2 3 4 5
//Length:6
var parts = line.Split(" ");
Branswe.Run("ref getvar " + parts[1]); //bool
var ifask = geted;
if (ifask == "\uE003")
{
if (parts[2] == "then")
{
Branswe.Run("rstr " + parts[3]); //then
}
}
else if (parts.Length >= 6)
{
if (parts[4] == "else")
{
Branswe.Run("rstr " + parts[5]); //else
}
}
break;
}
case "concls":
{
Console.Clear();
break;
}
/*case "conchange":
{
switch (line.Split(" ")[1])
{
case "bgcolor":
{
Console.BackgroundColor = Color.FromArgb(int.Parse(line.Split(" ")[2]));
break;
}
}
}*/
default:
{
var methodEntries = methods.Split('\uE001', StringSplitOptions.RemoveEmptyEntries);
for (int i = 0; i < methodEntries.Length; i++)
{
var parts = methodEntries[i].Split('\uE002');
if (parts.Length < 2) continue;
string varPart = parts[0].Trim(); // "a c b"
string codeTemplate = parts[1].Trim(); // "d"
// 1. <20>ƶϷ<C6B6><CFB7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> varPart <20>ĵ<EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
var firstToken = varPart.Split(' ')[0];
Branswe.Run("ref getvar " + firstToken);
string methodName = geted.TrimEnd('(').Trim(); // "print(" <20><> "print"
// 2. <20><><EFBFBD><EFBFBD>Ƿ<EFBFBD>ƥ<EFBFBD><C6A5>
if (line.StartsWith(methodName))
{
// 3. <20><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD>
string args = line.Substring(methodName.Length).Trim();
if (args.StartsWith("(") && args.EndsWith(")"))
{
args = args.Substring(1, args.Length - 2);
}
// 4. <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>մ<EFBFBD><D5B4><EFBFBD>
string finalCode = codeTemplate;
var varTokens = varPart.Split(' ', StringSplitOptions.RemoveEmptyEntries);
// <20>ȴ<EFBFBD><C8B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
for (int j = 1; j < varTokens.Length; j++) // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
{
var token = varTokens[j];
if (token == "[]")
{
// varPart <20>е<EFBFBD> [] <20>Ѿ<EFBFBD><D1BE><EFBFBD> args ʹ<><CAB9><EFBFBD><EFBFBD>
// codeTemplate <20>п<EFBFBD><D0BF><EFBFBD>Ҳ<EFBFBD><D2B2> []<5D><><EFBFBD><EFBFBD>Ҫ<EFBFBD>
finalCode = finalCode.Replace("[]", args);
}
else
{
Branswe.Run("ref getvar " + token);
finalCode = finalCode.Replace(token, geted);
}
}
// 5. ִ<><D6B4>
Branswe.Run(finalCode);
return;
}
}
// û<><C3BB>ƥ<EFBFBD><C6A5>ķ<EFBFBD><C4B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֱ<EFBFBD><D6B1>ִ<EFBFBD><D6B4>
Branswe.Run(line);
break;
}
}
}
}
}

View File

@@ -262,7 +262,7 @@ namespace CMLeonOS
}
}
private string GetFullPath(string path)
public string GetFullPath(string path)
{
if (path.StartsWith(@"0:\"))
{
@@ -291,6 +291,69 @@ namespace CMLeonOS
}
}
}
else if (path.StartsWith("../") || path.StartsWith("..\\"))
{
// 支持多层..操作
int level = 0;
string tempPath = path;
while (tempPath.StartsWith("../") || tempPath.StartsWith("..\\"))
{
level++;
if (tempPath.StartsWith("../"))
{
tempPath = tempPath.Substring(3);
}
else if (tempPath.StartsWith("..\\"))
{
tempPath = tempPath.Substring(3);
}
}
// 向上移动level级
string resultPath = currentDirectory;
for (int i = 0; i < level; i++)
{
int lastSlash = resultPath.LastIndexOf('\\');
if (lastSlash == 2) // 0:\
{
resultPath = @"0:\";
}
else
{
resultPath = resultPath.Substring(0, lastSlash);
}
}
return resultPath;
}
else if (path.StartsWith("dir") || path.StartsWith("DIR"))
{
// 支持cd dir1/dir2/dir3等格式
string dirName = path;
// 提取数字部分
string numberPart = "";
for (int i = 3; i < path.Length; i++)
{
if (char.IsDigit(path[i]))
{
numberPart += path[i];
}
else
{
break;
}
}
// 构建完整路径
if (currentDirectory == @"0:\")
{
return $@"0:\{dirName}";
}
else
{
return $@"{currentDirectory}\{dirName}";
}
}
else
{
if (currentDirectory == @"0:\")

View File

@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.IO;
using Sys = Cosmos.System;
namespace CMLeonOS
@@ -103,6 +104,8 @@ namespace CMLeonOS
Console.WriteLine(" edit <file> - Simple code editor");
Console.WriteLine(" ls <dir> - List files and directories");
Console.WriteLine(" cd <dir> - Change directory");
Console.WriteLine(" cd .. - Go to parent directory");
Console.WriteLine(" cd dir1/dir2/dir3 - Go to numbered directory");
Console.WriteLine(" pwd - Show current directory");
Console.WriteLine(" mkdir <dir> - Create directory");
Console.WriteLine(" rm <file> - Remove file");
@@ -126,6 +129,8 @@ namespace CMLeonOS
Console.WriteLine(" user delete <username> - Delete user");
Console.WriteLine(" user list - List all users");
Console.WriteLine(" cpass - Change password");
Console.WriteLine(" beep - Play beep sound");
Console.WriteLine(" branswe <filename> - Execute Branswe code file");
Console.WriteLine(" version - Show OS version");
Console.WriteLine(" about - Show about information");
Console.WriteLine(" help - Show this help message");
@@ -285,6 +290,12 @@ namespace CMLeonOS
case "cpass":
userSystem.ChangePassword();
break;
case "beep":
Console.Beep();
break;
case "branswe":
ProcessBransweCommand(args);
break;
default:
Console.WriteLine($"Unknown command: {command}");
break;
@@ -936,5 +947,35 @@ namespace CMLeonOS
Console.WriteLine("Available commands: add, delete, list");
}
}
private void ProcessBransweCommand(string args)
{
if (string.IsNullOrEmpty(args))
{
Console.WriteLine("Error: Please specify file name");
Console.WriteLine("Usage: branswe <filename>");
return;
}
string filePath = fileSystem.GetFullPath(args);
if (!File.Exists(filePath))
{
Console.WriteLine($"Error: File not found: {args}");
return;
}
try
{
string fileContent = File.ReadAllText(filePath);
// Console.WriteLine($"Executing Branswe code from: {args}");
Branswe.Run(fileContent);
// Console.WriteLine("Branswe execution completed.");
}
catch (Exception ex)
{
Console.WriteLine($"Error executing Branswe: {ex.Message}");
}
}
}
}

View File

@@ -305,6 +305,8 @@ namespace CMLeonOS
{
Console.WriteLine("Login successful!");
Console.Beep();
// 创建用户文件夹
CreateUserFolder(foundUser.Username);

45
example.bran Normal file
View File

@@ -0,0 +1,45 @@
# Branswe代码示例
# 这个文件展示了Branswe编程语言的基本功能
# 示例1简单的Hello World
conshow hello
var(text) hello = Hello, Branswe!
# 示例2变量操作
var() name = LeonOS
var(text) greeting = Hello, name!
conshow greeting
# 示例3条件判断
if hello == hello
then
conshow match
else
conshow no match
end
# 示例4循环
loop 5
conshow count: i
var(text) count = i
conshow count: count
end
# 示例5计算
var(text) result = 10
var(text) num1 = 5
var(text) num2 = 3
rstr result + num1 + num2
conshow result
# 示例6扬声器响声
conbeep
# 示例7睡眠
sleep 1000
# 示例8清屏
concls
# 示例9结束
end