partly broken smalltalk impl

This commit is contained in:
jacekpoz 2024-04-12 14:02:25 +02:00
parent 5d27e61528
commit 5a60f7842c
Signed by: poz
SSH key fingerprint: SHA256:JyLeVWE4bF3tDnFeUpUaJsPsNlJyBldDGV/dIKSLyN8
5 changed files with 207 additions and 0 deletions

9
lab2/zad3/package.xml Normal file
View file

@ -0,0 +1,9 @@
<package>
<name>GF</name>
<namespace>GF</namespace>
<test>
<filein>test/Test.st</filein>
</test>
<filein>src/GF.st</filein>
<filein>src/main.st</filein>
</package>

1
lab2/zad3/run.st Normal file
View file

@ -0,0 +1 @@
PackageLoader fileInPackage: #GF.

115
lab2/zad3/src/GF.st Normal file
View file

@ -0,0 +1,115 @@
Object subclass: GF [
| characteristic value |
GF class >> create: newCharacteristic [
^self new initialize: newCharacteristic
withValue: 0;
yourself
]
GF class >> create: newCharacteristic withValue: newValue [
^self new initialize: newCharacteristic
withValue: newValue;
yourself
]
initialize: newCharacteristic withValue: newValue [
characteristic := newCharacteristic.
value := newValue.
]
verifyCharacteristics: rhs [
(characteristic ~= rhs characteristic)
ifTrue: [Error signal: 'both arguments must have the same characteristic']
]
+ rhs [
self verifyCharacteristics: rhs.
^GF create: characteristic
withValue: ((value + rhs value) \\ characteristic)
]
- rhs [
self verifyCharacteristics: rhs.
"^GF create: characteristic
withValue: ((value < (rhs value))
ifTrue: [ ^(characteristic - ((rhs value) - value)) ]
ifFalse: [ ^(value - (rhs value)) ])"
(value < (rhs value))
ifTrue: [
^GF create: characteristic
withValue: (characteristic - ((rhs value) - value))
]
ifFalse: [
^GF create: characteristic
withvalue: (value - (rhs value))
].
]
* rhs [
self verifyCharacteristics: rhs.
^GF create: characteristic
withValue: ((value * rhs value) \\ characteristic)
]
/ rhs [
| inverse |
self verifyCharacteristics: rhs.
(rhs value = 0) ifTrue: [ZeroDivide signal: 'division by 0 is illegal'].
1 to: (characteristic - 1) do: [ :i |
(((rhs value * i) \\ characteristic) = 1) ifTrue: [
inverse := i.
]
].
^GF create: characteristic
withValue: ((value * inverse) \\ characteristic)
]
= rhs [
self verifyCharacteristics: rhs.
^value = rhs value
]
~= rhs [
self verifyCharacteristics: rhs.
^value ~= rhs value
]
> rhs [
self verifyCharacteristics: rhs.
^value > rhs value
]
< rhs [
self verifyCharacteristics: rhs.
^value < rhs value
]
>= rhs [
self verifyCharacteristics: rhs.
^value >= rhs value
]
<= rhs [
self verifyCharacteristics: rhs.
^value <= rhs value
]
characteristic [
^characteristic
]
value [
^value
]
displayOn: stream [
'GF<' displayOn: stream.
characteristic displayOn: stream.
'>(' displayOn: stream.
value displayOn: stream.
')' displayOn: stream.
]
]

32
lab2/zad3/src/main.st Normal file
View file

@ -0,0 +1,32 @@
FileStream fileIn: 'src/GF.st'.
a := GF create: 1234577 withValue: ((stdin nextLine) asInteger).
b := GF create: 1234577 withValue: ((stdin nextLine) asInteger).
a displayOn: stdout.
' + ' displayOn: stdout.
b displayOn: stdout.
' = ' displayOn: stdout.
(a + b) displayOn: stdout.
'' displayNl.
a displayOn: stdout.
' - ' displayOn: stdout.
b displayOn: stdout.
' = ' displayOn: stdout.
(a - b) displayOn: stdout.
'' displayNl.
a displayOn: stdout.
' * ' displayOn: stdout.
b displayOn: stdout.
' = ' displayOn: stdout.
(a * b) displayOn: stdout.
'' displayNl.
a displayOn: stdout.
' / ' displayOn: stdout.
b displayOn: stdout.
' = ' displayOn: stdout.
(a / b) displayOn: stdout.
'' displayNl.

50
lab2/zad3/test/Test.st Normal file
View file

@ -0,0 +1,50 @@
FileStream fileIn: 'src/GF.st'.
a := GF create: 5 withValue: 2.
b := GF create: 5 withValue: 4.
(a + b) = (GF create: 5 withValue: 1)
ifFalse: [self error: 'failed on the addition test!'].
(a - b) = (GF create: 5 withValue: 3)
ifFalse: [self error: 'failed on the subtraction test!'].
a := GF create: 6 withValue: 2.
b := GF create: 6 withValue: 4.
(a * b) = (GF create: 6 withValue: 2)
ifFalse: [self error: 'failed on the multiplication test!'].
a := GF create: 7 withValue: 2.
b := GF create: 7 withValue: 4.
(a / b) = (GF create: 7 withValue: 4)
ifFalse: [self error: 'failed on the division test!'].
a := GF create: 5 withValue: 2.
b := GF create: 5 withValue: 0.
exceptionRaised := false.
(a / b) on: ZeroDivide do: [ :sig |
exceptionRaised := true.
((sig errorString) = 'division by 0 is illegal')
ifFalse: [self error: 'division by 0 error string doesn''t match!'].
].
exceptionRaised
ifFalse: [self error: 'failed on the division by 0 test!'].
a := GF create: 10 withValue: 5.
b := GF create: 8 withValue: 5.
exceptionRaised := false.
(a + b) on: Error do: [ :sig |
exceptionRaised := true.
((sig errorString) = 'both arguments must have the same characteristic')
ifFalse: [self error: 'different characteristics error string doesn''t match!'].
].
exceptionRaised
ifFalse: [self error: 'failed on the different characteristics test!'].