mirror of
https://github.com/PashaBibko/LX.git
synced 2026-04-04 01:49:05 +00:00
Added support for all basic math operators
This commit is contained in:
@@ -43,9 +43,33 @@ namespace LX::AST
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Generates the IR of the operation //
|
// Generates the IR of the operation //
|
||||||
// TODO: Support other operators other than ADD //
|
llvm::Value* out = nullptr;
|
||||||
// TODO: Make the error actually output information //
|
|
||||||
llvm::Value* out = LLVM.builder.CreateAdd(lhs, rhs);
|
// Creates the correct operation depending on the operand //
|
||||||
|
switch (m_Operand)
|
||||||
|
{
|
||||||
|
case Token::ADD:
|
||||||
|
out = LLVM.builder.CreateAdd(lhs, rhs);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case Token::SUB:
|
||||||
|
out = LLVM.builder.CreateSub(lhs, rhs);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case Token::MUL:
|
||||||
|
out = LLVM.builder.CreateMul(lhs, rhs);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case Token::DIV:
|
||||||
|
out = LLVM.builder.CreateSDiv(lhs, rhs);
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
// TODO: Add an error here
|
||||||
|
out = nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Checks it all went succesfully before returning //
|
||||||
ThrowIf<IRGenerationError>(out == nullptr);
|
ThrowIf<IRGenerationError>(out == nullptr);
|
||||||
return out;
|
return out;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,6 +7,22 @@
|
|||||||
|
|
||||||
namespace LX
|
namespace LX
|
||||||
{
|
{
|
||||||
|
// Util function for working out if a token is a two sided operator //
|
||||||
|
static bool IsTwoSidedOperator(Token::TokenType t)
|
||||||
|
{
|
||||||
|
switch (t)
|
||||||
|
{
|
||||||
|
case Token::ADD:
|
||||||
|
case Token::SUB:
|
||||||
|
case Token::DIV:
|
||||||
|
case Token::MUL:
|
||||||
|
return true;
|
||||||
|
|
||||||
|
default:
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Local struct so everything can be public //
|
// Local struct so everything can be public //
|
||||||
struct Parser
|
struct Parser
|
||||||
{
|
{
|
||||||
@@ -73,7 +89,7 @@ namespace LX
|
|||||||
// TODO: Add more than just add //
|
// TODO: Add more than just add //
|
||||||
if (p.index + 1 < p.len) [[likely]]
|
if (p.index + 1 < p.len) [[likely]]
|
||||||
{
|
{
|
||||||
if (p.tokens[p.index + 1].type == Token::ADD)
|
if (IsTwoSidedOperator(p.tokens[p.index + 1].type))
|
||||||
{
|
{
|
||||||
// Parses the left hand side of the operation //
|
// Parses the left hand side of the operation //
|
||||||
std::unique_ptr<AST::Node> lhs = ParsePrimary(p);
|
std::unique_ptr<AST::Node> lhs = ParsePrimary(p);
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ func main()
|
|||||||
b = 7
|
b = 7
|
||||||
|
|
||||||
int c
|
int c
|
||||||
c = a + b
|
c = a / b
|
||||||
|
|
||||||
return c
|
return c
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user