tplgen: Add package

* test/core/suite.xml: Import new tplgen package.
* test/core/tplgen.xml: Add test.
* tplgen.xml: Add package.
master
Mike Gerwitz 2018-01-04 16:06:59 -05:00
parent 2e453ee703
commit 108ada745a
3 changed files with 145 additions and 2 deletions

View File

@ -1,6 +1,6 @@
<?xml version="1.0"?>
<!--
Copyright (C) 2015 R-T Specialty, LLC.
Copyright (C) 2015, 2018 R-T Specialty, LLC.
This file is part of tame-core.
@ -26,12 +26,15 @@
<import package="../../base" />
<import package="../spec" />
<import package="insurance" />
<import package="numeric/round" />
<import package="numeric/convert" />
<import package="numeric/percent" />
<import package="vector/interpolate" />
<import package="vector/length" />
<import package="insurance" />
<import package="tplgen" />
<!-- XXX broken!
<import package="ui" />
-->

View File

@ -0,0 +1,67 @@
<?xml version="1.0"?>
<!--
Copyright (C) 2018 R-T Specialty, LLC.
This file is part of tame-core.
tame-core is free software: you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
-->
<package xmlns="http://www.lovullo.com/rater"
xmlns:c="http://www.lovullo.com/calc"
xmlns:t="http://www.lovullo.com/rater/apply-template"
desc="Template Generation Package Specification">
<import package="../../test/spec" />
<import package="../../base" />
<import package="../../tplgen" />
<t:describe name="_for-each-n_">
<t:describe name="given a positive step">
<t:it desc="produces body N times exposing current N">
<t:given>
<c:sum>
<t:for-each-n start="2" end="6" step="2">
<c:const value="@current_n@" desc="Current N" />
</t:for-each-n>
</c:sum>
</t:given>
<t:expect>
<!-- 2 + 4 + 6 -->
<t:match-result eq="12" />
</t:expect>
</t:it>
</t:describe>
<t:describe name="given a negative step">
<t:it desc="produces body N times exposing current N">
<t:given>
<c:sum>
<t:for-each-n start="4" end="1" step="-1">
<c:const value="@current_n@" desc="Current N" />
</t:for-each-n>
</c:sum>
</t:given>
<t:expect>
<!-- 4 + 3 + 2 + 1 = 4! (4 factorial) -->
<t:match-result eq="10" />
</t:expect>
</t:it>
</t:describe>
</t:describe>
</package>

73
core/tplgen.xml 100644
View File

@ -0,0 +1,73 @@
<?xml version="1.0"?>
<!--
Copyright (C) 2018 R-T Specialty, LLC.
This file is part of tame-core.
tame-core is free software: you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
-->
<package xmlns="http://www.lovullo.com/rater"
xmlns:c="http://www.lovullo.com/calc"
xmlns:t="http://www.lovullo.com/rater/apply-template"
core="true"
desc="Template Generation">
This package provides various templates for generating templates and
iterating using templates.
In essense,
this package is intended to abstract away certain implementation details
and complexities that make groking code more difficult;
they should be used when possible to improve code maintenance.
<section title="Iteration">
\ref{_for-each-n_} recurisvely produces the body~\tt{@values@} given a
numeric range and step.
This can also be used to generate sequences at compile-time rather than
using functions,
provided that the sequence data are static.
<template name="_for-each-n_"
desc="Recursively apply body with counter and step">
<param name="@values@" desc="Body" />
<param name="@start@" desc="Counter start" />
<param name="@end@" desc="Counter end" />
<param name="@step@" desc="Counter step" />
<param name="@next_n@" desc="Next iteration value">
<param-add name="@start@" value="@step@" />
</param>
<!-- inefficient trick to expose @current_n@ to the body -->
<inline-template>
<for-each>
<set current_n="@start@" />
</for-each>
<param-copy name="@values@" />
</inline-template>
<!-- repeat body for each step, as sibling -->
<unless name="@start@" eq="@end@">
<t:for-each-n start="@next_n@" end="@end@" step="@step@">
<param-copy name="@values@" />
</t:for-each-n>
</unless>
</template>
</section>
</package>