PDA

View Full Version : Use characters as longs



stevenmess2004
01-08-2008, 02:06 PM
Is it possible to use a series of byte as a long? I have tried it but with no luck so far.

If I have a prototype
PUB callObject(objectName,methodName,arg)

is there some way of doing this
return callObject("test","run",0)

I can just use numbers but if I can use words it will mean less mistakes.

Does anyone have any ideas?

Steven

Phil Pilgrim (PhiPi)
01-08-2008, 02:13 PM
You can, but it's awkward:




a := constant("t" | "e" << 8 | "s" << 16 | "t" << 24)




There really isn't a simple, natural-looking constructor in Spin for concatenating bytes into a long. As an alternative you could use string, but that creates an additional level of indirection that you may not want.

-Phil

stevenmess2004
01-08-2008, 02:17 PM
In that case it would be simpler to just use numbers. Could use hex though. It would be better than nothing.

Steven

Phil Pilgrim (PhiPi)
01-08-2008, 02:21 PM
More expressive would be to use defined constants, viz:




CON
test = 0
run = 1

...

return callObject(test, run, 0)




-Phil

stevenmess2004
01-08-2008, 02:31 PM
True. I was thinking about using it for DOL. Because the objects will be compiled separately they will have to be declared in every object that wants to call other objects. But that's just a copy and paste so its not that hard.

Thanks Phil

Steven

Phil Pilgrim (PhiPi)
01-08-2008, 02:40 PM
You can access another object's defined constants using the # accessor: e.g. myObject#test. It's a little wordy if you use long names in your OBJ section, but it keeps objects and the programs that use them in sync, in case of changes.

-Phil

stevenmess2004
01-08-2008, 02:43 PM
Yes, but only if that other object is declared in an 'OBJ' section. DOL will allow you to load objects at runtime. When you compile the objects you can call other objects that have dynamically loaded at runtime and so are not in the 'OBJ' section.

Steven

Paul Baker
01-08-2008, 02:47 PM
You can set up an enumeration:

CON
#0, test, run, thing1, thing2

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Paul Baker (mailto:pbaker@parallax.com)
Propeller Applications Engineer
[/url][url=http://www.parallax.com] (http://www.parallax.com)
Parallax, Inc. (http://www.parallax.com)

Phil Pilgrim (PhiPi)
01-08-2008, 03:52 PM
Steven,

With the DOL, you could still create otherwise-null objects that just have constants in them and declare those objects in both caller and callee programs. Referring to a single definition file is a Good Thing that eliminates all kinds of maintenance headaches. Granted, the objectname#const notation is a bit wordy.

-Phil

CardboardGuru
01-08-2008, 03:59 PM
Phil Pilgrim (PhiPi) said...
Granted, the objectname#const notation is a bit wordy.
-Phil


Less so if you set such constant files to be referred to by a single letter such as "c" or "g" in the OBJ statement.

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Help to build the Propeller wiki - propeller.wikispaces.com (http://propeller.wikispaces.com)
Play Defender - Propeller version of the classic game (http://forums.parallax.com/showthread.php?p=685888)
Prop Room Robotics - my web store for Roomba spare parts (http://www.proproomrobotics.co.uk) in the UK

deSilva
01-08-2008, 04:15 PM
I should definitely use constants from a referenced Object.
Whatever DOL does, constants are resolved at compile time, there is no overhead.

When you use "ABC" as LONGs or WORDs you are in the bad habbit of some languages of the "everything is a string" kind, which is acceptable for GB memories but not for the Propeller.

Note that that the LONG containing "ABC" has to be alloctated somewhere; the storrage situation will be equivalent to:



DAT
cABC LONG BYTE "ABC"

Nick Mueller
01-08-2008, 05:13 PM
I have the impression that what Steven wants, are something multi-byte char (constants).
In C, they look like this int32 = "ABcd". They only occupy 4 (2) bytes (no terminating zero). They are handy for signatures.

That doesn't work in Spin.

Nick

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Never use force, just go for a bigger hammer!

The DIY Digital-Readout for mills, lathes etc.:
YADRO (http://www.yadro.de)

stevenmess2004
01-08-2008, 05:49 PM
Yes nick, that is what I want.

@Phil and CardboardGuru - I thought you had to have a method in a *.spin file? I'll check. If you don't then that's what I'll do.

Peter Verkaik
01-08-2008, 06:10 PM
Steven,
this works:
CON
name = "t"<<24 + "e"<<16 + "s"<<8 + "t"


and you then use it like
callObject(objectConstants#name,...)


regards peter

CardboardGuru
01-08-2008, 09:56 PM
stevenmess2004 said...
Yes nick, that is what I want.

@Phil and CardboardGuru - I thought you had to have a method in a *.spin file? I'll check. If you don't then that's what I'll do.


You do. So just end the file with:
PUB dummy

You don't have to call it.

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Help to build the Propeller wiki - propeller.wikispaces.com (http://propeller.wikispaces.com)
Play Defender - Propeller version of the classic game (http://forums.parallax.com/showthread.php?p=685888)
Prop Room Robotics - my web store for Roomba spare parts (http://www.proproomrobotics.co.uk) in the UK

deSilva
01-09-2008, 12:22 AM
I shall try to explain again.

It is very clear to most persons here (to some earlier, to some later) what stevenmess intends to do: He wants to have a "speaking" parametrization. This is quite common in "everything is a string"- languages, as TK, Perl, PHP,...

In SPIN the construct "ABC" does not denote a string (as there is no datastructor or "strings" in SPIN), rather it can be used in CONSTANT context as a specific preset.



string("ABC")


statically allocates "A", "B", "C", 0 to the memory and yields a pointer to it.



DAT
LONG BYTE "ABC"


also allocates memory (three bytes).

The main objective of stevenmess - to have "speaking parameters" - can not be obtained in this way, as "ABC" is just not a valid element as a parameter.

If it were, it would take also take the same amount of additional storage as in my second example.

Languages that allow strings and word constants sometimes distinguish this be using ".." for strings and '...' for alphanumeric contents.

So please use constants as recomended many times here!