Advanced Computing Environment
Hosted by SourceForge
brix-os project page

Previous: Unions ----- Up: Contents ----- Next: Booleans


show notes
Features that need to be added to the page or ideas that haven't been thought out.
  • types can mask themselves as virtual subtypes with different interfaces. the ~ keyword at beginning of Enum disables any interfaces that may have been inherited. Boolean inherits a single group Enum without relational_interface and then implements its own AND, OR, XOR and NOT operators
  • use commas to separate states and something like | to split groups?
    	Enum(a, b, c | x = 24, y, z)
    	Enum(@2 a,b,c | @14 x,y,z)
    or replace commas with a |?
    	Enum(a b c | x=24 y z)
    	Enum(a b c @2 | x y z @14)
    should @ be located at the start or end of the group?
  • need an extension to return the string value of a single or multiple group Enum.
  • need ability to iterate over each group and each state within a group. this would happen on the type and not the object.
  • allow variables as positional values? the Enum is evaluated inside an isolated scope preventing access to outside identifiers, using variables would require the $ prefix operator.

Enum is a powerful symbolic type using flags to represent multiple states and even groups of states. The left-most symbol in each group is the default state and defaults to a zero value. The empty flag represents an anonymous placeholder state and may be used to initialize the object to its default states. The value of each state increments and resets to zero at the beginning of each group and each group is located in its own block of bits large enough to represent all possible states in the group.

The value of each state may be altered with the = operator and the starting bit of groups altered with the @ operator. Both operators use the numeric literal to their right which must be greater than the incremental value they would have been given.

The flag tick (`) is required outside the Enum but is optional inside unless the flag contains a hyphen and then it is required for proper lexing. Expressions that set or use the enumeration flag values must be terminated with a semicolon or wrapped in parentheses. This is due to the fact that flags are automatically attached to the right-most token by the parser, and this would cause the expression to continue into the next expression.

	// bit-fields
	states:Enum(`power-off `power-on, left right, up down) = `;
	states = `power-on `down;
	// reposition second field
	Enum(a b c d, ` x y z @ 14)

	// start `a at 1 instead of 0, start `x at 24
	Enum(a = 1 b c x = 24 y z)
The insantiated object has a single value slot holding an unsigned Number large enough to represent all states and groups. This value slot will be mutable if the ! keyword is used at the beginning of the Enum. This however could lead to inconsistent states if the Enum has unused gaps.
	f:Enum(! a b c d) = `a;
	f.value = 3 // set to `d
	f.value = 4 // compile-time error, 2-bit value

	f:Enum(! a b c d e) = `a;
	f.value = 5 // undefined state, 3-bit value
Group names may also be used to extract individual states from multi-state Enums. Virtual slots are created for each group and receive their own custom type but the value slot prevents any groups from being named `value. The group name may not contain hyphens since slot names don't allow them.
	// flags become `group-symbol
	eflags:Enum(iopl : dpl0 dpl1 dpl2 dpl3 @ 12) = `iopl-dpl1; // 01_0000_0000_0000b

	// iopl has a type of Enum(dpl0 dpl1 dpl2 dpl3)
	iopl := eflags.iopl // `dpl1
	echo iopl.value; // displays 1






Two Enum objects can only be compared through their value slots and the operators available from the Number type. The following operators compare an Enum object to a flag and should always be wrapped with parentheses to keep the parser from binding the flags to the token after the comparison.

	// compare one or more states
	(states == `state1 `state2)

	// compare single state
	(states != `state1)
	(states > `state1)
	(states < `state1)
	(states >= `state1)
	(states <= `state1)

Previous: Unions ----- Up: Contents ----- Next: Booleans