129 lines
3.9 KiB
C#
129 lines
3.9 KiB
C#
namespace tlang;
|
|
class Program
|
|
{
|
|
static int Main(string[] args)
|
|
{
|
|
|
|
//Lexer lexer = new Lexer("testString.tlang");
|
|
Parser parser = new Parser();
|
|
//parser.Add(lexer.Tokens);
|
|
int r=0;
|
|
RootEnvironment env=new RootEnvironment();
|
|
parser.LoadEnvironment(env);
|
|
env["array"] = new TExternalMethod((args2)=>{
|
|
return new TArray();
|
|
});
|
|
env["dict"] = new TExternalMethod((args2)=>{
|
|
return new TDictionary();
|
|
});
|
|
env["print"] = new TExternalMethod((args2)=>{
|
|
foreach(var arg in args2)
|
|
{
|
|
Console.Write(arg);
|
|
}
|
|
return new TNumber(args2.Length);
|
|
});
|
|
env["println"] = new TExternalMethod((args2)=>{
|
|
foreach(var arg in args2)
|
|
{
|
|
Console.Write(arg);
|
|
}
|
|
Console.WriteLine();
|
|
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) as TNumber;
|
|
var main = env["main"] as ICallable;
|
|
|
|
if(main != null)
|
|
{
|
|
List<TString> args2=new List<TString>();
|
|
foreach(var argument in args)
|
|
{
|
|
args2.Add(new TString(argument));
|
|
}
|
|
res=main.Call(args2.ToArray()) as TNumber;
|
|
}
|
|
|
|
if(res != null)
|
|
{
|
|
return (int)res.Value;
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
}
|