/vqdr/trunk

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/vqdr/trunk

« back to all changes in this revision

Viewing changes to src/libvqdr/operator/divide_operator_token.vala

  • Committer: Gustav Hartvigsson
  • Date: 2020-06-07 18:48:24 UTC
  • Revision ID: gustav.hartvigsson@gmail.com-20200607184824-jf14f7a1b1di2i2q
* Initial code - far from done

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
namespace VQDR.Expression {
 
2
  
 
3
  class DivideOperatorToken : OperatorToken {
 
4
    
 
5
    construct {
 
6
      mandatory_num_child = 2;
 
7
      priority = PRIO_MULTIPLICATIVE;
 
8
    }
 
9
    
 
10
    protected DivideOperatorToken (int position) {
 
11
      base (position);
 
12
    }
 
13
    
 
14
    public override void evaluate_self (Context instance) throws GLib.Error {
 
15
      Token r_child = get_right_child (),
 
16
            l_child = get_left_child ();
 
17
             
 
18
      l_child.evaluate (instance);
 
19
      r_child.evaluate (instance);
 
20
      
 
21
      // Division by zero is not somthing we want to do.
 
22
      // Black holes are bad.
 
23
      if (l_child.result_value == 0) {
 
24
        throw new VQDR.Common.MathError.DIVIDE_BY_ZERO
 
25
        ("(DivideOperationToken) The left value is less than zero.");
 
26
      }
 
27
      
 
28
      var sb = new GLib.StringBuilder ("(");
 
29
      
 
30
      // We check the precidence of the token, and inclose the value if
 
31
      // it has a lower precidence than multiply, if it is, we enclose it in a
 
32
      // praranthesis.
 
33
      if (l_child.priority > 0 && l_child.priority < priority) {
 
34
        sb.append ("(").append (l_child.result_string).append (")");
 
35
        //l_child.result_string = "(" + l_child.result_string + ")";
 
36
      } else {
 
37
        sb.append (l_child.result_string);
 
38
      }
 
39
      
 
40
      sb.append ("*");
 
41
      
 
42
      // We do the same with the othre child.
 
43
      if (r_child.priority > 0 && r_child.priority < priority) {
 
44
        sb.append ("(").append (r_child.result_string).append (")");
 
45
        //r_child.result_string = "(" + r_child.result_string + ")";
 
46
      } else {
 
47
        sb.append (r_child.result_string);
 
48
      }
 
49
      
 
50
      sb.append (")");
 
51
      
 
52
      result_value = l_child.result_value * l_child.result_value;
 
53
      result_max_value = l_child.result_max_value * l_child.result_max_value;
 
54
      reorder_max_min_values ();
 
55
      result_string = sb.str;
 
56
       
 
57
    }
 
58
    
 
59
  }
 
60
  
 
61
  
 
62
}