A new version of Kernel was added to project The Inbox:
http://source.squeak.org/inbox/Kernel-nice.692.mcz ==================== Summary ==================== Name: Kernel-nice.692 Author: nice Time: 22 May 2012, 10:58:15.58 pm UUID: 11e38193-788b-4aa2-8480-a04d6afb2ec5 Ancestors: Kernel-nice.691 Let Fraction readFrom: '1/2' and Fraction readFrom: '0.1' answer the expected fractions. The sign, if any, must be attached to numerator. =============== Diff against Kernel-nice.691 =============== Item was added: + ----- Method: ExtendedNumberParser>>nextFraction (in category 'parsing-public') ----- + nextFraction + | numerator denominator numberOfTrailingZeroInIntegerPart | + base := 10. + neg := self peekSignIsMinus. + (integerPart := self nextUnsignedIntegerOrNilBase: base) + ifNil: [numberOfTrailingZeroInIntegerPart := 0] + ifNotNil: [ + numberOfTrailingZeroInIntegerPart := nDigits - lastNonZero. + (sourceStream peekFor: $r) + ifTrue: ["<base>r<integer>" + (base := integerPart) < 2 + ifTrue: [ + sourceStream skip: -1. + ^ self expected: 'an integer greater than 1 as valid radix']. + self peekSignIsMinus + ifTrue: [neg := neg not]. + integerPart := self nextUnsignedIntegerBase: base. + numberOfTrailingZeroInIntegerPart := nDigits - lastNonZero]]. + (sourceStream peekFor: $.) + ifTrue: + [^self readFractionPartNumberOfTrailingZeroInIntegerPart: numberOfTrailingZeroInIntegerPart]. + integerPart + ifNil: + ["No integerPart, raise an error" + ^ self expected: 'a digit']. + numerator := neg + ifTrue: [integerPart negated] + ifFalse: [integerPart]. + self readExponent ifTrue: [numerator := numerator * (base raisedToInteger: exponent)]. + (sourceStream peekFor: $/) ifFalse: [^numerator]. + base := 10. + (denominator := self nextUnsignedIntegerOrNilBase: base) + ifNil: + [sourceStream skip: -1. "Not a valid denominator, ungobble / and return numerator" + ^numerator]. + (sourceStream peekFor: $r) + ifTrue: ["<base>r<integer>" + (base := denominator) < 2 + ifTrue: [ + sourceStream skip: -1. + ^ self expected: 'an integer greater than 1 as valid radix']. + denominator := self nextUnsignedIntegerBase: base]. + self readExponent ifTrue: [denominator := denominator * (base raisedToInteger: exponent)]. + ^numerator / denominator! Item was added: + ----- Method: ExtendedNumberParser>>readFractionPartNumberOfTrailingZeroInIntegerPart: (in category 'parsing-private') ----- + readFractionPartNumberOfTrailingZeroInIntegerPart: numberOfTrailingZeroInIntegerPart + "at this stage, sign integerPart and a fraction point have been read. + try and form a number with a fractionPart" + + | numberOfNonZeroFractionDigits numberOfTrailingZeroInFractionPart mantissa value | + fractionPart := self nextUnsignedIntegerOrNilBase: base. + fractionPart + ifNil: [ + "No fractionPart found, but can be an extended 1.e2 syntax" + integerPart ifNil: ["No integerPart, nor fractionPart found, ungobble the fraction point and raise an error" + sourceStream skip: -1. + ^self expected: 'a digit']. + fractionPart := 0. + numberOfNonZeroFractionDigits := 0. + numberOfTrailingZeroInFractionPart := 0] + ifNotNil: [. + numberOfNonZeroFractionDigits := lastNonZero. + numberOfTrailingZeroInFractionPart := nDigits - lastNonZero]. + self readExponent. + integerPart ifNil: [integerPart := 0]. + + fractionPart isZero + ifTrue: [mantissa := integerPart + // (base raisedToInteger: numberOfTrailingZeroInIntegerPart). + exponent := exponent + numberOfTrailingZeroInIntegerPart] + ifFalse: [mantissa := integerPart + * (base raisedToInteger: numberOfNonZeroFractionDigits) + (fractionPart // (base raisedToInteger: numberOfTrailingZeroInFractionPart)). + exponent := exponent - numberOfNonZeroFractionDigits]. + + value := exponent positive + ifTrue: [mantissa * (base raisedToInteger: exponent)] + ifFalse: [mantissa / (base raisedToInteger: exponent negated)]. + ^ neg + ifTrue: [value negated] + ifFalse: [value]! Item was added: + ----- Method: Fraction class>>readFrom: (in category 'instance creation') ----- + readFrom: stringOrStream + "Answer a fraction as described on aStream. + The fraction may be specified as numerator/denominator, or just a numerator, or as integerPart.fractionPart. + In numerator/denominator form, both parts may have a radix specification and an exponent, but only numerator can have a sign. + In fractional digits form, an alternate radix and an exponent can also be provided, the integer or fraction part being optional." + + ^(ExtendedNumberParser on: stringOrStream) nextFraction! Item was added: + ----- Method: Fraction class>>readFrom:base: (in category 'instance creation') ----- + readFrom: stringOrStream base: aRadix + "Answer a Fraction as described on aStream. + The Fraction may be specified as numerator/denominator, or just with numerator. + Both numerator and denominator can have an exponent, but only numerator can have a sign." + + ^(ExtendedNumberParser on: stringOrStream) nextFractionBase: aRadix! Item was added: + ----- Method: NumberParser>>nextInteger (in category 'parsing-public') ----- + nextInteger + "Read an Integer from sourceStream, asnwser that Integer. + This is a generic version dealing with an optional sign and a simple sequence of decimal digits. + Subclass might define extended syntax." + + base := 10. + ^self nextIntegerBase: base ifFail: [^self expected: ('a digit between 0 and ' copyWith: (Character digitValue: base - 1))]! Item was added: + ----- Method: NumberParser>>nextUnsignedInteger (in category 'parsing-public') ----- + nextUnsignedInteger + "Read an Integer from sourceStream, asnwser that Integer. + This is a generic version dealing with a simple sequence of decimal digits. + Subclass might define extended syntax." + + base := 10. + ^self nextUnsignedIntegerBase: base ifFail: [^self expected: ('a digit between 0 and ' copyWith: (Character digitValue: base - 1))]! Item was added: + ----- Method: SqNumberParser>>nextFraction (in category 'parsing-public') ----- + nextFraction + | numerator denominator numberOfTrailingZeroInIntegerPart | + base := 10. + neg := self peekSignIsMinus. + (integerPart := self nextUnsignedIntegerOrNilBase: base) + ifNil: ["No integerPart, raise an error" + ^ self expected: 'a digit']. + numberOfTrailingZeroInIntegerPart := nDigits - lastNonZero. + (sourceStream peekFor: $r) + ifTrue: ["<base>r<integer>" + (base := integerPart) < 2 + ifTrue: [ + sourceStream skip: -1. + ^ self expected: 'an integer greater than 1 as valid radix']. + self peekSignIsMinus + ifTrue: [neg := neg not]. + integerPart := self nextUnsignedIntegerBase: base. + numberOfTrailingZeroInIntegerPart := nDigits - lastNonZero]. + (sourceStream peekFor: $.) + ifTrue: + [^self readFractionPartNumberOfTrailingZeroInIntegerPart: numberOfTrailingZeroInIntegerPart]. + numerator := neg + ifTrue: [integerPart negated] + ifFalse: [integerPart]. + self readExponent ifTrue: [numerator := numerator * (base raisedToInteger: exponent)]. + (sourceStream peekFor: $/) ifFalse: [^numerator]. + base := 10. + (denominator := self nextUnsignedIntegerOrNilBase: base) + ifNil: + [sourceStream skip: -1. "Not a valid denominator, ungobble / and return numerator" + ^numerator]. + (sourceStream peekFor: $r) + ifTrue: ["<base>r<integer>" + (base := denominator) < 2 + ifTrue: [ + sourceStream skip: -1. + ^ self expected: 'an integer greater than 1 as valid radix']. + denominator := self nextUnsignedIntegerBase: base]. + self readExponent ifTrue: [denominator := denominator * (base raisedToInteger: exponent)]. + ^numerator / denominator! Item was added: + ----- Method: SqNumberParser>>nextFractionBase: (in category 'parsing-public') ----- + nextFractionBase: aRadix + | numerator denominator | + base := aRadix. + neg := self peekSignIsMinus. + (integerPart := self nextUnsignedIntegerOrNilBase: base) + ifNil: [^self expected: 'a digit between 0 and ' , (Character digitValue: aRadix)]. + numerator := neg + ifTrue: [integerPart negated] + ifFalse: [integerPart]. + self readExponent ifTrue: [numerator := numerator * (base raisedToInteger: exponent)]. + (sourceStream peekFor: $/) ifFalse: [^numerator]. + (denominator := self nextUnsignedIntegerOrNilBase: base) + ifNil: + [sourceStream skip: -1. "Not a valid denominator, ungobble / and return numerator" + ^numerator]. + self readExponent ifTrue: [denominator := denominator * (base raisedToInteger: exponent)]. + ^numerator / denominator! Item was added: + ----- Method: SqNumberParser>>nextInteger (in category 'parsing-public') ----- + nextInteger + "Read an Integer from sourceStream, asnwser that Integer. + In Smalltalk syntax, a radix can be specified, and an exponent too." + + | numberOfTrailingZeroInIntegerPart | + base := 10. + neg := self peekSignIsMinus. + integerPart := self nextUnsignedIntegerOrNilBase: base. + numberOfTrailingZeroInIntegerPart := nDigits - lastNonZero. + (sourceStream peekFor: $r) + ifTrue: ["<base>r<integer>" + (base := integerPart) < 2 + ifTrue: [ + sourceStream skip: -1. + ^ self expected: 'an integer greater than 1 as valid radix']. + self peekSignIsMinus + ifTrue: [neg := neg not]. + integerPart := self nextUnsignedIntegerBase: base. + numberOfTrailingZeroInIntegerPart := nDigits - lastNonZero]. + ^ self makeIntegerOrScaledInteger! Item was added: + ----- Method: SqNumberParser>>nextUnsignedInteger (in category 'parsing-public') ----- + nextUnsignedInteger + "Read an unsigned Integer from sourceStream, asnwser that Integer. + In Smalltalk syntax, a radix can be specified, and an exponent too." + + base := 10. + neg := false. + integerPart := self nextUnsignedIntegerOrNilBase: base. + (sourceStream peekFor: $r) + ifTrue: ["<base>r<integer>" + (base := integerPart) < 2 + ifTrue: [ + sourceStream skip: -1. + ^ self expected: 'an integer greater than 1 as valid radix']. + integerPart := self nextUnsignedIntegerBase: base]. + ^ self makeIntegerOrScaledInteger! Item was added: + ----- Method: SqNumberParser>>readFractionPartNumberOfTrailingZeroInIntegerPart: (in category 'parsing-private') ----- + readFractionPartNumberOfTrailingZeroInIntegerPart: numberOfTrailingZeroInIntegerPart + "at this stage, sign integerPart and a decimal point have been read. + try and form a number with a fractionPart" + + | numberOfNonZeroFractionDigits numberOfTrailingZeroInFractionPart mantissa value | + fractionPart := self nextUnsignedIntegerOrNilBase: base. + fractionPart ifNil: ["No fractionPart found, ungobble the decimal point and return the integerPart" + sourceStream skip: -1. + ^ neg + ifTrue: [integerPart negated] + ifFalse: [integerPart]]. + numberOfNonZeroFractionDigits := lastNonZero. + numberOfTrailingZeroInFractionPart := nDigits - lastNonZero. + self readExponent. + fractionPart isZero + ifTrue: [mantissa := integerPart + // (base raisedToInteger: numberOfTrailingZeroInIntegerPart). + exponent := exponent + numberOfTrailingZeroInIntegerPart] + ifFalse: [mantissa := integerPart + * (base raisedToInteger: numberOfNonZeroFractionDigits) + (fractionPart // (base raisedToInteger: numberOfTrailingZeroInFractionPart)). + exponent := exponent - numberOfNonZeroFractionDigits]. + + value := exponent positive + ifTrue: [mantissa * (base raisedToInteger: exponent)] + ifFalse: [mantissa / (base raisedToInteger: exponent negated)]. + ^ neg + ifTrue: [value negated] + ifFalse: [value]! |
Free forum by Nabble | Edit this page |