Skip to content

Commit

Permalink
Implement groval variable.
Browse files Browse the repository at this point in the history
  • Loading branch information
YSawc committed Oct 15, 2020
1 parent 93cdb97 commit 4df4ee8
Show file tree
Hide file tree
Showing 7 changed files with 129 additions and 36 deletions.
18 changes: 12 additions & 6 deletions src/node_arr/node_arr.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use super::super::location::location::*;
use super::super::node::node::*;
use super::super::parser::error::*;
use super::super::program::program::*;
use super::super::simplified::*;
use super::super::token::token::*;
use super::super::var::var::*;
Expand Down Expand Up @@ -34,8 +35,8 @@ impl NodeArr {

pub fn w_parser(
mut it: &mut std::iter::Peekable<std::slice::Iter<Annot<TokenKind>>>,
g: Vec<Var>,
) -> Result<Self, ParseError> {

let et = it.to_owned();
if it.peek().unwrap().value != TokenKind::Fn {
return Err(ParseError::OperatorOutOfFnction(
Expand All @@ -60,7 +61,7 @@ impl NodeArr {
}
it.next();

let l = Self::stp(&mut it);
let l = Self::stp(&mut it, g);

if isi {
match l.to_owned()?.to_owned().ret_node_st.c.value {
Expand Down Expand Up @@ -95,6 +96,7 @@ impl NodeArr {

pub fn stp(
mut it: &mut std::iter::Peekable<std::slice::Iter<Annot<TokenKind>>>,
g: Vec<Var>,
) -> Result<Self, ParseError> {
let mut uv: Vec<String> = vec![];
let mut nv = vec![];
Expand Down Expand Up @@ -127,7 +129,7 @@ impl NodeArr {
}
}

match Self::find_l(_s.to_owned(), l.to_owned()) {
match Program::find_v(_s.to_owned(), g.to_owned(), l.to_owned()) {
Some(mut f) => {
match uv.contains(&f.to_owned().s.to_owned()) {
true => (),
Expand All @@ -136,6 +138,7 @@ impl NodeArr {
uv.retain(|s| s != &f.to_owned().s.to_owned());
let mut n = vex(
&mut n.to_owned().rhs.unwrap().to_owned(),
g.to_owned(),
l.to_owned(),
&mut uv,
);
Expand All @@ -148,6 +151,7 @@ impl NodeArr {
_ => {
let mut n = vex(
&mut n.to_owned().rhs.unwrap().to_owned(),
g.to_owned(),
l.to_owned(),
&mut uv,
);
Expand All @@ -170,7 +174,7 @@ impl NodeArr {
}
}

match Self::find_l(_s.to_owned(), l.to_owned()) {
match Program::find_v(_s.to_owned(), g.to_owned(), l.to_owned()) {
Some(mut f) => {
// println!("uv: {:?}", uv);
match uv.contains(&f.to_owned().s.to_owned()) {
Expand All @@ -179,6 +183,7 @@ impl NodeArr {
}
let mut n = vex(
&mut n.to_owned().rhs.unwrap().to_owned(),
g.to_owned(),
l.to_owned(),
&mut uv,
);
Expand Down Expand Up @@ -207,7 +212,7 @@ impl NodeArr {
r = n.to_owned();
n
}
NodeKind::Ident(s) => match Self::find_l(s, l.to_owned()) {
NodeKind::Ident(s) => match Program::find_v(s, g.to_owned(), l.to_owned()) {
Some(v) => {
if it.peek().unwrap().to_owned().to_owned().value == TokenKind::RBrace {
b = true;
Expand All @@ -232,6 +237,7 @@ impl NodeArr {
}
let mut c = vex(
&mut n.to_owned().cond.unwrap().to_owned(),
g.to_owned(),
l.to_owned(),
&mut uv,
);
Expand Down Expand Up @@ -262,7 +268,7 @@ impl NodeArr {
b = true;
}

let n = vex(&mut n.to_owned(), l.to_owned(), &mut uv);
let n = vex(&mut n.to_owned(), g.to_owned(), l.to_owned(), &mut uv);
if b {
r = n.to_owned();
}
Expand Down
82 changes: 78 additions & 4 deletions src/program/program.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
// use super::super::location::location::*;
use super::super::location::location::*;
// use super::super::node::node::*;
use super::super::node::node::*;
use super::super::node_arr::node_arr::*;
use super::super::parser::error::*;
// use super::super::parser::parser::*;
// use super::super::simplified::*;
use super::super::simplified::*;
use super::super::token::token::*;
use super::super::var::var::*;

Expand Down Expand Up @@ -37,10 +38,83 @@ impl Program {
}

pub fn w_parser(vt: Vec<Token>) -> Result<Self, ParseError> {
let g: Vec<Var> = vec![];
// let g: Vec<Var> = vec![];
let mut na: Vec<NodeArr> = vec![];
let mut it = vt.iter().peekable();
na.push(NodeArr::w_parser(&mut it)?);

// while it.peek().unwrap().value != TokenKind::Fn {}
let g: Vec<Var> = Self::stp(&mut it)?;
// g.puch

na.push(NodeArr::w_parser(&mut it, g.to_owned())?);
Ok(Self::new(g, na))
}

pub fn stp(
mut it: &mut std::iter::Peekable<std::slice::Iter<Annot<TokenKind>>>,
) -> Result<Vec<Var>, ParseError> {
let mut uv: Vec<String> = vec![];
let mut _l = vec![];
let mut g: Vec<Var> = vec![];
// let mut r : NodeSt = NodeSt::default();
let mut b = false;
while it.peek().unwrap().value != TokenKind::Fn && b == false {
match NodeSt::parser(&mut it) {
Ok(n) => match n.c.value {
NodeKind::Fn => {
b = true;
}
NodeKind::NewAssign => {
let mut _s = String::new();
match n.to_owned().lhs.unwrap().c.value {
NodeKind::Ident(si) => _s = si,
_ => {
match n.to_owned().lhs.unwrap().lhs.unwrap().c.value {
NodeKind::Ident(si) => _s = si,
_ => unreachable!(),
};
}
}
match NodeArr::find_l(_s.to_owned(), g.to_owned()) {
Some(mut f) => {
match uv.contains(&f.to_owned().s.to_owned()) {
true => (),
false => return Err(ParseError::UnusedVariable(f.n.c.loc)),
}
uv.retain(|s| s != &f.to_owned().s.to_owned());
let mut n = vex(
&mut n.to_owned().rhs.unwrap().to_owned(),
g.to_owned(),
_l.to_vec(),
&mut uv,
);
n = simplified::exec(n);
f.n = n;
let ff = f.to_owned();
g.retain(|s| s.s != _s.to_owned());
g.push(ff);
}
_ => {
let mut n = vex(
&mut n.to_owned().rhs.unwrap().to_owned(),
g.to_owned(),
_l.to_vec(),
&mut uv,
);
n = simplified::exec(n);
let v = Var::new(_s, n);
g.push(v);
}
}
continue;
}
_ => return Err(ParseError::Eof),
},
Err(e) => return Err(e),
}
}

println!("g: {:?}", g);
return Ok(g);
}
}
28 changes: 14 additions & 14 deletions src/tests/parser/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use super::super::super::token::token::*;
fn parser_test() {
let t = Token::tokenize("fn int { 12+3; }").unwrap();
let mut t = t.iter().peekable();
let n = NodeArr::w_parser(&mut t).unwrap().ret_node_st;
let n = NodeArr::w_parser(&mut t, vec![]).unwrap().ret_node_st;
let e = {
NodeSt {
c: Node::plus(Loc::new(14, 15)),
Expand All @@ -29,7 +29,7 @@ fn parser_test() {
fn evaluation_final_value_test() {
let t = Token::tokenize("fn int { 12+3 }").unwrap();
let mut t = t.iter().peekable();
let n = NodeArr::w_parser(&mut t).unwrap().ret_node_st;
let n = NodeArr::w_parser(&mut t, vec![]).unwrap().ret_node_st;
let e = {
NodeSt {
c: Node::plus(Loc::new(14, 15)),
Expand All @@ -45,7 +45,7 @@ fn evaluation_final_value_test() {
fn parser_assign_test() {
let t = Token::tokenize("fn int { int a = 3; a }").unwrap();
let mut t = t.iter().peekable();
let n = NodeArr::w_parser(&mut t).unwrap().ret_node_st;
let n = NodeArr::w_parser(&mut t, vec![]).unwrap().ret_node_st;
let e = { NodeSt::num(3, n.to_owned().c.loc) };
assert_eq!(e, n)
}
Expand All @@ -54,7 +54,7 @@ fn parser_assign_test() {
fn variable_expansion_test() {
let t = Token::tokenize("fn int { int a = 3; int b = 5; b*a; }").unwrap();
let mut t = t.iter().peekable();
let n = NodeArr::w_parser(&mut t).unwrap().ret_node_st;
let n = NodeArr::w_parser(&mut t, vec![]).unwrap().ret_node_st;
let e = NodeSt {
c: Node::mul(Loc::new(32, 33)),
lhs: Some(n.to_owned().lhs.unwrap()),
Expand All @@ -69,7 +69,7 @@ fn variable_expansion_test() {
fn return_with_unclosed_test() {
let t = Token::tokenize("fn int { return 3 }").unwrap();
let mut t = t.iter().peekable();
let n = match NodeArr::w_parser(&mut t) {
let n = match NodeArr::w_parser(&mut t, vec![]) {
Ok(_) => false,
Err(e) => match e {
ParseError::NotClosedStmt(_) => true,
Expand All @@ -83,7 +83,7 @@ fn return_with_unclosed_test() {
fn operater_after_return_test() {
let t = Token::tokenize("fn int { return 3; 4 }").unwrap();
let mut t = t.iter().peekable();
let n = match NodeArr::w_parser(&mut t) {
let n = match NodeArr::w_parser(&mut t, vec![]) {
Ok(_) => false,
Err(e) => match e {
ParseError::OperatorAfterRetrun(_) => true,
Expand All @@ -97,7 +97,7 @@ fn operater_after_return_test() {
fn not_exit_when_failed_parser_test() {
let t = Token::tokenize("fn { +3 }").unwrap();
let mut t = t.iter().peekable();
let n = match NodeArr::w_parser(&mut t) {
let n = match NodeArr::w_parser(&mut t, vec![]) {
Ok(_) => false,
Err(_) => true,
};
Expand All @@ -108,7 +108,7 @@ fn not_exit_when_failed_parser_test() {
fn not_number_test() {
let t = Token::tokenize("fn { 5+ }").unwrap();
let mut t = t.iter().peekable();
let n = match NodeArr::w_parser(&mut t) {
let n = match NodeArr::w_parser(&mut t, vec![]) {
Ok(_) => false,
Err(e) => match e {
ParseError::NotNumber(_) => true,
Expand All @@ -122,7 +122,7 @@ fn not_number_test() {
fn unclosed_paren_test() {
let t = Token::tokenize("fn { 5+(3+2*2 }").unwrap();
let mut t = t.iter().peekable();
let n = match NodeArr::w_parser(&mut t) {
let n = match NodeArr::w_parser(&mut t, vec![]) {
Ok(_) => false,
Err(e) => match e {
ParseError::NotClosedParen(_) => true,
Expand All @@ -136,7 +136,7 @@ fn unclosed_paren_test() {
fn operator_out_of_function_test() {
let t = Token::tokenize("0; fn { 12+3; }").unwrap();
let mut t = t.iter().peekable();
let n = match NodeArr::w_parser(&mut t) {
let n = match NodeArr::w_parser(&mut t, vec![]) {
Ok(_) => false,
Err(e) => match e {
ParseError::OperatorOutOfFnction(_) => true,
Expand All @@ -150,7 +150,7 @@ fn operator_out_of_function_test() {
fn not_match_return_type_test() {
let t = Token::tokenize("fn { 12+3; }").unwrap();
let mut t = t.iter().peekable();
let n = match NodeArr::w_parser(&mut t) {
let n = match NodeArr::w_parser(&mut t, vec![]) {
Ok(_) => false,
Err(e) => match e {
ParseError::NotMatchReturnType(_) => true,
Expand All @@ -164,7 +164,7 @@ fn not_match_return_type_test() {
fn match_void_type_test() {
let t = Token::tokenize("fn { _ }").unwrap();
let mut t = t.iter().peekable();
let n = match NodeArr::w_parser(&mut t) {
let n = match NodeArr::w_parser(&mut t, vec![]) {
Ok(_) => true,
_ => false,
};
Expand All @@ -175,7 +175,7 @@ fn match_void_type_test() {
fn not_match_void_type_test() {
let t = Token::tokenize("fn int { _ }").unwrap();
let mut t = t.iter().peekable();
let n = match NodeArr::w_parser(&mut t) {
let n = match NodeArr::w_parser(&mut t, vec![]) {
Ok(_) => false,
Err(e) => match e {
ParseError::NotMatchReturnType(_) => true,
Expand All @@ -189,7 +189,7 @@ fn not_match_void_type_test() {
fn unexpected_underscore_operator_test() {
let t = Token::tokenize("fn int { _; 1 }").unwrap();
let mut t = t.iter().peekable();
let n = match NodeArr::w_parser(&mut t) {
let n = match NodeArr::w_parser(&mut t, vec![]) {
Ok(_) => false,
Err(e) => match e {
ParseError::UnexpectedUnderScoreOperator(_) => true,
Expand Down
4 changes: 2 additions & 2 deletions src/tests/simplified/simplified/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use super::super::super::super::token::token::*;
fn simplified_test() {
let t = Token::tokenize("fn int { 2*(2-1)+5; }").unwrap();
let mut t = t.iter().peekable();
let n = NodeArr::w_parser(&mut t).unwrap();
let n = NodeArr::w_parser(&mut t, vec![]).unwrap();
let n = simplified(n).ret_node_st;
let l = NodeSt::num(7, Loc::new(12, 13));
assert_eq!(l, n);
Expand All @@ -23,7 +23,7 @@ fn simplified_test() {
fn simplified_with_minus_test() {
let t = Token::tokenize("fn int { 2*(2-4)+5; }").unwrap();
let mut t = t.iter().peekable();
let n = NodeArr::w_parser(&mut t).unwrap();
let n = NodeArr::w_parser(&mut t, vec![]).unwrap();
let n = simplified(n).ret_node_st;
let l = NodeSt::num(1, Loc::new(12, 13));
assert_eq!(l, n);
Expand Down
Loading

0 comments on commit 4df4ee8

Please sign in to comment.