From 466215a381695b7ecbf29b07b558684cfd43ec8f Mon Sep 17 00:00:00 2001
From: LunaStev <96914208+LunaStev@users.noreply.github.com>
Date: Tue, 26 Nov 2024 14:50:15 +0900
Subject: [PATCH] rollback

---
 src/parser.rs | 315 ++++++++++++++++++--------------------------------
 1 file changed, 113 insertions(+), 202 deletions(-)

diff --git a/src/parser.rs b/src/parser.rs
index 82d1433..0a80faf 100644
--- a/src/parser.rs
+++ b/src/parser.rs
@@ -1,4 +1,4 @@
-use crate::ast::ASTNode;
+use std::thread::sleep;
 use crate::lexer::{Lexer, Token, TokenType};
 
 #[derive(Debug)]
@@ -13,11 +13,9 @@ impl<'a> Parser<'a> {
         Parser { lexer, current_token }
     }
 
-    pub fn parse(&mut self) -> Vec<ASTNode> {
-        let mut nodes = Vec::new();
-
+    pub fn parse(&mut self) {
         while self.current_token.token_type != TokenType::EOF {
-            let node = match self.current_token.token_type {
+            match self.current_token.token_type {
                 TokenType::FUN => self.function(),
                 TokenType::VAR => self.variable(),
                 TokenType::IF => self.if_statement(),
@@ -25,31 +23,25 @@ impl<'a> Parser<'a> {
                 TokenType::FOR => self.for_statement(),
                 TokenType::IMPORT => self.import_statement(),
                 TokenType::PRINT | TokenType::PRINTLN => self.print_statement(),
-                _ => {
-                    self.advance();
-                    continue;
-                }
-            };
-            nodes.push(node);
+                _ => self.advance(),
+            }
         }
-
-        nodes
     }
 
     fn advance(&mut self) {
         self.current_token = self.lexer.next_token();
     }
 
-    fn function(&mut self) -> ASTNode {
+    fn function(&mut self) {
+        // 함수 구문 처리
         self.advance(); // `fun`
 
-        let name = if let TokenType::IDENTIFIER(name) = &self.current_token.token_type {
-            let name = name.clone();
+        if let TokenType::IDENTIFIER(name) = &self.current_token.token_type {
+            println!("Parsing function: {}", name);
             self.advance();
-            name
         } else {
             panic!("Expected function name after 'fun'");
-        };
+        }
 
         if self.current_token.token_type != TokenType::LPAREN {
             panic!("Expected '(' after function name");
@@ -61,6 +53,7 @@ impl<'a> Parser<'a> {
             if let TokenType::IDENTIFIER(param_name) = &self.current_token.token_type {
                 params.push(param_name.clone());
                 self.advance();
+
                 if self.current_token.token_type == TokenType::COMMA {
                     self.advance();
                 }
@@ -68,115 +61,101 @@ impl<'a> Parser<'a> {
                 panic!("Expected parameter name in function parameter list");
             }
         }
-        self.advance(); // `)`
+        self.advance();
 
         if self.current_token.token_type != TokenType::LBRACE {
-            panic!("Expected LBRACE at the beginning of function body");
+            panic!("Expected 'LBRACE' at the beginning of function body");
         }
         self.advance();
 
-        let mut body = Vec::new();
         while self.current_token.token_type != TokenType::RBRACE {
-            body.push(self.parse_statement());
+            self.advance();
         }
-        self.advance(); // `}`
+        self.advance();
 
-        ASTNode::Function {
-            name,
-            params,
-            body,
-        }
     }
 
-    fn variable(&mut self) -> ASTNode {
+    fn variable(&mut self) {
+        // 변수 구문 처리
         self.advance(); // `var`
 
-        let name = if let TokenType::IDENTIFIER(var_name) = &self.current_token.token_type {
-            let var_name = var_name.clone();  // 먼저 불변 참조로 값을 추출
-            self.advance();  // advance() 호출은 그 후에
-            var_name
+        if let TokenType::IDENTIFIER(var_name) = &self.current_token.token_type {
+            println!("Parsing variable: {}", var_name);
+            self.advance();
         } else {
-            panic!("Expected variable name");
+            panic!("Expected variable name after 'var'");
         };
 
         if self.current_token.token_type == TokenType::COLON {
             self.advance();
 
-            let var_type = match &self.current_token.token_type {
-                TokenType::TYPE_INT(int_type) => format!("{:?}", int_type),
-                TokenType::TYPE_FLOAT(float_type) => format!("{:?}", float_type),
-                TokenType::TYPE_STRING => "String".to_string(),
-                _ => panic!("Expected a valid type after ':'"),
-            };
-            self.advance();
-
-            if self.current_token.token_type != TokenType::EQUAL {
-                panic!("Expected '=' after variable name");
-            }
-            self.advance();
-
-            let value = match &self.current_token.token_type {
-                TokenType::NUMBER(value) => value.to_string(),
-                TokenType::STRING(value) => value.clone(),
-                _ => panic!("Expected a numeric or string initial value after '='"),
-            };
-            self.advance();
-
-            if self.current_token.token_type != TokenType::SEMICOLON {
-                panic!("Expected ';' at the end of variable declaration");
-            }
-            self.advance();
-
-            ASTNode::Variable {
-                name,
-                var_type,
-                value,
+            match &self.current_token.token_type {
+                TokenType::TYPE_INT(int_type) => {
+                    println!("Type of variable: {:?}", int_type);
+                    self.advance();
+                },
+                TokenType::TYPE_FLOAT(float_type) => {
+                    println!("Type of variable: {:?}", float_type);
+                    self.advance();
+                },
+                TokenType::TYPE_STRING => {
+                    println!("Type of variable: String");
+                    self.advance();
+                },
+                _ => {
+                    panic!("Expected a valid type after ':'");
+                }
             }
         } else {
             panic!("Expected ':' after variable name");
         }
-    }
 
-    fn print_statement(&mut self) -> ASTNode {
-        let is_println = matches!(self.current_token.token_type, TokenType::PRINTLN);
-        self.advance(); // Move past `print` or `println`
-
-        let value = match &self.current_token.token_type {
-            TokenType::STRING(content) => {
-                let content = content.clone();
-                self.advance(); // Move past the string literal
-                ASTNode::Literal {
-                    value: content,
-                    is_println,
-                }
+        if self.current_token.token_type != TokenType::EQUAL {
+            panic!("Expected '=' after variable name");
+        }
+        self.advance();
+
+        match &self.current_token.token_type {
+            TokenType::NUMBER(value) => {
+                println!("Initial value: {}", value);
+                self.advance();
             },
-            TokenType::NUMBER(num) => {
-                let num = num.to_string();
-                self.advance(); // Move past the number literal
-                ASTNode::Literal {
-                    value: num,
-                    is_println,
-                }
+            TokenType::STRING(value) => {
+                println!("Initial value: \"{}\"", value);
+                self.advance();
             },
-            _ => panic!("Expected a literal (string or number) after print/println"),
-        };
+            _ => panic!("Expected a numeric inital value after '='"),
+        }
 
         if self.current_token.token_type != TokenType::SEMICOLON {
-            panic!("Expected ';' at the end of print/println statement");
+            panic!("Expected ';' at the end of variable declaration");
         }
-        self.advance(); // Move past the semicolon
+        self.advance();
 
-        // Return the Print node that uses the value from the Literal
-        ASTNode::Print {
-            message: match value {
-                ASTNode::Literal { value, .. } => value,
-                _ => unreachable!(), // This should never happen
-            },
-        }
     }
 
+    fn print_statement(&mut self) {
+        self.advance();
+        if let TokenType::STRING(_) = &self.current_token.token_type {
+            // It's a string, proceed with extracting the string.
+        } else {
+            panic!("Expected a string literal after print/println");
+        }
+        let message = match &self.current_token.token_type {
+            TokenType::STRING(s) => s,
+            _ => panic!("Expected a string"),
+        };
+
+        if self.current_token.token_type == TokenType::PRINTLN {
+            println!("{}", message);
+        } else {
+            print!("{}", message);
+        }
+        self.advance();
+    }
 
-    fn if_statement(&mut self) -> ASTNode {
+    fn if_statement(&mut self) {
+        // if 구문 처리
         self.advance(); // `if`
 
         if self.current_token.token_type != TokenType::LPAREN {
@@ -184,8 +163,12 @@ impl<'a> Parser<'a> {
         }
         self.advance();
 
-        // 여기서 조건 노드 파싱 로직 필요 (예: 변수 또는 표현식)
-        let condition = self.parse_expression();
+        if let TokenType::NUMBER(value) = &self.current_token.token_type {
+            println!("Condition value: {}", value);
+            self.advance();
+        } else {
+            panic!("Expected a condition after '('");
+        }
 
         if self.current_token.token_type != TokenType::RPAREN {
             panic!("Expected ')' after condition");
@@ -193,41 +176,32 @@ impl<'a> Parser<'a> {
         self.advance();
 
         if self.current_token.token_type != TokenType::LBRACE {
-            panic!("Expected LBRACE at the beginning of if body");
+            panic!("Expected 'LBRACE' at the beginning of if body");
         }
         self.advance();
 
-        let mut body = Vec::new();
         while self.current_token.token_type != TokenType::RBRACE {
-            body.push(self.parse_statement());
+            self.advance();
         }
-        self.advance(); // `}`
+        self.advance();
 
-        let else_body = if self.current_token.token_type == TokenType::ELSE {
+        if self.current_token.token_type == TokenType::ELSE {
             self.advance();
+
             if self.current_token.token_type != TokenType::LBRACE {
-                panic!("Expected LBRACE at the beginning of else body");
+                panic!("Expected 'LBRACE' at the beginning of else body");
             }
             self.advance();
 
-            let mut else_body = Vec::new();
             while self.current_token.token_type != TokenType::RBRACE {
-                else_body.push(self.parse_statement());
+                self.advance();
             }
-            self.advance(); // `}`
-            Some(else_body)
-        } else {
-            None
-        };
-
-        ASTNode::If {
-            condition: Box::new(condition),
-            body,
-            else_body,
+            self.advance();
         }
     }
 
-    fn while_statement(&mut self) -> ASTNode {
+    fn while_statement(&mut self) {
+        // while 구문 처리
         self.advance(); // `while`
 
         if self.current_token.token_type != TokenType::LPAREN {
@@ -235,7 +209,12 @@ impl<'a> Parser<'a> {
         }
         self.advance();
 
-        let condition = self.parse_expression();
+        if let TokenType::NUMBER(value) = &self.current_token.token_type {
+            println!("Condition value: {}", value);
+            self.advance();
+        } else {
+            panic!("Expected a conditon after '('");
+        }
 
         if self.current_token.token_type != TokenType::RPAREN {
             panic!("Expected ')' after condition");
@@ -243,57 +222,30 @@ impl<'a> Parser<'a> {
         self.advance();
 
         if self.current_token.token_type != TokenType::LBRACE {
-            panic!("Expected LBRACE at the beginning of while body");
+            panic!("Expected 'LBRACE' at the beginning of while body");
         }
         self.advance();
 
-        let mut body = Vec::new();
         while self.current_token.token_type != TokenType::RBRACE {
-            body.push(self.parse_statement());
-        }
-        self.advance(); // `}`
-
-        ASTNode::While {
-            condition: Box::new(condition),
-            body,
+            self.advance();
         }
+        self.advance();
     }
 
-    fn for_statement(&mut self) -> ASTNode {
-        self.advance(); // `for`
+    fn for_statement(&mut self) {
+        // for 구문 처리
+        self.advance(); // 'for'
 
         if self.current_token.token_type != TokenType::LPAREN {
             panic!("Expected '(' after 'for'");
         }
-        self.advance();
-
-        // `for` 구문 파싱 로직 추가 필요 (초기화, 조건, 증감식 등)
 
-        if self.current_token.token_type != TokenType::RPAREN {
-            panic!("Expected ')' after for conditions");
-        }
-        self.advance();
 
-        if self.current_token.token_type != TokenType::LBRACE {
-            panic!("Expected LBRACE at the beginning of for body");
-        }
         self.advance();
-
-        let mut body = Vec::new();
-        while self.current_token.token_type != TokenType::RBRACE {
-            body.push(self.parse_statement());
-        }
-        self.advance(); // `}`
-
-        ASTNode::For {
-            init: None,
-            condition: None,
-            increment: None,
-            body,
-        }
     }
 
-    fn import_statement(&mut self) -> ASTNode {
+    fn import_statement(&mut self) {
+        // import 구문 처리
         self.advance(); // `import`
 
         if self.current_token.token_type != TokenType::LPAREN {
@@ -301,64 +253,23 @@ impl<'a> Parser<'a> {
         }
         self.advance();
 
-        let module_name = if let TokenType::STRING(name) = &self.current_token.token_type {
-            name.clone()
+        if let TokenType::STRING(module_name) = &self.current_token.token_type {
+            println!("Importing module: {}", module_name); // 모듈 이름 출력
+            self.advance();
         } else {
-            panic!("Expected module name string after '('");
-        };
-        self.advance(); // 모듈명 건너뜀
+            panic!("Expected a module name string after '('");
+        }
 
+        // ')' 토큰 체크
         if self.current_token.token_type != TokenType::RPAREN {
             panic!("Expected ')' after module name");
         }
-        self.advance(); // `)` 건너뜀
+        self.advance(); // ')' 건너뜀
 
+        // 문장의 끝을 의미하는 `;` 체크
         if self.current_token.token_type != TokenType::SEMICOLON {
             panic!("Expected ';' at the end of import statement");
         }
         self.advance(); // `;` 건너뜀
-
-        ASTNode::Import {
-            module_name,
-        }
     }
-
-    pub fn parse_expression(&mut self) -> ASTNode {
-        match self.current_token.token_type {
-            TokenType::NUMBER(num) => {
-                self.advance(); // 숫자 파싱 후 다음 토큰으로 이동
-                ASTNode::Number(num)
-            }
-            TokenType::STRING(ref content) => {
-                let content_clone = content.clone();
-
-                self.advance();
-
-                ASTNode::String(content_clone)
-            }
-            TokenType::VAR => {
-                let var_name = self.current_token.lexeme.to_string();
-                self.advance(); // 변수 이름 후 이동
-                ASTNode::Variable {
-                    name: var_name,
-                    var_type: "unknown".to_string(),
-                    value: "undefined".to_string(),
-                }
-            }
-            _ => panic!("Unexpected token while parsing expression"),
-        }
-    }
-
-    pub fn parse_statement(&mut self) -> ASTNode {
-        match self.current_token.token_type {
-            TokenType::PRINT | TokenType::PRINTLN => self.print_statement(),
-            TokenType::IF => self.if_statement(),
-            TokenType::WHILE => self.while_statement(),
-            TokenType::FOR => self.for_statement(),
-            TokenType::VAR => self.variable(),
-            TokenType::FUN => self.function(),
-            TokenType::IMPORT => self.import_statement(),
-            _ => panic!("Unexpected token in statement parsing"),
-        }
-    }
-}
\ No newline at end of file
+}