Skip to content

Commit 3487412

Browse files
more comments, small optimisations
1 parent b7c0f8f commit 3487412

19 files changed

+3143
-3082
lines changed

out.txt

Lines changed: 3 additions & 3 deletions
Large diffs are not rendered by default.

src/enums.nr

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
mod ScanMode {
2+
global GRAMMAR_SCAN = 0;
3+
global STRING_SCAN = 1;
4+
global NUMERIC_SCAN = 2;
5+
global LITERAL_SCAN: Field = 3;
6+
}
7+
8+
mod Token {
9+
global NO_TOKEN = 0;
10+
global BEGIN_OBJECT_TOKEN = 1;
11+
global END_OBJECT_TOKEN = 2;
12+
global BEGIN_ARRAY_TOKEN = 3;
13+
global END_ARRAY_TOKEN: Field = 4;
14+
global KEY_SEPARATOR_TOKEN = 5;
15+
global VALUE_SEPARATOR_TOKEN = 6;
16+
global STRING_TOKEN = 7;
17+
global NUMERIC_TOKEN = 8;
18+
global LITERAL_TOKEN =9;
19+
global KEY_TOKEN = 10;
20+
global NUM_TOKENS = 11;
21+
global NUM_TOKENS_MUL_2 = 22;
22+
}
23+
24+
mod Layer {
25+
global OBJECT_LAYER = 0;
26+
global ARRAY_LAYER = 1;
27+
global SINGLE_VALUE_LAYER = 2;
28+
}
29+

src/get_array.nr

Lines changed: 45 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use crate::json_entry::JSONEntry;
22
use crate::json::JSON;
33
use crate::lt::lt_field_16_bit;
4-
use crate::json_tables::{OBJECT_LAYER, ARRAY_LAYER, BEGIN_ARRAY_TOKEN};
5-
use crate::keyhash::get_keyhash;
4+
use crate::enums::Token::END_ARRAY_TOKEN;
5+
use crate::enums::Layer::{OBJECT_LAYER, ARRAY_LAYER};
66
use crate::getters::JSONValue;
77

88
/**
@@ -14,8 +14,8 @@ impl<let NumBytes: u32, let NumPackedFields: u16, let MaxNumTokens: u16, let Max
1414
* @brief if the root JSON is an array, return its length
1515
**/
1616
fn get_length(self) -> u32 {
17-
assert(self.layer_context == ARRAY_LAYER, "can only get length of an array type");
18-
let parent_entry: JSONEntry = self.packed_json_entries[self.layer_index_in_transcript].into();
17+
assert(self.layer_type_of_root == ARRAY_LAYER, "can only get length of an array type");
18+
let parent_entry: JSONEntry = self.json_entries_packed[self.root_index_in_transcript].into();
1919
parent_entry.num_children as u32
2020
}
2121

@@ -24,18 +24,18 @@ impl<let NumBytes: u32, let NumPackedFields: u16, let MaxNumTokens: u16, let Max
2424
* @description returns an Option<JSON> where, if the array exists, the JSON object will have the requested array as its root value
2525
**/
2626
fn get_array<let KeyBytes: u16>(self, key: [u8; KeyBytes]) -> Option<Self> {
27-
assert(self.layer_context != ARRAY_LAYER, "cannot extract array elements via a key");
27+
assert(self.layer_type_of_root != ARRAY_LAYER, "cannot extract array elements via a key");
2828
let (exists, key_index) = self.key_exists_impl(key, KeyBytes);
29-
let entry: JSONEntry = self.packed_json_entries[key_index].into();
29+
let entry: JSONEntry = self.json_entries_packed[key_index].into();
3030
assert(
31-
(entry.entry_type - BEGIN_ARRAY_TOKEN) * exists as Field == 0, "key does not describe an object"
31+
(entry.entry_type - END_ARRAY_TOKEN) * exists as Field == 0, "key does not describe an object"
3232
);
3333

3434
let mut r = self;
35-
r.layer_id = entry.parent_index;
35+
r.layer_type_of_root = entry.parent_index;
3636
r.root_id = entry.id;
37-
r.layer_context = ARRAY_LAYER;
38-
r.layer_index_in_transcript = key_index;
37+
r.layer_type_of_root = ARRAY_LAYER;
38+
r.root_index_in_transcript = key_index;
3939

4040
Option { _is_some: exists, _value: r }
4141
}
@@ -45,16 +45,16 @@ impl<let NumBytes: u32, let NumPackedFields: u16, let MaxNumTokens: u16, let Max
4545
* @description will revert if the array does not exist
4646
**/
4747
fn get_array_unchecked<let KeyBytes: u16>(self, key: [u8; KeyBytes]) -> Self {
48-
assert(self.layer_context != ARRAY_LAYER, "cannot extract array elements via a key");
48+
assert(self.layer_type_of_root != ARRAY_LAYER, "cannot extract array elements via a key");
4949

5050
let (entry, key_index) = self.get_json_entry_unchecked_with_key_index_var(key, KeyBytes);
51-
assert(entry.entry_type == BEGIN_ARRAY_TOKEN, "key does not describe an object");
51+
assert(entry.entry_type == END_ARRAY_TOKEN, "key does not describe an object");
5252

5353
let mut r = self;
54-
r.layer_id = entry.parent_index;
54+
r.layer_type_of_root = entry.parent_index;
5555
r.root_id = entry.id;
56-
r.layer_context = ARRAY_LAYER;
57-
r.layer_index_in_transcript = key_index;
56+
r.layer_type_of_root = ARRAY_LAYER;
57+
r.root_index_in_transcript = key_index;
5858

5959
r
6060
}
@@ -63,19 +63,19 @@ impl<let NumBytes: u32, let NumPackedFields: u16, let MaxNumTokens: u16, let Max
6363
* @brief same as `get_array` for where the key length may be less than KeyBytes
6464
**/
6565
fn get_array_var<let KeyBytes: u16>(self, key: [u8; KeyBytes], key_length: u16) -> Option<Self> {
66-
assert(self.layer_context != ARRAY_LAYER, "cannot extract array elements via a key");
66+
assert(self.layer_type_of_root != ARRAY_LAYER, "cannot extract array elements via a key");
6767
let (exists, key_index) = self.key_exists_impl(key, key_length);
68-
let entry: JSONEntry = self.packed_json_entries[key_index].into();
68+
let entry: JSONEntry = self.json_entries_packed[key_index].into();
6969
// TODO: ADD A layer_context VARIABLE INTO JSON WHICH DESCRIBES WHETHER WE ARE AN OBJECT, ARRAY OR SINGLE VALUE
7070
assert(
71-
(entry.entry_type - BEGIN_ARRAY_TOKEN) * exists as Field == 0, "key does not describe an object"
71+
(entry.entry_type - END_ARRAY_TOKEN) * exists as Field == 0, "key does not describe an object"
7272
);
7373

7474
let mut r = self;
75-
r.layer_id = entry.parent_index;
75+
r.layer_type_of_root = entry.parent_index;
7676
r.root_id = entry.id;
77-
r.layer_context = ARRAY_LAYER;
78-
r.layer_index_in_transcript = key_index;
77+
r.layer_type_of_root = ARRAY_LAYER;
78+
r.root_index_in_transcript = key_index;
7979

8080
Option { _is_some: exists, _value: r }
8181
}
@@ -84,16 +84,16 @@ impl<let NumBytes: u32, let NumPackedFields: u16, let MaxNumTokens: u16, let Max
8484
* @brief same as `get_array_unchecked` for where the key length may be less than KeyBytes
8585
**/
8686
fn get_array_unchecked_var<let KeyBytes: u16>(self, key: [u8; KeyBytes], key_length: u16) -> Self {
87-
assert(self.layer_context != ARRAY_LAYER, "cannot extract array elements via a key");
87+
assert(self.layer_type_of_root != ARRAY_LAYER, "cannot extract array elements via a key");
8888

8989
let (entry, key_index) = self.get_json_entry_unchecked_with_key_index_var(key, key_length);
90-
assert(entry.entry_type == BEGIN_ARRAY_TOKEN, "key does not describe an object");
90+
assert(entry.entry_type == END_ARRAY_TOKEN, "key does not describe an object");
9191

9292
let mut r = self;
93-
r.layer_id = entry.parent_index;
93+
r.layer_type_of_root = entry.parent_index;
9494
r.root_id = entry.id;
95-
r.layer_context = ARRAY_LAYER;
96-
r.layer_index_in_transcript = key_index;
95+
r.layer_type_of_root = ARRAY_LAYER;
96+
r.root_index_in_transcript = key_index;
9797

9898
r
9999
}
@@ -103,22 +103,22 @@ impl<let NumBytes: u32, let NumPackedFields: u16, let MaxNumTokens: u16, let Max
103103
* @description returns an Option<JSON> where, if the array exists, the JSON object will have the requested array as its root value
104104
**/
105105
fn get_array_from_array(self, array_index: Field) -> Option<Self> {
106-
assert(self.layer_context == ARRAY_LAYER, "can only acceess array elements from array");
106+
assert(self.layer_type_of_root == ARRAY_LAYER, "can only acceess array elements from array");
107107

108-
let parent_entry: JSONEntry = self.packed_json_entries[self.layer_index_in_transcript].into();
108+
let parent_entry: JSONEntry = self.json_entries_packed[self.root_index_in_transcript].into();
109109
let valid = lt_field_16_bit(array_index, parent_entry.num_children);
110110
let entry_index = (parent_entry.child_pointer + array_index) * valid as Field;
111111

112-
let entry: JSONEntry = self.packed_json_entries[entry_index].into();
112+
let entry: JSONEntry = self.json_entries_packed[entry_index].into();
113113
assert(
114-
(entry.entry_type - BEGIN_ARRAY_TOKEN) * valid as Field == 0, "get_object_from_array: entry exists but is not an object!"
114+
(entry.entry_type - END_ARRAY_TOKEN) * valid as Field == 0, "get_object_from_array: entry exists but is not an object!"
115115
);
116116

117117
let mut r = self;
118-
r.layer_id = entry.parent_index;
118+
r.layer_type_of_root = entry.parent_index;
119119
r.root_id = entry.id;
120-
r.layer_context = ARRAY_LAYER;
121-
r.layer_index_in_transcript = entry_index;
120+
r.layer_type_of_root = ARRAY_LAYER;
121+
r.root_index_in_transcript = entry_index;
122122

123123
Option { _is_some: valid, _value: r }
124124
}
@@ -128,40 +128,39 @@ impl<let NumBytes: u32, let NumPackedFields: u16, let MaxNumTokens: u16, let Max
128128
* @description will revert if the array does not exist
129129
**/
130130
fn get_array_from_array_unchecked(self, array_index: Field) -> Self {
131-
assert(self.layer_context == ARRAY_LAYER, "can only acceess array elements from array");
131+
assert(self.layer_type_of_root == ARRAY_LAYER, "can only acceess array elements from array");
132132

133-
let parent_entry: JSONEntry = self.packed_json_entries[self.layer_index_in_transcript].into();
133+
let parent_entry: JSONEntry = self.json_entries_packed[self.root_index_in_transcript].into();
134134
let valid = lt_field_16_bit(array_index, parent_entry.num_children);
135135
assert(valid, "array overflow");
136136
let entry_index = (parent_entry.child_pointer + array_index);
137-
138-
let entry: JSONEntry = self.packed_json_entries[entry_index].into();
137+
let entry: JSONEntry = self.json_entries_packed[entry_index].into();
139138
assert(
140-
entry.entry_type == BEGIN_ARRAY_TOKEN, "get_array_from_array_unchecked: entry exists but is not an array!"
139+
entry.entry_type == END_ARRAY_TOKEN, "get_array_from_array_unchecked: entry exists but is not an array!"
141140
);
142141

143142
let mut r = self;
144-
r.layer_id = entry.parent_index;
143+
r.layer_type_of_root = entry.parent_index;
145144
r.root_id = entry.id;
146-
r.layer_context = ARRAY_LAYER;
147-
r.layer_index_in_transcript = entry_index;
145+
r.layer_type_of_root = ARRAY_LAYER;
146+
r.root_index_in_transcript = entry_index;
148147
r
149148
}
150149

