2 * Copyright (C) 2012 Arth Patel
3 * Copyright (C) 2012 Robert Ancell
5 * This program is free software: you can redistribute it and/or modify it under
6 * the terms of the GNU General Public License as published by the Free Software
7 * Foundation, either version 2 of the License, or (at your option) any later
8 * version. See http://www.gnu.org/copyleft/gpl.html the full text of the
12 /* Operator Associativity. */
13 public enum Associativity
19 /* Operator Precedence. */
20 private enum Precedence
32 /* UNARY_MINUS and POWER must have same precedence. */
37 /* DEPTH should be always at the bottom. It stops node jumping off the current depth level. */
41 /* ParseNode structure for parse tree. */
42 public class ParseNode
45 public ParseNode? parent
= null;
46 public ParseNode? left
= null;
47 public ParseNode? right
= null;
48 public LexerToken token
;
49 public uint precedence
;
50 public Associativity associativity
;
53 public ParseNode (Parser parser
, LexerToken? token
, uint precedence
, Associativity associativity
, string? value
= null)
57 this
.precedence
= precedence
;
58 this
.associativity
= associativity
;
62 public virtual Number?
solve ()
68 public abstract class RNode
: ParseNode
70 public RNode (Parser parser
, LexerToken? token
, uint precedence
, Associativity associativity
)
72 base (parser
, token
, precedence
, associativity
);
75 public override Number?
solve ()
77 var r
= right
.solve ();
83 public abstract Number
solve_r (Number r
);
86 public abstract class LRNode
: ParseNode
88 public LRNode (Parser parser
, LexerToken? token
, uint precedence
, Associativity associativity
)
90 base (parser
, token
, precedence
, associativity
);
93 public override Number?
solve ()
95 var l
= left
.solve ();
96 var r
= right
.solve ();
97 if (l
== null || r
== null)
99 return solve_lr (l
, r
);
102 public abstract Number
solve_lr (Number left
, Number r
);
105 public class ConstantNode
: ParseNode
107 public ConstantNode (Parser parser
, LexerToken? token
, uint precedence
, Associativity associativity
)
109 base (parser
, token
, precedence
, associativity
);
112 public override Number?
solve ()
114 return mp_set_from_string (token
.text
, parser
.number_base
);
118 public class AssignNode
: RNode
120 public AssignNode (Parser parser
, LexerToken? token
, uint precedence
, Associativity associativity
)
122 base (parser
, token
, precedence
, associativity
);
125 public override Number
solve_r (Number r
)
127 parser
.set_variable (left
.token
.text
, r
);
132 public class NameNode
: ParseNode
134 public NameNode (Parser parser
, LexerToken? token
, uint precedence
, Associativity associativity
, string? text
= null)
136 base (parser
, token
, precedence
, associativity
, text
);
140 public class VariableNode
: ParseNode
142 public VariableNode (Parser parser
, LexerToken? token
, uint precedence
, Associativity associativity
)
144 base (parser
, token
, precedence
, associativity
);
147 public override Number?
solve ()
149 /* If defined, then get the variable */
150 var ans
= parser
.get_variable (token
.text
);
154 /* If has more than one character then assume a multiplication of variables */
155 // FIXME: Do this in the lexer
156 var value
= new Number
.integer (1);
159 while (token
.text
.get_next_char (ref index
, out c
))
161 var t
= parser
.get_variable (c
.to_string ());
164 parser
.set_error (ErrorCode
.UNKNOWN_VARIABLE
, token
.text
, token
.start_index
, token
.end_index
);
167 value
= value
.multiply (t
);
174 public class VariableWithPowerNode
: ParseNode
176 public VariableWithPowerNode (Parser parser
, LexerToken? token
, uint precedence
, Associativity associativity
, string text
)
178 base (parser
, token
, precedence
, associativity
, text
);
181 public override Number?
solve ()
183 var pow
= super_atoi (value
);
187 /* If defined, then get the variable */
188 var ans
= parser
.get_variable (token
.text
);
190 return ans
.xpowy_integer (pow
);
192 /* If has more than one character then assume a multiplication of variables */
193 // FIXME: Do in lexer
194 var value
= new Number
.integer (1);
197 while (token
.text
.get_next_char (ref index
, out c
))
199 var t
= parser
.get_variable (c
.to_string ());
202 parser
.set_error (ErrorCode
.UNKNOWN_VARIABLE
, token
.text
, token
.start_index
, token
.end_index
);
206 /* If last term do power */
209 if (!token
.text
.get_next_char (ref i
, out next
))
210 t
= t
.xpowy_integer (pow
);
211 value
= value
.multiply (t
);
218 public class FunctionNode
: RNode
220 public FunctionNode (Parser parser
, LexerToken? token
, uint precedence
, Associativity associativity
)
222 base (parser
, token
, precedence
, associativity
);
225 public override Number
solve_r (Number r
)
227 var ans
= parser
.get_function (token
.text
, r
);
229 parser
.set_error (ErrorCode
.UNKNOWN_FUNCTION
, token
.text
, token
.start_index
, token
.end_index
);
235 public class FunctionWithPowerNode
: ParseNode
237 public FunctionWithPowerNode (Parser parser
, LexerToken? token
, uint precedence
, Associativity associativity
, string text
)
239 base (parser
, token
, precedence
, associativity
, text
);
242 public override Number?
solve ()
244 var val
= right
.solve ();
250 var tmp
= parser
.get_function (token
.text
, val
);
254 parser
.set_error (ErrorCode
.UNKNOWN_FUNCTION
, token
.text
, token
.start_index
, token
.end_index
);
258 var pow
= super_atoi (value
);
261 return tmp
.xpowy_integer (pow
);
265 public class FunctionWithNegativePowerNode
: ParseNode
267 public FunctionWithNegativePowerNode (Parser parser
, LexerToken? token
, uint precedence
, Associativity associativity
, string text
)
269 base (parser
, token
, precedence
, associativity
, text
);
272 public override Number?
solve ()
274 var val
= right
.solve ();
280 var inv_name
= token
.text
+ "⁻¹";
281 var tmp
= parser
.get_function (inv_name
, val
);
285 parser
.set_error (ErrorCode
.UNKNOWN_FUNCTION
, token
.text
, token
.start_index
, token
.end_index
);
289 var pow
= super_atoi (value
);
292 return tmp
.xpowy_integer (-pow
);
296 public class UnaryMinusNode
: RNode
298 public UnaryMinusNode (Parser parser
, LexerToken? token
, uint precedence
, Associativity associativity
)
300 base (parser
, token
, precedence
, associativity
);
303 public override Number
solve_r (Number r
)
305 return r
.invert_sign ();
309 public class AbsoluteValueNode
: RNode
311 public AbsoluteValueNode (Parser parser
, LexerToken? token
, uint precedence
, Associativity associativity
)
313 base (parser
, token
, precedence
, associativity
);
316 public override Number
solve_r (Number r
)
322 public class FloorNode
: RNode
324 public FloorNode (Parser parser
, LexerToken? token
, uint precedence
, Associativity associativity
)
326 base (parser
, token
, precedence
, associativity
);
329 public override Number
solve_r (Number r
)
335 public class CeilingNode
: RNode
337 public CeilingNode (Parser parser
, LexerToken? token
, uint precedence
, Associativity associativity
)
339 base (parser
, token
, precedence
, associativity
);
342 public override Number
solve_r (Number r
)
348 public class FractionalComponentNode
: RNode
350 public FractionalComponentNode (Parser parser
, LexerToken? token
, uint precedence
, Associativity associativity
)
352 base (parser
, token
, precedence
, associativity
);
355 public override Number
solve_r (Number r
)
357 return r
.fractional_part ();
361 public class RoundNode
: RNode
363 public RoundNode (Parser parser
, LexerToken? token
, uint precedence
, Associativity associativity
)
365 base (parser
, token
, precedence
, associativity
);
368 public override Number
solve_r (Number r
)
374 public class PercentNode
: RNode
376 public PercentNode (Parser parser
, LexerToken? token
, uint precedence
, Associativity associativity
)
378 base (parser
, token
, precedence
, associativity
);
381 public override Number
solve_r (Number r
)
383 return r
.divide_integer (100);
387 public class FactorialNode
: RNode
389 public FactorialNode (Parser parser
, LexerToken? token
, uint precedence
, Associativity associativity
)
391 base (parser
, token
, precedence
, associativity
);
394 public override Number
solve_r (Number r
)
396 return r
.factorial ();
400 public class AddNode
: LRNode
402 public bool do_percentage
= false;
404 public AddNode (Parser parser
, LexerToken? token
, uint precedence
, Associativity associativity
)
406 base (parser
, token
, precedence
, associativity
);
409 public override Number
solve_lr (Number l
, Number r
)
413 var per
= r
.add (new Number
.integer (100));
414 per
= per
.divide_integer (100);
415 return l
.multiply (per
);
422 public class SubtractNode
: LRNode
424 public bool do_percentage
= false;
426 public SubtractNode (Parser parser
, LexerToken? token
, uint precedence
, Associativity associativity
)
428 base (parser
, token
, precedence
, associativity
);
431 public override Number
solve_lr (Number l
, Number r
)
435 var per
= r
.add (new Number
.integer (-100));
436 per
= per
.divide_integer (-100);
437 return l
.multiply (per
);
440 return l
.subtract (r
);
444 public class MultiplyNode
: LRNode
446 public MultiplyNode (Parser parser
, LexerToken? token
, uint precedence
, Associativity associativity
)
448 base (parser
, token
, precedence
, associativity
);
451 public override Number
solve_lr (Number l
, Number r
)
453 return l
.multiply (r
);
457 public class DivideNode
: LRNode
459 public DivideNode (Parser parser
, LexerToken? token
, uint precedence
, Associativity associativity
)
461 base (parser
, token
, precedence
, associativity
);
464 public override Number
solve_lr (Number l
, Number r
)
470 public class ModulusDivideNode
: LRNode
472 public ModulusDivideNode (Parser parser
, LexerToken? token
, uint precedence
, Associativity associativity
)
474 base (parser
, token
, precedence
, associativity
);
477 public override Number
solve_lr (Number l
, Number r
)
479 return l
.modulus_divide (r
);
483 public class RootNode
: RNode
487 public RootNode (Parser parser
, LexerToken? token
, uint precedence
, Associativity associativity
, int n
)
489 base (parser
, token
, precedence
, associativity
);
493 public override Number
solve_r (Number r
)
499 public class XPowYNode
: LRNode
501 public XPowYNode (Parser parser
, LexerToken? token
, uint precedence
, Associativity associativity
)
503 base (parser
, token
, precedence
, associativity
);
506 public override Number
solve_lr (Number l
, Number r
)
512 public class XPowYIntegerNode
: ParseNode
514 public XPowYIntegerNode (Parser parser
, LexerToken? token
, uint precedence
, Associativity associativity
)
516 base (parser
, token
, precedence
, associativity
);
519 public override Number?
solve ()
521 var val
= left
.solve ();
522 var pow
= super_atoi (right
.token
.text
);
526 return val
.xpowy_integer (pow
);
530 public class NotNode
: RNode
532 public NotNode (Parser parser
, LexerToken? token
, uint precedence
, Associativity associativity
)
534 base (parser
, token
, precedence
, associativity
);
537 public override Number
solve_r (Number r
)
539 if (!mp_is_overflow (r
, parser
.wordlen
))
541 parser
.set_error (ErrorCode
.OVERFLOW
);
542 return new Number
.integer (0);
545 return r
.not (parser
.wordlen
);
549 public class AndNode
: LRNode
551 public AndNode (Parser parser
, LexerToken? token
, uint precedence
, Associativity associativity
)
553 base (parser
, token
, precedence
, associativity
);
556 public override Number
solve_lr (Number l
, Number r
)
562 public class OrNode
: LRNode
564 public OrNode (Parser parser
, LexerToken? token
, uint precedence
, Associativity associativity
)
566 base (parser
, token
, precedence
, associativity
);
569 public override Number
solve_lr (Number l
, Number r
)
575 public class XorNode
: LRNode
577 public XorNode (Parser parser
, LexerToken? token
, uint precedence
, Associativity associativity
)
579 base (parser
, token
, precedence
, associativity
);
582 public override Number
solve_lr (Number l
, Number r
)
588 public class ConvertNode
: LRNode
590 public ConvertNode (Parser parser
, LexerToken? token
, uint precedence
, Associativity associativity
)
592 base (parser
, token
, precedence
, associativity
);
595 public override Number
solve_lr (Number l
, Number r
)
598 if (left
.value
!= null)
604 from
= left
.token
.text
;
607 if (right
.value
!= null)
613 to
= right
.token
.text
;
615 var tmp
= new Number
.integer (1);
617 var ans
= parser
.convert (tmp
, from
, to
);
619 parser
.set_error (ErrorCode
.UNKNOWN_CONVERSION
);
625 public class ConvertNumberNode
: ParseNode
627 public ConvertNumberNode (Parser parser
, LexerToken? token
, uint precedence
, Associativity associativity
)
629 base (parser
, token
, precedence
, associativity
);
632 public override Number?
solve ()
635 if (left
.value
!= null)
641 from
= left
.token
.text
;
644 if (right
.value
!= null)
650 to
= right
.token
.text
;
652 var tmp
= mp_set_from_string (left
.left
.token
.text
, parser
.number_base
);
656 var ans
= parser
.convert (tmp
, from
, to
);
658 parser
.set_error (ErrorCode
.UNKNOWN_CONVERSION
);
666 private string input
;
667 private ParseNode root
;
668 private ParseNode right_most
;
670 public int number_base
;
672 private uint depth_level
;
673 private ErrorCode error
;
674 private string error_token
;
675 private int error_token_start
;
676 private int error_token_end
;
678 public Parser (string input
, int number_base
, int wordlen
)
681 lexer
= new
Lexer (input
, this
, number_base
);
685 this
.number_base
= number_base
;
686 this
.wordlen
= wordlen
;
687 error
= ErrorCode
.NONE
;
689 error_token_start
= 0;
693 public void set_error (ErrorCode errorno
, string? token
= null, uint token_start
= 0, uint token_end
= 0)
697 error_token_start
= input
.char_count (token_start
);
698 error_token_end
= input
.char_count (token_end
);
701 public virtual bool variable_is_defined (string name
)
706 public virtual Number?
get_variable (string name
)
711 public virtual void set_variable (string name
, Number x
)
715 public virtual bool function_is_defined (string name
)
720 public virtual Number?
get_function (string name
, Number x
)
725 public virtual Number?
convert (Number x
, string x_units
, string z_units
)
730 /* Start parsing input string. And call evaluate on success. */
731 public Number?
parse (out ErrorCode error_code
, out string? error_token
, out uint error_start
, out uint error_end
)
733 /* Scan string and split into tokens */
737 var ret
= statement ();
739 var token
= lexer
.get_next_token ();
740 if (token
.type
== LexerTokenType
.ASSIGN
)
742 token
= lexer
.get_next_token ();
743 if (token
.type
!= LexerTokenType
.PL_EOS
)
745 /* Full string is not parsed. */
747 set_error (ErrorCode
.INVALID
, token
.text
, token
.start_index
, token
.end_index
);
750 error_token
= this
.error_token
;
751 error_start
= error_token_start
;
752 error_end
= error_token_end
;
756 if (token
.type
!= LexerTokenType
.PL_EOS
)
758 /* Full string is not parsed. */
760 set_error (ErrorCode
.INVALID
, token
.text
, token
.start_index
, token
.end_index
);
763 error_token
= this
.error_token
;
764 error_start
= error_token_start
;
765 error_end
= error_token_end
;
769 /* Input can't be parsed with grammar. */
773 error_token
= this
.error_token
;
774 error_start
= error_token_start
;
775 error_end
= error_token_end
;
778 var ans
= root
.solve ();
781 error_code
= ErrorCode
.INVALID
;
783 error_start
= error_token_start
;
784 error_end
= error_token_end
;
788 error_code
= ErrorCode
.NONE
;
795 /* Converts LexerTokenType to Precedence value. */
796 private Precedence
get_precedence (LexerTokenType type
)
798 /* WARNING: This function doesn't work for Unary Plus and Unary Minus. Use their precedence directly while inserting them in tree. */
799 if (type
== LexerTokenType
.ADD
|| type
== LexerTokenType
.SUBTRACT
)
800 return Precedence
.ADD_SUBTRACT
;
801 if (type
== LexerTokenType
.MULTIPLY
)
802 return Precedence
.MULTIPLY
;
803 if (type
== LexerTokenType
.MOD
)
804 return Precedence
.MOD
;
805 if (type
== LexerTokenType
.DIVIDE
)
806 return Precedence
.DIVIDE
;
807 if (type
== LexerTokenType
.NOT
)
808 return Precedence
.NOT
;
809 if (type
== LexerTokenType
.ROOT
|| type
== LexerTokenType
.ROOT_3
|| type
== LexerTokenType
.ROOT_4
)
810 return Precedence
.ROOT
;
811 if (type
== LexerTokenType
.FUNCTION
)
812 return Precedence
.FUNCTION
;
813 if (type
== LexerTokenType
.AND
|| type
== LexerTokenType
.OR
|| type
== LexerTokenType
.XOR
)
814 return Precedence
.BOOLEAN
;
815 if (type
== LexerTokenType
.PERCENTAGE
)
816 return Precedence
.PERCENTAGE
;
817 if (type
== LexerTokenType
.POWER
)
818 return Precedence
.POWER
;
819 if (type
== LexerTokenType
.FACTORIAL
)
820 return Precedence
.FACTORIAL
;
821 if (type
== LexerTokenType
.NUMBER
|| type
== LexerTokenType
.VARIABLE
)
822 return Precedence
.NUMBER_VARIABLE
;
823 return Precedence
.UNKNOWN
;
826 /* Return associativity of specific token type from precedence. */
827 private Associativity
get_associativity_p (Precedence type
)
829 if (type
== Precedence
.BOOLEAN
|| type
== Precedence
.DIVIDE
|| type
== Precedence
.MOD
|| type
== Precedence
.MULTIPLY
|| type
== Precedence
.ADD_SUBTRACT
)
830 return Associativity
.LEFT
;
831 if (type
== Precedence
.POWER
)
832 return Associativity
.RIGHT
;
833 /* For all remaining / non-associative operators, return Left Associativity. */
834 return Associativity
.LEFT
;
837 /* Return associativity of specific token by converting it to precedence first. */
838 private Associativity
get_associativity (LexerToken token
)
840 return get_associativity_p (get_precedence (token
.type
));
843 /* Generate precedence for a node from precedence value. Includes depth_level. */
844 private uint make_precedence_p (Precedence p
)
846 return p
+ (depth_level
* Precedence
.DEPTH
);
849 /* Generate precedence for a node from lexer token type. Includes depth_level. */
850 private uint make_precedence_t (LexerTokenType type
)
852 return get_precedence (type
) + (depth_level
* Precedence
.DEPTH
);
855 /* Compares two nodes to decide, which will be parent and which will be child. */
856 private bool cmp_nodes (ParseNode? left
, ParseNode? right
)
859 * true = right goes up (near root) in parse tree.
860 * false = left goes up (near root) in parse tree.
864 if (left
.precedence
> right
.precedence
)
866 else if (left
.precedence
< right
.precedence
)
869 return right
.associativity
!= Associativity
.RIGHT
;
872 /* Unified interface (unary and binary nodes) to insert node into parse tree. */
873 private void insert_into_tree_all (ParseNode node
, bool unary_function
)
881 ParseNode tmp
= right_most
;
882 while (cmp_nodes (tmp
, node
))
887 /* If tmp is null, that means, we have to insert new node at root. */
891 node
.right
.parent
= node
;
897 node
.right
= tmp
.right
;
898 if (node
.right
!= null)
899 node
.right
.parent
= node
;
902 if (tmp
.right
!= null)
903 tmp
.right
.parent
= tmp
;
907 while (right_most
.right
!= null)
908 right_most
= right_most
.right
;
912 /* If tmp is null, that means, we have to insert new node at root. */
916 node
.left
.parent
= node
;
922 node
.left
= tmp
.right
;
923 if (node
.left
!= null)
924 node
.left
.parent
= node
;
927 if (tmp
.right
!= null)
928 tmp
.right
.parent
= tmp
;
935 /* Insert binary node into the parse tree. */
936 private void insert_into_tree (ParseNode node
)
938 insert_into_tree_all (node
, false);
941 /* Insert unary node into the parse tree. */
942 private void insert_into_tree_unary (ParseNode node
)
944 insert_into_tree_all (node
, true);
947 /* Recursive call to free every node of parse-tree. */
948 private void destroy_all_nodes (ParseNode node
)
953 destroy_all_nodes (node
.left
);
954 destroy_all_nodes (node
.right
);
955 /* Don't call free for tokens, as they are allocated and freed in lexer. */
956 /* WARNING: If node.value is freed elsewhere, please assign it null before calling destroy_all_nodes (). */
959 /* LL (*) parser. Lookahead count depends on tokens. Handle with care. :P */
961 /* Check if string "name" is a valid variable for given Parser. It is the same code, used to get the value of variable in parserfunc.c. */
962 private bool check_variable (string name
)
964 /* If defined, then get the variable */
965 if (variable_is_defined (name
))
968 /* If has more than one character then assume a multiplication of variables */
971 while (name
.get_next_char (ref index
, out c
))
973 if (!variable_is_defined (c
.to_string ()))
980 private bool statement ()
982 var token
= lexer
.get_next_token ();
983 if (token
.type
== LexerTokenType
.VARIABLE
)
985 var token_old
= token
;
986 token
= lexer
.get_next_token ();
987 if (token
.type
== LexerTokenType
.ASSIGN
)
989 insert_into_tree (new
NameNode (this
, token_old
, make_precedence_p (Precedence
.NUMBER_VARIABLE
), get_associativity (token_old
)));
990 insert_into_tree (new
AssignNode (this
, token
, 0, get_associativity (token
)));
997 else if (token
.type
== LexerTokenType
.IN
)
1004 lexer
.get_next_token ();
1006 insert_into_tree (new
ConvertNode (this
, token
, 0, get_associativity (token
)));
1013 else if (token
.type
== LexerTokenType
.SUP_NUMBER
)
1015 token
= lexer
.get_next_token ();
1016 if (token
.type
== LexerTokenType
.IN
)
1023 lexer
.get_next_token ();
1025 insert_into_tree (new
ConvertNode (this
, token
, 0, get_associativity (token
)));
1055 else if (token
.type
== LexerTokenType
.NUMBER
)
1057 var token_old
= token
;
1058 token
= lexer
.get_next_token ();
1059 if (token
.type
== LexerTokenType
.VARIABLE
)
1061 token
= lexer
.get_next_token ();
1062 if (token
.type
== LexerTokenType
.IN
)
1067 insert_into_tree (new
ConstantNode (this
, token_old
, make_precedence_t (token_old
.type
), get_associativity (token
)));
1072 token
= lexer
.get_next_token ();
1073 insert_into_tree (new
ConvertNumberNode (this
, token
, 0, get_associativity (token
)));
1080 else if (token
.type
== LexerTokenType
.SUP_NUMBER
)
1082 token
= lexer
.get_next_token ();
1083 if (token
.type
== LexerTokenType
.IN
)
1089 insert_into_tree (new
ConstantNode (this
, token_old
, make_precedence_t (token_old
.type
), get_associativity (token
)));
1093 token
= lexer
.get_next_token ();
1095 insert_into_tree (new
ConvertNumberNode (this
, token
, 0, get_associativity (token
)));
1140 private bool unit ()
1142 var token
= lexer
.get_next_token ();
1143 if (token
.type
== LexerTokenType
.VARIABLE
)
1145 var token_old
= token
;
1146 token
= lexer
.get_next_token ();
1147 if (token
.type
== LexerTokenType
.SUP_NUMBER
)
1149 insert_into_tree (new
NameNode (this
, token_old
, make_precedence_t (token_old
.type
), get_associativity (token_old
), token_old
.text
+ token
.text
));
1155 insert_into_tree (new
NameNode (this
, token_old
, make_precedence_t (token_old
.type
), get_associativity (token_old
)));
1166 private bool expression ()
1168 if (!expression_1 ())
1170 if (!expression_2 ())
1176 private bool expression_1 ()
1178 var token
= lexer
.get_next_token ();
1180 if (token
.type
== LexerTokenType
.PL_EOS
|| token
.type
== LexerTokenType
.ASSIGN
)
1186 if (token
.type
== LexerTokenType
.L_R_BRACKET
)
1193 token
= lexer
.get_next_token ();
1194 if (token
.type
== LexerTokenType
.R_R_BRACKET
)
1199 //Expected ")" here...
1203 else if (token
.type
== LexerTokenType
.L_S_BRACKET
)
1207 /* Give round, preference of Precedence.UNKNOWN aka 0, to keep it on the top of expression. */
1209 insert_into_tree_unary (new
RoundNode (this
, token
, make_precedence_p (Precedence
.UNKNOWN
), get_associativity (token
)));
1214 token
= lexer
.get_next_token ();
1215 if (token
.type
== LexerTokenType
.R_S_BRACKET
)
1221 //Expected "]" here...
1224 else if (token
.type
== LexerTokenType
.L_C_BRACKET
)
1228 /* Give fraction, preference of Precedence.UNKNOWN aka 0, to keep it on the top of expression. */
1230 insert_into_tree_unary (new
FractionalComponentNode (this
, token
, make_precedence_p (Precedence
.UNKNOWN
), get_associativity (token
)));
1235 token
= lexer
.get_next_token ();
1236 if (token
.type
== LexerTokenType
.R_C_BRACKET
)
1241 //Expected "}" here...
1245 else if (token
.type
== LexerTokenType
.ABS
)
1249 /* Give abs, preference of Precedence.UNKNOWN aka 0, to keep it on the top of expression. */
1251 insert_into_tree_unary (new
AbsoluteValueNode (this
, token
, make_precedence_p (Precedence
.UNKNOWN
), get_associativity (token
)));
1256 token
= lexer
.get_next_token ();
1257 if (token
.type
== LexerTokenType
.ABS
)
1262 //Expected "|" here...
1266 else if (token
.type
== LexerTokenType
.NOT
)
1268 insert_into_tree_unary (new
NotNode (this
, token
, make_precedence_p (Precedence
.NOT
), get_associativity (token
)));
1275 else if (token
.type
== LexerTokenType
.NUMBER
)
1277 insert_into_tree (new
ConstantNode (this
, token
, make_precedence_t (token
.type
), get_associativity (token
)));
1279 token
= lexer
.get_next_token ();
1282 if (token
.type
== LexerTokenType
.FUNCTION
|| token
.type
== LexerTokenType
.VARIABLE
|| token
.type
== LexerTokenType
.SUB_NUMBER
|| token
.type
== LexerTokenType
.ROOT
|| token
.type
== LexerTokenType
.ROOT_3
|| token
.type
== LexerTokenType
.ROOT_4
)
1284 insert_into_tree (new
MultiplyNode (this
, null, make_precedence_p (Precedence
.MULTIPLY
), get_associativity_p (Precedence
.MULTIPLY
)));
1294 else if (token
.type
== LexerTokenType
.L_FLOOR
)
1297 /* Give floor, preference of Precedence.UNKNOWN aka 0, to keep it on the top of expression. */
1299 insert_into_tree_unary (new
FloorNode (this
, null, make_precedence_p (Precedence
.UNKNOWN
), get_associativity_p (Precedence
.UNKNOWN
)));
1304 token
= lexer
.get_next_token ();
1305 if (token
.type
== LexerTokenType
.R_FLOOR
)
1310 //Expected ⌋ here...
1314 else if (token
.type
== LexerTokenType
.L_CEILING
)
1317 /* Give ceiling, preference of Precedence.UNKNOWN aka 0, to keep it on the top of expression. */
1319 insert_into_tree_unary (new
CeilingNode (this
, null, make_precedence_p (Precedence
.UNKNOWN
), get_associativity_p (Precedence
.UNKNOWN
)));
1324 token
= lexer
.get_next_token ();
1325 if (token
.type
== LexerTokenType
.R_CEILING
)
1330 //Expected ⌉ here...
1334 else if (token
.type
== LexerTokenType
.SUBTRACT
)
1336 insert_into_tree_unary (new
UnaryMinusNode (this
, token
, make_precedence_p (Precedence
.UNARY_MINUS
), get_associativity_p (Precedence
.UNARY_MINUS
)));
1338 if (!expression_1 ())
1343 else if (token
.type
== LexerTokenType
.ADD
)
1345 token
= lexer
.get_next_token ();
1346 if (token
.type
== LexerTokenType
.NUMBER
)
1348 /* Ignore ADD. It is not required. */
1349 insert_into_tree (new
ConstantNode (this
, token
, make_precedence_t (token
.type
), get_associativity (token
)));
1365 private bool expression_2 ()
1367 var token
= lexer
.get_next_token ();
1368 if (token
.type
== LexerTokenType
.L_R_BRACKET
)
1370 insert_into_tree (new
MultiplyNode (this
, null, make_precedence_p (Precedence
.MULTIPLY
), get_associativity_p (Precedence
.MULTIPLY
)));
1375 token
= lexer
.get_next_token ();
1376 if (token
.type
== LexerTokenType
.R_R_BRACKET
)
1380 if (!expression_2 ())
1388 else if (token
.type
== LexerTokenType
.POWER
)
1390 insert_into_tree (new
XPowYNode (this
, token
, make_precedence_t (token
.type
), get_associativity (token
)));
1392 if (!expression_1 ())
1394 if (!expression_2 ())
1399 else if (token
.type
== LexerTokenType
.SUP_NUMBER
)
1401 insert_into_tree (new
XPowYIntegerNode (this
, null, make_precedence_p (Precedence
.POWER
), get_associativity_p (Precedence
.POWER
)));
1402 insert_into_tree (new
NameNode (this
, token
, make_precedence_p (Precedence
.NUMBER_VARIABLE
), get_associativity_p (Precedence
.NUMBER_VARIABLE
)));
1404 if (!expression_2 ())
1409 else if (token
.type
== LexerTokenType
.NSUP_NUMBER
)
1411 insert_into_tree (new
XPowYIntegerNode (this
, null, make_precedence_p (Precedence
.POWER
), get_associativity_p (Precedence
.POWER
)));
1412 insert_into_tree (new
NameNode (this
, token
, make_precedence_p (Precedence
.NUMBER_VARIABLE
), get_associativity_p (Precedence
.NUMBER_VARIABLE
)));
1414 if (!expression_2 ())
1419 else if (token
.type
== LexerTokenType
.FACTORIAL
)
1421 insert_into_tree_unary (new
FactorialNode (this
, token
, make_precedence_t (token
.type
), get_associativity (token
)));
1423 if (!expression_2 ())
1428 else if (token
.type
== LexerTokenType
.MULTIPLY
)
1430 insert_into_tree (new
MultiplyNode (this
, token
, make_precedence_t (token
.type
), get_associativity (token
)));
1432 if (!expression_1 ())
1434 if (!expression_2 ())
1439 else if (token
.type
== LexerTokenType
.PERCENTAGE
)
1441 insert_into_tree_unary (new
PercentNode (this
, token
, make_precedence_t (token
.type
), get_associativity (token
)));
1443 if (!expression_2 ())
1448 else if (token
.type
== LexerTokenType
.AND
)
1450 insert_into_tree (new
AndNode (this
, token
, make_precedence_t (token
.type
), get_associativity (token
)));
1452 if (!expression_1 ())
1454 if (!expression_2 ())
1459 else if (token
.type
== LexerTokenType
.OR
)
1461 insert_into_tree (new
OrNode (this
, token
, make_precedence_t (token
.type
), get_associativity (token
)));
1463 if (!expression_1 ())
1465 if (!expression_2 ())
1470 else if (token
.type
== LexerTokenType
.XOR
)
1472 insert_into_tree (new
XorNode (this
, token
, make_precedence_t (token
.type
), get_associativity (token
)));
1474 if (!expression_1 ())
1476 if (!expression_2 ())
1481 else if (token
.type
== LexerTokenType
.DIVIDE
)
1483 insert_into_tree (new
DivideNode (this
, token
, make_precedence_t (token
.type
), get_associativity (token
)));
1485 if (!expression_1 ())
1487 if (!expression_2 ())
1492 else if (token
.type
== LexerTokenType
.MOD
)
1494 insert_into_tree (new
ModulusDivideNode (this
, token
, make_precedence_t (token
.type
), get_associativity (token
)));
1496 if (!expression_1 ())
1498 if (!expression_2 ())
1503 else if (token
.type
== LexerTokenType
.ADD
)
1505 var node
= new
AddNode (this
, token
, make_precedence_t (token
.type
), get_associativity (token
));
1506 insert_into_tree (node
);
1508 if (!expression_1 ())
1511 token
= lexer
.get_next_token ();
1512 if (token
.type
== LexerTokenType
.PERCENTAGE
)
1514 //FIXME: This condition needs to be verified for all cases.. :(
1515 if (node
.right
.precedence
> Precedence
.PERCENTAGE
)
1517 node
.precedence
= Precedence
.PERCENTAGE
;
1518 node
.do_percentage
= true;
1523 /* Assume '%' to be part of 'expression PERCENTAGE' statement. */
1525 if (!expression_2 ())
1532 if (!expression_2 ())
1537 else if (token
.type
== LexerTokenType
.SUBTRACT
)
1539 var node
= new
SubtractNode (this
, token
, make_precedence_t (token
.type
), get_associativity (token
));
1540 insert_into_tree (node
);
1542 if (!expression_1 ())
1544 token
= lexer
.get_next_token ();
1545 if (token
.type
== LexerTokenType
.PERCENTAGE
)
1547 //FIXME: This condition needs to be verified for all cases.. :(
1548 if (node
.right
.precedence
> Precedence
.PERCENTAGE
)
1550 node
.precedence
= Precedence
.PERCENTAGE
;
1551 node
.do_percentage
= true;
1556 /* Assume '%' to be part of 'expression PERCENTAGE' statement. */
1558 if (!expression_2 ())
1565 if (!expression_2 ())
1577 private bool variable ()
1579 var token
= lexer
.get_next_token ();
1580 if (token
.type
== LexerTokenType
.FUNCTION
)
1582 var token_old
= token
;
1583 token
= lexer
.get_next_token ();
1584 if (token
.type
== LexerTokenType
.SUP_NUMBER
)
1586 /* Pass power as void * value. That will be taken care in pf_apply_func_with_power. */
1588 insert_into_tree_unary (new
FunctionWithPowerNode (this
, token_old
, make_precedence_t (token_old
.type
), get_associativity (token_old
), token
.text
));
1594 else if (token
.type
== LexerTokenType
.NSUP_NUMBER
)
1596 /* Pass power as void * value. That will be taken care in pf_apply_func_with_npower. */
1598 insert_into_tree_unary (new
FunctionWithNegativePowerNode (this
, token_old
, make_precedence_t (token_old
.type
), get_associativity (token_old
), token
.text
));
1607 insert_into_tree_unary (new
FunctionNode (this
, token_old
, make_precedence_t (token_old
.type
), get_associativity (token_old
)));
1614 else if (token
.type
== LexerTokenType
.SUB_NUMBER
)
1616 var token_old
= token
;
1617 token
= lexer
.get_next_token ();
1618 if (token
.type
== LexerTokenType
.ROOT
)
1620 insert_into_tree_unary (new
RootNode (this
, token
, make_precedence_t (token
.type
), get_associativity (token
), sub_atoi (token_old
.text
)));
1629 else if (token
.type
== LexerTokenType
.ROOT
)
1631 insert_into_tree_unary (new
RootNode (this
, token
, make_precedence_t (token
.type
), get_associativity (token
), 2));
1638 else if (token
.type
== LexerTokenType
.ROOT_3
)
1640 insert_into_tree_unary (new
RootNode (this
, token
, make_precedence_t (token
.type
), get_associativity (token
), 3));
1647 else if (token
.type
== LexerTokenType
.ROOT_4
)
1649 insert_into_tree_unary (new
RootNode (this
, token
, make_precedence_t (token
.type
), get_associativity (token
), 4));
1656 else if (token
.type
== LexerTokenType
.VARIABLE
)
1659 //TODO: unknown function ERROR for (VARIABLE SUP_NUMBER expression).
1669 private bool term ()
1671 var token
= lexer
.get_next_token ();
1673 if (token
.type
== LexerTokenType
.VARIABLE
)
1675 var token_old
= token
;
1676 /* Check if the token is a valid variable or not. */
1677 if (!check_variable (token
.text
))
1679 set_error (ErrorCode
.UNKNOWN_VARIABLE
, token
.text
, token
.start_index
, token
.end_index
);
1682 token
= lexer
.get_next_token ();
1683 if (token
.type
== LexerTokenType
.SUP_NUMBER
)
1684 insert_into_tree (new
VariableWithPowerNode (this
, token_old
, make_precedence_t (token_old
.type
), get_associativity (token_old
), token
.text
));
1688 insert_into_tree (new
VariableNode (this
, token_old
, make_precedence_t (token_old
.type
), get_associativity (token_old
)));
1700 private bool term_2 ()
1702 var token
= lexer
.get_next_token ();
1705 if (token
.type
== LexerTokenType
.PL_EOS
|| token
.type
== LexerTokenType
.ASSIGN
)
1708 if (token
.type
== LexerTokenType
.VARIABLE
)
1710 /* Insert multiply in between two distinct (variable). */
1711 insert_into_tree (new
MultiplyNode (this
, null, make_precedence_p (Precedence
.MULTIPLY
), get_associativity_p (Precedence
.MULTIPLY
)));