Skip to content

Commit

Permalink
+ comment support and + excute and + lsp (#41)
Browse files Browse the repository at this point in the history
  • Loading branch information
meloalright authored Nov 18, 2023
1 parent e531aef commit 73e678b
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 2 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,10 @@ $ cargo test -p three_body_interpreter
Create issues: [issues](https://github.com/rustq/3body-lang/issues)
## Visual Studio Code Extension
[3body-vscode-language-server](https://marketplace.visualstudio.com/items?itemName=meloalright.3body-vscode-language-server)
## License
[MIT](https://opensource.org/licenses/MIT)
17 changes: 17 additions & 0 deletions interpreter/src/evaluator/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1225,4 +1225,21 @@ f()

assert_eq!(Some(object::Object::Int(5)), eval(input));
}

#[test]
fn test_comment() {
let tests = vec![
(
"let identity = fn(x) { // function defination here
x; // just x
};
identity(5); // run with param 5",
Some(object::Object::Int(5)),
),
];

for (input, expect) in tests {
assert_eq!(expect, eval(input));
}
}
}
26 changes: 25 additions & 1 deletion interpreter/src/lexer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,15 @@ impl Lexer {
let tok = match self.ch {
'+' => Token::Plus,
'-' => Token::Minus,
'/' => Token::Slash,
'/' => {
if self.next_is('/') {
self.walk_char();
self.skip_comment();
return self.next_token();
} else {
Token::Slash
}
},
'*' => Token::Asterisk,
'<' => {
if self.next_is('=') {
Expand Down Expand Up @@ -181,6 +189,13 @@ impl Lexer {
}
}

fn skip_comment(&mut self) {
while !matches!(self.next_ch(), '\n' | '\0') {
self.walk_char();
}
self.walk_char();
}

fn consume_identifier(&mut self) -> Token {
let start_pos = self.pos;

Expand Down Expand Up @@ -331,6 +346,9 @@ if (5 < 10) {
"foobar";
"foo bar";
'foo bar';
"foobar";//
"foo bar"; // just a comment
'foo bar'; // 一段注释
[1, 2];
Expand Down Expand Up @@ -433,6 +451,12 @@ if (5 < 10) {
Token::Semicolon,
Token::String(String::from("foo bar")),
Token::Semicolon,
Token::String(String::from("foobar")),
Token::Semicolon,
Token::String(String::from("foo bar")),
Token::Semicolon,
Token::String(String::from("foo bar")),
Token::Semicolon,
Token::Blank,
Token::LBracket,
Token::Int(1),
Expand Down
23 changes: 23 additions & 0 deletions interpreter/src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1891,6 +1891,29 @@ return 993322;
);
}

#[test]
fn test_comment() {
let input = "fn(x, y){
x + y // just return x + y 即可
}";

let mut parser = Parser::new(Lexer::new(input));
let program = parser.parse();

check_parse_errors(&mut parser);
assert_eq!(
vec![Stmt::Expr(Expr::Function {
params: vec![Ident(String::from("x")), Ident(String::from("y"))],
body: vec![Stmt::Expr(Expr::Infix(
Infix::Plus,
Box::new(Expr::Ident(Ident(String::from("x")))),
Box::new(Expr::Ident(Ident(String::from("y")))),
))],
})],
program,
);
}

/// errors panic
#[test]
Expand Down
24 changes: 23 additions & 1 deletion src/bin/repl/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use three_body_interpreter::lexer::Lexer;
use three_body_interpreter::parser::Parser;
use std::cell::RefCell;
use std::rc::Rc;
use std::fs;


fn main() {
Expand Down Expand Up @@ -50,7 +51,7 @@ fn main() {
}
}
}
_ => {
"-h" => {
println!("usage: 3body [option] ... [arg] ...
Options and arguments:
Expand All @@ -60,6 +61,27 @@ Options and arguments:
-c cmd : program passed in as string (terminates option list)
- : program in repl (default)
")
},
path => {
let contents = fs::read_to_string(path).expect("Should have been able to read the file");
let mut lexer = Lexer::new(&contents);
let mut parser = Parser::new(lexer);
let program = parser.parse();
let errors = parser.get_errors();

if errors.len() > 0 {
for err in errors {
println!("{:?}", err);
}
return;
}

if let Some(evaluated) = evaluator.eval(&program) {
match evaluated {
object::Object::Null => {},
_ => println!("{}\n", evaluated),
}
}
}
}
return;
Expand Down

0 comments on commit 73e678b

Please sign in to comment.