libnxsl; nxscript; nxlptest - synched
[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_REFERENCE 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
109
110 //
111 // Modified lexer class
112 //
113
114 class NXSL_Compiler;
115
116 class NXSL_Lexer
117 {
118 friend int yylex(YYSTYPE *, yyscan_t);
119
120 protected:
121 int m_nSourceSize;
122 int m_nSourcePos;
123 char *m_pszSourceCode;
124 NXSL_Compiler *m_pCompiler;
125
126 int m_nCurrLine;
127 int m_nCommentLevel;
128 int m_nStrSize;
129 char m_szStr[MAX_STRING_SIZE];
130 BOOL m_bErrorState;
131
132 public:
133 NXSL_Lexer(NXSL_Compiler *pCompiler, const TCHAR *pszCode);
134 virtual ~NXSL_Lexer();
135
136 int LexerInput(char *pBuffer, int nMaxSize);
137
138 int GetCurrLine(void) { return m_nCurrLine; }
139 void Error(const char *pszText);
140
141 void SetErrorState(void) { m_bErrorState = TRUE; }
142 BOOL IsErrorState(void) { return m_bErrorState; }
143 };
144
145
146 //
147 // Compiler class
148 //
149
150 class NXSL_Compiler
151 {
152 protected:
153 TCHAR *m_pszErrorText;
154 NXSL_Lexer *m_pLexer;
155 NXSL_Stack *m_pAddrStack;
156 NXSL_Stack *m_pBreakStack;
157 int m_idOpCode;
158
159 public:
160 NXSL_Compiler(void);
161 ~NXSL_Compiler();
162
163 NXSL_Program *Compile(const TCHAR *pszSourceCode);
164 void Error(const char *pszMsg);
165
166 TCHAR *GetErrorText(void) { return CHECK_NULL(m_pszErrorText); }
167
168 void PushAddr(DWORD dwAddr) { m_pAddrStack->Push(CAST_TO_POINTER(dwAddr, void *)); }
169 DWORD PopAddr(void);
170 DWORD PeekAddr(void);
171
172 void AddBreakAddr(DWORD dwAddr);
173 void CloseBreakLevel(NXSL_Program *pScript);
174 BOOL CanUseBreak(void) { return m_pBreakStack->Size() > 0; }
175 void NewBreakLevel(void) { m_pBreakStack->Push(new Queue); }
176
177 void SetIdentifierOperation(int opcode) { m_idOpCode = opcode; }
178 int GetIdentifierOperation(void) { return m_idOpCode; }
179 };
180
181
182 //
183 // Global variables
184 //
185
186 extern const char *g_szTypeNames[];
187
188
189 #endif