New child language - (Function member address (structure))

Pages: 1... 34567... 20
Jackson Marie, I don't know if this is what you're looking for, but there's a really good tutorial on a recursive-descent/operator-precedence parser for a working functional language here: http://llvm.org/docs/tutorial/

EDIT: You can disregard the llvm-stuff and just use the lexer/parser part, like I have done for several toy languages :)
Last edited on
Wow wow wow TheIdeasMan!!! I like this stack feature!!!
I'll try this XDXD TheIdeasMan@!!
That's it, the stack problem has been solved, only parsing process (Reading and parsing a file and do everything if possible) Now I realized that programming are only calculations and calculations, and no anything else... ) Great!!!
Oh... I like this!!! Now, I'm totally ready XDXD !!! Making an interpreter is very easy, but it.. takes time... Certainly, it's always.. true.
Stack? Yes. It's the most important primary thing 100%!!! I throw an object, then other machines will wait for it... And catch!!!
The gigantic world, the huge world, the large world, the very big world, the big world, the medium world, the small world, the very small world, the tiny world...
I'll come back soon. Hope the idea... will come true someday....
=*@TheIdeasMan!!!!
Last edited on
Uh... TheIdeasMan's suggestion is exactly what I said two pages back:
http://www.cplusplus.com/forum/lounge/85713/3/#msg464893
Both the Assembly I suggested earlier and RPN evaluation are based on a stack machine execution model. For example, the RPN string "1 2 + 3 *" translates to
1
2
3
4
5
push 1
push 2
add
push 3
mul
It's literally the same thing, save for the syntax.
@helios

Of course, maybe there has been so much BS (by the OP) since then ....

Simple ideas seem lost on the OP, she is still dead keen on the Starship Enterprise, anything simpler seems to be ignored, along with generic planning advice. As well, it is difficult to offer solutions (like your code), because she doesn't understand C++, and cannot even write C properly.

Comments by the OP like this:

The OP wrote:
I throw an object, then other machines will wait for it... And catch!!!


Presumably that is an interpretation of exceptions and a try - catch block, something I mentioned as an alternative to goto. See what I mean?

It's almost like she is delusional, and if I was going to be Dr. Phil, I would say she lacks self esteem and is seeking attention.

I still think the best thing would be to ignore her unless she posts some credible code of something easier like what I proposed as a starting example.
@TheIdeasWoman I'm confused - perhaps I missed the part where the OP said they were female?
@L B

HAHA

Some friendly advice (In Jest of course): Anyone who thinks I am a woman (or refers to me as such) should come down here to find out in person. Upon arriving they will see it is not a good idea to eff with me. :D

Ages ago she said her given name was Marie, there haven't been any objections to female references.

