2015-04-25 06:30:14 +00:00
//: Arrays contain a variable number of elements of the same type. Their value
//: starts with the length of the array.
2015-04-18 03:36:25 +00:00
//:
//: You can create arrays of containers, but containers can only contain
//: elements of a fixed size, so you can't create containers containing arrays.
//: Create containers containing addresses to arrays instead.
2015-04-17 18:22:59 +00:00
2015-08-14 23:58:59 +00:00
//: You can create arrays using 'create-array'.
: ( scenario create_array )
2016-03-08 09:30:14 +00:00
def main [
2015-08-14 23:58:59 +00:00
# create an array occupying locations 1 (for the size) and 2-4 (for the elements)
2016-09-17 07:43:13 +00:00
1 : array : num : 3 < - create - array
2015-08-14 23:58:59 +00:00
]
2018-06-24 16:16:17 +00:00
+ run : creating array from 4 locations
2015-08-14 23:58:59 +00:00
: ( before " End Primitive Recipe Declarations " )
CREATE_ARRAY ,
: ( before " End Primitive Recipe Numbers " )
2015-11-06 19:06:58 +00:00
put ( Recipe_ordinal , " create-array " , CREATE_ARRAY ) ;
2015-10-01 23:25:21 +00:00
: ( before " End Primitive Recipe Checks " )
2015-08-14 23:58:59 +00:00
case CREATE_ARRAY : {
2015-10-01 23:25:21 +00:00
if ( inst . products . empty ( ) ) {
2017-05-26 23:43:18 +00:00
raise < < maybe ( get ( Recipe , r ) . name ) < < " 'create-array' needs one product and no ingredients but got ' " < < to_original_string ( inst ) < < ' \n ' < < end ( ) ;
2015-08-14 23:58:59 +00:00
break ;
}
2016-05-06 07:46:39 +00:00
reagent /*copy*/ product = inst . products . at ( 0 ) ;
2016-04-24 07:36:30 +00:00
// Update CREATE_ARRAY product in Check
2015-08-14 23:58:59 +00:00
if ( ! is_mu_array ( product ) ) {
2016-05-21 05:09:06 +00:00
raise < < maybe ( get ( Recipe , r ) . name ) < < " 'create-array' cannot create non-array ' " < < product . original_string < < " ' \n " < < end ( ) ;
2015-08-14 23:58:59 +00:00
break ;
}
2015-10-26 04:42:18 +00:00
if ( ! product . type - > right ) {
2017-05-26 23:43:18 +00:00
raise < < maybe ( get ( Recipe , r ) . name ) < < " create array of what? ' " < < to_original_string ( inst ) < < " ' \n " < < end ( ) ;
2015-08-14 23:58:59 +00:00
break ;
}
// 'create-array' will need to check properties rather than types
3309
Rip out everything to fix one failing unit test (commit 3290; type
abbreviations).
This commit does several things at once that I couldn't come up with a
clean way to unpack:
A. It moves to a new representation for type trees without changing
the actual definition of the `type_tree` struct.
B. It adds unit tests for our type metadata precomputation, so that
errors there show up early and in a simpler setting rather than dying
when we try to load Mu code.
C. It fixes a bug, guarding against infinite loops when precomputing
metadata for recursive shape-shifting containers. To do this it uses a
dumb way of comparing type_trees, comparing their string
representations instead. That is likely incredibly inefficient.
Perhaps due to C, this commit has made Mu incredibly slow. Running all
tests for the core and the edit/ app now takes 6.5 minutes rather than
3.5 minutes.
== more notes and details
I've been struggling for the past week now to back out of a bad design
decision, a premature optimization from the early days: storing atoms
directly in the 'value' slot of a cons cell rather than creating a
special 'atom' cons cell and storing it on the 'left' slot. In other
words, if a cons cell looks like this:
o
/ | \
left val right
..then the type_tree (a b c) used to look like this (before this
commit):
o
| \
a o
| \
b o
| \
c null
..rather than like this 'classic' approach to s-expressions which never
mixes val and right (which is what we now have):
o
/ \
o o
| / \
a o o
| / \
b o null
|
c
The old approach made several operations more complicated, most recently
the act of replacing a (possibly atom/leaf) sub-tree with another. That
was the final straw that got me to realize the contortions I was going
through to save a few type_tree nodes (cons cells).
Switching to the new approach was hard partly because I've been using
the old approach for so long and type_tree manipulations had pervaded
everything. Another issue I ran into was the realization that my layers
were not cleanly separated. Key parts of early layers (precomputing type
metadata) existed purely for far later ones (shape-shifting types).
Layers I got repeatedly stuck at:
1. the transform for precomputing type sizes (layer 30)
2. type-checks on merge instructions (layer 31)
3. the transform for precomputing address offsets in types (layer 36)
4. replace operations in supporting shape-shifting recipes (layer 55)
After much thrashing I finally noticed that it wasn't the entirety of
these layers that was giving me trouble, but just the type metadata
precomputation, which had bugs that weren't manifesting until 30 layers
later. Or, worse, when loading .mu files before any tests had had a
chance to run. A common failure mode was running into types at run time
that I hadn't precomputed metadata for at transform time.
Digging into these bugs got me to realize that what I had before wasn't
really very good, but a half-assed heuristic approach that did a whole
lot of extra work precomputing metadata for utterly meaningless types
like `((address number) 3)` which just happened to be part of a larger
type like `(array (address number) 3)`.
So, I redid it all. I switched the representation of types (because the
old representation made unit tests difficult to retrofit) and added unit
tests to the metadata precomputation. I also made layer 30 only do the
minimal metadata precomputation it needs for the concepts introduced
until then. In the process, I also made the precomputation more correct
than before, and added hooks in the right place so that I could augment
the logic when I introduced shape-shifting containers.
== lessons learned
There's several levels of hygiene when it comes to layers:
1. Every layer introduces precisely what it needs and in the simplest
way possible. If I was building an app until just that layer, nothing
would seem over-engineered.
2. Some layers are fore-shadowing features in future layers. Sometimes
this is ok. For example, layer 10 foreshadows containers and arrays and
so on without actually supporting them. That is a net win because it
lets me lay out the core of Mu's data structures out in one place. But
if the fore-shadowing gets too complex things get nasty. Not least
because it can be hard to write unit tests for features before you
provide the plumbing to visualize and manipulate them.
3. A layer is introducing features that are tested only in later layers.
4. A layer is introducing features with tests that are invalidated in
later layers. (This I knew from early on to be an obviously horrendous
idea.)
Summary: avoid Level 2 (foreshadowing layers) as much as possible.
Tolerate it indefinitely for small things where the code stays simple
over time, but become strict again when things start to get more
complex.
Level 3 is mostly a net lose, but sometimes it can be expedient (a real
case of the usually grossly over-applied term "technical debt"), and
it's better than the conventional baseline of no layers and no
scenarios. Just clean it up as soon as possible.
Definitely avoid layer 4 at any time.
== minor lessons
Avoid unit tests for trivial things, write scenarios in context as much as
possible. But within those margins unit tests are fine. Just introduce them
before any scenarios (commit 3297).
Reorganizing layers can be easy. Just merge layers for starters! Punt on
resplitting them in some new way until you've gotten them to work. This is the
wisdom of Refactoring: small steps.
What made it hard was not wanting to merge *everything* between layer 30
and 55. The eventual insight was realizing I just need to move those two
full-strength transforms and nothing else.
2016-09-10 01:32:52 +00:00
type_tree * array_length_from_type = product . type - > right - > right ;
if ( ! array_length_from_type ) {
2017-05-26 23:43:18 +00:00
raise < < maybe ( get ( Recipe , r ) . name ) < < " create array of what size? ' " < < to_original_string ( inst ) < < " ' \n " < < end ( ) ;
2015-08-14 23:58:59 +00:00
break ;
}
3309
Rip out everything to fix one failing unit test (commit 3290; type
abbreviations).
This commit does several things at once that I couldn't come up with a
clean way to unpack:
A. It moves to a new representation for type trees without changing
the actual definition of the `type_tree` struct.
B. It adds unit tests for our type metadata precomputation, so that
errors there show up early and in a simpler setting rather than dying
when we try to load Mu code.
C. It fixes a bug, guarding against infinite loops when precomputing
metadata for recursive shape-shifting containers. To do this it uses a
dumb way of comparing type_trees, comparing their string
representations instead. That is likely incredibly inefficient.
Perhaps due to C, this commit has made Mu incredibly slow. Running all
tests for the core and the edit/ app now takes 6.5 minutes rather than
3.5 minutes.
== more notes and details
I've been struggling for the past week now to back out of a bad design
decision, a premature optimization from the early days: storing atoms
directly in the 'value' slot of a cons cell rather than creating a
special 'atom' cons cell and storing it on the 'left' slot. In other
words, if a cons cell looks like this:
o
/ | \
left val right
..then the type_tree (a b c) used to look like this (before this
commit):
o
| \
a o
| \
b o
| \
c null
..rather than like this 'classic' approach to s-expressions which never
mixes val and right (which is what we now have):
o
/ \
o o
| / \
a o o
| / \
b o null
|
c
The old approach made several operations more complicated, most recently
the act of replacing a (possibly atom/leaf) sub-tree with another. That
was the final straw that got me to realize the contortions I was going
through to save a few type_tree nodes (cons cells).
Switching to the new approach was hard partly because I've been using
the old approach for so long and type_tree manipulations had pervaded
everything. Another issue I ran into was the realization that my layers
were not cleanly separated. Key parts of early layers (precomputing type
metadata) existed purely for far later ones (shape-shifting types).
Layers I got repeatedly stuck at:
1. the transform for precomputing type sizes (layer 30)
2. type-checks on merge instructions (layer 31)
3. the transform for precomputing address offsets in types (layer 36)
4. replace operations in supporting shape-shifting recipes (layer 55)
After much thrashing I finally noticed that it wasn't the entirety of
these layers that was giving me trouble, but just the type metadata
precomputation, which had bugs that weren't manifesting until 30 layers
later. Or, worse, when loading .mu files before any tests had had a
chance to run. A common failure mode was running into types at run time
that I hadn't precomputed metadata for at transform time.
Digging into these bugs got me to realize that what I had before wasn't
really very good, but a half-assed heuristic approach that did a whole
lot of extra work precomputing metadata for utterly meaningless types
like `((address number) 3)` which just happened to be part of a larger
type like `(array (address number) 3)`.
So, I redid it all. I switched the representation of types (because the
old representation made unit tests difficult to retrofit) and added unit
tests to the metadata precomputation. I also made layer 30 only do the
minimal metadata precomputation it needs for the concepts introduced
until then. In the process, I also made the precomputation more correct
than before, and added hooks in the right place so that I could augment
the logic when I introduced shape-shifting containers.
== lessons learned
There's several levels of hygiene when it comes to layers:
1. Every layer introduces precisely what it needs and in the simplest
way possible. If I was building an app until just that layer, nothing
would seem over-engineered.
2. Some layers are fore-shadowing features in future layers. Sometimes
this is ok. For example, layer 10 foreshadows containers and arrays and
so on without actually supporting them. That is a net win because it
lets me lay out the core of Mu's data structures out in one place. But
if the fore-shadowing gets too complex things get nasty. Not least
because it can be hard to write unit tests for features before you
provide the plumbing to visualize and manipulate them.
3. A layer is introducing features that are tested only in later layers.
4. A layer is introducing features with tests that are invalidated in
later layers. (This I knew from early on to be an obviously horrendous
idea.)
Summary: avoid Level 2 (foreshadowing layers) as much as possible.
Tolerate it indefinitely for small things where the code stays simple
over time, but become strict again when things start to get more
complex.
Level 3 is mostly a net lose, but sometimes it can be expedient (a real
case of the usually grossly over-applied term "technical debt"), and
it's better than the conventional baseline of no layers and no
scenarios. Just clean it up as soon as possible.
Definitely avoid layer 4 at any time.
== minor lessons
Avoid unit tests for trivial things, write scenarios in context as much as
possible. But within those margins unit tests are fine. Just introduce them
before any scenarios (commit 3297).
Reorganizing layers can be easy. Just merge layers for starters! Punt on
resplitting them in some new way until you've gotten them to work. This is the
wisdom of Refactoring: small steps.
What made it hard was not wanting to merge *everything* between layer 30
and 55. The eventual insight was realizing I just need to move those two
full-strength transforms and nothing else.
2016-09-10 01:32:52 +00:00
if ( ! product . type - > right - > right - > atom )
array_length_from_type = array_length_from_type - > left ;
if ( ! is_integer ( array_length_from_type - > name ) ) {
2016-05-21 05:09:06 +00:00
raise < < maybe ( get ( Recipe , r ) . name ) < < " 'create-array' product should specify size of array after its element type, but got ' " < < product . type - > right - > right - > name < < " ' \n " < < end ( ) ;
2015-08-14 23:58:59 +00:00
break ;
}
2015-10-01 23:25:21 +00:00
break ;
}
: ( before " End Primitive Recipe Implementations " )
case CREATE_ARRAY : {
2016-05-06 07:46:39 +00:00
reagent /*copy*/ product = current_instruction ( ) . products . at ( 0 ) ;
2016-04-24 07:36:30 +00:00
// Update CREATE_ARRAY product in Run
2016-03-14 03:26:47 +00:00
int base_address = product . value ;
3309
Rip out everything to fix one failing unit test (commit 3290; type
abbreviations).
This commit does several things at once that I couldn't come up with a
clean way to unpack:
A. It moves to a new representation for type trees without changing
the actual definition of the `type_tree` struct.
B. It adds unit tests for our type metadata precomputation, so that
errors there show up early and in a simpler setting rather than dying
when we try to load Mu code.
C. It fixes a bug, guarding against infinite loops when precomputing
metadata for recursive shape-shifting containers. To do this it uses a
dumb way of comparing type_trees, comparing their string
representations instead. That is likely incredibly inefficient.
Perhaps due to C, this commit has made Mu incredibly slow. Running all
tests for the core and the edit/ app now takes 6.5 minutes rather than
3.5 minutes.
== more notes and details
I've been struggling for the past week now to back out of a bad design
decision, a premature optimization from the early days: storing atoms
directly in the 'value' slot of a cons cell rather than creating a
special 'atom' cons cell and storing it on the 'left' slot. In other
words, if a cons cell looks like this:
o
/ | \
left val right
..then the type_tree (a b c) used to look like this (before this
commit):
o
| \
a o
| \
b o
| \
c null
..rather than like this 'classic' approach to s-expressions which never
mixes val and right (which is what we now have):
o
/ \
o o
| / \
a o o
| / \
b o null
|
c
The old approach made several operations more complicated, most recently
the act of replacing a (possibly atom/leaf) sub-tree with another. That
was the final straw that got me to realize the contortions I was going
through to save a few type_tree nodes (cons cells).
Switching to the new approach was hard partly because I've been using
the old approach for so long and type_tree manipulations had pervaded
everything. Another issue I ran into was the realization that my layers
were not cleanly separated. Key parts of early layers (precomputing type
metadata) existed purely for far later ones (shape-shifting types).
Layers I got repeatedly stuck at:
1. the transform for precomputing type sizes (layer 30)
2. type-checks on merge instructions (layer 31)
3. the transform for precomputing address offsets in types (layer 36)
4. replace operations in supporting shape-shifting recipes (layer 55)
After much thrashing I finally noticed that it wasn't the entirety of
these layers that was giving me trouble, but just the type metadata
precomputation, which had bugs that weren't manifesting until 30 layers
later. Or, worse, when loading .mu files before any tests had had a
chance to run. A common failure mode was running into types at run time
that I hadn't precomputed metadata for at transform time.
Digging into these bugs got me to realize that what I had before wasn't
really very good, but a half-assed heuristic approach that did a whole
lot of extra work precomputing metadata for utterly meaningless types
like `((address number) 3)` which just happened to be part of a larger
type like `(array (address number) 3)`.
So, I redid it all. I switched the representation of types (because the
old representation made unit tests difficult to retrofit) and added unit
tests to the metadata precomputation. I also made layer 30 only do the
minimal metadata precomputation it needs for the concepts introduced
until then. In the process, I also made the precomputation more correct
than before, and added hooks in the right place so that I could augment
the logic when I introduced shape-shifting containers.
== lessons learned
There's several levels of hygiene when it comes to layers:
1. Every layer introduces precisely what it needs and in the simplest
way possible. If I was building an app until just that layer, nothing
would seem over-engineered.
2. Some layers are fore-shadowing features in future layers. Sometimes
this is ok. For example, layer 10 foreshadows containers and arrays and
so on without actually supporting them. That is a net win because it
lets me lay out the core of Mu's data structures out in one place. But
if the fore-shadowing gets too complex things get nasty. Not least
because it can be hard to write unit tests for features before you
provide the plumbing to visualize and manipulate them.
3. A layer is introducing features that are tested only in later layers.
4. A layer is introducing features with tests that are invalidated in
later layers. (This I knew from early on to be an obviously horrendous
idea.)
Summary: avoid Level 2 (foreshadowing layers) as much as possible.
Tolerate it indefinitely for small things where the code stays simple
over time, but become strict again when things start to get more
complex.
Level 3 is mostly a net lose, but sometimes it can be expedient (a real
case of the usually grossly over-applied term "technical debt"), and
it's better than the conventional baseline of no layers and no
scenarios. Just clean it up as soon as possible.
Definitely avoid layer 4 at any time.
== minor lessons
Avoid unit tests for trivial things, write scenarios in context as much as
possible. But within those margins unit tests are fine. Just introduce them
before any scenarios (commit 3297).
Reorganizing layers can be easy. Just merge layers for starters! Punt on
resplitting them in some new way until you've gotten them to work. This is the
wisdom of Refactoring: small steps.
What made it hard was not wanting to merge *everything* between layer 30
and 55. The eventual insight was realizing I just need to move those two
full-strength transforms and nothing else.
2016-09-10 01:32:52 +00:00
type_tree * array_length_from_type = product . type - > right - > right ;
if ( ! product . type - > right - > right - > atom )
array_length_from_type = array_length_from_type - > left ;
int array_length = to_integer ( array_length_from_type - > name ) ;
2016-08-26 20:47:39 +00:00
// initialize array length, so that size_of will work
2017-11-03 08:50:46 +00:00
trace ( " mem " ) < < " storing " < < array_length < < " in location " < < base_address < < end ( ) ;
2016-04-13 16:03:06 +00:00
put ( Memory , base_address , array_length ) ; // in array elements
2016-03-14 03:26:47 +00:00
int size = size_of ( product ) ; // in locations
2018-06-24 16:16:17 +00:00
trace ( 9998 , " run " ) < < " creating array from " < < size < < " locations " < < end ( ) ;
2015-08-14 23:58:59 +00:00
// initialize array
2017-08-19 12:38:22 +00:00
for ( int i = 1 ; i < = size_of ( product ) ; + + i )
2015-11-06 19:06:58 +00:00
put ( Memory , base_address + i , 0 ) ;
2016-04-13 16:03:06 +00:00
// no need to update product
2017-05-28 21:28:07 +00:00
write_products = false ;
break ;
2015-08-14 23:58:59 +00:00
}
2015-02-22 06:08:58 +00:00
: ( scenario copy_array )
2015-05-13 17:03:26 +00:00
# Arrays can be copied around with a single instruction just like numbers,
2015-03-27 04:47:29 +00:00
# no matter how large they are.
2016-04-13 16:03:06 +00:00
# You don't need to pass the size around, since each array variable stores its
# size in memory at run-time. We'll call a variable with an explicit size a
# 'static' array, and one without a 'dynamic' array since it can contain
# arrays of many different sizes.
2016-03-08 09:30:14 +00:00
def main [
2016-09-17 07:43:13 +00:00
1 : array : num : 3 < - create - array
2 : num < - copy 14
3 : num < - copy 15
4 : num < - copy 16
5 : array : num < - copy 1 : array : num : 3
2015-02-22 06:08:58 +00:00
]
2015-03-24 06:59:59 +00:00
+ mem : storing 3 in location 5
+ mem : storing 14 in location 6
+ mem : storing 15 in location 7
+ mem : storing 16 in location 8
2015-02-22 06:08:58 +00:00
2015-08-11 06:15:00 +00:00
: ( scenario stash_array )
2016-03-08 09:30:14 +00:00
def main [
2016-09-17 07:43:13 +00:00
1 : array : num : 3 < - create - array
2 : num < - copy 14
3 : num < - copy 15
4 : num < - copy 16
stash [ foo : ] , 1 : array : num : 3
2015-08-11 06:15:00 +00:00
]
+ app : foo : 3 14 15 16
2016-11-11 05:39:02 +00:00
: ( before " End types_coercible Special-cases " )
if ( is_mu_array ( from ) & & is_mu_array ( to ) )
return types_strictly_match ( array_element ( from . type ) , array_element ( to . type ) ) ;
2016-11-07 17:10:48 +00:00
: ( before " End size_of(reagent r) Special-cases " )
2018-05-13 02:55:21 +00:00
if ( ! r . type - > atom & & r . type - > left - > atom & & r . type - > left - > value = = Array_type_ordinal ) {
2015-10-26 04:42:18 +00:00
if ( ! r . type - > right ) {
2016-02-26 21:04:55 +00:00
raise < < maybe ( current_recipe_name ( ) ) < < " ' " < < r . original_string < < " ' is an array of what? \n " < < end ( ) ;
2015-08-07 19:18:09 +00:00
return 1 ;
2015-03-27 03:24:38 +00:00
}
2016-11-07 17:59:51 +00:00
return /*space for length*/ 1 + array_length ( r ) * size_of ( array_element ( r . type ) ) ;
2015-08-07 19:18:09 +00:00
}
2015-02-22 09:32:24 +00:00
2016-11-08 18:20:49 +00:00
: ( before " End size_of(type) Non-atom Special-cases " )
2018-05-13 02:55:21 +00:00
if ( type - > left - > value = = Array_type_ordinal ) return static_array_length ( type ) ;
2016-11-08 18:20:49 +00:00
: ( code )
int static_array_length ( const type_tree * type ) {
2016-11-11 05:39:02 +00:00
if ( ! type - > atom & & type - > right & & ! type - > right - > atom & & type - > right - > right & & ! type - > right - > right - > atom & & ! type - > right - > right - > right // exactly 3 types
& & type - > right - > right - > left & & type - > right - > right - > left - > atom & & is_integer ( type - > right - > right - > left - > name ) ) { // third 'type' is a number
2016-11-08 18:20:49 +00:00
// get size from type
2016-11-11 05:39:02 +00:00
return to_integer ( type - > right - > right - > left - > name ) ;
2016-11-08 18:20:49 +00:00
}
cerr < < to_string ( type ) < < ' \n ' ;
assert ( false ) ;
}
2016-04-13 16:03:06 +00:00
//: disable the size mismatch check for arrays since the destination array
//: need not be initialized
2016-11-07 17:10:48 +00:00
: ( before " End size_mismatch(x) Special-cases " )
2018-05-13 02:55:21 +00:00
if ( x . type & & ! x . type - > atom & & x . type - > left - > value = = Array_type_ordinal ) return false ;
2016-04-13 16:03:06 +00:00
2016-11-07 17:23:35 +00:00
//:: arrays inside containers
2016-02-15 20:25:13 +00:00
//: arrays are disallowed inside containers unless their length is fixed in
//: advance
2016-07-12 04:31:22 +00:00
: ( scenario container_permits_static_array_element )
2016-02-15 20:25:13 +00:00
container foo [
2016-09-17 07:43:13 +00:00
x : array : num : 3
2016-02-15 20:25:13 +00:00
]
$ error : 0
2016-11-07 18:27:57 +00:00
: ( before " End insert_container Special-cases " )
else if ( is_integer ( type - > name ) ) { // sometimes types will contain non-type tags, like numbers for the size of an array
type - > value = 0 ;
}
2016-02-25 19:29:42 +00:00
: ( scenario container_disallows_dynamic_array_element )
2016-02-15 20:25:13 +00:00
% Hide_errors = true ;
container foo [
2016-09-17 07:43:13 +00:00
x : array : num
2016-02-15 20:25:13 +00:00
]
2016-05-21 05:09:06 +00:00
+ error : container ' foo ' cannot determine size of element ' x '
2016-02-15 20:25:13 +00:00
2016-11-07 18:27:57 +00:00
: ( before " End Load Container Element Definition " )
{
const type_tree * type = info . elements . back ( ) . type ;
if ( type & & type - > atom & & type - > name = = " array " ) {
raise < < " container ' " < < name < < " ' doesn't specify type of array elements for ' " < < info . elements . back ( ) . name < < " ' \n " < < end ( ) ;
continue ;
}
if ( type & & ! type - > atom & & type - > left - > atom & & type - > left - > name = = " array " ) {
if ( ! type - > right ) {
raise < < " container ' " < < name < < " ' doesn't specify type of array elements for ' " < < info . elements . back ( ) . name < < " ' \n " < < end ( ) ;
continue ;
}
2016-11-11 05:39:02 +00:00
if ( ! type - > right - > right | | ! is_integer ( type - > right - > right - > left - > name ) ) { // array has no length
2016-11-07 18:27:57 +00:00
raise < < " container ' " < < name < < " ' cannot determine size of element ' " < < info . elements . back ( ) . name < < " ' \n " < < end ( ) ;
continue ;
}
}
2016-11-07 17:23:35 +00:00
}
2016-07-12 04:31:22 +00:00
//: disable the size mismatch check for 'merge' instructions since containers
//: can contain arrays, and since we already do plenty of checking for them
2016-11-07 17:10:48 +00:00
: ( before " End size_mismatch(x) Special-cases " )
2016-07-12 04:31:22 +00:00
if ( current_call ( ) . running_step_index < SIZE ( get ( Recipe , current_call ( ) . running_recipe ) . steps )
& & current_instruction ( ) . operation = = MERGE ) {
return false ;
}
: ( scenario merge_static_array_into_container )
container foo [
2016-09-17 07:43:13 +00:00
x : num
y : array : num : 3
2016-07-12 04:31:22 +00:00
]
def main [
2016-09-17 07:43:13 +00:00
1 : array : num : 3 < - create - array
10 : foo < - merge 34 , 1 : array : num : 3
2016-07-12 04:31:22 +00:00
]
# no errors
2016-06-17 18:30:24 +00:00
: ( scenario code_inside_container )
2016-06-18 00:22:15 +00:00
% Hide_errors = true ;
2016-06-17 18:30:24 +00:00
container card [
2016-09-17 07:43:13 +00:00
rank : num < - next - ingredient
2016-06-17 18:30:24 +00:00
]
2016-09-15 20:23:00 +00:00
def foo [
2016-06-17 18:30:24 +00:00
1 : card < - merge 3
2016-09-17 07:43:13 +00:00
2 : num < - get 1 : card rank : offset
2016-06-17 18:30:24 +00:00
]
# shouldn't die
2015-04-18 06:24:52 +00:00
//:: To access elements of an array, use 'index'
2015-04-24 17:19:03 +00:00
: ( scenario index )
2016-03-08 09:30:14 +00:00
def main [
2016-09-17 07:43:13 +00:00
1 : array : num : 3 < - create - array
2 : num < - copy 14
3 : num < - copy 15
4 : num < - copy 16
2018-06-24 16:16:17 +00:00
10 : num < - index 1 : array : num : 3 , 0 / index # the index must be a non - negative whole number
2015-03-27 04:47:29 +00:00
]
2018-06-24 16:16:17 +00:00
+ mem : storing 14 in location 10
2015-03-27 04:47:29 +00:00
3309
Rip out everything to fix one failing unit test (commit 3290; type
abbreviations).
This commit does several things at once that I couldn't come up with a
clean way to unpack:
A. It moves to a new representation for type trees without changing
the actual definition of the `type_tree` struct.
B. It adds unit tests for our type metadata precomputation, so that
errors there show up early and in a simpler setting rather than dying
when we try to load Mu code.
C. It fixes a bug, guarding against infinite loops when precomputing
metadata for recursive shape-shifting containers. To do this it uses a
dumb way of comparing type_trees, comparing their string
representations instead. That is likely incredibly inefficient.
Perhaps due to C, this commit has made Mu incredibly slow. Running all
tests for the core and the edit/ app now takes 6.5 minutes rather than
3.5 minutes.
== more notes and details
I've been struggling for the past week now to back out of a bad design
decision, a premature optimization from the early days: storing atoms
directly in the 'value' slot of a cons cell rather than creating a
special 'atom' cons cell and storing it on the 'left' slot. In other
words, if a cons cell looks like this:
o
/ | \
left val right
..then the type_tree (a b c) used to look like this (before this
commit):
o
| \
a o
| \
b o
| \
c null
..rather than like this 'classic' approach to s-expressions which never
mixes val and right (which is what we now have):
o
/ \
o o
| / \
a o o
| / \
b o null
|
c
The old approach made several operations more complicated, most recently
the act of replacing a (possibly atom/leaf) sub-tree with another. That
was the final straw that got me to realize the contortions I was going
through to save a few type_tree nodes (cons cells).
Switching to the new approach was hard partly because I've been using
the old approach for so long and type_tree manipulations had pervaded
everything. Another issue I ran into was the realization that my layers
were not cleanly separated. Key parts of early layers (precomputing type
metadata) existed purely for far later ones (shape-shifting types).
Layers I got repeatedly stuck at:
1. the transform for precomputing type sizes (layer 30)
2. type-checks on merge instructions (layer 31)
3. the transform for precomputing address offsets in types (layer 36)
4. replace operations in supporting shape-shifting recipes (layer 55)
After much thrashing I finally noticed that it wasn't the entirety of
these layers that was giving me trouble, but just the type metadata
precomputation, which had bugs that weren't manifesting until 30 layers
later. Or, worse, when loading .mu files before any tests had had a
chance to run. A common failure mode was running into types at run time
that I hadn't precomputed metadata for at transform time.
Digging into these bugs got me to realize that what I had before wasn't
really very good, but a half-assed heuristic approach that did a whole
lot of extra work precomputing metadata for utterly meaningless types
like `((address number) 3)` which just happened to be part of a larger
type like `(array (address number) 3)`.
So, I redid it all. I switched the representation of types (because the
old representation made unit tests difficult to retrofit) and added unit
tests to the metadata precomputation. I also made layer 30 only do the
minimal metadata precomputation it needs for the concepts introduced
until then. In the process, I also made the precomputation more correct
than before, and added hooks in the right place so that I could augment
the logic when I introduced shape-shifting containers.
== lessons learned
There's several levels of hygiene when it comes to layers:
1. Every layer introduces precisely what it needs and in the simplest
way possible. If I was building an app until just that layer, nothing
would seem over-engineered.
2. Some layers are fore-shadowing features in future layers. Sometimes
this is ok. For example, layer 10 foreshadows containers and arrays and
so on without actually supporting them. That is a net win because it
lets me lay out the core of Mu's data structures out in one place. But
if the fore-shadowing gets too complex things get nasty. Not least
because it can be hard to write unit tests for features before you
provide the plumbing to visualize and manipulate them.
3. A layer is introducing features that are tested only in later layers.
4. A layer is introducing features with tests that are invalidated in
later layers. (This I knew from early on to be an obviously horrendous
idea.)
Summary: avoid Level 2 (foreshadowing layers) as much as possible.
Tolerate it indefinitely for small things where the code stays simple
over time, but become strict again when things start to get more
complex.
Level 3 is mostly a net lose, but sometimes it can be expedient (a real
case of the usually grossly over-applied term "technical debt"), and
it's better than the conventional baseline of no layers and no
scenarios. Just clean it up as soon as possible.
Definitely avoid layer 4 at any time.
== minor lessons
Avoid unit tests for trivial things, write scenarios in context as much as
possible. But within those margins unit tests are fine. Just introduce them
before any scenarios (commit 3297).
Reorganizing layers can be easy. Just merge layers for starters! Punt on
resplitting them in some new way until you've gotten them to work. This is the
wisdom of Refactoring: small steps.
What made it hard was not wanting to merge *everything* between layer 30
and 55. The eventual insight was realizing I just need to move those two
full-strength transforms and nothing else.
2016-09-10 01:32:52 +00:00
: ( scenario index_compound_element )
def main [
{ 1 : ( array ( address number ) 3 ) } < - create - array
2018-06-24 16:16:17 +00:00
# skip alloc id
3 : num < - copy 14
# skip alloc id
5 : num < - copy 15
# skip alloc id
7 : num < - copy 16
10 : address : num < - index { 1 : ( array ( address number ) 3 ) } , 0
3309
Rip out everything to fix one failing unit test (commit 3290; type
abbreviations).
This commit does several things at once that I couldn't come up with a
clean way to unpack:
A. It moves to a new representation for type trees without changing
the actual definition of the `type_tree` struct.
B. It adds unit tests for our type metadata precomputation, so that
errors there show up early and in a simpler setting rather than dying
when we try to load Mu code.
C. It fixes a bug, guarding against infinite loops when precomputing
metadata for recursive shape-shifting containers. To do this it uses a
dumb way of comparing type_trees, comparing their string
representations instead. That is likely incredibly inefficient.
Perhaps due to C, this commit has made Mu incredibly slow. Running all
tests for the core and the edit/ app now takes 6.5 minutes rather than
3.5 minutes.
== more notes and details
I've been struggling for the past week now to back out of a bad design
decision, a premature optimization from the early days: storing atoms
directly in the 'value' slot of a cons cell rather than creating a
special 'atom' cons cell and storing it on the 'left' slot. In other
words, if a cons cell looks like this:
o
/ | \
left val right
..then the type_tree (a b c) used to look like this (before this
commit):
o
| \
a o
| \
b o
| \
c null
..rather than like this 'classic' approach to s-expressions which never
mixes val and right (which is what we now have):
o
/ \
o o
| / \
a o o
| / \
b o null
|
c
The old approach made several operations more complicated, most recently
the act of replacing a (possibly atom/leaf) sub-tree with another. That
was the final straw that got me to realize the contortions I was going
through to save a few type_tree nodes (cons cells).
Switching to the new approach was hard partly because I've been using
the old approach for so long and type_tree manipulations had pervaded
everything. Another issue I ran into was the realization that my layers
were not cleanly separated. Key parts of early layers (precomputing type
metadata) existed purely for far later ones (shape-shifting types).
Layers I got repeatedly stuck at:
1. the transform for precomputing type sizes (layer 30)
2. type-checks on merge instructions (layer 31)
3. the transform for precomputing address offsets in types (layer 36)
4. replace operations in supporting shape-shifting recipes (layer 55)
After much thrashing I finally noticed that it wasn't the entirety of
these layers that was giving me trouble, but just the type metadata
precomputation, which had bugs that weren't manifesting until 30 layers
later. Or, worse, when loading .mu files before any tests had had a
chance to run. A common failure mode was running into types at run time
that I hadn't precomputed metadata for at transform time.
Digging into these bugs got me to realize that what I had before wasn't
really very good, but a half-assed heuristic approach that did a whole
lot of extra work precomputing metadata for utterly meaningless types
like `((address number) 3)` which just happened to be part of a larger
type like `(array (address number) 3)`.
So, I redid it all. I switched the representation of types (because the
old representation made unit tests difficult to retrofit) and added unit
tests to the metadata precomputation. I also made layer 30 only do the
minimal metadata precomputation it needs for the concepts introduced
until then. In the process, I also made the precomputation more correct
than before, and added hooks in the right place so that I could augment
the logic when I introduced shape-shifting containers.
== lessons learned
There's several levels of hygiene when it comes to layers:
1. Every layer introduces precisely what it needs and in the simplest
way possible. If I was building an app until just that layer, nothing
would seem over-engineered.
2. Some layers are fore-shadowing features in future layers. Sometimes
this is ok. For example, layer 10 foreshadows containers and arrays and
so on without actually supporting them. That is a net win because it
lets me lay out the core of Mu's data structures out in one place. But
if the fore-shadowing gets too complex things get nasty. Not least
because it can be hard to write unit tests for features before you
provide the plumbing to visualize and manipulate them.
3. A layer is introducing features that are tested only in later layers.
4. A layer is introducing features with tests that are invalidated in
later layers. (This I knew from early on to be an obviously horrendous
idea.)
Summary: avoid Level 2 (foreshadowing layers) as much as possible.
Tolerate it indefinitely for small things where the code stays simple
over time, but become strict again when things start to get more
complex.
Level 3 is mostly a net lose, but sometimes it can be expedient (a real
case of the usually grossly over-applied term "technical debt"), and
it's better than the conventional baseline of no layers and no
scenarios. Just clean it up as soon as possible.
Definitely avoid layer 4 at any time.
== minor lessons
Avoid unit tests for trivial things, write scenarios in context as much as
possible. But within those margins unit tests are fine. Just introduce them
before any scenarios (commit 3297).
Reorganizing layers can be easy. Just merge layers for starters! Punt on
resplitting them in some new way until you've gotten them to work. This is the
wisdom of Refactoring: small steps.
What made it hard was not wanting to merge *everything* between layer 30
and 55. The eventual insight was realizing I just need to move those two
full-strength transforms and nothing else.
2016-09-10 01:32:52 +00:00
]
2018-06-24 16:16:17 +00:00
# skip alloc id
+ mem : storing 14 in location 11
3309
Rip out everything to fix one failing unit test (commit 3290; type
abbreviations).
This commit does several things at once that I couldn't come up with a
clean way to unpack:
A. It moves to a new representation for type trees without changing
the actual definition of the `type_tree` struct.
B. It adds unit tests for our type metadata precomputation, so that
errors there show up early and in a simpler setting rather than dying
when we try to load Mu code.
C. It fixes a bug, guarding against infinite loops when precomputing
metadata for recursive shape-shifting containers. To do this it uses a
dumb way of comparing type_trees, comparing their string
representations instead. That is likely incredibly inefficient.
Perhaps due to C, this commit has made Mu incredibly slow. Running all
tests for the core and the edit/ app now takes 6.5 minutes rather than
3.5 minutes.
== more notes and details
I've been struggling for the past week now to back out of a bad design
decision, a premature optimization from the early days: storing atoms
directly in the 'value' slot of a cons cell rather than creating a
special 'atom' cons cell and storing it on the 'left' slot. In other
words, if a cons cell looks like this:
o
/ | \
left val right
..then the type_tree (a b c) used to look like this (before this
commit):
o
| \
a o
| \
b o
| \
c null
..rather than like this 'classic' approach to s-expressions which never
mixes val and right (which is what we now have):
o
/ \
o o
| / \
a o o
| / \
b o null
|
c
The old approach made several operations more complicated, most recently
the act of replacing a (possibly atom/leaf) sub-tree with another. That
was the final straw that got me to realize the contortions I was going
through to save a few type_tree nodes (cons cells).
Switching to the new approach was hard partly because I've been using
the old approach for so long and type_tree manipulations had pervaded
everything. Another issue I ran into was the realization that my layers
were not cleanly separated. Key parts of early layers (precomputing type
metadata) existed purely for far later ones (shape-shifting types).
Layers I got repeatedly stuck at:
1. the transform for precomputing type sizes (layer 30)
2. type-checks on merge instructions (layer 31)
3. the transform for precomputing address offsets in types (layer 36)
4. replace operations in supporting shape-shifting recipes (layer 55)
After much thrashing I finally noticed that it wasn't the entirety of
these layers that was giving me trouble, but just the type metadata
precomputation, which had bugs that weren't manifesting until 30 layers
later. Or, worse, when loading .mu files before any tests had had a
chance to run. A common failure mode was running into types at run time
that I hadn't precomputed metadata for at transform time.
Digging into these bugs got me to realize that what I had before wasn't
really very good, but a half-assed heuristic approach that did a whole
lot of extra work precomputing metadata for utterly meaningless types
like `((address number) 3)` which just happened to be part of a larger
type like `(array (address number) 3)`.
So, I redid it all. I switched the representation of types (because the
old representation made unit tests difficult to retrofit) and added unit
tests to the metadata precomputation. I also made layer 30 only do the
minimal metadata precomputation it needs for the concepts introduced
until then. In the process, I also made the precomputation more correct
than before, and added hooks in the right place so that I could augment
the logic when I introduced shape-shifting containers.
== lessons learned
There's several levels of hygiene when it comes to layers:
1. Every layer introduces precisely what it needs and in the simplest
way possible. If I was building an app until just that layer, nothing
would seem over-engineered.
2. Some layers are fore-shadowing features in future layers. Sometimes
this is ok. For example, layer 10 foreshadows containers and arrays and
so on without actually supporting them. That is a net win because it
lets me lay out the core of Mu's data structures out in one place. But
if the fore-shadowing gets too complex things get nasty. Not least
because it can be hard to write unit tests for features before you
provide the plumbing to visualize and manipulate them.
3. A layer is introducing features that are tested only in later layers.
4. A layer is introducing features with tests that are invalidated in
later layers. (This I knew from early on to be an obviously horrendous
idea.)
Summary: avoid Level 2 (foreshadowing layers) as much as possible.
Tolerate it indefinitely for small things where the code stays simple
over time, but become strict again when things start to get more
complex.
Level 3 is mostly a net lose, but sometimes it can be expedient (a real
case of the usually grossly over-applied term "technical debt"), and
it's better than the conventional baseline of no layers and no
scenarios. Just clean it up as soon as possible.
Definitely avoid layer 4 at any time.
== minor lessons
Avoid unit tests for trivial things, write scenarios in context as much as
possible. But within those margins unit tests are fine. Just introduce them
before any scenarios (commit 3297).
Reorganizing layers can be easy. Just merge layers for starters! Punt on
resplitting them in some new way until you've gotten them to work. This is the
wisdom of Refactoring: small steps.
What made it hard was not wanting to merge *everything* between layer 30
and 55. The eventual insight was realizing I just need to move those two
full-strength transforms and nothing else.
2016-09-10 01:32:52 +00:00
2015-04-24 17:19:03 +00:00
: ( scenario index_direct_offset )
2016-03-08 09:30:14 +00:00
def main [
2016-09-17 07:43:13 +00:00
1 : array : num : 3 < - create - array
2 : num < - copy 14
3 : num < - copy 15
4 : num < - copy 16
2018-06-24 16:16:17 +00:00
10 : num < - copy 0
20 : num < - index 1 : array : num , 10 : num
2015-04-03 19:49:11 +00:00
]
2018-06-24 16:16:17 +00:00
+ mem : storing 14 in location 20
2015-04-03 19:49:11 +00:00
2015-04-17 17:31:17 +00:00
: ( before " End Primitive Recipe Declarations " )
INDEX ,
2015-02-22 09:32:24 +00:00
: ( before " End Primitive Recipe Numbers " )
2015-11-06 19:06:58 +00:00
put ( Recipe_ordinal , " index " , INDEX ) ;
2015-10-01 23:25:21 +00:00
: ( before " End Primitive Recipe Checks " )
2015-02-22 09:32:24 +00:00
case INDEX : {
2015-10-01 23:25:21 +00:00
if ( SIZE ( inst . ingredients ) ! = 2 ) {
2017-05-26 23:43:18 +00:00
raise < < maybe ( get ( Recipe , r ) . name ) < < " 'index' expects exactly 2 ingredients in ' " < < to_original_string ( inst ) < < " ' \n " < < end ( ) ;
2015-07-25 21:19:28 +00:00
break ;
}
2016-05-06 07:46:39 +00:00
reagent /*copy*/ base = inst . ingredients . at ( 0 ) ;
2016-04-24 07:36:30 +00:00
// Update INDEX base in Check
2015-08-13 23:32:36 +00:00
if ( ! is_mu_array ( base ) ) {
2016-05-21 05:09:06 +00:00
raise < < maybe ( get ( Recipe , r ) . name ) < < " 'index' on a non-array ' " < < base . original_string < < " ' \n " < < end ( ) ;
2015-08-01 00:06:38 +00:00
break ;
}
2016-05-06 07:46:39 +00:00
reagent /*copy*/ index = inst . ingredients . at ( 1 ) ;
2016-04-24 07:36:30 +00:00
// Update INDEX index in Check
if ( ! is_mu_number ( index ) ) {
2016-05-21 05:09:06 +00:00
raise < < maybe ( get ( Recipe , r ) . name ) < < " second ingredient of 'index' should be a number, but got ' " < < index . original_string < < " ' \n " < < end ( ) ;
2016-04-24 07:36:30 +00:00
break ;
}
2015-10-06 00:02:32 +00:00
if ( inst . products . empty ( ) ) break ;
2016-05-06 07:46:39 +00:00
reagent /*copy*/ product = inst . products . at ( 0 ) ;
2016-04-24 07:36:30 +00:00
// Update INDEX product in Check
2018-03-14 07:59:41 +00:00
reagent /*local*/ element ( copy_array_element ( base . type ) ) ;
2015-11-27 18:32:34 +00:00
if ( ! types_coercible ( product , element ) ) {
2016-05-21 05:09:06 +00:00
raise < < maybe ( get ( Recipe , r ) . name ) < < " 'index' on ' " < < base . original_string < < " ' can't be saved in ' " < < product . original_string < < " '; type should be ' " < < names_to_string_without_quotes ( element . type ) < < " ' \n " < < end ( ) ;
2015-10-06 00:02:32 +00:00
break ;
}
2015-10-01 23:25:21 +00:00
break ;
}
: ( before " End Primitive Recipe Implementations " )
case INDEX : {
2016-05-06 07:46:39 +00:00
reagent /*copy*/ base = current_instruction ( ) . ingredients . at ( 0 ) ;
2016-04-24 07:36:30 +00:00
// Update INDEX base in Run
2016-03-14 03:26:47 +00:00
int base_address = base . value ;
2016-01-21 04:47:54 +00:00
trace ( 9998 , " run " ) < < " base address is " < < base_address < < end ( ) ;
2015-08-01 00:06:38 +00:00
if ( base_address = = 0 ) {
2017-05-26 23:43:18 +00:00
raise < < maybe ( current_recipe_name ( ) ) < < " tried to access location 0 in ' " < < to_original_string ( current_instruction ( ) ) < < " ' \n " < < end ( ) ;
2015-07-24 08:14:49 +00:00
break ;
}
2016-05-06 07:46:39 +00:00
reagent /*copy*/ index = current_instruction ( ) . ingredients . at ( 1 ) ;
2016-04-24 07:36:30 +00:00
// Update INDEX index in Run
vector < double > index_val ( read_memory ( index ) ) ;
if ( index_val . at ( 0 ) < 0 | | index_val . at ( 0 ) > = get_or_insert ( Memory , base_address ) ) {
2017-05-26 23:43:18 +00:00
raise < < maybe ( current_recipe_name ( ) ) < < " invalid index " < < no_scientific ( index_val . at ( 0 ) ) < < " in ' " < < to_original_string ( current_instruction ( ) ) < < " ' \n " < < end ( ) ;
2015-07-17 21:30:17 +00:00
break ;
}
2018-03-14 07:59:41 +00:00
reagent /*local*/ element ( copy_array_element ( base . type ) ) ;
2016-11-07 17:59:51 +00:00
element . set_value ( base_address + /*skip length*/ 1 + index_val . at ( 0 ) * size_of ( element . type ) ) ;
trace ( 9998 , " run " ) < < " address to copy is " < < element . value < < end ( ) ;
trace ( 9998 , " run " ) < < " its type is " < < to_string ( element . type ) < < end ( ) ;
2016-04-20 16:58:07 +00:00
// Read element
products . push_back ( read_memory ( element ) ) ;
2015-02-22 09:32:24 +00:00
break ;
}
2015-03-27 04:47:29 +00:00
: ( code )
2016-05-03 06:11:33 +00:00
type_tree * copy_array_element ( const type_tree * type ) {
2016-11-07 17:59:51 +00:00
return new type_tree ( * array_element ( type ) ) ;
}
type_tree * array_element ( const type_tree * type ) {
2016-05-03 06:11:33 +00:00
assert ( type - > right ) ;
2016-11-11 05:39:02 +00:00
if ( type - > right - > atom ) {
return type - > right ;
}
else if ( ! type - > right - > right ) {
return type - > right - > left ;
}
// hack: support array:num:3 without requiring extra parens
else if ( type - > right - > right - > left & & type - > right - > right - > left - > atom & & is_integer ( type - > right - > right - > left - > name ) ) {
assert ( ! type - > right - > right - > right ) ;
2016-11-07 17:59:51 +00:00
return type - > right - > left ;
2016-11-11 05:39:02 +00:00
}
2016-11-07 17:59:51 +00:00
return type - > right ;
2015-03-27 04:47:29 +00:00
}
2016-04-13 16:03:06 +00:00
int array_length ( const reagent & x ) {
2016-11-07 18:41:24 +00:00
// x should already be canonized.
2016-11-11 05:39:02 +00:00
// hack: look for length in type
if ( ! x . type - > atom & & x . type - > right & & ! x . type - > right - > atom & & x . type - > right - > right & & ! x . type - > right - > right - > atom & & ! x . type - > right - > right - > right // exactly 3 types
& & x . type - > right - > right - > left & & x . type - > right - > right - > left - > atom & & is_integer ( x . type - > right - > right - > left - > name ) ) { // third 'type' is a number
2016-05-04 20:57:32 +00:00
// get size from type
2016-11-11 05:39:02 +00:00
return to_integer ( x . type - > right - > right - > left - > name ) ;
2016-04-13 16:03:06 +00:00
}
2016-11-11 05:39:02 +00:00
// this should never happen at transform time
2016-04-13 16:03:06 +00:00
return get_or_insert ( Memory , x . value ) ;
}
2016-11-07 18:41:24 +00:00
: ( before " End Unit Tests " )
2016-05-04 20:57:32 +00:00
void test_array_length_compound ( ) {
put ( Memory , 1 , 3 ) ;
put ( Memory , 2 , 14 ) ;
put ( Memory , 3 , 15 ) ;
put ( Memory , 4 , 16 ) ;
2018-06-24 16:16:17 +00:00
reagent x ( " 1:array:address:num " ) ; // 3 types, but not a static array
2016-05-04 20:57:32 +00:00
populate_value ( x ) ;
CHECK_EQ ( array_length ( x ) , 3 ) ;
}
2016-11-11 05:39:02 +00:00
void test_array_length_static ( ) {
reagent x ( " 1:array:num:3 " ) ;
CHECK_EQ ( array_length ( x ) , 3 ) ;
}
2016-11-06 04:35:25 +00:00
: ( scenario index_truncates )
def main [
1 : array : num : 3 < - create - array
2 : num < - copy 14
3 : num < - copy 15
4 : num < - copy 16
2018-06-24 16:16:17 +00:00
10 : num < - index 1 : array : num : 3 , 1.5 # non - whole number
2016-11-06 04:35:25 +00:00
]
# fraction is truncated away
2018-06-24 16:16:17 +00:00
+ mem : storing 15 in location 10
2016-11-06 04:35:25 +00:00
2015-07-17 21:30:17 +00:00
: ( scenario index_out_of_bounds )
2015-10-07 05:15:45 +00:00
% Hide_errors = true ;
2016-03-08 09:30:14 +00:00
def main [
2018-06-24 16:16:17 +00:00
1 : array : point : 3 < - create - array
index 1 : array : point : 3 , 4 # less than size of array in locations , but larger than its length in elements
2015-07-17 21:30:17 +00:00
]
2018-06-24 16:16:17 +00:00
+ error : main : invalid index 4 in ' index 1 : array : point : 3 , 4 '
2015-07-17 21:30:17 +00:00
2015-08-09 19:26:31 +00:00
: ( scenario index_out_of_bounds_2 )
2015-10-07 05:15:45 +00:00
% Hide_errors = true ;
2016-03-08 09:30:14 +00:00
def main [
2018-06-24 16:16:17 +00:00
1 : array : num : 3 < - create - array
index 1 : array : num , - 1
2015-07-17 21:30:17 +00:00
]
2018-06-24 16:16:17 +00:00
+ error : main : invalid index - 1 in ' index 1 : array : num , - 1 '
2015-02-22 09:32:24 +00:00
2015-10-06 00:02:32 +00:00
: ( scenario index_product_type_mismatch )
2015-10-07 05:15:45 +00:00
% Hide_errors = true ;
2016-03-08 09:30:14 +00:00
def main [
2015-10-06 00:02:32 +00:00
1 : array : point : 3 < - create - array
2018-06-24 16:16:17 +00:00
10 : num < - index 1 : array : point , 0
2015-10-06 00:02:32 +00:00
]
2018-06-24 16:16:17 +00:00
+ error : main : ' index ' on ' 1 : array : point ' can ' t be saved in ' 10 : num ' ; type should be ' point '
2015-10-06 00:02:32 +00:00
2015-11-16 02:36:34 +00:00
//: we might want to call 'index' without saving the results, say in a sandbox
: ( scenario index_without_product )
2016-03-08 09:30:14 +00:00
def main [
2016-09-17 07:43:13 +00:00
1 : array : num : 3 < - create - array
index 1 : array : num : 3 , 0
2015-11-16 02:36:34 +00:00
]
# just don't die
2016-04-23 21:51:20 +00:00
//:: To write to elements of arrays, use 'put'.
2015-04-17 17:31:17 +00:00
2016-04-23 21:51:20 +00:00
: ( scenario put_index )
2016-03-08 09:30:14 +00:00
def main [
2016-09-17 07:43:13 +00:00
1 : array : num : 3 < - create - array
1 : array : num < - put - index 1 : array : num , 1 , 34
2015-04-17 17:31:17 +00:00
]
2016-04-23 21:51:20 +00:00
+ mem : storing 34 in location 3
2015-04-17 17:31:17 +00:00
: ( before " End Primitive Recipe Declarations " )
2016-04-23 21:51:20 +00:00
PUT_INDEX ,
2015-02-22 09:32:24 +00:00
: ( before " End Primitive Recipe Numbers " )
2016-04-23 21:51:20 +00:00
put ( Recipe_ordinal , " put-index " , PUT_INDEX ) ;
2015-10-01 23:25:21 +00:00
: ( before " End Primitive Recipe Checks " )
2016-04-23 21:51:20 +00:00
case PUT_INDEX : {
if ( SIZE ( inst . ingredients ) ! = 3 ) {
2017-05-26 23:43:18 +00:00
raise < < maybe ( get ( Recipe , r ) . name ) < < " 'put-index' expects exactly 3 ingredients in ' " < < to_original_string ( inst ) < < " ' \n " < < end ( ) ;
2015-07-25 21:19:28 +00:00
break ;
}
2016-05-06 07:46:39 +00:00
reagent /*copy*/ base = inst . ingredients . at ( 0 ) ;
2016-04-24 07:36:30 +00:00
// Update PUT_INDEX base in Check
2015-08-13 23:32:36 +00:00
if ( ! is_mu_array ( base ) ) {
2016-05-21 05:09:06 +00:00
raise < < maybe ( get ( Recipe , r ) . name ) < < " 'put-index' on a non-array ' " < < base . original_string < < " ' \n " < < end ( ) ;
2015-07-24 08:14:49 +00:00
break ;
}
2016-05-06 07:46:39 +00:00
reagent /*copy*/ index = inst . ingredients . at ( 1 ) ;
2016-04-24 07:36:30 +00:00
// Update PUT_INDEX index in Check
if ( ! is_mu_number ( index ) ) {
2016-05-21 05:09:06 +00:00
raise < < maybe ( get ( Recipe , r ) . name ) < < " second ingredient of 'put-index' should have type 'number', but got ' " < < inst . ingredients . at ( 1 ) . original_string < < " ' \n " < < end ( ) ;
2016-04-23 21:51:20 +00:00
break ;
}
2016-05-06 07:46:39 +00:00
reagent /*copy*/ value = inst . ingredients . at ( 2 ) ;
2016-04-24 07:36:30 +00:00
// Update PUT_INDEX value in Check
2018-03-14 07:59:41 +00:00
reagent /*local*/ element ( copy_array_element ( base . type ) ) ;
2016-04-23 21:51:20 +00:00
if ( ! types_coercible ( element , value ) ) {
2016-05-21 05:09:06 +00:00
raise < < maybe ( get ( Recipe , r ) . name ) < < " 'put-index " < < base . original_string < < " , " < < inst . ingredients . at ( 1 ) . original_string < < " ' should store " < < names_to_string_without_quotes ( element . type ) < < " but ' " < < value . name < < " ' has type " < < names_to_string_without_quotes ( value . type ) < < ' \n ' < < end ( ) ;
2015-10-06 00:02:32 +00:00
break ;
}
2016-07-21 18:56:27 +00:00
if ( inst . products . empty ( ) ) break ; // no more checks necessary
if ( inst . products . at ( 0 ) . name ! = inst . ingredients . at ( 0 ) . name ) {
2016-05-21 06:45:03 +00:00
raise < < maybe ( get ( Recipe , r ) . name ) < < " product of 'put-index' must be first ingredient ' " < < inst . ingredients . at ( 0 ) . original_string < < " ', but got ' " < < inst . products . at ( 0 ) . original_string < < " ' \n " < < end ( ) ;
break ;
}
2016-07-21 18:56:27 +00:00
// End PUT_INDEX Product Checks
2015-10-01 23:25:21 +00:00
break ;
}
: ( before " End Primitive Recipe Implementations " )
2016-04-23 21:51:20 +00:00
case PUT_INDEX : {
2016-05-06 07:46:39 +00:00
reagent /*copy*/ base = current_instruction ( ) . ingredients . at ( 0 ) ;
2016-04-24 07:36:30 +00:00
// Update PUT_INDEX base in Run
2016-03-14 03:26:47 +00:00
int base_address = base . value ;
2015-08-01 00:06:38 +00:00
if ( base_address = = 0 ) {
2017-05-26 23:43:18 +00:00
raise < < maybe ( current_recipe_name ( ) ) < < " tried to access location 0 in ' " < < to_original_string ( current_instruction ( ) ) < < " ' \n " < < end ( ) ;
2015-08-01 00:06:38 +00:00
break ;
}
2016-05-06 07:46:39 +00:00
reagent /*copy*/ index = current_instruction ( ) . ingredients . at ( 1 ) ;
2016-04-24 07:36:30 +00:00
// Update PUT_INDEX index in Run
vector < double > index_val ( read_memory ( index ) ) ;
if ( index_val . at ( 0 ) < 0 | | index_val . at ( 0 ) > = get_or_insert ( Memory , base_address ) ) {
2017-05-26 23:43:18 +00:00
raise < < maybe ( current_recipe_name ( ) ) < < " invalid index " < < no_scientific ( index_val . at ( 0 ) ) < < " in ' " < < to_original_string ( current_instruction ( ) ) < < " ' \n " < < end ( ) ;
2015-07-17 21:30:17 +00:00
break ;
}
2016-11-07 17:59:51 +00:00
int address = base_address + /*skip length*/ 1 + index_val . at ( 0 ) * size_of ( array_element ( base . type ) ) ;
2016-04-23 21:51:20 +00:00
trace ( 9998 , " run " ) < < " address to copy to is " < < address < < end ( ) ;
// optimization: directly write the element rather than updating 'product'
// and writing the entire array
2017-05-28 21:28:07 +00:00
write_products = false ;
2016-04-23 21:51:20 +00:00
vector < double > value = read_memory ( current_instruction ( ) . ingredients . at ( 2 ) ) ;
2016-05-04 00:38:33 +00:00
// Write Memory in PUT_INDEX in Run
2016-10-20 05:10:35 +00:00
for ( int i = 0 ; i < SIZE ( value ) ; + + i ) {
2017-11-03 08:50:46 +00:00
trace ( " mem " ) < < " storing " < < no_scientific ( value . at ( i ) ) < < " in location " < < address + i < < end ( ) ;
2016-04-23 21:51:20 +00:00
put ( Memory , address + i , value . at ( i ) ) ;
}
2017-05-28 21:28:07 +00:00
break ;
2015-02-22 09:32:24 +00:00
}
2015-05-26 22:16:36 +00:00
2016-04-23 21:51:20 +00:00
: ( scenario put_index_out_of_bounds )
2015-10-07 05:15:45 +00:00
% Hide_errors = true ;
2016-03-08 09:30:14 +00:00
def main [
2015-08-14 23:58:59 +00:00
1 : array : point : 3 < - create - array
2016-04-23 21:51:20 +00:00
8 : point < - merge 34 , 35
1 : array : point < - put - index 1 : array : point , 4 , 8 : point # ' 4 ' is less than size of array in locations , but larger than its length in elements
2015-07-17 21:30:17 +00:00
]
2016-08-18 14:59:41 +00:00
+ error : main : invalid index 4 in ' 1 : array : point < - put - index 1 : array : point , 4 , 8 : point '
2015-07-17 21:30:17 +00:00
2016-04-23 21:51:20 +00:00
: ( scenario put_index_out_of_bounds_2 )
2015-10-07 05:15:45 +00:00
% Hide_errors = true ;
2016-03-08 09:30:14 +00:00
def main [
2015-08-14 23:58:59 +00:00
1 : array : point : 3 < - create - array
2018-06-24 16:16:17 +00:00
10 : point < - merge 34 , 35
1 : array : point < - put - index 1 : array : point , - 1 , 10 : point
2015-07-17 21:30:17 +00:00
]
2018-06-24 16:16:17 +00:00
+ error : main : invalid index - 1 in ' 1 : array : point < - put - index 1 : array : point , - 1 , 10 : point '
2015-07-17 21:30:17 +00:00
2016-05-21 06:45:03 +00:00
: ( scenario put_index_product_error )
% Hide_errors = true ;
def main [
2016-09-17 07:43:13 +00:00
1 : array : num : 3 < - create - array
4 : array : num : 3 < - put - index 1 : array : num : 3 , 0 , 34
2016-05-21 06:45:03 +00:00
]
2016-09-17 07:43:13 +00:00
+ error : main : product of ' put - index ' must be first ingredient ' 1 : array : num : 3 ' , but got ' 4 : array : num : 3 '
2016-05-21 06:45:03 +00:00
2015-05-26 22:16:36 +00:00
//:: compute the length of an array
: ( scenario array_length )
2016-03-08 09:30:14 +00:00
def main [
2016-09-17 07:43:13 +00:00
1 : array : num : 3 < - create - array
2018-06-24 16:16:17 +00:00
10 : num < - length 1 : array : num
2015-05-26 22:16:36 +00:00
]
2018-06-24 16:16:17 +00:00
+ mem : storing 3 in location 10
2015-05-26 22:16:36 +00:00
: ( before " End Primitive Recipe Declarations " )
LENGTH ,
: ( before " End Primitive Recipe Numbers " )
2015-11-06 19:06:58 +00:00
put ( Recipe_ordinal , " length " , LENGTH ) ;
2015-10-01 23:25:21 +00:00
: ( before " End Primitive Recipe Checks " )
2015-05-26 22:16:36 +00:00
case LENGTH : {
2015-10-01 23:25:21 +00:00
if ( SIZE ( inst . ingredients ) ! = 1 ) {
2017-05-26 23:43:18 +00:00
raise < < maybe ( get ( Recipe , r ) . name ) < < " 'length' expects exactly 2 ingredients in ' " < < to_original_string ( inst ) < < " ' \n " < < end ( ) ;
2015-07-25 21:19:28 +00:00
break ;
}
2016-05-06 07:46:39 +00:00
reagent /*copy*/ array = inst . ingredients . at ( 0 ) ;
2016-04-24 07:36:30 +00:00
// Update LENGTH array in Check
if ( ! is_mu_array ( array ) ) {
2016-05-21 05:09:06 +00:00
raise < < " tried to calculate length of non-array ' " < < array . original_string < < " ' \n " < < end ( ) ;
2015-05-26 22:16:36 +00:00
break ;
}
2015-10-01 23:25:21 +00:00
break ;
}
: ( before " End Primitive Recipe Implementations " )
case LENGTH : {
2016-05-06 07:46:39 +00:00
reagent /*copy*/ array = current_instruction ( ) . ingredients . at ( 0 ) ;
2016-04-24 07:36:30 +00:00
// Update LENGTH array in Run
if ( array . value = = 0 ) {
2017-05-26 23:43:18 +00:00
raise < < maybe ( current_recipe_name ( ) ) < < " tried to access location 0 in ' " < < to_original_string ( current_instruction ( ) ) < < " ' \n " < < end ( ) ;
2015-08-01 00:06:38 +00:00
break ;
}
2015-08-03 05:18:19 +00:00
products . resize ( 1 ) ;
2016-04-24 07:36:30 +00:00
products . at ( 0 ) . push_back ( get_or_insert ( Memory , array . value ) ) ;
2015-05-26 22:16:36 +00:00
break ;
}
2015-08-15 04:13:49 +00:00
//: optimization: none of the instructions in this layer use 'ingredients' so
//: stop copying potentially huge arrays into it.
: ( before " End should_copy_ingredients Special-cases " )
recipe_ordinal r = current_instruction ( ) . operation ;
2016-04-23 21:51:20 +00:00
if ( r = = CREATE_ARRAY | | r = = INDEX | | r = = PUT_INDEX | | r = = LENGTH )
2015-08-15 04:13:49 +00:00
return false ;