Created
September 10, 2015 13:49
-
-
Save mkhoeini/b8a8ac8478741b24b9ad to your computer and use it in GitHub Desktop.
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
// RPN calculator | |
import std.algorithm, std.container.array, | |
std.conv, std.stdio, std.meta; | |
void main() | |
{ | |
Array!int stack; | |
void binop(string op)() | |
{ | |
stack[$ - 2] = mixin("stack[$ - 2] " ~ | |
op ~ " stack[$ - 1]"); | |
stack.removeBack(); | |
writeln(stack[$ - 1]); | |
} | |
void process(in char[] token) | |
{ | |
alias Ops = AliasSeq!("+", "-", "*", "/", "%"); | |
Lswitch: | |
switch (token) | |
{ | |
foreach (op; Ops) | |
{ | |
case op: | |
binop!op(); | |
break Lswitch; | |
} | |
case "=": | |
writeln(stack[$ - 1]); | |
stack.removeBack(); | |
break; | |
default: | |
stack.insertBack(token.to!int); | |
break; | |
} | |
} | |
stdin.byLine.map!splitter.joiner.each!process; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment