Skip to content

Commit c174652

Browse files
committed
Fixed compound assignments not parsed
1 parent 74b4eee commit c174652

File tree

9 files changed

+25
-28
lines changed

9 files changed

+25
-28
lines changed

Cargo.lock

+10-10
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[package]
2-
name = "MeowRust"
2+
name = "meow_rust"
33
version = "0.1.0"
44
authors = ["Liu Xiaoyi <circuitcoder0@gmail.com>"]
55
edition = "2018"

src/grammar/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ pub enum Expr<'a> {
162162
rhs: Box<Expr<'a>>,
163163
},
164164

165-
CompoundAssign {
165+
Assign {
166166
op: Option<ArithOp>,
167167
lhs: Box<Expr<'a>>,
168168
rhs: Box<Expr<'a>>,

src/grammar/type.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ pub enum SolidType<'a> {
77
Ident(TypePath<'a>),
88
Tuple(Vec<Type<'a>>),
99
Ref(bool, Box<Type<'a>>),
10-
Array(Box<Type<'a>>, usize),
10+
Array(Box<Type<'a>>, Box<Expr<'a>>),
1111
Slice(Box<Type<'a>>),
1212

1313
BareFunc(FnTypeSpec<'a>),

src/parser/expr.rs

+5-7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use nom::{
22
alt, call, complete, many0, map, named, one_of, opt, separated_nonempty_list, tag, terminated,
3-
value, IResult,
3+
IResult,
44
};
55

66
use super::ident::*;
@@ -84,14 +84,12 @@ macro_rules! bin_ltr_expr {
8484
};
8585
}
8686

87-
macro_rules! bin_rtl_expr {
87+
macro_rules! assign_rtl_expr {
8888
($cur:ident, $op_parser:ident, $lower:ident) => {
8989
pub fn $cur(input: &str) -> IResult<&str, Expr> {
9090
dbg!(input);
9191
let (sliced, init) = $lower(input)?;
9292

93-
use nom::fold_many0;
94-
9593
let (left, terms) = many0!(sliced, complete!(mrws!(tuple!($op_parser, $lower))))?;
9694

9795
let ret = match terms
@@ -101,15 +99,15 @@ macro_rules! bin_rtl_expr {
10199
None => Some((op, term)),
102100
Some((cop, dec)) => Some((
103101
op,
104-
Expr::BinaryOp {
102+
Expr::Assign {
105103
op: cop.into(),
106104
lhs: Box::new(term),
107105
rhs: Box::new(dec),
108106
},
109107
)),
110108
}) {
111109
None => init,
112-
Some((cop, dec)) => Expr::BinaryOp {
110+
Some((cop, dec)) => Expr::Assign {
113111
op: cop.into(),
114112
lhs: Box::new(init),
115113
rhs: Box::new(dec),
@@ -133,7 +131,7 @@ bin_ltr_expr!(logical_t3_expr, logical_op_t3, logical_t2_expr);
133131

134132
// logical_t3_expr falls through compound_assign_expr
135133
named!(pub t8_expr<&str, Expr>, call!(compound_assign_expr));
136-
bin_rtl_expr!(compound_assign_expr, compound_assign, logical_t3_expr);
134+
assign_rtl_expr!(compound_assign_expr, compound_assign, logical_t3_expr);
137135

138136
named!(pub expr_without_block<&str, Expr>, alt!(
139137
cont_expr

src/parser/literal.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use nom::{
2-
alt, char, complete, fold_many0, fold_many1, many_till, map, multi, named, none_of, one_of, opt,
3-
preceded, switch, tag, take, tuple, value, IResult,
2+
alt, char, complete, fold_many1, many_till, map, multi, named, none_of, one_of, opt, preceded,
3+
switch, tag, take, tuple, value, IResult,
44
};
55

66
use crate::grammar::{IntSuffix, Literal};

src/parser/mod.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use crate::grammar;
2-
use nom::bytes;
3-
use nom::{call, complete, named, IResult};
2+
use nom::{call, named, IResult};
43

54
// Common
65

src/parser/path.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use nom::{alt, complete, map, named, opt, separated_nonempty_list, tag, IResult};
1+
use nom::{alt, complete, map, named, opt, separated_nonempty_list, tag};
22
use std::collections::HashMap;
33

44
use super::ident::*;

src/parser/type.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use nom::{alt, call, complete, map, named, opt, tag};
22

3+
use super::expr::expr;
34
use super::ident::*;
45
use super::literal::*;
56
use super::path::*;
@@ -57,11 +58,10 @@ named!(array_type<&str, Type>,
5758
tag!("["),
5859
r#type,
5960
tag!(";"),
60-
// TODO: EXPR
61-
literal,
61+
expr,
6262
tag!("]")
6363
)),
64-
|(_, t, _, n, _)| SolidType::Array(Box::new(t), 0).into() // TODO: compute length
64+
|(_, t, _, n, _)| SolidType::Array(Box::new(t), Box::new(n)).into()
6565
)
6666
);
6767

0 commit comments

Comments
 (0)