From 49ada446f8e25ea75a8a66ea95d8c715af827c41 Mon Sep 17 00:00:00 2001 From: Daniel M Date: Sat, 29 Jan 2022 23:29:09 +0100 Subject: [PATCH] Implement line comments --- README.md | 2 +- src/lexer.rs | 14 ++++++++------ src/main.rs | 3 +-- 3 files changed, 10 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index d8014d7..949d385 100644 --- a/README.md +++ b/README.md @@ -42,7 +42,7 @@ - [ ] If else statement `if X { ... } else { ... }` - [ ] If Statement - [ ] Else statement -- [ ] Line comments `//` +- [x] Line comments `//` - [ ] Strings - [ ] IO Intrinsics - [ ] Print diff --git a/src/lexer.rs b/src/lexer.rs index 5a2b577..0aedac8 100644 --- a/src/lexer.rs +++ b/src/lexer.rs @@ -9,7 +9,7 @@ pub enum Token { /// Identifier (name for variables, functions, ...) Ident(String), - + /// Left Parenthesis ('(') LParen, @@ -62,8 +62,8 @@ pub enum Token { RAngle, /// Left angle bracket Equal (<=) - LAngleEqu, - + LAngleEqu, + /// Left angle bracket Equal (>=) RAngleEqu, @@ -129,7 +129,9 @@ impl<'a> Lexer<'a> { self.next(); tokens.push(Token::LArrow); } - + // Line comment. Consume every char until linefeed (next line) + '/' if matches!(self.peek(), '/') => while self.next() != '\n' {}, + ';' => tokens.push(Token::Semicolon), '+' => tokens.push(Token::Add), '-' => tokens.push(Token::Sub), @@ -169,7 +171,7 @@ impl<'a> Lexer<'a> { // TODO: We only added numeric chars to the string, but the conversion could still fail tokens.push(Token::I64(sval.parse().unwrap())); } - + // Lex characters as identifier ch @ ('a'..='z' | 'A'..='Z' | '_') => { let mut ident = String::from(ch); @@ -237,7 +239,7 @@ impl Token { Token::LAngle => BinOpType::Less, Token::LAngleEqu => BinOpType::LessEqu, - + Token::RAngle => BinOpType::Greater, Token::RAngleEqu => BinOpType::GreaterEqu, diff --git a/src/main.rs b/src/main.rs index 140c190..3e5cb39 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,3 @@ -use std::io::Write; - use nek_lang::{lexer::lex, parser::parse, interpreter::Interpreter}; @@ -10,6 +8,7 @@ fn main() { // let mut code = String::new(); let code = " a <- 5; + // nek-lang best lang a * 2; ";