LCB: How to evaluate an expression

LiveCode Builder is a language for extending LiveCode's capabilities, creating new object types as Widgets, and libraries that access lower-level APIs in OSes, applications, and DLLs.

Moderators: LCMark, LCfraser

Post Reply
jun
Posts: 10
Joined: Mon May 27, 2013 7:44 am
Contact:

LCB: How to evaluate an expression

Post by jun » Wed Dec 23, 2020 1:55 pm

Sorry, I am back with a newbie question.

Right now, I am trying to make an expression work. This could be an `eval` in web.

In Livecode Script, I could easily use `value` handler like this:

Code: Select all

put "10 * 20" into tExpression
answer the value of tExpression
But how can I do this in LCB? Here's what I tried:

Code: Select all

public handler testEval() returns Number
  variable tExpression as String
  put "10 * 20" into tExpression
  return the value of tExpression                                     //won't work
  //return the value of tExpression parsed as number      //won't work  
end handler

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3990
Joined: Sun Jan 07, 2007 9:12 pm
Location: Bochum, Germany

Re: LCB: How to evaluate an expression

Post by bn » Wed Dec 30, 2020 3:37 pm

In LCB you would use

Code: Select all

public handler testEval() returns Number
  variable tExpression as String
  variable tList as List
  put "10 * 20" into tExpression
  split tExpression by "*" into tList
  return (element 1 of tList parsed as number) * (element 2 of tList parsed as number)
end handler
I just picked up your code and modified it to work.

After running the code through "Extension Builder" I made a new stack with one button

Code: Select all

on mouseUp
   dispatch function "testEval"
   answer the result && it
end mouseUp
It answered 200 handled

The LCB file was a library.

The whole LCB file is

Code: Select all

library community.livecode.xxx.mylibrary
use com.livecode.canvas
use com.livecode.widget
use com.livecode.engine
use com.livecode.library.widgetutils
use com.livecode.math
use com.livecode.string

metadata version is "1.0.0"
metadata author is "yyy"
metadata title is "testlib"


public handler testEval() returns Number
  variable tExpression as String
  variable tList as List
  put "10 * 20" into tExpression
  split tExpression by "*" into tList
  return (element 1 of tList parsed as number) * (element 2 of tList parsed as number)
end handler

end library

When getting my feet wet with LCB I looked up as many examples as I could. Then starting to write my own code/copy code structures.
It took my quite some time to get it working.

In this case you try to use "value" which does not exist in LCB. Make it a habit to look up your LCB stuff in the dictionary.
LCB is close in sytax to LCS but it is definitely not the same. For one declaring and "typing" of variables needs to get used to.

I make extensive use of the "log" command to see what actually is in the variables.
Use square brackets to log more than one variable e.g.: log [variable1, variable2]

Kind regards
Bernd

Post Reply

Return to “LiveCode Builder”