How to use a self compiling compiler in Java

I'm creating a user-customizable calculator - a calculator where the user can create his/her own buttons while the calculator runs.

What I need to know is how I can get the calculator to compile an instruction - i.e. what a new button does, and add it to itself.

For example, and user would create a button that calculates (UK) V.A.T by writing y = (x*0.175)

This instruction would then be compiled while the calculator was running and be added to the calculator.

Thanks,

Aled.

[515 byte] By [COMSCBoya] at [2007-9-28 10:17:08]
# 1

> I'm creating a user-customizable calculator - a

> calculator where the user can create his/her own

> buttons while the calculator runs.

> What I need to know is how I can get the calculator to

> compile an instruction - i.e. what a new button does,

> and add it to itself.

> For example, and user would create a button that

> calculates (UK) V.A.T by writing y = (x*0.175)

> This instruction would then be compiled while the

> calculator was running and be added to the

> calculator.

> Thanks,

> Aled.

You will probable generate a new java file, which implements an interface say ButtonLogic with one method computeIt. You can call the compiler then, which will produce a class file and then you can load this class file via Class.forName("..."), e.g.:

ButtonLogic bl = Class.forName("...").newInstance();

bl.computeIt()

fischerta at 2007-7-12 0:02:05 > top of Java-index,Developer Tools,Java Compiler...
# 2

You need to think about how the whole system is going to work - you need to know everything that the button will require, and then think about how to provide this information to the button.

So, by the sounds of you need say a single button to press when you want to add a new button to the calculator. Associated with this have text fields that take the function for the calculator. I would specify that the function uses variables e.g. x and y as in function = x * y. Then your program could parse the input string describing the function, by recognising "*" to mean * in the Java code syntax and "/" to mean /. Then treat everything else as variables. Since you can filter out the other variables i.e. not the operators, you can then say associate an array to contain the values of these. Then when the user uses the button and specifies the input parameters and presses the button, you can update this array, and perform the calculation based upon these values.

Hope this helps.

My advice really is just to think about everything that a button that creates new function buttons is going to need.

PS. Remember to repaint the GUI once update its Container. Swing does this automatically, but I always specify it explicitly anyway to be safe. You can use either repaint(), validate(), or revalidate().

Goodluck mate!

mightymightyeddturnipa at 2007-7-12 0:02:05 > top of Java-index,Developer Tools,Java Compiler...