Skip to content

Commit

Permalink
Reorganize definitions; remove Object struct;
Browse files Browse the repository at this point in the history
  • Loading branch information
Peter Suciu committed Nov 23, 2023
1 parent 22d37d0 commit a9d76b8
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 90 deletions.
82 changes: 21 additions & 61 deletions src/convert.nr
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ use crate::globals::CHAR_t;

use crate::JSON;
use crate::Object;
use crate::Property;

trait FieldConversion
{
Expand All @@ -32,28 +31,10 @@ trait ByteArrayConversions
fn as_field(self) -> Option<Field>;
fn as_string(self) -> [u8];
fn as_list(self) -> [[u8]];
fn as_object(self, parent : JSON) -> Object;
fn as_object(self, parent : JSON) -> JSON;
fn as_json(self) -> JSON;
}

trait PropertyLookup
{
fn get<N> (self, key : str<N>) -> [u8];

fn linear_search<N>(self, key : [u8; N], index : Field) -> [u8];
fn binary_search<N>(self, key : [u8; N], left : Field, right : Field) -> [u8];
}

trait PropertyConversions
{
fn get<N> (self, key : str<N>) -> [u8] { self.doc.get(key) }
fn get_bool<N> (self, key : str<N>) -> Option<bool> { self.doc.get(key).as_bool() }
fn get_field<N> (self, key : str<N>) -> Option<Field> { self.doc.get(key).as_field() }
fn get_string<N>(self, key : str<N>) -> [u8] { self.doc.get(key).as_string() }
fn get_array<N> (self, key : str<N>) -> [[u8]] { self.doc.get(key).as_list() }
fn get_object<N>(self, key : str<N>) -> Object { self.doc.get(key).as_object(self) }
}

