diff --git a/Branswe.cs b/Branswe.cs new file mode 100644 index 0000000..35dcfed --- /dev/null +++ b/Branswe.cs @@ -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) + { + //��ʼ�� + string[] codelines = Code.Split("\n"); + int Codelength = codelines.Length; + + + + for (int coderun = 0; coderun < Codelength; coderun++) + { + string line = codelines[coderun]; + + // �������к��� # ��ͷ��ע�� + if (string.IsNullOrWhiteSpace(line) || line.TrimStart().StartsWith("#")) + { + continue; + } + //��ȡ���ʶ�����ո�ָ���һ���� + + 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(); //����C#��beep + break; + } + case "sleep": + { + Branswe.Run("ref getvar " + line.Split(" ")[1]); + Thread.Sleep(TimeSpan.FromMilliseconds(long.Parse(geted)));//����C# Threading��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 then else + //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. �ƶϷ��������� varPart �ĵ�һ�������� + var firstToken = varPart.Split(' ')[0]; + Branswe.Run("ref getvar " + firstToken); + string methodName = geted.TrimEnd('(').Trim(); // "print(" �� "print" + + // 2. ����Ƿ�ƥ�� + if (line.StartsWith(methodName)) + { + // 3. ��ȡ���� + string args = line.Substring(methodName.Length).Trim(); + if (args.StartsWith("(") && args.EndsWith(")")) + { + args = args.Substring(1, args.Length - 2); + } + + // 4. �������մ��� + string finalCode = codeTemplate; + var varTokens = varPart.Split(' ', StringSplitOptions.RemoveEmptyEntries); + + // �ȴ��������滻 + for (int j = 1; j < varTokens.Length; j++) // ������һ������������ + { + var token = varTokens[j]; + if (token == "[]") + { + // varPart �е� [] �Ѿ��� args ʹ���� + // codeTemplate �п���Ҳ�� []����Ҫ�滻 + finalCode = finalCode.Replace("[]", args); + } + else + { + Branswe.Run("ref getvar " + token); + finalCode = finalCode.Replace(token, geted); + } + } + + // 5. ִ�� + Branswe.Run(finalCode); + return; + } + } + + // û��ƥ��ķ���������ֱ��ִ�� + Branswe.Run(line); + break; + } + } + } + } +} \ No newline at end of file diff --git a/FileSystem.cs b/FileSystem.cs index d7ba48e..2cc0596 100644 --- a/FileSystem.cs +++ b/FileSystem.cs @@ -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:\") diff --git a/Shell.cs b/Shell.cs index c0db871..fe88cf7 100644 --- a/Shell.cs +++ b/Shell.cs @@ -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 - Simple code editor"); Console.WriteLine(" ls - List files and directories"); Console.WriteLine(" cd - 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 - Create directory"); Console.WriteLine(" rm - Remove file"); @@ -126,6 +129,8 @@ namespace CMLeonOS Console.WriteLine(" user delete - Delete user"); Console.WriteLine(" user list - List all users"); Console.WriteLine(" cpass - Change password"); + Console.WriteLine(" beep - Play beep sound"); + Console.WriteLine(" branswe - 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 "); + 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}"); + } + } } } \ No newline at end of file diff --git a/UserSystem.cs b/UserSystem.cs index 851637e..1b46158 100644 --- a/UserSystem.cs +++ b/UserSystem.cs @@ -304,6 +304,8 @@ namespace CMLeonOS if (foundUser.Password == password) { Console.WriteLine("Login successful!"); + + Console.Beep(); // 创建用户文件夹 CreateUserFolder(foundUser.Username); diff --git a/example.bran b/example.bran new file mode 100644 index 0000000..1ba6087 --- /dev/null +++ b/example.bran @@ -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