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

Pages: 123456... 20
Dec 2, 2012 at 3:01am
Nobody? Is there any method which can do better than the method "converting to int"???
Dec 2, 2012 at 3:26am
Well, bitwise operations only make sense when performed on integer values. "1.5 XOR 2.3" is as meaningful as "the integer whose double is 1".

EDIT: You could do something like this:
1
2
3
4
5
float a=1.5f,b=2.3f;
unsigned c=*(unsigned *)&a; //Undefined behavior!
unsigned d=*(unsigned *)&b; //Undefined behavior!
unsigned e=c^d;
float f=*(float *)&e; //Undefined behavior! 
But the value of f will have little or nothing to do with the values of a and b.
Last edited on Dec 2, 2012 at 3:29am
Dec 2, 2012 at 10:05am
I've tried finding & performing lots of bitwise operators and modulus commands by many methods. And finally I've found the solution!!!!! You'll need to insert a temporary variable (unsigned) among the conversion expressions (integer or decimal). It doesn't affect the result and help the computer fixing the decimal calculation errors. And, that solution will completely solve the (bit) float-double problem and improve the accuracy of decimal calculation. In short it's very efficiency and you... should try it! :)

How to do this :
- Define a magnification power value. (10x - 100x - 1000x - ....x)
Note : The decimal accuracy is dependent of the magnification power.

- Multiply both two expressions. (Note : convert to unsigned) - Zoom!!!
- Calculate!!!
- Finally you will need to recovery the original value. (Zoom out)

And, an example code (modulus) :
1
2
3
4
5
6
#define MaxZoom 1000
double d = 5.4;
d = (int)(d * MaxZoom) % (5 * MaxZoom); //400
d /= MaxZoom;  //0.4

//Output : d = 0.4 


What do you think? Is this a good idea? :)
Last edited on Dec 2, 2012 at 10:10am
Dec 2, 2012 at 10:51am
Dec 2, 2012 at 12:20pm
No no...modf doesn't perform my expected command (modulus a decimal value)

But, can I apply this method above for bitwise operator commands?
Last edited on Dec 2, 2012 at 1:05pm
Dec 2, 2012 at 2:30pm
The function is fmod(), not modf() (who came up with that?).

No no...modf doesn't perform my expected command (modulus a decimal value)
So just convert the operand to floating point:
fmod(d,(double)5);
If for some strange reason you still don't want to use fmod(), this is a better hand-coded solution:
1
2
3
double a=5.4;
int b=5;
double mod=a-int(a/b)*b;


What the eff does this have to do with implementing a language, anyway?
Dec 3, 2012 at 10:25am
Here, take my interpreter code as an 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
#include <windows.h>
#include <vector>
#include <map>
#include <fstream>
#include <string.h>
#include <time.h>
#include <math.h>
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
using namespace std;

#define TotalCommand 16

#define TotalSymbol 9

#define TotalExp 7

char Symbol[TotalSymbol] = {'&', '*', '+', '-', '/', '=', '^', '|'};

#define GetLength(string) ((string) ? strlen(string) : 0)
#define InRange(fVal, min, max) ((!(fVal < min) && !(fVal > max)))
#define HalfRange(fVal, min, max) ((!(fVal <= min) && !(fVal >= max)))

#define Null -0.29855


inline int GetCalMode(const char *str, unsigned int len){
unsigned char chr = str[0];


	if(len == 1){
	if(chr == '%')return Modulus;
	if(chr == '*')return Multiply;
	if(chr == '+')return Add;
	if(chr == '-')return Subtract;
	if(chr == '/')return Divide;
	if(chr == '=')return Equal;}

return -1;
}

char Buffer[TotalCommand][7] = {
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
"cal", "random", "if", "while", "for", "switch", "point", "goto", "call",
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
"int", "bool", "char", "short", "long",  "float", "double"
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
};


char shortExp[TotalExp][7] = {"equal", "add", "sub", "mul", "div", "neg", "mod"};
char longExp[TotalExp][10] = {"equal", "add", "subtract", "multiply", "divide", "negative", "modulus"};
enum ExpName {Equal = 0, Add = 1, Subtract = 2, Multiply = 3, Divide = 4, Negative = 5, Modulus = 6};

enum CmdName {
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Calculate, Random, If, While, For, Switch, Point, Goto, Call,
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Int, Bool, Char, Short, Float, Double, Long
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
};


char *IntToBit(long int x, char *str, char *Temp){
	int nCount = 0;
	bool Neg = (x < 0);
	while(x)
	{
   if (x&1)
      str[nCount] = '1';
    else
      str[nCount] = '0';

	  nCount++;
      str[nCount] = 0;
    x>>=1;  
  }
	for(int i =0;i < nCount;i++)
	
	Temp[i] = str[nCount -1 - i];
	Temp[nCount] = 0;

	if(Neg){while(nCount < 63){Temp[nCount] = '0';nCount++;}Temp[63] = '1';Temp[64] = '0';}
	return Temp;
}


struct subData{inline subData(){dData = 0;nId = 0;}unsigned char nId;double dData;};

struct Data{inline Data() {data.clear();arr.clear();nId = 0;}vector <subData> data;vector <int> arr;unsigned short nId;unsigned int Att : 4;};

struct Function{struct Parameter {int nId;}; vector <Parameter>Param;};

struct ClassData{vector <Data> data;vector <ClassData> classData;vector <Function> Func;bool Main;};

