Module: LexerKit::CLI::Commands::Disasm

Defined in:
lib/lexer_kit/cli/commands.rb

Overview

Disassemble a .lkt1 file

Class Method Summary collapse

Class Method Details

.run(argv) ⇒ Object



273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
# File 'lib/lexer_kit/cli/commands.rb', line 273

def self.run(argv)
  options = { dfa: nil, jump: nil, keyword: nil }

  parser = OptionParser.new do |opts|
    opts.banner = "Usage: lexer_kit disasm [options] <file.lkt1|file.lkb1>"

    opts.on("--dfa INDEX", Integer, "Show DFA table at INDEX") do |v|
      options[:dfa] = v
    end

    opts.on("--jump INDEX", Integer, "Show jump table at INDEX") do |v|
      options[:jump] = v
    end

    opts.on("--keyword INDEX", Integer, "Show keyword table at INDEX") do |v|
      options[:keyword] = v
    end

    opts.on("-h", "--help", "Show this help") do
      puts opts
      return 0
    end
  end

  parser.parse!(argv)

  if argv.empty?
    warn "error: No input file specified"
    warn parser.banner
    return 1
  end

  path = argv.shift
  program = CLI.load_lexer(path)

  require "lexer_kit/debug"

  if options[:dfa]
    dfa = program.dfa_tables[options[:dfa]]
    raise ArgumentError, "DFA table #{options[:dfa]} not found" unless dfa

    puts Debug::Visualizer.format_dfa(dfa, program: program)
  elsif options[:jump]
    table = program.jump_tables[options[:jump]]
    raise ArgumentError, "Jump table #{options[:jump]} not found" unless table

    puts Debug::Visualizer.format_jump_table(table)
  elsif options[:keyword]
    table = program.keyword_tables[options[:keyword]]
    raise ArgumentError, "Keyword table #{options[:keyword]} not found" unless table

    puts Debug::Visualizer.format_keyword_table(table, program: program)
  else
    puts Debug::Disassembler.new(program).disassemble
  end

  0
end