~vbursian/research-assistant/intervers

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
////////////////////////////////////////////////////////////////////////////////
/*! @file NetMath.cpp   Basic expression interpreter.
- Part of RANet - Research Assistant Net Library.
- Copyright(C) 2014, Viktor E. Bursian, St.Petersburg, Russia.
                     Viktor.Bursian@mail.ioffe.ru
*///////////////////////////////////////////////////////////////////////////////
#include "NetMath.h"
#include "ExpressionNode.h"
#include "DataArrayFolder.h"
#include "DataListFolder.h"
#include "DataTableFolder.h"
#include "DataArrayRecord.h"
#include "DataListRecord.h"
#include "DataTableRecord.h"
namespace RA {
//------------------------------------------------------------------------------

//----------------------------------------------------------------- sNetMath ---

sNetMath::sMathOpRegistrator::sMathOpRegistrator ()
{
}


pcsFunction  sNetMath::GetMathFunctionFrom (sNodePtr  node)
{
  pcsFunction                 Func = NULL;
  psMathValue                 Value = NULL;
  if(    get_math_value_from<sExpressionNode>(node,Value)
      || get_math_value_from<sDataArrayFolder>(node,Value)
      || get_math_value_from<sDataListFolder>(node,Value)
      || get_math_value_from<sDataTableFolder>(node,Value)
      || get_math_value_from<sDataArrayRecord>(node,Value)
      || get_math_value_from<sDataListRecord>(node,Value)
      || get_math_value_from<sDataTableRecord>(node,Value) )
    Func = dynamic_cast<pcsFunction>(Value);
  return Func;
}


psMathValue  sNetMath::Term ()
{
  int                         StartCursor = Cursor;
  int                         AttrNameStart = Cursor;
  psMathValue                 Result = NULL;
  sNodePtr                    ValueNode = Host;
  sString                     Symbolic;
  integer                     Numeric;
  integer                     Integer;
  sPhysValue                  PhysValue;

  if( ! ValueNode.IsNULL() && ! End() && (   (Source[Cursor] == '#')
                                          || (Source[Cursor] == '@') ) ){
    if( ! ValueNode.IsNULL() && ! End() && (Source[Cursor] == '#') ){
      int  Y;  int  M;  int  D;  int  N;
      if( ! sRecord::ParseIdentifier(Source,Cursor,Y,M,D,N) ){
        return new sMathError("incorrect form of a record reference"
                             ,StartCursor,Cursor);
      }else{
        sPtr<sRecord>                 R = sRecord::FindRecord(Y,M,D,N);
        if( R.IsNULL() ){
          return new sMathError("cannot find such record",StartCursor,Cursor);
        }else{
          ValueNode = R;
        }
      }
    }
    while( ! ValueNode.IsNULL() && ! End() && (Source[Cursor] == '@') ){
      Cursor++;
      AttrNameStart = Cursor;
      if( (Cursor < SourceLength) && (Source[Cursor] == '\'') ){
        sString                     Tail = Source.Tail(Cursor+1);
        size_t                      CC = Tail.Pos("'");
        if( ! (CC < Tail.Length()) ){
          return new sMathError("missed closing apostrophe"
                               ,Cursor-1,Cursor+1);
        }else{
          Symbolic = Tail.Substr(0,CC);
          if( Symbolic.Empty() ){
            return new sMathError("empty symbolic attribute name"
                                 ,Cursor-1,Cursor+2);
          }else{
            ValueNode = ValueNode >> Symbolic;
            Cursor += CC + 2;
          }
        }
      }else if( (Cursor < SourceLength) && (Source[Cursor] == '-') ){
        Cursor++;
        if( IntegerNumber(Numeric) ){
          ValueNode = ValueNode >> (-Numeric);
        }else{
          return new sMathError("incorrect numeric attribute name"
                               ,Cursor-1,Cursor+1);
        }
      }else if( Identifier(Symbolic) ){
        ValueNode = ValueNode >> Symbolic;
      }else if( IntegerNumber(Numeric) ){
        ValueNode = ValueNode >> Numeric;
      }else{
        return new sMathError("incorrect form of attribute name"
                                " (probably missed '...')"
                             ,Cursor-1,Cursor+1);
      }
    }
    if( ValueNode.IsNULL() ){
      return new sMathError("no such attribute",AttrNameStart,Cursor);
    }else if( get_from<sIntegerNode>(ValueNode,Integer) ){
      Result = new sIntegerValue(Integer);
    }else if( get_from<sPhysValueNode>(ValueNode,PhysValue) ){
      Result = new sPhysValue(PhysValue);
    }else if( get_math_value_from<sExpressionNode>(ValueNode,Result) ){
    }else if( get_math_value_from<sDataArrayFolder>(ValueNode,Result) ){
    }else if( get_math_value_from<sDataListFolder>(ValueNode,Result) ){
    }else if( get_math_value_from<sDataTableFolder>(ValueNode,Result) ){
    }else if( get_math_value_from<sDataArrayRecord>(ValueNode,Result) ){
    }else if( get_math_value_from<sDataListRecord>(ValueNode,Result) ){
    }else if( get_math_value_from<sDataTableRecord>(ValueNode,Result) ){
    }else{
      return new sMathError("unknown type of value",StartCursor,Cursor);
    }
  }else{
    Result = sPhysMath::Term();
  }
  return Result;
}

//------------------------------------------------------------------------------
} //namespace RA