Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
Bug 326855 - provide constant expression evaluator
Summary: provide constant expression evaluator
Status: RESOLVED FIXED
Alias: None
Product: z_Archived
Classification: Eclipse Foundation
Component: b3 (show other bugs)
Version: unspecified   Edit
Hardware: All All
: P3 enhancement (vote)
Target Milestone: ---   Edit
Assignee: Project Inbox CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks: 306547 307011
  Show dependency tree
 
Reported: 2010-10-02 21:38 EDT by Henrik Lindberg CLA
Modified: 2017-09-13 11:52 EDT (History)
0 users

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Henrik Lindberg CLA 2010-10-02 21:38:41 EDT
Provide a constant expression evaluator.

Suggested implementation:
Use a polymorphic dispatcher.
Use a return type of ConstantResult with an indicator if expression was constant as evaluation of a constant expression is the same as detecting if an expression is constant and it allows computation of "null" result.

The constant expression evaluator is useful when computing "unreachable code" and similar.

A simple implementation could support operators directly (i.e. + - * / etc), but to be really useful it needs to be able to call system functions, use lists and arrays etc. This requires access to B3Engine for the special purpose of evaluating constants (as the system functions require a context).

as an example
if [1,2 3] == [1,2,3] then foo() else bar() endif;

should result in an unreachable code error for the else part.

Constant detection can get quite complicated:

if [ {|10}, {|20+30} ].collect(f | f.evaluate()) ~= [ _, 50 ] then foo() else bar() endif; 

System functions would need to be able to answer if they produce a constant result given constant input.
Comment 1 Henrik Lindberg CLA 2010-10-03 18:35:55 EDT
Implementing a constant expression evaluator turned out to be a quite simple exercise since the relevant system, arithmetic and relational functions could be used without an execution context. In total 229 lines of code.

A simple implementation is now available in org.eclipse.b3.backend.evaluator.B3ConstantEvaluator.
It supports:
Arithmetic: + - * / unary - (numbers) 
String : + (concatenation of strings and string/number combinations)
Relational: all relational operations except matches

expressions: &&, ||, !, val

literal values : strings, numbers, ANY

All other expressions are considered to be variable.

It is possible to enhance the constant evaluator with support for constant lists and maps, lambdas that return constant values, etc, but it should be good enough to capture common developer mistakes.

In revision 1328.