Script Engine - VB.NET Based-VBForums
Page 1 of 2 12 LastLast
Results 1 to 40 of 45

Thread: Script Engine - VB.NET Based

  1. #1

    Thread Starter
    Member
    Join Date
    May 2012
    Posts
    37

    Script Engine - VB.NET Based

    I recently came to the experience that there are a few real good script engines done VB.NET so i made up my mind and started to code my own engine wich had all the features i liked most seen from the VSA class method or the old MSScript.
    With all that in mind, maybe some of you seen the script engine of mIRC and i really like its simplicity and how high its tolerance level was on performing tasks.

    So far im done with the following:

    - Basic parser (Such as if-else-end or while-loops)
    - Evaluating variables and identifiers
    - Custom events
    - Object binding (Expose you components to the engine)
    - Custom DLL loading (Use external dll functions for tasks like the plugin idea)

    editted, making changes.

    At this point the engine does what i want on a few things i want to make more complex like local variables , now i have a global variable system and
    a better if-then parser , this one does the job but cannot handle a second if-statement while being in one.

    Maybe you guys have suggestion for me.

    Sincerly Barret,
    Last edited by TheBarret; May 10th, 2012 at 10:07 AM.

  2. #2
    Super Moderator Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,458

    Re: Script Engine - F2

    Hello Barret and welcome to the VBForums.

    Before I comment on your scripting language I need to inform you about the rules of this forum:

    This is not a place for show casing your work but if you have a specific question you are more than welcome to post here. Since the only question you have are more in the form of "what do you think?" I'm going to move this thread to the chit-chat forum.

    Over to your post:

    Over the years I've created several vertical languages and I've also created one full fledged language with its own IDE. So I'm very interested in giving you a hand with anything you want to achieve with this.

    With that said, I must say that I find the structure of your language a bit odd. For example you use -> for both assignment and as the object inspector (the separator between the object and its properties). I also find your WHILE loop confusing, but I suspect that is because of a typo in your code... To me the following: "while (%i == 10)" would mean that you would never enter the while loop since the variable %i has the value 1 when it reach that line. Or doesn't the while statement read "while %i is equal to 10"?
    Joacim Andersson
    If anyone's answer has helped you, please show your appreciation by rating that answer.
    I'd rather run ScriptBrix...
    Joacim's view on stuff.

    MVP

  3. #3
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    4,986

    Re: Script Engine - F2

    Isnt this something more suitable to the code bank than chit chat ?

    Also, why not simply model your language on C's syntax ? Its easily the most popular syntax for scripting languages.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena


    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. -jmcilhinney

  4. #4

    Thread Starter
    Member
    Join Date
    May 2012
    Posts
    37

    Re: Script Engine - F2

    Quote Originally Posted by Joacim Andersson View Post

    With that said, I must say that I find the structure of your language a bit odd. For example you use -> for both assignment and as the object inspector (the separator between the object and its properties). I also find your WHILE loop confusing, but I suspect that is because of a typo in your code... To me the following: "while (%i == 10)" would mean that you would never enter the while loop since the variable %i has the value 1 when it reach that line. Or doesn't the while statement read "while %i is equal to 10"?
    Your totally right (on that while loop), i made huge mistake there ill make the changes right away.
    maybe i was sleepy atm i was doing it, so much lines of code

    The "->" are indeed looking out of place but they are handled by the function trap, first it checks if the object is actually binded, then it goes to the next param wich is the property, then it checks if the property does exist in there, then it checks what kind of type the value holds of that property, (bool,string, etc) and converts to a proper value to finally set the value given.


    - Barret

  5. #5
    Super Moderator Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,458

    Re: Script Engine - F2

    Quote Originally Posted by Niya View Post
    Isnt this something more suitable to the code bank than chit chat ?
    The Code Bank is for sharing source code so it doesn't fit there either. It either belongs here or not at all in these forums.
    Joacim Andersson
    If anyone's answer has helped you, please show your appreciation by rating that answer.
    I'd rather run ScriptBrix...
    Joacim's view on stuff.

    MVP

  6. #6
    Super Moderator Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,458

    Re: Script Engine - F2

    Quote Originally Posted by TheBarret View Post
    The "->" are indeed looking out of place but they are handled by the function trap, first it checks if the object is actually binded, then it goes to the next param wich is the property, then it checks if the property does exist in there, then it checks what kind of type the value holds of that property, (bool,string, etc) and converts to a proper value to finally set the value given.
    I understand how you handle it. What I meant was that the structure itself is wrong. You don't use -> to assign values to variables so why would you use it to assign values to properties?
    Code:
    %i->1 INCORRECT syntax
    %i=1 CORRECT syntax
    The -> arrow seams to be pointing in the wrong direction. It would make more sense if it was:
    Code:
    textbox->enabled<-false
    How do you handle properties that are by themself objects?
    Code:
    textbox->font->size->12
    or if I want to assign the font size to a variable, according to your code it would be
    Code:
    %i=textbox->font->size
    So now you suddenly use the equal sign for assignment but to do the opposite you do:
    Code:
    textbox->font->size->%i
    If I would use your script language I would find this very confusing.
    Last edited by Joacim Andersson; May 9th, 2012 at 01:55 PM.
    Joacim Andersson
    If anyone's answer has helped you, please show your appreciation by rating that answer.
    I'd rather run ScriptBrix...
    Joacim's view on stuff.

    MVP

  7. #7

    Thread Starter
    Member
    Join Date
    May 2012
    Posts
    37

    Re: Script Engine - F2

    Good points sir, ill change this asap. huge thanks for this feedback!!!

    I cannot "rep" you for this feedback if i could then i would.
    Last edited by TheBarret; May 9th, 2012 at 02:30 PM.

  8. #8
    Super Moderator Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,458

    Re: Script Engine - F2

    No you can't give reputation points in the Chit-Chat forum but you need to have 10 posts before your rep points counts anyway and posts in Chit-Chat doesn't increase your post count either.
    Joacim Andersson
    If anyone's answer has helped you, please show your appreciation by rating that answer.
    I'd rather run ScriptBrix...
    Joacim's view on stuff.

    MVP

  9. #9

    Thread Starter
    Member
    Join Date
    May 2012
    Posts
    37

    Re: Script Engine - F2

    Oh well then i shall use the power of words

    Thank you very much for your time

  10. #10

    Thread Starter
    Member
    Join Date
    May 2012
    Posts
    37

    Re: Script Engine - F2

    Ok re-coded that part of where the engine starts reconizing the "object" functions

    Code:
    function start
    	!form.text = Hello there!
    end function
    
    function button_click1
    	//Disable component
    	!textbox.enabled = false
    	!textbox.visible = false
    end function
    
    function button_click2
    	//Enable component
    	!textbox.enabled = true
    	!textbox.visible = true
    end function

  11. #11
    Super Moderator Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,458

    Re: Script Engine - F2

    Why the exclamtion point? Is exclamation used for objects and &#37; for variables or do you have more of these and if not why make a distinction between what would be a reference variable (the textbox object) and a value variable? In your earlier post you showed code (before you edited it) that used == for equality comparison my question is: What do you use for "not equal"? I hope you don't plan to use the exclamation point for the NOT operator because then the above would be very confusing. (since it would be read as: NOT textbox.enabled = false).
    Last edited by Joacim Andersson; May 9th, 2012 at 03:29 PM.
    Joacim Andersson
    If anyone's answer has helped you, please show your appreciation by rating that answer.
    I'd rather run ScriptBrix...
    Joacim's view on stuff.

    MVP

  12. #12

    Thread Starter
    Member
    Join Date
    May 2012
    Posts
    37

    Re: Script Engine - F2

    Quote Originally Posted by Joacim Andersson View Post
    Why the exclamtion point? Is exclamation used for objects and % for variables or do you have more of these and if not why make a distinction between what would be a reference variable (the textbox object) and a value variable? In your earlier post you showed code (before you edited it) that used == for equality comparison my question is: What do you use for "not equal"? I hope you don't plan to use the exclamation point for the NOT operator because then the above would be very confusing. (since it would be read as: NOT textbox.enabled = false).
    Its because the engine parses into commands if not found it evaluates the line, the "!" is a mark that the line is part of object.

    What is your suggestion for "equal" and "not equal" sign then?

  13. #13
    Super Moderator Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,458

    Re: Script Engine - F2

    What I'm saying is that if you use the exclamation point as a marker it should not also be used as an operator which would rule out != for "not equal" and also rule out ! for NOT. So if you can't use != for not equal I guess the only other choice would be <>.
    Joacim Andersson
    If anyone's answer has helped you, please show your appreciation by rating that answer.
    I'd rather run ScriptBrix...
    Joacim's view on stuff.

    MVP

  14. #14

    Thread Starter
    Member
    Join Date
    May 2012
    Posts
    37

    Re: Script Engine - F2

    Quote Originally Posted by Joacim Andersson View Post
    What I'm saying is that if you use the exclamation point as a marker it should not also be used as an operator which would rule out != for "not equal" and also rule out ! for NOT. So if you can't use != for not equal I guess the only other choice would be <>.
    Indeed, never approached it from that view. Well i tried to keep some other script engines as "reference" how they do it but i also want to give it a little bit of my own ways, but did not figure out that it might be easy to read for me but for someone else totally crappy or not understand a single line.


    Thank you Joacim, you help me see the things they make sense.
    Last edited by TheBarret; May 9th, 2012 at 04:59 PM.

  15. #15
    Moderator
    Join Date
    Jan 2005
    Location
    Sydney
    Posts
    13,621

    Re: Script Engine - F2

    Moved from Chit Chat.
    Interesting thread.

  16. #16

    Thread Starter
    Member
    Join Date
    May 2012
    Posts
    37

    Re: Script Engine - F2

    Ok i changed alote of things arround, had some trouble with the "seperator" wich is my engine the "," (comma) so what i did was before a identifier is being checked or set to a variable it will check and will replace wit a temp. symbol that later will be put back again.


    Procedure i take when i parse:
    - New function call
    - Checks if function does exist
    - "script" input comes in as a array variable
    - Checks params and store it in the variable table
    - Enters a for-loop (to do per line)
    - Trims spaces from the begin and end of the line and removes vbtabs
    - Splits the line using the space as delimeter so we get: <CMD> <identifiers>
    - Enters a select-case that checks the <CMD>

    -- if case <cmd>:
    - Filters out the variables and returns when found any
    - Evaluate ($) identifiers, this will then parse per-character and determines what identifiers are used (supports embedded identifiers) and replace the return values of these identfiers
    - Performs the command (and return value if any)

    -- if Case else:
    - if the line starts with $ , evaluate the line but might not return any value (like $sleep(1000) )
    - if the line starts with _ , tells the engine its an object, give input to other function that handles it
    - if the line starts with &#37; is an variable , new/update that variable with given value
    - if all not pass, then it might be a function within the script itself, try's to call it with params and return
    - if else: unkown cmd

    - Before it exits the for-loop it checks if where not currently in a do/while/if conditions


    Others things i took in account:

    - Script side variables are being checked what kind of type value it is (integer,boolean,string) and converts it
    - Strip for illegal chrs and seperators
    - Object handling also checks what kind of value the property accepts and will convert if possible
    (This is still experimental because it does not support if the property only accepts a new object)
    - I have a list of consts that define all the syntaxes and such so i can change certain things quit easily.


    This might be look silly to you but this is my first time making a script parser so do be to hard on me :P
    Overall, the engines runs pretty good i implemented a stopwatch that calculates the execution time and big scripts never exceed the 1000+ms
    - Barret

  17. #17
    Super Moderator Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,458

    Re: Script Engine - VB.NET Based

    How are you handling assignments?
    Code:
    &#37;s = _textbox.text
    Or when there are no spaces?
    Code:
    %s=_textbox.text
    Joacim Andersson
    If anyone's answer has helped you, please show your appreciation by rating that answer.
    I'd rather run ScriptBrix...
    Joacim's view on stuff.

    MVP

  18. #18

    Thread Starter
    Member
    Join Date
    May 2012
    Posts
    37

    Re: Script Engine - VB.NET Based

    Quote Originally Posted by Joacim Andersson View Post
    How are you handling assignments?
    Code:
    &#37;s = _textbox.text
    Or when there are no spaces?
    Code:
    %s=_textbox.text
    Little change of format all function/params are inside of brackets {} instead of () this is because i had a problem with the $calc{} that also accepts () so i had to approach it from this way.

    To your qeustion:

    I have a ($) identifier for that, lets say i have declared a name "form" that represents my current app form

    then i would get it's property like this:
    Code:
    function GetAppTitle
         %title = $object{form,text}
         echo My app title is: %title
    end function
    The format of " = " is before handling replaced with the correct format, with the exception to replace just on 1 occourence so that it will not filter the rest out if that holds a "=" as well.


    Code part wich is responsible for that:
    Code:
    If (Left(Line, 1) = F2Tables.C_VARIABLE) Then
    
       ' Here is that part, looks for a wrong format
       If (InStr(Line, F2Tables.C_VARIABLE_SET_DELIM_WRONG, CompareMethod.Text) > 0) Then
           'Replace it but just once so it wont reformat the value
           Line = Replace(Line, F2Tables.C_VARIABLE_SET_DELIM_WRONG, F2Tables.C_VARIABLE_SET_DELIM, 1, 1)
       End If
       
       If (InStr(Line, F2Tables.C_VARIABLE_SET_DELIM) > 0) Then
           Dim p() As String = Split(Line, F2Tables.C_VARIABLE_SET_DELIM, 2)
           If (p.Length < 1) Then GoTo NextLine
           Dim cVar As String = p(0) : Dim cVal As String = p(1)
           cVal = EvalVars(cVal) : cVal = IdentifierParser.Parser(cVal, Me) : Variables.UpdateVar(cVar, cVal)
           GoTo NextLine
       End If
    End If
    Example script:
    Code:
    function start
    	%title=$object{form,text}=some text with = = == =  = =
    	echo %title
    end function
    This will output exactly what i want and will leave the other '=' occourences alone.
    It works but im sure it could be done in a different way i guess.
    - Barret
    Last edited by TheBarret; May 10th, 2012 at 12:12 PM.

  19. #19
    Super Moderator Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,458

    Re: Script Engine - VB.NET Based

    So you can't check if one value is equal to another and assign the boolean result to a variable?
    Code:
    ' assign true if &#37;a is equal to %b
    %c = %a == %b
    Joacim Andersson
    If anyone's answer has helped you, please show your appreciation by rating that answer.
    I'd rather run ScriptBrix...
    Joacim's view on stuff.

    MVP

  20. #20
    PowerPoster
    Join Date
    May 2002
    Posts
    25,381

    Re: Script Engine - VB.NET Based

    alternative - make the assignment token := rather than a simple =

    ' assign true if &#37;a is equal to %b
    %c := %a = %b


    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  21. #21

    Thread Starter
    Member
    Join Date
    May 2012
    Posts
    37

    Re: Script Engine - VB.NET Based

    Quote Originally Posted by Joacim Andersson View Post
    So you can't check if one value is equal to another and assign the boolean result to a variable?
    Code:
    ' assign true if &#37;a is equal to %b
    %c = %a == %b
    I'm afraid that part hasn't been come to my attention but i'll come to that later with and with such good help within this forum community

    Anyway i did some testing with do-loops and reading from INI file.

    example script:
    Code:
    function start
    	%file = $startpath{}\test.ini
    	%x = 1
    	
    	do (%x==$ini{%file,-1})
    		echo Key: $iniread{%file,$ini{%file,%x},key%x}
    		%x++
    	enddo
    
    end function
    The INI file :
    Code:
    [test1]
    key1=This
    
    [test2]
    key2=INI reader
    
    [test3]
    key3=Works!

    Results in:
    Code:
    Key: This
    Key: INI reader
    Key: Works!
    My goal was to test out if my engine could perform embedded identifiers with do-loops and read them out correctly to the functions.

    The ini function are simple,
    $ini(file,-1) = Total topics in the ini
    $ini(file,x) = returns the name x'th topic in the ini (and so on...)
    $iniread(file,topic,key)

    Suggestions are welcome!

    - TB

  22. #22
    Super Moderator Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,458

    Re: Script Engine - VB.NET Based

    I find it a bit confusing that you don't quote literal strings.
    Would is the output of this?
    Code:
    echo 1+2
    Joacim Andersson
    If anyone's answer has helped you, please show your appreciation by rating that answer.
    I'd rather run ScriptBrix...
    Joacim's view on stuff.

    MVP

  23. #23

    Thread Starter
    Member
    Join Date
    May 2012
    Posts
    37

    Re: Script Engine - VB.NET Based

    Quote Originally Posted by Joacim Andersson View Post
    I find it a bit confusing that you don't quote literal strings.
    Would is the output of this?
    Code:
    echo 1+2
    That will come out the same "1+2" for math i have a identifier $calc{1+2}

  24. #24
    Super Moderator Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,458

    Re: Script Engine - VB.NET Based

    Can you escape the dollar sign? Let's say I wanted this output: "The result of $calc{1+2} is 3". This code wouldn't work for that:
    Code:
    echo The result of $calc{1+2} is $calc{1+2}
    since that would output "The result of 3 is 3".
    Joacim Andersson
    If anyone's answer has helped you, please show your appreciation by rating that answer.
    I'd rather run ScriptBrix...
    Joacim's view on stuff.

    MVP

  25. #25

    Thread Starter
    Member
    Join Date
    May 2012
    Posts
    37

    Re: Script Engine - VB.NET Based

    Quote Originally Posted by Joacim Andersson View Post
    Can you escape the dollar sign? Let's say I wanted this output: "The result of $calc{1+2} is 3". This code wouldn't work for that:
    Code:
    echo The result of $calc{1+2} is $calc{1+2}
    since that would output "The result of 3 is 3".
    Good point, i think i can code something easy for that, ill modify the char-parser to detect " as escape and continue when another one is found.

  26. #26

    Thread Starter
    Member
    Join Date
    May 2012
    Posts
    37

    Re: Script Engine - VB.NET Based

    I regret saying "easy" because this was tricky!!

    To let the engine recognize an escape (quote) char was easy, but the code that replaces that specific part of a line was hard (if you got identically parts)!

    Here's what i did:

    I made a new function that rebuilds a given string but will skip characters from x to y. Now once that is done, i used the String.insert(x) to insert that evaluated value back in. This function is usefull if the strLine has parts with are identical to eachother.

    Code:
    Public Function RebuildString(StrLine As String, ReplaceWith As String, SkipFrom As Integer, SkipStop As Integer) As String
            Dim TmpLine As String = ""
            For i = 1 To Len(StrLine)
                If (i < SkipFrom) Or (i > SkipStop) Then
                    TmpLine = TmpLine & Mid(StrLine, i, 1)
                End If
            Next
            TmpLine = TmpLine.Insert(SkipFrom - 1, ReplaceWith)
            Return TmpLine
        End Function
    I could use the Replace() but somehow it did not what i wanted so i made this function just for this purpose.


    Script example:
    Code:
    function start
    	echo "$calc{1+2}" $calc{1+2} "$calc{1+2}" $calc{1+2}
    end function
    Notice the quote's, now i want just the non-quoted parts to be evaluated and the rest to be just how they are.

    This will output:
    Code:
    "$calc{1+2}" 3 "$calc{1+2}" 3
    - TB
    Last edited by TheBarret; May 10th, 2012 at 03:30 PM.

  27. #27
    Super Moderator Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,458

    Re: Script Engine - VB.NET Based

    It almost looks like you're writing this with VB6. Here's the same function in a more VB.Net way:
    Code:
    Public Function RebuildString(line As String, replaceWith As String, skipFrom As Integer, skipStop As Integer) As String
        Return line.SubString(0, skipFrom) & replaceWith & line.SubString(skipStop, line.Length - skipStop)
    End Function
    Joacim Andersson
    If anyone's answer has helped you, please show your appreciation by rating that answer.
    I'd rather run ScriptBrix...
    Joacim's view on stuff.

    MVP

  28. #28

    Thread Starter
    Member
    Join Date
    May 2012
    Posts
    37

    Re: Script Engine - VB.NET Based

    Quote Originally Posted by Joacim Andersson View Post
    It almost looks like you're writing this with VB6. Here's the same function in a more VB.Net way:
    Code:
    Public Function RebuildString(line As String, replaceWith As String, skipFrom As Integer, skipStop As Integer) As String
        Return line.SubString(0, skipFrom) & replaceWith & line.SubString(skipStop, line.Length - skipStop)
    End Function

    I know .NET has alote of functions i have yet to discover, im glad you show me this one, i feel kinda stupid now

  29. #29
    Super Moderator Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,458

    Re: Script Engine - VB.NET Based

    Quote Originally Posted by TheBarret View Post
    Script example:
    Code:
    function start
    	echo "$calc{1+2}" $calc{1+2} "$calc{1+2}" $calc{1+2}
    end function
    Notice the quote's, now i want just the non-quoted parts to be evaluated and the rest to be just how they are.

    This will output:
    Code:
    "$calc{1+2}" 3 "$calc{1+2}" 3
    So the output will include the quotes?
    Joacim Andersson
    If anyone's answer has helped you, please show your appreciation by rating that answer.
    I'd rather run ScriptBrix...
    Joacim's view on stuff.

    MVP

  30. #30

    Thread Starter
    Member
    Join Date
    May 2012
    Posts
    37

    Re: Script Engine - VB.NET Based

    I just finished that part, the awnser is no, they are removed upon return from where it was called.

    Had to make sure every other function still works the same since i changed the char-parser a bit.

  31. #31
    Moderator
    Join Date
    Jan 2005
    Location
    Sydney
    Posts
    13,621

    Re: Script Engine - VB.NET Based

    Can you put quote characters within quotes?

  32. #32

    Thread Starter
    Member
    Join Date
    May 2012
    Posts
    37

    Re: Script Engine - VB.NET Based

    Quote Originally Posted by penagate View Post
    Can you put quote characters within quotes?


    Actually i did a test, for a moment i was not sure until i thought why not "cheat" my own engine so to call. What my engine does if it finds symbols that are in this case " (quote's) removed up detection because they set out a param or non-evaluating area's it replaces them for so called shortcuts.

    like, a quote is \\^34 and a comma \\^44 and so on...

    Heres what it looks like:
    Code:
    function start
    	echo "This is a \\^34 within a quoted line"
    end function
    Output: This is a " within a quoted line


    So your awnser is yes, thats possible but it only applies to those kind of symbols that the engine filters out, perhaps i could add the whole ascii table in there just to make it another functional possibility.

    I used ascii table to keep the right number for that character (http://www.asciitable.com/index/asciifull.gif)

    - TB
    Last edited by TheBarret; May 10th, 2012 at 07:55 PM.

  33. #33

    Thread Starter
    Member
    Join Date
    May 2012
    Posts
    37

    Re: Script Engine - VB.NET Based

    I just started to rethink of the $calc{} and realized that this function has alote of important functions within scripts so i was in doubt if i would re-invent the wheel or not, i decide to use a library that takes care of this evaluation of this function.

    So i picked nCalc for this wich is very powerfull.
    Link: http://ncalc.codeplex.com/

    Or is there a better solution to this?

  34. #34

    Thread Starter
    Member
    Join Date
    May 2012
    Posts
    37

    Re: Script Engine - VB.NET Based

    Redid the variable_check code, altho im not sure if im doing it right. however it does work.

    Maybe any suggestions on this code:

    Code:
    Private Function ReplaceVars(Evalstr As String) As String
            Dim CurrentChar As String = ""
            Dim TheName As String = ""
            Dim Value As String = ""
            Dim InVar As Boolean = False
            Dim LastPos As Integer = 0
    Again:
            For x = 1 To Len(Evalstr)
                CurrentChar = Mid(Evalstr, x, 1)
    
                '* We hit a var syntax, start collecting
                If (CurrentChar = F2Tables.C_VARIABLE) Then LastPos = x : InVar = True
    
                '* Stop collecting once we hit:
                ' - Brackets {}
                ' - Brackets2 ()
                ' - Operators +-*/
                ' - Identifier syntax $
                ' - or a space
                If (CurrentChar = " ") Then InVar = False
                If (CurrentChar = F2Tables.C_SYNTAX) Then InVar = False
                If (CurrentChar = F2Tables.C_BRACKET_START) Then InVar = False
                If (CurrentChar = F2Tables.C_BRACKET_END) Then InVar = False
                If (CurrentChar = F2Tables.C_BRACKET2_START) Then InVar = False
                If (CurrentChar = F2Tables.C_BRACKET2_END) Then InVar = False
                If (CurrentChar = F2Tables.C_OP_ADD) Then InVar = False
                If (CurrentChar = F2Tables.C_OP_SUB) Then InVar = False
                If (CurrentChar = F2Tables.C_OP_MULT) Then InVar = False
                If (CurrentChar = F2Tables.C_OP_DIV) Then InVar = False
    
                'Collect bool is true, start collecting...
                If (InVar = True) Then
                    TheName = TheName & CurrentChar
                    'This was all, then put the collect bool back to false
                    If (x = Len(Evalstr)) Then InVar = False
                End If
    
                'if collect bool is false and we have still a name
                'in the collect var, lets see if that returns a value
                If (InVar = False) And (Len(TheName) > 0) Then
                    Value = Variables.GetVar(TheName)
                    If (Len(Value) > 0) Then
                        Evalstr = RebuildString(Evalstr, Value, LastPos, (LastPos + (Len(TheName) - 1)))
                        TheName = ""
                        Value = ""
                        InVar = False
                        LastPos = 0
                        'Eval string has changed that means we need to do the for-loop again.
                        GoTo again
                    End If
                    'To make sure...
                    TheName = ""
                End If
            Next
            Return Evalstr
        End Function

    You might wonder why i use this method simply because to avoid the problem that occours when you have
    two variables that are like: &#37;x and %xx
    If you use the Replace() function that will change all the %x in the current line so i had to do it per-char.


    Thank you in advance!
    - TB
    Last edited by TheBarret; May 12th, 2012 at 03:28 PM.

  35. #35
    Super Moderator Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,458

    Re: Script Engine - VB.NET Based

    Sorry, I stopped reading as soon as I saw that you had a label, named Again, which made me convinced that there's a (oh horror) Goto statement somewhere in there.
    Joacim Andersson
    If anyone's answer has helped you, please show your appreciation by rating that answer.
    I'd rather run ScriptBrix...
    Joacim's view on stuff.

    MVP

  36. #36

    Thread Starter
    Member
    Join Date
    May 2012
    Posts
    37

    Re: Script Engine - VB.NET Based

    Well im sorrie that im not an expert on coding, maybe you can give me a suggestion how to improve myself how to avoid using these methods. not asking to code for me but little direction would help.

  37. #37
    Super Moderator Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,458

    Re: Script Engine - VB.NET Based

    You could use a Do ... Loop While instead of a Goto. Your many If statements could be a single Select Case statement.
    Code:
    Select Case CurrentChar
        Case " ", F2Tables.C_SYNTAX, F2Tables.C_BRACKET_START, F2Tables.C_BRACKET_END, _
            F2Tables.C_BRACKET2_START, F2Tables.C_BRACKET2_END, F2Tables.C_OP_ADD, _
            F2Tables.C_OP_SUB, F2Tables.C_OP_MULT, F2Tables.C_OP_DIV
             InVar = False
     End Select
    Joacim Andersson
    If anyone's answer has helped you, please show your appreciation by rating that answer.
    I'd rather run ScriptBrix...
    Joacim's view on stuff.

    MVP

  38. #38

    Thread Starter
    Member
    Join Date
    May 2012
    Posts
    37

    Re: Script Engine - VB.NET Based

    Been reading on why GoTo's are bad and now i understand but also find it pretty confusing on the reactions people give. Seems half of them say it's ok but not too much the other half just treats you like a retard when they see you do it. There are i guess rare situation where you cannot use any other method then "GoTo" but im gonna make a long story short, "Use goto only as a last resort". no harm in that.

    Ontopic:
    I used in my entire project just 1 goto and i applied the rule i just typed.
    And correct me if im wrong, since you are mod the reaction you gave me was not of better taste either.


    -TB

  39. #39
    Super Moderator Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,458

    Re: Script Engine - VB.NET Based

    If you can show me one single time where a Goto is needed then I'll show you how you have used bad structure in your code.

    Maybe this can convice you of what can happen when you use Goto statements:
    http://xkcd.com/292/

    I'm sorry if I didn't read your code because you used a goto but my head hurts when I read unstructured code but then I showed you a better way of doing it. I'm sorry if my comment hurt your feeling, it was not my intention to come out as rude.
    Last edited by Joacim Andersson; May 13th, 2012 at 04:48 PM.
    Joacim Andersson
    If anyone's answer has helped you, please show your appreciation by rating that answer.
    I'd rather run ScriptBrix...
    Joacim's view on stuff.

    MVP

  40. #40

    Thread Starter
    Member
    Join Date
    May 2012
    Posts
    37

    Re: Script Engine - VB.NET Based

    Was not about hurting feelings more about manors.

    Anyway, ive been doing new ways of this current function but i cannot see any other solution.

    What happens is, the function loops trough the string that searches the variables once found it will replace the variable name with the value but this will change the orginal line that it was looping trough, so i thought well lets save it to a temp string but then the rebuildstring function does not work since the length of the orginal and new line do not match anymore wich is needed in order to get the position of the variable name and replacement.

    I cannot see how this could be done differently...

    Code:
     Private Function CheckForVar(strline As String) As String
            Dim TmpLine As String = strline
            Dim CurrentChar As String = ""
            Dim VarName As String = ""
            Dim VarValue As String = ""
            Dim Pos As Integer = 0
            Dim PosT As Integer = Len(strline)
            Dim Check As Boolean = False
    
    Again:
            For i = 1 To PosT
                CurrentChar = Mid(TmpLine, i, 1)
                If (CurrentChar = TABLE_VARIABLE_SYNTAX) Then
                    If (Check = False) Then
                        Check = True
                        Pos = i
                    Else
                        RaiseError("Unexpected variable symbol")
                        Return ""
                        Exit Function
                    End If
                End If
                If (Check = True) Then
                    If (CurrentChar = " ") Then
                        VarName = Replace(VarName, TABLE_VARIABLE_SYNTAX, "")
                        VarValue = Variables.GetVar(VarName)
                        TmpLine = RebuildString(TmpLine, VarValue, Pos, Pos + Len(VarName))
                        Check = False
                        VarName = ""
                        VarValue = ""
                        GoTo Again
                    End If
                    If (i = PosT) Then
                        VarName = Replace(VarName, TABLE_VARIABLE_SYNTAX, "")
                        VarValue = Variables.GetVar(VarName)
                        TmpLine = RebuildString(TmpLine, VarValue, Pos, Pos + Len(VarName))
                        Check = False
                        VarName = ""
                    End If
                End If
    
                If (Check = True) Then
                    VarName = VarName & CurrentChar
                End If
    
            Next
    
            Return TmpLine
        End Function
    Little help is needed i suppose.
    -TB

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.