From 012597b112bf2498f63e028d1eaf1044146562d2 Mon Sep 17 00:00:00 2001 From: Roland Conybeare Date: Mon, 17 Jun 2024 12:26:24 -0400 Subject: [PATCH] xo-expression: + if-expressions --- include/xo/expression/IfExpr.hpp | 67 ++++++++++++++++++++++++++++++ include/xo/expression/exprtype.hpp | 3 ++ src/expression/CMakeLists.txt | 2 +- src/expression/IfExpr.cpp | 20 +++++++++ 4 files changed, 91 insertions(+), 1 deletion(-) create mode 100644 include/xo/expression/IfExpr.hpp create mode 100644 src/expression/IfExpr.cpp diff --git a/include/xo/expression/IfExpr.hpp b/include/xo/expression/IfExpr.hpp new file mode 100644 index 00000000..d60ab1d3 --- /dev/null +++ b/include/xo/expression/IfExpr.hpp @@ -0,0 +1,67 @@ +/** @file IfExpr.hpp + * + * Author: Roland Conybeare + **/ + +#pragma once + +#include "Expression.hpp" +#include +#include +//#include + +namespace xo { + namespace ast { + /** @class IfExpr + * @brief abstract syntax tree for a function definition + * + **/ + class IfExpr : public Expression { + public: + /** @p test test-expression; always execute + * @p when_true then-branch; executes only when test succeeds + * @p when_false else-branch; executes only when test fails + **/ + IfExpr(const ref::rp & test, + const ref::rp & when_true, + const ref::rp & when_false) + : Expression(exprtype::if_expr), + test_{test}, + when_true_{when_true}, + when_false_{when_false} {} + + /** downcast from Expression **/ + static ref::brw from(ref::brw x) { + return ref::brw::from(x); + } + + const ref::rp & test() const { return test_; } + const ref::rp & when_true() const { return when_true_; } + const ref::rp & when_false() const { return when_false_; } + + // ----- Expression ----- + + virtual void display(std::ostream & os) const override; + + private: + /** if: + * (if x y z) + * + * executes x; if true execute y; otherwise execute z + **/ + ref::rp test_; + ref::rp when_true_; + ref::rp when_false_; + }; /*IfExpr*/ + + inline ref::rp + make_if_expr(const ref::rp & test, + const ref::rp & when_true, + const ref::rp & when_false) + { + return new IfExpr(test, when_true, when_false); + } + } /*namespace ast*/ +} /*namespace xo*/ + +/** end IfExpr.hpp **/ diff --git a/include/xo/expression/exprtype.hpp b/include/xo/expression/exprtype.hpp index 91bcfbb5..ee33190e 100644 --- a/include/xo/expression/exprtype.hpp +++ b/include/xo/expression/exprtype.hpp @@ -28,6 +28,8 @@ namespace xo { lambda, /** variable reference **/ variable, + /** if-then-else **/ + if_expr, /** not an expression. comes last, counts entries **/ n_expr @@ -43,6 +45,7 @@ namespace xo { case exprtype::apply: return "apply"; case exprtype::lambda: return "lambda"; case exprtype::variable: return "variable"; + case exprtype::if_expr: return "if_expr"; default: break; } diff --git a/src/expression/CMakeLists.txt b/src/expression/CMakeLists.txt index 0ef351e1..ef5cf6f2 100644 --- a/src/expression/CMakeLists.txt +++ b/src/expression/CMakeLists.txt @@ -6,7 +6,7 @@ set(SELF_SRCS Apply.cpp Lambda.cpp Variable.cpp - #init_reflect.cpp + IfExpr.cpp ) xo_add_shared_library4(${SELF_LIB} ${PROJECT_NAME}Targets ${PROJECT_VERSION} 1 ${SELF_SRCS}) diff --git a/src/expression/IfExpr.cpp b/src/expression/IfExpr.cpp new file mode 100644 index 00000000..a12fbdb7 --- /dev/null +++ b/src/expression/IfExpr.cpp @@ -0,0 +1,20 @@ +/* @file IfExpr.cpp */ + +#include "IfExpr.hpp" +#include "xo/indentlog/print/vector.hpp" + +namespace xo { + namespace ast { + void + IfExpr::display(std::ostream & os) const { + os << ""; + } /*display*/ + } /*namespace ast*/ +} /*namespace xo*/ + + +/* end IfExpr.cpp */