Compacting C++ Functions using Macros, Template, or something else.

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

35 Replies - 1883 Views - Last Post: 14 September 2016 - 09:30 AM Rate Topic: -----

#1 Randy2017   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 28
  • Joined: 24-August 16

Compacting C++ Functions using Macros, Template, or something else.

Posted 24 August 2016 - 09:46 AM

Hey guys,

I need a way to compact functions.
In order to understand my question, please go to

www.github.com/juliarLang/juliar


Click
DestkopServer/src/main.cpp


At the bottom, you will see something like
if(command == "block") return block(param); if(command == "section") return section(param);


My question is, is there a way in C++11 (without boost libraries preferably) to have it so that you can execute function "command_varaible"(mparam). This is compile time replacement(so no need to worry about run time). I've heard of using templates and MACROS, but what is the best way to achieve this without compromising run-time performance.

2) In that same file you may notice I am using if(args == 0) then execute function with just parameter, if(args==1) then execute same function with just parameter and 1 argument, if (args==2) then execute same function with just parameter and 2 argument. Is there a way to create a template or MACRO for that. I know I can just pass an array pointer, but what do you guys recommend.

Any help would be appreciated.

(Please do not bash me or the language or coding style that I use as the program still in early stages and I am still figuring out problems. )

Is This A Good Question/Topic? 0
  • +

Replies To: Compacting C++ Functions using Macros, Template, or something else.

#2 jimblumberg   User is offline

  • member icon

Reputation: 5519
  • View blog
  • Posts: 17,109
  • Joined: 25-December 09

Re: Compacting C++ Functions using Macros, Template, or something else.

Posted 24 August 2016 - 10:05 AM

Please post the relevant code into a post (inside code tags).

:code:

Quote

so that you can execute function "command_varaible"(mparam).

You may want to look to see if using function pointers may be useful.


Jim
Was This Post Helpful? 1
  • +
  • -

#3 Randy2017   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 28
  • Joined: 24-August 16

Re: Compacting C++ Functions using Macros, Template, or something else.

Posted 24 August 2016 - 10:09 AM

I am sorry jim, I am new to the forums. I am not sure if I can edit my original post but I will definitely use it next time.

Wow, I've never actually seen a pointer to a function itself. I will definitely take a look at it.

Thanks Jim
Was This Post Helpful? 0
  • +
  • -

#4 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 14146
  • View blog
  • Posts: 56,699
  • Joined: 12-June 08

Re: Compacting C++ Functions using Macros, Template, or something else.

Posted 24 August 2016 - 10:14 AM

Now I am curious - what problem is this new language going to solve?

Quote

Well, Juliar is anything you are looking for in coding in the future.

Is that a take on the speech in 'Napoleon Dynamite'?

Quote

ere we are making this for everyone to understand but easy to learn and can just about do what all other languages do, but way easier to comprehend.
[...]
Just imagine making the best program ever with few less lines and a lot less typing, or make a program just like your friend did in a lot less time but make it even better and interesting?

... on second thought it is sounding like a Trump speech.

Quote

, Juliar internally divides 1 big program into many smaller problems and sends them through a graphing card if applicable. Hence, getting a speedup boost of 30x

Where is that being implemented in the code on github/
Was This Post Helpful? 0
  • +
  • -

#5 Randy2017   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 28
  • Joined: 24-August 16

Re: Compacting C++ Functions using Macros, Template, or something else.

Posted 24 August 2016 - 10:20 AM

Dear modi123,

I didn't write the first part, it was suggested and I just added it. I wanted the language to be open where anybody can submit ideas and thoughts. If you have an improvement, I will be happy to add it.

Quote

Where is that being implemented in the code on github/


That feature is currently not implemented, but if you look at the while loop. It forks each request into a new processor. Again, I haven't worked on that feature and probably won't work until the language is more stable.

*process
Was This Post Helpful? 0
  • +
  • -

#6 Randy2017   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 28
  • Joined: 24-August 16

Re: Compacting C++ Functions using Macros, Template, or something else.

Posted 24 August 2016 - 10:55 AM

@jimblumberg

I've read how to do the function pointers, but how would I use them for this purpose? i.e. what would be the benefit?

Thanks!
Was This Post Helpful? 0
  • +
  • -

#7 jimblumberg   User is offline

  • member icon

Reputation: 5519
  • View blog
  • Posts: 17,109
  • Joined: 25-December 09

Re: Compacting C++ Functions using Macros, Template, or something else.

Posted 24 August 2016 - 01:58 PM

Quote

but how would I use them for this purpose?

Until you post some code and ask specific questions about that code, I won't really guess.

Jim

This post has been edited by jimblumberg: 24 August 2016 - 01:58 PM

Was This Post Helpful? 0
  • +
  • -

#8 Randy2017   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 28
  • Joined: 24-August 16

Re: Compacting C++ Functions using Macros, Template, or something else.

Posted 24 August 2016 - 02:13 PM

Let's say you have:

