Files
CMLeonOS/Branswe.cs

683 lines
29 KiB
C#
Raw Normal View History

2026-01-31 23:36:50 +08:00
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;
}
2026-02-01 15:21:17 +08:00
case "cat":
{
// 与CMLeonOS兼容支持cat命令
var parts = line.Split(" ", 2);
if (parts.Length < 2)
{
Console.WriteLine("Error: Please specify file name");
Console.WriteLine("Usage: cat <filename>");
break;
}
string filePath = parts[1];
if (!File.Exists(filePath))
{
Console.WriteLine($"Error: File not found: {filePath}");
break;
}
try
{
string content = File.ReadAllText(filePath);
Console.WriteLine(content);
}
catch (Exception ex)
{
Console.WriteLine($"Error reading file: {ex.Message}");
}
break;
}
case "echo":
{
// 与CMLeonOS兼容支持echo命令
var parts = line.Split(" ", 2);
if (parts.Length < 2)
{
Console.WriteLine("Error: Please specify text");
Console.WriteLine("Usage: echo <text>");
break;
}
try
{
File.WriteAllText(parts[1], parts[0]);
Console.WriteLine("Text written successfully.");
}
catch (Exception ex)
{
Console.WriteLine($"Error writing file: {ex.Message}");
}
break;
}
case "ls":
{
// 与CMLeonOS兼容支持ls命令
var parts = line.Split(" ", 2);
string dirPath = parts.Length >= 2 ? parts[1] : "";
try
{
if (Directory.Exists(dirPath))
{
var files = Directory.GetFiles(dirPath);
var dirs = Directory.GetDirectories(dirPath);
foreach (var file in files)
{
Console.WriteLine($" {file}");
}
foreach (var dir in dirs)
{
Console.WriteLine($" [{dir}]/");
}
}
else
{
Console.WriteLine($"Error: Directory not found: {dirPath}");
}
}
catch (Exception ex)
{
Console.WriteLine($"Error listing directory: {ex.Message}");
}
break;
}
case "pwd":
{
// 与CMLeonOS兼容支持pwd命令
Console.WriteLine("0:\\");
break;
}
case "mkdir":
{
// 与CMLeonOS兼容支持mkdir命令
var parts = line.Split(" ", 2);
if (parts.Length < 2)
{
Console.WriteLine("Error: Please specify directory name");
Console.WriteLine("Usage: mkdir <dirname>");
break;
}
try
{
Directory.CreateDirectory(parts[1]);
Console.WriteLine($"Directory created: {parts[1]}");
}
catch (Exception ex)
{
Console.WriteLine($"Error creating directory: {ex.Message}");
}
break;
}
case "rm":
{
// 与CMLeonOS兼容支持rm命令
var parts = line.Split(" ", 2);
if (parts.Length < 2)
{
Console.WriteLine("Error: Please specify file name");
Console.WriteLine("Usage: rm <filename>");
break;
}
string filePath = parts[1];
try
{
File.Delete(filePath);
Console.WriteLine($"File deleted: {filePath}");
}
catch (Exception ex)
{
Console.WriteLine($"Error deleting file: {ex.Message}");
}
break;
}
case "rmdir":
{
// 与CMLeonOS兼容支持rmdir命令
var parts = line.Split(" ", 2);
if (parts.Length < 2)
{
Console.WriteLine("Error: Please specify directory name");
Console.WriteLine("Usage: rmdir <dirname>");
break;
}
try
{
Directory.Delete(parts[1]);
Console.WriteLine($"Directory deleted: {parts[1]}");
}
catch (Exception ex)
{
Console.WriteLine($"Error deleting directory: {ex.Message}");
}
break;
}
// default:
// {
// // û<><C3BB>ƥ<EFBFBD><C6A5>ķ<EFBFBD><C4B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֱ<EFBFBD><D6B1>ִ<EFBFBD><D6B4>
// Branswe.Run(line);
// break;
// }
2026-01-31 23:36:50 +08:00
}
}
}
}