flat assembler
Message board for the users of flat assembler.
 Home   FAQ   Search   Register 
 Profile   Log in to check your private messages   Log in 
flat assembler > Macroinstructions > [fasmg] Symbol is undefined or out of scope problem

Thread Post new topic Reply to topic

Joined: 12 Apr 2005
Posts: 489
Location: Belarus

[fasmg] Symbol is undefined or out of scope problem

Tomasz, please take a look at the small template below


macro datatypes types*&
  irp <ntype>, types
    macro type args:[?]&
      match [?], args
        emit n?
      else match [x], args
        emit nx dup ?
      else match [xvalueargs
        match =, vvalue
          emit nx dup ?v
          emit nx dup value
        end match
        irp argargs
          if arg eqtype ''
            emit narg
            local value
            value = +arg
            emit nvalue
          end if
        end irp
      end match
      type.__size = n
      datatype.type equ ::type::
    end macro
    struc (nametype args&
      label .n
      type args
      name.__size = n
      name.__length = $ - .
    end struc
  end irp
end macro

datatypes 1,byte?2,word?4,dword?6,fword?8,qword?10,tbyte?16,oword?

macro log.number num
  local ns
  n = +num
  if n = 0
    s = '0'
    s = 0
    while n > 0
      s = s shl 8 + (n mod 10 + '0')
      n = n / 10
    end while
  end if
  display string s,13,10
end macro

;------------ ^^ just helpers above to compile --------------

macro struct? structname
  macro end?.struct?!
      end namespace
      irp argargs
        match p:varg
          log.number .p.__size
        end match
      end irp
    end struc
    virtual at 0
      structname structname
      structname.__size = $
    end virtual
    purge end?.struct?
  end macro
    struc (namestructname args&
      label .structname.__size
      namespace .
      name.__size = structname.__size ;<<<<<<< problem line
end macro

struct POINT
  x word
  y byte
end struct

struct POINT2
end struct

my POINT2 z.x:1z:1

I'm trying to get the size of a variable defined as structure. Variable, not structure itself, because later it will be an array of structures.

The problem is when I add


name.__size = structname.__size

I start getting the following error:


Processed: label .: POINT.__size
Error: symbol '__size' is undefined or out of scope.

However, both sizes are logged correctly. If line is removed, label .: POINT.__size doesn't generate the error any more.

I thought that it could be processing order, and structname.__size is not initialized yet, when name.__size appears in code, but I guess it would then fail on that very line. Thanks in advance for your assistance!
Post 03 Apr 2017, 11:44
View user's profile Send private message Reply with quote
Tomasz Grysztar
Assembly Artist

Joined: 16 Jun 2003
Posts: 6741
Location: Kraków, Poland

The problem is caused by the case when both "name" and "structname" are equal to "POINT", because then

name.__size = structname.__size

gets evaluated to:

POINT.__size = POINT.__size

Because you then have "POINT.__size" defined with the actual value, this symbols becomes variable and can no longer be forward-referenced. That's why you get the "out of scope" error when trying to access the symbol before the first definition happens.

You can see that this is the problem when you put additional condition around the problematic line:

if `name <> `structname
      name.__size = structname.__size
end if

I would suggest to use ":=" instead of "=" to make sure that symbols can always be forward-referenced, but you still have to ensure that each of them is defined exactly once.
Post 03 Apr 2017, 12:44
View user's profile Send private message Visit poster's website Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  

< Last Thread | Next Thread >

Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum

Main index   Download   Documentation   Examples   Message board
Copyright © 2004-2018, Tomasz Grysztar.
Powered by rwasa.