std::string cube(std::string name = "1", std::string color = "red", std::string size = "4"){
   //..Performs something
}
void main(){
   vector<string> args;
   //Code for getting 0-3 args here
   if(args.size() == 0) return cube();
   if(args.size() == 1) return cube(arg1);
   if(args.size() == 2) return cube(arg1,arg2);
   if(args.size() == 3) return cube(arg1,arg2,arg3);
}



Is there a way to use macro, vector, or function pointer to somehow generate the function based on args size... SO I will need to generate for 4 different calls for cube function. Is there a way to automate it. Say at compile time where for each parameter the function is generated? Something like:

void main(){
   vector<string> args;
   //Code for getting 0-3 args here
   cube(unpack args);
}


Was This Post Helpful? 0
  • +
  • -

#9 Skydiver   User is online

  • Code herder
  • member icon

Reputation: 6282
  • View blog
  • Posts: 21,601
  • Joined: 05-May 12

Re: Compacting C++ Functions using Macros, Template, or something else.

Posted 24 August 2016 - 03:01 PM

For the curious, here's the full source for main.cpp as referred to in the OP:
Spoiler


Anyway, jimblumberg was right on the money for the use of function pointers in the general cases. Basically setup a bunch of std::maps for string to function pointers. You'll need multiple maps for each flavor of function that takes a different number of parameters, or for functions that take parameters in different orders. But that will dramatically trim down the code.

As for your specific code in post #8, the better approach is to not depend on the default parameters of the function, but rather setup default values for each of the parameters, and pass all the parameters.

In pseudo code:
funcArgs[0] = "1";
funcArgs[1] = "red";
funcArgs[2] = "4";
funcArgs[3] = "frodo";

for(int i = 0; i < args.size(); i++)
    funcArgs[i] = args[i];

cube(funcArgs[0], funcArgs[1], funcArgs[2], funcArgs[3]);


Was This Post Helpful? 0
  • +
  • -

#10 Randy2017   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 28
  • Joined: 24-August 16

Re: Compacting C++ Functions using Macros, Template, or something else.

Posted 24 August 2016 - 07:54 PM

Thanks so much.

I had a question about pre-defining arguments instead of relying on default. Let's say we have more than one function ~30,
is it a good idea to do this then? or is it better to use default values then?
if(type == "circle"){
args[0] = "red";
args[1] = "4";
//change args if needed.
circle(args[0],args[1]);
}
else if(type == "cube"){
args[0] = "blue";
args[1] = "3";
//change args if needed
cube(args[0],args[1]);
}


This post has been edited by Skydiver: 25 August 2016 - 05:28 AM
Reason for edit:: Put code in code tags. You obviously know how to do this based on your original post.

Was This Post Helpful? 0
  • +
  • -

#11 sepp2k   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2627
  • View blog
  • Posts: 4,181
  • Joined: 21-June 11

Re: Compacting C++ Functions using Macros, Template, or something else.

Posted 25 August 2016 - 05:29 AM

In my mind, the best way to solve your problem, long-term at least, would be to define your standard library in your language itself - or at least provide the appropriate extern stubs where the function can't be defined within the language. That is, you put function definitions for your standard library in some file that you then simply read and evaluate when your interpreter starts. Then calls to built-in functions can be treated exactly like user-defined functions and you don't need to map strings to functions manually at all. That said, I don't think you support user-defined functions yet, so this is probably more of a note for further down the line.

Some general notes/questions about your language, if you don't mind:

Why did you decide to use * as a replacement for both opening and closing parentheses? Doesn't this require knowing the arity of any function that you call (and on further thought: doesn't that then suggest that you don't need * at all)? This will make user-defined functions quite complicated to handle as you'll need to know their arity when parsing function calls and not just later when evaluating them, so you'll either need forward-declarations (urgh) or multiple parsing stages (first partially parse the whole file to find all definitions and their arity, then go through it again to actually parse the function bodies once you have the arity information needed to do that). With proper parentheses that would be a non-issue. I also find that this makes the code quit difficult to read as it's impossible to know whether a given * ends a function call or starts a new one unless you know the arity of all functions and keep a count of how many parameters were already supplied to which function.

In what sense is your language (going to be?) functional?

Do you currently support string literals and/or comments? At a quick glance, I don't think so, but I may have missed it. However I assume, you are going to at some point, so note that once you do, your current approach to parsing is going to break horribly: if a string or comment contains a *, your parser is just gonna rip it apart and interpret the * as the beginning or end of a function call. You'll need a proper tokenizer to prevent this.
Was This Post Helpful? 0
  • +
  • -

#12 Skydiver   User is online

  • Code herder
  • member icon

Reputation: 6282
  • View blog
  • Posts: 21,601
  • Joined: 05-May 12

Re: Compacting C++ Functions using Macros, Template, or something else.

Posted 25 August 2016 - 05:32 AM

For your use case you outlined in posts #10 and #8, the default values aren't really buying you.

Just like you could have a map of function pointers, you could also also have a map of structures where each structure contains a function pointer and its set of default arguments.
Was This Post Helpful? 0
  • +
  • -

