#include "parser.h"

/**
    TOKEN_VARIABLE,      // x, y, etc.
    TOKEN_NUMBER_INT,    // int
    TOKEN_NUMBER_DOUBLE, // double
    TOKEN_POWER,         // ^ or **
    TOKEN_SIGN,          // + or -
    TOKEN_FACTOR,        // * or /
    TOKEN_EQUAL,         // =
    TOKEN_END            // null (end of input)

    1. VARIABLE | NUMBER_INT | NUMBER_DOUBLE | POWER | SIGN | FACTOR | EQUAL | END
    2. VARIABLE | NUMBER_INT | NUMBER_DOUBLE | POWER | SIGN | FACTOR | EQUAL | END
    3. VARIABLE | NUMBER_INT | NUMBER_DOUBLE | POWER | SIGN | FACTOR | EQUAL | END

    term_position position;
    term_sign sign;
    coefficient_type coefficient_type;
    int coefficient;
    int exponent;
 */

static term_sign get_sign(token *tokens, int *token_count)
{
    if (tokens)           // placeholder
        *token_count = 1; // placeholder
    return '+';           // placeholder
}

static double get_coefficient(token *tokens, int *token_count)
{
    if (tokens)           // placeholder
        *token_count = 1; // placeholder
    return 1.0;           // placeholder
}

static int get_exponent(token *tokens, int *token_count)
{
    if (tokens)           // placeholder
        *token_count = 1; // placeholder
    return 1;             // placeholder
}

int parse(token *tokens, term *terms, int terms_count_max)
{
    /*
    if (tokens)
    {
        terms[0].exponent = 0;
    }
    return 1;
    */

    int i;
    int terms_count;
    int token_count;
    term_position term_position;

    terms_count = 0;
    token_count = 0;
    i = 0;
    term_position = TERM_LEFT;
    while (tokens[i].type != TOKEN_END && terms_count < terms_count_max)
    {
        if (tokens[i].type == TOKEN_EQUAL)
        {
            term_position = TERM_RIGHT;
            i++;
            break;
        }

        // position
        terms[terms_count].position = term_position;

        // sign
        terms[terms_count].sign = get_sign(&tokens[i], &token_count);
        i += token_count;

        // coefficient
        terms[terms_count].coefficient = get_coefficient(&tokens[i], &token_count);
        i += token_count;

        // exponent
        terms[terms_count].exponent = get_exponent(&tokens[i], &token_count);
        i += token_count;

        i++;
        terms_count++;
    }

    if (tokens[i].type != TOKEN_END && terms_count >= terms_count_max)
    {
        stop_errors(ERROR_PARSING, "deal with that later");
    }

    return terms_count;
}