Results 1 to 10 of 10

Thread: How to provide compiler functionality to qtscript

  1. #1

    Default How to provide compiler functionality to qtscript

    Hi all,
    I have implemented scripting functionality to my application using qtScript module. Its working fine when i execute complete application. Now i need to provide a compiler/interpreter for script. Means i need to provide a Compile button with script editor and it need to check all syntax and semantics of script written by user before actual execution of script. please let me know if anyone have any idea to achieve this...

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to provide compiler functionality to qtscript

    I wouldn't call the button "Compile" because it doesn't compile anything. What you can do is create an instance of QScriptProgram that contains your code and before feeding it, call QScriptEngine::checkSyntax() to check the syntax of the script.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3

    Default Re: How to provide compiler functionality to qtscript

    but is CheckSyntax is enough mature to check the syntax of user defined function names, its parameters etc?

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to provide compiler functionality to qtscript

    It checks the syntax, not the semantics. If you call a function parameter "if" (which is a reserved keyword), it will detect it, if you use an undefined variable it will not detect it (as it is not a syntax error in JavaScript).
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5

    Default Re: How to provide compiler functionality to qtscript

    Exactly i know this.... that's why i asked question how can i provide compiler type functionality from qtscript, is any other tool/class available in qt where i can provide info about user defined functions and classes and can check semantics of script?

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to provide compiler functionality to qtscript

    A compiler does not check the semantics of the script. It is simply not possible for javascript. Consider this piece of code:

    javascript Code:
    1. function fun(arg) { if(arg == 1) Array.prototype.foo = function() { return 7 } }
    2.  
    3. fun(userInput)
    4.  
    5. var x = Array.foo()
    To copy to clipboard, switch view to plain text mode 

    Depending on the external value provided from "userInput" the foo method in Array will exist or not. The script syntax is fine but its semantics may vary between runs.

    A compiler, by definition, is a tool that translates a high-level program definition into a lower-level program definition that is easier to execute by the runtime environment. For example a C compiler will not bail out if you divide by 0 in your application. The runtime will.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #7

    Default Re: How to provide compiler functionality to qtscript

    OMG.......see for example , my application has different classes, and objects of these classes are created from script. I need a mechanism to check in script is user has written correct function names, parameters, object is created and used to call function- is that function belong to that class or not. All these things can be checked when we execute script. But it should not happen, i need to check all these and save script... user can run script at some other time, at that time any error or any problem should not appear. I hope you understand what i am saying.
    So please guide me if you have any idea or any knowledge to achieve this. And i need something from qt so it will be implemented easily.

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to provide compiler functionality to qtscript

    Quote Originally Posted by sheetalw View Post
    I need a mechanism to check in script is user has written correct function names, parameters, object is created and used to call function- is that function belong to that class or not.
    I am telling you this is not possible. JavaScript is not C++ where a simple syntax check would be sufficient to do what you want. In JavaScript there are no classes and you can add and remove methods from objects dynamically from within the script. If you want a static analysis that assumes nobody will do anything that modifies the environment, then do the parsing yourself or just execute the script in a controlled environment where each function that performs some task is substituted by a stub that will do nothing but report success. If you get an exception then the code is invalid in context of provided input parametrs. If you don't get an exception then the script is valid in context of provided input parameters and environment (but not valid in general).

    For instance, is this script valid or not (as the whole script)?
    javascript Code:
    1. var a = b
    To copy to clipboard, switch view to plain text mode 

    Is this one valid?
    javascript Code:
    1. var a = "aaa"
    2. b = a+7
    To copy to clipboard, switch view to plain text mode 

    What about this one?
    javascript Code:
    1. var a;
    2. if(Math.random() < 0.5)
    3. a = new Array()
    4. else
    5. a = 7
    6. a.splice(1, 1)
    To copy to clipboard, switch view to plain text mode 

    It's a bit like Schroedinger's cat, you never know if it's dead or alive until you check, so until you do it's both dead and alive at the same time
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  9. #9

    Default Re: How to provide compiler functionality to qtscript

    Okies.. thanks, i understood it means i need to implement complete environment for such checking. directly nothing is available.

  10. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to provide compiler functionality to qtscript

    No, it means you cannot possibly do a foulproof check.

    Consider this:
    javascript Code:
    1. function a() { }
    2.  
    3. function b(arg) { print(arg) }
    4.  
    5. a(1,2,3,4)
    6. b()
    To copy to clipboard, switch view to plain text mode 

    The code is correct as far as JavaScript is concerned (try running it and see for yourself). I understand that you'd like to get a "oh, your code is wrong, you messed up function arguments" message but the problem is the code is not wrong, it is a correct working JavaScript snippet.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Replies: 4
    Last Post: 19th December 2011, 11:19
  2. Qt Creator Parse compiler errors of a non gcc compiler
    By kiozen in forum Qt Tools
    Replies: 4
    Last Post: 4th July 2011, 01:34
  3. Strange compiler behaviour (compiler passes wrong argument)
    By SasaVilic in forum General Programming
    Replies: 2
    Last Post: 2nd November 2010, 11:36
  4. Replies: 0
    Last Post: 25th November 2009, 07:46
  5. Is webkit functionality a superset of qtscript?
    By jurojon in forum Qt Programming
    Replies: 5
    Last Post: 17th October 2008, 23:12

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.