better test formatting, etc.

This commit is contained in:
programsnail 2024-04-21 18:00:12 +03:00
parent 06ccf59e45
commit ec04e05b7b
3 changed files with 124 additions and 28 deletions

View file

@ -1,13 +1,14 @@
#pragma once
#include <iostream>
#include <memory>
#include <optional>
#include <string>
#include <variant>
#include <vector>
#include "utils.hpp"
#include "types.hpp"
#include "utils.hpp"
namespace nodes {
@ -22,10 +23,10 @@ using ExprPtr = shared_ptr<Expr>;
using ExprPtrV = std::vector<ExprPtr>;
struct Arg : public NodeInfo {
Arg(string name, types::Mode mode_hint = {}) : name(std::move(name)), mode_hint(mode_hint) {}
Arg(string name, types::Mode mode_hint = {})
: name(std::move(name)), mode_hint(mode_hint) {}
string name;
types::Mode mode_hint;
};
@ -110,6 +111,10 @@ inline ExprPtr lambda1(Arg var, ExprPtr expr) {
return make_expr<Lambda>(vector<Arg>{var}, std::move(expr));
}
inline ExprPtr lambda2(Arg var1, Arg var2, ExprPtr expr) {
return make_expr<Lambda>(vector<Arg>{var1, var2}, std::move(expr));
}
inline ExprPtr operator_call(string name, ExprPtr left, ExprPtr right) {
return make_expr<Call>(make_expr<Var>(name), ExprPtrV{left, right});
}

76
include/printers.hpp Normal file
View file

@ -0,0 +1,76 @@
#pragma once
#include "parsing_tree.hpp"
using namespace nodes;
inline std::ostream &operator<<(std::ostream &, const Expr &);
inline std::ostream &operator<<(std::ostream &out, const Arg &expr) {
out << expr.name << (expr.mode_hint.uniq == types::Mode::Uniq::UNIQUE ? "<unique>" : ""); // TODO: all modes
return out;
}
inline std::ostream &operator<<(std::ostream &out, const Const &expr) {
out << expr.value;
return out;
}
inline std::ostream &operator<<(std::ostream &out, const Var &expr) {
out << expr.name;
return out;
}
inline std::ostream &operator<<(std::ostream &out, const Let &expr) {
out << "let " << expr.name << " = " << *expr.body << " in " << *expr.where;
return out;
}
inline std::ostream &operator<<(std::ostream &out, const Lambda &expr) {
out << "\\";
for (const auto &arg : expr.args) {
out << arg << " ";
}
out << "-> " << *expr.expr;
return out;
}
inline std::ostream &operator<<(std::ostream &out, const Call &expr) {
out << *expr.func;
for (const auto &arg : expr.args) {
out << " " << *arg;
}
return out;
}
inline std::ostream &operator<<(std::ostream &out, const Condition &expr) {
out << "if " << *expr.condition << " then " << *expr.then_case << " else "
<< *expr.else_case;
return out;
}
inline std::ostream &operator<<(std::ostream &out, const Expr &expr) {
switch (expr.value.index()) {
case 0: // Const
out << std::get<0>(expr.value);
break;
case 1: // Var
out << std::get<1>(expr.value);
break;
case 2: // Let
out << std::get<2>(expr.value);
break;
case 3: // Lambda
out << std::get<3>(expr.value);
break;
case 4: // Call
out << std::get<4>(expr.value);
break;
case 5: // Condition
out << std::get<5>(expr.value);
break;
default:
utils::unreachable();
}
return out;
}