[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: enum/bitops patch
- From: Asko Kauppi <askok@...>
- Date: Sun, 1 Oct 2006 01:24:07 +0300
...continuing the monologue
Some sample on the usage of the enums -- to give a practical view:
Lua 5.1.1 Copyright (C) 1994-2006 Lua.org, PUC-Rio
> =enum
table: 0x5037c0
> a= enum.new(0x22)
> b= enum.new(0x11,"kuu")
a is an anonymous enum (family ""), b has family "kuu" (Lua in Finnish)
> =b
0x11
> =type(b)
enum kuu
> =a<b
stdin:1: attempt to compare two enum values
stack traceback:
stdin:1: in main chunk
[C]: ?
> c= enum.new(0x44,"kuu")
> =c(b)
false
That was a 'test' operations: binary and with a boolean return - handy.
> =c[b]
0
Binary and, but with numeric return value (0x11 & 0x44 = 0)
> =c("and",b)
0x0
Binary and, with enum (family "kuu") return value; thus 0x0 via
tostring.
> =c"not"
0xffffffbb
> =c"<<"
0x88
There's numerous call type operations, they all return another enum
of the same family. These _could_ be done using special (new) Lua
operations, but don't really (in my opinion) need to be. Also, +,-,*
etc. arithmetic operations are left undefined for enums. <=
comparisons are defined, within the family (arguable, but I can think
of C code that does such - left mainly for ease of porting
application code).
> =b<c
true
> =a("xor",a,a,a,a,a,a,a)
0x0
> =a("xor",a,a,a,a,a,a,a,a)
0x22
In case there's multiple parameters, they're handled during the same
call (of course). Also, concatenation is done at bytecode compile
time (I think), making an effective or:
> print( b..c )
0x55
Concatenation is rather important since it's often used for combining
flags.
> = a.value
34
> = a"not".value
4294967261
This is the way to get actual numeric values out of the enums
(tonumber should also work..)
> =tonumber(a)
34
> =tostring(a)
0x22