Skip to content

Commit

Permalink
Split statement parser from if parser.
Browse files Browse the repository at this point in the history
  • Loading branch information
YSawc committed Nov 8, 2020
1 parent 75ff188 commit f991dd9
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 128 deletions.
123 changes: 62 additions & 61 deletions src/node_arr/node_arr.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use super::super::node::node::*;
use super::super::parser::error::*;
use super::super::parser::parser::*;
use super::super::program::program::*;
use super::super::simplified::beta::*;
use super::super::token::token::*;
Expand Down Expand Up @@ -86,12 +87,15 @@ impl NodeArr {

it.next_with_shadow();

let mut isi: bool = false;
if it.peek_value() == TokenKind::Int {
it.shadow_p.to_owned().next();
it.p.next();
isi = true;
}
let isi = {
match it.peek_value() {
TokenKind::Int => {
it.next_with_shadow();
true
}
_ => false,
}
};

if it.p.peek() == None {
return Err(ParseError::NotLBrace(it.peek_shadow()));
Expand Down Expand Up @@ -146,6 +150,35 @@ impl NodeArr {
}
}

pub fn parse_statement(
it: &mut TokenIter,
ev: Vec<Vec<Var>>,
) -> Result<(Self, Vec<String>), ParseError> {
let mut a = Self::new();
a.set_env(ev);

expect_token(
TokenKind::LBrace,
ParseError::NotOpenedStmt(it.p.peek().unwrap().to_owned().to_owned()),
it,
)?;

while it.peek_value() != TokenKind::RBrace {
match NodeSt::parser(it)? {
n => match n.c.value {
_ => {
a.set_imm_env();
let n = beta(&mut n.to_owned(), &mut a);
a.node_st_vec.push(n.to_owned());
}
},
}
}
it.p.next().unwrap();

Ok((a, vec![]))
}

pub fn stp(it: &mut TokenIter, ev: Vec<Vec<Var>>) -> Result<(Self, Vec<String>), ParseError> {
let mut a = Self::new();
a.set_env(ev);
Expand Down Expand Up @@ -310,49 +343,42 @@ impl NodeArr {
if it.p.peek() == None {
return Err(ParseError::Eof);
}

if it.peek_value() == TokenKind::RBrace {
a.set_end_of_node()
}

a.set_imm_env();
let mut c = beta(&mut n.to_owned().cond.unwrap(), &mut a);
c = c.simplified();

let if_stmts_a =
Self::parse_statement(it, a.imm_env_v.to_owned())?.0;

let mut _else_if_stmts: NodeArr = NodeArr::new();
match it.p.peek().unwrap().value {
TokenKind::Else => {
it.next();
_else_if_stmts =
Self::parse_statement(it, a.imm_env_v.to_owned())?.0;
}
_ => (),
};

match c.c.value {
NodeKind::Num(num) => {
if num == 0 {
if n.to_owned().else_if_stmts != None {
let (else_if_stmts_a, _) = Self::statement_parser(
n.to_owned()
.else_if_stmts
.unwrap()
.as_ref()
.to_owned(),
&mut a,
)?;

if a.end_of_node {
a.set_ret_node(else_if_stmts_a.ret_node_st);
}
let mut n = n.to_owned();
n.else_if_stmts =
Some(Box::new(else_if_stmts_a.node_st_vec));
a.node_st_vec.push(n);
if _else_if_stmts.node_st_vec != vec![] {
a.node_st_vec.append(
&mut _else_if_stmts.node_st_vec.to_owned(),
);
} else {
continue;
}
} else {
let (if_stmts_a, _) = Self::statement_parser(
n.to_owned().if_stmts.unwrap().as_ref().to_owned(),
&mut a,
)?;
if a.end_of_node {
a.set_ret_node(if_stmts_a.ret_node_st);
}

let mut n = n.to_owned();
n.if_stmts = Some(Box::new(if_stmts_a.node_st_vec));
a.node_st_vec.push(n);
}

a.node_st_vec
.append(&mut if_stmts_a.node_st_vec.to_owned());
}
_ => {
a.set_imm_env();
Expand Down Expand Up @@ -414,12 +440,11 @@ impl NodeArr {
a.set_imm_env();

let n = beta(&mut n.to_owned(), &mut a);
a.node_st_vec.push(n.to_owned());

if a.end_of_node {
a.set_ret_node(n.to_owned());
}
// println!("r: {:?}", r);
a.node_st_vec.push(n);
}
},
Err(e) => return Err(e),
Expand Down Expand Up @@ -449,27 +474,3 @@ impl NodeArr {
Ok((a, ugv))
}
}

impl NodeArr {
pub fn statement_parser(
vn: Vec<NodeSt>,
p_a: &mut Self,
) -> Result<(Self, Vec<String>), ParseError> {
let mut a = Self::new();

let mut min = vn.iter().peekable();

while min.to_owned().peek() != None {
match min.to_owned().peek().unwrap().c.value {
_ => {
let n = beta(&mut min.next().unwrap().to_owned(), p_a);
a.node_st_vec.push(n);
}
};
}

a.set_ret_node(a.node_st_vec.last().unwrap().to_owned());

Ok((a, p_a.used_variable.to_owned()))
}
}
64 changes: 10 additions & 54 deletions src/parser/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,10 @@ impl NodeSt {
}
}

pub fn new_if(c: Node, cond: NodeSt, vstmt: Vec<NodeSt>) -> Self {
pub fn new_if(c: Node, cond: NodeSt) -> Self {
Self {
c,
cond: Some(Box::new(cond)),
if_stmts: Some(Box::new(vstmt)),
..Default::default()
}
}
Expand Down Expand Up @@ -128,62 +127,15 @@ impl NodeSt {
ParseError::NotOpenedParen(it.p.peek().unwrap().to_owned().to_owned()),
it,
)?;

let cond = Self::cmp(it)?;
expect_token(
TokenKind::RParen,
ParseError::NotClosedStmt(it.p.peek().unwrap().to_owned().to_owned()),
it,
)?;
expect_token(
TokenKind::LBrace,
ParseError::NotOpenedStmt(it.p.peek().unwrap().to_owned().to_owned()),
it,
)?;

let mut if_stmts: Vec<NodeSt> = vec![];
while it.p.peek().unwrap().value != TokenKind::RBrace {
if_stmts.push(Self::stmt(it)?);
}

let mut lhs = Self::new_if(op, cond.to_owned(), if_stmts);

expect_token(
TokenKind::RBrace,
ParseError::NotClosedStmt(it.p.peek().unwrap().to_owned().to_owned()),
it,
)?;
match it.p.peek().unwrap() {
Token {
value: TokenKind::Else,
..
} => {
it.p.next().unwrap();
expect_token(
TokenKind::LBrace,
ParseError::NotOpenedStmt(
it.p.peek().unwrap().to_owned().to_owned(),
),
it,
)?;

let mut else_if_stmts: Vec<NodeSt> = vec![];
while it.p.peek().unwrap().value != TokenKind::RBrace {
else_if_stmts.push(Self::stmt(it)?);
}

lhs.else_if_stmts = Some(Box::new(else_if_stmts));

expect_token(
TokenKind::RBrace,
ParseError::NotClosedStmt(
it.p.peek().unwrap().to_owned().to_owned(),
),
it,
)?;
return Ok(lhs);
}
_ => return Ok(lhs),
}
let lhs = Self::new_if(op, cond.to_owned());
return Ok(lhs);
}
Token {
value: TokenKind::UnderScore,
Expand Down Expand Up @@ -412,15 +364,19 @@ fn expect_ident(err: ParseError, it: &mut TokenIter) -> Result<(String, Loc), Pa
}
}

fn expect_token(ty: TokenKind, err: ParseError, it: &mut TokenIter) -> Result<Loc, ParseError> {
pub fn expect_token(ty: TokenKind, err: ParseError, it: &mut TokenIter) -> Result<Loc, ParseError> {
if it.p.peek().unwrap().value == ty {
Ok(it.p.next().unwrap().loc.to_owned())
} else {
Err(err)
}
}

fn unexpect_token(ty: TokenKind, err: ParseError, it: &mut TokenIter) -> Result<(), ParseError> {
pub fn unexpect_token(
ty: TokenKind,
err: ParseError,
it: &mut TokenIter,
) -> Result<(), ParseError> {
if it.p.peek().unwrap().value == ty {
Err(err)
} else {
Expand Down
26 changes: 13 additions & 13 deletions src/tests/parser/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -242,19 +242,19 @@ fn not_opened_else_stmt_test() {
// assert!(n)
// }

#[test]
fn eof_around_closed_else_stmt_test() {
let mut t = Token::tokenize("fn int { if (2 == 3) { 5; } else { 10; }").unwrap();
let mut it = TokenIter::new(&mut t);
let n = match NodeArr::w_parser(&mut it, vec![]) {
Ok(_) => false,
Err(e) => match e {
ParseError::Eof => true,
_ => false,
},
};
assert!(n)
}
// #[test]
// fn eof_around_closed_else_stmt_test() {
// let mut t = Token::tokenize("fn int { if (2 == 3) { 5; } else { 10; }").unwrap();
// let mut it = TokenIter::new(&mut t);
// let n = match NodeArr::w_parser(&mut it, vec![]) {
// Ok(_) => false,
// Err(e) => match e {
// ParseError::Eof => true,
// _ => false,
// },
// };
// assert!(n)
// }

#[test]
fn unexpected_under_score_operator_test() {
Expand Down
1 change: 1 addition & 0 deletions test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ assert_llvm 34 'fn int { int _u = 8; int a = 2; 4+2*a*_u-2 }'
assert_llvm 1 'fn int { int i = 1; if (i) { 0 } else { 0 } i }'
assert_llvm 0 'fn { if (2 == 3) { 1; 2; } else { 3; 4; } _ }'
assert_llvm 0 'fn { int i = 9; if (i) { 1; 2; } else { 3*4; 5; } _ }'
assert_llvm 0 'fn { int i = 9; if (i) { i; 2; } else { 3*4; 5; } _ }'

echo "------------------------------"
echo "All llvm test passed!\n"

0 comments on commit f991dd9

Please sign in to comment.