Assigning value to reference does not work

I tried to make a reference to some memory per our previous discussion, but
the value is zero afterwards.

This code reveals the problem. “by” prints out the value 2. And
"!packetCount" prints out zero. Further debugging a loop that takes the
value !packetCount confirms it is zero.

Am I using incorrect syntax for “val _ = !packetCount = by”

%{^
const uint8_t packet[131];
const uint8_t *packetCount = packet;
const uint8_t *packetHeader = packet + 1;
const uint8_t *packetContents = packet + 2;

%}

macdef packet =
$extval(arrayref(uint8, 131),“packet”)

macdef packetCount =
$extval(ref(uint8),“packetCount”)

val by = serial_poll_for_byte(): uint8
val _ = print! by

val _ = !packetCount = by
val _ = print! (!packetCount)

I could not reproduce your error.

When I tried this example, I saw 28 being printed out.On Mon, Oct 12, 2015 at 11:57 PM, Mike Jones proc...@gmail.com wrote:

Ok, so val () = basically says the result must be void aka (), and not
ignored. Makes sense.

I have a similar problem passing a value used in an arrayref. In the code
below, if I do this:

crcLookupTable[n8(u8(4)]

it will print the value in the forth element of the array.

If I call pec_add(u8(4)), it prints 0. Basically, it seems not to pass
into the call, or the cast somehow turns a 4 into a 0.

I assume the arrayref is ok or hard coding u8(4) would fail.

What can make 4 become zero?

// SATS file

fun pec_add (old: uint8, new: uint8): uint8

// DATS file

extern fun{} n8 (x: uint8): natLt(256)
implement{} n8 (x) = cast{natLt(256)}(x) // Can’t go wrong because they
are the same size

%{^
const uint8_t crcLookupTable[256] PROGMEM = { 0, 7, 14, 9, 28, 27, 18, 21,
56, 63, 54, 49, 36, 35, 42, 45,
112, 119, 126, 121, 108, 107, 98,
101,…
};
%}

macdef crcLookupTable =
$extval(arrayref(uint8,256),“crcLookupTable”)

implement pec_add (old, new) = crcLookupTable[n8(new)]

// APP DATS File

val pec = pec_add(ZERO, u8(4))
val _ = print! pec

On Monday, October 12, 2015 at 8:36:04 PM UTC-6, gmhwxi wrote:

This example actually shows why it is good idea to write

val () = !a := b

instead of

val _ = !a := b

In the case where := is replaced with =, the former results in a
type-error
while the latter may be well-typed.

On Monday, October 12, 2015 at 9:45:55 PM UTC-4, Mike Jones wrote:

I tried to make a reference to some memory per our previous discussion,
but the value is zero afterwards.

This code reveals the problem. “by” prints out the value 2. And
“!packetCount” prints out zero. Further debugging a loop that takes the
value !packetCount confirms it is zero.
Am I using incorrect syntax for “val _ = !packetCount = by”

%{^
const uint8_t packet[131];
const uint8_t *packetCount = packet;
const uint8_t *packetHeader = packet + 1;
const uint8_t *packetContents = packet + 2;

%}

macdef packet =
$extval(arrayref(uint8, 131),“packet”)

macdef packetCount =
$extval(ref(uint8),“packetCount”)

val by = serial_poll_for_byte(): uint8
val _ = print! by

val _ = !packetCount = by
val _ = print! (!packetCount)


You received this message because you are subscribed to the Google Groups
“ats-lang-users” group.
To unsubscribe from this group and stop receiving emails from it, send an
email to ats-lang-user...@googlegroups.com.
To post to this group, send email to ats-lan...@googlegroups.com.
Visit this group at http://groups.google.com/group/ats-lang-users.
To view this discussion on the web visit
https://groups.google.com/d/msgid/ats-lang-users/f051fce6-bffe-476c-abef-b68995de0570%40googlegroups.com
https://groups.google.com/d/msgid/ats-lang-users/f051fce6-bffe-476c-abef-b68995de0570%40googlegroups.com?utm_medium=email&utm_source=footer
.

I pushed a copy of my arduino-ats to
https://github.com/Proclivis/arduino-ats.git. It does not have an app to
demonstrate the problem. It just contains the CRC functions in
DATS/i2c.dats which I use from my application.

I can work on a test cast tonight or tomorrow, but you are free to
reference it and try if you want. You can just take something like the
blinky example and hack it. My top of file for the app is:

#include “config.hats”
#include "{$TOP}/avr_prelude/kernel_staload.hats"
staload "prelude/SATS/bool.sats"
staload “prelude/SATS/unsafe.sats”

staload "{$TOP}/SATS/arduino.sats"
staload "{$TOP}/SATS/hardware_serial.sats"
staload “{$TOP}/SATS/i2c.sats”

Exposing crcLookupTable as arrayref brings a little bit of convenience but
it is
largely a bad idea in my opinion.

Here is a way to do it:

If needed, we could also introduce a type for progmem pointers but it seems
to be an overkill
for this particular problem.On Wednesday, October 14, 2015 at 8:13:13 PM UTC-4, Mike Jones wrote:

I have determined that using PROGMEM results in zero (failure) and without
PROGMEM results in working.

There is a site that describes the problem here progmem
http://www.nongnu.org/avr-libc/user-manual/pgmspace.html

Which says I need code like this:

byte = pgm_read_byte
http://www.nongnu.org/avr-libc/user-manual/group__avr__pgmspace.html#ga73084a8bbde259ffae72980354b3f027
(&(mydata[i][j]));

So now I have the problem of how to wrap an arrayref around something that
needs a macro.

Unwinding the macros:

#define pgm_read_byte( address_short) pgm_read_byte_near
http://www.nongnu.org/avr-libc/user-manual/group__avr__pgmspace.html#ga88d7dd4863f87530e1a34ece430a587c
(address_short)
#define pgm_read_byte_near( address_short) __LPM((uint16_t
http://www.nongnu.org/avr-libc/user-manual/group__avr__stdint.html#ga1f1825b69244eb3ad2c7165ddc99c956
)(address_short))

