Skip to content

Commit a40bec7

Browse files
Syntax 2 (#31)
* a little more playing, and syntax feeling better
1 parent 2c71ff9 commit a40bec7

File tree

8 files changed

+264
-68
lines changed

8 files changed

+264
-68
lines changed

ast/src/lib.rs

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,32 @@ impl For {
2424
}
2525
}
2626

27+
#[derive(Debug, Clone, PartialEq)]
28+
pub struct If {
29+
pub expr: Box<Expr>,
30+
pub body: Box<Expr>,
31+
}
32+
33+
impl If {
34+
pub fn new(if_expr: Box<Expr>, if_body: Box<Expr>) -> Self {
35+
If {
36+
expr: if_expr,
37+
body: if_body,
38+
}
39+
}
40+
}
41+
42+
#[derive(Debug, Clone, PartialEq)]
43+
pub struct Destructure {
44+
pub elements: Vec<Box<Expr>>,
45+
}
46+
47+
impl Destructure {
48+
pub fn new(elements: Vec<Box<Expr>>) -> Self {
49+
Destructure { elements }
50+
}
51+
}
52+
2753
#[derive(Debug, Clone, PartialEq)]
2854
pub struct Match {
2955
pub expr: Box<Expr>,
@@ -300,27 +326,27 @@ pub struct EnumDecl {
300326
pub visibility: Option<Lexeme>,
301327
pub mutability: Lexeme,
302328
pub identifier: Box<Expr>,
303-
pub declarators: Vec<Box<Expr>>,
329+
pub variants: Vec<Box<Expr>>,
304330
pub sig: Option<Box<Expr>>,
305-
pub variant: Option<Box<Expr>>,
331+
pub enum_type: Option<Box<Expr>>,
306332
}
307333

308334
impl EnumDecl {
309335
pub fn new(
310336
visibility: Option<Lexeme>,
311337
mutability: Lexeme,
312338
identifier: Box<Expr>,
313-
declarators: Vec<Box<Expr>>,
339+
variants: Vec<Box<Expr>>,
314340
sig: Option<Box<Expr>>,
315-
variant: Option<Box<Expr>>,
341+
enum_type: Option<Box<Expr>>,
316342
) -> Self {
317343
EnumDecl {
318344
visibility,
319345
mutability,
320346
identifier,
321-
declarators,
347+
variants,
322348
sig,
323-
variant,
349+
enum_type,
324350
}
325351
}
326352
}
@@ -330,6 +356,7 @@ pub struct ErrorDecl {
330356
pub visibility: Option<Lexeme>,
331357
pub mutability: Lexeme,
332358
pub identifier: Box<Expr>,
359+
pub variants: Vec<Box<Expr>>,
333360
pub sig: Option<Box<Expr>>,
334361
}
335362

@@ -338,12 +365,14 @@ impl ErrorDecl {
338365
visibility: Option<Lexeme>,
339366
mutability: Lexeme,
340367
identifier: Box<Expr>,
368+
variants: Vec<Box<Expr>>,
341369
sig: Option<Box<Expr>>,
342370
) -> Self {
343371
ErrorDecl {
344372
visibility,
345373
mutability,
346374
identifier,
375+
variants,
347376
sig,
348377
}
349378
}
@@ -676,6 +705,8 @@ pub enum Expr {
676705
ArrayDecl(ArrayDecl),
677706
Rest(Rest),
678707
For(For),
708+
Destructure(Destructure),
709+
If(If),
679710
While(While),
680711
Match(Match),
681712
Arm(Arm),

ebnf.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
<fn> ::= "fn " "(" <args>? ") " <block>
1212
<struct> ::= "struct " "{ " <declarators>? "}"
1313
<error> ::= "error " ("| " <ident>)+
14-
<if> ::= "if " "(" <expr> ")" (<fn> | <block>) ("else if "(" <expr> ")" (<fn> | <block>))* ("else " ( <fn> | <block>))?
14+
<if> ::= "if " "(" <expr> ")" (<fn> | <block>)
1515
<for> ::= "for " "(" <expr> ")" (<fn> | <block>)
1616
<while> ::= "while " "(" <expr> ")" (<fn> | <block>)
1717
<match> ::= "match " "(" <expr> ")" "{ " <arm>+ "}"
@@ -41,7 +41,7 @@
4141
<array_access> ::= "[" <expr> "]"
4242
<invoke> ::= "." <ident>
4343
<call> ::= "(" (<or> ("," <or>)*)? ")"
44-
<struct_body> ::= "{" (<ident> ":" <ident>)* "}"
44+
<struct_body> ::= "{" (<ident> ":" <or> ("," <ident> ":" <or>))? "}"
4545
<terminal> ::= "_" | "true " | "false " | "undefined " | "self " | "never " | <num> | <ident> | <chars> | <array_decl> | <anon_fn> | "(" <expr> ")"
4646
<anon_fn> ::= "fn " "(" <args>? ") " <block>
4747
<ident> ::= ([a-z] | [A-Z]) ([A-Z] | [a-z] | [0-9] | "_")*

linter/src/lib.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ impl<'buf, 'ttb, 'sco> LintSource<'buf, 'ttb, 'sco> {
5858
Expr::Declarator(declarator) => self.check_declarator(&declarator),
5959
Expr::Match(_match) => self.check_match(&_match),
6060
Expr::For(_for) => self.check_for(&_for),
61+
Expr::If(_if) => self.check_if(&_if),
6162
Expr::Invoke(invoke) => self.check_invoke(&invoke),
6263
Expr::PropAccess(prop) => self.check_prop_access(&prop),
6364
Expr::Arm(arm) => self.check_arm(&arm),
@@ -152,6 +153,19 @@ impl<'buf, 'ttb, 'sco> LintSource<'buf, 'ttb, 'sco> {
152153
ok_simple_tree!(UndefinedValue, typ)
153154
}
154155

156+
pub fn check_if(&mut self, _if: &If) -> ResultTreeType {
157+
let res = self.lint_recurse(&_if.expr)?;
158+
let body = self.lint_recurse(&_if.body)?;
159+
let if_op = IfOp {
160+
in_expr: res.0,
161+
in_curried: res.1,
162+
body: body.0,
163+
body_curried: body.1,
164+
};
165+
let cur = if_op.body_curried.clone();
166+
ok_tree!(If, if_op, cur)
167+
}
168+
155169
pub fn check_for(&mut self, _for: &For) -> ResultTreeType {
156170
let res = self.lint_recurse(&_for.expr)?;
157171
let body = self.lint_recurse(&_for.var_loop)?;

parser/src/lib.rs

Lines changed: 56 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ impl<'s> Parser<'s> {
5858
) -> ResultExpr {
5959
let mut variants: Vec<Box<Expr>> = vec![];
6060
while let Some(_) = self.lexer.collect_if(Token::Bar) {
61-
let x = self.ident().xconvert_to_sym_decl()?;
61+
let x = self.ident().xconvert_to_decl()?;
6262
variants.push(x);
6363
}
6464
result_expr!(TagDecl, visibility, mutability, identifier, variants, sig)
@@ -71,15 +71,12 @@ impl<'s> Parser<'s> {
7171
identifier: Box<Expr>,
7272
sig: Option<Box<Expr>>,
7373
) -> ResultExpr {
74-
let _ = self
75-
.lexer
76-
.collect_if(Token::OBrace)
77-
.xexpect_token(&self, "expected '{'".to_string())?;
78-
let _ = self
79-
.lexer
80-
.collect_if(Token::CBrace)
81-
.xexpect_token(&self, "expected '}'".to_string())?;
82-
result_expr!(ErrorDecl, visibility, mutability, identifier, sig)
74+
let mut variants: Vec<Box<Expr>> = vec![];
75+
while let Some(_) = self.lexer.collect_if(Token::Bar) {
76+
let x = self.ident().xconvert_to_decl()?;
77+
variants.push(x);
78+
}
79+
result_expr!(ErrorDecl, visibility, mutability, identifier, variants, sig)
8380
}
8481

8582
pub fn _enum(
@@ -99,7 +96,7 @@ impl<'s> Parser<'s> {
9996
.xexpect_token(&self, "expected ')'".to_string())?;
10097
}
10198
while let Some(_) = self.lexer.collect_if(Token::Bar) {
102-
let x = self.ident().xconvert_to_sym_decl()?;
99+
let x = self.ident().xconvert_to_decl()?;
103100
variants.push(x);
104101
}
105102
result_expr!(EnumDecl, visibility, mutability, identifier, variants, sig, enum_type)
@@ -131,9 +128,9 @@ impl<'s> Parser<'s> {
131128
.collect_of_if(&[Token::Let, Token::Const, Token::Type, Token::Impl])
132129
.xexpect_token(&self, "expected mutability".to_string())?;
133130
let identifier = self
134-
.ident()
135-
.xexpect_expr(&self, "expected identifier".to_string())
136-
.xconvert_to_sym_decl()?;
131+
.destructure()
132+
.xexpect_expr(&self, "expected identifier, or destructure".to_string())
133+
.xconvert_to_decl()?;
137134
let sig = self.opt_signature()?;
138135
let _ = self
139136
.lexer
@@ -266,7 +263,7 @@ impl<'s> Parser<'s> {
266263
let sig = self
267264
.opt_signature()
268265
.xexpect_expr(&self, "expected signature".to_string())?;
269-
return result_expr!(Declarator, id.xconvert_to_sym_decl().unwrap(), sig)
266+
return result_expr!(Declarator, id.xconvert_to_decl().unwrap(), sig)
270267
.xconvert_to_result_opt();
271268
}
272269
pub fn args(&mut self) -> Result<Option<Vec<Box<Expr>>>> {
@@ -421,8 +418,9 @@ impl<'s> Parser<'s> {
421418
let mutability = self.lexer.collect_of_if(&[Token::Let, Token::Const]);
422419
if let Some(muta) = mutability {
423420
let identifier = self
424-
.ident()
425-
.xexpect_expr(&self, "expected an identifier".to_string())?;
421+
.destructure()
422+
.xexpect_expr(&self, "expected identifier, or destructure".to_string())
423+
.xconvert_to_decl()?;
426424
let sig = self.opt_signature()?;
427425
let _ = self
428426
.lexer
@@ -449,8 +447,11 @@ impl<'s> Parser<'s> {
449447
.lexer
450448
.collect_if(Token::CParen)
451449
.xexpect_token(&self, "expected ')'".to_string())?;
450+
if let Some(_fn) = self.anon_fn()? {
451+
return bubble_expr!(If, x, _fn);
452+
}
452453
let blk = self.block()?;
453-
return bubble_expr!(For, x, blk);
454+
return bubble_expr!(If, x, blk);
454455
}
455456
pub fn _while(&mut self) -> ResultOptExpr {
456457
let f = self.lexer.collect_if(Token::While);
@@ -492,6 +493,27 @@ impl<'s> Parser<'s> {
492493
let blk = self.block()?;
493494
return bubble_expr!(For, x, blk);
494495
}
496+
pub fn destructure(&mut self) -> ResultOptExpr {
497+
let brace = self.lexer.collect_if(Token::OBrace);
498+
if brace.is_some() {
499+
let mut idents: Vec<Box<Expr>> = vec![];
500+
loop {
501+
match self.ident() {
502+
Some(x) => {
503+
idents.push(Box::new(Expr::SymbolDecl(x.into_symbol())));
504+
let _ = self.lexer.collect_if(Token::Comma);
505+
}
506+
None => break,
507+
}
508+
}
509+
let _ = self
510+
.lexer
511+
.collect_if(Token::CBrace)
512+
.xexpect_token(&self, "expected '}' or more identifiers".to_string())?;
513+
return bubble_expr!(Destructure, idents);
514+
}
515+
return Ok(self.ident());
516+
}
495517
pub fn block(&mut self) -> ResultExpr {
496518
self.lexer
497519
.collect_if(Token::OBrace)
@@ -996,12 +1018,12 @@ trait ConvertToResult {
9961018
fn xconvert_to_result(self, parser: &Parser, title: String) -> ResultExpr;
9971019
}
9981020

999-
trait ConvertToSymbolDecl {
1000-
fn xconvert_to_sym_decl(self) -> ResultExpr;
1021+
trait ConvertToDecl {
1022+
fn xconvert_to_decl(self) -> ResultExpr;
10011023
}
10021024

1003-
trait ConvertToSymbolDeclResult {
1004-
fn xconvert_to_sym_decl(self) -> ResultExpr;
1025+
trait ConvertToDeclResult {
1026+
fn xconvert_to_decl(self) -> ResultExpr;
10051027
}
10061028

10071029
trait ConvertToResultOpt {
@@ -1077,22 +1099,25 @@ impl ExpectExpr for OptExpr {
10771099
}
10781100
}
10791101

1080-
impl ConvertToSymbolDeclResult for ResultExpr {
1081-
fn xconvert_to_sym_decl(self) -> ResultExpr {
1102+
impl ConvertToDeclResult for ResultExpr {
1103+
fn xconvert_to_decl(self) -> ResultExpr {
10821104
match self {
10831105
Err(x) => Err(x),
10841106
Ok(val) => match *val {
10851107
Expr::Symbol(x) => {
10861108
return Ok(Box::new(Expr::SymbolDecl(x)));
10871109
}
1110+
Expr::Destructure(_) => {
1111+
return Ok(val);
1112+
}
10881113
_ => panic!("type lang issue, expected into symbol"),
10891114
},
10901115
}
10911116
}
10921117
}
10931118

1094-
impl ConvertToSymbolDecl for OptExpr {
1095-
fn xconvert_to_sym_decl(self) -> ResultExpr {
1119+
impl ConvertToDecl for OptExpr {
1120+
fn xconvert_to_decl(self) -> ResultExpr {
10961121
match self {
10971122
None => panic!("type lang issue, expected a symbol"),
10981123
Some(val) => match *val {
@@ -1310,14 +1335,11 @@ mod tests {
13101335
token: Token::Let,
13111336
span: 2..5,
13121337
},
1313-
expr!(
1314-
Symbol,
1315-
Lexeme {
1316-
slice: String::from("x"),
1317-
token: Token::Symbol,
1318-
span: 6..7
1319-
}
1320-
),
1338+
Box::new(Expr::SymbolDecl(Symbol::new(Lexeme {
1339+
slice: String::from("x"),
1340+
token: Token::Symbol,
1341+
span: 6..7
1342+
}))),
13211343
None,
13221344
expr!(
13231345
Number,

0 commit comments

Comments
 (0)