class lngParser{public : inline lngParser(){}~lngParser(){}

char ReadFile(const char *FileName,const char *Source);

ClassData main;
};


Any suggestion?
Last edited on Dec 3, 2012 at 12:21pm
Dec 3, 2012 at 12:12pm
See my general interpreter code structure above...
Dec 3, 2012 at 12:48pm
Oh.. I'm very tired.. (=.=)
Dec 3, 2012 at 3:40pm
I see code. I don't see interpreter code.

And your style sucks ass.

This may be relevant later on. You should at least be able to get some ideas out of it: http://www.cplusplus.com/forum/lounge/11845/
Dec 3, 2012 at 5:20pm
helios wrote:
And your style sucks assertions.

+1.
You're not probably even on the right way to get everything optimized easily - But besides that you will have big troubles expanding the project.
Also redefining Null to a negative floating-point value? Woah.
Macros? You're on C++.

You probably need to learn some more things on the world of C++ and assembly before jumping into it - But it's also good to learn on-the-way.

The most useful thing you must be able to do is calling a function from an external module (for Win32: LoadLibrary, GetProcAddress and FreeLibrary) and calling it with a number of parameters chosen at runtime. Quite impossible unless you have some asm experience.
Also you will have to store the return value.
You can do them both by asm inlining.

Sure you can , I did my own interpreter a couple months ago which allowed for memory handling, heap memory and pointers handling, basic math operations, floating point handling, asm-like calling of external functions, jump/call/ret "emulation" and so on, all from a interactive console with the ability to load an external script file... (By the way it also has localized informations about errors, in case you push too many/too few parameters)
Last edited on Dec 3, 2012 at 5:36pm
Dec 3, 2012 at 8:41pm
If there any method which can call a function indirectly (especially Unlimited function parameters (printf, sprintf,...)) Does asm C++ support this feature? And how to do this?
Last edited on Dec 3, 2012 at 8:44pm
Dec 3, 2012 at 8:46pm
Well you need to understand calling conventions, there are four main calling conventions:
stdcall: http://www.nynaeve.net/?p=53
cdecl: http://www.nynaeve.net/?p=41
thiscall: http://www.nynaeve.net/?p=73
fastcall: http://www.nynaeve.net/?p=63

Everything you need to know is in the C/C++ header files, and is different for each function.
I'd say printf is cdecl on MSVS10. Be sure to support both cdecl and stdcall at least: Windows API uses stdcall. C Stdlib uses cdecl - At least vs10's does.
Dec 3, 2012 at 8:54pm
To call a function I know I have push the variable object or with a register value buffer, then call it, then check the ESP value (I'm not sure)...
I have no idea how to call a function by assemmbly C++...
Do you know how?
Dec 3, 2012 at 8:57pm
You can (edit: probably) do it with C++11's variadic templates and brace initialisers. I'd figure out how to actually write it, but my compiler (cygwin-g++ 4.5.3) doesn't fully support variadic templates yet.
Last edited on Dec 3, 2012 at 9:14pm
Dec 3, 2012 at 9:05pm
I think your method is very convenient. :)
A command in general is very different (Function name, parameter list type, number of parameters, returning value...) So, the only method is using assembly (I'm sure that C++ asm should support?) So it's really a main problem.
Last edited on Dec 3, 2012 at 9:06pm
Dec 3, 2012 at 9:07pm
Why do you want to use assembly? It's not a good idea if you don't need it.
Dec 3, 2012 at 9:12pm
Yes, I'm confused - means I want to call a function which supports unlimited parameters (printf, sprintf). But, is there any class which can do this? It's valid when you know exactly the number of parameters that the program will call, and otherwise? 5 - 9 - 35??? Any solution?
Last edited on Dec 3, 2012 at 9:17pm
Dec 3, 2012 at 9:58pm
@EssGeIsh
Can you tell me some programs which are done by your interpreter... I'd like to see them XDXD...
Now I realized that nothing is impossible...
Dec 3, 2012 at 10:21pm
@JM

With reference to your Code style thread and this one, I hope you can see that I am not the only one who thinks that your style is bad, and knowledge lacking. I sincerely hope you take on board what I said to you - especially about giving advice to others.

Another problem I think you have, is you don't understand enough C++ to understand the code in links given to you - like helios's link above.

So if you still insist (rather stubbornly IMO) on trying this interpreter, why don't you start with something really easy, get it working, then add more functionality incrementally, instead of jumping straight into something complex like variable length argument lists, and dozens of other potentially tricky things. This is a main concept for software development, along with the "Divide & Conquer" concept. For example you need to define what the syntax is going to look like, what are the types & operators going to look like? Try to get this working first:

1
2
3
4
5
6
7
Integer a = 2;
Integer b = 3;
Integer c;

c = a * b;

display (c);


This obviously uses C operators, which probably the easiest thing, instead of inventing your own. However you do have do something to separate your language from others - there is no point otherwise. So this example only has 'Integer' instead of 'int' as a type, and the display function, instead of cout.

Edit:
This is trickier than it looks, you have to handle negative numbers, errors like non declaration and non initialisation, missing semicolons, poorly formed tokens (types & variable names), varying amounts of white space etc.

If you want something much easier, try implementing a Reverse Polish Notation (RPN) calculator. It uses a stack. The algorithm is: If it is a number push onto stack, if it is an operator, pop the stack and do the calculation. Do some research of your own and see if you can come with some code.




Last edited on Dec 3, 2012 at 10:26pm
Pages: 123456... 20