From 327402db009d05f29748bd90efb86a4a9ab67500 Mon Sep 17 00:00:00 2001 From: Dominic Grimm Date: Sat, 18 Jun 2022 12:32:57 +0200 Subject: [PATCH] Add compile time debugger --- src/hence/assembler.cr | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/hence/assembler.cr b/src/hence/assembler.cr index 63c8f03..4eceeb7 100644 --- a/src/hence/assembler.cr +++ b/src/hence/assembler.cr @@ -1,3 +1,5 @@ +require "colorize" + module Hence module Assembler extend self @@ -48,6 +50,21 @@ module Hence end MACROS = { + "debug" => ->(data : Data, args : Array(Parser::AST::Arg)) do + args.each do |arg| + arg_s = arg.to_s.gsub(/\n/, "\\n") + + num = arg.resolve_as_number(data) + slice = arg.resolve(data) + + puts arg_s.colorize(:green) + puts " => #{"0x#{num.to_s(16).rjust(4, '0')}".colorize(:magenta)}" + puts " => #{num.colorize(:magenta)}" + puts " => [#{slice.map { |n| "0x#{n.to_s(16).rjust(2, '0')}".colorize(:magenta) }.join(", ")}]" + puts " => #{"\"#{String.new(slice).gsub(/\n/, "\\n")}\"".colorize(:light_green)}" + puts + end + end, "org" => ->(data : Data, args : Array(Parser::AST::Arg)) do data.offset = args[0].resolve_as_number(data) end,