void NumericComplexImaginaryOperator::apply(LiteralsStack &stack) const {
    if (stack.size() < 1) {
        throw InvalidSyntaxException("Imaginary part operator requires 1 operand");
    }

    LiteralPointer first = stack.top();
    NumericLiteralPointer firstNumeric = dynamic_pointer_cast<NumericLiteral>(first);

    if (!firstNumeric) {
        throw InvalidOperandException(first->toString());
    }

    if (firstNumeric->toString().find("$") == std::string::npos) {
        throw InvalidOperandException(first->toString());
    }

    stack.pop();

    stack.pushAndNotify(LiteralPointer(new NumericLiteral(
        firstNumeric->getImaginaryNumerator(),
        firstNumeric->getImaginaryDenominator()
    )));

    stack.save();
}
Esempio n. 2
0
void StackDropOperator::apply(LiteralsStack &stack) const {
    if (stack.size() == 0) {
        throw InvalidSyntaxException("Stack empty");
    }

    stack.popAndNotify();
    stack.save();
}
Esempio n. 3
0
Node *Parser::parse(const QString &str)
{
    QStringList tokens;

    if (Tokenizer().tokenize(str, tokens)) {
        return SyntaxTree().buildTree(tokens);
    } else {
        throw InvalidSyntaxException();
    }
}
Esempio n. 4
0
LiteralVector Lexer::tokenize(string command) const {
    // Vector of found tokens
    LiteralVector tokens;

    // Initialization of variables
    LiteralDefinitionVector::const_iterator iterator;
    smatch matchs;
    string match;
    regex pattern;
    bool definitionFound;

    // While the command is not fully parsed
    while (command.size() > 0) {
        definitionFound = false;

        // We iterate over each literal definition trying to find one matching
        for (iterator = definitions.begin(); !definitionFound && iterator != definitions.end(); ++iterator) {
            // The pattern is defined by the LiteralDefinition object.
            // We try to match if at the beginning of the command: we use ^ to do so.
            // We also want to remove the matched string: we catch it using the parenthesis.
            pattern = regex("^(" + (*iterator)->getPattern() + ")");

            // If we found the right literal definition, we build a literal using it
            if (regex_search(command, matchs, pattern)) {
                match = matchs[1].str();

                // We remove the matched element from the command
                command = command.substr(match.length());

                // We create a literal using the definition
                tokens.push_back((*iterator)->createInstance(match));
                cout << (*iterator)->createInstance(match)->toString() << endl;

                // Stop the for here
                definitionFound = true;
            }
        }

        if (!definitionFound) {
            // We found not literal definition matching the given string: invalid synthax
            throw InvalidSyntaxException(command);
        }
    }

    return tokens;
}
Esempio n. 5
0
void StoOperator::apply(LiteralsStack &stack) const {
    if (stack.size() < 2) {
        throw InvalidSyntaxException("STO operator requires 2 operands");
    }

    LiteralPointer first = stack.top();
    stack.pop();

    LiteralPointer second = stack.top();
    stack.pop();

    ExpressionLiteralPointer firstExpression = dynamic_pointer_cast<ExpressionLiteral>(first);

    if (!firstExpression) {
        throw InvalidOperandException(first->toString());
    }

    string expectedAtom = first->toString();

    expectedAtom.erase(remove(expectedAtom.begin(),expectedAtom.end(), '\''),expectedAtom.end());

    AtomLiteralDefinition definition;

    if(dynamic_pointer_cast<AtomLiteral>(definition.createInstance(expectedAtom)) == nullptr)
    {
        throw InvalidOperandException(firstExpression->toString());
    }

    ProgramLiteralPointer secondProgram = dynamic_pointer_cast<ProgramLiteral>(second);

    if(!secondProgram) {
        variableMap.setAndNotify(expectedAtom, second);
    } else{
        try {
            LiteralVector secondProgramVector = lexer.tokenize(second->toString());
            programMap.setAndNotify(expectedAtom,secondProgramVector);
        }
        catch (string except) {
            cout << " problem is : " <<  except << endl;
        }

    }

    stack.notify();
}