Skip to content

Commit

Permalink
Merge pull request #88 from KeenS/infix
Browse files Browse the repository at this point in the history
Infix operatiors and related
  • Loading branch information
KeenS authored May 29, 2022
2 parents 1010c47 + 197a948 commit 648366b
Show file tree
Hide file tree
Showing 8 changed files with 990 additions and 183 deletions.
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ Compiles only minimal subset of SML codes. The garbage collector is not complete
- [x] `decl decl`
- [x] `decl ; decl`
+ [x] `infix`
+ [ ] `infixr`
+ [ ] `nofix`
+ [x] `infixr`
+ [x] `nonfix`
* Expressions
+ [ ] special constant
- [x] integer
Expand All @@ -61,7 +61,7 @@ Compiles only minimal subset of SML codes. The garbage collector is not complete
- [x] char
- [x] string
+ [x] value identifier
+ [ ] `op`
+ [x] `op`
+ [ ] record
- [ ] basic (`{ label = expr , ...}`)
- [x] tuple
Expand All @@ -74,9 +74,9 @@ Compiles only minimal subset of SML codes. The garbage collector is not complete
- [x] basic (`let decl ... in expr end`)
- [x] derived (`let decl ... in expr; ...; expr end`)
+ [x] function application
+ [ ] infix operator
+ [x] infix operator
- [x] L
- [ ] R
- [x] R
+ [ ] typed (`exp : ty`)
+ [ ] exception
- [ ] `handle`
Expand All @@ -98,7 +98,7 @@ Compiles only minimal subset of SML codes. The garbage collector is not complete
- [x] char
- [x] string
+ [x] value identifier
+ [ ] `op`
+ [x] `op`
+ [ ] record
- [ ] basic (`{ label = pat , ...}`)
- [ ] wildcard (`...`)
Expand All @@ -108,7 +108,7 @@ Compiles only minimal subset of SML codes. The garbage collector is not complete
+ [ ] list
+ [x] paren
+ [x] Constructor
+ [ ] infix
+ [x] infix
+ [ ] typed (`pat : ty`)
+ [ ] layerd (`ident as pat`)
* Type
Expand Down
1 change: 1 addition & 0 deletions ml_example/binary_operators.sml
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ val e = 5 > 6
val f = 7 >= 8
val g = 9 < 10
val h = 11 <= 12
val i = op+(1,op*(2, 3))
11 changes: 11 additions & 0 deletions ml_example/infix.sml
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,14 @@ in
end

val z = sub (2, 3)

datatype intlist = :: of int * intlist | nil
infixr 7 ::

val list = 1 :: 2 :: 3 :: nil
val b = case list of
1 :: 2 :: 3 :: nil => print "true\n"
| _ => print "false\n"
nonfix add

val a = add(1, 2)
12 changes: 11 additions & 1 deletion src/ast/desugar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,11 @@ impl Desugar {
LangItem { name, decl } => self.transform_langitem(name, *decl),
Local { binds, body } => Some(self.transform_local(binds, body)),
D(DerivedDeclaration::Fun { name, clauses }) => Some(self.transform_fun(name, clauses)),
D(DerivedDeclaration::Infix { .. }) => None,
D(
DerivedDeclaration::Infix { .. }
| DerivedDeclaration::Infixr { .. }
| DerivedDeclaration::Nonfix { .. },
) => None,
D(DerivedDeclaration::Expr { expr }) => Some(self.transform_decl_expr(expr)),
}
}
Expand Down Expand Up @@ -196,6 +200,7 @@ impl Desugar {
D(DerivedExprKind::Seq { seq }) => self.transform_seq(span, seq),
D(DerivedExprKind::BindSeq { binds, ret }) => self.transform_bind_seq(span, binds, ret),
D(DerivedExprKind::String { value }) => self.transform_string(span, value),
D(DerivedExprKind::Op { name }) => self.transform_op(span, name),
};
UntypedCoreExpr {
ty: expr.ty,
Expand Down Expand Up @@ -465,6 +470,10 @@ impl Desugar {
.inner
}

fn transform_op(&mut self, _: Span, name: Symbol) -> UntypedCoreExprKind {
ExprKind::Symbol { name }
}

fn transform_pattern(&mut self, pattern: UntypedPattern) -> UntypedCorePattern {
use PatternKind::*;
let span = pattern.span;
Expand Down Expand Up @@ -509,6 +518,7 @@ impl Desugar {
.rfold(empty, |s, c| cons(span.clone(), s, c))
.inner
}
D(DerivedPatternKind::Op { name }) => UntypedCorePatternKind::Variable { name },
};
UntypedCorePattern::new(span, inner)
}
Expand Down
21 changes: 21 additions & 0 deletions src/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,13 @@ pub enum DerivedDeclaration<Ty> {
priority: Option<u8>,
names: Vec<Symbol>,
},
Infixr {
priority: Option<u8>,
names: Vec<Symbol>,
},
Nonfix {
names: Vec<Symbol>,
},
Expr {
expr: Expr<Ty>,
},
Expand Down Expand Up @@ -230,6 +237,9 @@ pub enum DerivedExprKind<Ty> {
String {
value: Vec<u32>,
},
Op {
name: Symbol,
},
}

