This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def pow(x: Int, n: Int) = { | |
def pow(y: Int, x: Int, n: Int): Int = | |
n match { | |
case 0 => 1 | |
case 1 => x * y | |
case _ if n % 2 == 0 => pow(y, x * x, n / 2) | |
case _ => pow(x * y, x * x, (n - 1) / 2) | |
} | |
if (n < 0) sys.error(s"pow: negative exponent: $n") else pow(1, x, n) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
object Main extends App { | |
val symbols = | |
Map[String, (PrefixOperator, InfixOperator)]( | |
"+" -> (PrefixOperator(30, +_), InfixOperator(10, LeftAssoc, _ + _)), | |
"-" -> (PrefixOperator(30, -_), InfixOperator(10, LeftAssoc, _ - _)), | |
"*" -> (null, InfixOperator(20, LeftAssoc, _ * _)), | |
"/" -> (null, InfixOperator(20, LeftAssoc, _ / _)), | |
"^" -> (null, InfixOperator(30, RightAssoc, pow)), | |
"(" -> null, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def dump { | |
val cur = file.getFilePointer | |
val width = 16 | |
file.seek( 0 ) | |
def printByte( b: Int ) = print( "%02x ".format(b&0xFF).toUpperCase ) | |
def printChar( c: Int ) = print( if (' ' <= c && c <= '~') c.asInstanceOf[Char] else '.' ) | |