1

I have made a program that plots a slope field for a given function.

img

It works correct if I plug in the function in the source code.

But I wan to turn this vb6 project into an exe file.

Because I knew this will happen before, I previously made a field for inputting the function. The function will be inputted in a special form. inspired by the language used in some add on in AutoCAD, I made this language and named it DiffSol.

So what the user is gonna do is to write a function in the field using DiffSol language.

The problem is that It needs to be a real mathematical function in vb to be evaluated for different x and y's. but I cannot find a strategy to turn that language into a vb math function which can be evaluated.

All I am gonna do is to evaluate the inputted function for 15*31 times.

The job looks like making a compiler. It seems a really hard job for me.

Any ideas?

AHB
  • 212
  • 2
  • 10
  • 1
    So you invented your own notation "DiffSolv" and now want to compile into machine code? To be called how? I would assume you want compile it into VB6 code. Wouldn't it be easier to insist that "Diffsolv" functions were simply VB6 code, and let users write a separate VB6 program containing the desired code? Or is there some inherently different between DiffSolv and VB6? – Ira Baxter May 05 '17 at 16:20
  • @IraBaxter emmm. The language doesn't matter at all! The problem is that I cannot tell VB `execute(Txt_input.text)`. So I have to translate it into vb myself (I think). The easiest way I could think about was a language in which all the math the function are introduced first and then there are the arguments. This was just what came into *My* mind. – AHB May 05 '17 at 16:28
  • 2
    Well, you could build an interpreter in VB6 to evaluate your formula. it wouldn't be "fast" like compiled code, but 15*31 = 4500 times really isn't that many.... if each iteration took 10 milliseconds you'd burn 4.5 seconds which is probably fine in your application. It that works for you, you can learn how to write a parser and a formula evaluator; its pretty easy. See this answer for how to do that: http://stackoverflow.com/questions/2245962/is-there-an-alternative-for-flex-bison-that-is-usable-on-8-bit-embedded-systems/2336769#2336769 – Ira Baxter May 05 '17 at 17:25
  • 1
    VB6 is almost 100% compatible with Excel VBA. One possible idea is to launch the userform from Excel. It is possible to let Excel's built-in expression parser (using standard infix notation) parse it and Excel's built-in calculation engine do the heavy lifting of evaluating it. If you don't want to go that route -- the idea of @IraBaxter is quite good and easy to implement given the nature of your expressions. Parsing mathematical expressions is only hard when you have to deal with precedence and associativity with infix notation. – John Coleman May 05 '17 at 20:37
  • @IraBaxter isn't that 465 times by the way? – AHB May 06 '17 at 14:58
  • @AHB: Ok, I can't do math... so its 0.45 seconds which you won't notice at all. – Ira Baxter May 06 '17 at 20:18
  • @IraBaxter I appreciate your help man! It's just that *I* am a beginner and don't understand your suggestions about parsing. sorry about that. I need to learn more about it... – AHB May 07 '17 at 13:47
  • Did you read the suggested link carefully? – Ira Baxter May 07 '17 at 16:06
  • You should look for an existing scripting language which you could use instead of inventing this yourself. – StayOnTarget May 08 '17 at 11:31

1 Answers1

0

Simple way is to transpile it in to VBScript or JScript.

Create a class containing your functions written in VB:

' +(a,b)
public function ADD(a as double, b as double) as double
    add = a + b
end function

'/(a,b)
public function DIV(a as double, b as double) as double
    div = a / b
end function

Add a reference to the Microsoft Script Control then:

Dim scr As ScriptControl: Set scr = New ScriptControl
scr.Language = "VBScript"

'// allow the script access to the class with the functions
scr.AddObject "DS", new diffsolClass

expr = " +(200, c(/(+(2,6), 2))) "

'//parse with simple substitution
parsed = expr
parsed = Replace$(parsed, "/", "DS.DIV")
parsed = Replace$(parsed, "+", "DS.ADD")
parsed = Replace$(parsed, "c", "cos") '//built in already

'//for some valid VB: DS.ADD(200, cos(DS.DIV(DS.ADD(2,6), 2))) 

'//run it
MsgBox scr.Eval(parsed)

returns 199.346356379136

Alex K.
  • 171,639
  • 30
  • 264
  • 288
  • Thank you. But, I am having a hard time understanding the things you have used and what other people talk about. Can you, by the way mention a good e-book available online that teaches these things? So that I will go read those part that I have problems with. I think it would be better that way... – AHB May 06 '17 at 15:07