Implement line comments

This commit is contained in:
Daniel M 2022-01-29 23:29:09 +01:00
parent 2ea2aa5203
commit 49ada446f8
3 changed files with 10 additions and 9 deletions

View File

@ -42,7 +42,7 @@
- [ ] If else statement `if X { ... } else { ... }` - [ ] If else statement `if X { ... } else { ... }`
- [ ] If Statement - [ ] If Statement
- [ ] Else statement - [ ] Else statement
- [ ] Line comments `//` - [x] Line comments `//`
- [ ] Strings - [ ] Strings
- [ ] IO Intrinsics - [ ] IO Intrinsics
- [ ] Print - [ ] Print

View File

@ -9,7 +9,7 @@ pub enum Token {
/// Identifier (name for variables, functions, ...) /// Identifier (name for variables, functions, ...)
Ident(String), Ident(String),
/// Left Parenthesis ('(') /// Left Parenthesis ('(')
LParen, LParen,
@ -62,8 +62,8 @@ pub enum Token {
RAngle, RAngle,
/// Left angle bracket Equal (<=) /// Left angle bracket Equal (<=)
LAngleEqu, LAngleEqu,
/// Left angle bracket Equal (>=) /// Left angle bracket Equal (>=)
RAngleEqu, RAngleEqu,
@ -129,7 +129,9 @@ impl<'a> Lexer<'a> {
self.next(); self.next();
tokens.push(Token::LArrow); 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::Semicolon),
'+' => tokens.push(Token::Add), '+' => tokens.push(Token::Add),
'-' => tokens.push(Token::Sub), '-' => 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 // TODO: We only added numeric chars to the string, but the conversion could still fail
tokens.push(Token::I64(sval.parse().unwrap())); tokens.push(Token::I64(sval.parse().unwrap()));
} }
// Lex characters as identifier // Lex characters as identifier
ch @ ('a'..='z' | 'A'..='Z' | '_') => { ch @ ('a'..='z' | 'A'..='Z' | '_') => {
let mut ident = String::from(ch); let mut ident = String::from(ch);
@ -237,7 +239,7 @@ impl Token {
Token::LAngle => BinOpType::Less, Token::LAngle => BinOpType::Less,
Token::LAngleEqu => BinOpType::LessEqu, Token::LAngleEqu => BinOpType::LessEqu,
Token::RAngle => BinOpType::Greater, Token::RAngle => BinOpType::Greater,
Token::RAngleEqu => BinOpType::GreaterEqu, Token::RAngleEqu => BinOpType::GreaterEqu,

View File

@ -1,5 +1,3 @@
use std::io::Write;
use nek_lang::{lexer::lex, parser::parse, interpreter::Interpreter}; use nek_lang::{lexer::lex, parser::parse, interpreter::Interpreter};
@ -10,6 +8,7 @@ fn main() {
// let mut code = String::new(); // let mut code = String::new();
let code = " let code = "
a <- 5; a <- 5;
// nek-lang best lang
a * 2; a * 2;
"; ";