flat assembler
Message board for the users of flat assembler.

Index > Macroinstructions > How to split symbolic name into 2 parts?

Author
Thread Post new topic Reply to topic
Jin X



Joined: 06 Mar 2004
Posts: 133
Location: Russia
Jin X 30 Dec 2020, 08:47
How can I split symbolic name into 2 parts?

E.g. check if the 1st symbol is '=' then get remaining part (get 'name' in '=name').

Or get left and right parts around comma (get 'left' and 'right' in 'left,right').
Post 30 Dec 2020, 08:47
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20363
Location: In your JS exploiting you and your system
revolution 30 Dec 2020, 08:55
What do you mean by "symbolic name"?

Like this?
Code:
my_symbolic_name equ left,right    
Post 30 Dec 2020, 08:55
View user's profile Send private message Visit poster's website Reply with quote
Jin X



Joined: 06 Mar 2004
Posts: 133
Location: Russia
Jin X 30 Dec 2020, 09:53
Macro parameter. And 'equ' too Smile
Code:
macro check sym_name&
{
  ; ...
}

check left,right    
Post 30 Dec 2020, 09:53
View user's profile Send private message Reply with quote
Jin X



Joined: 06 Mar 2004
Posts: 133
Location: Russia
Jin X 30 Dec 2020, 09:57
I made myself Smile

Code:
macro x name&
{
  match left=,right, name \{
    display `left,10
    display `right,10
  \}
}