151150
/**
152151
* @brief if the root is an array, map over the array values, applying `fn f` to each value
153152
**/
154153
fn map<U, let MaxElements: u32, let MaxElementBytes: u32>(self, f: fn(JSONValue<MaxElementBytes>) -> U) -> [U; MaxElements] where U: std::default::Default {
155-
assert(self.layer_context == ARRAY_LAYER, "can only call map on an array");
154+
assert(self.layer_type_of_root == ARRAY_LAYER, "can only call map on an array");
156155

157-
let entry: JSONEntry = self.packed_json_entries[self.layer_index_in_transcript].into();
156+
let entry: JSONEntry = self.json_entries_packed[self.root_index_in_transcript].into();
158157
let num_children = entry.num_children;
159158
let mut r: [U; MaxElements] = [U::default(); MaxElements];
160159

161160
for i in 0..MaxElements {
162161
let valid = lt_field_16_bit(i as Field, num_children);
163162
let entry_index = (entry.child_pointer + i as Field) * valid as Field;
164-
let child_entry: JSONEntry = self.packed_json_entries[entry_index].into();
163+
let child_entry: JSONEntry = self.json_entries_packed[entry_index].into();
165164
let mut parsed_string: [u8; MaxElementBytes] = [0; MaxElementBytes];
166165
for j in 0..MaxElementBytes {
167166
let byte_valid = lt_field_16_bit(j as Field, child_entry.json_length);
@@ -187,12 +186,12 @@ impl<let NumBytes: u32, let NumPackedFields: u16, let MaxNumTokens: u16, let Max
187186
fn test_array() {
188187
let text = "{ \"foo\": [ [1,2,3], [[3,4]], [[]], [], { \"bar\": [\"b\", \"a\", \"z\" ]} ]}";
189188

190-
let mut json: JSON<_, 7, 60, 60> = JSON::parse_json(text);
189+
let mut json: JSON<_, 7, 60, 60> = JSON::parse_json_from_string(text);
191190

192191
let first = json.get_array_unchecked("foo".as_bytes());
193192
assert(first.get_length() == 5);
194193

195-
let A = first.get_array_from_array_unchecked(0);
194+
let A: JSON<68, 7, 60, 60> = first.get_array_from_array_unchecked(0);
196195
assert(A.get_length() == 3);
197196

198197
let B = first.get_array_from_array_unchecked(1);
@@ -217,11 +216,6 @@ fn test_array() {
217216

218217
let E = first.get_object_from_array_unchecked(4);
219218

220-
let entry_maybe: JSONEntry = E.packed_json_entries[E.layer_index_in_transcript].into();
221-
println(f"entry = {entry_maybe}");
222-
let child: JSONEntry = E.packed_json_entries[entry_maybe.child_pointer].into();
223-
println(f"target? = {child}");
224-
println(f"{E}");
225219
let E_A = E.get_array_unchecked("bar".as_bytes());
226220
assert(E_A.get_length() == 3);
227221
}

src/get_literal.nr

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use crate::json_entry::JSONEntry;
22
use crate::json::JSON;
33
use crate::lt::lt_field_16_bit;
4-
use crate::json_tables::{OBJECT_LAYER, ARRAY_LAYER, LITERAL_TOKEN};
5-
use crate::keyhash::get_keyhash;
4+
use crate::enums::Layer::{OBJECT_LAYER, ARRAY_LAYER};
5+
use crate::enums::Token::LITERAL_TOKEN;
66
use crate::getters::JSONValue;
77

88
global MAX_LITERAL_LENGTH_AS_STRING = 5;
@@ -72,7 +72,7 @@ impl<let NumBytes: u32, let NumPackedFields: u16, let MaxNumTokens: u16, let Max
7272
* @description returns an Option<JSONLiteral> which will be null if the literal does not exist
7373
**/
7474
fn get_literal<let KeyBytes: u16>(self, key: [u8; KeyBytes]) -> Option<JSONLiteral> {
75-
assert(self.layer_context != ARRAY_LAYER, "cannot extract array elements via a key");
75+
assert(self.layer_type_of_root != ARRAY_LAYER, "cannot extract array elements via a key");
7676

7777
let (exists, entry) = self.get_json_entry(key);
7878
assert(
@@ -88,7 +88,7 @@ impl<let NumBytes: u32, let NumPackedFields: u16, let MaxNumTokens: u16, let Max
8888
* @description will revert if the literal does not exist
8989
**/
9090
fn get_literal_unchecked<let KeyBytes: u16>(self, key: [u8; KeyBytes]) -> JSONLiteral {
91-
assert(self.layer_context != ARRAY_LAYER, "cannot extract array elements via a key");
91+
assert(self.layer_type_of_root != ARRAY_LAYER, "cannot extract array elements via a key");
9292

9393
let entry= self.get_json_entry_unchecked(key);
9494
assert(
@@ -103,7 +103,7 @@ impl<let NumBytes: u32, let NumPackedFields: u16, let MaxNumTokens: u16, let Max
103103
* @brief same as `get_literal` for where the key length may be less than KeyBytes
104104
**/
105105
fn get_literal_var<let KeyBytes: u16>(self, key: [u8; KeyBytes], key_length: u16) -> Option<JSONLiteral> {
106-
assert(self.layer_context != ARRAY_LAYER, "cannot extract array elements via a key");
106+
assert(self.layer_type_of_root != ARRAY_LAYER, "cannot extract array elements via a key");
107107

108108
let (exists, entry) = self.get_json_entry_var(key, key_length);
109109
assert(
@@ -118,7 +118,7 @@ impl<let NumBytes: u32, let NumPackedFields: u16, let MaxNumTokens: u16, let Max
118118
* @brief same as `get_literal_unchecked` for where the key length may be less than KeyBytes
119119
**/
120120
fn get_literal_unchecked_var<let KeyBytes: u16>(self, key: [u8; KeyBytes], key_length: u16) -> JSONLiteral {
121-
assert(self.layer_context != ARRAY_LAYER, "cannot extract array elements via a key");
121+
assert(self.layer_type_of_root != ARRAY_LAYER, "cannot extract array elements via a key");
122122

123123
let entry= self.get_json_entry_unchecked_var(key, key_length);
124124
assert(
@@ -134,14 +134,14 @@ impl<let NumBytes: u32, let NumPackedFields: u16, let MaxNumTokens: u16, let Max
134134
* @description returns an Option<JSONLiteral> which will be null if the literal does not exist
135135
**/
136136
fn get_literal_from_array(self, array_index: Field) -> Option<JSONLiteral> {
137-
assert(self.layer_context == ARRAY_LAYER, "can only acceess array elements from array");
137+
assert(self.layer_type_of_root == ARRAY_LAYER, "can only acceess array elements from array");
138138

139-
let parent_entry : JSONEntry = self.packed_json_entries[self.layer_index_in_transcript].into();
139+
let parent_entry : JSONEntry = self.json_entries_packed[self.root_index_in_transcript].into();
140140

141141
let valid = lt_field_16_bit(array_index, parent_entry.num_children);
142142
let entry_index = (parent_entry.child_pointer + array_index) * valid as Field;
143143

144-
let entry : JSONEntry = self.packed_json_entries[entry_index].into();
144+
let entry : JSONEntry = self.json_entries_packed[entry_index].into();
145145

146146
assert(
147147
(entry.entry_type - LITERAL_TOKEN) * valid as Field == 0, "get_literal_from_array: entry exists but is not a literal!"
@@ -158,15 +158,15 @@ impl<let NumBytes: u32, let NumPackedFields: u16, let MaxNumTokens: u16, let Max
158158
* @description will revert if the literal does not exist
159159
**/
160160
fn get_literal_from_array_unchecked(self, array_index: Field) -> JSONLiteral {
161-
assert(self.layer_context == ARRAY_LAYER, "can only acceess array elements from array");
161+
assert(self.layer_type_of_root == ARRAY_LAYER, "can only acceess array elements from array");
162162

163-
let parent_entry : JSONEntry = self.packed_json_entries[self.layer_index_in_transcript].into();
163+
let parent_entry : JSONEntry = self.json_entries_packed[self.root_index_in_transcript].into();
164164

165165
let valid = lt_field_16_bit(array_index, parent_entry.num_children);
166166
assert(valid, "array overflow");
167167
let entry_index = (parent_entry.child_pointer + array_index);
168168

169-
let entry : JSONEntry = self.packed_json_entries[entry_index].into();
169+
let entry : JSONEntry = self.json_entries_packed[entry_index].into();
170170

171171
assert(
172172
entry.entry_type == LITERAL_TOKEN, "get_literal_from_array_unchecked: entry exists but is not a literal!"

0 commit comments

Comments
 (0)