aboutsummaryrefslogtreecommitdiffstats
path: root/Model/Declaration.php
diff options
context:
space:
mode:
authorIvan Enderlin <ivan.enderlin@hoa-project.net>2013-09-16 17:16:38 +0200
committerIvan Enderlin <ivan.enderlin@hoa-project.net>2013-09-16 17:16:38 +0200
commitd44a853b840c8629e3272a8a552d8e2d72c637fb (patch)
treebb96ca609ebb7caec5b2f5ca7666c009ec5b80b1 /Model/Declaration.php
parent22efd46b04b4e551d3c8fbab812225e7433367f4 (diff)
downloadPraspel-d44a853b840c8629e3272a8a552d8e2d72c637fb.zip
Praspel-d44a853b840c8629e3272a8a552d8e2d72c637fb.tar.gz
Praspel-d44a853b840c8629e3272a8a552d8e2d72c637fb.tar.bz2
Add borrowing variables and \old(…) support.
A borrowing variable represents a link between two variables (kind a redirection). It is useful to create link between pre- and post-state in the contract, such as \old(…).
Diffstat (limited to 'Model/Declaration.php')
-rw-r--r--Model/Declaration.php59
1 files changed, 42 insertions, 17 deletions
diff --git a/Model/Declaration.php b/Model/Declaration.php
index 7d36937..599a651 100644
--- a/Model/Declaration.php
+++ b/Model/Declaration.php
@@ -39,6 +39,11 @@ namespace {
from('Hoa')
/**
+ * \Hoa\Praspel\Exception\Model
+ */
+-> import('Praspel.Exception.Model')
+
+/**
* \Hoa\Praspel\Model\Clause
*/
-> import('Praspel.Model.Clause')
@@ -46,7 +51,12 @@ from('Hoa')
/**
* \Hoa\Praspel\Model\Variable
*/
--> import('Praspel.Model.Variable')
+-> import('Praspel.Model.Variable.~')
+
+/**
+ * \Hoa\Praspel\Model\Variable\Borrowing
+ */
+-> import('Praspel.Model.Variable.Borrowing')
/**
* \Hoa\Iterator\Aggregate
@@ -127,15 +137,42 @@ abstract class Declaration
*/
public function offsetGet ( $offset ) {
- if(false === $this->offsetExists($offset)) {
+ return $this->getVariable($offset);
+ }
+
+ /**
+ * Declare or get a new variable.
+ *
+ * @access public
+ * @param string $name Variable name.
+ * @param bool $borrowing Borrowing variable or not.
+ * @return \Hoa\Praspel\Model\Variable
+ * @throw \Hoa\Praspel\Exception\Model
+ */
+ public function getVariable ( $name, $borrowing = false ) {
+
+ if(true === $borrowing) {
- $variable = new Variable($offset, $this->_let, $this);
+ $out = new Variable\Borrowing($name, $this->_let, $this);
$this->_let = false;
- return $this->_variables[$offset] = $variable;
+ return $out;
}
- return $this->_variables[$offset];
+ if('\old(' === substr($name, 0, 5))
+ throw new \Hoa\Praspel\Exception\Model(
+ 'Cannot declare domains for %s in @%s.',
+ 0, array($name, $this->getName()));
+
+ if(false === $this->offsetExists($name)) {
+
+ $variable = new Variable($name, $this->_let, $this);
+ $this->_let = false;
+
+ return $this->_variables[$name] = $variable;
+ }
+
+ return $this->_variables[$name];
}
/**
@@ -188,18 +225,6 @@ abstract class Declaration
}
/**
- * Alias of $this->offsetGet().
- *
- * @access public
- * @param string $variable Variable name.
- * @return \Hoa\Prasel\Model\Variable
- */
- public function getVariable ( $variable ) {
-
- return $this->offsetGet($variable);
- }
-
- /**
* Iterator over local variables.
*
* @access public