summaryrefslogtreecommitdiffstats
path: root/kommander/widget/function.cpp
blob: 3bcd21cda3d8c4e6023ae4376cdc9f8ee072f6fb (plain)
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
132
133
134
135
/***************************************************************************
                    function.cpp - Functions for internal parser
                             -------------------
    copyright          : (C) 2004      Michal Rudolf <[email protected]>
    
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software{} you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation{} either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#include "function.h"

using namespace Parse;

Function::Function() : m_function(0), m_minArgs(0), m_maxArgs(0)
{
}

Function::Function(FunctionPointer fp, ValueType value, const TypeList& params, uint min, uint max)
  : m_params(params)
{
  m_function = fp;
  m_returnValue = value;
  m_minArgs = min <= m_params.count() ? min : m_params.count();
  m_maxArgs = max >= m_params.count() ? max : m_params.count();
}

Function::Function(FunctionPointer fp, ValueType value, ValueType param1, uint min, uint max)
{
  m_function = fp;
  m_returnValue = value;
  m_params.append(param1);
  m_minArgs = min <= 1 ? min : 1;
  m_maxArgs = max >= 1 ? max : 1;
}

Function::Function(FunctionPointer fp, ValueType value, ValueType param1, ValueType param2, uint min , 
         uint max)
{
  m_function = fp;
  m_returnValue = value;
  m_params.append(param1);
  m_params.append(param2);
  m_minArgs = min <= 2 ? min : 2;
  m_maxArgs = max >= 2 ? max : 2;
}

Function::Function(FunctionPointer fp, ValueType value, ValueType param1, ValueType param2, ValueType param3, 
         uint min, uint max)
{
  m_function = fp;
  m_returnValue = value;
  m_params.append(param1);
  m_params.append(param2);
  m_params.append(param3);
  m_minArgs = min <= 3 ? min : 3;
  m_maxArgs = max >= 3 ? max : 3;
}

Function::Function(FunctionPointer fp, ValueType value, ValueType param1, ValueType param2, ValueType param3, 
                   ValueType param4, uint min, uint max)
{
  m_function = fp;
  m_returnValue = value;
  m_params.append(param1);
  m_params.append(param2);
  m_params.append(param3);
  m_params.append(param4);
  m_minArgs = min <= 4 ? min : 4;
  m_maxArgs = max >= 4 ? max : 4;
}

Function::Function(FunctionPointer fp, ValueType value, ValueType param1, ValueType param2, ValueType param3, 
                   ValueType param4, ValueType param5, uint min, uint max)
{
  m_function = fp;
  m_returnValue = value;
  m_params.append(param1);
  m_params.append(param2);
  m_params.append(param3);
  m_params.append(param4);
  m_params.append(param5);
  m_minArgs = min <= 5 ? min : 5;
  m_maxArgs = max >= 5 ? max : 5;
}

bool Function::isVoid() const
{
  return returnValue() == ValueNone;
}

ValueType Function::returnValue() const
{
  return m_returnValue;
}

ValueType Function::argType(uint i) const
{
  if (i < m_params.count())
    return m_params[i];
  else if (i < m_maxArgs)
    return m_params.last();
  else
    return ValueNone;
}

uint Function::minArgs() const
{
  return m_minArgs;
}

uint Function::maxArgs() const
{
  return m_maxArgs;
}

bool Function::isValid(const ParameterList& params) const
{
  return params.count() >= minArgs() && params.count() <= maxArgs();
}

ParseNode Function::execute(Parser* P, const ParameterList& params) const
{
  if (m_function)
    return m_function(P, params);
  else
    return ParseNode();
}