Skip to content
Extraits de code Groupes Projets
Vérifiée Valider 95959748 rédigé par Kubat's avatar Kubat
Parcourir les fichiers

WIP: Continue to parse expression bis

parent 66fe5e7b
Aucune branche associée trouvée
Aucune étiquette associée trouvée
1 requête de fusion!25Draft: New Vivy module spec
Pipeline #2822 en échec
...@@ -306,3 +306,21 @@ toString(IrECompOp::OpType op) noexcept ...@@ -306,3 +306,21 @@ toString(IrECompOp::OpType op) noexcept
} }
} }
} }
namespace Vivy::Script
{
IrELogicOp::OpType
IrELogicOp::getOpFromToken(Token tok)
{
}
IrECompOp::OpType
IrECompOp::getOpFromToken(Token tok)
{
}
IrEArithmeticOp::OpType
IrEArithmeticOp::getOpFromToken(Token tok)
{
}
}
...@@ -78,6 +78,7 @@ class IrECompOp : public IrExpression { ...@@ -78,6 +78,7 @@ class IrECompOp : public IrExpression {
public: public:
enum class OpType { LT, GT, LE, GE, EQ, NEQ }; enum class OpType { LT, GT, LE, GE, EQ, NEQ };
static OpType getOpFromToken(Token);
private: private:
const OpType selfOpType; const OpType selfOpType;
...@@ -97,6 +98,7 @@ class IrEArithmeticOp : public IrExpression { ...@@ -97,6 +98,7 @@ class IrEArithmeticOp : public IrExpression {
public: public:
enum class OpType { Add, Sub, Mul, Div, Mod }; enum class OpType { Add, Sub, Mul, Div, Mod };
static OpType getOpFromToken(Token);
private: private:
const OpType selfOpType; const OpType selfOpType;
...@@ -116,6 +118,7 @@ class IrELogicOp : public IrExpression { ...@@ -116,6 +118,7 @@ class IrELogicOp : public IrExpression {
public: public:
enum class OpType { And, Or, Xor }; enum class OpType { And, Or, Xor };
static OpType getOpFromToken(Token);
private: private:
const OpType selfOpType; const OpType selfOpType;
......
...@@ -5,6 +5,27 @@ auto throwUnexpectedToken = [](const Token &tok) { ...@@ -5,6 +5,27 @@ auto throwUnexpectedToken = [](const Token &tok) {
tok.toString()); tok.toString());
}; };
/* <|!!|> The left will be left empty (nullptr) */
auto updateExprWithInfixOperator = [throwUnexpectedToken](IrExpression *&current,
Token tok) -> void {
/* operation -> new current, add old into right */
if (current->isInfixOperator())
throwUnexpectedToken(tok);
current =
(tok.isSimple(TOKEN_LIST_ARITHMETIC_OP))
? dynamic_cast<IrExpression *>(
IrElement::create<IrEArithmeticOp>(nullptr, IrEArithmeticOp::getOpFromToken(tok),
std::move(current), std::move(nullptr)))
: (tok.isSimple(TOKEN_LIST_LOGIC_OP))
? dynamic_cast<IrExpression *>(IrElement::create<IrELogicOp>(
nullptr, IrELogicOp::getOpFromToken(tok), std::move(current), std::move(nullptr)))
: (tok.isSimple(TOKEN_LIST_COMP_OP))
? dynamic_cast<IrExpression *>(IrElement::create<IrECompOp>(
nullptr, IrECompOp::getOpFromToken(tok), std::move(current), std::move(nullptr)))
: nullptr;
};
auto updateExprWithConstExpr = [](IrExpression *&current, Token tok) -> void { auto updateExprWithConstExpr = [](IrExpression *&current, Token tok) -> void {
IrExpression *newLeft = IrElement::create<IrEConstExpr>(current, tok); IrExpression *newLeft = IrElement::create<IrEConstExpr>(current, tok);
/* (... `op` <- currentExpression) newLeft -> currentExpression */ /* (... `op` <- currentExpression) newLeft -> currentExpression */
...@@ -33,8 +54,9 @@ volatile ssize_t expressionScopeLevel = 0; ...@@ -33,8 +54,9 @@ volatile ssize_t expressionScopeLevel = 0;
** parse tokens => new expr, take current, add to left ** parse tokens => new expr, take current, add to left
*/ */
auto parseNextExpression = [&expressionScopeLevel, updateExprWithConstExpr]( auto parseNextExpression = [&expressionScopeLevel, updateExprWithConstExpr, throwUnexpectedToken,
std::vector<Token> *tokens, auto &itSelf) -> IrExpression * { updateExprWithInfixOperator](std::vector<Token> *tokens,
auto &itSelf) -> IrExpression * {
const ssize_t expressionScopeLevelAtLaunchTime = expressionScopeLevel; const ssize_t expressionScopeLevelAtLaunchTime = expressionScopeLevel;
IrExpression *currentExpression = nullptr; IrExpression *currentExpression = nullptr;
...@@ -65,9 +87,29 @@ auto parseNextExpression = [&expressionScopeLevel, updateExprWithConstExpr]( ...@@ -65,9 +87,29 @@ auto parseNextExpression = [&expressionScopeLevel, updateExprWithConstExpr](
updateExprWithConstExpr(currentExpression, firstToken); updateExprWithConstExpr(currentExpression, firstToken);
} }
else { /* End a function call */
// operation -> new current, add old into right else if (firstToken.isSimple(TOKEN_PARENT_RIGHT)) {
expressionScopeLevel = expressionScopeLevel - 1;
return currentExpression;
}
/* New element in a function call */
else if (firstToken.isSimple(TOKEN_COMMA)) {
throw std::runtime_error("Multi-arg function calls not handled");
} }
/* Create arithmetic operation */
else if (firstToken.isSimple(TOKEN_LIST_ARITHMETIC_OP)) {
updateExprWithInfixOperator(currentExpression, firstToken);
}
/* Create logic operation */
else if (firstToken.isSimple(TOKEN_LIST_LOGIC_OP)) {
updateExprWithInfixOperator(currentExpression, firstToken);
}
else
throwUnexpectedToken(firstToken);
break; break;
} }
} }
......
...@@ -61,6 +61,21 @@ namespace Vivy::Script ...@@ -61,6 +61,21 @@ namespace Vivy::Script
#define TOKEN_FUNCTION STRV_STATIC("function") #define TOKEN_FUNCTION STRV_STATIC("function")
#define TOKEN_JOB STRV_STATIC("job") #define TOKEN_JOB STRV_STATIC("job")
#define TOKEN_LIST_LOGIC_OP \
{ \
TOKEN_AND, TOKEN_OR \
}
#define TOKEN_LIST_ARITHMETIC_OP \
{ \
TOKEN_PLUS, TOKEN_MINUS, TOKEN_DIV, TOKEN_TIMES, TOKEN_MOD \
}
#define TOKEN_LIST_COMP_OP \
{ \
TOKEN_EQ, TOKEN_NEQ, TOKEN_LT, TOKEN_LE, TOKEN_GT, TOKEN_GE \
}
#define TOKEN_LIST_BASE_TYPE \ #define TOKEN_LIST_BASE_TYPE \
{ \ { \
TOKEN_BOOL, TOKEN_REAL, TOKEN_INT, TOKEN_COLOR, TOKEN_VOID, TOKEN_STRING \ TOKEN_BOOL, TOKEN_REAL, TOKEN_INT, TOKEN_COLOR, TOKEN_VOID, TOKEN_STRING \
......
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Veuillez vous inscrire ou vous pour commenter