Skip to content

Commit

Permalink
AST error resolution does not solve anything. Why dowsn't the AST nod…
Browse files Browse the repository at this point in the history
…e appear??
  • Loading branch information
LunaStev committed Dec 18, 2024
1 parent ecbce24 commit 2bdcc4f
Showing 1 changed file with 89 additions and 111 deletions.
200 changes: 89 additions & 111 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,47 @@ impl<'a> Parser<'a> {
let mut ast = AST::new();

while self.current_token.token_type != TokenType::EOF {
eprintln!("Current Token: {:?}", self.current_token); // 디버깅 메시지 추가
match self.current_token.token_type {
TokenType::FUN => self.function(&mut ast),
TokenType::VAR => self.variable(&mut ast),
TokenType::IF => self.if_statement(&mut ast),
TokenType::WHILE => self.while_statement(&mut ast),
TokenType::FOR => self.for_statement(/* &mut ast */),
TokenType::IMPORT => self.import_statement(),
TokenType::PRINT | TokenType::PRINTLN => self.print_statement(&mut ast),
_ => self.advance(),
TokenType::FUN => {
eprintln!("Parsing function..."); // 디버깅 메시지 추가
self.function(&mut ast)
},
TokenType::VAR => {
eprintln!("Parsing variable..."); // 디버깅 메시지 추가
self.variable(&mut ast)
},
TokenType::IF => {
eprintln!("Parsing if statement..."); // 디버깅 메시지 추가
self.if_statement(&mut ast)
},
TokenType::WHILE => {
eprintln!("Parsing while statement..."); // 디버깅 메시지 추가
self.while_statement(&mut ast)
},
TokenType::FOR => {
eprintln!("Parsing for statement..."); // 디버깅 메시지 추가
self.for_statement()
},
TokenType::IMPORT => {
eprintln!("Parsing import statement..."); // 디버깅 메시지 추가
self.import_statement(&mut ast)
},
TokenType::PRINT | TokenType::PRINTLN => {
eprintln!("Parsing print statement..."); // 디버깅 메시지 추가
self.print_statement(&mut ast)
},
_ => {
eprintln!("Unknown token: {:?}", self.current_token.token_type); // 디버깅 메시지 추가
self.advance()
},
}
}

if ast.nodes.is_empty() {
eprintln!("Warning: The AST is empty. No nodes were parsed.");
}

ast
}

Expand All @@ -36,14 +66,15 @@ impl<'a> Parser<'a> {
}

