7
#include "parserfunc.h"
8
#include "mp-equation.h"
10
/* Converts LexerTokenType to Precedence value. */
12
p_get_precedence(LexerTokenType type)
14
/* WARNING: This function doesn't work for Unary Plus and Unary Minus. Use their precedence directly while inserting them in tree. */
18
if(type == T_MULTIPLY)
30
if(type == T_FUNCTION)
36
if(type == T_PERCENTAGE)
40
if(type == T_FACTORIAL)
44
return P_NumberVariable;
48
/* Return associativity of specific token type from precedence. */
50
p_get_associativity_p(Precedence type)
56
||type == P_AddSubtract)
57
return LEFT_ASSOCIATIVE;
59
return RIGHT_ASSOCIATIVE;
60
/* For all remaining / non-associative operators, return Left Associativity. */
61
return LEFT_ASSOCIATIVE;
64
/* Return associativity of specific token by converting it to precedence first. */
66
p_get_associativity(LexerToken* token)
68
return p_get_associativity_p(p_get_precedence(token->token_type));
71
/* Generate precedence for a node from precedence value. Includes depth_level. */
73
p_make_precedence_p(ParserState* state, Precedence p)
75
return (p + (state->depth_level * P_Depth));
78
/* Generate precedence for a node from lexer token type. Includes depth_level. */
80
p_make_precedence_t(ParserState* state, LexerTokenType type)
82
return (p_get_precedence(type) + (state->depth_level * P_Depth));
85
/* Allocate and create a new node. */
87
p_create_node(ParserState* state, LexerToken* token, guint precedence, Associativity associativity, void* value, void* (*function)(ParseNode*))
90
new = (ParseNode*) malloc(sizeof(ParseNode));
96
new->precedence = precedence;
97
new->associativity = associativity;
100
new->evaluate = function;
104
/* Compares two nodes to decide, which will be parent and which willbe child. */
106
p_cmp_nodes(ParseNode* left, ParseNode* right)
109
1 = right goes up (near root) in parse tree.
110
0 = left goes up (near root) in parse tree.
114
if(left->precedence > right->precedence)
118
else if(left->precedence < right->precedence)
124
if(right->associativity == RIGHT_ASSOCIATIVE)
135
/* Unified interface (unary and binary nodes) to insert node into parse tree. */
137
p_insert_into_tree_all(ParserState* state, ParseNode* node, guint unary_function)
139
if(state->root == NULL)
142
state->right_most = state->root;
145
ParseNode* tmp = state->right_most;
146
while(p_cmp_nodes(tmp, node))
150
/* If tmp is null, that means, we have to insert new node at root. */
153
node->right = state->root;
154
node->right->parent = node;
160
node->right = tmp->right;
162
node->right->parent = node;
166
tmp->right->parent = tmp;
169
state->right_most = node;
170
while(state->right_most->right != NULL)
171
state->right_most = state->right_most->right;
175
/* If tmp is null, that means, we have to insert new node at root. */
178
node->left = state->root;
179
node->left->parent = node;
185
node->left = tmp->right;
187
node->left->parent = node;
191
tmp->right->parent = tmp;
194
state->right_most = node;
198
/* Insert binary node into the parse tree. */
200
p_insert_into_tree(ParserState* state, ParseNode* node)
202
p_insert_into_tree_all(state, node, 0);
205
/* Insert unary node into the parse tree. */
207
p_insert_into_tree_unary(ParserState* state, ParseNode* node)
209
p_insert_into_tree_all(state, node, 1);
212
/* Recursive call to free every node of parse-tree. */
214
p_destroy_all_nodes(ParseNode* node)
218
p_destroy_all_nodes(node->left);
219
p_destroy_all_nodes(node->right);
220
/* Don't call free for tokens, as they are allocated and freed in lexer. */
221
/* WARNING: If node->value is freed elsewhere, please assign it NULL before calling p_destroy_all_nodes(). */
227
/* Create parser state. */
229
p_create_parser(const gchar* input, MPEquationOptions* options)
232
state = (ParserState*) malloc(sizeof(ParserState));
233
assert(state != NULL);
234
state->lexer = l_create_lexer(input, state);
236
state->depth_level = 0;
237
state->right_most = NULL;
238
state->options = options;
240
state->error_token = NULL;
241
state->error_token_start = 0;
242
state->error_token_end = 0;
246
static guint statement (ParserState*);
247
/* Start parsing input string. And call evaluate on success. */
249
p_parse(ParserState* state)
254
l_insert_all_tokens(state->lexer);
255
ret = statement(state);
256
token = l_get_next_token(state->lexer);
257
if(token->token_type == T_ASSIGN)
259
token = l_get_next_token(state->lexer);
260
if(token->token_type != PL_EOS)
262
/* Full string is not parsed. */
264
set_error(state, PARSER_ERR_INVALID, token->string, token->start_index, token->end_index);
265
return PARSER_ERR_INVALID;
268
if(token->token_type != PL_EOS)
270
/* Full string is not parsed. */
272
set_error(state, PARSER_ERR_INVALID, token->string, token->start_index, token->end_index);
273
return PARSER_ERR_INVALID;
276
/* Input can't be parsed with grammar. */
277
return PARSER_ERR_INVALID;
278
ans = (MPNumber *) (*(state->root->evaluate))(state->root);
281
mp_set_from_mp(ans, &state->ret);
283
return PARSER_ERR_NONE;
285
return PARSER_ERR_INVALID;
288
/* Destroy parser state. */
290
p_destroy_parser(ParserState* state)
292
/* If state has a parse tree, destroy it first. */
295
p_destroy_all_nodes(state->root);
297
l_destroy_lexer(state->lexer);
301
/* LL (*) parser. Lookahead count depends on tokens. Handle with care. :P */
303
static guint expression(ParserState* state);
304
static guint expression_1(ParserState* state);
305
static guint expression_2(ParserState* state);
306
static guint unit(ParserState* state);
307
static guint variable(ParserState* state);
308
static guint term(ParserState* state);
309
static guint term_2(ParserState* state);
311
/* Helping function to p_check_variable. */
313
utf8_next_char(const gchar* c)
316
while((*c & 0xC0) == 0x80)
321
/* Check if string "name" is a valid variable for given ParserState. It is the same code, used to get the value of variable in parserfunc.c. */
323
p_check_variable(ParserState* state, gchar* name)
327
const gchar *c, *next;
331
if(!(state->get_variable))
336
/* If defined, then get the variable */
337
if((*(state->get_variable))(state, name, &temp))
342
/* If has more than one character then assume a multiplication of variables */
343
if(utf8_next_char(name)[0] != '\0')
346
buffer = (gchar*) malloc(sizeof(gchar) * strlen(name));
347
for(c = name; *c != '\0'; c = next)
349
next = utf8_next_char(c);
350
snprintf(buffer, next - c + 1, "%s", c);
351
if(!(*(state->get_variable))(state, buffer, &temp))
367
statement(ParserState* state)
370
LexerToken* token_old;
372
token = l_get_next_token(state->lexer);
373
if(token->token_type == T_VARIABLE)
376
token = l_get_next_token(state->lexer);
377
if(token->token_type == T_ASSIGN)
379
/* VARIABLE = expression. */
381
node = p_create_node(state, token_old, p_make_precedence_p(state, P_NumberVariable), p_get_associativity(token_old), NULL, pf_none);
382
p_insert_into_tree(state, node);
384
node = p_create_node(state, token, 0, p_get_associativity(token), NULL, pf_set_var);
385
p_insert_into_tree(state, node);
387
if(!expression(state))
391
else if(token->token_type == T_IN)
394
l_roll_back(state->lexer);
395
l_roll_back(state->lexer);
398
l_get_next_token(state->lexer);
400
node = p_create_node(state, token, 0, p_get_associativity(token), NULL, pf_convert_1);
401
p_insert_into_tree(state, node);
407
else if(token->token_type == T_SUP_NUMBER)
409
token = l_get_next_token(state->lexer);
410
if(token->token_type == T_IN)
413
l_roll_back(state->lexer);
414
l_roll_back(state->lexer);
415
l_roll_back(state->lexer);
418
l_get_next_token(state->lexer);
420
node = p_create_node(state, token, 0, p_get_associativity(token), NULL, pf_convert_1);
421
p_insert_into_tree(state, node);
429
l_roll_back(state->lexer);
430
l_roll_back(state->lexer);
431
l_roll_back(state->lexer);
432
if(!expression(state))
439
l_roll_back(state->lexer);
440
l_roll_back(state->lexer);
441
if(!expression(state))
446
else if(token->token_type == T_NUMBER)
449
token = l_get_next_token(state->lexer);
450
if(token->token_type == T_VARIABLE)
452
token = l_get_next_token(state->lexer);
453
if(token->token_type == T_IN)
455
/* NUMBER UNIT in UNIT */
456
l_roll_back(state->lexer);
457
l_roll_back(state->lexer);
459
node = p_create_node(state, token_old, p_make_precedence_t(state, token_old->token_type), p_get_associativity(token), NULL, pf_constant);
460
p_insert_into_tree(state, node);
464
token = l_get_next_token(state->lexer);
466
node = p_create_node(state, token, 0, p_get_associativity(token), NULL, pf_convert_number);
467
p_insert_into_tree(state, node);
473
else if(token->token_type == T_SUP_NUMBER)
475
token = l_get_next_token(state->lexer);
476
if(token->token_type == T_IN)
478
/* NUMBER UNIT in UNIT */
479
l_roll_back(state->lexer);
480
l_roll_back(state->lexer);
481
l_roll_back(state->lexer);
483
node = p_create_node(state, token_old, p_make_precedence_t(state, token_old->token_type), p_get_associativity(token), NULL, pf_constant);
484
p_insert_into_tree(state, node);
488
token = l_get_next_token(state->lexer);
490
node = p_create_node(state, token, 0, p_get_associativity(token), NULL, pf_convert_number);
491
p_insert_into_tree(state, node);
499
l_roll_back(state->lexer);
500
l_roll_back(state->lexer);
501
l_roll_back(state->lexer);
502
l_roll_back(state->lexer);
503
if(!expression(state))
510
l_roll_back(state->lexer);
511
l_roll_back(state->lexer);
512
l_roll_back(state->lexer);
513
if(!expression(state))
520
l_roll_back(state->lexer);
521
l_roll_back(state->lexer);
522
if(!expression(state))
529
l_roll_back(state->lexer);
530
if(!expression(state))
537
unit(ParserState* state)
540
LexerToken* token_old;
542
token = l_get_next_token(state->lexer);
543
if(token->token_type == T_VARIABLE)
546
token = l_get_next_token(state->lexer);
547
if(token->token_type == T_SUP_NUMBER)
551
node = p_create_node(state, token_old, p_make_precedence_t(state, token_old->token_type), p_get_associativity(token_old), pf_make_unit(token_old->string, token->string), pf_none);
552
p_insert_into_tree(state, node);
558
l_roll_back(state->lexer);
561
node = p_create_node(state, token_old, p_make_precedence_t(state, token_old->token_type), p_get_associativity(token_old), NULL, pf_none);
562
p_insert_into_tree(state, node);
569
l_roll_back(state->lexer);
575
expression(ParserState* state)
577
if(!expression_1(state))
579
if(!expression_2(state))
585
expression_1(ParserState* state)
589
token = l_get_next_token(state->lexer);
590
if(token->token_type == PL_EOS
591
||token->token_type == T_ASSIGN)
593
l_roll_back(state->lexer);
596
if(token->token_type == T_L_R_BRACKET)
598
state->depth_level++;
599
if(!expression(state))
601
token = l_get_next_token(state->lexer);
602
if(token->token_type == T_R_R_BRACKET)
604
state->depth_level--;
608
//Expected ")" here...
611
else if(token->token_type == T_L_S_BRACKET)
613
state->depth_level++;
615
/* Give round, preference of P_Unknown aka 0, to keep it on the top of expression. */
617
node = p_create_node(state, token, p_make_precedence_p(state, P_Unknown), p_get_associativity(token), NULL, pf_do_round);
618
p_insert_into_tree_unary(state, node);
620
if(!expression(state))
622
token = l_get_next_token(state->lexer);
623
if(token->token_type == T_R_S_BRACKET)
625
state->depth_level--;
629
//Expected "]" here...
632
else if(token->token_type == T_L_C_BRACKET)
634
state->depth_level++;
636
/* Give fraction, preference of P_Unknown aka 0, to keep it on the top of expression. */
638
node = p_create_node(state, token, p_make_precedence_p(state, P_Unknown), p_get_associativity(token), NULL, pf_do_fraction);
639
p_insert_into_tree_unary(state, node);
641
if(!expression(state))
643
token = l_get_next_token(state->lexer);
644
if(token->token_type == T_R_C_BRACKET)
646
state->depth_level--;
650
//Expected "}" here...
653
else if(token->token_type == T_ABS)
655
state->depth_level++;
657
/* Give abs, preference of P_Unknown aka 0, to keep it on the top of expression. */
659
node = p_create_node(state, token, p_make_precedence_p(state, P_Unknown), p_get_associativity(token), NULL, pf_do_abs);
660
p_insert_into_tree_unary(state, node);
662
if(!expression(state))
664
token = l_get_next_token(state->lexer);
665
if(token->token_type == T_ABS)
667
state->depth_level--;
671
//Expected "|" here...
674
else if(token->token_type == T_NOT)
678
node = p_create_node(state, token, p_make_precedence_p(state, P_Not), p_get_associativity(token), NULL, pf_do_not);
679
p_insert_into_tree_unary(state, node);
681
if(!expression(state))
685
else if(token->token_type == T_NUMBER)
689
node = p_create_node(state, token, p_make_precedence_t(state, token->token_type), p_get_associativity(token), NULL, pf_constant);
690
p_insert_into_tree(state, node);
692
token = l_get_next_token(state->lexer);
693
l_roll_back(state->lexer);
695
if(token->token_type == T_FUNCTION
696
||token->token_type == T_VARIABLE
697
||token->token_type == T_SUB_NUMBER
698
||token->token_type == T_ROOT
699
||token->token_type == T_ROOT_3
700
||token->token_type == T_ROOT_4)
702
/* NUMBER variable. */
704
node = p_create_node(state, NULL, p_make_precedence_p(state, P_Multiply), p_get_associativity_p(P_Multiply), NULL, pf_do_multiply);
705
p_insert_into_tree(state, node);
717
else if(token->token_type == T_L_FLOOR)
719
state->depth_level++;
720
/* Give floor, preference of P_Unknown aka 0, to keep it on the top of expression. */
722
node = p_create_node(state, NULL, p_make_precedence_p(state, P_Unknown), p_get_associativity_p(P_Unknown), NULL, pf_do_floor);
723
p_insert_into_tree_unary(state, node);
725
if(!expression(state))
727
token = l_get_next_token(state->lexer);
728
if(token->token_type == T_R_FLOOR)
730
state->depth_level--;
737
else if(token->token_type == T_L_CEILING)
739
state->depth_level++;
740
/* Give ceiling, preference of P_Unknown aka 0, to keep it on the top of expression. */
742
node = p_create_node(state, NULL, p_make_precedence_p(state, P_Unknown), p_get_associativity_p(P_Unknown), NULL, pf_do_ceiling);
743
p_insert_into_tree_unary(state, node);
745
if(!expression(state))
747
token = l_get_next_token(state->lexer);
748
if(token->token_type == T_R_CEILING)
750
state->depth_level--;
757
else if(token->token_type == T_SUBTRACT)
759
/* UnaryMinus expression */
761
node = p_create_node(state, token, p_make_precedence_p(state, P_UnaryMinus), p_get_associativity_p(P_UnaryMinus), NULL, pf_unary_minus);
762
p_insert_into_tree_unary(state, node);
764
if(!expression_1(state))
768
else if(token->token_type == T_ADD)
770
token = l_get_next_token(state->lexer);
771
if(token->token_type == T_NUMBER)
773
/* UnaryPlus expression */
774
/* Ignore T_ADD. It is not required. */
776
node = p_create_node(state, token, p_make_precedence_t(state, token->token_type), p_get_associativity(token), NULL, pf_constant);
777
p_insert_into_tree(state, node);
787
l_roll_back(state->lexer);
796
expression_2(ParserState* state)
800
token = l_get_next_token(state->lexer);
801
if(token->token_type == T_L_R_BRACKET)
803
/* expression "(" expression ")" */
805
node = p_create_node(state, NULL, p_make_precedence_p(state, P_Multiply), p_get_associativity_p(P_Multiply), NULL, pf_do_multiply);
806
p_insert_into_tree(state, node);
808
state->depth_level++;
809
if(!expression(state))
811
token = l_get_next_token(state->lexer);
812
if(token->token_type == T_R_R_BRACKET)
814
state->depth_level--;
815
if(!expression_2(state))
824
else if(token->token_type == T_POWER)
826
/* expression "^" expression */
828
node = p_create_node(state, token, p_make_precedence_t(state, token->token_type), p_get_associativity(token), NULL, pf_do_x_pow_y);
829
p_insert_into_tree(state, node);
831
if(!expression_1(state))
833
if(!expression_2(state))
837
else if(token->token_type == T_SUP_NUMBER)
839
/* expression T_SUP_NUMBER */
841
node = p_create_node(state, NULL, p_make_precedence_p(state, P_Power), p_get_associativity_p(P_Power), NULL, pf_do_x_pow_y_int);
842
p_insert_into_tree(state, node);
844
node = p_create_node(state, token, p_make_precedence_p(state, P_NumberVariable), p_get_associativity_p(P_NumberVariable), NULL, pf_none);
845
p_insert_into_tree(state, node);
847
if(!expression_2(state))
851
else if(token->token_type == T_NSUP_NUMBER)
853
/* expression T_NSUP_NUMBER */
855
node = p_create_node(state, NULL, p_make_precedence_p(state, P_Power), p_get_associativity_p(P_Power), NULL, pf_do_x_pow_y_int);
856
p_insert_into_tree(state, node);
858
node = p_create_node(state, token, p_make_precedence_p(state, P_NumberVariable), p_get_associativity_p(P_NumberVariable), NULL, pf_none);
859
p_insert_into_tree(state, node);
861
if(!expression_2(state))
865
else if(token->token_type == T_FACTORIAL)
867
/* expression T_FACTORIAL */
869
node = p_create_node(state, token, p_make_precedence_t(state, token->token_type), p_get_associativity(token), NULL, pf_do_factorial);
870
p_insert_into_tree_unary(state, node);
872
if(!expression_2(state))
876
else if(token->token_type == T_MULTIPLY)
878
/* expression T_MULTIPLY expression */
880
node = p_create_node(state, token, p_make_precedence_t(state, token->token_type), p_get_associativity(token), NULL, pf_do_multiply);
881
p_insert_into_tree(state, node);
883
if(!expression_1(state))
885
if(!expression_2(state))
889
else if(token->token_type == T_PERCENTAGE)
893
node = p_create_node(state, token, p_make_precedence_t(state, token->token_type), p_get_associativity(token), NULL, pf_do_percent);
894
p_insert_into_tree_unary(state, node);
896
if(!expression_2(state))
900
else if(token->token_type == T_AND)
902
/* expression T_AND expression */
904
node = p_create_node(state, token, p_make_precedence_t(state, token->token_type), p_get_associativity(token), NULL, pf_do_and);
905
p_insert_into_tree(state, node);
907
if(!expression_1(state))
909
if(!expression_2(state))
913
else if(token->token_type == T_OR)
915
/* expression T_OR expression */
917
node = p_create_node(state, token, p_make_precedence_t(state, token->token_type), p_get_associativity(token), NULL, pf_do_or);
918
p_insert_into_tree(state, node);
920
if(!expression_1(state))
922
if(!expression_2(state))
926
else if(token->token_type == T_XOR)
928
/* expression T_XOR expression */
930
node = p_create_node(state, token, p_make_precedence_t(state, token->token_type), p_get_associativity(token), NULL, pf_do_xor);
931
p_insert_into_tree(state, node);
933
if(!expression_1(state))
935
if(!expression_2(state))
939
else if(token->token_type == T_DIV)
941
/* expression T_DIV expression */
943
node = p_create_node(state, token, p_make_precedence_t(state, token->token_type), p_get_associativity(token), NULL, pf_do_divide);
944
p_insert_into_tree(state, node);
946
if(!expression_1(state))
948
if(!expression_2(state))
952
else if(token->token_type == T_MOD)
954
/* expression T_MOD expression */
956
node = p_create_node(state, token, p_make_precedence_t(state, token->token_type), p_get_associativity(token), NULL, pf_do_mod);
957
p_insert_into_tree(state, node);
959
if(!expression_1(state))
961
if(!expression_2(state))
965
else if(token->token_type == T_ADD)
967
/* expression T_ADD expression */
969
node = p_create_node(state, token, p_make_precedence_t(state, token->token_type), p_get_associativity(token), NULL, pf_do_add);
970
p_insert_into_tree(state, node);
972
if(!expression_1(state))
974
token = l_get_next_token(state->lexer);
975
if(token->token_type == T_PERCENTAGE)
977
//FIXME: This condition needs to be verified for all cases.. :(
978
if(node->right->precedence > P_Percentage)
980
node->precedence = P_Percentage;
981
node->evaluate = pf_do_add_percent;
986
/* Assume '%' to be part of 'expression T_PERCENTAGE' statement. */
987
l_roll_back(state->lexer);
988
if(!expression_2(state))
994
l_roll_back(state->lexer);
996
if(!expression_2(state))
1000
else if(token->token_type == T_SUBTRACT)
1002
/* expression T_SUBTRACT expression */
1004
node = p_create_node(state, token, p_make_precedence_t(state, token->token_type), p_get_associativity(token), NULL, pf_do_subtract);
1005
p_insert_into_tree(state, node);
1007
if(!expression_1(state))
1009
token = l_get_next_token(state->lexer);
1010
if(token->token_type == T_PERCENTAGE)
1012
//FIXME: This condition needs to be verified for all cases.. :(
1013
if(node->right->precedence > P_Percentage)
1015
node->precedence = P_Percentage;
1016
node->evaluate = pf_do_subtract_percent;
1021
/* Assume '%' to be part of 'expression T_PERCENTAGE' statement. */
1022
l_roll_back(state->lexer);
1023
if(!expression_2 (state))
1029
l_roll_back(state->lexer);
1031
if(!expression_2(state))
1037
l_roll_back(state->lexer);
1043
variable(ParserState* state)
1046
LexerToken* token_old;
1048
token = l_get_next_token(state->lexer);
1049
if(token->token_type == T_FUNCTION)
1052
token = l_get_next_token(state->lexer);
1053
if(token->token_type == T_SUP_NUMBER)
1055
/* FUNCTION SUP_NUMBER expression */
1056
/* Pass power as void * value. That will be taken care in pf_apply_func_with_powre. */
1058
node = p_create_node(state, token_old, p_make_precedence_t(state, token_old->token_type), p_get_associativity(token_old), token, pf_apply_func_with_power);
1059
p_insert_into_tree_unary(state, node);
1061
if(!expression(state))
1065
else if(token->token_type == T_NSUP_NUMBER)
1067
/* FUNCTION NSUP_NUMBER expression */
1068
/* Pass power as void * value. That will be taken care in pf_apply_func_with_npowre. */
1070
node = p_create_node(state, token_old, p_make_precedence_t(state, token_old->token_type), p_get_associativity(token_old), token, pf_apply_func_with_npower);
1071
p_insert_into_tree_unary(state, node);
1073
if(!expression(state))
1079
l_roll_back(state->lexer);
1080
/* FUNCTION expression */
1082
node = p_create_node(state, token_old, p_make_precedence_t(state, token_old->token_type), p_get_associativity(token_old), NULL, pf_apply_func);
1083
p_insert_into_tree_unary(state, node);
1085
if(!expression(state))
1090
else if(token->token_type == T_SUB_NUMBER)
1093
token = l_get_next_token(state->lexer);
1094
if(token->token_type == T_ROOT)
1096
/* SUB_NUM ROOT expression */
1097
/* Pass SUB_NUM as void* value in node. pf_do_nth_root will take care of it. */
1099
node = p_create_node(state, token, p_make_precedence_t(state, token->token_type), p_get_associativity(token), token_old, pf_do_nth_root);
1100
p_insert_into_tree_unary(state, node);
1102
if(!expression (state))
1111
else if(token->token_type == T_ROOT)
1113
/* ROOT expression */
1115
node = p_create_node(state, token, p_make_precedence_t(state, token->token_type), p_get_associativity(token), NULL, pf_do_sqrt);
1116
p_insert_into_tree_unary(state, node);
1118
if(!expression(state))
1122
else if(token->token_type == T_ROOT_3)
1124
/* ROOT_3 expression */
1126
node = p_create_node(state, token, p_make_precedence_t(state, token->token_type), p_get_associativity(token), NULL, pf_do_root_3);
1127
p_insert_into_tree_unary(state, node);
1129
if(!expression(state))
1133
else if(token->token_type == T_ROOT_4)
1135
/* ROOT_4 expression */
1137
node = p_create_node(state, token, p_make_precedence_t(state, token->token_type), p_get_associativity(token), NULL, pf_do_root_4);
1138
p_insert_into_tree_unary(state, node);
1140
if(!expression(state))
1144
else if(token->token_type == T_VARIABLE)
1146
l_roll_back(state->lexer);
1147
//TODO: unknown function ERROR for (T_VARIABLE T_SUP_NUMBER expression).
1159
term(ParserState* state)
1162
LexerToken* token_old;
1164
token = l_get_next_token(state->lexer);
1165
if(token->token_type == T_VARIABLE)
1168
/* Check if the token is a valid variable or not. */
1169
if(!p_check_variable(state, token->string))
1171
set_error(state, PARSER_ERR_UNKNOWN_VARIABLE, token->string, token->start_index, token->end_index);
1174
token = l_get_next_token(state->lexer);
1175
if(token->token_type == T_SUP_NUMBER)
1177
/* VARIABLE SUP_NUMBER */
1178
/* Pass power as void* value. pf_get_variable_with_power will take care of it. */
1180
node = p_create_node(state, token_old, p_make_precedence_t(state, token_old->token_type), p_get_associativity(token_old), token, pf_get_variable_with_power);
1181
p_insert_into_tree(state, node);
1186
l_roll_back(state->lexer);
1189
node = p_create_node(state, token_old, p_make_precedence_t(state, token_old->token_type), p_get_associativity(token_old), NULL, pf_get_variable);
1190
p_insert_into_tree(state, node);
1204
term_2(ParserState* state)
1208
token = l_get_next_token(state->lexer);
1209
l_roll_back(state->lexer);
1210
if(token->token_type == PL_EOS
1211
||token->token_type == T_ASSIGN)
1215
if(token->token_type == T_VARIABLE)
1217
/* Insert multiply in between two distinct (variable). */
1219
node = p_create_node(state, NULL, p_make_precedence_p(state, P_Multiply), p_get_associativity_p(P_Multiply), NULL, pf_do_multiply);
1220
p_insert_into_tree(state, node);