View previous topic :: View next topic |
Author |
Message |
Carcosa
Joined: 03 Mar 2017 Posts: 4
|
How to create loops or logic statements programmaticaly |
Posted: Mon Aug 28, 2017 2:14 pm |
|
|
Hello CCS Forum I have a intresting question that I have been thinking a while.
I want to write a code that creates loops and logical statements dynamically according to user input.
For example;
I have three functions, func1, func2 ,func3 and if user wants to put them in a if statement like:
Code: | if(x>5){
func1();
} |
Also:
Code: | if(x>5){
if(y>3){
func2();
func3();
}
} |
and it goes like that. If user wants to create 7 ifs how can I create them ?
Same way if user wants to create while loops defined by user how can I create them dynamically?
I want to create mini IDE but I have no idea how Arduino or other IDEs doing that.
For clarify if you write this code to Arduino IDE:
Code: | while(y<5){
if(a>5){
digitalWrite(ledPin,HIGH)
}
else
digitalWrite(ledPin,LOW)
} |
How they convert this statements to C Code and compile in Microcontroller ?
It is a long term mistery for me if someone tell me I think I can achieve things
If my question is messy or confusing sorry for this. English isn't my native language |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19552
|
|
Posted: Mon Aug 28, 2017 2:21 pm |
|
|
The code you post is basically C...
It'll compile as posted, if you just have
Code: |
#define HIGH 1
#define LOW 0
#define digitalWrite(x,y) output_pin(x,y);
|
However to write your own parser with it's own syntax, that is then converted to C, is a lot of work:
<http://www.drdobbs.com/architecture-and-design/so-you-want-to-write-your-own-language/240165488>
There are scripting engines that will generate C around. So things like MPLAB will create as an output C, which is 99% portable.
Last edited by Ttelmah on Mon Aug 28, 2017 2:37 pm; edited 1 time in total |
|
|
Carcosa
Joined: 03 Mar 2017 Posts: 4
|
|
Posted: Mon Aug 28, 2017 2:37 pm |
|
|
Thank you Ttelmah for you answer. I think I couldn't tell what I mean.
Think that I wrote an IDE on PC works with flowcharts and I have these blocks;
while[statement] , if[statement] , for[0:variable] , motorForward[speed] , motorBackward[speed]
and user created algorithm then send me like this:
Code: | while[x>5] //User defined loop, it can be 4 while inside each other or it can be for loop not defined in start of the code
if[x>3]
motorForward[100]
[endif]
else if[x>3 && x<8]
motorBackward[80]
[endif]
.
. // user defined ifs, it can be 10 ifs or 20 ifs it can have different variables with different statements
.
.
[endwhile] |
How can I implement ifs, statements, loops and put functions inside them if I don't define in the begining of the code. How can I generate them dynamically?
Last edited by Carcosa on Mon Aug 28, 2017 2:39 pm; edited 1 time in total |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19552
|
|
Posted: Mon Aug 28, 2017 2:38 pm |
|
|
Look at something like MPLAB. Otherwise you are going to have to write it yourself. |
|
|
Gabriel
Joined: 03 Aug 2009 Posts: 1067 Location: Panama
|
|
Posted: Tue Aug 29, 2017 5:49 am |
|
|
I think your question was not understood.
You can't make your code create additional If or loops based on conditions.... you however can make your code FLOW vary with conditions.
So, define all the possible loops and ifs you need and then make your code flow through the different loops and ifs you need.
In other words select/deselect ifs and loops based on a variable (or more than 1 variable).
If you need loops or ifs to be added at COMPILE time... then look at #ifdef statements _________________ CCS PCM 5.078 & CCS PCH 5.093 |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19552
|
|
Posted: Tue Aug 29, 2017 6:01 am |
|
|
By writing code to do it....
Everything you describe is just code.... If you want, you can have for example a table containing function addresses, and select these based upon logic statements implemented using bitfields. It's totally up to you.
and I actually meant MATLAB, which can generate blocks of C code based upon rules. |
|
|
|