From 4dd4e596ae6e5da84f2ce4296cbf9c030efe69bb Mon Sep 17 00:00:00 2001 From: Matthew Hall Date: Fri, 2 Sep 2022 21:03:42 +1200 Subject: Can read code from STDIN, can parse brainfuck --- src/brainfuck/parser.cr | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) (limited to 'src/brainfuck/parser.cr') diff --git a/src/brainfuck/parser.cr b/src/brainfuck/parser.cr index b937f73..df381e3 100644 --- a/src/brainfuck/parser.cr +++ b/src/brainfuck/parser.cr @@ -1,5 +1,33 @@ +require "./brainfuck.cr" +require "../util.cr" + class Brainfuck::Parser - def initialize() + def initialize(code_io : IO) + @raw_code = code_io.gets_to_end + code_io.close # just in case + end + + def parse + loop_depth = 0 + code = [] of Opcode + @raw_code.chars.each do |c| + op = Brainfuck.opcode_from_char(c) + unless op.nil? + if loop_depth == 0 && op == Opcode::LoopEnd + raise Util::ParserError.new("Missing loop entrance(s)") + end + + loop_depth -= 1 if op == Opcode::LoopEnd + loop_depth += 1 if op == Opcode::LoopStart + code << op + end + end + + unless loop_depth == 0 + raise Util::ParserError.new("Missing loop exit(s)") + end + + return code end end -- cgit v1.2.1