tlang-interperter-cs/tlanglib/TDictionary.cs

65 lines
1.5 KiB
C#
Raw Normal View History

2023-03-10 10:12:36 +00:00
using System.Collections.Generic;
2023-03-09 19:40:14 +00:00
namespace tlang
{
2023-03-10 04:33:58 +00:00
public class TRootDict : TDictionary
{
IScopeEnvironment env;
public TRootDict(IScopeEnvironment env)
{
this.env=env;
}
public override TObject GetMember(string name)
{
return env.GetVariable(name);
}
public override void SetMember(string name, TObject obj)
{
env.SetVariable(name,obj);
}
public override bool MemberExists(string name)
{
return env.VariableExists(name);
}
}
2023-03-09 19:40:14 +00:00
public class TDictionary : TObject
{
2023-03-09 23:57:16 +00:00
public override bool AsBoolean {get =>true;}
2023-03-09 19:40:14 +00:00
public TDictionary()
{
}
Dictionary<string,TObject> items {get;set;}=new Dictionary<string, TObject>();
2023-03-10 04:33:58 +00:00
public virtual TObject GetMember(string name)
2023-03-09 19:40:14 +00:00
{
if(items.ContainsKey(name))
{
return items[name];
}else{
return TObject.Uninit;
}
}
2023-03-10 04:33:58 +00:00
public virtual void SetMember(string name, TObject obj)
2023-03-09 19:40:14 +00:00
{
if(items.ContainsKey(name))
{
items[name] = obj;
}
else
{
items.Add(name,obj);
}
}
2023-03-10 04:33:58 +00:00
public virtual bool MemberExists(string name)
2023-03-09 19:40:14 +00:00
{
return items.ContainsKey(name);
}
public TObject this[string variable] { get => GetMember(variable); set => SetMember(variable,value); }
}
}