| 1 | data Object = Operatortype Operator | Integertype Integer | Realtype Float deriving (Eq,Show) |
|---|
| 2 | type Stack = [Object] |
|---|
| 3 | type Operator= (Stack->Stack) |
|---|
| 4 | instance Eq Operator where |
|---|
| 5 | _ == _ = error "typecheck" |
|---|
| 6 | instance Show Operator where |
|---|
| 7 | show _ = error "can't show operator!" |
|---|
| 8 | instance Num Object where |
|---|
| 9 | (Integertype a) + (Integertype b) = Integertype (a+b) |
|---|
| 10 | (Realtype a) + (Realtype b) = Realtype (a + b) |
|---|
| 11 | (Realtype a) + (Integertype b) = Realtype (a + fromInteger(b) ) |
|---|
| 12 | (Integertype b) + (Realtype a) = Realtype (a + fromInteger(b) ) |
|---|
| 13 | _ + _ = error "typecheck" |
|---|
| 14 | apply2:: (Object->Object->Object)->Stack->Stack |
|---|
| 15 | apply2 op (s0:(s1:s2)) = ((op s0 s1):s2) |
|---|
| 16 | |
|---|
| 17 | add:: Operator |
|---|
| 18 | add = apply2 (+) |
|---|
| 19 | sub:: Operator |
|---|
| 20 | sub = apply2 (-) |
|---|