tlang-interperter-cs/tlanglib/TDictionary.cs

65 lines
1.5 KiB
C#

using System.Collections.Generic;
namespace tlang
{
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);
}
}
public class TDictionary : TObject
{
public override bool AsBoolean {get =>true;}
public TDictionary()
{
}
Dictionary<string,TObject> items {get;set;}=new Dictionary<string, TObject>();
public virtual TObject GetMember(string name)
{
if(items.ContainsKey(name))
{
return items[name];
}else{
return TObject.Uninit;
}
}
public virtual void SetMember(string name, TObject obj)
{
if(items.ContainsKey(name))
{
items[name] = obj;
}
else
{
items.Add(name,obj);
}
}
public virtual bool MemberExists(string name)
{
return items.ContainsKey(name);
}
public TObject this[string variable] { get => GetMember(variable); set => SetMember(variable,value); }
}
}