diff options
| author | Matthew Hall <hallmatthew314@gmail.com> | 2022-09-02 21:03:42 +1200 |
|---|---|---|
| committer | Matthew Hall <hallmatthew314@gmail.com> | 2022-09-02 21:03:42 +1200 |
| commit | 4dd4e596ae6e5da84f2ce4296cbf9c030efe69bb (patch) | |
| tree | 401bf737a26c3cac4a32cf4862180d6288f9c010 /src/flint.cr | |
| parent | dd2bcbdadf2068723bdd88729d295171b0291cd2 (diff) | |
Can read code from STDIN, can parse brainfuck
Diffstat (limited to 'src/flint.cr')
| -rw-r--r-- | src/flint.cr | 31 |
1 files changed, 25 insertions, 6 deletions
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 |