macro y name
{
  match ==rest, name \{
    display `rest,10
  \}
}

x left,right
y =name    
Post 30 Dec 2020, 09:57
View user's profile Send private message Reply with quote
Jin X



Joined: 06 Mar 2004
Posts: 133
Location: Russia
Jin X 30 Dec 2020, 10:29
Oh, it does't work as I wish...
I see 'rest' instead of 'name'.

Actually I want to make something like this...

Code:
_ok equ ok
_left equ left
_right equ left
_some equ some

macro x el*&
{
  if <el> eq <_ok>
    display 'OK',10
  end if

  match l=,r, el \{
    display ',',10
    if <el> eq <_left,_right>
      display 'LEFT,RIGHT',10
    end if
  \}

  match ==r, el \{
    display '=',10
    if <r> eq <_some>
      display 'SOME',10
    end if
  \}
}

macro z list*&
{
  irps a, list \{
    x a
  \}
}

z ok =some left,right    

But I see only message 'OK' and nothing else.
Why???
Post 30 Dec 2020, 10:29
View user's profile Send private message Reply with quote
DimonSoft



Joined: 03 Mar 2010
Posts: 1228
Location: Belarus
DimonSoft 30 Dec 2020, 22:07
Jin X wrote:
Oh, it does't work as I wish...
I see 'rest' instead of 'name'.

Didn’t get what you try to achieve but from your complaint it seems like the ` operator should also be escaped to make it work one step later, when the rest symbol gets replaced with appropriate value.
Post 30 Dec 2020, 22:07
View user's profile Send private message Visit poster's website Reply with quote
Jin X



Joined: 06 Mar 2004
Posts: 133
Location: Russia
Jin X 31 Dec 2020, 08:53
The problem is that irps splits each semantic element regardless of spaces.
E.g.
Code:
irps a, ok =some left,right {
  display `a,10
}    
This will print
ok
=
some
left
,
right


but I want to see:
ok
=some
left,right


Is there a way to do this?
Post 31 Dec 2020, 08:53
View user's profile Send private message Reply with quote
DimonSoft



Joined: 03 Mar 2010
Posts: 1228
Location: Belarus
DimonSoft 31 Dec 2020, 09:16
You’ll have to build some kind of state machine then: whenever you get yet another token you analyze what you see and change the state accordingly, and sometimes being in a particular state and receiving particular token means you finally have to perform some action.

As an example, I once played with irps to parse key-value strings and got something like this (one level over-escaped since it’s taken out of a bigger project):
Code:
  macro unpack Dest*, Src&
  \{
    \local parsed, state, nested

    define state key
    nested equ 0
    key equ
    value equ
    irps item , Src
    \\{
      parsed equ 0
      match =0 =0 =key == , parsed nested state item
      \\\{
        define state value
        parsed equ 1
      \\\}
      match =0 =0 =key =, , parsed nested state item
      \\\{
        match k , key
        \\\\{
          restore Dest \# @ \\\\# k
          Dest \# @ \\\\# k equ
        \\\\}
        key equ
        parsed equ 1
      \\\}
      match =0 =0 =key any , parsed nested state item
      \\\{
        key equ key any
        parsed equ 1
      \\\}
      match =0 =value < , parsed state item
      \\\{
        value equ value <
        nested equ 1
        parsed equ 1
      \\\}
      match =0 =1 =value > , parsed nested state item
      \\\{
        restore nested
        value equ value >
        parsed equ 1
      \\\}
      match =0 =0 =value =, , parsed nested state item
      \\\{
        match k , key
        \\\\{
          restore Dest \# @ \\\\# k
          Dest \# @ \\\\# k equ value
        \\\\}
        define state key
        key equ
        value equ
        parsed equ 1
      \\\}
      match =0 =value any , parsed state item
      \\\{
        value equ value any
        parsed equ 1
      \\\}
      match =0 , parsed \\\{ 'Syntax error.' \\\}
    \\}

    match =key k , state key
    \\{
      restore Dest \# @ \\# k
      Dest \# @ \\# k equ
    \\}
    match =value k , state key
    \\{
      restore Dest \# @ \\# k
      Dest \# @ \\# k equ value
    \\}
  \}    
Intended use is along the lines of this:
Code:
    unpack Params, BS_OEMName      = 'MSWIN4.1',\
                   BPB_BytsPerSec  = 512,\
                   BPB_SecPerClus  = 1,\
                   BPB_RsvdSecCnt  = 1,\
                   BPB_NumFATs     = 2,\
                   BPB_HiddSec     = 0,\
                   BPB_TotSec32    = 0,\
                   BS_DrvNum       = $00,\
                   BS_Reserved1    = 0,\
                   BS_BootSig      = $29,\
                   BS_VolID        = ,\
                   BS_VolLab       = ,\
                   BS_FilSysType   = 'FAT12   ',\
                   tpl, defaults    
This piece of code should define a set of Params@-prefixed symbolic constants with their corresponding values. Should also support <this, kind = of, values> as a single value, but I finally didn’t use this capability at all.

Note also that these macros produce a lot of output for prepsrc which makes macro debugging harder and, I guess, increases memory requirements for building such projects.
Post 31 Dec 2020, 09:16
View user's profile Send private message Visit poster's website Reply with quote
Jin X



Joined: 06 Mar 2004
Posts: 133
Location: Russia
Jin X 31 Dec 2020, 09:52
As I see this will be the same:
unpack Params,one=1,two=2
and
unpack Params, one = 1 , two = 2

But I want to distinguish strings with and without spaces...
Post 31 Dec 2020, 09:52
View user's profile Send private message Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 8356
Location: Kraków, Poland
Tomasz Grysztar 31 Dec 2020, 11:42
Jin X wrote:
But I want to distinguish strings with and without spaces...
You would need fasmg for that - in fasm 1 the information about whitespace is lost during the tokenization.
Post 31 Dec 2020, 11:42
View user's profile Send private message Visit poster's website Reply with quote
DimonSoft



Joined: 03 Mar 2010
Posts: 1228
Location: Belarus
DimonSoft 31 Dec 2020, 19:33
Jin X wrote:
But I want to distinguish strings with and without spaces...

Obligatory question: why and what are you REALLY trying to achieve? I can think of something like auto-generating documentation from the Javadoc-like comments during the compilation of the program, but maybe your case is easier?
Post 31 Dec 2020, 19:33
View user's profile Send private message Visit poster's website Reply with quote
Jin X



Joined: 06 Mar 2004
Posts: 133
Location: Russia
Jin X 02 Jan 2021, 13:46
Simply put, I want to make a macro to transform mnemonics to instructions like this: calc pi 180 / 7.5 *
This should be transformed to:
Code:
fldpi
fild word [_180]
fdivp
fld dword [_7.5]
fmulp
; . . .
_180 dw 180
_7.5 dd 7.5    

The problem begins, e.g., when number is specified with signs: -1.23e-3 (this will be iterated by irps to -, 1.23e, -, 3). I can make state machine, of course, but it will be larger that could be.

So, if no way to do this quite easy, I'll think about doing this another way...

p.s. Happy new year! Wink


Last edited by Jin X on 02 Jan 2021, 13:51; edited 2 times in total
Post 02 Jan 2021, 13:46
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20363
Location: In your JS exploiting you and your system
revolution 02 Jan 2021, 13:50
Use the comma and irp.
Code:
macro do [stuff] {
 common
  irp thing, stuff \{
 ...

do pi,180,/,7.5,*    
Post 02 Jan 2021, 13:50
View user's profile Send private message Visit poster's website Reply with quote
Jin X



Joined: 06 Mar 2004
Posts: 133
Location: Russia
Jin X 02 Jan 2021, 13:52
Yes, I can use commas but it's not so beautiful...
I'll think about this too Smile
Post 02 Jan 2021, 13:52
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20363
Location: In your JS exploiting you and your system
revolution 02 Jan 2021, 13:57
Or use quotes:
Code:
irps thing, "-1.23e-3" -1.23e-3 {display "<",`thing,">",13,10}    
Post 02 Jan 2021, 13:57
View user's profile Send private message Visit poster's website Reply with quote
Jin X



Joined: 06 Mar 2004
Posts: 133
Location: Russia
Jin X 02 Jan 2021, 15:12
Good. Thanks! Smile
Post 02 Jan 2021, 15:12
View user's profile Send private message Reply with quote
Jin X



Joined: 06 Mar 2004
Posts: 133
Location: Russia
Jin X 02 Jan 2021, 15:15
But... it's a string.
dd thing will not work...
Post 02 Jan 2021, 15:15
View user's profile Send private message Reply with quote
DimonSoft



Joined: 03 Mar 2010
Posts: 1228
Location: Belarus
DimonSoft 03 Jan 2021, 12:19
My 5 cents.
1. State machine. Not really large, you’ll only need a couple of additional states to handle the FP case the rest is also pretty simple, so I’d give about ten states a most for parsing, maybe a few more for calculations if you want to have them separated.
2. Commas. Not quite aesthetically attractive. Easy to get messed when written by hand.
3. Strings. This might be interesting, but for me—only if the whole expression is a string. Which means parsing it by hand. Quite expensive since one should load data character-by-character and do even more bookkeeping. OTOH, makes it easier to be used with arbitrary data.
Post 03 Jan 2021, 12:19
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


Copyright © 1999-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.