RosettaCodeData/Task/Arithmetic-Integer/Onyx/arithmetic-integer.onyx

44 lines
1.2 KiB
Plaintext

# Most of this long script is mere presentation.
# All you really need to do is push two integers onto the stack
# and then execute add, sub, mul, idiv, or pow.
$ClearScreen { # Using ANSI terminal control
`\e[2J\e[1;1H' print flush
} bind def
$Say { # string Say -
`\n' cat print flush
} bind def
$ShowPreamble {
`To show how integer arithmetic in done in Onyx,' Say
`we\'ll use two numbers of your choice, which' Say
`we\'ll call A and B.\n' Say
} bind def
$Prompt { # stack: string --
stdout exch write pop flush
} def
$GetInt { # stack: name -- integer
dup cvs `Enter integer ' exch cat `: ' cat
Prompt stdin readline pop cvx eval def
} bind def
$Template { # arithmetic_operator_name label_string Template result_string
A cvs ` ' B cvs ` ' 5 ncat over cvs ` gives ' 3 ncat exch
A B dn cvx eval cvs `.' 3 ncat Say
} bind def
$ShowResults {
$add `Addition: ' Template
$sub `Subtraction: ' Template
$mul `Multiplication: ' Template
$idiv `Division: ' Template
`Note that the result of integer division is rounded toward zero.' Say
$pow `Exponentiation: ' Template
`Note that the result of raising to a negative power always gives a real number.' Say
} bind def
ClearScreen ShowPreamble $A GetInt $B GetInt ShowResults