#13 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3796
  • View blog
  • Posts: 13,742
  • Joined: 08-August 08

Re: Compacting C++ Functions using Macros, Template, or something else.

Posted 25 August 2016 - 06:06 AM

View Postmodi123_1, on 24 August 2016 - 01:14 PM, said:

... on second thought it is sounding like a Trump speech.

He has the best codes! :whistling:
Was This Post Helpful? 0
  • +
  • -

#14 Randy2017   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 28
  • Joined: 24-August 16

Re: Compacting C++ Functions using Macros, Template, or something else.

Posted 25 August 2016 - 06:17 AM

View PostSkydiver, on 25 August 2016 - 05:32 AM, said:

For your use case you outlined in posts #10 and #8, the default values aren't really buying you.

Just like you could have a map of function pointers, you could also also have a map of structures where each structure contains a function pointer and its set of default arguments.



That's actually a great idea. I am thinking of adding a 2nd map where each parameter is defined for each function. I just hope it won't complicate look up time, since you will be doing 2 look ups at that point? Do you guys think it will be better to keep default values, but for example, have a 2nd function which basically does this:

string execute(*funcname, args){
if(args.empty()) return *funcname();
else if(args.size() == 1) return *funcname(args[0]);
else if(args.size() == 2) return *funcname(args[0],args[1]);
else if(args.size() == 3) return *funcname(args[0],args[1],args[2]);
else{
  retrun *funcname(args[0],args[1],args[2],args[3])
}
}



Was This Post Helpful? 0
  • +
  • -

#15 Randy2017   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 28
  • Joined: 24-August 16

Re: Compacting C++ Functions using Macros, Template, or something else.

Posted 25 August 2016 - 06:37 AM

View Postsepp2k, on 25 August 2016 - 05:29 AM, said:

In my mind, the best way to solve your problem, long-term at least, would be to define your standard library in your language itself - or at least provide the appropriate extern stubs where the function can't be defined within the language. That is, you put function definitions for your standard library in some file that you then simply read and evaluate when your interpreter starts. Then calls to built-in functions can be treated exactly like user-defined functions and you don't need to map strings to functions manually at all. That said, I don't think you support user-defined functions yet, so this is probably more of a note for further down the line.


I do have some user defined functions but they rely on already compiled code..i.e. you can import a properly formatted external binary program into the language... code just for reading a file is currently not supported. However, I would like to add that feature eventually for user-defined function. Would I need 2 maps then? 1 for looking up function calls and 1 for looking up parameters.

Quote

Why did you decide to use * as a replacement for both opening and closing parentheses? Doesn't this require knowing the arity of any function that you call (and on further thought: doesn't that then suggest that you don't need * at all)?

The main reason I used * instead of parenthesis is because I didn't want people to think it's LISP, which in retrospect does include some LISP like functions. The hope is that editor coloring will support the highlighting. However, on the drawing board the language does have a way to redefine opening and closing tags i.e. you can use parenthesis or any other tag if you want to.

Quote

This will make user-defined functions quite complicated to handle as you'll need to know their arity when parsing function calls and not just later when evaluating them, so you'll either need forward-declarations (urgh) or multiple parsing stages (first partially parse the whole file to find all definitions and their arity, then go through it again to actually parse the function bodies once you have the arity information needed to do that).


I had the same concern, however, the * is now properly defined. I was thinking of doing *=functioname=args2,args3 args1 * to define custom functions.


Quote

In what sense is your language (going to be?) functional?

For the most part it will be a functional language. However, it will have some anti-functional structure and functions i.e. loop.

Quote

Do you currently support string literals and/or comments? At a quick glance, I don't think so, but I may have missed it.


Everything is considered a string in the language, so everything that's outside of *commandname * is just assumed to be text. The language says that if * is followed by a non-white space then it's considered to be a start of command. If * is followed by whitespace or another * then it's considered to be end of command.

In order for
*
to display as
*
, all you do is
\*[code].

In order to hide an output, you can use [code]*hide *
command.

You can also use
*ignore *
command in order to prevent the execution of command, but still output as text. Basically it converts all * to \*. Without Any parameters, ignore is not safe as it assumes you have a net of 0 of opening - closing *.
In order to have a more safer ignore, you can use an optional, which allows you to define custom end tag. So you can do like:

*ignore=HELLO   some command names lots of ****** and other command executions HELLO*



The optional parameter specifies to look at that parameter with an asterisks at the end to mark the end of ignore statement.

The safest way to do a comment is to use both *hide * to hide the output and *ignore * command which prevents execution, so

 *hide *ignore=123   some comment goes here with any asterisks I want 123** 


Ofcourse, that is WAY TOO LONG to type...so there is a shortcut:

[code] *hi=### hello I am some comment that can be used for commenting ###*


Quote

However I assume, you are going to at some point, so note that once you do, your current approach to parsing is going to break horribly: if a string or comment contains a *, your parser is just gonna rip it apart and interpret the * as the beginning or end of a function call. You'll need a proper tokenizer to prevent this.


I am always looking for suggestions. So I will definitely consider using a tokenizer.
Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3