tlang-interperter-cs/Program.cs

99 lines
3.1 KiB
C#

namespace tlang;
class Program
{
static void Main(string[] args)
{
//Lexer lexer = new Lexer("testString.tlang");
Parser parser = new Parser();
//parser.Add(lexer.Tokens);
int r=0;
RootEnvironment env=new RootEnvironment();
env["mol"] = new TNumber(42);
env["printargs"] = new TExternalMethod((args2)=>{
foreach(var arg in args2)
{
Console.WriteLine(arg);
}
return new TNumber(args2.Length);
});
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;
});
env["filecreate"]=new TExternalMethod((args2)=>{
if(args2.Length == 1)
{
var fileName = args2[0] as TString;
if(fileName != null)
{
return new TInternalObject(File.Create(fileName.Value));
}
}
return new TNull();
});
env["streamwritestr"]=new TExternalMethod((args2)=>{
if(args2.Length == 2)
{
var internalObj = args2[0] as TInternalObject;
if(internalObj != null)
{
var strm = internalObj.Object as Stream;
if(strm != null)
{
var str = args2[1] as TString;
if(str != null)
{
var bytes=System.Text.Encoding.UTF8.GetBytes(str.Value);
strm.Write(bytes,0,bytes.Length);
}
}
}
}
return TObject.Null;
});
env["streamclose"]=new TExternalMethod((args2)=>{
if(args2.Length == 1)
{
var internalObj = args2[0] as TInternalObject;
if(internalObj != null)
{
var strm = internalObj.Object as Stream;
if(strm != null)
{
strm.Dispose();
}
}
}
return TObject.Null;
});
Lexer lexer =new Lexer(new StreamReader("file.tlang"));
var res=parser.ParseExpression(lexer.Tokens,ref r,true).Execute(env);
r=0;
Console.WriteLine(res);
}
}