Adobe Source Libraries 1.49.0
A collection of C++ libraries.
Loading...
Searching...
No Matches
virtual_machine_t Class Reference

Stack-based non-branching expression evaluator. More...

#include <adobe/virtual_machine.hpp>

Public Types

typedef array_t expression_t
typedef any_regular_t variable_lookup_signature_t(name_t)
typedef any_regular_t dictionary_function_lookup_signature_t(name_t, const dictionary_t &)
typedef any_regular_t array_function_lookup_signature_t(name_t, const array_t &)
typedef any_regular_t named_index_lookup_signature_t(const any_regular_t &, name_t index)
typedef any_regular_t numeric_index_lookup_signature_t(const any_regular_t &, std::size_t index)
using variable_lookup_t
using dictionary_function_lookup_t
using array_function_lookup_t
using named_index_lookup_t
using numeric_index_lookup_t
typedef any_regular_t binary_op_override_signature_t(const any_regular_t &, const any_regular_t &)
using binary_op_override_t

Public Member Functions

 virtual_machine_t ()
 virtual_machine_t (const virtual_machine_t &)
virtual_machine_toperator= (const virtual_machine_t &rhs)
 ~virtual_machine_t ()
void evaluate (const expression_t &expression)
const any_regular_tback () const
any_regular_tback ()
void pop_back ()
void set_variable_lookup (const variable_lookup_t &)
void set_array_function_lookup (const array_function_lookup_t &)
void set_dictionary_function_lookup (const dictionary_function_lookup_t &)
void set_named_index_lookup (const named_index_lookup_t &)
void set_numeric_index_lookup (const numeric_index_lookup_t &)
void override_operator (name_t, const binary_op_override_t &)

Detailed Description

virtual_machine_t is a stack-based expression evaluator. There are several built-in operators as well as named- and unnamed- argument functions, as well as the capability of extending these functions within a given client.
An expression is simply a sequence of elements, some of which can be expressions themselves. The virtual machine iterates through the expression sequence, pushing all the elements it finds onto the evaluation stack until it runs across an execute token. An execute token is any adobe::name_t whose first character is a period ('.'). When the evaluator finds this kind of token it is used as the name of the operator, and one or more arguments are popped off the stack successively for this operator. The evaluation of the arguments is handled by recursing to evaluate. There is no branching in the evaluator. The result of the operator is then pushed onto the stack and the evaluation continues down the expression sequence. Conditionals are handled with recursive evaluates; the machine always moves forward. The evaluator is very specifically not turing complete. It has no loops, no primitive recursion within the machine model, and it is not infinite.

Operators

The following table describes the built-in operators available for use in the virtual machine. For each operation there is listed one or more required parameters that must be pushed onto the stack for the operation to succeed. The result of the operation is pushed onto the top of the stack. All values must meet the requirements for adobe::any_regular_t.
Unary Operation NameParameterResult
.notbooleanlogical not
.unary_negatenumberunary negation
Binary Operation NameParametersResult
.addnumber_0number_1number_0 + number_1
.subtractnumber_0number_1number_0 - number_1
.multiplynumber_0number_1number_0 * number_1
.modulusnumber_0number_1number_0 % number_1
.dividenumber_0number_1number_0 / number_1
Predicate Operation NameParametersResult
.lessnumber_0number_1number_0 < number_1
.greaternumber_0number_1number_0 > number_1
.less_equalnumber_0number_1number_0 <= number_1
.greater_equalnumber_0number_1number_0 >= number_1
.equalvalue_0value_1value_0 == value_1
.not_equalvalue_0value_1value_0 != value_1
.and_opbooleanexpressionboolean && expression [1] [3]
.or_opbooleanexpressionboolean || expression [2] [3]
Misc. Operation NameParametersResult
.arraynumbernumber unnamed argument(s)an array_t containing the top number elements in the evaluation stack
.dictionarynumbernumber key/value pair(s) (named argument(s))a dictionary_t containing the top number key/value pairs in the evaluation stack
.ifelsebooleanexpression_0expression_1evaluated expression_0 if boolean is true; evaluated expression_1 otherwise [4]
.indexarraynumberevaluated array[number]
.indexdictionarynameevaluated dictionary[name]
.functionnamedictionary of expressionsresult of calling function name with argument dictionary
.functionnamearray of expressionsresult of calling function name with argument array
.variablenameevaluated value of name
Notes
[1] expression is evaluated only if boolean is true.
[2] expression is evaluated only if boolean is false.
[3] expression must yield a boolean.
[4] only the pertinent expression is evaluated.

Named Functions

