MIXAL
Enumerations | Functions
expression.cpp File Reference

The parsing and evaluation of expressions. More...

#include <cassert>
#include <limits>
#include <iostream>
#include "expression.h"

Go to the source code of this file.

Enumerations

enum  mixal::ExprParseState { mixal::ExprParseState::START, mixal::ExprParseState::ATOMIC, mixal::ExprParseState::OPERATION, mixal::ExprParseState::END }
 

Functions

bool mixal::isOperationFirst (char ch)
 
std::ostream & mixal::operator<< (std::ostream &out, Operation operation)
 
std::ostream & mixal::operator<< (std::ostream &out, const Expression &expression)
 

Detailed Description

The parsing and evaluation of expressions.

Definition in file expression.cpp.

Enumeration Type Documentation

◆ ExprParseState

enum mixal::ExprParseState
strong

The states while parsing the expression.

EXPRESSION -> + EXPRESSION
| - EXPRESSION
| EXPRESSION OPERATOR EXPRESSION
| ATOMIC
OPERATOR -> + | - | * | / | // | :
ATOMIC -> INTEGER
| SYMBOL
| *
INTEGER -> DIGIT
| DIGIT INTEGER
SYMBOL -> ALPHA
| DIGIT
| ALPHS SYMBOL
| DIGIT SYMBOL
DIGIT -> [0-9]
ALPHA -> [A-Z]

However, multiple signs like ++ or --- will be rejected as they are useless.

Definition at line 77 of file expression.cpp.

77  {
78  START,
79  ATOMIC,
80  OPERATION,
81  END,
82 };

Function Documentation

◆ isOperationFirst()

bool mixal::isOperationFirst ( char  ch)
inline

Whether an operation can start with the given character.

Definition at line 85 of file expression.cpp.

85  {
86  return ch == '+' || ch == '-' || ch == '*' || ch == '/' || ch == ':';
87 }

References mixal::isOperationFirst().

Referenced by mixal::isOperationFirst(), and mixal::Expression::parse().

◆ operator<<() [1/2]

std::ostream& mixal::operator<< ( std::ostream &  out,
const Expression expression 
)

Output the expression.

Definition at line 346 of file expression.cpp.

346  {
347  if (expression._atomics.size() > 0) {
348  out << expression._atomics[0];
349  }
350  for (size_t i = 0; i < expression._operations.size(); ++i) {
351  out << expression._operations[i];
352  out << expression._atomics[i + 1];
353  }
354  return out;
355 }

References mixal::operator<<().

◆ operator<<() [2/2]

std::ostream & mixal::operator<< ( std::ostream &  out,
Operation  operation 
)

Output the symbol of the operation.

Definition at line 335 of file expression.cpp.

335  {
336  switch (operation) {
337  case Operation::ADD: out << '+'; break;
338  case Operation::SUBTRACT: out << '-'; break;
339  case Operation::MULTIPLY: out << '*'; break;
340  case Operation::FLOOR_DIV: out << '/'; break;
341  case Operation::FIELD: out << ':'; break;
342  }
343  return out;
344 }

References mixal::operator<<().

Referenced by mixal::operator<<().