You are using a version without Ads of this website. Please, consider donating:

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

Pages: 123456... 20
Nobody? Is there any method which can do better than the method "converting to int"???
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:
 ``12345`` ``````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
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) :
 ``123456`` ``````#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
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
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:
 ``123`` ``````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?
Here, take my interpreter code as an example :
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103`` ``````#include #include #include #include #include #include #include #include #include #include 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 data;vector arr;unsigned short nId;unsigned int Att : 4;}; struct Function{struct Parameter {int nId;}; vector Param;}; struct ClassData{vector data;vector classData;vector Func;bool Main;}; class lngParser{public : inline lngParser(){}~lngParser(){} char ReadFile(const char *FileName,const char *Source); ClassData main; }; ``````

Any suggestion?
Last edited on
See my general interpreter code structure above...
Oh.. I'm very tired.. (=.=)
I see code. I don't see interpreter code.

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/
helios wrote:

+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
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
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.
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?
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
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
Why do you want to use assembly? It's not a good idea if you don't need it.
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
@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...
@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:

 ``1234567`` ``````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
Pages: 123456... 20

You are using a version without Ads of this website. Please, consider donating: