diff --git a/examples/easy.blight b/examples/easy.blight index d54d5e36b65b6981e239ee87bb074f5aab481232..3fd14633a137273fc6ee0ff72f516c67114dbeac 100644 --- a/examples/easy.blight +++ b/examples/easy.blight @@ -1,3 +1,7 @@ var x: int = 0; var y: float = 0; var z: bigint = 0; + +let a: int = 0; +let b: float = 0; +let c: bigint = 0; diff --git a/src/transpile/ast.rs b/src/transpile/ast.rs index 20923286fc1c33ec99c224493b8ccf56401f3a5c..681845ae057ba288aee21fc14a16f817fb4aa547 100644 --- a/src/transpile/ast.rs +++ b/src/transpile/ast.rs @@ -26,6 +26,11 @@ pub enum Type { #[derive(Debug)] pub enum Ast { + ConstantDeclaration { + name: String, + typ: Type, + value: Box + }, VariableDeclaration { name: String, typ: Type, @@ -49,6 +54,41 @@ fn parse_expression(tokens: &mut dyn PeekableIterator) -> Result) -> Result { match tokens.next().unwrap() { + Token::Let => { + let name = match tokens.next() { + Some(Token::Identifier(name)) => name, + _ => return Err(AstError::new("Expected identifier after var").into()) + }; + let typ = match tokens.next() { + Some(Token::Colon) => { + match tokens.next() { + Some(Token::Identifier(typ)) => { + match typ.as_str() { + "int" => Type::Int, + "float" => Type::Float, + "bigint" => Type::Bigint, + _ => return Err(AstError::new("Expected type after colon").into()) + } + }, + _ => return Err(AstError::new("Expected type after colon").into()) + } + }, + _ => return Err(AstError::new("Expected colon after identifier").into()) + }; + let value = match tokens.next() { + Some(Token::Equals) => parse_expression(tokens)?, + _ => return Err(AstError::new("Expected equals after identifier").into()) + }; + match tokens.next() { + Some(Token::Semicolon) => (), + _ => return Err(AstError::new("Expected semicolon after variable declaration").into()) + }; + Ok(Ast::ConstantDeclaration { + name: name.clone(), + typ, + value: Box::new(value), + }) + }, Token::Var => { let name = match tokens.next() { Some(Token::Identifier(name)) => name, diff --git a/src/transpile/tokenizer.rs b/src/transpile/tokenizer.rs index d5cc33087f4eca2f4b86b704178b751866b12fbf..2ffa334d9b5e9edda908207756da5a17387871e5 100644 --- a/src/transpile/tokenizer.rs +++ b/src/transpile/tokenizer.rs @@ -22,6 +22,7 @@ pub enum Token { Identifier(String), Number(String), // Keywords + Let, Var, Colon, Equals, @@ -58,6 +59,7 @@ pub fn tokenize(content: String) -> Result> { match current_token.as_str() { "var" => tokens.push(Token::Var), + "let" => tokens.push(Token::Let), _ => tokens.push(Token::Identifier(current_token)), } }, diff --git a/src/transpile/walker.rs b/src/transpile/walker.rs index 368619b2a5f8007016126864428f6aee5e8b4b0d..936288b1a0d2fd7942c21bc4e7ca9be3266602ed 100644 --- a/src/transpile/walker.rs +++ b/src/transpile/walker.rs @@ -9,6 +9,12 @@ fn transpile_helper(tree: Ast) -> String { _ => format!("let {} = {};\n", name, transpile_helper(*value)) } }, + Ast::ConstantDeclaration { name, typ, value } => { + match typ { + Type::Bigint => format!("const {} = BigInt({});\n", name, transpile_helper(*value)), + _ => format!("const {} = {};\n", name, transpile_helper(*value)) + } + } } }