Skip to content

Commit

Permalink
Merge branch 'type-annotations' into trunk
Browse files Browse the repository at this point in the history
  • Loading branch information
Quaqqer committed May 1, 2024
2 parents aadc9fb + ab9d4e6 commit b17f0da
Show file tree
Hide file tree
Showing 9 changed files with 32 additions and 8 deletions.
2 changes: 1 addition & 1 deletion crates/saft-ir/src/lowerer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ impl<N: Clone> Lowerer<N> {
self.scoped_based(|l| {
let params = params
.iter()
.map(|ident| l.declare(ident))
.map(|ident| l.declare(&ident.v.name))
.try_collect::<Vec<_>>()?;
let body = l.lower_block(body)?;

Expand Down
10 changes: 9 additions & 1 deletion crates/saft-syntax/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,13 +127,21 @@ pub enum Item {
Function(Function),
}

#[derive(Debug, Clone, PartialEq)]
pub struct Parameter {
pub name: Spanned<Ident>,
pub ty: Spanned<Type>,
}

#[derive(Debug, Clone, PartialEq)]
pub struct Function {
pub ident: Spanned<Ident>,
pub params: Vec<Spanned<Ident>>,
pub params: Vec<Spanned<Parameter>>,
pub body: Spanned<Block>,
pub ret_ty: Option<Spanned<Type>>,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Type {
Ident(Ident),
}
1 change: 1 addition & 0 deletions crates/saft-syntax/src/lex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ impl<'a> Lexer<'a> {
'!' if Self::eat_chars(&mut cur, "!=") => mktoken!(cur, T::BangEqual),
'!' => eat_token!(cur, T::Bang),
'+' => eat_token!(cur, T::Plus),
'-' if Self::eat_chars(&mut cur, "->") => mktoken!(cur, T::Arrow),
'-' => eat_token!(cur, T::Minus),
'*' => eat_token!(cur, T::Star),
'/' if Self::eat_chars(&mut cur, "//") => mktoken!(cur, T::SlashSlash),
Expand Down
17 changes: 15 additions & 2 deletions crates/saft-syntax/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -493,11 +493,17 @@ impl<'a> Parser<'a> {
let start = self.eat(Token::Fn)?;
let ident = self.eat_ident()?;

let mut params = Vec::new();
let mut params = Vec::<Spanned<ast::Parameter>>::new();

self.eat(Token::LParen)?;
while self.peek().v != Token::RParen {
let param = self.eat_ident()?;
let name = self.eat_ident()?;
self.eat(Token::Colon)?;
let ty = self.parse_ty()?;
let s = name.s.join(&ty.s);

let param = s.spanned(ast::Parameter { name, ty });

params.push(param);

if self.try_eat(Token::Comma).is_none() {
Expand All @@ -507,6 +513,12 @@ impl<'a> Parser<'a> {

self.eat(Token::RParen)?;

let ret_ty = if self.try_eat(Token::Arrow).is_some() {
Some(self.parse_ty()?)
} else {
None
};

let body = self.parse_block(None)?;
let s = start.join(&body.s);

Expand All @@ -515,6 +527,7 @@ impl<'a> Parser<'a> {
ident,
params,
body,
ret_ty,
})),
s,
))
Expand Down
2 changes: 2 additions & 0 deletions crates/saft-syntax/src/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ pub enum Token {
Comma,
Semicolon,
Colon,
Arrow,

Identifier(String),
Float(f64),
Expand Down Expand Up @@ -98,6 +99,7 @@ impl Token {
Comment(s) => format!("comment '{s}'"),
Colon => "':'".into(),
Let => "'let'".into(),
Arrow => "'->'".into(),
}
}
}
2 changes: 1 addition & 1 deletion tests/res/tests/items/functions/assignment.saf
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
fn f(x) {
fn f(x: int) {
print("Okay boomer " + x)
}

Expand Down
2 changes: 1 addition & 1 deletion tests/res/tests/items/functions/order.saf
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
printadd(1, 2);

fn printadd(a, b) {
fn printadd(a: int, b: int) {
print(a + b);
}

Expand Down
2 changes: 1 addition & 1 deletion tests/res/tests/misc/fib.saf
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
fn fib(i) {
fn fib(i: int) -> int {
if i < 2 {
i
} else {
Expand Down
2 changes: 1 addition & 1 deletion tests/res/tests/misc/fib_loop.saf
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
fn fib(n) {
fn fib(n: int) -> int {
if n == 0 { return 0; };

let a = 0;
Expand Down

0 comments on commit b17f0da

Please sign in to comment.