-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
semver: chore
- Loading branch information
Showing
11 changed files
with
211 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
use std::collections::HashSet; | ||
|
||
use crate::{ | ||
parser::{ | ||
ast::{Function, Statement}, | ||
lookup::{BindingPower, Lookup}, | ||
macros::{expect_token, expect_value}, | ||
ParseResult, Parser, | ||
}, | ||
scanner::lexeme::TokenType, | ||
}; | ||
|
||
pub fn register(lookup: &mut Lookup) { | ||
lookup.add_statement_handler(TokenType::Trait, parse_trait); | ||
lookup.add_statement_handler(TokenType::Implementation, parse_impl); | ||
} | ||
|
||
fn parse_trait<'a>(parser: &mut Parser<'a>) -> ParseResult<'a, Statement> { | ||
expect_token!(parser, Trait)?; | ||
let identifier = expect_token!(parser, Identifier)?; | ||
let identifier = expect_value!(identifier, Identifier); | ||
|
||
expect_token!(parser, Colon)?; | ||
|
||
expect_token!(parser, IndentationOpen)?; | ||
|
||
let mut functions = HashSet::new(); | ||
|
||
loop { | ||
let token = expect_token!(parser)?; | ||
|
||
if token.token_type == TokenType::IndentationClose { | ||
break; | ||
} | ||
|
||
let function = crate::parser::statement::functions::parse_function_signature(parser)?; | ||
functions.insert(function); // TODO: Error if function already exists | ||
} | ||
|
||
expect_token!(parser, IndentationClose)?; | ||
|
||
Ok(Statement::Trait(identifier, functions)) | ||
} | ||
|
||
fn parse_impl<'a>(parser: &mut Parser<'a>) -> ParseResult<'a, Statement> { | ||
expect_token!(parser, Implementation)?; | ||
let identifier = expect_token!(parser, Identifier)?; | ||
let identifier = expect_value!(identifier, Identifier); | ||
|
||
expect_token!(parser, For)?; | ||
|
||
let typest = crate::parser::typest::parse(parser, BindingPower::None)?; | ||
|
||
expect_token!(parser, Colon)?; | ||
expect_token!(parser, IndentationOpen)?; | ||
|
||
let mut functions = HashSet::new(); | ||
|
||
loop { | ||
let token = expect_token!(parser)?; | ||
|
||
if token.token_type == TokenType::IndentationClose { | ||
break; | ||
} | ||
|
||
let signature = crate::parser::statement::functions::parse_function_signature(parser)?; | ||
expect_token!(parser, Colon)?; | ||
let body = crate::parser::statement::parse(parser)?; | ||
|
||
functions.insert(Function { | ||
signature, | ||
body: Box::new(body), | ||
}); // TODO: Error if function already exists | ||
} | ||
|
||
expect_token!(parser, IndentationClose)?; | ||
|
||
Ok(Statement::Implementation(identifier, typest, functions)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,16 @@ | ||
enum shape: | ||
square ~ string | ||
struct shape: | ||
shape_type ~ shape_type | ||
width ~ number | ||
height ~ number | ||
|
||
enum shape_type: | ||
square | ||
rectangle | ||
circle | ||
|
||
enum color: | ||
red blue green | ||
|
||
struct person: | ||
names ~ [string] | ||
age ~ number | ||
|
||
fn main(person ~ person, shapes ~ [shape]) -> number: <- 12 + 12 | ||
|
||
trait area: | ||
trait area: | ||
fn area() -> number | ||
|
||
impl area for shape: | ||
fn area() -> number: | ||
width * height; |