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/flint.cr | 31 +++++++++++++++++++++++++------ 1 file changed, 25 insertions(+), 6 deletions(-) (limited to 'src/flint.cr') diff --git a/src/flint.cr b/src/flint.cr index 39d8363..1c67cc9 100644 --- a/src/flint.cr +++ b/src/flint.cr @@ -2,6 +2,7 @@ require "option_parser" +require "./util.cr" require "./brainfuck/*" require "./brainfuck/parser.cr" @@ -13,6 +14,7 @@ module Flint chosen_language = nil memory_size = nil source_file = nil + read_stdin = false parser = OptionParser.new do |parser| parser.banner = "Basic usage: flint [LANGUAGE] [OPTIONS] [FILE]" @@ -33,6 +35,9 @@ module Flint parser.on("-c", "--compile", "produce a binary or different source code file instead of interpeting the code, overrides an explicit '-i' or '--intepret' flag") do execution_mode = :compile end + parser.on("--stdin", "read source code from STDIN instead of a file") do + read_stdin = true + end parser.unknown_args do |_args| source_file = _args[0] unless _args.empty? @@ -47,17 +52,31 @@ module Flint exit(1) end - if source_file.nil? + if read_stdin + source_io = STDIN + elsif source_file + # compilier complains without the not_nil! + source_io = File.new(source_file.not_nil!) + else puts "ERROR: No source file chosen" puts parser + puts source_file.nil? + puts (!read_stdin) exit(1) end - # temporary - puts execution_mode - puts chosen_language - puts memory_size - puts source_file + if chosen_language == :brainfuck + bf_parser = Brainfuck::Parser.new(source_io) + program = Brainfuck::Program.new(bf_parser) + end + + begin + program.not_nil!.interpret + rescue ex : Util::ParserError + puts "Caught ParserError" + puts ex.message + exit(1) + end end end -- cgit v1.2.1