From fb9fa4353c98c7ac744ba2744de60f984b209809 Mon Sep 17 00:00:00 2001 From: Dominic Grimm Date: Sat, 18 Jun 2022 13:02:52 +0200 Subject: [PATCH] Fixed constants not resolving on declaration --- src/hence/assembler.cr | 2 +- src/hence/parser.cr | 23 +++++++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/src/hence/assembler.cr b/src/hence/assembler.cr index 4eceeb7..7645d62 100644 --- a/src/hence/assembler.cr +++ b/src/hence/assembler.cr @@ -12,7 +12,7 @@ module Hence when Parser::AST::ConstantDeclarationNode raise "Constants can't be redefined" if data.constants[node.name]? - data.constants[node.name] = node.value + data.constants[node.name] = Parser::AST::BytesArg.new(node.value.resolve(data)) when Parser::AST::LabelNode raise "Labels can't be redefined" if data.constants[node.name]? diff --git a/src/hence/parser.cr b/src/hence/parser.cr index 39fed74..ee85e9e 100644 --- a/src/hence/parser.cr +++ b/src/hence/parser.cr @@ -213,6 +213,29 @@ module Hence end end + class BytesArg < Arg + property bytes + + def initialize(@bytes : Bytes) + end + + def resolve(_data : Assembler::Data) : Bytes + @bytes + end + + def resolve_as_number(_data : Assembler::Data) : UInt16 + if @bytes.size == 1 + @bytes[0].to_u16 + else + Utils.merge_uint16(@bytes[0], @bytes[1]) + end + end + + def to_s : String + "\"#{String.new(@bytes).gsub(/\n/, "\\n")}\"" + end + end + class StringArg < Arg property string