diff options
| author | Matthew Hall <hallmatthew314@gmail.com> | 2022-09-08 20:12:09 +1200 |
|---|---|---|
| committer | Matthew Hall <hallmatthew314@gmail.com> | 2022-09-08 20:12:09 +1200 |
| commit | ed741d3235841cd373dfa1cc5ddcf61806d3e5b7 (patch) | |
| tree | fdd6fb0f71e1a6c2e5522113e2485ef97fd89d27 /src/among_us | |
| parent | 97e3b4a9d7e995848341e53b2c002930c85d5c47 (diff) | |
Among Us basic implementation, not well-tested
Diffstat (limited to 'src/among_us')
| -rw-r--r-- | src/among_us/program.cr | 82 |
1 files changed, 80 insertions, 2 deletions
diff --git a/src/among_us/program.cr b/src/among_us/program.cr index a54f24b..cd7d6fc 100644 --- a/src/among_us/program.cr +++ b/src/among_us/program.cr @@ -1,17 +1,95 @@ require "big" +require "random" + +require "../util.cr" struct AmongUs::Program < Flint::Program def interpret : Nil + rand = Random.new + code = Parser.new(@source_io).parse jumps = find_jumps(code) + code_ptr = 0 stack = [] of BigInt acc1 = BigInt.new acc2 = BigInt.new color = code.find &.is_a?(Color) - puts code - puts jumps + while code_ptr < code.size + case code[code_ptr] + in Color::RED + color = Color::RED + in Color::BLUE + color = Color::BLUE + in Color::PURPLE + color = Color::PURPLE + in Color::GREEN + color = Color::GREEN + in Color::YELLOW + color = Color::YELLOW + in Color::CYAN + color = Color::CYAN + in Color::BLACK + color = Color::BLACK + in Color::WHITE + color = Color::WHITE + in Color::BROWN + color = Color::BROWN + in Color::LIME + color = Color::LIME + in Color::PINK + color = Color::PINK + in Color::ORANGE + color = Color::ORANGE + + in Command::SUS + case color + when Color::RED + acc1 += 1 + when Color::BLUE + stack << BigInt.new(acc1) + when Color::PURPLE + stack.pop unless stack.empty? + when Color::GREEN + print (stack.pop % 256).to_u8.chr unless stack.empty? + when Color::YELLOW + c = STDIN.read_char + stack << BigInt.new(c.ord) unless c.nil? + when Color::CYAN + r = rand.rand(acc1 + 1) + r.times do + break if stack.empty? + stack.pop + end + when Color::BLACK + print stack.pop + when Color::WHITE + acc1 -= 1 + when Color::BROWN + acc1 = stack.pop + when Color::LIME + stack[-1] *= 2 + when Color::PINK + acc1 = 0 + when Color::ORANGE + acc1 += 10 + when nil + raise Util::InterpreterError.new("Tried to use SUS command with no color.") + end + in Command::VENTED + acc2 += 10 + in Command::SUSSY + acc2 -= 1 + in Command::ELECTRICAL + acc2 = 0 + in Command::WHO + code_ptr = jumps[code_ptr] if !stack.empty? && stack[-1] % 256 == acc2 % 256 + in Command::WHERE + code_ptr = jumps[code_ptr] if !stack.empty? && stack[-1] % 256 != acc2 % 256 + end + code_ptr += 1 + end end private def find_jumps(code : Array(Instruction)) : Hash(Int32, Int32) |
