Setup the minimal rewrite wiring to rewrite 'def' to Def

This commit is contained in:
Sameer Rahmani 2021-04-18 18:39:48 +01:00
parent 30c54f9e73
commit 9f90e42bbb
4 changed files with 29 additions and 24 deletions

View File

@ -44,7 +44,7 @@ public:
node value;
Def(reader::LocationRange &loc, llvm::StringRef binding, node &v)
: Expression(loc), binding(binding), value(std::move(v)){};
: Expression(loc), binding(binding), value(v){};
ExprType getType() const;
std::string toString() const;

View File

@ -46,7 +46,6 @@ public:
List(const reader::LocationRange &loc) : Expression(loc){};
List(const reader::LocationRange &loc, node &e);
// List(const reader::LocationRange &loc, llvm::MutableArrayRef<node> elems);
List(const reader::LocationRange &loc, ast elems);
ExprType getType() const;

View File

@ -26,6 +26,7 @@
#include "serene/exprs/def.h"
#include "serene/exprs/symbol.h"
#include "llvm/Support/Casting.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/FormatVariadic.h"
#include <iterator>
@ -37,10 +38,6 @@ List::List(const reader::LocationRange &loc, node &e) : Expression(loc) {
elements.push_back(e);
};
// List::List(const reader::LocationRange &loc, llvm::MutableArrayRef<node>
// elems)
// : Expression(loc), elements(elems.begin(), elems.end()) {};
List::List(const reader::LocationRange &loc, ast elems)
: Expression(loc), elements(elems){};
@ -58,24 +55,34 @@ std::string List::toString() const {
};
maybe_node List::analyze(reader::SemanticContext &ctx) {
// if (!elements.empty()) {
// auto *first = elements[0].get();
if (!elements.empty()) {
auto *first = elements[0].get();
// if (first->getType() == ExprType::Symbol) {
// auto *sym = llvm::dyn_cast<Symbol>(first);
if (first->getType() == ExprType::Symbol) {
auto *sym = llvm::dyn_cast<Symbol>(first);
// if (sym->name == "def") {
// if (auto err = Def::isValid(this)) {
// // Not a valid `def` form
// return Result<node>::Error(std::move(err));
// }
if (sym) {
if (sym->name == "def") {
if (auto err = Def::isValid(this)) {
// Not a valid `def` form
return Result<node>::Error(std::move(err));
}
// auto *binding = llvm::dyn_cast<Symbol>(elements[1].get());
// auto def = make<Def>(binding->name, std::move(elements[2]));
// return Result<node>::Success(std::move(def));
// }
// }
// }
Symbol *binding = llvm::dyn_cast<Symbol>(elements[1].get());
if (!binding) {
llvm_unreachable("Def::isValid should of catch this.");
}
node def = make<Def>(location, binding->name, elements[2]);
return Result<node>::Success(def);
}
}
// TODO: Return an error saying the binding has to be
// a symbol
}
}
return Result<node>::Success(nullptr);
};

View File

@ -39,10 +39,9 @@ exprs::maybe_ast Semantics::analyze(exprs::ast &inputAst) {
auto &node = maybeNode.getValue();
if (node) {
llvm::outs() << "HERE\n" << node->toString() << " n\n";
ast.push_back(std::move(node));
ast.push_back(node);
} else {
ast.push_back(std::move(element));
ast.push_back(element);
}
} else {
Result<exprs::ast>::Error(std::move(maybeNode.getError()));