CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

How to create loops or logic statements programmaticaly

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Carcosa



Joined: 03 Mar 2017
Posts: 4

View user's profile Send private message Send e-mail

How to create loops or logic statements programmaticaly
PostPosted: Mon Aug 28, 2017 2:14 pm     Reply with quote

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 Very Happy

If my question is messy or confusing sorry for this. English isn't my native language Sad
Ttelmah



Joined: 11 Mar 2010
Posts: 19516

View user's profile Send private message

PostPosted: Mon Aug 28, 2017 2:21 pm     Reply with quote

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

View user's profile Send private message Send e-mail

PostPosted: Mon Aug 28, 2017 2:37 pm     Reply with quote

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: 19516

View user's profile Send private message

PostPosted: Mon Aug 28, 2017 2:38 pm     Reply with quote

Look at something like MPLAB. Otherwise you are going to have to write it yourself.
Gabriel



Joined: 03 Aug 2009
Posts: 1067
Location: Panama

View user's profile Send private message

PostPosted: Tue Aug 29, 2017 5:49 am     Reply with quote

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: 19516

View user's profile Send private message

PostPosted: Tue Aug 29, 2017 6:01 am     Reply with quote

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.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group