-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support arbitrary expression as argument to
interval(...)
.
So far we used constructor expressions like `interval(...)` to create *constants* of the given type, and for that we required that the argument was a single atomic value. The result was that these constructor expressions were really more like literals for the corresponding types then "normal" expressions. While that's useful internally, it's confusing for the user. This commits is beginning to change that, using `interval` as the first instance that now supports arbitrary expressions as its argument. In follow-up commits, we'll port other constructor expressions over to that new syntax. To change this without loosing a way to create actual constants (which we need at some places), we implement this in two stages: the parser initially turns such `interval(...)` expressions into `cast` nodes (i.e., `cast<interval>(...)`). Such casts are already supported to create an interval from an integer, so that just works, but it means we don't have constants anymore. To get them back where we can, we extend the normalizer to look at all such casts: if they are just receiving a constant value as their argument, then it substitutes them with a plain ``ctor::Interval`` node-—-which is what we used to have there. That check for "if they just receive a constant value" isn't quite trivial in general. In our case, where we just want to go back to what we had, it's not that difficult, but more generally it would be nice if we had a constant folder that could compute results even for more complex, but constant, expressions. As that's a piece that our AST infrastructure is still missing, this commit starts going that route by adding a new "constant folder" visitor. Right now, it's covers only the simple cases we need here, but it's something we can extend going forward. Addresses #1123. TODO: Not quite sure how to test the substitution as it's only visible inside the AST.
- Loading branch information
Showing
8 changed files
with
133 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// Copyright (c) 2020-2021 by the Zeek Project. See LICENSE for details. | ||
|
||
#include <hilti/ast/ctors/integer.h> | ||
#include <hilti/ast/detail/visitor.h> | ||
#include <hilti/ast/expression.h> | ||
#include <hilti/ast/operators/signed-integer.h> | ||
#include <hilti/ast/operators/real.h> | ||
#include <hilti/ast/types/integer.h> | ||
#include <hilti/base/logger.h> | ||
#include <hilti/base/util.h> | ||
#include <hilti/ast/builder/expression.h> | ||
#include <hilti/compiler/detail/visitors.h> | ||
|
||
using namespace hilti; | ||
|
||
namespace { | ||
|
||
// For now, this is only a very basic constant folder that only covers cases we | ||
// need to turn type constructor expressions coming with a single argument into | ||
// ctor expressions. | ||
struct VisitorConstantFolder : public visitor::PreOrder<Ctor, VisitorConstantFolder> { | ||
result_t operator()(const expression::Ctor& n, position_t p) { return n.ctor(); } | ||
|
||
result_t operator()(const operator_::signed_integer::SignNeg& n, position_t p) { | ||
if ( auto op = detail::foldConstant<ctor::SignedInteger>(n.op1()) ) | ||
return ctor::SignedInteger(- op->value(), op->width(), op->meta()); | ||
else | ||
return {}; | ||
} | ||
|
||
result_t operator()(const operator_::real::SignNeg& n, position_t p) { | ||
if ( auto op = detail::foldConstant<ctor::Real>(n.op1()) ) | ||
return ctor::Real(- op->value(), op->meta()); | ||
else | ||
return {}; | ||
} | ||
}; | ||
|
||
} // anonymous namespace | ||
|
||
Result<Ctor> detail::foldConstant(const Expression& expr) { | ||
auto v = VisitorConstantFolder(); | ||
|
||
if ( auto ctor = v.dispatch(expr) ) | ||
return *ctor; | ||
else | ||
return result::Error("not a foldable constant expression"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
### BTest baseline data generated by btest-diff. Do not edit. Use "btest -U/-u" to update. Requires BTest >= 0.63. | ||
[error] <...>/ctor-out-of-range.spicy:7:12: value cannot be represented as an interval | ||
[error] <...>/ctor-out-of-range.spicy:8:12: value cannot be represented as an interval | ||
[error] spicyc: parse error | ||
[error] spicyc: aborting after errors |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters