about summary refs log tree commit diff stats
path: root/archive/1.vm/070table.mu
blob: 21184084d8e0fe70bb1ed204df62f72c9ea96335 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# A table is like an array, except that you can index it with arbitrary types
# and not just non-negative whole numbers.

# incomplete; doesn't handle hash conflicts

scenario table-read-write [
  local-scope
  tab:&:table:num:num <- new-table 30
  run [
    put-index tab, 12, 34
    60:num/raw, 61:bool/raw <- index tab, 12
  ]
  memory-should-contain [
    60 <- 34
    61 <- 1  # found
  ]
]

scenario table-read-write-non-integer [
  local-scope
  tab:&:table:text:num <- new-table 30
  run [
    put-index tab, [abc def], 34
    1:num/raw, 2:bool/raw <- index tab, [abc def]
  ]
  memory-should-contain [
    1 <- 34
    2 <- 1  # found
  ]
]

scenario table-read-not-found [
  local-scope
  tab:&:table:text:num <- new-table 30
  run [
    1:num/raw, 2:bool/raw <- index tab, [abc def]
  ]
  memory-should-contain [
    1 <- 0
    2 <- 0  # not found
  ]
]

container table:_key:_value [
  length:num
  capacity:num
  data:&:@:table-row:_key:_value
]

container table-row:_key:_value [
  occupied?:bool
  key:_key
  value:_value
]

def new-table capacity:num -> result:&:table:_key:_value [
  local-scope
  load-inputs
  result <- new {(table _key _value): type}
  data:&:@:table-row:_key:_value <- new {(table-row _key _value): type}, capacity
  *result <- merge 0/length, capacity, data
]

# todo: tag results as /required so that call-sites are forbidden from ignoring them
# then we could handle conflicts simply by resizing the table
def put-index table:&:table:_key:_value, key:_key, value:_value -> table:&:table:_key:_value [
  local-scope
  load-inputs
  hash:num <- hash key
  hash <- abs hash
  capacity:num <- get *table, capacity:offset
  _, hash-key:num <- divide-with-remainder hash, capacity
  hash-key <- abs hash-key  # in case hash overflows from a double into a negative integer inside 'divide-with-remainder' above
  table-data:&:@:table-row:_key:_value <- get *table, data:offset
  x:table-row:_key:_value <- index *table-data, hash-key
  occupied?:bool <- get x, occupied?:offset
  not-occupied?:bool <- not occupied?:bool
  assert not-occupied?, [can't handle collisions yet]
  new-row:table-row:_key:_value <- merge true, key, value
  *table-data <- put-index *table-data, hash-key, new-row
]

def index table:&:table:_key:_value, key:_key -> result:_value, found?:bool [
  local-scope
  load-inputs
  hash:num <- hash key
  hash <- abs hash
  capacity:num <- get *table, capacity:offset
  _, hash-key:num <- divide-with-remainder hash, capacity
  hash-key <- abs hash-key  # in case hash overflows from a double into a negative integer inside 'divide-with-remainder' above
  table-data:&:@:table-row:_key:_value <- get *table, data:offset
  x:table-row:_key:_value <- index *table-data, hash-key
  empty:&:_value <- new _value:type
  result <- copy *empty
  found?:bool <- get x, occupied?:offset
  return-unless found?
  key2:_key <- get x, key:offset
  found?:bool <- equal key, key2
  return-unless found?
  result <- get x, value:offset
]

def abs n:num -> result:num [
  local-scope
  load-inputs
  positive?:bool <- greater-or-equal n, 0
  return-if positive?, n
  result <- multiply n, -1
]
pan class="w"> << maybe(recipe_name) << "missing type for '" << x.original_string << "' in '" << inst.original_string << "'\n" << end(); return true; } if (is_raw(x)) return true; if (is_literal(x)) return true; // End is_disqualified Cases if (x.initialized) return true; return false; } bool already_transformed(const reagent& r, const map<string, int>& names) { return contains_key(names, r.name); } int lookup_name(const reagent& r, const recipe_ordinal default_recipe) { return Name[default_recipe][r.name]; } type_ordinal skip_addresses(type_tree* type) { type_ordinal address = get(Type_ordinal, "address"); for (; type; type = type->right) { if (type->value != address) return type->value; } return -1; } int find_element_name(const type_ordinal t, const string& name, const string& recipe_name) { const type_info& container = get(Type, t); for (int i = 0; i < SIZE(container.elements); ++i) if (container.elements.at(i).name == name) return i; raise << maybe(recipe_name) << "unknown element '" << name << "' in container '" << get(Type, t).name << "'\n" << end(); return -1; } bool is_numeric_location(const reagent& x) { if (is_literal(x)) return false; if (is_raw(x)) return false; if (x.name == "0") return false; // used for chaining lexical scopes return is_integer(x.name); } bool is_named_location(const reagent& x) { if (is_literal(x)) return false; if (is_raw(x)) return false; if (is_special_name(x.name)) return false; return !is_integer(x.name); } // all names here should either be disqualified or also in bind_special_scenario_names bool is_special_name(const string& s) { if (s == "_") return true; if (s == "0") return true; // End is_special_name Cases return false; } :(scenario transform_names_supports_containers) def main [ x:point <- merge 34, 35 y:number <- copy 3 ] +name: assign x 1 # skip location 2 because x occupies two locations +name: assign y 3 :(scenario transform_names_supports_static_arrays) def main [ x:array:number:3 <- create-array y:number <- copy 3 ] +name: assign x 1 # skip locations 2, 3, 4 because x occupies four locations +name: assign y 5 :(scenario transform_names_passes_dummy) # _ is just a dummy result that never gets consumed def main [ _, x:number <- copy 0, 1 ] +name: assign x 1 -name: assign _ 1 //: an escape hatch to suppress name conversion that we'll use later :(scenarios run) :(scenario transform_names_passes_raw) % Hide_errors = true; def main [ x:number/raw <- copy 0 ] -name: assign x 1 +error: can't write to location 0 in 'x:number/raw <- copy 0' :(scenarios transform) :(scenario transform_names_fails_when_mixing_names_and_numeric_locations) % Hide_errors = true; def main [ x:number <- copy 1:number ] +error: main: mixing variable names and numeric addresses :(scenario transform_names_fails_when_mixing_names_and_numeric_locations_2) % Hide_errors = true; def main [ x:number <- copy 1 1:number <- copy x:number ] +error: main: mixing variable names and numeric addresses :(scenario transform_names_does_not_fail_when_mixing_names_and_raw_locations) def main [ x:number <- copy 1:number/raw ] -error: main: mixing variable names and numeric addresses $error: 0 :(scenario transform_names_does_not_fail_when_mixing_names_and_literals) def main [ x:number <- copy 1 ] -error: main: mixing variable names and numeric addresses $error: 0 //:: Support element names for containers in 'get' and 'get-location' and 'put'. //: (get-location is implemented later) :(scenario transform_names_transforms_container_elements) def main [ p:address:point <- copy 0 a:number <- get *p:address:point, y:offset b:number <- get *p:address:point, x:offset ] +name: element y of type point is at offset 1 +name: element x of type point is at offset 0 :(before "End transform_names(inst) Special-cases") // replace element names of containers with offsets if (inst.name == "get" || inst.name == "get-location" || inst.name == "put") { //: avoid raising any errors here; later layers will support overloading new //: instructions with the same names (static dispatch), which could lead to //: spurious errors if (SIZE(inst.ingredients) < 2) break; // error raised elsewhere if (!is_literal(inst.ingredients.at(1))) break; // error raised elsewhere if (inst.ingredients.at(1).name.find_first_not_of("0123456789") != string::npos) { // since first non-address in base type must be a container, we don't have to canonize type_ordinal base_type = skip_addresses(inst.ingredients.at(0).type); if (base_type == -1) break; // error raised elsewhere if (contains_key(Type, base_type)) { // otherwise we'll raise an error elsewhere inst.ingredients.at(1).set_value(find_element_name(base_type, inst.ingredients.at(1).name, get(Recipe, r).name)); trace(9993, "name") << "element " << inst.ingredients.at(1).name << " of type " << get(Type, base_type).name << " is at offset " << no_scientific(inst.ingredients.at(1).value) << end(); } } } //: this test is actually illegal so can't call run :(scenarios transform) :(scenario transform_names_handles_containers) def main [ a:point <- copy 0/unsafe b:number <- copy 0/unsafe ] +name: assign a 1 +name: assign b 3 //:: Support variant names for exclusive containers in 'maybe-convert'. :(scenarios run) :(scenario transform_names_handles_exclusive_containers) def main [ 12:number <- copy 1 13:number <- copy 35 14:number <- copy 36 20:point, 22:boolean <- maybe-convert 12:number-or-point/unsafe, p:variant ] +name: variant p of type number-or-point has tag 1 +mem: storing 1 in location 22 +mem: storing 35 in location 20 +mem: storing 36 in location 21 :(before "End transform_names(inst) Special-cases") // convert variant names of exclusive containers if (inst.name == "maybe-convert") { if (SIZE(inst.ingredients) != 2) { raise << maybe(get(Recipe, r).name) << "exactly 2 ingredients expected in '" << inst.original_string << "'\n" << end(); break; } assert(is_literal(inst.ingredients.at(1))); if (inst.ingredients.at(1).name.find_first_not_of("0123456789") != string::npos) { // since first non-address in base type must be an exclusive container, we don't have to canonize type_ordinal base_type = skip_addresses(inst.ingredients.at(0).type); if (base_type == -1) raise << maybe(get(Recipe, r).name) << "expected an exclusive-container in '" << inst.original_string << "'\n" << end(); if (contains_key(Type, base_type)) { // otherwise we'll raise an error elsewhere inst.ingredients.at(1).set_value(find_element_name(base_type, inst.ingredients.at(1).name, get(Recipe, r).name)); trace(9993, "name") << "variant " << inst.ingredients.at(1).name << " of type " << get(Type, base_type).name << " has tag " << no_scientific(inst.ingredients.at(1).value) << end(); } } }