[add] Added the func module for calling function.

This commit is contained in:
falsycat 2019-10-05 00:00:00 +00:00
parent ad7e005c7c
commit 1a18f59930
3 changed files with 51 additions and 5 deletions

View File

@ -2,13 +2,15 @@
module sjscript.calculate; module sjscript.calculate;
import std.algorithm, import std.algorithm,
std.array,
std.exception, std.exception,
std.format, std.format,
std.math, std.math,
std.traits, std.traits,
std.variant; std.variant;
import sjscript.Expression; import sjscript.Expression,
sjscript.func;
/// ///
enum IsVarStore(T) = enum IsVarStore(T) =
@ -59,7 +61,6 @@ float CalculateTermValue(VarStore)(in Term.Value value, VarStore vars)
/// ///
float CalculateFunction(VarStore)(in FunctionCall fcall, VarStore vars) float CalculateFunction(VarStore)(in FunctionCall fcall, VarStore vars)
if (IsVarStore!VarStore) { if (IsVarStore!VarStore) {
const args = fcall.args.map!(x => x.CalculateExpression(vars)); return fcall.name.CallFunction(
// TODO: calling function fcall.args.map!(x => x.CalculateExpression(vars)).array);
return 0;
} }

View File

@ -0,0 +1,43 @@
/// License: MIT
module sjscript.func;
import std.algorithm,
std.exception,
std.math;
///
float CallFunction(string name, float[] args) {
void EnforceArgs(size_t len)() {
(args.length == len).enforce("invalid arguments");
}
switch (name) {
// ---- misc
case "abs": EnforceArgs!1; return fabs(args[0]);
case "sqrt": EnforceArgs!1; return sqrt(args[0]);
case "exp": EnforceArgs!1; return exp(args[0]);
case "pow": EnforceArgs!2; return pow(args[0], args[1]);
// ---- triangle
case "sin": EnforceArgs!1; return sin(args[0]);
case "cos": EnforceArgs!1; return cos(args[0]);
case "tan": EnforceArgs!1; return tan(args[0]);
case "asin": EnforceArgs!1; return asin(args[0]);
case "acos": EnforceArgs!1; return acos(args[0]);
case "atan": EnforceArgs!1; return atan(args[0]);
case "atan2": EnforceArgs!2; return atan2(args[0], args[1]);
// ---- rounding
case "ceil": EnforceArgs!1; return ceil(args[0]);
case "floor": EnforceArgs!1; return floor(args[0]);
case "round": EnforceArgs!1; return round(args[0]);
// ---- conditional switch
case "step": EnforceArgs!2; return args[1] > args[0]? 1: 0;
case "clamp": EnforceArgs!3; return clamp(args[0], args[1], args[3]);
default: throw new Exception("unknown exception");
}
}

View File

@ -1,5 +1,7 @@
// this is comment // this is comment
$define pi {3.14}
$define sinwave { $define sinwave {
translate_x = $sinwave_add_x + sin(rtime * $sinwave_hz) * $sinwave_amp_x; translate_x = $sinwave_add_x + sin(rtime * $sinwave_hz) * $sinwave_amp_x;
translate_y = $sinwave_add_y + cos(rtime * $sinwave_hz) * $sinwave_amp_y; translate_y = $sinwave_add_y + cos(rtime * $sinwave_hz) * $sinwave_amp_y;
@ -16,7 +18,7 @@ $define shoot {
translate_y += __dir_y; translate_y += __dir_y;
} }
$define beat {0} $define beat {sin($pi/4)}
A [$beat..$beat+10] { A [$beat..$beat+10] {
$define sinwave_add_x {0.5} $define sinwave_add_x {0.5}
$define sinwave_add_y {0.5} $define sinwave_add_y {0.5}