From 39a6977caf6215524ce26ab5c7f0ce7b6775c6b4 Mon Sep 17 00:00:00 2001 From: falsycat Date: Sat, 5 Oct 2019 00:00:00 +0000 Subject: [PATCH] [update] Replaced the NullVarStore with the StandardVarStore. --- sjscript/src/sjscript/calculate.d | 13 +++++++++---- sjscript/src/sjscript/parse.d | 4 ++-- sjscript/test/test01.sj | 4 +--- 3 files changed, 12 insertions(+), 9 deletions(-) diff --git a/sjscript/src/sjscript/calculate.d b/sjscript/src/sjscript/calculate.d index 39c2c2a..327187c 100644 --- a/sjscript/src/sjscript/calculate.d +++ b/sjscript/src/sjscript/calculate.d @@ -19,13 +19,18 @@ enum IsVarStore(T) = static assert(IsVarStore!(float[string])); /// -struct NullVarStore { +struct StandardVarStore { public: static float opIndex(string name) { - // TODO: error handling - throw new Exception("undefined variable %s".format(name)); + switch (name) { + case "PI": return PI; + case "PI_2": return PI_2; + case "PI_4": return PI_4; + case "E": return E; + default: throw new Exception("undefined variable %s".format(name)); + } } - static assert(IsVarStore!NullVarStore); + static assert(IsVarStore!StandardVarStore); } /// diff --git a/sjscript/src/sjscript/parse.d b/sjscript/src/sjscript/parse.d index eb2a033..5900e18 100644 --- a/sjscript/src/sjscript/parse.d +++ b/sjscript/src/sjscript/parse.d @@ -68,8 +68,8 @@ private class RuleSet { Expression end, @(TokenType.CloseBracket) Token) { return Period( - start.CalculateExpression(NullVarStore()), - end .CalculateExpression(NullVarStore())); + start.CalculateExpression(StandardVarStore()), + end .CalculateExpression(StandardVarStore())); } static Parameter[] ParseParameterListFirstItem(Parameter param) { diff --git a/sjscript/test/test01.sj b/sjscript/test/test01.sj index 1ec5d7f..1a0e36c 100644 --- a/sjscript/test/test01.sj +++ b/sjscript/test/test01.sj @@ -1,7 +1,5 @@ // this is comment -$define pi {3.14} - $define sinwave { translate_x = $sinwave_add_x + sin(rtime * $sinwave_hz) * $sinwave_amp_x; translate_y = $sinwave_add_y + cos(rtime * $sinwave_hz) * $sinwave_amp_y; @@ -18,7 +16,7 @@ $define shoot { translate_y += __dir_y; } -$define beat {sin($pi/4)} +$define beat {sin(PI/4)} A [$beat..$beat+10] { $define sinwave_add_x {0.5} $define sinwave_add_y {0.5}