-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathvar.cpp
More file actions
141 lines (105 loc) · 2.66 KB
/
var.cpp
File metadata and controls
141 lines (105 loc) · 2.66 KB
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
136
137
138
139
140
141
#include<iostream>
#include<string>
#include<stdlib.h>
#include<conio.h>
//#include "to_string.cpp"
using namespace std;
int lin = 0;
//Variable Linked List Stack
class var{
public:
string name;
int type;
string value;
var *next;
var(){
type = 'u';
next = nullptr;
}
void addvar(string vname);
bool assignment(string vname,string value,int valtype);
string returnval(string vname);
void intadd(string vname, int adduct);
int delvar(string vname);
}*prime = nullptr;
//Push a varible to the stack
void var::addvar(string vname){
if(prime == nullptr){
prime = new var;
prime->name = vname;
prime->next = nullptr;
}
else{
var *temp = new var;
temp->name = vname;
temp->next = prime;
prime = temp;
}
}
//Assign a value to the variable
bool var::assignment(string vname, string value, int valtype){
var *temp = prime;
while(temp){
if(temp->name == vname){
temp->type = valtype;
temp->value = value;
return true;
}
temp = temp->next;
}
return false;
}
//Return the value of a given variable
string var::returnval(string vname){
var *temp = prime;
bool flag = false;
while(temp){
if(temp->name == vname){
flag = true;
if(temp->type == 0 || temp->type == 1)
return temp->value.substr(1,temp->value.length()-2);
else return temp->value.c_str();
}
temp = temp->next;
}
if(flag == false){
cout<<"\n\nInvalid variable token specified in line "<<lin<<endl;
cout<<"\n\nProgram Terminated.";
exit(0);
}
return "0";
}
//Perform addition to a variable (only used in for loop)
void var::intadd(string vname, int adduct){
var *temp = prime;
while(temp){
if(temp->name == vname){
int tem = atoi(temp->value.c_str());
tem += adduct;
temp->value = to_string(tem);
}
temp = temp->next;
}
}
//Delete topmost occurence of the given variable in the stack
int var::delvar(string vname){
var *temp = prime, *bridge = prime;
while(temp){
if(temp->name == vname){
if(prime == temp){
prime = prime->next;
delete temp;
return 1;
}
else{
while(bridge->next != temp)
bridge = bridge->next;
bridge->next = (bridge->next)->next;
delete temp;
return 1;
}
}
temp = temp->next;
}
return 0;
}