Pre-Processor?

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
MrWizard.
Posts: 32
Joined: Tue Apr 17, 2012 3:31 am

Pre-Processor?

Post by MrWizard. » Thu Jul 18, 2013 2:47 pm

Does LC have a concept of pre-processor directives and tokens?

I know I can use runtime logic to accomplish some of the same effects but i would prefer have access to a preprocessor.

In a simple example one could use a preprocessor directive to include / exclude debug code.

If the native tool does not have it available, has anyone done a "trick" or "tip" sheet on how to do it?

Thanks,
E

makeshyft
Posts: 220
Joined: Mon Apr 15, 2013 4:41 am
Contact:

Re: Pre-Processor?

Post by makeshyft » Mon Jul 22, 2013 6:14 am

i dunno if this helps you...but what I did was create an INI file which I load on preopen of the main stack.... from it I load variables that let me direct the flow of the application from the very beginning using the values in the INI file.
Founder & Developer @ MakeShyft R.D.A - https://www.makeshyft.com
Build Software with AppStarterStack for Livecode - https://www.AppStarterStack.com
Save Time with The Time Saver's Toolbox - https://www.TimeSaversToolbox.com

MrWizard.
Posts: 32
Joined: Tue Apr 17, 2012 3:31 am

Re: Pre-Processor?

Post by MrWizard. » Mon Jul 22, 2013 7:58 pm

Thanks for he reply.

Yes, one can certainly accomplish control flow via run-time execution, but i am looking for a pre-processor stage between the IDE and the code compilation. I know the framework is there, since LC uses it to determine the compilation target (iPhone, iPad, etc). I am hoping to have that exposed to the application layer, so I can make some intelligent decisions at that stage of the game.

For instance, quoting from the Apple Guide;
"....iOS apps can be as large as 2 GB, but the executable file cannot exceed 60MB. However, consider download times when determining your app’s size. Minimize the file’s size as much as possible, keeping in mind that there is a 50 MB limit for over-the-air downloads.

Abnormally large binary files are usually the result of storing data, such as images, inside the compiled binary itself, instead of as a resource inside your app bundle. If you are compiling an image or large dataset into your binary, it would be best to split this data out into a resource that is loaded dynamically by your app."
https://developer.apple.com/library/ios ... 5-CH19-SW1

Now if i can tell the compiler to include / exclude blocks of code based on the selected target (one function of a pre-processor), I can ensure my delivered app is as slim as possible. If i have to delay to run time execution, i run the risk of having Android logic embedded into my deliverable.

If the 'include' or 'require' keywords were available to the IDE client (server only now), I could play some tricks to move in/out code blocks prior to compile....

Thoughts?

Thanks,
E

Simon
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am
Location: Palo Alto

Re: Pre-Processor?

Post by Simon » Mon Jul 22, 2013 8:21 pm

Hi E,
If I might dumb this down a bit.
What you'd like is to remove "If the platform is..." at the standalone build time?
I recently thought about that.

There is some discussion going on in the Engine Contributor forum under Include/Require, maybe you should look up that thread.

Simon
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

shaosean
Posts: 906
Joined: Thu Nov 04, 2010 7:53 am

Re: Pre-Processor?

Post by shaosean » Tue Jul 23, 2013 1:32 am

http://livecode.com/developers/api/6.0. ... tandalone/
http://livecode.com/developers/api/6.0. ... tandalone/

I would like to see this enhanced to include an additional parameter saying what platform is currently being saved..

MrWizard.
Posts: 32
Joined: Tue Apr 17, 2012 3:31 am

Re: Pre-Processor?

Post by MrWizard. » Tue Jul 23, 2013 9:32 pm

Here is a quick and dirty example in HTML to conceptualize one form of usage.

There are three files;

