/
EvilBeaver
/
OneScript
Обзор
Документация
Войти
/
EvilBeaver
/
OneScript
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
v2.0.3-alpha
src/ScriptEngine/Compiler/SymbolScope.cs
135 строк
4 KB
Andrey Ovsiankin
Рефакторинг неймспейсов
30 авг 2021, 16:41
30 авг 2021, 16:41
225528b
Код
Авторство
О чём код?
/*---------------------------------------------------------- This Source Code Form is subject to the terms of the Mozilla Public License, v.2.0. If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/. ----------------------------------------------------------*/ using System; using OneScript.Commons; using OneScript.Contexts; using ScriptEngine.Machine; namespace ScriptEngine.Compiler { public class SymbolScope { private readonly IndexedNameValueCollection<VariableInfo> _variables = new IndexedNameValueCollection<VariableInfo>(); private readonly IndexedNameValueCollection<BslMethodInfo> _methods = new IndexedNameValueCollection<BslMethodInfo>(); public BslMethodInfo GetMethod(string name) { var num = GetMethodNumber(name); return _methods[num]; } public BslMethodInfo GetMethod(int number) { return _methods[number]; } public int GetVariableNumber(string name) { var index = _variables.IndexOf(name); if (index >= 0) return index; throw new SymbolNotFoundException(name); } public VariableInfo GetVariable(int number) { return _variables[number]; } public int GetMethodNumber(string name) { var number = _methods.IndexOf(name); if(number >= 0) { return number; } throw new SymbolNotFoundException(name); } public bool IsVarDefined(string name) { return _variables.IndexOf(name) >= 0; } public bool IsMethodDefined(string name) { return _methods.IndexOf(name) >= 0; } public int DefineVariable(string name, string alias = null) { return DefineVariable(name, alias, SymbolType.Variable); } public int DefineProperty(string name, string alias = null) { return DefineVariable(name, alias, SymbolType.ContextProperty); } private int DefineVariable(string name, string alias, SymbolType symbolType) { if (IsVarDefined(name)) { throw new InvalidOperationException($"Symbol already defined in the scope ({name})"); } if (!string.IsNullOrEmpty(alias) && IsVarDefined(alias)) { throw new InvalidOperationException($"Symbol already defined in the scope ({alias})"); } var newIdx = _variables.Count; var item = new VariableInfo { Index = newIdx, Identifier = name, Alias = alias, Type = symbolType }; _variables.Add(item, name, alias); return newIdx; } public int DefineMethod(BslMethodInfo method) { if (!IsMethodDefined(method.Name)) { int newIdx = _methods.Add(method, method.Name, method.Alias); return newIdx; } else { throw new InvalidOperationException("Symbol already defined in the scope"); } } public string GetVariableName(int number) { return _variables[number].Identifier; } public int VariableCount => _variables.Count; public int MethodCount => _methods.Count; public bool IsDynamicScope { get; set; } } class SymbolNotFoundException : CompilerException { public SymbolNotFoundException(string symbol) : base($"Неизвестный символ: {symbol}") { } } }