Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- match self.peek() {
- &Kind::ID(ref name) => {
- self.consume();
- match self.peek() {
- &Kind::EQ => {
- self.consume();
- let v = self.expression();
- if (doit == 1) {
- self.vars.insert(name.to_string(),v);
- }
- },
- &Kind::CALL => {
- if (doit == 1) {
- let lastIndex = self.currIndex;
- let temp : usize = *self.vars.get(name).unwrap() as usize;
- let nextIndex = self.functions[temp];
- self.currIndex = nextIndex;
- self.statement(doit);
- self.currIndex = lastIndex;
- self.consume();
- }
- else {
- self.consume();
- }
- },
- _ => {
- self.error();
- }
- }
- return 1;
- },
- &Kind::LBRACE => {
- self.consume();
- self.seq(doit);
- match self.peek() {
- RBRACE => self.error()
- };
- self.consume();
- return 1;
- },
- &Kind::IF => {
- self.consume();
- let result = self.expression();
- self.statement(if (result != 0 && doit == 1) {1} else {0});
- match self.peek() {
- &Kind::ELSE => {
- self.consume();
- self.statement(if (result == 0 && doit == 1) {1} else {0});
- },
- _ => {}
- }
- return 1;
- },
- &Kind::WHILE => {
- let startIndex = self.currIndex;
- self.consume();
- while (self.expression() != 0 && doit == 1){
- self.statement(1);
- self.currIndex = startIndex;
- self.consume();
- //self.consume();
- }
- self.statement(0);
- return 1;
- },
- &Kind::PRINT => {
- self.consume();
- //self.seq(1);
- if (doit == 1) {
- println!("{}",self.expression());
- }
- else {
- self.expression();
- }
- return 1;
- },
- _ => return 0
- }
- }
- unsafe fn seq(&mut self, doit: u32){
- while self.statement(doit) == 1 {}
- }
- unsafe fn run(&mut self) {
- self.seq(1);
- match self.peek() {
- &Kind::END => println!("ran program succesfully"),
- _ => println!("exited run when not at end")
- }
- }
- fn parseTokens(&mut self) {
- let mut done = false;
- println!("there are {} characters",self.chars.len());
- while !done {
- let startIndex = self.currIndex;
- let nextKind = self.getToken();
- // let mut name = String::new();
- // match nextKind{
- // Kind::ID => {for i in startIndex..self.currIndex {
- // //println!("char = {}",self.chars[i]);
- // name.push(self.chars[i]);
- // }},
- // _ => {}
- // }
- println!("{:?}",self.currIndex);
- println!("{:?}",nextKind);
- done = match nextKind {
- Kind::END => true,
- Kind::NONE => false,
- _ => false
- }; //&& self.currIndex < self.chars.len();
- match nextKind {
- Kind::NONE => {},
- Kind::WHITESPACE => {},
- // Kind::ID => self.tokens.push(Kind(name)),
- // Kind::INT => self.tokens.push(Kind(value)),
- _ => self.tokens.push(nextKind)
- };
- }
- }
- fn getToken(&mut self) -> Kind {
- if (self.currIndex >= self.chars.len()) {
- return Kind::END;
- }
- let mut curr_char = self.chars[self.currIndex];
- self.currIndex += 1;
- println!("starting char = {}",curr_char);
- if (curr_char.is_whitespace()) {
- return Kind::WHITESPACE;
- }
- else if (curr_char == '('){
- let mut startIndex : usize = self.currIndex;
- let mut nextChar = self.chars[startIndex];
- while nextChar.is_whitespace() {
- startIndex += 1;
- nextChar = self.chars[startIndex];
- }
- if (nextChar == ')') {
- self.currIndex += 1;
- return Kind::FUN;
- }
- return Kind::LEFT;
- }
- else if (curr_char == ')') {
- return Kind::RIGHT;
- }
- else if (curr_char == '{') {
- return Kind::LBRACE;
- }
- else if (curr_char == '}') {
- return Kind::RBRACE;
- }
- else if (curr_char == '*') {
- return Kind::MUL;
- }
- else if (curr_char == '+') {
- return Kind::PLUS;
- }
- else if (curr_char == '='){
- if (self.chars[self.currIndex] == '='){
- self.currIndex += 1;
- return Kind::EQEQ;
- }
- else {
- return Kind::EQ;
- }
- }
- else if (curr_char.is_alphabetic()){
- let startIndex = self.currIndex - 1;
- self.currIndex -= 1;
- let mut curr_char = self.chars[self.currIndex];
- let mut has_digits = false;
- println!("curr_char = {}",curr_char);
- while (curr_char.is_alphabetic() || curr_char.is_digit(10)){
- has_digits = (has_digits) || curr_char.is_digit(10);
- self.currIndex += 1;
- curr_char = self.chars[self.currIndex];
- }
- let mut keyword = String::new();
- for i in startIndex..self.currIndex {
- println!("char = {}",self.chars[i]);
- keyword.push(self.chars[i]);
- }
- //self.currIndex -= 1;
- if has_digits {
- return Kind::ID(keyword);
- }
- //rintf("running getKeyword()");
- //let char_arr = &self.chars;
- //let keyword = char_arr[startIndex..self.currIndex].iter().cloned().collect();
- println!("keyword = {}",keyword);
- return match keyword.as_ref() {
- "if" => Kind::IF,
- "else" => Kind::ELSE,
- "print" => Kind::PRINT,
- "while" => Kind::WHILE,
- "fun" => Kind::FUN,
- _ => Kind::ID(keyword)
- };
- // return getKeyword(start_ptr,self.currIndex);
- // return Kind::PRINT;
- }
- else if (curr_char.is_digit(10)){
- //printf("getToken() found num\n");
- let startIndex = self.currIndex - 1;
- self.currIndex -= 1;
- while (curr_char.is_digit(10) || curr_char == '_'){
- self.currIndex += 1;
- curr_char = self.chars[self.currIndex];
- }
- let mut num = String::new();
- for i in startIndex..self.currIndex {
- //println!("char = {}",self.chars[i]);
- num.push(self.chars[i]);
- }
- // self.currIndex -= 1;
- return Kind::INT(num.parse::<u64>().unwrap());
- }
- else {
- return Kind::NONE;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement