From 22dd5bc5862e91330c8da0d5f141998cdbe546fb Mon Sep 17 00:00:00 2001 From: Graham Northup Date: Tue, 3 Oct 2017 14:50:18 -0400 Subject: Finished clientside parsing, starting graphics --- src/lang/parser.rs | 53 +++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 43 insertions(+), 10 deletions(-) (limited to 'src/lang/parser.rs') diff --git a/src/lang/parser.rs b/src/lang/parser.rs index 7419a56..c49cf65 100644 --- a/src/lang/parser.rs +++ b/src/lang/parser.rs @@ -55,16 +55,18 @@ impl fmt::Display for ErrorType { pub struct Parser> { tzr: Tokenizer, + env: Environment, token: Token, pushback: Option, factories: HashMap, } impl> Parser { - pub fn new(mut tzr: Tokenizer) -> Result, Box> { + pub fn new(mut tzr: Tokenizer, env: Environment) -> Result, Box> { let token = tzr.next_token()?; Ok(Parser { tzr: tzr, + env: env, token: token, pushback: None, factories: all_factories(), @@ -105,15 +107,51 @@ impl> Parser { } } - pub fn parse(&mut self) -> Result> { - self.parse_gen() + pub fn parse_gen_vec(&mut self) -> Result, Box> { + let mut ret: Vec = Vec::new(); + self.expect_op('[')?; + + + loop { + if self.expect_op(']').is_ok() { + break; + } + + /* TODO: Can't yet clone a GenBox safely + let repeat = match self.token { + Token::Integer(v) => { + self.expect_op('*')?; + v + }, + _ => 1, + }; + */ + + ret.push(self.parse_gen()?); + + if self.expect_op(',').is_err() { + self.expect_op(']')?; + break; + } + } + + Ok(ret) } pub fn parse_gen(&mut self) -> Result> { let name = self.expect_ident()?; + let mut params = self.parse_factory_params()?; + let factory = match self.factories.get(&name) { + Some(fac) => fac, + None => return Err(ErrorType::new(ErrorKind::UnknownGen(name)).into()), + }; + factory.new(&mut params).map_err(Into::into) + } + pub fn parse_factory_params(&mut self) -> Result> { self.expect_op('(')?; - let mut params: FactoryParameters = Default::default(); + + let mut params: FactoryParameters = FactoryParameters { env: self.env.clone(), ..Default::default() }; let mut ctr = 0; loop { if self.expect_op(')').is_ok() { @@ -124,17 +162,12 @@ impl> Parser { ctr = new_ctr; if self.expect_op(',').is_err() { - eprintln!("No comma: {:?}", self.token); self.expect_op(')')?; break; } } - let factory = match self.factories.get(&name) { - Some(fac) => fac, - None => return Err(ErrorType::new(ErrorKind::UnknownGen(name)).into()), - }; - factory.new(&mut params).map_err(Into::into) + Ok(params) } pub fn parse_param(&mut self, pos: usize) -> Result<(String, ParamValue, usize), Box> { -- cgit v1.2.3-70-g09d2