pub type CorePattern<Ty> = Pattern<Ty, Nothing>;
Expand Down Expand Up @@ -270,6 +280,7 @@ pub enum PatternKind<Ty, DP = DerivedPatternKind> {
#[derive(Debug, Clone, PartialEq)]
pub enum DerivedPatternKind {
String { value: Vec<u32> },
Op { name: Symbol },
}

#[derive(Debug, Clone, PartialEq, Default)]
Expand Down Expand Up @@ -337,6 +348,16 @@ pub enum BIF {
LeChar,
}

pub trait HaveLocation {
fn span(&self) -> Span;
}

impl<Ty, T> HaveLocation for Annot<Ty, T> {
fn span(&self) -> Span {
self.span.clone()
}
}

impl<Ty, Inner> Annot<Ty, Inner> {
pub fn boxed(self) -> Box<Self> {
Box::new(self)
Expand Down
48 changes: 48 additions & 0 deletions src/ast/pp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,25 @@ impl<Ty: PP> PP for DerivedDeclaration<Ty> {
}
Ok(())
}
Infixr { priority, names } => {
write!(w, "infixr")?;
if let Some(p) = priority {
write!(w, " {}", p)?;
}
for name in names {
write!(w, " ")?;
name.pp(w, indent)?;
}
Ok(())
}
Nonfix { names } => {
write!(w, "nonfix")?;
for name in names {
write!(w, " ")?;
name.pp(w, indent)?;
}
Ok(())
}
Expr { expr } => {
expr.pp(w, indent)?;
Ok(())
Expand Down Expand Up @@ -244,6 +263,23 @@ impl<Ty: fmt::Display> fmt::Display for DerivedDeclaration<Ty> {
}
Ok(())
}
Infixr { priority, names } => {
write!(f, "infixr")?;
if let Some(p) = priority {
write!(f, " {}", p)?;
}
for name in names {
write!(f, " {}", name)?;
}
Ok(())
}
Nonfix { names } => {
write!(f, "nonfix")?;
for name in names {
write!(f, " {}", name)?;
}
Ok(())
}
Expr { expr } => {
write!(f, "{}", expr)
}
Expand Down Expand Up @@ -536,6 +572,9 @@ impl<Ty: PP> PP for DerivedExprKind<Ty> {
String { value } => {
write!(w, "{:?}", value)?;
}
Op { name } => {
write!(w, "op {:indent$}", name, indent = indent)?;
}
}
Ok(())
}
Expand Down Expand Up @@ -607,6 +646,9 @@ impl<Ty: fmt::Display> fmt::Display for DerivedExprKind<Ty> {
String { value } => {
write!(f, "{:?}", value)?;
}
Op { name } => {
write!(f, "op {:indent$}", name, indent = indent)?;
}
}
Ok(())
}
Expand Down Expand Up @@ -704,6 +746,9 @@ impl PP for DerivedPatternKind {
.map(|&u| char::from_u32(u).unwrap())
.collect::<String>()
),
DerivedPatternKind::Op { name } => {
write!(w, "{}", name)
}
}
}
}
Expand All @@ -719,6 +764,9 @@ impl fmt::Display for DerivedPatternKind {
.map(|&u| char::from_u32(u).unwrap())
.collect::<String>()
),
DerivedPatternKind::Op { name } => {
write!(f, "{}", name)
}
}
}
}
Expand Down
Loading

0 comments on commit 648366b

Please sign in to comment.