- On the Browser create a new category: 'FooFloat'. Create the plugin as subclass of InterpreterPlugin
InterpreterPlugin subclass: #FooFloatPlugin
instanceVariableNames: ''
classVariableNames: ''
poolDictionaries: ''
category: 'FooFloat'
- We will write two primitives, one to answer identity of a float, the other to answer sum of two floats. Both primitives are of the class FooFloatPlugin.
primsIdentityOfFloat": aFloat"
"primitive, return the identity of a float"
| aFloat |
self export: true.
self inline: false.
self var: aFloat type:' float'.
aFloat := interpreterProxy stackFloatValue: 0.
interpreterProxy pop:2.
interpreterProxy push: (interpreterProxy floatObjectOf: aFloat)
- Primitive to answer sum of two floats.
primsSumOfFloatAnd": float1 :float2"
"primitive, return the identity of a float"
| float1 float2 |
self export: true.
self inline: false.
self var: float1 type:' float'.
self var: float2 type:' float '.
float2 := interpreterProxy stackFloatValue: 0.
float1 := interpreterProxy stackFloatValue: 1.
interpreterProxy pop:3.
interpreterProxy push: (interpreterProxy floatObjectOf: (float1 + float2))
- Add an interface for the plugin by creating a class named 'FooFloat'.
Object subclass: #FooFloat
instanceVariableNames: ''
classVariableNames: ''
poolDictionaries: ''
category: 'FooFloat'
- We will write two methods to call the two primitives, in class FooFloat. Firstly identityOfFloat:
identityOfFloat: aFloat
"return identity of a float"
< primitive: 'primsIdentityOfFloat' module: 'FooFloatPlugin'>
^ FooFloatPlugin doPrimitive: 'primsIdentityOfFloat'
Note: first statement call the primitive, second statement will execute when primitive fail, or if primitive not found.
- Secondly, sumOfFloat: and:
sumOfFloat:float1 and: float2
"return sum of two floats"
< primitive: 'primsSumOfFloatAnd' module: 'FooFloatPlugin'>
^ FooFloatPlugin doPrimitive: 'primsSumOfFloatAnd'
- Write a class-side method for the plugin.
moduleName
^ 'FooFloatPlugin'
- Drag a workspace from the Tools flab. We can now test the primitive. Type the following in the workspace.
FooFloat new identityOfFloat: 12.34567
FooFloat new sumOfFloat: 12.34567 and: 13.45678
ctrl + p at the end of each like you will get the answers which is 12.34567 and 25.80245. Note that at this point primitive will fail, as we haven't generate the plugin, answer are returned by simulator.