fn function(&mut self, ast: &mut AST) {
// 함수 구문 처리
eprintln!("Start parsing function...");
self.advance(); // `fun`

let name = if let TokenType::IDENTIFIER(name) = &self.current_token.token_type {
name.clone()
} else {
panic!("Expected function name after 'fun'");
panic!("Expected function name after 'fun', but got {:?}", self.current_token);
};
eprintln!("Function name: {}", name);
self.advance();

if self.current_token.token_type != TokenType::LPAREN {
Expand All @@ -53,6 +84,7 @@ impl<'a> Parser<'a> {

let mut params = Vec::new();
while self.current_token.token_type != TokenType::RPAREN {
eprintln!("Function param: {:?}", self.current_token);
if let TokenType::IDENTIFIER(param_name) = &self.current_token.token_type {
params.push(param_name.clone());
self.advance();
Expand All @@ -64,7 +96,7 @@ 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");
Expand All @@ -74,90 +106,20 @@ impl<'a> Parser<'a> {
let mut body = Vec::new();
// 함수 본문 처리: 중괄호 안에서 명령문들을 처리
while self.current_token.token_type != TokenType::RBRACE {
eprintln!("Parsing statement in function body: {:?}", self.current_token);
match self.current_token.token_type {
TokenType::VAR => {
// 변수 선언 처리 (예시)
self.advance(); // `var`
let var_name = if let TokenType::IDENTIFIER(name) = &self.current_token.token_type {
name.clone()
} else {
panic!("Expected variable name after 'var'");
};
self.advance();

// 변수 타입 처리
if self.current_token.token_type != TokenType::COLON {
panic!("Expected ':' after variable name");
}
self.advance();

let var_type = if let TokenType::TYPE_INT(t) = &self.current_token.token_type {
t.to_string() // IntegerType에 Display 구현 후 to_string 사용
} else {
panic!("Expected type after ':'");
};
self.advance();

// 변수 값 처리
if self.current_token.token_type != TokenType::EQUAL {
panic!("Expected '=' after variable type");
}
self.advance();

let value = if let TokenType::NUMBER(val) = &self.current_token.token_type {
Value::Int(*val) // `i64` 값을 직접 사용
} else {
panic!("Expected number after '='");
};
self.advance();

// 변수 선언 AST 노드 추가
body.push(ASTNode::Variable {
name: var_name,
var_type,
value,
});
}
TokenType::PRINTLN => {
// println 처리 (예시)
self.advance(); // `println`
self.advance(); // '('

let message = if let TokenType::STRING(msg) = &self.current_token.token_type {
msg.clone()
} else {
panic!("Expected string in println");
};
self.advance();

// 변수 또는 값 처리
let expr = if let TokenType::IDENTIFIER(expr) = &self.current_token.token_type {
expr.clone()
} else {
panic!("Expected expression after println string");
};
self.advance();

// `println` 처리
body.push(ASTNode::Print {
message,
newline: true,
});
}
_ => {
// 기타 처리해야 할 문장들 추가 (예: 표현식, 조건문 등)
self.advance(); // 단순히 advance()로 진행할 수도 있습니다
}
TokenType::VAR => self.variable(ast),
TokenType::PRINTLN => self.print_statement(ast),
_ => self.advance(),
}
}
self.advance(); // `RBRACE`

let function_node = ASTNode::Function {
ast.add_node(ASTNode::Function {
name,
params,
body,
};
ast.add_node(function_node);
})
}

fn variable(&mut self, ast: &mut AST) {
Expand All @@ -168,7 +130,8 @@ impl<'a> Parser<'a> {
let var_name = if let TokenType::IDENTIFIER(var_name) = &self.current_token.token_type {
var_name.clone()
} else {
panic!("Expected variable name after 'var'");
eprintln!("Error: Expected variable name after 'var', but got {:?}", self.current_token.token_type);
return;
};
self.advance();

Expand Down Expand Up @@ -220,10 +183,14 @@ impl<'a> Parser<'a> {
TokenType::TYPE_FLOAT(FloatType::F16384) => "f16384".to_string(),
TokenType::TYPE_FLOAT(FloatType::F32768) => "f32768".to_string(),
TokenType::TYPE_STRING => "string".to_string(),
_ => panic!("Expected a valid type after ':'"),
_ => {
eprintln!("Error: Expected variable type after ':', but got {:?}", self.current_token.token_type);
return;
}
}
} else {
panic!("Expected ':' after variable name");
eprintln!("Error: Expected variable type after ':', but got {:?}", self.current_token.token_type);
return;
};
self.advance();

Expand All @@ -236,16 +203,22 @@ impl<'a> Parser<'a> {
let value = match &self.current_token.token_type {
TokenType::NUMBER(value) => Value::Int(*value), // i64 값을 Value::Int로 감싸줌
TokenType::STRING(value) => Value::Text(value.clone()), // String 값을 Value::Text로 감싸줌
_ => panic!("Expected a valid initial value after '='"),
_ => {
eprintln!("Error: Expected a balue for the variable, but got {:?}", self.current_token.token_type);
return;
}
};
self.advance();

// 세미콜론 확인
if self.current_token.token_type != TokenType::SEMICOLON {
panic!("Expected ';' at the end of variable declaration");
eprintln!("Error: Expected ';' at the end of variable declaration, but got {:?}", self.current_token.token_type);
return;
}
self.advance();

eprintln!("Adding variable node: {:?} of type {:?} with calue {:?}", var_name, var_type, value);

// AST에 노드 추가
ast.add_node(ASTNode::Variable {
name: var_name,
Expand All @@ -256,28 +229,29 @@ impl<'a> Parser<'a> {


fn print_statement(&mut self, ast: &mut AST) {
self.advance();
if let TokenType::STRING(_) = &self.current_token.token_type {
// It's a string, proceed with extracting the string.
self.advance(); // println
self.advance(); // '('

let message = if let TokenType::STRING(msg) = &self.current_token.token_type {
msg.clone()
} 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"),
eprintln!("Error: Expected string after 'println', but got {:?}", self.current_token.token_type);
return;
};
self.advance(); // advance to after the message

if self.current_token.token_type == TokenType::PRINTLN {
println!("{}", message);
} else {
print!("{}", message);
if self.current_token.token_type != TokenType::RPAREN {
eprintln!("Error: Expected ')' after the string, but got {:?}", self.current_token.token_type);
return;
}
self.advance();
self.advance(); // skip ')'

eprintln!("Adding print node with message: {:?}", message);

ast.add_node(ASTNode::Print {
message: "".to_string(),
newline: false,
})
message,
newline: true,
});
}

fn if_statement(&mut self, ast: &mut AST) {
Expand Down Expand Up @@ -330,7 +304,7 @@ impl<'a> Parser<'a> {
condition: "".to_string(),
body: vec![],
else_body: None,
})
});
}

fn while_statement(&mut self, ast: &mut AST) {
Expand Down Expand Up @@ -367,7 +341,7 @@ impl<'a> Parser<'a> {
ast.add_node(ASTNode::WhileLoop {
condition: "".to_string(),
body: vec![]
})
});
}

fn for_statement(&mut self /*, ast: &mut AST */) {
Expand Down Expand Up @@ -457,7 +431,7 @@ impl<'a> Parser<'a> {
*/
}

fn import_statement(&mut self) {
fn import_statement(&mut self, ast: &mut AST) {
// import 구문 처리
self.advance(); // `import`

Expand All @@ -484,5 +458,9 @@ impl<'a> Parser<'a> {
panic!("Expected ';' at the end of import statement");
}
self.advance(); // `;` 건너뜀

ast.add_node(ASTNode::Import {
module_name: "".to_string()
});
}
}

0 comments on commit 2bdcc4f

Please sign in to comment.