From 73e25a571f12d3deaa6f4493a5b4792a9dae39eb Mon Sep 17 00:00:00 2001 From: johannst Date: Sat, 24 Sep 2022 22:38:40 +0000 Subject: deploy: 295081130ca1eed6e67dfc035e2df2c9ed49b174 --- src/llvm_kaleidoscope_rs/lexer.rs.html | 286 ++++++++++++++++++++------------- 1 file changed, 170 insertions(+), 116 deletions(-) (limited to 'src/llvm_kaleidoscope_rs/lexer.rs.html') diff --git a/src/llvm_kaleidoscope_rs/lexer.rs.html b/src/llvm_kaleidoscope_rs/lexer.rs.html index 4b13fd9..2249ba0 100644 --- a/src/llvm_kaleidoscope_rs/lexer.rs.html +++ b/src/llvm_kaleidoscope_rs/lexer.rs.html @@ -1,102 +1,108 @@ -lexer.rs - source
  1
-  2
-  3
-  4
-  5
-  6
-  7
-  8
-  9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
+lexer.rs - source
+    
1
+2
+3
+4
+5
+6
+7
+8
+9
+10
+11
+12
+13
+14
+15
+16
+17
+18
+19
+20
+21
+22
+23
+24
+25
+26
+27
+28
+29
+30
+31
+32
+33
+34
+35
+36
+37
+38
+39
+40
+41
+42
+43
+44
+45
+46
+47
+48
+49
+50
+51
+52
+53
+54
+55
+56
+57
+58
+59
+60
+61
+62
+63
+64
+65
+66
+67
+68
+69
+70
+71
+72
+73
+74
+75
+76
+77
+78
+79
+80
+81
+82
+83
+84
+85
+86
+87
+88
+89
+90
+91
+92
+93
+94
+95
+96
+97
+98
+99
 100
 101
 102
@@ -179,8 +185,32 @@
 179
 180
 181
-
-#[derive(Debug, PartialEq)]
+182
+183
+184
+185
+186
+187
+188
+189
+190
+191
+192
+193
+194
+195
+196
+197
+198
+199
+200
+201
+202
+203
+204
+205
+206
+
#[derive(Debug, PartialEq)]
 pub enum Token {
     Eof,
     Def,
@@ -188,6 +218,11 @@
     Identifier(String),
     Number(f64),
     Char(char),
+    If,
+    Then,
+    Else,
+    For,
+    In,
 }
 
 pub struct Lexer<I>
@@ -202,12 +237,12 @@
 where
     I: Iterator<Item = char>,
 {
-    pub fn new(mut input: I) -> Lexer<I> {
+    pub fn new(mut input: I) -> Lexer<I> {
         let last_char = input.next();
         Lexer { input, last_char }
     }
 
-    fn step(&mut self) -> Option<char> {
+    fn step(&mut self) -> Option<char> {
         self.last_char = self.input.next();
         self.last_char
     }
@@ -215,7 +250,7 @@
     /// Lex and return the next token.
     ///
     /// Implement `int gettok();` from the tutorial.
-    pub fn gettok(&mut self) -> Token {
+    pub fn gettok(&mut self) -> Token {
         // Eat up whitespaces.
         while matches!(self.last_char, Some(c) if c.is_ascii_whitespace()) {
             self.step();
@@ -242,21 +277,26 @@
             }
 
             match ident.as_ref() {
-                "def" => return Token::Def,
-                "extern" => return Token::Extern,
-                _ => {}
+                "def" => return Token::Def,
+                "extern" => return Token::Extern,
+                "if" => return Token::If,
+                "then" => return Token::Then,
+                "else" => return Token::Else,
+                "for" => return Token::For,
+                "in" => return Token::In,
+                _ => {}
             }
 
             return Token::Identifier(ident);
         }
 
         // Number: [0-9.]+
-        if last_char.is_ascii_digit() || last_char == '.' {
+        if last_char.is_ascii_digit() || last_char == '.' {
             let mut num = String::new();
             num.push(last_char);
 
             while let Some(c) = self.step() {
-                if c.is_ascii_digit() || c == '.' {
+                if c.is_ascii_digit() || c == '.' {
                     num.push(c)
                 } else {
                     break;
@@ -268,12 +308,12 @@
         }
 
         // Eat up comment.
-        if last_char == '#' {
+        if last_char == '#' {
             loop {
                 match self.step() {
-                    Some(c) if c == '\r' || c == '\n' => return self.gettok(),
-                    None => return Token::Eof,
-                    _ => { /* consume comment */ }
+                    Some(c) if c == '\r' || c == '\n' => return self.gettok(),
+                    None => return Token::Eof,
+                    _ => { /* consume comment */ }
                 }
             }
         }
@@ -360,8 +400,22 @@
         assert_eq!(Token::Identifier("c".into()), lex.gettok());
         assert_eq!(Token::Eof, lex.gettok());
     }
+
+    #[test]
+    fn test_ite() {
+        let mut lex = Lexer::new("if then else".chars());
+        assert_eq!(Token::If, lex.gettok());
+        assert_eq!(Token::Then, lex.gettok());
+        assert_eq!(Token::Else, lex.gettok());
+    }
+
+    #[test]
+    fn test_for() {
+        let mut lex = Lexer::new("for in".chars());
+        assert_eq!(Token::For, lex.gettok());
+        assert_eq!(Token::In, lex.gettok());
+    }
 }
-
-
- +
+
\ No newline at end of file -- cgit v1.2.3