diff --git a/src/main.rs b/src/main.rs index 043b1db0..4bfc8ccb 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,12 +1,11 @@ mod lexer; mod parser; mod error; -mod node; use std::fs; use lexer::{Lexer, Token}; -use parser::Parser; -use parser::ast::AST; +use crate::lexer::TokenType; +use crate::parser::ast::create_function_ast; // use crate::node::function_node; fn format_tokens(tokens: &Vec) -> String { @@ -22,6 +21,7 @@ fn format_tokens(tokens: &Vec) -> String { result } +/* fn format_parser(parser: &Parser) -> String { format!( "{{\n lexer: {{\n source: {:?},\n current: {},\n line: {}\n }},\n current_token: {{\n token_type: {:?},\n lexeme: {:?},\n line: {}\n }}\n}}", @@ -40,39 +40,22 @@ fn format_ast(ast: &AST) -> String { ast.nodes ) } + */ fn main() { let code = fs::read_to_string("test/test4.wave").expect("Failed to read the file"); - // Create a Lexer let mut lexer = Lexer::new(code.as_str()); - // Tokenize the source code let tokens = lexer.tokenize(); + eprintln!("Tokens: {}", format_tokens(&tokens)); - // Create a Parser - let mut parser = Parser::new(lexer); - - // Parse the AST - let ast = parser.parse(); - /* - let mut asta = AST::new(); - - let node = ASTNode::Variable { - name: String::from("x"), - var_type: String::from(":i32"), - value: Value::Int(42), - is_immutable: false, - }; + let function_name = tokens.iter() + .find(|token| matches!(token.token_type, TokenType::IDENTIFIER(_))) + .map(|token| token.lexeme.clone()) + .unwrap_or_default(); - asta.add_node(node); - */ + let ast = create_function_ast(function_name); - // a formalized output - eprintln!("Tokens: {}", format_tokens(&tokens)); - eprintln!("\nParser: {}", format_parser(&parser)); - eprintln!("\nAST: {}", format_ast(&ast)); - // println!("\nTEST AST: {}", format_ast(&asta)); - // println!("{:?}", function_node()); - // println!("{:#?}", function_node()); + eprintln!("AST: {:?}", &ast) } \ No newline at end of file diff --git a/src/node.rs b/src/node.rs deleted file mode 100644 index 6ab487aa..00000000 --- a/src/node.rs +++ /dev/null @@ -1,23 +0,0 @@ -use std::fs; -use crate::parser::ast::AST; -use crate::lexer::Lexer; -use crate::parser::Parser; - -pub(crate) fn function_node() { - let code = fs::read_to_string("test/test2.wave").expect("Failed to read the file"); - let mut parser = Parser { - lexer: Lexer { - source: &code, - current: 0, - line: 0, - }, - current_token: Default::default(), - }; - - let mut ast = AST { nodes: vec![] }; - - parser.function(&mut ast); - - // AST에 추가된 함수 노드를 출력 - println!("{:#?}", ast); -} \ No newline at end of file diff --git a/src/parser/ast.rs b/src/parser/ast.rs index 4ead1399..b8bfe82b 100644 --- a/src/parser/ast.rs +++ b/src/parser/ast.rs @@ -6,49 +6,31 @@ pub enum Value { #[derive(Debug, Clone)] pub enum ASTNode { - Function { - name: String, - params: Vec, - body: Vec, - }, - Variable { - name: String, - var_type: String, - value: Value, - is_immutable: bool, - }, - IfStatement { - condition: String, - body: Vec, - else_body: Option>, - }, - WhileLoop { - condition: String, - body: Vec, - }, - ForLoop { - init: Box, - condition: String, - increment: Box, - body: Vec, - }, - Import { - module_name: String, - }, - Print { - message: String, - newline: bool, - }, - Literal { - value: String, - }, - Expression { - operator: String, - left: Box, - right: Box, - }, + Function(FunctionNode), } +#[derive(Debug, Clone)] +pub struct FunctionNode { + pub name: String, + pub parameters: Vec, + pub body: Vec, +} + +#[derive(Debug, Clone)] +pub struct ParameterNode { + pub name: String, + pub param_type: String, // For simplicity, assuming type as string. +} + +pub fn create_function_ast(function_name: String) -> ASTNode { + ASTNode::Function(FunctionNode { + name: function_name, + parameters: vec![], // No parameters + body: vec![], // Empty body + }) +} + +/* #[derive(Debug, Clone)] pub struct AST { pub nodes: Vec, @@ -66,3 +48,5 @@ impl AST { self.nodes.push(node); } } + + */ \ No newline at end of file diff --git a/src/parser/parser.rs b/src/parser/parser.rs index 9bf027e7..36f3b832 100644 --- a/src/parser/parser.rs +++ b/src/parser/parser.rs @@ -1,3 +1,4 @@ +/* use crate::lexer::{FloatType, IntegerType, Lexer, Token, TokenType}; use crate::parser::ast::{AST, ASTNode, Value}; @@ -545,3 +546,5 @@ impl<'a> Parser<'a> { }); } } + + */ \ No newline at end of file diff --git a/test/test4.wave b/test/test4.wave index 9e5f0bbb..c189eccf 100644 --- a/test/test4.wave +++ b/test/test4.wave @@ -1,3 +1 @@ -fun main() { - println("Hello World"); -} \ No newline at end of file +fun main() { } \ No newline at end of file