#define LPM_enhanced(addr)
(extension({
uint16_t __addr16 = (uint16_t)(addr);
uint8_t __result;
asm
(
“lpm %0, Z” “\n\t”
: “=r” (__result)
: “z” (__addr16)
);
__result;
}))

So it takes an assembly instruction “lpm” to get the data.

Based on my limited understanding:

abstype
arrayref_vt0ype_int_type
(a:vt@ype(elt), n:int(size)) = ptr
stadef arrayref = arrayref_vt0ype_int_type

This view type wraps around a ptr, so there is no way to get a special
assembly instruction involved.

Is there any similar problem solved in ATS that might be used as a model
for making an arrayrefavr that would be able to use the macro under the
hood?

On Wednesday, October 14, 2015 at 2:17:58 PM UTC-6, Mike Jones wrote:

By compiling a linux exe, this works. My unworking case is on Arduino.
I’ll dig deeper, try not to use PROGMEM, etc and see if I can find clues.

I have determined that using PROGMEM results in zero (failure) and without
PROGMEM results in working.

There is a site that describes the problem here progmem
http://www.nongnu.org/avr-libc/user-manual/pgmspace.html

Which says I need code like this:

byte = pgm_read_byte
http://www.nongnu.org/avr-libc/user-manual/group__avr__pgmspace.html#ga73084a8bbde259ffae72980354b3f027
(&(mydata[i][j]));

So now I have the problem of how to wrap an arrayref around something that
needs a macro.

Unwinding the macros:

#define pgm_read_byte( address_short) pgm_read_byte_near
http://www.nongnu.org/avr-libc/user-manual/group__avr__pgmspace.html#ga88d7dd4863f87530e1a34ece430a587c
(address_short)
#define pgm_read_byte_near( address_short) __LPM((uint16_t
http://www.nongnu.org/avr-libc/user-manual/group__avr__stdint.html#ga1f1825b69244eb3ad2c7165ddc99c956
)(address_short))

