cursor ai 가 내가 원하는 스크립트 언어을 만들어줌, 아직 수정할께 많은데 이러면 최적의 스크립트 언어을 만들어 쓰는게 유리할수도 있음
using UnityEngine;
using System.Collections.Generic;
using System.Linq;
public class ScriptLanguage
{
private Dictionary<string, object> variables = new Dictionary<string, object>();
private Dictionary<string, List<string>> functions = new Dictionary<string, List<string>>();
private Dictionary<string, ScriptLanguage> modules = new Dictionary<string, ScriptLanguage>();
public void ExecuteScript(string script)
{
string[] lines = script.Split('\n');
int currentLine = 0;
while (currentLine < lines.Length)
{
string line = lines[currentLine].Trim();
if (!string.IsNullOrEmpty(line) && !line.StartsWith("--"))
{
currentLine = ExecuteLine(line, lines, currentLine);
}
currentLine++;
}
}
private int ExecuteLine(string line, string[] lines, int currentLine)
{
if (line.StartsWith("function"))
{
string funcName = line.Split(' ')[1].Split('(')[0].Trim();
List<string> funcBody = new List<string>();
currentLine++;
while (currentLine < lines.Length && !lines[currentLine].Trim().StartsWith("end"))
{
funcBody.Add(lines[currentLine].Trim());
currentLine++;
}
functions[funcName] = funcBody;
return currentLine;
}
else if (line.StartsWith("if"))
{
string condition = line.Substring(2).Trim();
bool result = EvaluateCondition(condition);
if (!result)
{
while (currentLine < lines.Length && !lines[currentLine].Trim().StartsWith("end"))
{
currentLine++;
}
}
return currentLine;
}
else if (line.StartsWith("for"))
{
string[] parts = line.Substring(3).Split('=');
string varName = parts[0].Trim();
string[] range = parts[1].Split(' ');
int start = int.Parse(range[0]);
int end = int.Parse(range[2]);
for (int i = start; i <= end; i++)
{
variables[varName] = i;
int tempLine = currentLine + 1;
while (tempLine < lines.Length && !lines[tempLine].Trim().StartsWith("end"))
{
ExecuteLine(lines[tempLine].Trim(), lines, tempLine);
tempLine++;
}
}
while (currentLine < lines.Length && !lines[currentLine].Trim().StartsWith("end"))
{
currentLine++;
}
return currentLine;
}
else if (line.StartsWith("import"))
{
string moduleName = line.Substring(6).Trim();
ScriptLanguage module = new ScriptLanguage();
modules[moduleName] = module;
return currentLine;
}
else if (line.Contains("="))
{
string[] parts = line.Split('=');
string varName = parts[0].Trim();
string value = parts[1].Trim();
if (value.Contains("+") || value.Contains("-") || value.Contains("*") || value.Contains("/"))
{
variables[varName] = EvaluateExpression(value);
}
else if (value.StartsWith("\"") && value.EndsWith("\""))
{
variables[varName] = value.Trim('"');
}
else if (float.TryParse(value, out float numValue))
{
variables[varName] = numValue;
}
}
else if (line.StartsWith("print"))
{
string content = line.Substring(5).Trim();
content = content.Trim('"');
Debug.Log(content);
}
else if (functions.ContainsKey(line.Split('(')[0].Trim()))
{
string funcName = line.Split('(')[0].Trim();
foreach (string funcLine in functions[funcName])
{
ExecuteLine(funcLine, new string[] { funcLine }, 0);
}
}
return currentLine;
}
private float EvaluateExpression(string expression)
{
expression = expression.Replace(" ", "");
if (expression.Contains("+"))
{
string[] parts = expression.Split('+');
return float.Parse(parts[0]) + float.Parse(parts[1]);
}
else if (expression.Contains("-"))
{
string[] parts = expression.Split('-');
return float.Parse(parts[0]) - float.Parse(parts[1]);
}
else if (expression.Contains("*"))
{
string[] parts = expression.Split('*');
return float.Parse(parts[0]) * float.Parse(parts[1]);
}
else if (expression.Contains("/"))
{
string[] parts = expression.Split('/');
return float.Parse(parts[0]) / float.Parse(parts[1]);
}
return 0;
}
private bool EvaluateCondition(string condition)
{
if (condition.Contains("=="))
{
string[] parts = condition.Split(new[] { "==" }, System.StringSplitOptions.None);
return float.Parse(parts[0].Trim()) == float.Parse(parts[1].Trim());
}
else if (condition.Contains(">"))
{
string[] parts = condition.Split('>');
return float.Parse(parts[0].Trim()) > float.Parse(parts[1].Trim());
}
else if (condition.Contains("<"))
{
string[] parts = condition.Split('<');
return float.Parse(parts[0].Trim()) < float.Parse(parts[1].Trim());
}
return false;
}
public object GetVariable(string name)
{
return variables.ContainsKey(name) ? variables[name] : null;
}
public ScriptLanguage GetModule(string name)
{
return modules.ContainsKey(name) ? modules[name] : null;
}
}
-- 수학 연산자 예제
a = 10
b = 5
result = a + b
print "덧셈 결과: " + result
result = a * b
print "곱셈 결과: " + result
-- 조건문 예제
if a > b
print "a가 b보다 큽니다"
end
-- 반복문 예제
for i = 1 5
print "반복 횟수: " + i
end
-- 함수 정의와 호출 예제
function greet(name)
print "안녕하세요, " + name + "님!"
end
greet("플레이어")
-- 모듈 예제
import math
math.a = 100
math.b = 200
print "모듈 변수 a: " + math.a
print "모듈 변수 b: " + math.b