kempis
Joined: 12 Jun 2008
Posts: 49
|
Hello:
I wrote an extended struct macro, it allows you to write code: struct SAMPLE_A
field1 dd ?
field2 dd ?
field3 dd ?
ends
struct SAMPLE_B
field1 dd ?
field2 dd ?
field3 SAMPLE_A
field4 SAMPLE_A
ends
;defining-data
data1 SAMPLE_B field4=<1,field3=2>,field1=3,4,<field2=5,6>
;it will set:
;[data1.field1]=3
;[data1.field2]=4
;[data1.field3.field1]=?
;[data1.field3.field2]=5
;[data1.field3.field3]=6
;[data1.field4.field1]=1
;[data1.field4.field2]=?
;[data1.field4.field3]=2
This is the code:
;advanced-struct
;but it's difficult to implement 'union'
;anyone have idea?
macro struct name*{
fields@struct equ name
match child parent,name\{
fields@struct equ child,fields@\#parent\}
irp type,b,u,w,d,p,q,t\{
macro d\#type[arg]\\{\\common\\local x
fields@struct equ fields@struct,x,d\#type,<arg>\\}
macro r\#type count*\\{\\local x
fields@struct equ fields@struct,x,d\#type,<(count) dup ?>\\}
struc d\#type[arg]\\{\\common fields@struct equ fields@struct,.,d\#type,<arg>\\}
struc r\#type count*\\{fields@struct equ fields@struct,.,d\#type,<(count) dup ?>\\}
\}
macro label NAME\{fields@struct equ fields@struct,NAME,:,\}
virtual at 0
}
macro ends{
purge db,du,dw,dd,dp,dq,dt,rb,ru,rw,rd,rp,rq,rt,label
restruc db,du,dw,dd,dp,dq,dt,rb,ru,rw,rd,rp,rq,rt
match name=,fields,fields@struct\{
fields@struct equ
define@struct name,fields
fields@\#name equ fields\}
}
fields@struct equ
macro define@struct name,[field,type,value]{
common
if $
display "Error: definition of struct ",`name
display "contains illegal instructions",0x0D,0x0A
"Error"
end if
match ,definedstruct@#name\{
display "Error: multiple definition of struct "
display `name,0x0D,0x0A
"Error"
\}
define definedstruct@#name
define else else
forward
name#.#field type value
sizeof.#name#.#field=$-name#.#field
match ,else\{
match BEFORE,beforefield@struct\\{
nextfield@#name#.\\#BEFORE equ field
beforefield@struct equ field
\\}
\}
match =else,else\{define else
beforefield@struct equ field
firstfield@#name equ field
\}
common
restore else,else
sizeof.#name=$
end virtual
struc name [arg]\{
\common
match ,fields@struct\\{
label .
parse@struct equ
bracket@struct equ
irps ARG,arg\\\{
define else else
match ==,ARG\\\\{define else
match ,bracket@struct\\\\\{
parse@struct equ parse@struct=,\\\\\}
match any,bracket@struct\\\\\{
parse@struct equ parse@struct=\\\\\}
\\\\}
match <,ARG\\\\{define else
parse@struct equ parse@struct<
bracket@struct equ bracket@struct,\\\\}
match >,ARG\\\\{define else
parse@struct equ parse@struct>
restore bracket@struct\\\\}
match =else,else\\\\{define else
parse@struct equ parse@struct ARG\\\\}
restore else,else
\\\}
forward
value@#name#.#field equ
common
match PARSE,parse@struct\\\{
;PARSE
make@struct name,PARSE
\\\}
forward
match VALUE,value@#name#.#field\\\{
.#field type VALUE
rb sizeof.#name#.#field-($-.#field)
\\\}
match ,value@#name#.#field\\\{
.#field type value
\\\}
common
\\}
match any,fields@struct\\{
fields@struct equ fields@struct,.,name,<arg>\\}
sizeof.\#.=sizeof.#name
\}
macro name[arg]\{
\common \local x
x name arg
\}
}
macro make@struct name,[value]{
common
currentfield@struct equ firstfield@#name
forward
define else else
match FIELD==,value\{define else
currentfield@struct equ FIELD
\}
match =else,else\{define else
match CURRENT,currentfield@struct\\{
match any,value@#name#.\\#CURRENT\\\{
display "Error: defining instance of struct "
display `name," contains illegal data",0x0D,0x0A
define else else
match =value@#name#.\\#CURRENT,any\\\\{define else
display "field exceed boundary",0x0D,0x0A
\\\\}
match =else,else\\\\{define else
display "Multiple definition of field "
display \\`CURRENT,0x0D,0x0A
\\\\}
restore else,else
"Error"
\\\}
match ,value@#name#.\\#CURRENT\\\{
value@#name#.\\#CURRENT equ value
currentfield@struct equ nextfield@#name#.\\#CURRENT
\\\}
\\}
restore else,else
\}
}
But it's still not complete. The union macro has not been implemented (It's so confusing). And I do'nt check for deeper field( does it still work?).
Maybe someone has some ideas about union macro etc...?
Or just check this macro if it works.
Last edited by kempis on 02 Jan 2009, 12:56; edited 3 times in total
|