license changed to LGPL for libnxcl, libnxsnmp, libnxlp, libnxsl, and libnxmap
[public/netxms.git] / src / libnxsl / instruction.cpp
1 /*
2 ** NetXMS - Network Management System
3 ** NetXMS Scripting Language Interpreter
4 ** Copyright (C) 2003-2010 Victor Kirhenshtein
5 **
6 ** This program is free software; you can redistribute it and/or modify
7 ** it under the terms of the GNU Lesser General Public License as published by
8 ** the Free Software Foundation; either version 3 of the License, or
9 ** (at your option) any later version.
10 **
11 ** This program is distributed in the hope that it will be useful,
12 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
13 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 ** GNU General Public License for more details.
15 **
16 ** You should have received a copy of the GNU Lesser General Public License
17 ** along with this program; if not, write to the Free Software
18 ** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 **
20 ** File: instruction.cpp
21 **
22 **/
23
24 #include "libnxsl.h"
25
26
27 //
28 // Constructors
29 //
30
31 NXSL_Instruction::NXSL_Instruction(int nLine, int nOpCode)
32 {
33 m_nOpCode = nOpCode;
34 m_nSourceLine = nLine;
35 m_nStackItems = 0;
36 }
37
38 NXSL_Instruction::NXSL_Instruction(int nLine, int nOpCode, NXSL_Value *pValue)
39 {
40 m_nOpCode = nOpCode;
41 m_nSourceLine = nLine;
42 m_operand.m_pConstant = pValue;
43 m_nStackItems = 0;
44 }
45
46 NXSL_Instruction::NXSL_Instruction(int nLine, int nOpCode, char *pszString)
47 {
48 m_nOpCode = nOpCode;
49 m_nSourceLine = nLine;
50 m_operand.m_pszString = pszString;
51 m_nStackItems = 0;
52 }
53
54 NXSL_Instruction::NXSL_Instruction(int nLine, int nOpCode, char *pszString, int nStackItems)
55 {
56 m_nOpCode = nOpCode;
57 m_nSourceLine = nLine;
58 m_operand.m_pszString = pszString;
59 m_nStackItems = nStackItems;
60 }
61
62 NXSL_Instruction::NXSL_Instruction(int nLine, int nOpCode, DWORD dwAddr)
63 {
64 m_nOpCode = nOpCode;
65 m_nSourceLine = nLine;
66 m_operand.m_dwAddr = dwAddr;
67 }
68
69 NXSL_Instruction::NXSL_Instruction(int nLine, int nOpCode, int nStackItems)
70 {
71 m_nOpCode = nOpCode;
72 m_nSourceLine = nLine;
73 m_nStackItems = nStackItems;
74 }
75
76 NXSL_Instruction::NXSL_Instruction(NXSL_Instruction *pSrc)
77 {
78 m_nOpCode = pSrc->m_nOpCode;
79 m_nSourceLine = pSrc->m_nSourceLine;
80 m_nStackItems = pSrc->m_nStackItems;
81 switch(m_nOpCode)
82 {
83 case OPCODE_PUSH_CONSTANT:
84 m_operand.m_pConstant = new NXSL_Value(pSrc->m_operand.m_pConstant);
85 break;
86 case OPCODE_PUSH_VARIABLE:
87 case OPCODE_SET:
88 case OPCODE_CALL_EXTERNAL:
89 case OPCODE_BIND:
90 case OPCODE_ARRAY:
91 case OPCODE_INC:
92 case OPCODE_DEC:
93 case OPCODE_INCP:
94 case OPCODE_DECP:
95 case OPCODE_GET_ATTRIBUTE:
96 case OPCODE_SET_ATTRIBUTE:
97 m_operand.m_pszString = strdup(pSrc->m_operand.m_pszString);
98 break;
99 default:
100 m_operand.m_dwAddr = pSrc->m_operand.m_dwAddr;
101 break;
102 }
103 }
104
105
106 //
107 // Destructor
108 //
109
110 NXSL_Instruction::~NXSL_Instruction()
111 {
112 switch(m_nOpCode)
113 {
114 case OPCODE_PUSH_VARIABLE:
115 case OPCODE_CALL_EXTERNAL:
116 case OPCODE_SET:
117 case OPCODE_BIND:
118 case OPCODE_ARRAY:
119 case OPCODE_INC:
120 case OPCODE_DEC:
121 case OPCODE_INCP:
122 case OPCODE_DECP:
123 case OPCODE_GET_ATTRIBUTE:
124 case OPCODE_SET_ATTRIBUTE:
125 safe_free(m_operand.m_pszString);
126 break;
127 case OPCODE_PUSH_CONSTANT:
128 delete m_operand.m_pConstant;
129 break;
130 default:
131 break;
132 }
133 }