Commit 3060d6c4 authored by Tom Niget's avatar Tom Niget

Fix tuple constant access

parent c9cb938b
...@@ -406,4 +406,14 @@ auto call_sync(auto f) { ...@@ -406,4 +406,14 @@ auto call_sync(auto f) {
} }
} }
namespace typon {
template <auto IDX, typename T> auto constant_get(T &&val) {
if constexpr (requires { std::get<IDX>(std::forward<T>(val)); }) {
return std::get<IDX>(std::forward<T>(val));
} else {
return dot(std::forward<T>(val), oo__getitem__oo)(IDX);
}
}
}; // namespace typon
#endif // TYPON_BUILTINS_HPP #endif // TYPON_BUILTINS_HPP
...@@ -336,14 +336,16 @@ class ExpressionVisitor(NodeVisitor): ...@@ -336,14 +336,16 @@ class ExpressionVisitor(NodeVisitor):
if isinstance(node.type, ClassTypeType): if isinstance(node.type, ClassTypeType):
yield from self.visit_BaseType(node.type.inner_type) yield from self.visit_BaseType(node.type.inner_type)
return return
if isinstance(node.value.type, TupleInstanceType):
if not (isinstance(node.slice, ast.Constant) and isinstance(node.slice.value, int)): if isinstance(node.slice, ast.Constant) and isinstance(node.slice.value, int):
raise NotImplementedError("Tuple subscript with non-constant not handled yet") # when the index is a constant, we special case the emitted code so we can use std::get
yield "std::get<" # if the subscripted object is a tuple because tuples are not subscriptable in C++ because
# the language is statically typed
yield "(co_await typon::constant_get<"
yield str(node.slice.value) yield str(node.slice.value)
yield ">(" yield ">("
yield from self.visit(node.value) yield from self.visit(node.value)
yield ")" yield "))"
return return
# yield "(" # yield "("
# yield from self.visit(node.value) # yield from self.visit(node.value)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment