Skip to content

Commit

Permalink
add Record - wip
Browse files Browse the repository at this point in the history
  • Loading branch information
daddinuz committed Aug 3, 2024
1 parent d9e17af commit ac0b1f9
Show file tree
Hide file tree
Showing 5 changed files with 589 additions and 1 deletion.
12 changes: 12 additions & 0 deletions rat/src/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use crate::decimal::Decimal;
use crate::integer::Integer;
use crate::process::Process;
use crate::quote::Quote;
use crate::record::Record;
use crate::string::String;
use crate::symbol::Symbol;
use crate::verb::Verb;
Expand All @@ -26,6 +27,7 @@ pub enum Expression {
Integer(Integer),
Process(Process),
Quote(Quote),
Record(Record),
String(String),
Symbol(Symbol),
Verb(Verb),
Expand Down Expand Up @@ -69,6 +71,13 @@ impl From<Quote> for Expression {
}
}

impl From<Record> for Expression {
#[inline]
fn from(value: Record) -> Self {
Self::Record(value)
}
}

impl From<String> for Expression {
#[inline]
fn from(value: String) -> Self {
Expand Down Expand Up @@ -100,6 +109,7 @@ impl Evaluate<Expression> for &mut Evaluator {
Expression::Integer(v) => self.evaluate(v),
Expression::Process(v) => self.evaluate(v),
Expression::Quote(v) => self.evaluate(v),
Expression::Record(v) => self.evaluate(v),
Expression::String(v) => self.evaluate(v),
Expression::Symbol(v) => self.evaluate(v),
Expression::Verb(v) => self.evaluate(v),
Expand All @@ -115,6 +125,7 @@ impl Display for Expression {
Expression::Integer(v) => Display::fmt(v, f),
Expression::Process(v) => Display::fmt(v, f),
Expression::Quote(v) => Display::fmt(v, f),
Expression::Record(v) => Display::fmt(v, f),
Expression::String(v) => Display::fmt(v, f),
Expression::Symbol(v) => Display::fmt(v, f),
Expression::Verb(v) => Display::fmt(v, f),
Expand All @@ -130,6 +141,7 @@ impl Debug for Expression {
Expression::Integer(v) => Debug::fmt(v, f),
Expression::Process(v) => Debug::fmt(v, f),
Expression::Quote(v) => Debug::fmt(v, f),
Expression::Record(v) => Debug::fmt(v, f),
Expression::String(v) => Debug::fmt(v, f),
Expression::Symbol(v) => Debug::fmt(v, f),
Expression::Verb(v) => Debug::fmt(v, f),
Expand Down
22 changes: 21 additions & 1 deletion rat/src/grammar.pest
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ Locution = @{
}

Expression = {
(Boolean | Decimal | Integer | Quote | String | Symbol)
(Boolean | Decimal | Integer | Quote | Record | String | Symbol)
}

Boolean = @{
Expand All @@ -50,6 +50,10 @@ Quote = {
LeftSquareBracket ~ Phrase? ~ RightSquareBracket
}

Record = {
LeftCurlyBracket ~ (Expression ~ Equal ~ Expression)? ~ (Comma ~ Expression ~ Equal ~ Expression)* ~ Comma? ~ RightCurlyBracket
}

String = ${
"\"" ~ StringUnicodeScalarValue* ~ "\""
}
Expand All @@ -74,10 +78,18 @@ Colon = @{
":"
}

Comma = @{
","
}

Division = @{
"÷"
}

Equal = @{
"="
}

LeftArrow = @{
"←"
}
Expand All @@ -86,6 +98,14 @@ Semicolon = @{
";"
}

LeftCurlyBracket = @{
"{"
}

RightCurlyBracket = @{
"}"
}

LeftSquareBracket = @{
"["
}
Expand Down
1 change: 1 addition & 0 deletions rat/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ pub mod decimal;
pub mod integer;
pub mod process;
pub mod quote;
pub mod record;
pub mod string;
pub mod symbol;
pub mod verb;
Expand Down
49 changes: 49 additions & 0 deletions rat/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use crate::expression::Expression;
use crate::integer::Integer;
use crate::locution::{Locution, OwnedLocution};
use crate::quote::Quote;
use crate::record::Record;
use crate::string::String;
use crate::symbol::Symbol;
use crate::vocabulary::{Definition, Visibility, Vocabulary};
Expand Down Expand Up @@ -257,6 +258,23 @@ impl FromStr for Quote {
}
}

impl FromStr for Record {
type Err = ParseError;

fn from_str(s: &str) -> Result<Self, Self::Err> {
check_token_boundary(s)?;

let mut pairs = Grammar::parse(Rule::Record, s).map_err(with_origin(Origin::Unknown))?;
assert_eq!(pairs.len(), 1);

parse_record(
&mut Default::default(),
Origin::Unknown,
pairs.next().unwrap(),
)
}
}

impl FromStr for String {
type Err = ParseError;

Expand Down Expand Up @@ -419,6 +437,7 @@ fn parse_expression(
Rule::Decimal => parse_decimal(origin, pair).map(Expression::Decimal),
Rule::Integer => parse_integer(origin, pair).map(Expression::Integer),
Rule::Quote => parse_quote(parser, origin, pair).map(Expression::Quote),
Rule::Record => parse_record(parser, origin, pair).map(Expression::Record),
Rule::String => parse_string(origin, pair).map(Expression::String),
Rule::Symbol => parse_symbol(origin, pair).map(Expression::Symbol),
rule => unreachable!("unexpected rule: `{rule:?}`"),
Expand Down Expand Up @@ -505,6 +524,36 @@ fn parse_quote(parser: &mut Parser, origin: Origin, pair: PestPair) -> Result<Qu
Ok(quote)
}

fn parse_record(parser: &mut Parser, origin: Origin, pair: PestPair) -> Result<Record, ParseError> {
assert_eq!(pair.as_rule(), Rule::Record);

let mut key = None;
let mut value = None;
let mut record = Record::new();

for pair in pair.into_inner() {
match pair.as_rule() {
Rule::Expression => {
let exp = parse_expression(parser, origin, pair)?;
if key.is_none() {
key = Some(exp);
} else {
value = Some(exp);
}
}
Rule::RightCurlyBracket => break,
Rule::Comma | Rule::Equal | Rule::LeftCurlyBracket => (),
rule => unreachable!("unexpected rule: `{rule:?}`"),
}

if value.is_some() {
record.insert(key.take().unwrap(), value.take().unwrap());
}
}

Ok(record)
}

fn parse_string(origin: Origin, pair: PestPair) -> Result<String, ParseError> {
assert_eq!(pair.as_rule(), Rule::String);
pair.into_inner()
Expand Down
Loading

0 comments on commit ac0b1f9

Please sign in to comment.