flat assembler
Message board for the users of flat assembler.

Index > Macroinstructions > macros params: parsing brackets recursively?

Author
Thread Post new topic Reply to topic
Devel99



Joined: 26 Oct 2015
Posts: 30
Devel99
Smile maybe someone has seen that on the web, is it possible?
I can surf expression as top-to-down parser but there's no reliable condition to detect that OP enentually has no brackets and represented by binar math operation

I want MATH recursion to stop when OP reaches binary math operation like "smth +" "smth+smth" "+ smth". Is that possible?

thanks

Code:
macro mk_MATH
{
  macro MATH op
  \{
    mk_MATH

    match (b), op
    \\{
      MATH b
    \\}
    match a(b), op
    \\{
      MATH b
    \\}
    match (b)c, op
    \\{
      MATH b
    \\}
    match a(b)c, op
    \\{
      MATH b
    \\}

    ;  match x * y, op
    ;  \\{
    ;  \\}
    ;
    ;  match x / y, op
    ;  \\{
    ;  \\}
    ;
    ;  match x + y, op
    ;  \\{
    ;  \\}
    ;
    ;  match x - y, op
    ;  \\{
    ;  \\}

    purge MATH
  \}
}
mk_MATH

MATH (1 + ((2 * 5) - 4))

    
Post 08 Dec 2015, 23:27
View user's profile Send private message Reply with quote
l_inc



Joined: 23 Oct 2009
Posts: 881
l_inc
Devel99
Do you mean something like an elsematch?
Code:
macro mk_MATH
{
  macro MATH op
  \{
    mk_MATH

    match (b), op
    \\{
      MATH b
      rept 0 \\\{
    \\}
    match, \\{ match a(b), op
    \\\{
      MATH b
      MATH a r
      restore r
      rept 0 \\\\{
    \\\}
    match, \\\{ match (b)c, op
    \\\\{
      MATH b
      MATH r c
      restore r
      rept 0 \\\\\{
    \\\\}
    match, \\\\{ match a(b)c, op
    \\\\\{
      ;MATH b
      ;not sure what this case is about
      match \\\\\\{ PREPROCESSING ERROR \\\\\\}
      rept 0 \\\\\\{
    \\\\\}
    match, \\\\\{
      match x * y, op \\\\\\{ rept 1 t:x*y
      \\\\\\\{
        define r t
        display \\\\\\\`x,'*',\\\\\\\`y,'=',\\\\\\\`t,13,10
      \\\\\\\} \\\\\\}
      match x / y, op \\\\\\{ rept 1 t:x/y
      \\\\\\\{
        define r t
        display \\\\\\\`x,'/',\\\\\\\`y,'=',\\\\\\\`t,13,10
      \\\\\\\} \\\\\\}
      match x + y, op \\\\\\{ rept 1 t:x+y
      \\\\\\\{
        define r t
        display \\\\\\\`x,'+',\\\\\\\`y,'=',\\\\\\\`t,13,10
      \\\\\\\} \\\\\\}
      match x - y, op \\\\\\{ rept 1 t:x-y
      \\\\\\\{
        define r t
        display \\\\\\\`x,'-',\\\\\\\`y,'=',\\\\\\\`t,13,10
      \\\\\\\} \\\\\\}
    \\\\\}\\\\}\\\}\\}

    purge MATH
  \}
}
mk_MATH

MATH (1 + ((2 * 5) - 4))    


Pure preprocessor solution might be fun, but for something more practical I'd recommend you to go the way described here (irps + virtual + load).

_________________
Faith is a superposition of knowledge and fallacy
Post 09 Dec 2015, 00:18
View user's profile Send private message Reply with quote
Devel99



Joined: 26 Oct 2015
Posts: 30
Devel99
l_inc Wise ideas =) thanks much. Bookmarked that. But it turns out that prep fails on the following expression:
MATH ([eax + 6] + [ebx - 8])
Straight matching with PLUS wildcard gives me "[eax" and "6] + [ebx - 8]" and extra workaround required
so I moved back to previously implemented MATH <<[eax + 6], +, [ebx - 8]>, *, [ecx + 7]>
at least that worked and produced something like this
Code:
mcr_LDARG [eax + 6]
mcr_LDARG [ebx - 8]
mcr_ADD
mcr_LDARG [ecx + 7]
mcr_MULL
mcr_STFLD ...
    

thank you for the cool ideas I need to think of that a bit more Smile
Post 09 Dec 2015, 14:01
View user's profile Send private message Reply with quote
l_inc



Joined: 23 Oct 2009
Posts: 881
l_inc
Devel99
Square brackets is an operator (of dereference) same as "+","-","*","/", but having the highest precedence among these. So providing support for it is not a workaround. It's a part of the design. You just need to process the operators in form of an "elsematch" (to achieve mutually exclusive processing) starting with match [a], op . And I suppose, you won't need excessive parentheses.

_________________
Faith is a superposition of knowledge and fallacy
Post 09 Dec 2015, 14:47
View user's profile Send private message 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-2020, Tomasz Grysztar. Also on GitHub, YouTube, Twitter.

Website powered by rwasa.