d10eb1ad06e7f0e36314d852113f9d8b9a24ee4b
[public/netxms.git] / src / libnxsl / libnxsl.h
1 /* $Id$ */
2 /*
3 ** NetXMS - Network Management System
4 ** NetXMS Scripting Language Interpreter
5 ** Copyright (C) 2005-2009 Victor Kirhenshtein
6 **
7 ** This program is free software; you can redistribute it and/or modify
8 ** it under the terms of the GNU General Public License as published by
9 ** the Free Software Foundation; either version 2 of the License, or
10 ** (at your option) any later version.
11 **
12 ** This program is distributed in the hope that it will be useful,
13 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
14 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 ** GNU General Public License for more details.
16 **
17 ** You should have received a copy of the GNU General Public License
18 ** along with this program; if not, write to the Free Software
19 ** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 **
21 ** File: libnxsl.h
22 **
23 **/
24
25 #ifndef _libnxsl_h_
26 #define _libnxsl_h_
27
28 #include <nms_common.h>
29 #include <nms_util.h>
30 #include <nxcpapi.h>
31 #include <nxsl.h>
32 #include <nxqueue.h>
33
34 #ifndef min
35 #define min(a, b) ((a) < (b) ? (a) : (b))
36 #endif
37 #ifndef max
38 #define max(a, b) ((a) > (b) ? (a) : (b))
39 #endif
40
41 union YYSTYPE;
42 typedef void *yyscan_t;
43
44
45 //
46 // Various defines
47 //
48
49 #define INVALID_ADDRESS ((DWORD)0xFFFFFFFF)
50 #define MAX_STRING_SIZE 8192
51
52
53 //
54 // Instruction opcodes
55 //
56
57 #define OPCODE_NOP 0
58 #define OPCODE_RETURN 1
59 #define OPCODE_JMP 2
60 #define OPCODE_CALL 3
61 #define OPCODE_CALL_EXTERNAL 4
62 #define OPCODE_PUSH_CONSTANT 5
63 #define OPCODE_PUSH_VARIABLE 6
64 #define OPCODE_EXIT 7
65 #define OPCODE_POP 8
66 #define OPCODE_SET 9
67 #define OPCODE_ADD 10
68 #define OPCODE_SUB 11
69 #define OPCODE_MUL 12
70 #define OPCODE_DIV 13
71 #define OPCODE_REM 14
72 #define OPCODE_EQ 15
73 #define OPCODE_NE 16
74 #define OPCODE_LT 17
75 #define OPCODE_LE 18
76 #define OPCODE_GT 19
77 #define OPCODE_GE 20
78 #define OPCODE_BIT_AND 21
79 #define OPCODE_BIT_OR 22
80 #define OPCODE_BIT_XOR 23
81 #define OPCODE_AND 24
82 #define OPCODE_OR 25
83 #define OPCODE_LSHIFT 26
84 #define OPCODE_RSHIFT 27
85 #define OPCODE_RET_NULL 28
86 #define OPCODE_JZ 29
87 #define OPCODE_PRINT 30
88 #define OPCODE_CONCAT 31
89 #define OPCODE_BIND 32
90 #define OPCODE_INC 33
91 #define OPCODE_DEC 34
92 #define OPCODE_NEG 35
93 #define OPCODE_NOT 36
94 #define OPCODE_BIT_NOT 37
95 #define OPCODE_CAST 38
96 #define OPCODE_GET_ATTRIBUTE 39
97 #define OPCODE_INCP 40
98 #define OPCODE_DECP 41
99 #define OPCODE_JNZ 42
100 #define OPCODE_LIKE 43
101 #define OPCODE_ILIKE 44
102 #define OPCODE_MATCH 45
103 #define OPCODE_IMATCH 46
104 #define OPCODE_CASE 47
105 #define OPCODE_ARRAY 48
106 #define OPCODE_GET_ELEMENT 49
107 #define OPCODE_SET_ELEMENT 50
108 #define OPCODE_SET_ATTRIBUTE 51
109
110
111 //
112 // Modified lexer class
113 //
114
115 class NXSL_Compiler;
116
117 class NXSL_Lexer
118 {
119 friend int yylex(YYSTYPE *, yyscan_t);
120
121 protected:
122 int m_nSourceSize;
123 int m_nSourcePos;
124 char *m_pszSourceCode;
125 NXSL_Compiler *m_pCompiler;
126
127 int m_nCurrLine;
128 int m_nCommentLevel;
129 int m_nStrSize;
130 char m_szStr[MAX_STRING_SIZE];
131 BOOL m_bErrorState;
132
133 public:
134 NXSL_Lexer(NXSL_Compiler *pCompiler, const TCHAR *pszCode);
135 virtual ~NXSL_Lexer();
136
137 int lexerInput(char *pBuffer, int nMaxSize);
138
139 int getCurrLine() { return m_nCurrLine; }
140 void error(const char *pszText);
141
142 void setErrorState() { m_bErrorState = TRUE; }
143 BOOL isErrorState() { return m_bErrorState; }
144 };
145
146
147 //
148 // Compiler class
149 //
150
151 class NXSL_Compiler
152 {
153 protected:
154 TCHAR *m_pszErrorText;
155 NXSL_Lexer *m_pLexer;
156 NXSL_Stack *m_pAddrStack;
157 NXSL_Stack *m_pBreakStack;
158 int m_idOpCode;
159
160 public:
161 NXSL_Compiler();
162 ~NXSL_Compiler();
163
164 NXSL_Program *compile(const TCHAR *pszSourceCode);
165 void error(const char *pszMsg);
166
167 const TCHAR *getErrorText() { return CHECK_NULL(m_pszErrorText); }
168
169 void pushAddr(DWORD dwAddr) { m_pAddrStack->push(CAST_TO_POINTER(dwAddr, void *)); }
170 DWORD popAddr();
171 DWORD peekAddr();
172
173 void addBreakAddr(DWORD dwAddr);
174 void closeBreakLevel(NXSL_Program *pScript);
175 BOOL canUseBreak() { return m_pBreakStack->getSize() > 0; }
176 void newBreakLevel() { m_pBreakStack->push(new Queue); }
177
178 void setIdentifierOperation(int opcode) { m_idOpCode = opcode; }
179 int getIdentifierOperation() { return m_idOpCode; }
180 };
181
182
183 //
184 // Global variables
185 //
186
187 extern const char *g_szTypeNames[];
188
189
190 #endif