mirror of
https://github.com/HigherOrderCO/Kind1.git
synced 2024-10-26 15:41:58 +03:00
feat: started the mutter, getter and setter feature
This commit is contained in:
parent
668233184f
commit
43eeb5ca0d
@ -288,6 +288,20 @@ impl<'a> Visitor for Subst<'a> {
|
||||
self.visit_expr(motive);
|
||||
}
|
||||
}
|
||||
ExprKind::SeqRecord(sec) => {
|
||||
use kind_tree::concrete::SeqOperation::*;
|
||||
|
||||
self.visit_ident(&mut sec.ident[0]);
|
||||
|
||||
self.visit_expr(&mut sec.typ_);
|
||||
|
||||
match &mut sec.operation {
|
||||
Set(expr) => self.visit_expr(expr),
|
||||
Mut(expr) => self.visit_expr(expr),
|
||||
Get => (),
|
||||
}
|
||||
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -48,7 +48,7 @@ impl<'a> DesugarState<'a> {
|
||||
}
|
||||
|
||||
res
|
||||
},
|
||||
}
|
||||
Literal::NumU120(num) => {
|
||||
if !self.check_implementation("U120.new", range, Sugar::U120) {
|
||||
return desugared::Expr::err(range);
|
||||
@ -74,6 +74,14 @@ impl<'a> DesugarState<'a> {
|
||||
)
|
||||
}
|
||||
|
||||
pub(crate) fn desugar_seq(
|
||||
&mut self,
|
||||
range: Range,
|
||||
sub: &expr::SeqRecord,
|
||||
) -> Box<desugared::Expr> {
|
||||
todo!()
|
||||
}
|
||||
|
||||
pub(crate) fn desugar_sttm(
|
||||
&mut self,
|
||||
bind_ident: &QualifiedIdent,
|
||||
@ -157,7 +165,10 @@ impl<'a> DesugarState<'a> {
|
||||
let pure = self.old_book.names.get(pure_ident.to_str());
|
||||
|
||||
if bind.is_none() || pure.is_none() {
|
||||
self.send_err(PassDiagnostic::NeedToImplementMethods(range, Sugar::DoNotation));
|
||||
self.send_err(PassDiagnostic::NeedToImplementMethods(
|
||||
range,
|
||||
Sugar::DoNotation,
|
||||
));
|
||||
return desugared::Expr::err(range);
|
||||
}
|
||||
|
||||
@ -249,7 +260,7 @@ impl<'a> DesugarState<'a> {
|
||||
type_name: &QualifiedIdent,
|
||||
var_name: &Ident,
|
||||
motive: &Option<Box<expr::Expr>>,
|
||||
next: &expr::Expr
|
||||
next: &expr::Expr,
|
||||
) -> Box<desugared::Expr> {
|
||||
let rec = self.old_book.entries.get(type_name.to_str());
|
||||
|
||||
@ -270,16 +281,23 @@ impl<'a> DesugarState<'a> {
|
||||
return desugared::Expr::err(range);
|
||||
}
|
||||
|
||||
let field_names : Vec<_> = record.fields.iter().map(|x| var_name.add_segment(x.0.to_str())).collect();
|
||||
let field_names: Vec<_> = record
|
||||
.fields
|
||||
.iter()
|
||||
.map(|x| var_name.add_segment(x.0.to_str()))
|
||||
.collect();
|
||||
|
||||
let irrelev = vec![false; field_names.len()];
|
||||
|
||||
let motive = motive.as_ref().map(|x| self.desugar_expr(x)).unwrap_or_else(|| self.gen_hole_expr(range));
|
||||
let motive = motive
|
||||
.as_ref()
|
||||
.map(|x| self.desugar_expr(x))
|
||||
.unwrap_or_else(|| self.gen_hole_expr(range));
|
||||
|
||||
let spine = vec![
|
||||
desugared::Expr::var(var_name.clone()),
|
||||
desugared::Expr::lambda(range, var_name.clone(), motive, false),
|
||||
desugared::Expr::unfold_lambda(&irrelev, &field_names, self.desugar_expr(next))
|
||||
desugared::Expr::unfold_lambda(&irrelev, &field_names, self.desugar_expr(next)),
|
||||
];
|
||||
|
||||
self.mk_desugared_fun(range, open_id, spine, false)
|
||||
@ -367,7 +385,13 @@ impl<'a> DesugarState<'a> {
|
||||
Pair { fst, snd } => self.desugar_pair(expr.range, fst, snd),
|
||||
Match(matcher) => self.desugar_match(expr.range, matcher),
|
||||
Subst(sub) => self.desugar_sub(expr.range, sub),
|
||||
Open { type_name, var_name, motive, next } => self.desugar_open(expr.range, type_name, var_name, motive, &next)
|
||||
Open {
|
||||
type_name,
|
||||
var_name,
|
||||
motive,
|
||||
next,
|
||||
} => self.desugar_open(expr.range, type_name, var_name, motive, &next),
|
||||
SeqRecord(sec) => self.desugar_seq(expr.range, sec),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -635,6 +635,20 @@ impl Visitor for UnboundCollector {
|
||||
}
|
||||
}
|
||||
}
|
||||
ExprKind::SeqRecord(sec) => {
|
||||
use kind_tree::concrete::SeqOperation::*;
|
||||
|
||||
self.visit_expr(&mut sec.typ_);
|
||||
|
||||
self.visit_ident(&mut sec.ident[0]);
|
||||
|
||||
match &mut sec.operation {
|
||||
Set(expr) => self.visit_expr(expr),
|
||||
Mut(expr) => self.visit_expr(expr),
|
||||
Get => (),
|
||||
}
|
||||
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -12,7 +12,7 @@ use std::fmt::{Display, Error, Formatter};
|
||||
|
||||
/// A binding express the positional or named argument of
|
||||
/// a constructor or function.
|
||||
#[derive(Clone, Debug, Hash, PartialEq, Eq)]
|
||||
#[derive(Clone, Debug)]
|
||||
pub enum Binding {
|
||||
Positional(Box<Expr>),
|
||||
Named(Range, Ident, Box<Expr>),
|
||||
@ -22,7 +22,7 @@ pub enum Binding {
|
||||
pub type Spine = Vec<Binding>;
|
||||
|
||||
/// A binding that is used inside applications.
|
||||
#[derive(Clone, Debug, Hash, PartialEq, Eq)]
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct AppBinding {
|
||||
pub data: Box<Expr>,
|
||||
pub erased: bool,
|
||||
@ -46,7 +46,7 @@ impl AppBinding {
|
||||
|
||||
/// A case binding is a field or a rename of some field
|
||||
/// inside a match expression.
|
||||
#[derive(Clone, Debug, Hash, PartialEq, Eq)]
|
||||
#[derive(Clone, Debug)]
|
||||
pub enum CaseBinding {
|
||||
Field(Ident),
|
||||
Renamed(Ident, Ident),
|
||||
@ -56,7 +56,7 @@ pub enum CaseBinding {
|
||||
/// strutinizer, bindings to the names of each arguments and
|
||||
/// a right-hand side value. The ignore_rest flag useful to just
|
||||
/// fill all of the case bindings that are not used with a default name.
|
||||
#[derive(Clone, Debug, Hash, PartialEq, Eq)]
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct Case {
|
||||
pub constructor: Ident,
|
||||
pub bindings: Vec<CaseBinding>,
|
||||
@ -66,7 +66,7 @@ pub struct Case {
|
||||
|
||||
/// A match block that will be desugared
|
||||
/// into an eliminator of a datatype.
|
||||
#[derive(Clone, Debug, Hash, PartialEq, Eq)]
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct Match {
|
||||
pub typ: QualifiedIdent,
|
||||
pub scrutinee: Ident,
|
||||
@ -77,7 +77,7 @@ pub struct Match {
|
||||
}
|
||||
|
||||
/// Substitution
|
||||
#[derive(Clone, Debug, Hash, PartialEq, Eq)]
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct Substitution {
|
||||
pub name: Ident,
|
||||
pub redx: usize,
|
||||
@ -85,7 +85,7 @@ pub struct Substitution {
|
||||
pub expr: Box<Expr>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Hash, PartialEq, Eq)]
|
||||
#[derive(Clone, Debug)]
|
||||
pub enum Literal {
|
||||
/// The universe of types (e.g. Type)
|
||||
Type,
|
||||
@ -111,13 +111,13 @@ pub enum Literal {
|
||||
|
||||
/// A destruct of a single constructor. It's a flat destruct
|
||||
/// and just translates into a eliminator for records.
|
||||
#[derive(Clone, Debug, Hash, PartialEq, Eq)]
|
||||
#[derive(Clone, Debug)]
|
||||
pub enum Destruct {
|
||||
Destruct(Range, QualifiedIdent, Vec<CaseBinding>, Option<Range>),
|
||||
Ident(Ident),
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Hash, PartialEq, Eq)]
|
||||
#[derive(Clone, Debug)]
|
||||
pub enum SttmKind {
|
||||
Expr(Box<Expr>, Box<Sttm>),
|
||||
Ask(Destruct, Box<Expr>, Box<Sttm>),
|
||||
@ -130,13 +130,27 @@ pub enum SttmKind {
|
||||
/// describes the idea of `sequence` inside a monad
|
||||
/// each monadic action contains a `next` element that is
|
||||
/// desugared into a 'monadic bind'.
|
||||
#[derive(Clone, Debug, Hash, PartialEq, Eq)]
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct Sttm {
|
||||
pub data: SttmKind,
|
||||
pub range: Range,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Hash, PartialEq, Eq)]
|
||||
#[derive(Clone, Debug)]
|
||||
pub enum SeqOperation {
|
||||
Set(Box<Expr>),
|
||||
Mut(Box<Expr>),
|
||||
Get
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct SeqRecord {
|
||||
pub typ_: Box<Expr>,
|
||||
pub ident: Vec<Ident>,
|
||||
pub operation: SeqOperation
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub enum ExprKind {
|
||||
/// Name of a variable
|
||||
Var { name: Ident },
|
||||
@ -210,12 +224,14 @@ pub enum ExprKind {
|
||||
type_name: QualifiedIdent,
|
||||
var_name: Ident,
|
||||
motive: Option<Box<Expr>>,
|
||||
next: Box<Expr>,
|
||||
next: Box<Expr>
|
||||
},
|
||||
|
||||
SeqRecord(SeqRecord)
|
||||
}
|
||||
|
||||
/// Describes a single expression inside Kind2.
|
||||
#[derive(Clone, Debug, Hash, PartialEq, Eq)]
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct Expr {
|
||||
pub data: ExprKind,
|
||||
pub range: Range,
|
||||
@ -292,25 +308,7 @@ impl Expr {
|
||||
range,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl Binding {
|
||||
pub fn to_app_binding(&self) -> AppBinding {
|
||||
match self {
|
||||
Binding::Positional(expr) => {
|
||||
AppBinding {
|
||||
data: expr.clone(),
|
||||
erased: false,
|
||||
}
|
||||
},
|
||||
Binding::Named(_, _, expr) => {
|
||||
AppBinding {
|
||||
data: expr.clone(),
|
||||
erased: false,
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Locatable for Binding {
|
||||
@ -485,34 +483,16 @@ impl Display for Match {
|
||||
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error> {
|
||||
write!(f, "match {} {}", self.typ, self.scrutinee)?;
|
||||
|
||||
if let Some(res) = &self.value {
|
||||
write!(f, " = {}", res)?;
|
||||
}
|
||||
|
||||
if !self.with_vars.is_empty() {
|
||||
write!(f, " with")?;
|
||||
for var in &self.with_vars {
|
||||
if let Some(ty) = &var.1 {
|
||||
write!(f, " ({} : {})", var.0, ty)?;
|
||||
} else {
|
||||
write!(f, " {}", var.0)?;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
match &self.motive {
|
||||
None => Ok(()),
|
||||
Some(res) => write!(f, " : {}", res),
|
||||
}?;
|
||||
write!(f, " {{ ")?;
|
||||
|
||||
for case in &self.cases {
|
||||
write!(f, "{}; ", case)?
|
||||
}
|
||||
|
||||
write!(f, "}}")?;
|
||||
|
||||
if let Some(res) = &self.motive {
|
||||
write!(f, " : {}", res)?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
write!(f, "}}")
|
||||
}
|
||||
}
|
||||
|
||||
@ -592,18 +572,8 @@ impl Display for Expr {
|
||||
args.iter().map(|x| format!(" {}", x)).collect::<String>()
|
||||
),
|
||||
Let { name, val, next } => write!(f, "(let {} = {}; {})", name, val, next),
|
||||
Open {
|
||||
type_name,
|
||||
var_name,
|
||||
motive: Some(motive),
|
||||
next,
|
||||
} => write!(f, "(open {} {} : {motive}; {})", type_name, var_name, next),
|
||||
Open {
|
||||
type_name,
|
||||
var_name,
|
||||
motive: None,
|
||||
next,
|
||||
} => write!(f, "(open {} {}; {})", type_name, var_name, next),
|
||||
Open { type_name, var_name, motive: Some(motive), next } => write!(f, "(open {} {} : {motive}; {})", type_name, var_name, next),
|
||||
Open { type_name, var_name, motive: None, next } => write!(f, "(open {} {}; {})", type_name, var_name, next),
|
||||
If { cond, then_, else_ } => {
|
||||
write!(f, "(if {} {{{}}} else {{{}}})", cond, then_, else_)
|
||||
}
|
||||
@ -620,6 +590,34 @@ impl Display for Expr {
|
||||
Match(matcher) => write!(f, "({})", matcher),
|
||||
Subst(subst) => write!(f, "({})", subst),
|
||||
Hole => write!(f, "_"),
|
||||
SeqRecord(rec) => {
|
||||
use SeqOperation::*;
|
||||
write!(f, "(!({}) {}", rec.typ_, rec.ident.iter().map(|x| x.to_str()).collect::<Vec<_>>().join(","))?;
|
||||
match &rec.operation {
|
||||
Set(expr) => write!(f, "+= {})", expr),
|
||||
Mut(expr) => write!(f, "@= {})", expr),
|
||||
Get => write!(f, ")")
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Binding {
|
||||
pub fn to_app_binding(&self) -> AppBinding {
|
||||
match self {
|
||||
Binding::Positional(expr) => {
|
||||
AppBinding {
|
||||
data: expr.clone(),
|
||||
erased: false,
|
||||
}
|
||||
},
|
||||
Binding::Named(_, _, expr) => {
|
||||
AppBinding {
|
||||
data: expr.clone(),
|
||||
erased: false,
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -22,7 +22,7 @@ pub mod visitor;
|
||||
pub use expr::*;
|
||||
|
||||
/// A value of a attribute
|
||||
#[derive(Clone, Debug, Hash, PartialEq, Eq)]
|
||||
#[derive(Clone, Debug)]
|
||||
pub enum AttributeStyle {
|
||||
Ident(Range, Ident),
|
||||
String(Range, String),
|
||||
@ -34,7 +34,7 @@ pub enum AttributeStyle {
|
||||
/// that usually is on the top of a declaration
|
||||
/// and can be attached to a function declaration
|
||||
/// it express some compiler properties
|
||||
#[derive(Clone, Debug, Hash, PartialEq, Eq)]
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct Attribute {
|
||||
pub name: Ident,
|
||||
pub args: Vec<AttributeStyle>,
|
||||
@ -48,7 +48,7 @@ pub struct Attribute {
|
||||
/// compiled.
|
||||
/// hide: that express a implicit argument (that will
|
||||
/// be discovered through unification).
|
||||
#[derive(Clone, Debug, Hash, PartialEq, Eq)]
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct Argument {
|
||||
pub hidden: bool,
|
||||
pub erased: bool,
|
||||
@ -60,7 +60,7 @@ pub struct Argument {
|
||||
/// A rule is a equation that in the left-hand-side
|
||||
/// contains a list of patterns @pats@ and on the
|
||||
/// right hand side a value.
|
||||
#[derive(Clone, Debug, Hash, PartialEq, Eq)]
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct Rule {
|
||||
pub name: QualifiedIdent,
|
||||
pub pats: Vec<Box<Pat>>,
|
||||
@ -72,7 +72,7 @@ pub struct Rule {
|
||||
/// and has rules. The type of the function
|
||||
/// consists of the arguments @args@ and the
|
||||
/// return type @typ@.
|
||||
#[derive(Clone, Debug, Hash, PartialEq, Eq)]
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct Entry {
|
||||
pub name: QualifiedIdent,
|
||||
pub docs: Vec<String>,
|
||||
@ -86,7 +86,7 @@ pub struct Entry {
|
||||
|
||||
/// A single cosntructor inside the algebraic data
|
||||
/// type definition.
|
||||
#[derive(Clone, Debug, Hash, PartialEq, Eq)]
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct Constructor {
|
||||
pub name: Ident,
|
||||
pub docs: Vec<String>,
|
||||
@ -97,7 +97,7 @@ pub struct Constructor {
|
||||
|
||||
/// An algebraic data type definition that supports
|
||||
/// parametric and indexed data type definitions.
|
||||
#[derive(Clone, Debug, Hash, PartialEq, Eq)]
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct SumTypeDecl {
|
||||
pub name: QualifiedIdent,
|
||||
pub docs: Vec<String>,
|
||||
@ -108,7 +108,7 @@ pub struct SumTypeDecl {
|
||||
}
|
||||
|
||||
/// A single constructor data type.
|
||||
#[derive(Clone, Debug, Hash, PartialEq, Eq)]
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct RecordDecl {
|
||||
pub name: QualifiedIdent,
|
||||
pub docs: Vec<String>,
|
||||
@ -126,7 +126,7 @@ impl RecordDecl {
|
||||
}
|
||||
|
||||
/// All of the structures
|
||||
#[derive(Clone, Debug, Hash, PartialEq, Eq)]
|
||||
#[derive(Clone, Debug)]
|
||||
pub enum TopLevel {
|
||||
SumType(SumTypeDecl),
|
||||
RecordType(RecordDecl),
|
||||
@ -166,7 +166,7 @@ impl TopLevel {
|
||||
/// A module is a collection of top level entries
|
||||
/// that contains syntatic sugars. In the future
|
||||
/// it will contain a HashMap to local renames.
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct Module {
|
||||
pub entries: Vec<TopLevel>,
|
||||
pub uses: FxHashMap<String, String>,
|
||||
|
@ -492,5 +492,6 @@ pub fn walk_expr<T: Visitor>(ctx: &mut T, expr: &mut Expr) {
|
||||
ExprKind::Hole => {}
|
||||
ExprKind::Subst(subst) => ctx.visit_substitution(subst),
|
||||
ExprKind::Match(matcher) => ctx.visit_match(matcher),
|
||||
ExprKind::SeqRecord(_) => todo!(),
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user