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

176 lines
5.4 KiB
C#

using System;
using System.Collections.Generic;
using System.IO;
namespace TLang.VM
{
public class RootEnvironment : TLangEnvironment
{
public RootEnvironment()
{
}
public Dictionary<string, LoadedClassData> AvailableClasses {get;set;}=new Dictionary<string, LoadedClassData>();
Dictionary<string,TObject> items = new Dictionary<string, TObject>();
public override TObject GetObject(string key)
{
if(HasObject(key))
{
return items[key];
}
return new TUndefined();
}
public override bool HasObject(string key)
{
return items.ContainsKey(key);
}
public override TLangEnvironment GetParentEnvironment()
{
return this;
}
public override RootEnvironment GetRootEnvironment()
{
return this;
}
public override void SetObject(string key, TObject value)
{
items[key] = value;
}
public override bool HasObjectRecurse(string key)
{
return HasObject(key);
}
public override TLangEnvironment GetSubEnvironment()
{
return new SubEnvironment(this);
}
public void LoadStd()
{
SetObject("array",new TExternalMethod((e)=>{
TArray array=new TArray();
if(e.Length > 0)
{
var n = e[0] as TNumber;
if(n != null) array.Items=new List<TObject>((int)n.Value);
}
return array;
}));
SetObject("arrayof",new TExternalMethod((e)=>{
TArray array=new TArray();
foreach(var item in e) array.Add(item);
return array;
}));
SetObject("dictionary",new TExternalMethod(e=>{
return new TDictionary();
}));
SetObject("toNumber",new TExternalMethod(e=>{
if(double.TryParse(e[0].ToString(),out var res))
{
return new TNumber(res);
}
return new TNumber(0);
}));
SetObject("typeof",new TExternalMethod(e=>{
if(e.Length > 0)
{
return new TString(e[0].Type);
}
return new TString("");
}));
SetObject("con",Con());
SetObject("fs",Filesystem());
}
private TObject Con()
{
TDictionary con=new TDictionary();
con.SetValue("println",new TExternalMethod((e)=>{
Console.WriteLine(e[0].ToString());
return new TNull();
}));
con. SetValue("print",new TExternalMethod((e)=>{
Console.Write(e[0].ToString());
return new TNull();
}));
con.SetValue("readln",new TExternalMethod((e)=>{
return new TString(Console.ReadLine());
}));
return con;
}
private TObject Filesystem()
{
TDictionary fs=new TDictionary();
fs.SetValue("mkdir",new TExternalMethod((e)=>{
if(e.Length > 0)
{
return new TBool(Directory.CreateDirectory(e[0].ToString()).Exists);
}
return new TBool(false);
}));
fs.SetValue("directoryExists",new TExternalMethod((e)=>{
if(e.Length > 0)
{
return new TBool(Directory.Exists(e[0].ToString()));
}
return new TBool(false);
}));
fs.SetValue("fileExists",new TExternalMethod((e)=>{
if(e.Length > 0)
{
return new TBool(File.Exists(e[0].ToString()));
}
return new TBool(false);
}));
fs.SetValue("readAllText",new TExternalMethod((e)=>{
if(e.Length > 0)
{
return new TString(File.ReadAllText(e[0].ToString()));
}
return new TNull();
}));
fs.SetValue("writeAllText",new TExternalMethod((e)=>{
if(e.Length > 1)
{
File.WriteAllText(e[0].ToString(),e[1].ToString());
}
return new TNull();
}));
fs.SetValue("getFiles",new TExternalMethod((e)=>{
TArray array=new TArray();
if(e.Length > 0)
{
foreach(var item in Directory.GetFiles(e[0].ToString()))
{
array.Items.Add(new TString(Path.GetFileName(item)));
}
}
return array;
}));
fs.SetValue("getDirectories",new TExternalMethod((e)=>{
TArray array=new TArray();
if(e.Length > 0)
{
foreach(var item in Directory.GetDirectories(e[0].ToString()))
{
array.Items.Add(new TString(Path.GetFileName(item)));
}
}
return array;
}));
return fs;
}
}
}