Source

Silver / scripts / core.sv

require('bridge');
//framework('Foundation');

@func('NSClassFromString','#@');

$LESS_THAN_OP = 3
$LESS_THAN_EQUAL_TO_OP = 4
$GREATER_THAN_OP = 5
$GREATER_THAN_EQUAL_TO_OP = 6
$LOGICAL_XOR_OP = 9
$ADD_OP = 10
$SUBTRACT_OP = 11
$MULTIPLY_OP = 12
$DIVIDE_OP = 13
$MODULUS_OP = 14
$EXP_OP = 15
$SHIFT_LEFT_OP = 16
$SHIFT_RIGHT_OP = 17
$BITWISE_AND_OP = 18
$BITWISE_OR_OP = 19
$BITWISE_XOR_OP = 20
$NEGATE_OP = 21

const $vm = svvm.alloc.init

class NSNumber {
    def -@() {
        return vm.dispatch($NEGATE_OP,$:,self)
    }

    def +@() {
        if(self < 0) {
            return -self;
        } else {
            return self;
        };
    }

    def <(obj) {
        return vm.dispatch($LESS_THAN_OP,$:,self,obj)
    }

    def <=(obj) {
        return vm.dispatch($LESS_THAN_EQUAL_TO_OP,$:,self,obj)
    }

    def >(obj) {
        return vm.dispatch($GREATER_THAN_OP,$:,self,obj)
    }

    def >=(obj) {
        return vm.dispatch($GREATER_THAN_EQUAL_TO_OP,$:,self,obj)
    }

    def +(obj) {
        return vm.dispatch($ADD_OP,$:,self,obj)
    }

    def -(obj) {
        return vm.dispatch($SUBTRACT_OP,$:,self,obj)
    }

    def *(obj) {
        return vm.dispatch($MULTIPLY_OP,$:,self,obj)
    }

    def /(obj) {
        return vm.dispatch($DIVIDE_OP,$:,self,obj)
    }

    def %(obj) {
        return vm.dispatch($MODULUS_OP,$:,self,obj)
    }

    def **(obj) {
        return vm.dispatch($EXP_OP,$:,self,obj)
    }

    def <<(obj) {
        return vm.dispatch($SHIFT_LEFT_OP,$:,self,obj)
    }

    def >>(obj) {
        return vm.dispatch($SHIFT_RIGHT_OP,$:,self,obj)
    }

    def &(obj) {
        return vm.dispatch($BITWISE_AND_OP,$:,self,obj)
    }

    def |(obj) {
        return vm.dispatch($BITWISE_OR_OP,$:,self,obj)
    }

    def ^(obj) {
        return vm.dispatch($BITWISE_XOR_OP,$:,self,obj)
    }
}

class NSObject {
    def ..(obj) {
        return SVBuiltinRange.rangeWithBeginning(self,end:obj,exclusive:NO).evaluateWithContext($:)
    }

    def ...(obj) {
        return SVBuiltinRange.rangeWithBeginning(self,end:obj,exclusive:YES).evaluateWithContext($:)
    }
    
    def ==(obj) {
        return self.isEqual(obj)
    }

    def ===(obj) {
        return self == obj;
    }

    def <=>(obj) {
        return self.compare(obj)
    }

    def is?(obj) {
        c = obj

        c = NSClassFromString(c.to_s) if c.isKindOfClass(SVSymbol.class)

        return self.isKindOfClass(c)
    }

    def class_eval(n) {
        def node(s) { SVNode.createNodeFromSource(s); };
        b = node(n);
        nd = b.statements[0];
        SVClass.evaluate(self,nd,withContext:$:);
    };

    def NSObject.class_eval(n) {
        def node(s) { SVNode.createNodeFromSource(s); };
        b = node(n);
        nd = b.statements[0];
        SVClass.evaluate(self,withObject:nd,withContext:$:)
    };

    def instance_eval(n) {
        def node(s) { SVNode.createNodeFromSource(s); };

        nd = node(n);
        nd = nd.statements[0] if nd.statements.count == 1;
        
        c = { '_SVParentContext' => $:, '__SV_IN_OBJECT' => YES, '__SV_OBJECT_CLASS' => self.class };
        c.createSymbolTable;
        t = c.symbolTable;
        sl = t.symbolWithName('self');
        sp = t.symbolWithName('super');
        sp.value = sl.value = self;
        nd.evaluateWithContext( c );
    };
}

class NSString {
    def =~(obj) {
        r = obj

        return nil unless r.isKindOfClass(SVRegex.class)

        result = r.search(self)

        c = $:
        for(i = 0; i < 3; i+=1) {
            c = c["_SVParentContext"]
        }
        result.injectVariablesFromString(self,intoContext:c) if result

        return result != nil
    }

    def +(str) {
        return self unless str.isKindOfClass(NSString.class)

        return self << str
    }

    def -(str) {
        return self unless str.isKindOfClass(NSString.class)

        return NSMutableString.stringWithString(self.stringByReplacingOccurrencesOfString(str,withString:''))
    }
}

class SVRegex {
    def =~(obj) {
        r = obj

        return nil unless r.isKindOfClass(NSString.class)

        result = self.search(r)

        c = $:
        for(i = 0; i < 3; i+=1) {
            c = c["_SVParentContext"]
        }

        result.injectVariablesFromString(r,intoContext:c) if result
        
        return result != nil
    }

    def ===(obj) {
        return self =~ obj
    }
}

class NSArray {
    def <<(rhs) {
        if(rhs.isKindOfClass(NSArray.class)) {
            self.addObjectsFromArray(rhs)
        } else {
            self.addObject(rhs)
        }
        return self
    }

    def [](i) {
        if(i.is?(:NSArray)) {
            ret = [];
            for(x = 0; x < i.count; x+=1) {
                ret << self.objectAtIndex(i[x]);
            };
            return ret;
        } else if(i >= 0) {
            return self.objectAtIndex(i)
        } else {
            return self.objectAtIndex(self.count + i);
        };
    }

    def []=(i,v) {
        self.setObject(v,atIndex:i)
        return self.objectAtIndex(i)
    }

    def ===(rhs) {
        return self.containsObject(rhs);
    }
}

class NSDictionary {
    def [](k) {
        return self.objectForKey(k)
    }

    def :: {
        return self.allKeys
    }
    
    def k(i) {
        return self.allKeys[i]
    }

    def []=(k,v) {
        self.setObject(v,forKey:k)
        return self.objectForKey(k)
    }
}

module SVDynamicIvarKeyValueSupport {
    def setValue(value,forUndefinedKey:key) {
        eval('@#{key} = #{value};');
    };
    
    def valueForUndefinedKey(key) {
        eval('@#{key}');
    };
};
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.