However this whole thing (all JM's posts) could still be a very elaborate troll - so JM could be anyone.
@L B
The OP's user profile says so. Even so, since English lacks singular gender-neutral pronouns, it's just as reasonable to call a person of unknown gender "she" as it is to call them "he".
Here's some more evidence the OP doesn't understand C++:

This version of helios' code uses a stack:

http://www.cplusplus.com/forum/lounge/11845/#msg56331


Line 6 is a real give away .......

It is also an example of how the OP ignores advice from experienced members.

Here is the OP's reply to a mention of using a stack:

Wow wow wow TheIdeasMan!!! I like this stack feature!!!
I'll try this XDXD TheIdeasMan@!!
That's it, the stack problem has been solved, only parsing process (Reading and parsing a file and do everything if possible) Now I realized that programming are only calculations and calculations, and no anything else... ) Great!!!
Oh... I like this!!! Now, I'm totally ready XDXD !!! Making an interpreter is very easy, but it.. takes time... Certainly, it's always.. true.
Stack? Yes. It's the most important primary thing 100%!!! I throw an object, then other machines will wait for it... And catch!!!


@JM are you sick of all this yet? Let's see your RPN calculator using a stack. If we do see it, then I might upgrade my opinion of you a smidgin.

I think I should make a tree stack structure...
But I have no idea. Please help...
Well, I was bored.
http://pastebin.com/ehNr36XY

Examples:
Computing polar coordinates of vector (7,13) (prints theta then r)
7 13 dup 2 ^ 3 rot dup 2 ^ 3 rot + sqrt 3 rot 3 rot atan2 $ pop $ pop

Sum all integers between 1 and 10:
0 1 begin dup 3 rot + swap 1 + dup 10 swap - 1 if end leave pop $ pop

Numerically approximate by the trapezoid rule the definite integral of sin() between 0 and pi with a step of 1/128:
0 0 1 128 / begin dup 3 rot dup 3 rot + sin swap dup sin 3 rot + 3 rot dup 3 rot * 2 / 4 rot + 3 rot 3 rot dup 3 rot + dup 3 rot swap pi swap - 1 if end leave pop pop $ pop

The problem with 0-operand instruction sets is that keeping track of intermediate results is a bitch. Hence all the 3 rot sequences.

I think I should make a tree stack structure...
Yeah. That totally makes sense.
Last edited on
Ok the the stack machine is almost done. @helios, I see your code example, and I'm making a similar style which gives anyone an easy look, And editing and adding additional function features (fancy stuff..) now is very easy!!!
I've included basic variable calculations (add, subtract, multiply, divide, equal, negative, modulus,...) And I'm going to add some bitwise operator detectors... A variable target pointer can be used if needed. But I got a trouble : Multiplication (x) and (:) are always performed first, then addition and subtraction...
An example :
x = 5 + 2 * 8

x = the final value of the expression 5 + 2 * 8
I'm going to write :
x += 5 += 2 *= 8
But it's odd because it's totally wrong. It's very hard problem and when I have to handle a main expression that contains many small expressions with their variables and values, and calculation modes...

But, I'm afraid of posting some code example demonstration here because... I think...

But, I'm looking forward to any suggestion and opinion about any better new algorithm solution and wonderful method...
@helios
What operation is $?
Peek and print the top.
Some friendly advice (In Jest of course): Anyone who thinks I am a woman (or refers to me as such) should come down here to find out in person. Upon arriving they will see it is not a good idea to eff with me. :D


Am I the only one who sees through the fake smiles. Am I the only one who sees this as cyber-threatening?
Tim wouldn't threaten anyone!
♪ ♫
"TheIdeasMan and Jackson Marie in a red-black tree,
F-i-g-ht-i-n-g"
♪ ♫
Am I the only one who sees this as cyber-threatening?


Tim wouldn't threaten anyone!


L B is right, I mean what I say when I say "In Jest of course" , my comment was more of a comical warning, coming from the comical gender replacement.

Edit: L B and I just had a big ole discussion in one of his threads - a lot of ideas exchanged, so I think we get along just fine.

@Grey Wolf

"TheIdeasMan and Jackson Marie in a red-black tree,
F-i-g-ht-i-n-g"


I see it more as education - the more stubborn the student - the harsher the education methods.

Edit: I hope you can back me up in what I am saying as you have in the past, because I see you as being one of the sensible realistic people.

@JM

But, I'm afraid of posting some code example demonstration here because... I think...


Don't worry, if you post the code then at least it will show that you have had a go - which would be commendable. If you started with helios' code then that is a good start. As long as you keep with the methodology. Also there would be no realistic advice that can be given unless we can see what you have done.

If I can see that you have had a fair go at the problem, then I might offer some gentle suggestions.

The reasons for giving harsh advice, would be for not listening to advice, or where the methodology is so far from reality that it is ridiculous, or you continue to write unconventional code, or you try to help others while exhibiting all these bad traits.

Also, I may not need to offer any comment at all, there are plenty of others much more knowledgeable than me who might help. However, if you present some nightmare code, then that might be the cause of not getting any (or few) replies, and you might miss out on help from those best capable of helping.

And, if you repeatedly don't listen to advice, then that is why you would attract ridicule, as opposed to constructive help.

So let's see what you have, (we are all waiting to give some great advice) and we can all go from there.


Last edited on
Finished!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
This is the stack structure tool. It supports basic calculation (+); (-); (x); (:); (%); (=); (--);... and (infinite expression world access)...

I'm a chicken (beginner) so sorry I can't explain more... (!!!Max post length!!!)

Here is the code (with example) :
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#include <iostream>
#include <vector>
#include <time.h>
using namespace std;

enum ExpName {Equal, Add, Subtract, Multiply, Divide, Modulus, Negative};
#define MaxZoom 1000
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

struct Stack{
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
inline Stack(){lpVal = new double();pVal = lpVal;bDone = false;}
//inline ~Stack(){delete lpVal;}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
inline void PushItSelf(double *Val,unsigned _nMode, bool bPointer = false){
if(!bPointer){(*lpVal) = (*Val);pVal = lpVal;}else pVal = Val;nMode = _nMode;bDone = false;}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
inline void Push(double *Val,unsigned nMode, bool bPointer = false){
	data.push_back(Stack());unsigned short pos = data.size() - 1;
	data[pos].PushItSelf(Val, nMode, bPointer);
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
inline char GetCharCal(int Mode){switch(Mode){case Equal : return '=';break;case Add : return '+'; break;case Subtract : return '-';break;
case Multiply : return '*';break;case Divide : return '/'; break;case Modulus : return '%'; break;case Negative : return '$';break;}}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

inline void PrintW(){
	cout << (*pVal) << endl;

	unsigned short len = data.size();
	if(len)cout << "The value (" << *pVal << ") has (" << len << ") elements :\n";

	for(int i = 0; i < len;i++){
	switch(data[i].nMode){
	case Equal : cout << " = ";break;
	case Add : cout << " + "; break;
	case Subtract : cout << " - ";break;
	case Multiply : cout << " * ";break;
	case Divide : cout << " / "; break;
	case Modulus : cout << " % "; break;
	case Negative : cout << " (-) ";break;} if(nMode != Negative)data[i].PrintW();};if(len)cout << "End : (" << *pVal << ")...\n";
}



double CalDebug(){if(bDone)pVal = lpVal;
	unsigned short len = data.size();

	unsigned char chr, Mode;
	double *temp = 0;

	switch(nMode){
	case Equal : chr = '=';break;
	case Add : chr = '+'; break;
	case Subtract : chr = '-';break;
	case Multiply : chr = '*';break;
	case Divide :chr = '/'; break;
	case Modulus : chr = '%'; break;
	case Negative : chr = ']';break;}

	if(len)cout << endl << "Before : (" << chr << ") :" << *pVal << " ("; //(Element)?

	for(int i = 0; i < len;i++){Mode = data[i + 1].nMode;

	if(i != len - 1 && (data[i].nMode == Add || data[i].nMode == Subtract) && 
		(data[i + 1].nMode == Multiply  || data[i + 1].nMode == Divide)){

	if(!temp)temp = new double(0);

	if(data[i].nMode == Add) (*temp) += data[i].CalDebug();
	else (*temp) -= data[i].CalDebug();i++;

	cout << "\nStart complex calculation !!! (" << *temp << ")" << endl;

	while(i != len && (data[i].nMode == Multiply  || data[i].nMode == Divide))
	{
		if(data[i].nMode == Multiply)(*temp) *= data[i].CalDebug();else (*temp) /= data[i].CalDebug();i++;
	}

	cout << "\nA variable has been detected : " << *temp << " The value : " << (*pVal) << endl;
	(*pVal) += (*temp); delete temp;temp = 0;
	cout << "\nAfter calculation : " << (*pVal) << endl;i--;continue;
}


switch(data[i].nMode){
	case Equal : (*pVal) = data[i].CalDebug();break;
	case Add : (*pVal) += data[i].CalDebug();break;
	case Subtract : (*pVal) -= data[i].CalDebug();break;
	case Multiply : (*pVal) *= data[i].CalDebug();break;
	case Divide : (*pVal) /= data[i].CalDebug();break;
	case Modulus : ((*pVal) = ((int)(*pVal) * (int)MaxZoom) % ((int)(data[i].CalDebug()) * (int)MaxZoom) / MaxZoom); break;
	case Negative : (*pVal) = - (*pVal);break;}
	cout << "\nAfter calculation ( " << GetCharCal(data[i].nMode) << " ) : " << (*pVal) << endl;
}




if(len)cout << "After : " << *pVal << ")\n"; 

data.clear();
return (*pVal);
}


double Cal(){if(bDone)pVal = lpVal;unsigned short len = data.size();
	double *temp = 0;

	for(unsigned short i = 0; i < len;i++){
	if(i != len - 1 && (data[i].nMode == Add || data[i].nMode == Subtract) && 
		(data[i + 1].nMode == Multiply  || data[i + 1].nMode == Divide)){
			if(!temp)temp = new double(0);
				if(data[i].nMode == Add) (*temp) += data[i].Cal();else (*temp) -= data[i].Cal();i++;
				while(i != len && (data[i].nMode == Multiply  || data[i].nMode == Divide)){
					if(data[i].nMode == Multiply)(*temp) *= data[i].Cal();else (*temp) /= data[i].Cal();i++;}
				(*pVal) += (*temp); delete temp;temp = 0;i--;continue;}
switch(data[i].nMode){
	case Equal : (*pVal) = data[i].Cal();break;
	case Add : (*pVal) += data[i].Cal();break;
	case Subtract : (*pVal) -= data[i].Cal();break;
	case Multiply : (*pVal) *= data[i].Cal();break;
	case Divide : (*pVal) /= data[i].Cal();break;
	case Modulus : ((*pVal) = ((int)(*pVal) * (int)MaxZoom) % ((int)(data[i].Cal()) * (int)MaxZoom) / MaxZoom); break;
	case Negative : (*pVal) = - (*pVal);break;}}

data.clear();
return (*pVal);}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
double *pVal, *lpVal;unsigned nMode : 3;bool bDone;vector <Stack> data;
};

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

int main(){
	

double Result = 10;
srand(time(0));
Stack stack;stack.PushItSelf(&Result, Add, true); // Main variable, assigned

cout << "Main variable : " << Result << endl; 
int max = 0, num = 0;double temp;

while(!max){cout << "Max value number : "; cin >> max;}
while(!num){cout << "How many expressions you want to generate : "; cin >> num;}


for(int d = 0;d < num;d++){
temp = rand() % max + 1;
stack.Push(&temp, rand() % 4 + 1, false); // Temp variable
}

//Multiple expressions
/*
cout << endl;

for(int e = num - 1;e >= 0;e--){cout << "Expression " << e + 1 << " :" << endl;
max = 0, num = 0;
while(!max){cout << "Max value number : "; cin >> max;}
while(!num){cout << "How many expressions you want to generate : "; cin >> num;}



for(int g = 0;g < num;g++){

temp = rand() % max + 1;
stack.data[e].Push(&temp, rand() % 4 + 1, false); // Temp variable
}
}
*/
  
cout << "\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\nStart : ";
stack.PrintW(); //List all elements
cout <<"\n";
stack.CalDebug(); // stack.Cal();
cout << "\n Final Result : " << Result <<"\n";
}

I made debug (log) functions (CalDebug()) and PrintW()

Any suggestion everybody?
Last edited on
Geez, your style seriously sucks. I had to run your code through Artistic Style to realize that some of your functions weren't global.

The only way you could have missed the point of stack-based evaluation by a wider margin would be if you had tried to implement a ray-casting algorithm.

Main problem: no data stack (!). From what I can gather based on how you're building your structures, you merely have a list of operations (more formally, a list of unary functions) that are applied one after the other. You're constructing a structure that looks like this:
f1(f2(f3(f4(...fn(initial)...))))
This is much weaker than expressions of binary operations. For example, the expression (1 + 2) * (3 + 4) can't be represented like this.

But the real screwed up part is the way you're using those structures. There's no rhyme or reason to it. It's less like an evaluation algorithm and more like a random number generator. That's really all I can say because I don't dare doing a detailed analysis of what exactly is going on for fear of losing my sanity.

So, +1 point for listening to other people and using a stack, but -10 points for not really using the stack, then another -20 points for putting an aborted tree around the stack, then on top of that, -1048576 points for writing mindfuck code.
Yeah, I was wondering, does this code actually work? Or even compile?
Pages: 1... 34567... 20