tlang-interperter-cs/tlanglib/Parser.cs

1119 lines
41 KiB
C#
Raw Normal View History

2023-03-10 10:12:36 +00:00
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Sockets;
using System.Threading;
2023-03-09 19:40:14 +00:00
namespace tlang
{
internal class Parser
{
2023-03-10 04:33:58 +00:00
public Parser(List<LexToken> tokens)
2023-03-09 19:40:14 +00:00
{
2023-03-10 04:33:58 +00:00
int i=0;
Node = ParseExpression(tokens,ref i,true);
2023-03-09 19:40:14 +00:00
}
2023-03-10 04:33:58 +00:00
public Node Node {get;set;}
2023-03-09 19:40:14 +00:00
2023-03-10 04:33:58 +00:00
public TObject Execute(IScopeEnvironment env,params TObject[] args)
{
var res=Node.Execute(env);
var main = env["main"] as ICallable;
if(main != null)
{
res=main.Call(args);
}
return res;
}
private Node ParseExpression(List<LexToken> tokens,ref int i,bool isFirst=false)
2023-03-09 19:40:14 +00:00
{
2023-03-09 23:57:16 +00:00
if(i>= tokens.Count) return new ConstNode(new TNumber(0));
2023-03-09 19:40:14 +00:00
if(tokens[i].Text == "{" || isFirst)
{
if(!isFirst)
i++;
2023-03-09 23:57:16 +00:00
ScopeNode scopeNode = new ScopeNode(){First=isFirst};
2023-03-09 19:40:14 +00:00
while(i<tokens.Count && tokens[i].Text != "}")
{
scopeNode.Body.Add(ParseExpression(tokens,ref i));
if(i<tokens.Count && tokens[i].Text == ";")
{
i++;
}
}
i++;
return scopeNode;
}else{
return ParseAssignable(tokens,ref i);
}
}
private Node ParseAssignable(List<LexToken> tokens,ref int i)
{
2023-03-10 10:12:36 +00:00
Node myExpression = ParseLOR(tokens,ref i);
2023-03-09 19:40:14 +00:00
while(i<tokens.Count && (tokens[i].Text == "="))
{
if(i<tokens.Count && tokens[i].Text == "=")
{
i++;
2023-03-10 04:33:58 +00:00
var gvar = myExpression as GetVariableValue;
2023-03-09 19:40:14 +00:00
if(gvar != null)
{
2023-03-10 10:12:36 +00:00
myExpression= new SetVariableNode(gvar,ParseLOR(tokens,ref i));
2023-03-09 23:57:16 +00:00
2023-03-09 19:40:14 +00:00
}
}
}
return myExpression;
}
2023-03-10 10:12:36 +00:00
private Node ParseLOR(List<LexToken> tokens,ref int i)
{
Node myExpression = ParseLAnd(tokens,ref i);
while(i<tokens.Count && tokens[i].Text == "||")
{
i++;
myExpression = new LogicalOrNode(myExpression,ParseLAnd(tokens,ref i));
}
return myExpression;
}
private Node ParseLAnd(List<LexToken> tokens,ref int i)
{
Node myExpression = ParseBOr(tokens,ref i);
while(i<tokens.Count && tokens[i].Text == "&&")
{
i++;
myExpression = new LogicalAndNode(myExpression,ParseBOr(tokens,ref i));
}
return myExpression;
}
private Node ParseBOr(List<LexToken> tokens,ref int i)
{
Node myExpression = ParseXOr(tokens,ref i);
while(i<tokens.Count && tokens[i].Text == "|")
{
i++;
myExpression = new BitwiseOrNode(myExpression,ParseXOr(tokens,ref i));
}
return myExpression;
}
private Node ParseXOr(List<LexToken> tokens,ref int i)
{
Node myExpression = ParseBAnd(tokens,ref i);
while(i<tokens.Count && tokens[i].Text == "^")
{
i++;
myExpression = new XOrNode(myExpression,ParseBAnd(tokens,ref i));
}
return myExpression;
}
private Node ParseBAnd(List<LexToken> tokens,ref int i)
{
Node myExpression = ParseEq(tokens,ref i);
while(i<tokens.Count && tokens[i].Text == "&")
{
i++;
myExpression = new BitwiseAndNode(myExpression,ParseEq(tokens,ref i));
}
return myExpression;
}
private Node ParseEq(List<LexToken> tokens,ref int i)
{
Node myExpression = ParseRo(tokens,ref i);
while(i<tokens.Count && (tokens[i].Text == "!=" || tokens[i].Text == "=="))
{
if(i<tokens.Count && tokens[i].Text == "!="){
i++;
myExpression = new NotEqualsNode(myExpression,ParseRo(tokens,ref i));
}
if(i<tokens.Count && tokens[i].Text == "==")
{
i++;
myExpression = new EqualsNode(myExpression,ParseRo(tokens,ref i));
}
}
return myExpression;
}
private Node ParseRo(List<LexToken> tokens,ref int i)
{
Node myExpression = ParseShift(tokens,ref i);
while(i<tokens.Count && (tokens[i].Text == "<=" || tokens[i].Text == ">=" || tokens[i].Text == "<" || tokens[i].Text == ">"))
{
if(i<tokens.Count && tokens[i].Text == "<="){
i++;
myExpression = new LessThanEqualsNode(myExpression,ParseShift(tokens,ref i));
}
if(i<tokens.Count && tokens[i].Text == ">=")
{
i++;
myExpression = new GreaterThanEqualsNode(myExpression,ParseShift(tokens,ref i));
}
if(i<tokens.Count && tokens[i].Text == "<"){
i++;
myExpression = new LessThanNode(myExpression,ParseShift(tokens,ref i));
}
if(i<tokens.Count && tokens[i].Text == ">")
{
i++;
myExpression = new GreaterThanNode(myExpression,ParseShift(tokens,ref i));
}
}
return myExpression;
}
private Node ParseShift(List<LexToken> tokens,ref int i)
{
Node myExpression = ParseSum(tokens,ref i);
while(i<tokens.Count && (tokens[i].Text == "<<" || tokens[i].Text == ">>"))
{
if(i<tokens.Count && tokens[i].Text == "<<"){
i++;
myExpression = new LeftShiftNode(myExpression,ParseSum(tokens,ref i));
}
if(i<tokens.Count && tokens[i].Text == ">>")
{
i++;
myExpression = new RightShiftNode(myExpression,ParseSum(tokens,ref i));
}
}
return myExpression;
}
2023-03-09 19:40:14 +00:00
private Node ParseSum(List<LexToken> tokens,ref int i)
{
Node myExpression = ParseFactor(tokens,ref i);
while(i<tokens.Count && (tokens[i].Text == "+" || tokens[i].Text == "-"))
{
if(i<tokens.Count && tokens[i].Text == "+"){
i++;
myExpression = new AddNode(myExpression,ParseFactor(tokens,ref i));
}
if(i<tokens.Count && tokens[i].Text == "-")
{
i++;
myExpression = new SubtractNode(myExpression,ParseFactor(tokens,ref i));
}
}
return myExpression;
}
private Node ParseValue(List<LexToken> tokens,ref int i)
{
var token=tokens[i++];
if(token.IsString) return new ConstNode(new TString(token.Text));
if(token.IsChar)
{
2023-03-09 23:57:16 +00:00
return new ConstNode(new TChar(token.Text.SingleOrDefault()));
2023-03-09 19:40:14 +00:00
}
long val;
if(long.TryParse(token.Text,out val))
{
if(i<tokens.Count && tokens[i].Text == "." && !tokens[i].IsChar && !tokens[i].IsString)
{
i++;
if(i<tokens.Count)
{
var token2=tokens[i++];
double dblR;
if(double.TryParse($"{val}.{token2.Text}",out dblR))
{
return new ConstNode(new TNumber(dblR));
}
}
}else{
return new ConstNode(new TNumber( val ));
}
}
else {
if(i<tokens.Count && tokens[i].Text=="(")
{
if(token.Text == "func")
{
List<string> args = new List<string>();
i++;
while(i<tokens.Count && tokens[i].Text != ")")
{
if(tokens[i].Text == ",") {i++;continue;}
if(i<tokens.Count)
{
var exp = ParseExpression(tokens,ref i);
var gvExp = exp as GetVariableValue;
if(gvExp != null)
{
args.Add(gvExp.Name);
}else{
throw new Exception("not a getvariablenode");
}
}
}
i++;
return new ClosureNode(args,ParseExpression(tokens,ref i));
2023-03-09 23:57:16 +00:00
}
else if(token.Text == "for")
{
i++;
//init;condition;inc)
Node init = new ConstNode(new TUninit());
Node condition = new ConstNode(new TUninit());
Node inc = new ConstNode(new TUninit());
Node body = new ConstNode(new TUninit());
if(i<tokens.Count && (tokens[i].Text != ")" || tokens[i].Text != ";"))
{
init = ParseExpression(tokens,ref i);
}
if(i<tokens.Count && tokens[i].Text == ";")
i++;
if(i<tokens.Count && (tokens[i].Text != ")" || tokens[i].Text != ";"))
{
condition = ParseExpression(tokens,ref i);
}
if(i<tokens.Count && tokens[i].Text == ";")
i++;
if(i<tokens.Count && (tokens[i].Text != ")" || tokens[i].Text != ";"))
{
inc = ParseExpression(tokens,ref i);
}
if(i<tokens.Count && tokens[i].Text == ")")
i++;
if(i<tokens.Count)
body = ParseExpression(tokens,ref i);
return new ForLoop(init,condition,inc,body);
}
else if(token.Text == "each")
{
i++;
Node ls = ParseExpression(tokens,ref i);
2023-03-10 04:33:58 +00:00
var name = new GetVariableValue("item");
2023-03-09 23:57:16 +00:00
if(i<tokens.Count && tokens[i].Text == ":")
{
i++;
var n=ls as GetVariableValue;
if(n != null) name = n;
ls = ParseExpression(tokens,ref i);
}
i++;
if(i<tokens.Count)
{
return new EachLoop(name,ls,ParseExpression(tokens,ref i));
}
}
else if(token.Text == "while")
{
i++;
var condition=ParseExpression(tokens,ref i);
i++;
Node yes = new ConstNode(new TUninit());
if(i<tokens.Count && tokens[i].Text != ";")
yes = ParseExpression(tokens,ref i);
return new WhileLoop(condition,yes,false);
}
else if(token.Text == "do")
{
i++;
var condition=ParseExpression(tokens,ref i);
i++;
Node yes = new ConstNode(new TUninit());
if(i<tokens.Count && tokens[i].Text != ";")
yes = ParseExpression(tokens,ref i);
return new WhileLoop(condition,yes,true);
}
else if(token.Text == "if")
2023-03-09 19:40:14 +00:00
{
i++;
var condition=ParseExpression(tokens,ref i);
i++;
Node yes = new ConstNode(new TUninit());
Node no = new ConstNode(new TUninit());
if(i<tokens.Count && tokens[i].Text != "else")
yes=ParseExpression(tokens,ref i);
if(i<tokens.Count && tokens[i].Text == "else")
{
i++;
no = ParseExpression(tokens,ref i);
}
return new IfNode(condition,yes,no);
}
else{
i++;
List<Node> args = new List<Node>();
//function call baby
while(i<tokens.Count && tokens[i].Text != ")")
{
if(tokens[i].Text == ",") {i++;continue;}
if(i<tokens.Count)
{
args.Add(ParseExpression(tokens,ref i));
}
}
i++;
2023-03-09 23:57:16 +00:00
Node ret = new FunctionCallNode(token.Text,args);
while(i<tokens.Count && tokens[i].Text == "[")
{
i++;
var arg = ParseExpression(tokens,ref i);
i++;
ret = new ArraySubscriptNode(ret,token.Text,arg);
}
2023-03-09 19:40:14 +00:00
while(i<tokens.Count && tokens[i].Text == ".")
{
i++;
var token2 = tokens[i++];
if(i<tokens.Count && tokens[i].Text == "(")
{
i++;
args = new List<Node>();
2023-03-09 23:57:16 +00:00
//function call baby
2023-03-09 19:40:14 +00:00
while(i<tokens.Count && tokens[i].Text != ")")
{
if(tokens[i].Text == ",") {i++;continue;}
if(i<tokens.Count)
{
args.Add(ParseExpression(tokens,ref i));
}
}
i++;
ret = new MemberFunctionCallNode(ret,token2.Text,args);
}
else if(i<tokens.Count && tokens[i].Text == "[")
{
2023-03-09 23:57:16 +00:00
i++;
var arg = ParseExpression(tokens,ref i);
i++;
ret = new ArraySubscriptNode(ret,token2.Text,arg);
2023-03-09 19:40:14 +00:00
}
else{
2023-03-09 23:57:16 +00:00
ret = new MemberGetVariableValue(ret,token2.Text);
2023-03-09 19:40:14 +00:00
}
}
2023-03-09 23:57:16 +00:00
if(i<tokens.Count && tokens[i].Text == "++")
{
var mgvn = ret as GetVariableValue;
i++;
if(mgvn != null)
{
return new PostfixIncrementVariableNode(mgvn);
}
2023-03-09 19:40:14 +00:00
2023-03-09 23:57:16 +00:00
}
if(i<tokens.Count && tokens[i].Text == "--")
{
var mgvn = ret as GetVariableValue;
i++;
if(mgvn != null)
{
return new PostfixDecrementVariableNode(mgvn);
}
}
2023-03-09 19:40:14 +00:00
return ret;
}
}else{
2023-03-09 23:57:16 +00:00
Node ret;
2023-03-09 19:40:14 +00:00
switch(token.Text)
{
case "null":
2023-03-09 23:57:16 +00:00
ret= new ConstNode(TObject.Null);
break;
2023-03-09 19:40:14 +00:00
case "undefined":
2023-03-09 23:57:16 +00:00
ret= new ConstNode(TObject.Uninit);
break;
2023-03-09 19:40:14 +00:00
case "true":
2023-03-09 23:57:16 +00:00
ret= new ConstNode(new TNumber(1));
break;
2023-03-09 19:40:14 +00:00
case "false":
2023-03-09 23:57:16 +00:00
ret= new ConstNode(new TNumber(0));
break;
2023-03-09 19:40:14 +00:00
default:
2023-03-09 23:57:16 +00:00
ret= new GetVariableValue(token.Text);
break;
}
while(i<tokens.Count && tokens[i].Text == "[")
{
i++;
var arg = ParseExpression(tokens,ref i);
i++;
ret = new ArraySubscriptNode(ret,token.Text,arg);
2023-03-09 19:40:14 +00:00
}
2023-03-09 23:57:16 +00:00
while(i<tokens.Count && tokens[i].Text == ".")
{
i++;
var token2 = tokens[i++];
if(i<tokens.Count && tokens[i].Text == "(")
{
i++;
var args = new List<Node>();
//function call baby
while(i<tokens.Count && tokens[i].Text != ")")
{
if(tokens[i].Text == ",") {i++;continue;}
if(i<tokens.Count)
{
args.Add(ParseExpression(tokens,ref i));
}
}
i++;
ret = new MemberFunctionCallNode(ret,token2.Text,args);
}
else if(i<tokens.Count && tokens[i].Text == "[")
{
i++;
var arg = ParseExpression(tokens,ref i);
i++;
ret = new ArraySubscriptNode(ret,token2.Text,arg);
}
else{
ret = new MemberGetVariableValue(ret,token2.Text);
}
}
if(i<tokens.Count && tokens[i].Text == "++")
{
var mgvn = ret as GetVariableValue;
i++;
if(mgvn != null)
{
return new PostfixIncrementVariableNode(mgvn);
}
}
if(i<tokens.Count && tokens[i].Text == "--")
{
var mgvn = ret as GetVariableValue;
i++;
if(mgvn != null)
{
return new PostfixDecrementVariableNode(mgvn);
}
}
return ret;
2023-03-09 19:40:14 +00:00
}
}
throw new Exception();
}
private Node ParseFactor(List<LexToken> tokens,ref int i)
{
Node myExpression = ParseValue(tokens,ref i);
while(i<tokens.Count && (tokens[i].Text == "*" || tokens[i].Text == "/" || tokens[i].Text == "%"))
{
if(i<tokens.Count && tokens[i].Text == "*"){
i++;
myExpression = new MultiplyNode(myExpression,ParseValue(tokens,ref i));
}
if(i<tokens.Count && tokens[i].Text == "/")
{
i++;
myExpression = new DivideNode(myExpression,ParseValue(tokens,ref i));
}
if(i<tokens.Count && tokens[i].Text == "%")
{
i++;
myExpression = new ModuloNode(myExpression,ParseValue(tokens,ref i));
}
}
return myExpression;
}
2023-03-10 04:33:58 +00:00
private TObject CreateStream(Stream strm)
{
TDictionary dict = new TDictionary();
dict["setposition"] = new TExternalMethod((args2)=>{
if(args2.Length ==1)
{
var pos = args2[0] as TNumber;
if(pos != null)
{
strm.Position = (long)pos.Value;
}
}
return TObject.Null;
});
dict["getposition"] = new TExternalMethod((args2)=>{
return new TNumber(strm.Position);
});
dict["getlength"] = new TExternalMethod((args2)=>{
return new TNumber(strm.Length);
});
dict["getcount"] = new TExternalMethod((args2)=>{
return new TNumber(strm.Length);
});
dict["canread"] = new TExternalMethod((args2)=>{
return new TNumber(strm.CanRead ? 1 : 0);
});
dict["canwrite"] = new TExternalMethod((args2)=>{
return new TNumber(strm.CanWrite ? 1 : 0);
});
dict["canseek"] = new TExternalMethod((args2)=>{
return new TNumber(strm.CanSeek ? 1 : 0);
});
dict["write"] = new TExternalMethod((args2)=>{
if(args2.Length >= 2)
{
var bufAr = args2[0] as TArray;
2023-03-09 23:57:16 +00:00
2023-03-10 04:33:58 +00:00
if(args2.Length == 3)
{
//buffer,offset,len
var _offset = args2[1] as TNumber;
var _len = args2[2] as TNumber;
if(bufAr != null && _offset != null && _len != null)
{
int count = (int)_len.Value;
int offset = (int)_offset.Value;
int totalRead = Math.Min(bufAr.Items.Count - offset,count);
byte[] buffer = new byte[totalRead];
for(int i = 0;i<totalRead;i++)
{
var item = bufAr.Items[i+offset];
var c = item as TChar;
var n = item as TNumber;
if(c != null) buffer[i] = (byte)c.Value;
if(n != null) buffer[i] = (byte)n.Value;
}
strm.Write(buffer,0,buffer.Length);
return new TNumber(totalRead);
}
}
else
{
var _len = args2[1] as TNumber;
if(bufAr != null && _len != null)
{
int count = (int)_len.Value;
int totalRead = Math.Min(bufAr.Items.Count,count);
byte[] buffer = new byte[totalRead];
for(int i = 0;i<totalRead;i++)
{
var item = bufAr.Items[i];
var c = item as TChar;
var n = item as TNumber;
if(c != null) buffer[i] = (byte)c.Value;
if(n != null) buffer[i] = (byte)n.Value;
}
strm.Write(buffer,0,buffer.Length);
return new TNumber(totalRead);
}
}
}return new TNumber(0);
});
dict["read"] = new TExternalMethod((args2)=>{
if(args2.Length >= 2)
{
var bufAr = args2[0] as TArray;
if(args2.Length == 3)
{
//buffer,offset,len
var _offset = args2[1] as TNumber;
var _len = args2[2] as TNumber;
if(bufAr != null && _offset != null && _len != null)
{
int count = (int)_len.Value;
int offset = (int)_offset.Value;
int totalRead = Math.Min(bufAr.Items.Count - offset,count);
byte[] buffer = new byte[totalRead];
totalRead=strm.Read(buffer,0,buffer.Length);
for(int i = 0;i<totalRead;i++)
{
bufAr.Items[i+offset] = new TNumber(buffer[i]);
}
return new TNumber(totalRead);
}
}
else
{
var _len = args2[1] as TNumber;
if(bufAr != null && _len != null)
{
int count = (int)_len.Value;
int totalRead = Math.Min(bufAr.Items.Count,count);
byte[] buffer = new byte[totalRead];
totalRead=strm.Read(buffer,0,buffer.Length);
for(int i = 0;i<totalRead;i++)
{
bufAr.Items[i] = new TNumber(buffer[i]);
}
return new TNumber(totalRead);
}
}
}return new TNumber(0);
});
dict["close"] = new TExternalMethod((args2)=>{
strm.Dispose();
return TObject.Uninit;
});
dict["flush"] = new TExternalMethod((args2)=>{
strm.Flush();
return TObject.Uninit;
});
return dict;
}
2023-03-09 23:57:16 +00:00
public void LoadEnvironment(RootEnvironment env)
{
TDictionary fs = new TDictionary();
2023-03-10 04:33:58 +00:00
fs["file_exists"] = new TExternalMethod((args2)=>{
if(args2.Length > 0)
{
var fileName=args2[0].ToString();
if(!string.IsNullOrWhiteSpace(fileName))
{
try{
if(File.Exists(fileName)) return new TNumber(1);
}catch(Exception ex)
{
_=ex;
}
}
}
return new TNumber(0);
});
fs["dir_exists"] = new TExternalMethod((args2)=>{
if(args2.Length > 0)
{
var fileName=args2[0].ToString();
if(!string.IsNullOrWhiteSpace(fileName))
{
try{
if(Directory.Exists(fileName)) return new TNumber(1);
}catch(Exception ex)
{
_=ex;
}
}
}
return new TNumber(0);
});
fs["mkdir"] = new TExternalMethod((args2)=>{
if(args2.Length > 0)
{
var fileName=args2[0].ToString();
if(!string.IsNullOrWhiteSpace(fileName))
{
try{
if(Directory.Exists(fileName)) return new TNumber(2);
Directory.CreateDirectory(fileName);
return new TNumber(1);
}catch(Exception ex)
{
_=ex;
}
}
}
return new TNumber(0);
});
fs["create"] = new TExternalMethod((args2)=>{
if(args2.Length > 0)
{
var fileName=args2[0].ToString();
if(!string.IsNullOrWhiteSpace(fileName))
{
try{
return CreateStream(File.Create(fileName));
}catch(Exception ex){
_=ex;
}
}
}
return TObject.Null;
});
fs["openread"] = new TExternalMethod((args2)=>{
if(args2.Length > 0)
{
var fileName=args2[0].ToString();
if(!string.IsNullOrWhiteSpace(fileName))
{
try{
return CreateStream(File.OpenRead(fileName));
}catch(Exception ex){
_=ex;
}
}
}
return TObject.Null;
});
fs["openwrite"] = new TExternalMethod((args2)=>{
if(args2.Length > 0)
{
var fileName=args2[0].ToString();
if(!string.IsNullOrWhiteSpace(fileName))
{
try{
return CreateStream(File.OpenWrite(fileName));
}catch(Exception ex){
_=ex;
}
}
}
return TObject.Null;
});
fs["append"] = new TExternalMethod((args2)=>{
if(args2.Length > 0)
{
var fileName=args2[0].ToString();
if(!string.IsNullOrWhiteSpace(fileName))
{
try{
return CreateStream(File.Open(fileName,FileMode.Append));
}catch(Exception ex){
_=ex;
}
}
}
return TObject.Null;
});
fs["readalltext"] = new TExternalMethod((args2)=>{
if(args2.Length > 0)
{
var fileName=args2[0].ToString();
if(!string.IsNullOrWhiteSpace(fileName))
{
try{
if(!File.Exists(fileName)) return TObject.Null;
return new TString(File.ReadAllText(fileName));
}catch(Exception ex)
{
_=ex;
}
}
}
return TObject.Null;
});
fs["writealltext"] = new TExternalMethod((args2)=>{
if(args2.Length >= 2)
{
var fileName=args2[0].ToString();
var text = args2[1].ToString();
if(!string.IsNullOrWhiteSpace(fileName) && text != null)
{
try{
File.WriteAllText(fileName,text);
return new TNumber(1);
}catch(Exception ex)
{
_=ex;
}
}
}
return new TNumber(0);
});
2023-03-10 00:01:08 +00:00
fs["home"] = new TString(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile));
2023-03-09 23:57:16 +00:00
fs["enumerate_files"] = new TExternalMethod((args)=>{
if(args.Length > 0)
{
var fileName=args[0].ToString();
if(!string.IsNullOrWhiteSpace(fileName) && Directory.Exists(fileName))
{
var dict = new TDictionary();
dict["getIttr"] = new TExternalMethod((args2)=>{
var enumerable=Directory.EnumerateFiles(fileName).GetEnumerator();
TDictionary dict2 = new TDictionary();
dict2["movenext"] = new TExternalMethod((args3)=>{
return new TNumber(enumerable.MoveNext() ? 1 : 0);
});
dict2["getCurrent"] = new TExternalMethod((args3)=>{
return new TString(enumerable.Current);
});
return dict2;
});
return dict;
}
}
return TObject.Null;
});
fs["enumerate_dirs"] = new TExternalMethod((args)=>{
if(args.Length > 0)
{
var fileName=args[0].ToString();
if(!string.IsNullOrWhiteSpace(fileName) && Directory.Exists(fileName))
{
var dict = new TDictionary();
dict["getIttr"] = new TExternalMethod((args2)=>{
var enumerable=Directory.EnumerateDirectories(fileName).GetEnumerator();
TDictionary dict2 = new TDictionary();
dict2["movenext"] = new TExternalMethod((args3)=>{
return new TNumber(enumerable.MoveNext() ? 1 : 0);
});
dict2["getCurrent"] = new TExternalMethod((args3)=>{
return new TString(enumerable.Current);
});
return dict2;
});
return dict;
}
}
return TObject.Null;
});
env["fs"]=fs;
2023-03-10 04:33:58 +00:00
TDictionary reflection = new TDictionary();
reflection["parse_code"] = new TExternalMethod((args2)=>{
if(args2.Length > 0)
{
var str = args2[0] as TString;
if(str != null)
{
Lexer lexer = new Lexer(new StringReader(str.Value));
try{
Parser _parser=new Parser(lexer.Tokens);
RootEnvironment env2 = new RootEnvironment();
TDictionary rootDict = new TRootDict(env2);
LoadEnvironment(env2);
TDictionary parseCodeVal = new TDictionary();
var argsArray= new TArray();
parseCodeVal["root"] = rootDict;
parseCodeVal["args"] = argsArray;
parseCodeVal["run"] = new TExternalMethod((args4)=>{
//run()
try{
return _parser.Execute(env2,argsArray.Items.ToArray());
}catch(Exception ex)
{
_=ex;
return TObject.Null;
}
});
return parseCodeVal;
}catch(Exception ex)
{
_=ex;
return TObject.Null;
}
}
}
return TObject.Uninit;
});
2023-03-10 10:12:36 +00:00
env["reflection"] = reflection;
2023-03-10 04:33:58 +00:00
var con = new TDictionary();
con["write"] = new TExternalMethod((args2)=>{
foreach(var arg in args2)
{
Console.Write(arg);
}
return new TNumber(args2.Length);
});
con["writeln"] = new TExternalMethod((args2)=>{
foreach(var arg in args2)
{
Console.Write(arg);
}
Console.WriteLine();
return new TNumber(args2.Length);
});
var instance = new TDictionary();
instance["array"] = new TExternalMethod((args2)=>{
return new TArray();
});
instance["dict"] = new TExternalMethod((args2)=>{
return new TDictionary();
});
2023-03-10 10:12:36 +00:00
instance["mutex"] = new TExternalMethod((args2)=>{
TDictionary dictionary=new TDictionary();
Mutex mtx = new Mutex();
dictionary["lock"]=new TExternalMethod((args3)=>{
mtx.WaitOne();
return TObject.Uninit;
});
dictionary["unlock"] = new TExternalMethod((args3)=>{
mtx.ReleaseMutex();
return TObject.Uninit;
});
return dictionary;
});
instance["thread"] = new TExternalMethod((args2)=>{
if(args2.Length > 0)
{
ICallable callable = args2[0] as ICallable;
if(callable !=null){
Thread thread=new Thread(()=>{
callable.Call();
});
thread.Start();
}
}
return TObject.Uninit;
});
2023-03-10 04:33:58 +00:00
env["create"] =instance;
env["console"]=con;
env["int"] = new TExternalMethod((args2)=>{
if(args2.Length == 1)
{
var arg = args2[0];
var sArg = arg as TString;
var dArg = arg as TNumber;
if(sArg != null)
{
long val;
if(long.TryParse(sArg.Value,out val))
{
return new TNumber(val);
}
}
if(dArg != null)
{
return new TNumber(Math.Round(dArg.Value));
}
}
return TObject.Null;
});
2023-03-10 10:12:36 +00:00
TDictionary net = new TDictionary();
net["tcpclient"] = new TExternalMethod((args2)=>{
if(args2.Length > 1)
{
var ipOrFQDN = args2[0] as TString;
var port = args2[1] as TNumber;
if(ipOrFQDN!= null && port != null)
{
var c = new TcpClient(ipOrFQDN.Value,(int)port.Value);
return CreateStream(c.GetStream());
}
}
return TObject.Uninit;
});
net["sslclient"] = new TExternalMethod((args2)=>{
return TObject.Uninit;
});
net["tcpserver"] = new TExternalMethod((args2)=>{
return TObject.Uninit;
});
env["net"] =net;
2023-03-09 23:57:16 +00:00
}
2023-03-09 19:40:14 +00:00
}
}