1
0
mirror of https://github.com/ksherlock/x65.git synced 2024-06-02 18:41:34 +00:00

STRUCT Directive fix

- Structs can contain empty lines
- Structs can be empty
- All structs have a size member named bytes, the number of bytes used
by a struct is <struct>.bytes
This commit is contained in:
Carl-Henrik Skårstedt 2016-01-04 20:55:32 -08:00
parent e3241c66aa
commit 1f50f07c24
2 changed files with 55 additions and 0 deletions

13
x65.cpp
View File

@ -3017,6 +3017,8 @@ StatusCode Asm::BuildStruct(strref name, strref declaration)
while (strref line = declaration.line()) {
line.trim_whitespace();
strref type = line.split_label();
if (!type)
continue;
line.skip_whitespace();
unsigned int type_hash = type.fnv1a();
unsigned short type_size = 0;
@ -3055,6 +3057,17 @@ StatusCode Asm::BuildStruct(strref name, strref declaration)
member_count++;
}
// add a trailing member of 0 bytes to access the size of the structure
{
struct MemberOffset bytes_member;
bytes_member.offset = size;
bytes_member.name = "bytes";
bytes_member.name_hash = bytes_member.name.fnv1a();
bytes_member.sub_struct = strref();
structMembers.push_back(bytes_member);
member_count++;
}
pStruct->numMembers = member_count;
pStruct->size = size;

42
x65.txt
View File

@ -662,6 +662,48 @@ Directives for String Symbols
Structs and Enums
-----------------
Hierarchical data structures (dot separated sub structures)
Structs helps define complex data types, there are two basic types to
define struct members, and as long as a struct is declared it can be
used as a member type of another struct.
The result of a struct is that each member is an offset from the start
of the data block in memory. Each substruct is referenced by separating
the struct names with dots.
To get the size of a struct simply use the automatic 'bytes' member as
in <struct>.bytes
Example:
struct MyStruct {
byte count
word pointer
}
struct TwoThings {
MyStruct thing_one
MyStruct thing_two
}
struct Mixed {
word banana
TwoThings things
}
Eval Mixed.bytes
Eval Mixed.things
Eval Mixed.things.thing_two
Eval Mixed.things.thing_two.pointer
Eval Mixed.things.thing_one.count
results in the output:
EVAL(15): "Mixed.bytes" = $3
EVAL(16): "Mixed.things" = $2
EVAL(27): "Mixed.things.thing_two" = $5
EVAL(28): "Mixed.things.thing_two.pointer" = $6
EVAL(29): "Mixed.things.thing_one.count" = $2
* ENUM - structs and enums, declare enumerations like C
* STRUCT - structs and enums, declare a C-like structure of symbols