81 lines
2.2 KiB
C#
81 lines
2.2 KiB
C#
|
namespace tlang
|
||
|
{
|
||
|
public class TInternalMethod : TObject, ICallable
|
||
|
{
|
||
|
public TInternalMethod(ClosureNode node,IScopeEnvironment env)
|
||
|
{
|
||
|
Body = node;
|
||
|
Environment = env;
|
||
|
}
|
||
|
public ClosureNode Body {get;set;}
|
||
|
public IScopeEnvironment Environment {get;set;}
|
||
|
public TObject Call(params TObject[] args)
|
||
|
{
|
||
|
var env=Environment.SubEnv;
|
||
|
int argCountClosure = Body.Arguments.Count;
|
||
|
int argCountCaller = args.Length;
|
||
|
if(argCountCaller < requiredArguments())
|
||
|
{
|
||
|
throw new Exception("not enough arguments");
|
||
|
}
|
||
|
int i = 0;
|
||
|
for(;i<optionalArgs(args.Length);i++)
|
||
|
{
|
||
|
env[Body.Arguments[i].TrimStart('.')] = args[i];
|
||
|
}
|
||
|
if(i==Body.Arguments.Count-1)
|
||
|
{
|
||
|
var tarray = new TArray();
|
||
|
env[Body.Arguments[i].TrimStart('.')] =tarray;
|
||
|
for(int j = i;j<args.Length;j++)
|
||
|
{
|
||
|
tarray.Items.Add(args[j]);
|
||
|
}
|
||
|
}
|
||
|
return Body.Node.Execute(env);
|
||
|
}
|
||
|
private int optionalArgs(int argLen)
|
||
|
{
|
||
|
for(int i =0;i<Body.Arguments.Count;i++)
|
||
|
{
|
||
|
if(Body.Arguments[i].StartsWith("..."))
|
||
|
{
|
||
|
return Math.Min(argLen,i);
|
||
|
}
|
||
|
}
|
||
|
return Math.Min(argLen,Body.Arguments.Count);
|
||
|
}
|
||
|
private int requiredArguments()
|
||
|
{
|
||
|
for(int i =0;i<Body.Arguments.Count;i++)
|
||
|
{
|
||
|
if(Body.Arguments[i].StartsWith('.'))
|
||
|
{
|
||
|
return i;
|
||
|
}
|
||
|
}
|
||
|
return Body.Arguments.Count;
|
||
|
}
|
||
|
|
||
|
}
|
||
|
public class TExternalMethod : TObject,ICallable
|
||
|
{
|
||
|
Func<TObject[],TObject> cb;
|
||
|
public TExternalMethod(Func<TObject[],TObject> func)
|
||
|
{
|
||
|
cb = func;
|
||
|
}
|
||
|
|
||
|
public TObject Call(params TObject[] args)
|
||
|
{
|
||
|
return cb(args);
|
||
|
}
|
||
|
|
||
|
|
||
|
}
|
||
|
|
||
|
public interface ICallable
|
||
|
{
|
||
|
TObject Call(params TObject[] args);
|
||
|
}
|
||
|
}
|