#define LPM_enhanced(addr)
(extension({
uint16_t __addr16 = (uint16_t)(addr);
uint8_t __result;
asm
(
“lpm %0, Z” “\n\t”
: “=r” (__result)
: “z” (__addr16)
);
__result;
}))

So it takes an assembly instruction “lpm” to get the data.

Based on my limited understanding:

abstype
arrayref_vt0ype_int_type
(a:vt@ype(elt), n:int(size)) = ptr
stadef arrayref = arrayref_vt0ype_int_type

This view type wraps around a ptr, so there is no way to get a special
assembly instruction involved.

Is there any similar problem solved in ATS that might be used as a model
for making an arrayrefavr that would be able to use the macro under the
hood?On Wednesday, October 14, 2015 at 2:17:58 PM UTC-6, Mike Jones wrote:

By compiling a linux exe, this works. My unworking case is on Arduino.
I’ll dig deeper, try not to use PROGMEM, etc and see if I can find clues.

Ok, so val () = basically says the result must be void aka (), and not
ignored. Makes sense.

I have a similar problem passing a value used in an arrayref. In the code
below, if I do this:

crcLookupTable[n8(u8(4)]

it will print the value in the forth element of the array.

If I call pec_add(u8(4)), it prints 0. Basically, it seems not to pass into
the call, or the cast somehow turns a 4 into a 0.

I assume the arrayref is ok or hard coding u8(4) would fail.

What can make 4 become zero?

// SATS file

fun pec_add (old: uint8, new: uint8): uint8

// DATS file

extern fun{} n8 (x: uint8): natLt(256)
implement{} n8 (x) = cast{natLt(256)}(x) // Can’t go wrong because they
are the same size

%{^
const uint8_t crcLookupTable[256] PROGMEM = { 0, 7, 14, 9, 28, 27, 18, 21,
56, 63, 54, 49, 36, 35, 42, 45,
112, 119, 126, 121, 108, 107, 98,
101,…
};
%}

macdef crcLookupTable =
$extval(arrayref(uint8,256),“crcLookupTable”)

implement pec_add (old, new) = crcLookupTable[n8(new)]

// APP DATS File

val pec = pec_add(ZERO, u8(4))
val _ = print! pecOn Monday, October 12, 2015 at 8:36:04 PM UTC-6, gmhwxi wrote:

This example actually shows why it is good idea to write

val () = !a := b

instead of

val _ = !a := b

In the case where := is replaced with =, the former results in a type-error
while the latter may be well-typed.

On Monday, October 12, 2015 at 9:45:55 PM UTC-4, Mike Jones wrote:

I tried to make a reference to some memory per our previous discussion,
but the value is zero afterwards.

This code reveals the problem. “by” prints out the value 2. And
“!packetCount” prints out zero. Further debugging a loop that takes the
value !packetCount confirms it is zero.
Am I using incorrect syntax for “val _ = !packetCount = by”

%{^
const uint8_t packet[131];
const uint8_t *packetCount = packet;
const uint8_t *packetHeader = packet + 1;
const uint8_t *packetContents = packet + 2;

%}

macdef packet =
$extval(arrayref(uint8, 131),“packet”)

macdef packetCount =
$extval(ref(uint8),“packetCount”)

val by = serial_poll_for_byte(): uint8
val _ = print! by

val _ = !packetCount = by
val _ = print! (!packetCount)

Change = to :=

that is, do

!packetCount := byOn Mon, Oct 12, 2015 at 9:45 PM, Mike Jones proc...@gmail.com wrote:

I tried to make a reference to some memory per our previous discussion,
but the value is zero afterwards.

This code reveals the problem. “by” prints out the value 2. And
“!packetCount” prints out zero. Further debugging a loop that takes the
value !packetCount confirms it is zero.

Am I using incorrect syntax for “val _ = !packetCount = by”

%{^
const uint8_t packet[131];
const uint8_t *packetCount = packet;
const uint8_t *packetHeader = packet + 1;
const uint8_t *packetContents = packet + 2;

%}

macdef packet =
$extval(arrayref(uint8, 131),“packet”)

macdef packetCount =
$extval(ref(uint8),“packetCount”)

val by = serial_poll_for_byte(): uint8
val _ = print! by

val _ = !packetCount = by
val _ = print! (!packetCount)


You received this message because you are subscribed to the Google Groups
“ats-lang-users” group.
To unsubscribe from this group and stop receiving emails from it, send an
email to ats-lang-user...@googlegroups.com.
To post to this group, send email to ats-lan...@googlegroups.com.
Visit this group at http://groups.google.com/group/ats-lang-users.
To view this discussion on the web visit
https://groups.google.com/d/msgid/ats-lang-users/00418f48-119e-4792-8d5f-de27bd760f41%40googlegroups.com
https://groups.google.com/d/msgid/ats-lang-users/00418f48-119e-4792-8d5f-de27bd760f41%40googlegroups.com?utm_medium=email&utm_source=footer
.

Ok, I’ll experiment, perhaps put the function in the same file, etc.

If I can’t find something else, I can post my version of arduino-ats and
try to make a test case.

By compiling a linux exe, this works. My unworking case is on Arduino. I’ll
dig deeper, try not to use PROGMEM, etc and see if I can find clues.

This example actually shows why it is good idea to write

val () = !a := b

instead of

val _ = !a := b

In the case where := is replaced with =, the former results in a type-error
while the latter may be well-typed.On Monday, October 12, 2015 at 9:45:55 PM UTC-4, Mike Jones wrote:

I tried to make a reference to some memory per our previous discussion,
but the value is zero afterwards.

This code reveals the problem. “by” prints out the value 2. And
“!packetCount” prints out zero. Further debugging a loop that takes the
value !packetCount confirms it is zero.
Am I using incorrect syntax for “val _ = !packetCount = by”

%{^
const uint8_t packet[131];
const uint8_t *packetCount = packet;
const uint8_t *packetHeader = packet + 1;
const uint8_t *packetContents = packet + 2;

%}

macdef packet =
$extval(arrayref(uint8, 131),“packet”)

macdef packetCount =
$extval(ref(uint8),“packetCount”)

val by = serial_poll_for_byte(): uint8
val _ = print! by

val _ = !packetCount = by
val _ = print! (!packetCount)

I suggest that you read the Chapter on extvar-declaration in the following
book,
which is currently Chapter 24:

http://ats-lang.sourceforge.net/DOCUMENT/ATS2TUTORIAL/HTML/HTMLTOC/book1.htmlOn Mon, Oct 12, 2015 at 9:54 PM, Hongwei Xi gmh...@gmail.com wrote:

Change = to :=

that is, do

!packetCount := by

On Mon, Oct 12, 2015 at 9:45 PM, Mike Jones proc...@gmail.com wrote:

I tried to make a reference to some memory per our previous discussion,
but the value is zero afterwards.

This code reveals the problem. “by” prints out the value 2. And
“!packetCount” prints out zero. Further debugging a loop that takes the
value !packetCount confirms it is zero.

Am I using incorrect syntax for “val _ = !packetCount = by”

%{^
const uint8_t packet[131];
const uint8_t *packetCount = packet;
const uint8_t *packetHeader = packet + 1;
const uint8_t *packetContents = packet + 2;

%}

macdef packet =
$extval(arrayref(uint8, 131),“packet”)

macdef packetCount =
$extval(ref(uint8),“packetCount”)

val by = serial_poll_for_byte(): uint8
val _ = print! by

val _ = !packetCount = by
val _ = print! (!packetCount)


You received this message because you are subscribed to the Google Groups
“ats-lang-users” group.
To unsubscribe from this group and stop receiving emails from it, send an
email to ats-lang-user...@googlegroups.com.
To post to this group, send email to ats-lan...@googlegroups.com.
Visit this group at http://groups.google.com/group/ats-lang-users.
To view this discussion on the web visit
https://groups.google.com/d/msgid/ats-lang-users/00418f48-119e-4792-8d5f-de27bd760f41%40googlegroups.com
https://groups.google.com/d/msgid/ats-lang-users/00418f48-119e-4792-8d5f-de27bd760f41%40googlegroups.com?utm_medium=email&utm_source=footer
.