Skip to content

Commit

Permalink
mpc: Allow defining constructors under aggregate type namespaces
Browse files Browse the repository at this point in the history
  • Loading branch information
kukrimate committed Apr 27, 2023
1 parent 9e1a3ae commit 1fb5228
Show file tree
Hide file tree
Showing 11 changed files with 60 additions and 58 deletions.
5 changes: 4 additions & 1 deletion misc/grm.lalrpop
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,10 @@ FunctionDefinition: () = {
"function" ReceiverDefinition? Identifier ("<" IdentifierList ">")? "(" ParameterDefinitionList? ")" ("->" TypeName)? BlockExpr,
};

ReceiverDefinition: () = "(" ParameterDefinition ")";
ReceiverDefinition: () = {
"(" "!" Path ")",
"(" ParameterDefinition ")"
};

ParameterDefinitionList: () = {
ParameterDefinition,
Expand Down
2 changes: 1 addition & 1 deletion mpc/src/parse/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ impl Repository {
for (def_id, def) in self.parsed_defs.iter() {
match def {
Def::Func(def) => {
if let Some((_, _, ty)) = &def.receiver {
if let Some((_, ty)) = &def.receiver {
let receiver_id = self.find_receiver_id(def.parent_id, ty)?;
q.push((def.loc.clone(), receiver_id, def.name, *def_id));
}
Expand Down
20 changes: 13 additions & 7 deletions mpc/src/parse/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -413,14 +413,20 @@ impl<'repo, 'path> Parser<'repo, 'path> {
}
}

fn parse_receiver(&mut self) -> Result<Option<(RefStr, IsMut, Ty)>, CompileError> {
fn parse_receiver(&mut self) -> Result<Option<(Option<(RefStr, IsMut)>, Ty)>, CompileError> {
Ok(if maybe_want!(self, Token::LParen) {
let is_mut = self.parse_is_mut()?;
let name = want!(self, Token::Ident(name), *name)?;
want!(self, Token::Colon, ())?;
let ty = self.parse_ty()?;
want!(self, Token::RParen, ())?;
Some((name, is_mut, ty))
if maybe_want!(self, Token::Excl) {
let ty = self.parse_ty()?;
want!(self, Token::RParen, ())?;
Some((None, ty))
} else {
let is_mut = self.parse_is_mut()?;
let name = want!(self, Token::Ident(name), *name)?;
want!(self, Token::Colon, ())?;
let ty = self.parse_ty()?;
want!(self, Token::RParen, ())?;
Some((Some((name, is_mut)), ty))
}
} else {
None
})
Expand Down
2 changes: 1 addition & 1 deletion mpc/src/parse/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ pub struct FuncDef {
pub parent_id: DefId,
pub name: RefStr,
pub type_params: Vec<RefStr>,
pub receiver: Option<(RefStr, IsMut, Ty)>,
pub receiver: Option<(Option<(RefStr, IsMut)>, Ty)>,
pub params: Vec<(RefStr, IsMut, Ty)>,
pub ret_ty: Ty,
pub body: Expr
Expand Down
4 changes: 2 additions & 2 deletions mpc/src/sema/inst.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ impl<'repo, 'tctx> GlobalCtx<'repo, 'tctx> {
let mut param_tys = Vec::new();

// Method receiver
if let Some((name, _, ty)) = &def.receiver {
if let Some((Some((name, _)), ty)) = &def.receiver {
let ty = def_ctx.infer_ty(ty)?;
param_tys.push((*name, ty.clone()));
}
Expand Down Expand Up @@ -248,7 +248,7 @@ impl<'repo, 'tctx> GlobalCtx<'repo, 'tctx> {
let mut params = Vec::new();

// Method receiver
if let Some((name, is_mut, ty)) = &def.receiver {
if let Some((Some((name, is_mut)), ty)) = &def.receiver {
let ty = def_ctx.infer_ty(ty)?;
let local_id = def_ctx.new_local_id();
def_ctx.define(*name, Sym::Param(local_id));
Expand Down
2 changes: 1 addition & 1 deletion mpc_std/box.m
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

struct Box<T>(mem: *mut T)

function new<T>(val: T) -> Box<T> {
function (!Box) new<T>(val: T) -> Box<T> {
let mem: *mut T = mem::allocate();
*mem = val;
Box(val)
Expand Down
8 changes: 0 additions & 8 deletions mpc_std/opt.m
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,6 @@
None
)

function some<T>(val: T) -> Option<T> {
Option::Some(val)
}

function none<T>() -> Option<T> {
Option::None
}

function (o: *Option<T>) is_some<T>() -> Bool {
match *o {
Some(val) => true,
Expand Down
8 changes: 0 additions & 8 deletions mpc_std/result.m
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,6 @@
Err(v: E)
)

function ok<O, E>(v: O) -> Result<O, E> {
Result::Ok(v)
}

function err<O, E>(v: E) -> Result<O, E> {
Result::Err(v)
}

function (result: *Result<O, E>) is_ok<O, E>() -> Bool {
match *result {
Ok(v) => true,
Expand Down
16 changes: 8 additions & 8 deletions mpc_std/slice.m
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
import opt::Option
import vec::Vec
import arr
import mem
import opt
import prog
import vec

struct Slice<ElementType>(base: *mut ElementType, length: Uintn)

function from_ptr<ElementType>(base: *mut ElementType, length: Uintn) -> Slice<ElementType> {
function (!Slice) from_ptr<ElementType>(base: *mut ElementType, length: Uintn) -> Slice<ElementType> {
Slice(base, length)
}

function from_vec<ElementType>(vec: *vec::Vec<ElementType>) -> Slice<ElementType> {
function (!Slice) from_vec<ElementType>(vec: *Vec<ElementType>) -> Slice<ElementType> {
Slice((*vec).mem, (*vec).length)
}
function from_array<ArrayType, ElementType>(array: *ArrayType) -> Slice<ElementType> {
function (!Slice) from_array<ArrayType, ElementType>(array: *ArrayType) -> Slice<ElementType> {
Slice(&(*array)[0], arr::length(array))
}
Expand All @@ -25,11 +25,11 @@
mem::ptr_off(slice.base, index)
}
function (slice: Slice<ElementType>) at_or_none<ElementType>(index: Uintn) -> opt::Option<*mut ElementType> {
function (slice: Slice<ElementType>) at_or_none<ElementType>(index: Uintn) -> Option<*mut ElementType> {
if index < slice.length {
opt::some(mem::ptr_off(slice.base, index))
Option::Some(mem::ptr_off(slice.base, index))
} else {
opt::none()
Option::None
}
}
Expand Down
37 changes: 23 additions & 14 deletions mpc_std/str.m
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,39 @@
* Description: String utilities
*/

import slice
import vec
import slice::Slice
import vec::Vec

type Str = vec::Vec<Uint8>
type StrView = slice::Slice<Uint8>
struct Str(vec: Vec<Uint8>)

function view_from_lit<ArrayType>(a: *ArrayType) -> StrView {
slice::from_array(a)
function (!Str) new() -> Str {
Str(Vec::new())
}

function view_from_str<ArrayType>(s: *Str) -> StrView {
slice::from_vec(s)
function (s: *mut Str) push(byte: Uint8) {
s.vec.push(byte)
}

function eq(a: StrView, b: StrView) -> Bool {
if a.length != b.length { return false }
struct StrView(slice: Slice<Uint8>)

function (!StrView) from_lit<ArrayType>(a: *ArrayType) -> StrView {
StrView(Slice::from_array(a))
}

function (!StrView) from_str<ArrayType>(s: *Str) -> StrView {
StrView(Slice::from_vec(&s.vec))
}

function (a: StrView) eq(b: StrView) -> Bool {
if a.slice.length != b.slice.length { return false }
let mut i = 0;
while i < a.length {
if *slice::at(a, i) != *slice::at(b, i) { return false }
while i < a.slice.length {
if *a.slice.at(i) != *b.slice.at(i) { return false }
i += 1;
}
true
}

function ne(a: StrView, b: StrView) -> Bool {
!eq(a, b)
function (a: StrView) ne(b: StrView) -> Bool {
!a.eq(b)
}
14 changes: 7 additions & 7 deletions mpc_std/vec.m
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
* Description: Implementation of a vector data structure
*/

import opt::Option
import mem
import opt
import prog

struct Vec<T>(mem: *mut T, length: Uintn, capacity: Uintn)

function new<T>() -> Vec<T> {
function (!Vec) new<T>() -> Vec<T> {
Vec(nil, 0, 0)
}

Expand Down Expand Up @@ -39,11 +39,11 @@
mem::ptr_off((*vec).mem, index)
}

function (vec: *mut Vec<T>) at_or_none<T>(index: Uintn) -> opt::Option<*mut T> {
function (vec: *mut Vec<T>) at_or_none<T>(index: Uintn) -> Option<*mut T> {
if index < (*vec).length {
opt::some(mem::ptr_off((*vec).mem, index))
Option::Some(mem::ptr_off((*vec).mem, index))
} else {
opt::none()
Option::None
}
}

Expand All @@ -58,8 +58,8 @@
function (vec: *mut Vec<T>) pop_or_none<T>() -> opt::Option<T> {
if (*vec).length > 0 {
(*vec).length -= 1;
opt::some(*mem::ptr_off((*vec).mem, (*vec).length))
Option::Some(*mem::ptr_off((*vec).mem, (*vec).length))
} else {
opt::none()
Option::None
}
}

0 comments on commit 1fb5228

Please sign in to comment.