Skip to content

Commit

Permalink
Simplify AST and improve naming of variants
Browse files Browse the repository at this point in the history
  • Loading branch information
brendanzab committed Nov 10, 2020
1 parent 16513dd commit 2a0dea6
Show file tree
Hide file tree
Showing 9 changed files with 114 additions and 111 deletions.
10 changes: 5 additions & 5 deletions pikelet/src/lang/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ pub enum TermData {
RecordElim(Arc<Term>, String),

/// Ordered sequences.
Sequence(Vec<Arc<Term>>),
SequenceTerm(Vec<Arc<Term>>),

/// Constants.
Constant(Constant),
Expand Down Expand Up @@ -424,7 +424,7 @@ impl<T: TryFromTerm> TryFromTerm for Vec<T> {

fn try_from_term(term: &Term) -> Result<Vec<T>, ()> {
match &term.data {
TermData::Sequence(entry_terms) => entry_terms
TermData::SequenceTerm(entry_terms) => entry_terms
.iter()
.map(|entry_term| T::try_from_term(entry_term).map_err(|_| ()))
.collect::<Result<Vec<_>, ()>>(),
Expand All @@ -440,7 +440,7 @@ macro_rules! impl_try_from_term_array {

fn try_from_term(term: &Term) -> Result<[T; $len], ()> {
match &term.data {
TermData::Sequence(entry_terms) if entry_terms.len() == $len => {
TermData::SequenceTerm(entry_terms) if entry_terms.len() == $len => {
use std::mem::MaybeUninit;

let mut entries: [MaybeUninit::<T>; $len] = unsafe {
Expand Down Expand Up @@ -523,7 +523,7 @@ impl_to_term!(str, |value| TermData::from(Constant::String(

impl<T: ToTerm> ToTerm for Vec<T> {
fn to_term(&self) -> Term {
Term::from(TermData::Sequence(
Term::from(TermData::SequenceTerm(
self.iter()
.map(|entry_term| Arc::new(T::to_term(entry_term)))
.collect(),
Expand All @@ -535,7 +535,7 @@ macro_rules! impl_to_term_array {
($($len:expr),*) => {
$(impl<T: ToTerm> ToTerm for [T; $len] {
fn to_term(&self) -> Term {
Term::from(TermData::Sequence(
Term::from(TermData::SequenceTerm(
self.iter()
.map(|entry_term| Arc::new(T::to_term(entry_term)))
.collect(),
Expand Down
12 changes: 6 additions & 6 deletions pikelet/src/lang/core/semantics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ pub enum Value {
RecordTerm(RecordClosure),

/// Ordered sequences.
Sequence(Vec<Arc<Value>>),
SequenceTerm(Vec<Arc<Value>>),

/// Constants.
Constant(Constant),
Expand Down Expand Up @@ -382,13 +382,13 @@ pub fn eval_term(
apply_function_elim(globals, head, Arc::new(input))
}

TermData::Sequence(term_entries) => {
TermData::SequenceTerm(term_entries) => {
let value_entries = term_entries
.iter()
.map(|entry_term| eval_term(globals, universe_offset, locals, entry_term))
.collect();

Arc::new(Value::Sequence(value_entries))
Arc::new(Value::SequenceTerm(value_entries))
}

TermData::Constant(constant) => Arc::new(Value::from(constant.clone())),
Expand Down Expand Up @@ -594,15 +594,15 @@ pub fn read_back_value(
Term::from(TermData::RecordTerm(term_entries.into()))
}

Value::Sequence(value_entries) => {
Value::SequenceTerm(value_entries) => {
let term_entries = value_entries
.iter()
.map(|value_entry| {
Arc::new(read_back_value(globals, local_size, unfold, value_entry))
})
.collect();

Term::from(TermData::Sequence(term_entries))
Term::from(TermData::SequenceTerm(term_entries))
}

Value::Constant(constant) => Term::from(TermData::from(constant.clone())),
Expand Down Expand Up @@ -760,7 +760,7 @@ fn is_equal(globals: &Globals, local_size: LocalSize, value0: &Value, value1: &V
true
}

(Value::Sequence(value_entries0), Value::Sequence(value_entries1)) => {
(Value::SequenceTerm(value_entries0), Value::SequenceTerm(value_entries1)) => {
if value_entries0.len() != value_entries1.len() {
return false;
}
Expand Down
6 changes: 3 additions & 3 deletions pikelet/src/lang/core/typing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ impl<'me> State<'me> {
}
}

(TermData::Sequence(entry_terms), Value::Stuck(Head::Global(name, _), spine)) => {
(TermData::SequenceTerm(entry_terms), Value::Stuck(Head::Global(name, _), spine)) => {
match (name.as_ref(), spine.as_slice()) {
("Array", [Elim::Function(len), Elim::Function(entry_type)]) => {
let entry_type = entry_type.force(self.globals);
Expand Down Expand Up @@ -227,7 +227,7 @@ impl<'me> State<'me> {
}
}
}
(TermData::Sequence(_), _) => {
(TermData::SequenceTerm(_), _) => {
let expected_type = self.read_back_value(expected_type);
self.report(CoreTypingMessage::NoSequenceConversion { expected_type })
}
Expand Down Expand Up @@ -401,7 +401,7 @@ impl<'me> State<'me> {
Arc::new(Value::Error)
}

TermData::Sequence(_) => {
TermData::SequenceTerm(_) => {
self.report(CoreTypingMessage::AmbiguousTerm {
term: AmbiguousTerm::Sequence,
});
Expand Down
22 changes: 7 additions & 15 deletions pikelet/src/lang/surface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,6 @@ mod grammar {
include!(concat!(env!("OUT_DIR"), "/lang/surface/grammar.rs"));
}

/// Literals.
#[derive(Debug, Clone)]
pub enum Literal {
/// Character literals.
Char(String),
/// String literals.
String(String),
/// Numeric literals.
Number(String),
}

/// Entry in a [record type](Term::RecordType).
pub type TypeEntry = (Ranged<String>, Option<Ranged<String>>, Term);
/// Entry in a [record term](Term::RecordTerm).
Expand Down Expand Up @@ -73,10 +62,13 @@ pub enum TermData {
RecordElim(Box<Term>, Ranged<String>),

/// Ordered sequences.
Sequence(Vec<Term>),

/// Literals.
Literal(Literal),
SequenceTerm(Vec<Term>),
/// Character literals.
CharTerm(String),
/// String literals.
StringTerm(String),
/// Numeric literals.
NumberTerm(String),

/// Error sentinel.
Error,
Expand Down
13 changes: 4 additions & 9 deletions pikelet/src/lang/surface/grammar.lalrpop
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::lang::Ranged;
use crate::lang::surface::{Term, TermData, Literal, TypeEntry, TermEntry};
use crate::lang::surface::{Term, TermData, TypeEntry, TermEntry};
use crate::lang::surface::lexer::Token;
use crate::reporting::LexerError;

Expand Down Expand Up @@ -86,7 +86,9 @@ AtomicTermData: TermData = {
"record" "{" <entries: List<TermEntry>> "}" => TermData::RecordTerm(entries),
<head_term: AtomicTerm> "." <label: Ranged<Name>> => TermData::RecordElim(Box::new(head_term), label),
"[" <entries: List<Term>> "]" => TermData::Sequence(entries),
<literal: Literal> => TermData::Literal(literal),
"character literal" => TermData::CharTerm(<>.to_owned()),
"string literal" => TermData::StringTerm(<>.to_owned()),
"numeric literal" => TermData::NumberTerm(<>.to_owned()),
};

#[inline]
Expand Down Expand Up @@ -116,13 +118,6 @@ Name: String = {
"name" => (<>).to_owned(),
};

#[inline]
Literal: Literal = {
"character literal" => Literal::Char(<>.to_owned()),
"string literal" => Literal::String(<>.to_owned()),
"numeric literal" => Literal::Number(<>.to_owned()),
};

#[inline]
Ranged<T>: Ranged<T> = {
<start: @L> <data: T> <end: @R> => Ranged::new(start..end, data),
Expand Down
2 changes: 1 addition & 1 deletion pikelet/src/pass/core_to_pretty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ where
.append(".")
.append(alloc.text(label)),

TermData::Sequence(term_entries) => (alloc.nil())
TermData::SequenceTerm(term_entries) => (alloc.nil())
.append("[")
.group()
.append(
Expand Down
32 changes: 16 additions & 16 deletions pikelet/src/pass/core_to_surface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,29 +230,29 @@ impl<'me> State<'me> {
Ranged::from(label.clone()),
),

TermData::Sequence(entry_terms) => {
TermData::SequenceTerm(entry_terms) => {
let core_entry_terms = entry_terms
.iter()
.map(|entry_term| self.from_term(entry_term))
.collect();

surface::TermData::Sequence(core_entry_terms)
surface::TermData::SequenceTerm(core_entry_terms)
}

TermData::Constant(constant) => surface::TermData::Literal(match constant {
Constant::U8(value) => surface::Literal::Number(value.to_string()),
Constant::U16(value) => surface::Literal::Number(value.to_string()),
Constant::U32(value) => surface::Literal::Number(value.to_string()),
Constant::U64(value) => surface::Literal::Number(value.to_string()),
Constant::S8(value) => surface::Literal::Number(value.to_string()),
Constant::S16(value) => surface::Literal::Number(value.to_string()),
Constant::S32(value) => surface::Literal::Number(value.to_string()),
Constant::S64(value) => surface::Literal::Number(value.to_string()),
Constant::F32(value) => surface::Literal::Number(value.to_string()),
Constant::F64(value) => surface::Literal::Number(value.to_string()),
Constant::Char(value) => surface::Literal::Char(format!("{:?}", value)),
Constant::String(value) => surface::Literal::String(format!("{:?}", value)),
}),
TermData::Constant(constant) => match constant {
Constant::U8(value) => surface::TermData::NumberTerm(value.to_string()),
Constant::U16(value) => surface::TermData::NumberTerm(value.to_string()),
Constant::U32(value) => surface::TermData::NumberTerm(value.to_string()),
Constant::U64(value) => surface::TermData::NumberTerm(value.to_string()),
Constant::S8(value) => surface::TermData::NumberTerm(value.to_string()),
Constant::S16(value) => surface::TermData::NumberTerm(value.to_string()),
Constant::S32(value) => surface::TermData::NumberTerm(value.to_string()),
Constant::S64(value) => surface::TermData::NumberTerm(value.to_string()),
Constant::F32(value) => surface::TermData::NumberTerm(value.to_string()),
Constant::F64(value) => surface::TermData::NumberTerm(value.to_string()),
Constant::Char(value) => surface::TermData::CharTerm(format!("{:?}", value)),
Constant::String(value) => surface::TermData::StringTerm(format!("{:?}", value)),
},

TermData::Error => surface::TermData::Error,
};
Expand Down
Loading

0 comments on commit 2a0dea6

Please sign in to comment.