When looking up named functions in your client-side callback, the name of the function will be the adobe::name_t parameter of the callback, and any arguments to that function will be in the second parameter. You are required to return the value returned by your client-side function as a any_regular_t, which will be pushed onto the evaluation stack of the machine. Note that these do not have periods in their names, as they are not operations but rather the first parameter to the .function operator.
Unnamed Argument FunctionsArgumentsResult
contributingA sequence of unnamed argumentsa dictionary_t outlining the values that contributed to this value [5]
maxThe largest of the evaluated numbers [6]
minThe smallest of the evaluated numbers [6]
roundRounds the argument to the nearest whole number [5]
typeofSpecifies the type of the element [5]
Named Argument FunctionsArgument TypeArgument NameArgument NoteResult
scalenumbermLinear scale slope.
Default: 1
Perform a linear scaling of x
numberbLinear scale y-intercept.
Default: 0
numberxValue to scale.
Default: 0
Notes
[5] Only operates on the first element in the argument list.
[6] All values in the argument list must be numbers.

Example

The following is a pdf-formatted expression sequence for the expression
  round(x<3 ? 2+9/5 : 7.8-2.94)
% pdf output
[
 /x /.variable 3 /.less
 [
  2 9 5 /.divide /.add
 ]
 [
  7.800000000000000 2.940000000000000 /.subtract
 ]
 /.ifelse 1 /.array /round /.function
]
% end pdf output
The following is a pdf-formatted expression sequence for the expression
  {name: @value, items: [4, "Hello"]}
% pdf output
[
 /name /value
 /items 4 (Hello) 2 /.array
 2 /.dictionary
]
% end pdf output

Definition at line 67 of file virtual_machine.hpp.

Member Typedef Documentation

◆ expression_t

The expression to be evaluated, broken up into elements of an adobe::array_t.

Definition at line 69 of file virtual_machine.hpp.

◆ variable_lookup_signature_t

typedef any_regular_t variable_lookup_signature_t(name_t)

Function signature for variable lookup functions.

Definition at line 71 of file virtual_machine.hpp.

◆ dictionary_function_lookup_signature_t

typedef any_regular_t dictionary_function_lookup_signature_t(name_t, const dictionary_t &)

Function signature for functions with dictionary-based (named) arguments.

Definition at line 72 of file virtual_machine.hpp.

◆ array_function_lookup_signature_t

typedef any_regular_t array_function_lookup_signature_t(name_t, const array_t &)

Function signature for functions with array-based (unnamed) arguments.

Definition at line 73 of file virtual_machine.hpp.

◆ named_index_lookup_signature_t

typedef any_regular_t named_index_lookup_signature_t(const any_regular_t &, name_t index)

Definition at line 74 of file virtual_machine.hpp.

◆ numeric_index_lookup_signature_t

typedef any_regular_t numeric_index_lookup_signature_t(const any_regular_t &, std::size_t index)

Definition at line 75 of file virtual_machine.hpp.

◆ variable_lookup_t

A function used to map an identifier with its underlying tracked_value_t.

Definition at line 78 of file virtual_machine.hpp.

◆ dictionary_function_lookup_t

A function callback used for client-side named-argument functions.

Definition at line 79 of file virtual_machine.hpp.

◆ array_function_lookup_t

A function callback used for client-side unnamed-argument functions.

Definition at line 80 of file virtual_machine.hpp.

◆ named_index_lookup_t

Definition at line 81 of file virtual_machine.hpp.

◆ numeric_index_lookup_t

Definition at line 82 of file virtual_machine.hpp.

◆ binary_op_override_signature_t

typedef any_regular_t binary_op_override_signature_t(const any_regular_t &, const any_regular_t &)

Definition at line 84 of file virtual_machine.hpp.

◆ binary_op_override_t

Definition at line 87 of file virtual_machine.hpp.

Constructor & Destructor Documentation

◆ virtual_machine_t() [1/2]

◆ virtual_machine_t() [2/2]

◆ ~virtual_machine_t()

Member Function Documentation

◆ operator=()

virtual_machine_t & operator= ( const virtual_machine_t & rhs)

◆ evaluate()

void evaluate ( const expression_t & expression)
Parameters
expressionAn array_t comprising all the elements of the expression for evaluation.
Note
The result(s) of the expression will be pushed onto the evaluation stack of this virtual machine.

◆ back() [1/2]

const any_regular_t & back ( ) const
Returns
A reference to the top of the evaluation stack.

◆ back() [2/2]

any_regular_t & back ( )

◆ pop_back()

void pop_back ( )

Pops the top of the evaluation stack

◆ set_variable_lookup()

void set_variable_lookup ( const variable_lookup_t & lookup)

Sets the function callback for variable lookup.

Parameters
lookupThe function to be used as the callback.

◆ set_array_function_lookup()

void set_array_function_lookup ( const array_function_lookup_t & )

◆ set_dictionary_function_lookup()

void set_dictionary_function_lookup ( const dictionary_function_lookup_t & )

◆ set_named_index_lookup()

void set_named_index_lookup ( const named_index_lookup_t & )

◆ set_numeric_index_lookup()

void set_numeric_index_lookup ( const numeric_index_lookup_t & )

◆ override_operator()

void override_operator ( name_t ,
const binary_op_override_t &  )