File tree Expand file tree Collapse file tree 5 files changed +92
-2
lines changed Expand file tree Collapse file tree 5 files changed +92
-2
lines changed Original file line number Diff line number Diff line change @@ -231,6 +231,10 @@ $ cargo test -p three_body_interpreter
231
231
232
232
Create issues: [issues](https://github.com/rustq/3body-lang/issues)
233
233
234
+ # # Visual Studio Code Extension
235
+
236
+ [3body-vscode-language-server](https://marketplace.visualstudio.com/items? itemName=meloalright.3body-vscode-language-server)
237
+
234
238
# # License
235
239
236
240
[MIT](https://opensource.org/licenses/MIT)
Original file line number Diff line number Diff line change @@ -1225,4 +1225,21 @@ f()
1225
1225
1226
1226
assert_eq ! ( Some ( object:: Object :: Int ( 5 ) ) , eval( input) ) ;
1227
1227
}
1228
+
1229
+ #[ test]
1230
+ fn test_comment ( ) {
1231
+ let tests = vec ! [
1232
+ (
1233
+ "let identity = fn(x) { // function defination here
1234
+ x; // just x
1235
+ };
1236
+ identity(5); // run with param 5" ,
1237
+ Some ( object:: Object :: Int ( 5 ) ) ,
1238
+ ) ,
1239
+ ] ;
1240
+
1241
+ for ( input, expect) in tests {
1242
+ assert_eq ! ( expect, eval( input) ) ;
1243
+ }
1244
+ }
1228
1245
}
Original file line number Diff line number Diff line change @@ -63,7 +63,15 @@ impl Lexer {
63
63
let tok = match self . ch {
64
64
'+' => Token :: Plus ,
65
65
'-' => Token :: Minus ,
66
- '/' => Token :: Slash ,
66
+ '/' => {
67
+ if self . next_is ( '/' ) {
68
+ self . walk_char ( ) ;
69
+ self . skip_comment ( ) ;
70
+ return self . next_token ( ) ;
71
+ } else {
72
+ Token :: Slash
73
+ }
74
+ } ,
67
75
'*' => Token :: Asterisk ,
68
76
'<' => {
69
77
if self . next_is ( '=' ) {
@@ -181,6 +189,13 @@ impl Lexer {
181
189
}
182
190
}
183
191
192
+ fn skip_comment ( & mut self ) {
193
+ while !matches ! ( self . next_ch( ) , '\n' | '\0' ) {
194
+ self . walk_char ( ) ;
195
+ }
196
+ self . walk_char ( ) ;
197
+ }
198
+
184
199
fn consume_identifier ( & mut self ) -> Token {
185
200
let start_pos = self . pos ;
186
201
@@ -331,6 +346,9 @@ if (5 < 10) {
331
346
"foobar";
332
347
"foo bar";
333
348
'foo bar';
349
+ "foobar";//
350
+ "foo bar"; // just a comment
351
+ 'foo bar'; // 一段注释
334
352
335
353
[1, 2];
336
354
@@ -433,6 +451,12 @@ if (5 < 10) {
433
451
Token :: Semicolon ,
434
452
Token :: String ( String :: from( "foo bar" ) ) ,
435
453
Token :: Semicolon ,
454
+ Token :: String ( String :: from( "foobar" ) ) ,
455
+ Token :: Semicolon ,
456
+ Token :: String ( String :: from( "foo bar" ) ) ,
457
+ Token :: Semicolon ,
458
+ Token :: String ( String :: from( "foo bar" ) ) ,
459
+ Token :: Semicolon ,
436
460
Token :: Blank ,
437
461
Token :: LBracket ,
438
462
Token :: Int ( 1 ) ,
Original file line number Diff line number Diff line change @@ -1891,6 +1891,29 @@ return 993322;
1891
1891
) ;
1892
1892
}
1893
1893
1894
+ #[ test]
1895
+ fn test_comment ( ) {
1896
+ let input = "fn(x, y){
1897
+ x + y // just return x + y 即可
1898
+ }" ;
1899
+
1900
+ let mut parser = Parser :: new ( Lexer :: new ( input) ) ;
1901
+ let program = parser. parse ( ) ;
1902
+
1903
+ check_parse_errors ( & mut parser) ;
1904
+ assert_eq ! (
1905
+ vec![ Stmt :: Expr ( Expr :: Function {
1906
+ params: vec![ Ident ( String :: from( "x" ) ) , Ident ( String :: from( "y" ) ) ] ,
1907
+ body: vec![ Stmt :: Expr ( Expr :: Infix (
1908
+ Infix :: Plus ,
1909
+ Box :: new( Expr :: Ident ( Ident ( String :: from( "x" ) ) ) ) ,
1910
+ Box :: new( Expr :: Ident ( Ident ( String :: from( "y" ) ) ) ) ,
1911
+ ) ) ] ,
1912
+ } ) ] ,
1913
+ program,
1914
+ ) ;
1915
+ }
1916
+
1894
1917
/// errors panic
1895
1918
1896
1919
#[ test]
Original file line number Diff line number Diff line change @@ -11,6 +11,7 @@ use three_body_interpreter::lexer::Lexer;
11
11
use three_body_interpreter:: parser:: Parser ;
12
12
use std:: cell:: RefCell ;
13
13
use std:: rc:: Rc ;
14
+ use std:: fs;
14
15
15
16
16
17
fn main ( ) {
@@ -50,7 +51,7 @@ fn main() {
50
51
}
51
52
}
52
53
}
53
- _ => {
54
+ "-h" => {
54
55
println ! ( "usage: 3body [option] ... [arg] ...
55
56
56
57
Options and arguments:
@@ -60,6 +61,27 @@ Options and arguments:
60
61
-c cmd : program passed in as string (terminates option list)
61
62
- : program in repl (default)
62
63
" )
64
+ } ,
65
+ path => {
66
+ let contents = fs:: read_to_string ( path) . expect ( "Should have been able to read the file" ) ;
67
+ let mut lexer = Lexer :: new ( & contents) ;
68
+ let mut parser = Parser :: new ( lexer) ;
69
+ let program = parser. parse ( ) ;
70
+ let errors = parser. get_errors ( ) ;
71
+
72
+ if errors. len ( ) > 0 {
73
+ for err in errors {
74
+ println ! ( "{:?}" , err) ;
75
+ }
76
+ return ;
77
+ }
78
+
79
+ if let Some ( evaluated) = evaluator. eval ( & program) {
80
+ match evaluated {
81
+ object:: Object :: Null => { } ,
82
+ _ => println ! ( "{}\n " , evaluated) ,
83
+ }
84
+ }
63
85
}
64
86
}
65
87
return ;
You can’t perform that action at this time.
0 commit comments