Show
Ignore:
Timestamp:
07/31/08 05:41:13 (5 months ago)
Author:
Chuck.Esterbrook
Message:

Added new all and any unary operators that take something enumerable (such as a list, set or generator) and return a boolean (true or false) indicating if all or any of the elements are true.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • cobra/trunk/Source/Expr.cobra

    r1562 r1564  
    23782378 
    23792379 
     2380class AllOrAnyExpr 
     2381        is abstract 
     2382        inherits Expr 
     2383        """ 
     2384        The base class for AllExpr and AnyExpr which have much in common. 
     2385         
     2386        They are unary prefix operators taking something enumerable and returning a bool. 
     2387        """ 
     2388 
     2389        var _expr as Expr 
     2390 
     2391        def init(token as IToken, expr as Expr) 
     2392                base.init(token) 
     2393                _expr = expr 
     2394 
     2395        get opName as String is abstract 
     2396 
     2397        get expr from var 
     2398 
     2399        get hasError as bool 
     2400                if base.hasError, return true 
     2401                if .expr.hasError, return true 
     2402                return false 
     2403 
     2404        get willChangeVar as bool is override 
     2405                if base.willChangeVar, return true 
     2406                if .expr.willChangeVar, return true 
     2407                return false 
     2408 
     2409        def addSubFields is override 
     2410                base.addSubFields 
     2411                .addField('expr', _expr) 
     2412 
     2413        def toCobraSource as String is override 
     2414                return '[.opName] [.expr.toCobraSource]' 
     2415 
     2416        def _bindImp 
     2417                base._bindImp 
     2418                _type = .compiler.boolType 
     2419                _expr.bindImp 
     2420                enumerable = .compiler.libraryBox('System.Collections.IEnumerable') 
     2421                if _expr.type.isDescendantOf(enumerable) 
     2422                        pass 
     2423                else if _expr.type.isDynamic 
     2424                        _expr.contextType = enumerable 
     2425                else 
     2426                        .throwError('Expecting an enumerable expression after "[.opName]", but got an expression of type "[.expr.type.name]".') 
     2427 
     2428        def writeSharpDef(sw as SharpWriter, parens as bool) is override 
     2429                sw.write('CobraImp.[Utils.capped(.opName)](') 
     2430                .expr.writeSharpDefInContext(sw) 
     2431                sw.write(')') 
     2432 
     2433        def writeSharpBreakdownItems(sw as SharpWriter) 
     2434                base.writeSharpBreakdownItems(sw) 
     2435                sw.write(', +1') 
     2436                .expr.writeSharpBreakdownItems(sw) 
     2437                sw.write(', -1') 
     2438 
     2439 
     2440class AllExpr 
     2441        inherits AllOrAnyExpr 
     2442        """ 
     2443        all <enumerable> --> true if all elements are true 
     2444        """ 
     2445         
     2446        def init(token as IToken, expr as Expr) 
     2447                base.init(token, expr) 
     2448 
     2449        get opName as String is override 
     2450                return 'all' 
     2451         
     2452 
     2453class AnyExpr 
     2454        inherits AllOrAnyExpr 
     2455        """ 
     2456        any <enumerable> --> true if any element is true 
     2457        """ 
     2458         
     2459        def init(token as IToken, expr as Expr) 
     2460                base.init(token, expr) 
     2461 
     2462        get opName as String is override 
     2463                return 'any' 
     2464 
     2465 
    23802466class UnaryOpExpr 
    23812467        inherits Expr