This question evaluates parsing of a simple message schema, construction of mappings from field names to primitive types and byte sizes, and computation of aggregate sizes for composite message types.
You are given a text definition of a message schema and a table of primitive types with their byte sizes. Implement a parser and expose two query functions:
get_size(name)
: returns the size in bytes of either:
"int"
), or
get_type(field_name)
: returns the declared type of the given field.
The schema is provided as a multi-line string. Example:
Message:
field_name field_type
field_name2 field_type2
Message:
). Treat the message type/name as
"Message"
.
<field_name> <field_type>
.
<field_type>
is a primitive type listed in the primitives table.
You are given a map/dictionary like:
float -> 8
int -> 4
char -> 4
get_size("Message")
returns the total size of the message (sum of sizes of each field’s type).
get_size(field_name)
returns the size of that field’s type.
get_type(field_name)
returns the type string for that field.
get_size(primitive_type)
returns the size from the primitives table.
-1
/
null
), but be consistent.
Given schema:
Message:
price float
qty int
and primitives {float: 8, int: 4}:
get_type("price") -> "float"
get_size("price") -> 8
get_size("Message") -> 12
get_size("int") -> 4
Implement the parser and these two query functions.