preprocessor.lc,
1 <?lc
2
3 put "<!DOCTYPE HTML PUBLIC " & quote & "-//W3C//DTD HTML 3.2 Final//EN" & qu ote & "> " && CRLF
4 put "<HTML>" && CRLF
5 put "<HEAD>" && CRLF
6 put tab && "</HEAD>" && CRLF
7 put tab && "<BODY>" && CRLF
8 put tab && "<h1>Hello Header</h1>" && CRLF
9 put tab && "<p>Hello world!</p>" && CRLF
10 require "commenton.inc"
11 put tab && "<p>Goodbye world!</p>" && CRLF
12 require "commentoff.inc"
13 put tab && "The server time is [" && the time && "]" && "and server date is [" && the date && "]" && CRLF
14 put tab && "</BODY>" && CRLF
15 put "</HTML>" && CRLF
16 ?>

commenton.inc
1 <!--

commentoff.inc
1 <!--
2 //-->

When commenton.inc contains the HTML start token '<!--' the LC server yields the following to the user;
Hello Header

Hello world!

The server time is [ 3:27 PM ] and server date is [ 7/23/13 ]

When we empty the contents of commenton.inc of the HTML start token '<!--' the LC server yields the following to the user;
Hello Header

Hello world!

Goodbye world!

The server time is [ 3:30 PM ] and server date is [ 7/23/13 ]

I would like to have the ability to accomplish this type of include / exclude of code blocks at compile (stand-alone generation) rather then run time

Thanks,
E

shaosean
Posts: 906
Joined: Thu Nov 04, 2010 7:53 am

Re: Pre-Processor?

Post by shaosean » Wed Jul 24, 2013 8:43 am

It would require you to use a comment, as require/include would through a script error, but if you are just looking at compile time, should be easy enough

Code: Select all

#require "commenton"  // this would be the name of a stack and the script in the stack would get imported

Code: Select all

on savingStandalone
  // the fun part of looping through all the code
  local tObjects
  local tStacks
  put the subStacks of this stack into tStacks
  put LF & me after tStacks
  repeat for each line tStack in tStacks
    if (tStack <> EMPTY) then
      if (the script of stack tStack <> EMPTY) AND (the script of stack tStack contains "#require") then
        put the long id of stack tStack & LF after tObjects
      end if
      repeat for each line tCard in the cardNames of stack tStack
        if (the script of card tCard of stack tStack <> EMPTY) AND (the script of card tCard of stack tStack contains "#require") then
          put the long id of card tCard of stack tStack & LF after tObjects
        end if
        if (the number of controls of card tCard of stack tStack > 0) then
          repeat with i = 1 to the number of controls of card tCard of stack tStack
            if (the script of control i of card tCard of stack tStack <> EMPTY) AND (the script of control i of card tCard of stack tStack contains "#require") then
              put the long id of control i of card tCard of stack tStack & LF after tObjects
            end if
          end repeat
        end if
      end repeat
    end if
  end repeat

  local tPreviousLineOffset = 0
  repeat for each line tObject in tObjects
    local tScript
    put the script of tObject into tScript
    
    local tLineOffset
    repeat until tLineOffset = 0
      put lineOffset("#require", tScript, tPreviousLineOffset) into tLineOffset

      if (tLineOffset <> 0) then
        local tRequiredScriptName
        put word 2 of line tLineOffset of script tScript into tRequiredScriptName
        put the script of stack tRequiredScriptName into line tLineOffset of tScript
        put tLineOffset into tPreviousLineOffset
      else  // all done
        set the script of tObject to tScript
        exit repeat
      end if
    end repeat
  end repeat
end savingStandalone
something like that.. just wrote that in here, so may or may not work, but should give you an idea of what to do..

MrWizard.
Posts: 32
Joined: Tue Apr 17, 2012 3:31 am

Re: Pre-Processor?

Post by MrWizard. » Wed Jul 24, 2013 2:23 pm

THanks for the psuedo code.

You have the right concept, but this appears to still be runtime evaluated.

I am looking for a pre-processor.

The preprocessor would take the LC stack and generate a new output prior to sending it to the compiler.

My LC Source Code with pre-processor language wrapping logic for all platforms ==> preprocessor ==> compiler for specific platform (processor function, machine function, platform function) ==> stand-alone for target

