summaryrefslogtreecommitdiff
path: root/src/flint.cr
blob: 89f1d3f8235fe351ce691f060e123b4f49ba8fa4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# TODO: Write documentation for `Flint`

require "option_parser"

require "./util.cr"
require "./brainfuck/*"
require "./thue/*"

module Flint
  VERSION = "0.1.1"

  def self.main
    execution_mode = :interpret
    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]"

      parser.on("brainfuck", "select brainfuck as the language") do
        chosen_language = :brainfuck
        memory_size = Brainfuck::DEFAULT_TAPE_SIZE

        parser.banner = "Usage: flint brainfuck [OPTIONS] [FILE]"
        parser.on("-t CELLS", "--tape-size=CELLS", "specify the number of memory cells in the tape, defaults to 30,000") do |_cells|
          memory_size = _cells.to_i
        end
      end

      parser.on("thue", "select Thue as the language") do
        chosen_language = :thue

        parser.banner = "Usage: flint thue [OPTIONS] [FILE]"
        # no thue-specific options yet
      end

      parser.on("-h", "--help", "show this help and exit") do
        puts parser
        exit(0)
      end
      parser.on("-i", "--interpet", "interpet the provided source code, implied by default, overridden by '-c' or '--compile'") { nil }
      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?
      end
    end

    parser.parse

    if read_stdin
      source_io = STDIN
    elsif !source_file.nil?
      # compilier complains without the not_nil!
      source_io = File.new(source_file.not_nil!)
    else
      puts "ERROR: No source file chosen"
      puts parser
      exit(1)
    end

    case chosen_language
    when :brainfuck
      bf_parser = Brainfuck::Parser.new(source_io)
      program = Brainfuck::Program.new(bf_parser, memory_size)
    when :thue
      thue_parser = Thue::Parser.new(source_io)
      program = Thue::Program.new(thue_parser)
    else
      puts "ERROR: No language chosen"
      puts parser
      exit(1)
    end

    begin
      p = program.not_nil!
      case execution_mode
      when :interpret
        p.interpret
      when :compile
        p.compile
      end
    rescue ex : Util::ParserError
      puts "Caught ParserError"
      puts ex.message
      exit(1)
    rescue ex : Util::InterpreterError
      puts "Error encountered while interpreting:"
      puts ex.message
      exit(1)
    rescue ex : Util::CompilerError
      puts "Failed to compile program:"
      puts ex.message
      exit(1)
    end
  end
end

Flint.main