impl<N> FieldConversion for [u8; N]
{
unconstrained
Expand Down Expand Up @@ -133,7 +114,7 @@ impl<N> ByteArrayConversions for [u8; N]
unconstrained
pub fn as_bool(self) -> Option<bool>
{
let bytes : [u8] = self.parse_string();
let bytes : [u8] = self.parse_value();

if (bytes.len() == 0) { Option::none() }
else
Expand All @@ -147,7 +128,7 @@ impl<N> ByteArrayConversions for [u8; N]
unconstrained
pub fn as_field(self) -> Option<Field>
{
let bytes : [u8] = self.parse_string();
let bytes : [u8] = self.parse_value();

let mut offsets : [Field; 5] = bytes.get_offsets();

Expand Down Expand Up @@ -200,7 +181,7 @@ impl<N> ByteArrayConversions for [u8; N]
unconstrained
pub fn as_list(self) -> [[u8]]
{
let bytes : [u8] = self.parse_string();
let bytes : [u8] = self.parse_value();

let mut size : Field = bytes.len();
if (size == 0) | (size == 1) { [] }
Expand Down Expand Up @@ -235,17 +216,16 @@ impl<N> ByteArrayConversions for [u8; N]
}

unconstrained
pub fn as_object(self, parent : JSON) -> Object
pub fn as_object(self, parent : JSON) -> JSON
{
let mut result : Object = Object::none();
let mut result : JSON = JSON::none();

let size : Field = self.len();
if (size != 0)
{
if (self[0] == QUOTATION_MARK)
{
let json = self.parse(&mut 1, (self.len() - 1), -1);
if (!json.is_none()) { result.doc = json.doc; }
result = self.parse(&mut 1, (self.len() - 1), -1);
}
else
{
Expand All @@ -254,7 +234,7 @@ impl<N> ByteArrayConversions for [u8; N]
if (OK & (size == 3))
{
let index : u8 = self[1];
if (index < (parent.children.len() as u8)) { result = Object { parent, doc: parent.children[index] }; }
if (index < (parent.children.len() as u8)) { result = parent.child(index); }
}
else if (OK & (size != 1)) { result.doc = [crate::Property::none()]; }
}
Expand All @@ -279,58 +259,38 @@ impl<N> ByteArrayConversions for [u8; N]
impl<N> ByteArrayConversions for str<N>
{
unconstrained
pub fn as_bool(string : Self) -> Option<bool> { string.as_bytes().as_bool() }
pub fn as_bool(self) -> Option<bool> { self.as_bytes().as_bool() }

unconstrained
pub fn as_field(string : Self) -> Option<Field> { string.as_bytes().as_field() }
pub fn as_field(self) -> Option<Field> { self.as_bytes().as_field() }

unconstrained
pub fn as_list(string : Self) -> [[u8]] { string.as_bytes().as_list() }
pub fn as_list(self) -> [[u8]] { self.as_bytes().as_list() }

unconstrained
pub fn as_string(string : Self) -> [u8] { string.as_bytes().as_string() }
pub fn as_string(self) -> [u8] { self.as_bytes().as_slice() }

unconstrained
pub fn as_object(string : Self, parent : JSON) -> Object { string.as_bytes().as_object(parent) }
pub fn as_object(self, parent : JSON) -> JSON { self.as_bytes().as_object(parent) }

unconstrained
pub fn as_json(string : Self) -> JSON { string.as_bytes().as_json() }
pub fn as_json(self) -> JSON { self.as_bytes().as_json() }
}

impl PropertyLookup for [Property]
impl JSON
{
unconstrained
pub fn get<N> (self, key : str<N>) -> [u8] { self.linear_search(key.as_bytes(), (self.len() - 1)) }
// pub fn get<N> (self, key : str<N>) -> [u8] { self.binary_search(key.as_bytes(), 0, (self.len() - 1)) }
pub fn get_bool<N> (self, key : str<N>) -> Option<bool> { self.doc.get(key).as_bool() }

unconstrained
pub fn linear_search<N>(self, key : [u8; N], index : Field) -> [u8]
{
if (index == -1) { [] }
else if (key.eq(self[index].key)) { self[index].value }
else { self.linear_search(key, (index - 1)) }
}
pub fn get_field<N> (self, key : str<N>) -> Option<Field> { self.doc.get(key).as_field() }

unconstrained
pub fn binary_search<N>(self, key : [u8; N], left : Field, right : Field) -> [u8]
{
if (left == (right + 1)) { [] }
else
{
let middle : Field = ((((left + right) as u64) / 2) as Field);
let (less_than, equal) = key.less_than_or_eq(self[middle].key);
pub fn get_string<N>(self, key : str<N>) -> [u8] { self.doc.get(key).as_string() }

if (equal) { self[middle].value }
else if (less_than) { self.binary_search(key, left, (middle - 1)) }
else { self.binary_search(key, (middle + 1), right) }
}
}
}
unconstrained
pub fn get_array<N> (self, key : str<N>) -> [[u8]] { self.doc.get(key).as_list() }

impl PropertyConversions for Object
{
unconstrained
pub fn get_object<N>(self, key : str<N>) -> Object { self.doc.get(key).as_object(self.parent) }
pub fn get_object<N>(self, key : str<N>) -> JSON { self.doc.get(key).as_object(self) }
}

impl PropertyConversions for JSON { }
34 changes: 15 additions & 19 deletions src/lib.nr
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,20 @@ struct Property
value : [u8],
}

struct Object
{
parent : JSON
, doc : [Property]
}
type Object = [Property];

struct JSON
{
doc : [Property]
, children : [[Property]]
doc : Object
, children : [Object]
}

trait Extensions
{
fn none() -> Self;
fn len(self) -> Field { self.doc.len() }
fn is_none(self) -> bool { self.doc.len() == 0 }
fn is_empty(self) -> bool { self.doc.is_empty() }
fn len(self) -> Field;
fn is_none(self) -> bool;
fn is_empty(self) -> bool;
fn print(self);
}

Expand Down Expand Up @@ -70,7 +66,7 @@ impl Property
}
}

impl Extensions for [Property]
impl Extensions for Object
{
unconstrained
fn none() -> Self { [] }
Expand All @@ -88,19 +84,19 @@ impl Extensions for [Property]
pub fn print(self) { for i in 0..self.len() { self[i].print_index(i); } }
}

impl Extensions for Object
impl Extensions for JSON
{
unconstrained
fn none() -> Self { Self { parent: dep::std::unsafe::zeroed(), doc : [] } }
fn none() -> Self { Self { doc: [], children : [] } }

unconstrained
pub fn is_none(self) -> bool { self.doc.len() == 0 }

unconstrained
pub fn print(self) { self.doc.print(); }
}
pub fn len(self) -> Field { self.doc.len() }

impl Extensions for JSON
{
unconstrained
fn none() -> Self { Self { doc: [], children : [] } }
pub fn is_empty(self) -> bool { self.doc.is_empty() }

unconstrained
pub fn print(self)
Expand All @@ -118,5 +114,5 @@ impl Extensions for JSON
impl JSON
{
unconstrained
pub fn child(self, index : u8) -> Object { Object { parent: self, doc: self.children[index] } }
pub fn child(self, index : u8) -> JSON { JSON { doc: self.children[index], children: self.children } }
}
62 changes: 52 additions & 10 deletions src/parse.nr
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
use crate::JSON;
use crate::Property;

use crate::globals::BEGIN_ARRAY;
use crate::globals::END_ARRAY;
use crate::globals::BEGIN_OBJECT;
Expand All @@ -20,22 +17,40 @@ use crate::globals::POINT;
use crate::globals::CHAR_e;
use crate::globals::CHAR_E;

use crate::JSON;
use crate::Object;
use crate::Property;

global FIELD_t : Field = 0x74;
global FIELD_tr : Field = 0x7472;
global FIELD_tru : Field = 0x747275;
global FIELD_true : Field = 0x74727565;
global FIELD_null : Field = 0x6E756C6C;
global FIELD_false : Field = 0x66616C7365;

trait PropertyLookup
{
fn get<N> (self, key : str<N>) -> [u8];

fn linear_search<N>(self, key : [u8; N], index : Field) -> [u8];
fn binary_search<N>(self, key : [u8; N], begin : Field, end : Field) -> [u8];
}

trait ByteArrayParser
{
fn parse_value(self) -> [u8];
fn parse(self, begin : &mut Field, end : Field, child_index : Field) -> JSON;
}

impl JSON
{
unconstrained
pub fn parse<SIZE>(string : str<SIZE>) -> Self { string.as_bytes().parse(&mut 0, SIZE, -1) }

unconstrained
fn store(self, prop : Property) -> [Property]
fn store(self, prop : Property) -> Object
{
let mut copy : [Property] = [];
let mut copy : Object = [];

// linear insert
for current in self.doc { if (!prop.key.eq(current.key)) { copy = copy.push_back(current); } }
Expand All @@ -60,18 +75,45 @@ impl JSON
// }
// if (inserted) { copy } else { copy.push_back(prop) }
}

unconstrained
pub fn get<N> (self, key : str<N>) -> [u8] { self.doc.get(key) }
}

trait ByteArrayParser
impl PropertyLookup for Object
{
fn parse_string(self) -> [u8];
fn parse(self, begin : &mut Field, end : Field, child_index : Field) -> JSON;
unconstrained
pub fn get<N> (self, key : str<N>) -> [u8] { self.linear_search(key.as_bytes(), (self.len() - 1)) }
// pub fn get<N> (self, key : str<N>) -> [u8] { self.binary_search(key.as_bytes(), 0, (self.len() - 1)) }

unconstrained
pub fn linear_search<N>(self, key : [u8; N], index : Field) -> [u8]
{
if (index == -1) { [] }
else if (key.eq(self[index].key)) { self[index].value }
else { self.linear_search(key, (index - 1)) }
}

unconstrained
pub fn binary_search<N>(self, key : [u8; N], begin : Field, end : Field) -> [u8]
{
if (begin == (end + 1)) { [] }
else
{
let middle : Field = ((((begin + end) as u64) / 2) as Field);
let (less_than, equal) = key.less_than_or_eq(self[middle].key);

if (equal) { self[middle].value }
else if (less_than) { self.binary_search(key, begin, (middle - 1)) }
else { self.binary_search(key, (middle + 1), end) }
}
}
}

impl<N> ByteArrayParser for [u8; N]
{
unconstrained
fn parse_string(self) -> [u8]
pub fn parse_value(self) -> [u8]
{
let mut bytes : [u8] = self.as_slice();

Expand All @@ -80,7 +122,7 @@ impl<N> ByteArrayParser for [u8; N]
if (self[0] == QUOTATION_MARK)
{
let json = self.parse(&mut 1, (self.len() - 1), -1);
if (!json.is_none()) { bytes = json.doc[0].value; }
if (json.doc.len() == 1) { bytes = json.doc[0].value; }
}
}

Expand Down

0 comments on commit a9d76b8

Please sign in to comment.