This approach will help me 1) avoid violating Apple's edict to not load code from a server by-passing the store and 2) staying under their app max file size

Thoughts?

Thanks,
E

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7266
Joined: Sat Apr 08, 2006 8:31 pm
Location: Minneapolis MN
Contact:

Re: Pre-Processor?

Post by jacque » Wed Jul 24, 2013 7:35 pm

MrWizard. wrote: You have the right concept, but this appears to still be runtime evaluated.

I am looking for a pre-processor.

The preprocessor would take the LC stack and generate a new output prior to sending it to the compiler.
The "savingStandalone" message is sent only once, after you have chosen the menu item to save as standalone, but before the standalone is actually built. Though LiveCode does not really compile its code, the "savingStandalone" message was added to the language for exactly the reason you want -- so you can perform last-minute changes before the standalone is built. The changes are not added to your working stack, they are only added to the standalone. It is, in effect, a "preprocessor". See the dictionary for more info.

I'm curious though why you'd need something like this. Generally there is very little branching needed to account for different platforms, and these rarely take up much room in a stack. What do you need to add that would cause you to go over a 50 meg limit?
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

monte
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 1564
Joined: Fri Jan 13, 2012 1:47 am
Contact:

Re: Pre-Processor?

Post by monte » Sun Aug 04, 2013 12:29 am

I'm curious though why you'd need something like this. Generally there is very little branching needed to account for different platforms, and these rarely take up much room in a stack. What do you need to add that would cause you to go over a 50 meg limit?
Yes, we are talking about saving a few kilobytes here. Really not worth the trouble as far as I can tell. The quoted docs from Apple clearly identify the more likely issue (particularly as LiveCode makes it so easy to do) of embedding images. Even a very complex app would have trouble cracking 5MB if there's no images embedded in it. Actually you would need to embed quite a few images to go anywhere near 50MB.

A note on savingStandalone... if we are talking mobile then you need to handle savingMobileStandalone and you need to understand it's not quite the same. When saving a mobile standalone the stackfile is just read from disk so if you make changes in savingMobileStandalone you need to save the stack... so you don't want to make changes that will permanently lose data.
LiveCode User Group on Facebook : http://FaceBook.com/groups/LiveCodeUsers/

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7266
Joined: Sat Apr 08, 2006 8:31 pm
Location: Minneapolis MN
Contact:

Re: Pre-Processor?

Post by jacque » Sun Aug 04, 2013 7:12 pm

Interesting. The docs on both "saving" messages vary slightly but the mobile one doesn't make sense to me. It says the changes are only seen in the standalone, not in the original stack file. That's the same as the desktop "saving" entry. But then it goes on to say that that you do have to save the stack in the mobile version if you want the app to include the differences. That's contradictory. So which is it? Are the changes included in the standalone or not?
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

monte
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 1564
Joined: Fri Jan 13, 2012 1:47 am
Contact:

Re: Pre-Processor?

Post by monte » Mon Aug 05, 2013 4:23 am

They are if you save ;-)

The desktop build messes with the stackfile in memory, saves as then reloads the original from disk. The mobile one just uses the stackfile from disk. It's much nicer for testing but less so for this kinds of stuff.
LiveCode User Group on Facebook : http://FaceBook.com/groups/LiveCodeUsers/

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7266
Joined: Sat Apr 08, 2006 8:31 pm
Location: Minneapolis MN
Contact:

Re: Pre-Processor?

Post by jacque » Mon Aug 05, 2013 6:23 pm

I see, thanks Monte. It would be easier on users if they'd made the two function the same way. I would have been tripped up on that.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

monte
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 1564
Joined: Fri Jan 13, 2012 1:47 am
Contact:

Re: Pre-Processor?

Post by monte » Wed Aug 07, 2013 12:20 am

Possibly but the way they have done it does make things much faster for testing on mobile.
LiveCode User Group on Facebook : http://FaceBook.com/groups/LiveCodeUsers/

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”