This wiki is static and therefore read-only as of August 2011. More information here.
Hosted by NexuizNinjaz.com

This is an old revision of the document!


Sandbox


Use this page to test the syntax.

8/23/05 12:55

dev:quakec


Because dokuwiki has NO TALK PAGE and I didn't want to edit quakec article directly:

  • float .field is the same as float *pointer_to_float in c, while self.field is self→field in c, where self is structure and field is its member

Therefore, doing this:

.float field1;
.float field2;
.float field3;
.float field4;
field1 = field2;  // points to the same float
self.field1 = 8;  // self.field2 is also 8
self.field3 = 66; 
self.field4 = self.field2; // self.field4 is 8
self.field4 = self.field3; // self.field4 is 66, self.field3 is 66, self.field2 and self.field1 have values of 8


it is not clear whether the space for float pointed from field1 was allocated during pointer declaration or not and whether quakecVM does some sort of garbage collecting. If not, it should be mentioned that programming like previous example is bad habit cause it will leak memory;

  • it should be mentioned that startpos should be 0 in substring if you want to search whole string and that generally strings are indexed from 0 upwards (no negative length, etc...);
  • can't see the difference between n fields and field of array type, which has n elements (both are accessed the same e.(array[index])) If there is difference, an explanation would be useful;
  • pointers to functions chapter is confusing. Which way is the general way? Separate var keyword explanation could be useful. As well, that original quakec didn't have neither typedef nor var and that fteqcc has a const keyword to separate variables from constants.


---

There is no such thing as “allocation” of fields. They are fixed, and “allocated” by the compiler.

Substring - add that if you want, but this page is not meant to explain all the functions. This should be in the extensions.qh file.

.float field1 declares NO VARIABLE, if used globally, but a field allocated by the compiler. “field1 = field2” should be an error, and if not, that's another bug of the compiler to fix.

var .float field1 would declare what you have used. And there is a separate var keyword explanation, explaining that the keyword is used to force it to be a variable declaration if it would be ambiguous.

And as for const - yes, that is obvious, but the Nexuiz code base does not use it (too many original QuakeC coders...).

As for the arrays - same difference as with the fields. You can assign a single array field to another field variable, as the example shows - but you can NOT have a variable of type .float[16].

---

Whether malloc(32) is called during declaration (float a;) or initialization (a = 3;) inside VM.
* There are only two ways to cause ANY malloc by the VM: spawn() (creating a new entity) and strzone() (allocating a string in permanent storage). Fields are ALWAYS allocated by the compiler. -- div0

.float myfield;
//code
var .float myfieldvar;
myfieldvar = myfield; //this is from fields chapter (i assume that this is the same as float *p1; float *p2; p1 = p2;)
e.myfieldvar = 42;


In the above case, it is not clear why var keyword is required at all. Are fields without var keyword not variables? I define variable as the thing which value I can change (if this is the case of misunderstanding).
* Fields without var keyword are NOT variables. They point to preallocated storage by the compiler (which, basically, adds to the size of the entity struct and is allocated per-entity when spawn() is used). -- div0

I can NOT have a variable of .float array [16], but I CAN have var .float myfloatfields[MAX_FLOATFIELDS]??? According to what was written, var keyword is only used when declaration otherwise would be ambiguous. Where is ambiguity in .float a or .float a[16] that they require preceding var keyword?
* .float a; declares a field “a” of type “float”, and expands the entity struct by 4 bytes to store the float (internally, the compiler does that by preinitializing .float a to some index, and increasing the field count by 1 so the engine knows how much spawn() has to allocate.
* var .float a;
declares a VARIABLE of field type ”.float”, so you can assign a .float to it. It does NOT preallocate a field!

  • Imo, strings chapter and if not chapter are self-contradictory:
"" is the only string that evaluates to false in an if expression //from string chapter
as "" counts as true in an if expression //from if not chapter
  • I'll fix that. string_null evaluates to false, ”” does not.
  • Isn't it array out-of-bounds?
#define MAX_FLOATFIELDS 3
var .float myfloatfields[MAX_FLOATFIELDS];
myfloatfields[3] = health; // array elements are 0, 1, 2.
  • Fixed.


---
Now it is almost clear.

I can fix strings myself, if you don't have time.

Another thing about float is:

1 sign bit and 23 bit mantissa can represent a:

  • signed number, which max/min size is +2^23-1/-2^23 or 0 11111111111111111111111/ 1 00000000000000000000000 (8388607/-8388608) or
  • unsigned number, which maximum size is (as you said) 1111111111111111111111111 (16777216).

--
I did that mistake at first too. However, notice that the first 1 of the mantissa is implicit.

So representable integers are:
- zero
- with exponent 0: 1 = (exponent 0, mantissa 00000000000000000000000)
- with exponent 1: 2, 3 = (exponent 1, mantissa x0000000000000000000000 where x stands for 0 or 1)
- with exponent 2: 4..7 = (exponent 2, mantissa xx000000000000000000000 where x stands for 0 or 1)
...
- with exponent 23: 8388607..16777216 = (exponent 23, mantissa xxxxxxxxxxxxxxxxxxxxxxx where x stands for 0 or 1)
All this works independent of the sign, so totally representable are -16777216 to 16777216.

 
playground/playground.1225477219.txt.gz · Last modified: 2008/10/31 19:20 by 84.58.41.240
Nexuiz Ninjaz Recent changes RSS feed Creative Commons License Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki
GFDL logoGFDLcontent Unless mentioned on the licensing page, the work on this page is licensed under the GNU Free Documentation License. The author states that the text and images can be used within the restrictions of this license (for example, they can be incorporated into certain free encyclopedias such as Wikipedia).
Kindly hosted by NexuizNinjaz.com