Advertisement
Guest User

Untitled

a guest
Apr 25th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.91 KB | None | 0 0
  1. match self.peek() {
  2. &Kind::ID(ref name) => {
  3. self.consume();
  4. match self.peek() {
  5. &Kind::EQ => {
  6. self.consume();
  7. let v = self.expression();
  8. if (doit == 1) {
  9. self.vars.insert(name.to_string(),v);
  10. }
  11. },
  12. &Kind::CALL => {
  13. if (doit == 1) {
  14. let lastIndex = self.currIndex;
  15. let temp : usize = *self.vars.get(name).unwrap() as usize;
  16. let nextIndex = self.functions[temp];
  17. self.currIndex = nextIndex;
  18. self.statement(doit);
  19. self.currIndex = lastIndex;
  20. self.consume();
  21. }
  22. else {
  23. self.consume();
  24. }
  25. },
  26. _ => {
  27. self.error();
  28. }
  29. }
  30. return 1;
  31. },
  32. &Kind::LBRACE => {
  33. self.consume();
  34. self.seq(doit);
  35. match self.peek() {
  36. RBRACE => self.error()
  37. };
  38. self.consume();
  39. return 1;
  40. },
  41. &Kind::IF => {
  42. self.consume();
  43. let result = self.expression();
  44. self.statement(if (result != 0 && doit == 1) {1} else {0});
  45. match self.peek() {
  46. &Kind::ELSE => {
  47. self.consume();
  48. self.statement(if (result == 0 && doit == 1) {1} else {0});
  49. },
  50. _ => {}
  51. }
  52. return 1;
  53. },
  54. &Kind::WHILE => {
  55. let startIndex = self.currIndex;
  56. self.consume();
  57. while (self.expression() != 0 && doit == 1){
  58. self.statement(1);
  59. self.currIndex = startIndex;
  60. self.consume();
  61. //self.consume();
  62. }
  63. self.statement(0);
  64. return 1;
  65. },
  66. &Kind::PRINT => {
  67. self.consume();
  68. //self.seq(1);
  69. if (doit == 1) {
  70. println!("{}",self.expression());
  71. }
  72. else {
  73. self.expression();
  74. }
  75. return 1;
  76. },
  77. _ => return 0
  78. }
  79. }
  80.  
  81. unsafe fn seq(&mut self, doit: u32){
  82. while self.statement(doit) == 1 {}
  83. }
  84.  
  85. unsafe fn run(&mut self) {
  86. self.seq(1);
  87. match self.peek() {
  88. &Kind::END => println!("ran program succesfully"),
  89. _ => println!("exited run when not at end")
  90. }
  91. }
  92.  
  93. fn parseTokens(&mut self) {
  94. let mut done = false;
  95. println!("there are {} characters",self.chars.len());
  96. while !done {
  97. let startIndex = self.currIndex;
  98. let nextKind = self.getToken();
  99. // let mut name = String::new();
  100. // match nextKind{
  101. // Kind::ID => {for i in startIndex..self.currIndex {
  102. // //println!("char = {}",self.chars[i]);
  103. // name.push(self.chars[i]);
  104. // }},
  105. // _ => {}
  106. // }
  107. println!("{:?}",self.currIndex);
  108. println!("{:?}",nextKind);
  109. done = match nextKind {
  110. Kind::END => true,
  111. Kind::NONE => false,
  112. _ => false
  113. }; //&& self.currIndex < self.chars.len();
  114. match nextKind {
  115. Kind::NONE => {},
  116. Kind::WHITESPACE => {},
  117. // Kind::ID => self.tokens.push(Kind(name)),
  118. // Kind::INT => self.tokens.push(Kind(value)),
  119. _ => self.tokens.push(nextKind)
  120. };
  121. }
  122. }
  123.  
  124. fn getToken(&mut self) -> Kind {
  125. if (self.currIndex >= self.chars.len()) {
  126. return Kind::END;
  127. }
  128. let mut curr_char = self.chars[self.currIndex];
  129. self.currIndex += 1;
  130. println!("starting char = {}",curr_char);
  131.  
  132. if (curr_char.is_whitespace()) {
  133. return Kind::WHITESPACE;
  134. }
  135. else if (curr_char == '('){
  136. let mut startIndex : usize = self.currIndex;
  137. let mut nextChar = self.chars[startIndex];
  138. while nextChar.is_whitespace() {
  139. startIndex += 1;
  140. nextChar = self.chars[startIndex];
  141. }
  142. if (nextChar == ')') {
  143. self.currIndex += 1;
  144. return Kind::FUN;
  145. }
  146. return Kind::LEFT;
  147. }
  148. else if (curr_char == ')') {
  149. return Kind::RIGHT;
  150. }
  151. else if (curr_char == '{') {
  152. return Kind::LBRACE;
  153. }
  154. else if (curr_char == '}') {
  155. return Kind::RBRACE;
  156. }
  157. else if (curr_char == '*') {
  158. return Kind::MUL;
  159. }
  160. else if (curr_char == '+') {
  161. return Kind::PLUS;
  162. }
  163. else if (curr_char == '='){
  164. if (self.chars[self.currIndex] == '='){
  165. self.currIndex += 1;
  166. return Kind::EQEQ;
  167. }
  168. else {
  169. return Kind::EQ;
  170. }
  171. }
  172. else if (curr_char.is_alphabetic()){
  173. let startIndex = self.currIndex - 1;
  174. self.currIndex -= 1;
  175. let mut curr_char = self.chars[self.currIndex];
  176. let mut has_digits = false;
  177. println!("curr_char = {}",curr_char);
  178. while (curr_char.is_alphabetic() || curr_char.is_digit(10)){
  179. has_digits = (has_digits) || curr_char.is_digit(10);
  180. self.currIndex += 1;
  181. curr_char = self.chars[self.currIndex];
  182. }
  183. let mut keyword = String::new();
  184. for i in startIndex..self.currIndex {
  185. println!("char = {}",self.chars[i]);
  186. keyword.push(self.chars[i]);
  187. }
  188. //self.currIndex -= 1;
  189. if has_digits {
  190. return Kind::ID(keyword);
  191. }
  192. //rintf("running getKeyword()");
  193. //let char_arr = &self.chars;
  194. //let keyword = char_arr[startIndex..self.currIndex].iter().cloned().collect();
  195. println!("keyword = {}",keyword);
  196. return match keyword.as_ref() {
  197. "if" => Kind::IF,
  198. "else" => Kind::ELSE,
  199. "print" => Kind::PRINT,
  200. "while" => Kind::WHILE,
  201. "fun" => Kind::FUN,
  202. _ => Kind::ID(keyword)
  203. };
  204. // return getKeyword(start_ptr,self.currIndex);
  205. // return Kind::PRINT;
  206. }
  207. else if (curr_char.is_digit(10)){
  208. //printf("getToken() found num\n");
  209. let startIndex = self.currIndex - 1;
  210. self.currIndex -= 1;
  211. while (curr_char.is_digit(10) || curr_char == '_'){
  212. self.currIndex += 1;
  213. curr_char = self.chars[self.currIndex];
  214. }
  215. let mut num = String::new();
  216. for i in startIndex..self.currIndex {
  217. //println!("char = {}",self.chars[i]);
  218. num.push(self.chars[i]);
  219. }
  220. // self.currIndex -= 1;
  221. return Kind::INT(num.parse::<u64>().unwrap());
  222. }
  223. else {
  224. return Kind::NONE;
  225. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement