Add asserts to check for the first element of the list in def and fn forms

This commit is contained in:
Sameer Rahmani 2021-04-24 18:04:04 +01:00
parent 573550ca07
commit 7c35f6a95e
4 changed files with 15 additions and 0 deletions

View File

@ -53,6 +53,9 @@ public:
maybe_node analyze(reader::SemanticContext &);
static bool classof(const Expression *e);
/// Create a Def node out a list. The list should contain the
/// correct `def` form like `(def blah value)`.
static maybe_node make(List *);
~Def() = default;
};

View File

@ -57,6 +57,10 @@ public:
maybe_node analyze(reader::SemanticContext &);
static bool classof(const Expression *e);
/// Creates a function node out of a function definition
/// in a list. the list has to contain the correct definition
/// of a function, for exmaple: `(fn (args1 arg2) body)`
static maybe_node make(List *);
~Fn() = default;
};

View File

@ -54,6 +54,10 @@ maybe_node Def::make(List *list) {
&errors::DefWrongNumberOfArgs, list->elements[0], msg));
}
Symbol *defSym = llvm::dyn_cast<Symbol>(list->elements[0].get());
assert((defSym && defSym->name == "def") &&
"The first element of the list should be a 'def'.");
Symbol *binding = llvm::dyn_cast<Symbol>(list->elements[1].get());
if (!binding) {

View File

@ -56,6 +56,10 @@ maybe_node Fn::make(List *list) {
&errors::FnNoArgsList, list->elements[0], msg));
}
Symbol *fnSym = llvm::dyn_cast<Symbol>(list->elements[0].get());
assert((fnSym && fnSym->name == "fn") &&
"The first element of the list should be a 'fn'.");
List *args = llvm::dyn_cast<List>(list->elements[1].get());
if (!args) {