Add negation unary
This commit is contained in:
parent
43df7efd6f
commit
5b776a1d66
@ -105,9 +105,12 @@ namespace AST {
|
|||||||
case types::Unary::SubPrefix:
|
case types::Unary::SubPrefix:
|
||||||
out << "--" << this->m_expr->formatted();
|
out << "--" << this->m_expr->formatted();
|
||||||
break;
|
break;
|
||||||
case types::Unary::Negation:
|
case types::Unary::Not:
|
||||||
out << "!" << this->m_expr->formatted();
|
out << "!" << this->m_expr->formatted();
|
||||||
break;
|
break;
|
||||||
|
case types::Unary::Negation:
|
||||||
|
out << "-" << this->m_expr->formatted();
|
||||||
|
break;
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -167,10 +167,10 @@ namespace types {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// Negation
|
// Not & Negation
|
||||||
for (auto& ty : { int_ty, char_ty, bool_ty }) {
|
for (auto& ty : { int_ty, char_ty, bool_ty }) {
|
||||||
definitions.push_back(UnopDefinition{
|
definitions.push_back(UnopDefinition{
|
||||||
ty, types::Unary::Negation, ty,
|
ty, types::Unary::Not, ty,
|
||||||
[](codegen::Builder& builder, std::shared_ptr<Type> ty, llvm::Value* value) {
|
[](codegen::Builder& builder, std::shared_ptr<Type> ty, llvm::Value* value) {
|
||||||
codegen::TypeMap structs {};
|
codegen::TypeMap structs {};
|
||||||
auto llvm_ty = ty->codegen(builder, structs);
|
auto llvm_ty = ty->codegen(builder, structs);
|
||||||
@ -181,6 +181,12 @@ namespace types {
|
|||||||
return builder.builder->CreateSelect(cmp, const_1, const_0, "not_select");
|
return builder.builder->CreateSelect(cmp, const_1, const_0, "not_select");
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
definitions.push_back(UnopDefinition{
|
||||||
|
ty, types::Unary::Negation, ty,
|
||||||
|
[](codegen::Builder& builder, std::shared_ptr<Type>, llvm::Value* value) {
|
||||||
|
return builder.builder->CreateNeg(value, "neg");
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
return definitions;
|
return definitions;
|
||||||
|
|||||||
@ -20,6 +20,7 @@ namespace types {
|
|||||||
AddPrefix,
|
AddPrefix,
|
||||||
SubPostfix,
|
SubPostfix,
|
||||||
SubPrefix,
|
SubPrefix,
|
||||||
|
Not,
|
||||||
Negation,
|
Negation,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@ -334,6 +334,14 @@ namespace parsing {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
else if (inner.peek().content == "!") {
|
else if (inner.peek().content == "!") {
|
||||||
|
inner.next();
|
||||||
|
auto expr = parse_primary_expression(inner, scope).unwrap();
|
||||||
|
stream.m_position = inner.m_position;
|
||||||
|
return std::unique_ptr<AST::Expression> {
|
||||||
|
new AST::UnaryExpression(before_meta + inner.metadata(), std::move(expr), types::Unary::Not)
|
||||||
|
};
|
||||||
|
}
|
||||||
|
else if (inner.peek().content == "-") {
|
||||||
inner.next();
|
inner.next();
|
||||||
auto expr = parse_primary_expression(inner, scope).unwrap();
|
auto expr = parse_primary_expression(inner, scope).unwrap();
|
||||||
stream.m_position = inner.m_position;
|
stream.m_position = inner.m_position;
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user