Wiki

Ticket #161 (closed enhancement: fixed)

Opened 16 years ago

Last modified 15 years ago

Support the expression: 0 <= x < 10

Reported by: Chuck Owned by: eric.sellon
Priority: medium Milestone:
Component: Cobra Compiler Version: 0.8.0
Keywords: Cc:

Description

Checking that a numeric variable falls between two boundaries can come up frequently for some applications. And especially in Cobra which has support for assert, require, ensure and invariant.

The general form is:

<boundaryA> <compare> <expression> <compare> <boundaryB>

Python can be mined for more information as this feature comes directly from that language.

Note that the <expression> should not be evaluated more than once. The test suite should verify that.

 Discussed here.

Attachments

ticket161.patch Download (8.9 KB) - added by eric.sellon 15 years ago.

Change History

Changed 15 years ago by eric.sellon

  • owner set to eric.sellon
  • status changed from new to assigned

Changed 15 years ago by eric.sellon

The attached patch adds the ability to chain comparisons like in Python. The syntax is more like:

<expression> <compare> <expression> <compare> <expression> ... <compare> <expression>

See  http://docs.python.org/reference/expressions.html#notin for more information. There is no hard coded limit on how many comparisons can be chained. The only difference right now between the linked Python documentation and the Cobra implementation in the patch file is that all comparisons will be evaluated before returning a result. Python will quit after the first false. Adding this would not be difficult if this is desired (i.e. one line). This would be done in CobraCore.cobra in the function chainedComparison with the following line in the loop:

if not returnValue, return false

Please note that this patch only addresses the CLR backend, but it should be fairly easy to add it to the other backends since the functionality has been added to the Cobra Lang library in order to be fairly backend independent. Also the removal of a troublesome not clause in Boxes.cobra has been retained in this patch.

Changed 15 years ago by eric.sellon

Well looks like I was wrong. Adding that line will bail the comparisons early but will not prevent all the expressions from being evaluated. This is due to the use of a function to implement the feature.

Changed 15 years ago by Chuck

As I was getting close to checking in the patch, I found two major problems. I only know the symptoms; I have not investigated the cause.

Problem 1:

class Test
	
	def main
		assert 3 < 10 < 7
# error: Unexpected symbol ")" (C#)

Problem 2:

class Test
	
	def main
		assert not (3 < 10 < 7)
/#
Unhandled Exception: Cobra.Lang_ert_9241f11c821083e383cbd6e8a86f4118.RequireException: 
sourceSite = /Users/chuck/Projects/Cobra/workspace-c/Source/Expr.cobra:137 in Expr.isKindOf for object CompareExpr-sh(6456, didBindInh=false, didBindInt=false, didStartBindImp=true, didBindImp=true, token=Token(LT, '<', '<', ln 12, col 17, 256-chained-comparison-mod.cobra), type=nil, 6456)
info       = nil
this       = CompareExpr-sh(6456, didBindInh=false, didBindInt=false, didStartBindImp=true, didBindImp=true, token=Token(LT, '<', '<', ln 12, col 17, 256-chained-comparison-mod.cobra), type=nil, 6456)
    .type = nil

  at Expr.IsKindOf (IType type) [0x00000] 
  at TruthExpr._bindImp () [0x00000] 
  at Node.BindImp () [0x00000] 
...
#/

So a false compilation error (caused by bad generated code) and an internal/uncaught compiler exception. We'll need these fixed before proceeding.

Changed 15 years ago by eric.sellon

Changed 15 years ago by eric.sellon

Fixed.

Changed 15 years ago by Chuck

  • status changed from assigned to closed
  • resolution set to fixed

Thanks. Applied in changeset:2229

Changed 15 years ago by Chuck

Note: See TracTickets for help on using tickets.