DrakeScript is an embedded scripting language for .NET applications.
Hello world
var context = new Context();
var testScript = context.LoadString(
@"PrintLn(""Hello, world!"");"
);
testScript.Invoke();
Simple function
var context = new Context();
var testScript = context.LoadString(
@"let function add3(a, b, c)
{
return a + b + c;
}
return add3(1, 2, 6);"
);
var result = testScript.Invoke();
Console.WriteLine(result);
Sum of array elements that are numbers
var context = new Context();
var testScript = context.LoadString(
@"let function arraySum(array)
{
let sum = 0;
let i = 0;
loop (lengthof array)
{
let e = array[i];
if (e is Number)
{
sum += e;
}
i += 1;
}
return sum;
}
return arraySum([24, 53, 1, 9, ""foo"", nil]);"
);
var result = testScript.Invoke();
Console.WriteLine(result);
Sum of array elements that are numbers (then-otherwise version)
var context = new Context();
var testScript = context.LoadString(
@"let function arraySum(array)
{
let sum = 0;
let i = 0;
loop (lengthof array)
{
let e = array[i];
sum += e is Number then e otherwise 0;
i += 1;
}
return sum;
}
return arraySum([24, 53, 1, 9, ""foo"", nil]);"
);
var result = testScript.Invoke();
Console.WriteLine(result);
Setting global variable from host application
var context = new Context();
context.SetGlobal("Foo", 5.1);
var testScript = context.LoadString(
@"PrintLn2(Foo);"
);
testScript.Invoke();
Registering global function from host application
var context = new Context();
context.SetGlobal(
"PrintLn2", context.CreateFunction(
(interpreter, location, args, argCount) =>
{
for (var i = 0; i < argCount; i++)
{
Console.Write(args[i].ToString());
}
Console.WriteLine();
return Value.Nil;
},
1 // *MINUMUM* argument count required
)
);
var testScript = context.LoadString(
@"PrintLn2(1, 2, 3);"
);
testScript.Invoke();
Creating table
var context = new Context();
var testScript = context.LoadString(
@"let tableKey = { 1 => [2, 4] };
PrintLn(
{
""foo"" => ""bar"",
""hello"" => 4,
89.2 => 2,
tableKey => 2
}
);"
);
testScript.Invoke();