tlang-runtime-compiler/TLang.VM/TDictionary.cs

50 lines
1.4 KiB
C#

using System;
using System.Collections.Generic;
namespace TLang.VM
{
public class TDictionary : TObject
{
public override string Type => "dictonary";
public override bool True => true;
public Dictionary<string,TObject> Items{get;}=new Dictionary<string, TObject>();
public void SetValue(string key,TObject value)
{
Items[key] = value;
}
public TObject GetValue(string key)
{
if(Items.ContainsKey(key)) return Items[key];
return new TUndefined();
}
public bool HasValue(string key)
{
return Items.ContainsKey(key);
}
public static TDictionary FromIEnumerator(IEnumerator<TObject> enumerator)
{
TDictionary dictionary=new TDictionary();
dictionary.SetValue("movenext",new TExternalMethod(e=>{
return new TBool(enumerator.MoveNext());
}));
dictionary.SetValue("reset",new TExternalMethod(e=>{
try{
enumerator.Reset();
}catch(Exception ex)
{
_=ex;
return new TBool(false);
}
return new TBool(true);
}));
dictionary.SetValue("getcurrent",new TExternalMethod(e=>{
return enumerator.Current;
}));
return dictionary;
}
}
}