Show
Ignore:
Timestamp:
07/30/08 02:34:05 (5 months ago)
Author:
Chuck.Esterbrook
Message:

Added support for declaring and raising events.

Files:
1 modified

Legend:

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

    r1553 r1560  
    11611161 
    11621162 
     1163class RaiseStmt 
     1164        inherits Stmt 
     1165        """ 
     1166        Raise an event. 
     1167         
     1168        TODO: give a good error message for the case where the user is trying to raise an exception 
     1169        TODO: test raising an inherited event, either from source or from binary 
     1170        TODO: support missing arguments such as 'this' and 'EventArgs()' 
     1171        """ 
     1172 
     1173        var _name = '' 
     1174        var _exprs as List<of Expr> 
     1175        var _definition as BoxEvent? 
     1176 
     1177        def init(token as IToken, exprs as List<of Expr>) 
     1178                require exprs.count > 0 
     1179                base.init(token) 
     1180                _exprs = exprs 
     1181 
     1182        def addSubFields 
     1183                base.addSubFields 
     1184                .addField('exprs', _exprs) 
     1185 
     1186        get name from var 
     1187 
     1188        get exprs from var 
     1189 
     1190        def _bindImp 
     1191                base._bindImp 
     1192                hasError = false 
     1193                for expr in _exprs 
     1194                        try 
     1195                                expr.bindImp 
     1196                        catch ne as NodeException 
     1197                                .compiler.recordError(ne) 
     1198                                hasError = true 
     1199                if not hasError 
     1200                        expr = _exprs[0] 
     1201                        if expr inherits DotExpr 
     1202                                right = expr.right 
     1203                                if right inherits IDotRightExpr 
     1204                                        defi = (right to dynamic).definition 
     1205                                        if defi inherits BoxEvent 
     1206                                                _definition = defi 
     1207                                                _name = defi.name 
     1208                                        else 
     1209                                                .throwError('Expecting an event to raise.') 
     1210                                else 
     1211                                        assert false # should be impossible to get here since it was a dotexpr 
     1212                        else 
     1213                                .throwError('Invalid expression for raising events. Try "raise .someEvent, args" or "throw SomeException(args)".') 
     1214 
     1215        def writeSharpDef(sw as SharpWriter) 
     1216                base.writeSharpDef(sw) 
     1217                name = Utils.capped(.name) 
     1218                sw.write('if (this.[name]!=null) this.[name](') 
     1219                sep = '' 
     1220                for expr in _exprs[1:] 
     1221                        sw.write(sep) 
     1222                        expr.writeSharpDef(sw, false) 
     1223                        sep = ', ' 
     1224                sw.write(');\n') 
     1225 
     1226 
    11631227class ReturnStmt 
    11641228        inherits Stmt 
     
    11741238                base.addSubFields 
    11751239                .addField('expr', _expr) 
     1240 
     1241        get expr from var 
    11761242 
    11771243        pro sharpResultVarName from var