forked from nulang-org/nulang
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrepl.rs
More file actions
1167 lines (1087 loc) · 43.3 KB
/
Copy pathrepl.rs
File metadata and controls
1167 lines (1087 loc) · 43.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//! Read-Eval-Print Loop for Nulang.
//!
//! Full-featured REPL with:
//! - Persistent type context across evaluations
//! - Multi-line input support
//! - `:commands` for introspection
//! - Graceful error handling
use crate::ast::{AstModule, Decl, Expr};
use crate::effect_checker::{CapContext, CapabilityAnalyzer, EffectChecker};
use crate::lexer::Lexer;
use crate::parser::Parser;
use crate::typechecker::TypeChecker;
use crate::types::{Capability, NuError, NuResult, Span, Type, TypeContext};
use crate::vm::{Value, VM};
use rustyline::error::ReadlineError;
use rustyline::{history::DefaultHistory, Editor, Result as RlResult};
use std::cell::RefCell;
use std::collections::HashSet;
use std::io::IsTerminal;
use std::rc::Rc;
/// REPL state that persists across evaluations.
pub struct Repl {
vm: VM,
/// Accumulated declarations from previous inputs (functions, actors, etc.)
accumulated_decls: Vec<Decl>,
/// Persistent type context across evaluations
type_ctx: TypeContext,
/// Fresh type checker (can be reused)
type_checker: TypeChecker,
/// Last compiled bytecode module (for :bytecode command)
last_bytecode: Option<String>,
/// Last AST (for :ast command display)
last_ast: Option<AstModule>,
/// Set of user-defined identifiers for tab completion (shared with ReplHelper).
user_names: Rc<RefCell<HashSet<String>>>,
}
struct ReplHelper {
keywords: Vec<String>,
user_names: Rc<RefCell<HashSet<String>>>,
}
impl ReplHelper {
fn new(user_names: Rc<RefCell<HashSet<String>>>) -> Self {
ReplHelper {
keywords: vec![
// Keywords
"fn",
"let",
"rec",
"in",
"if",
"then",
"else",
"match",
"with",
"actor",
"behavior",
"state",
"spawn",
"send",
"ask",
"receive",
"perform",
"handle",
"resume",
"effect",
"workflow",
"step",
"parallel",
"compensate",
"statemachine",
"event",
"on_entry",
"on_exit",
"persistent",
"local",
"durable",
"eventsourced",
"crdt",
"module",
"import",
"pub",
"type",
"alias",
"extern",
"iso",
"trn",
"ref",
"val",
"box",
"tag",
"lineariso",
"linear",
"true",
"false",
"nil",
"unit",
"for",
"loop",
"break",
"return",
"node",
"link",
"monitor",
"exit",
"agent",
"database",
// Types
"Int",
"Float",
"String",
"Bool",
"Unit",
"Nil",
// Built-in effects
"IO.print",
"IO.read",
"IO.flush",
"Timer.sleep",
"Timer.now",
"Signal.wait",
"Signal.notify",
"Inference.ask",
"LLM.ask",
"LLM.complete",
"Net.connect",
"Net.listen",
"Actor.spawn",
"Actor.send",
"Actor.link",
"Actor.monitor",
"Actor.trap_exit",
"Actor.exit",
"Actor.register",
"Actor.unregister",
"Actor.whereis",
"Actor.set_priority",
"Actor.stats",
"Workflow.query",
"Workflow.respond",
// REPL commands
":help",
":quit",
":type",
":ast",
":bytecode",
":clear",
":reset",
":version",
":stats",
":load",
]
.into_iter()
.map(|s| s.to_string())
.collect(),
user_names,
}
}
}
impl rustyline::Helper for ReplHelper {}
impl rustyline::highlight::Highlighter for ReplHelper {
fn highlight<'l>(&self, line: &'l str, _pos: usize) -> std::borrow::Cow<'l, str> {
// Respect NO_COLOR
if std::env::var("NO_COLOR").is_ok_and(|v| !v.is_empty()) {
return std::borrow::Cow::Borrowed(line);
}
let mut lexer = crate::lexer::Lexer::new(line);
let tokens = match lexer.lex() {
Ok(ts) => ts,
Err(_) => return std::borrow::Cow::Borrowed(line),
};
let mut result = String::with_capacity(line.len() + 64);
let mut last_end = 0usize;
for token in &tokens {
let start = token.span.start as usize;
let end = token.span.end as usize;
// Copy unhighlighted text before this token
if start > last_end {
result.push_str(&line[last_end..start]);
}
let color = color_for_token(&token.kind);
let text = &line[start..end];
if color.is_empty() {
result.push_str(text);
} else {
result.push_str(color);
result.push_str(text);
result.push_str("\x1b[0m");
}
last_end = end;
}
// Copy remaining unhighlighted text
if last_end < line.len() {
result.push_str(&line[last_end..]);
}
std::borrow::Cow::Owned(result)
}
fn highlight_char(&self, line: &str, pos: usize, _kind: rustyline::highlight::CmdKind) -> bool {
// Bracket matching: indicate if char at pos has a matching partner
if pos >= line.len() {
return false;
}
let ch = line.as_bytes()[pos] as char;
if !matches!(ch, '(' | ')' | '{' | '}' | '[' | ']') {
return false;
}
// Check if brackets are balanced (the char at pos has a match)
let mut stack: Vec<char> = Vec::new();
for (_i, c) in line.char_indices() {
match c {
'(' | '{' | '[' => stack.push(c),
')' | '}' | ']' => {
if let Some(&open) = stack.last() {
let expected = match open {
'(' => ')',
'{' => '}',
'[' => ']',
_ => unreachable!(),
};
if c == expected {
stack.pop();
} else {
return false;
}
} else {
return false;
}
}
_ => {}
}
}
stack.is_empty()
}
}
/// Map a lexer token kind to an ANSI color string (empty for no color).
fn color_for_token(kind: &crate::lexer::TokenKind) -> &'static str {
use crate::lexer::TokenKind::*;
match kind {
// Keywords — bright yellow
Fn | Let | Rec | In | If | Then | Else | Match | With | Case | Actor | Entity
| Behavior | State | StateMachine | SelfKw | Spawn | Send | Remote | Ask | Persistent
| Local | Durable | EventSourced | Crdt | Until | Emit | Workflow | Step | Parallel
| Compensate | Agent | Database | Receive | Effect | Perform | Handle | Resume | Extern
| Module | Import | Pub | Migrate | Monitor | Link | Exit | For | While | Break
| Return | Type | Alias | Iso | Trn | Ref | Val | Box | Tag | True | False | Unit
| Tool | Handler | Consume | Initial | Throws | As => "\x1b[1;33m",
// String literals — green
StringLit(_) => "\x1b[32m",
// Numeric literals — magenta
IntLit(_) | FloatLit(_) => "\x1b[35m",
// Comments — dim
Comment(_) | DocComment(_) => "\x1b[2m",
// Delimiters and everything else — no color
_ => "",
}
}
impl rustyline::hint::Hinter for ReplHelper {
type Hint = String;
}
impl rustyline::validate::Validator for ReplHelper {}
impl rustyline::completion::Completer for ReplHelper {
type Candidate = String;
fn complete(
&self,
line: &str,
pos: usize,
_ctx: &rustyline::Context<'_>,
) -> RlResult<(usize, Vec<String>)> {
// Find the word boundary before the cursor.
let start = line[..pos]
.rfind(|c: char| c.is_whitespace() || c == '(' || c == '{' || c == '[')
.map(|i| i + 1)
.unwrap_or(0);
let prefix = &line[start..pos];
if prefix.is_empty() {
return Ok((pos, vec![]));
}
let prefix_lower = prefix.to_lowercase();
// Merge static keywords with dynamic user-defined names, deduplicate.
let mut seen: HashSet<&str> = HashSet::new();
let mut matches: Vec<String> = Vec::new();
for kw in self.keywords.iter().chain(self.user_names.borrow().iter()) {
if kw.to_lowercase().starts_with(&prefix_lower) && seen.insert(kw.as_str()) {
matches.push(kw.clone());
}
}
Ok((start, matches))
}
}
impl Repl {
pub fn new() -> Self {
Repl {
vm: VM::new(),
accumulated_decls: Vec::new(),
type_ctx: TypeContext::new(),
type_checker: TypeChecker::new(),
last_bytecode: None,
last_ast: None,
user_names: Rc::new(RefCell::new(HashSet::new())),
}
}
/// Run the interactive REPL loop.
pub fn run(&mut self) {
println!(
"Nulang v{} \u{2014} Actor-Based Distributed Language",
env!("CARGO_PKG_VERSION")
);
println!("Type :help for commands, :quit to exit\n");
let mut editor = match Editor::<ReplHelper, DefaultHistory>::new() {
Ok(ed) => ed,
Err(e) => {
eprintln!(
"Warning: Could not initialize line editor ({}). Falling back to basic input.",
e
);
run_basic_repl();
return;
}
};
editor.set_helper(Some(ReplHelper::new(Rc::clone(&self.user_names))));
let history_path = std::env::var("HOME")
.map(|h| format!("{}/.nulang_history", h))
.unwrap_or_else(|_| ".nulang_history".to_string());
let _ = editor.load_history(&history_path);
let mut buffer = String::new();
let mut brace_stack: Vec<char> = Vec::new();
loop {
let prompt = if brace_stack.is_empty() {
"nulang> ".to_string()
} else {
".... ".to_string()
};
let line = match editor.readline(&prompt) {
Ok(line) => line,
Err(ReadlineError::Interrupted) => {
println!("^C");
buffer.clear();
brace_stack.clear();
continue;
}
Err(ReadlineError::Eof) => {
println!("Goodbye!");
break;
}
Err(e) => {
eprintln!("Error reading input: {}", e);
continue;
}
};
let trimmed = line.trim();
// REPL commands (only when not in multi-line mode)
if brace_stack.is_empty() && trimmed.starts_with(':') {
editor.add_history_entry(&line).ok();
let mut parts = trimmed[1..].splitn(2, ' ');
let cmd = parts.next().unwrap_or("");
let rest = parts.next().unwrap_or("").trim();
match cmd {
"quit" | "q" => {
println!("Goodbye!");
break;
}
"help" | "h" => {
if rest.is_empty() {
self.print_help();
} else {
self.print_help_topic(rest);
}
}
"type" => {
if rest.is_empty() {
eprintln!("Usage: :type <expression>");
} else if let Err(e) = self.show_type(rest) {
self.print_error(&e);
}
}
"load" => {
if rest.is_empty() {
eprintln!("Usage: :load <file>");
} else if let Err(e) = self.load_file(rest) {
self.print_error(&e);
}
}
"ast" => {
if rest.is_empty() {
eprintln!("Usage: :ast <expression>");
} else if let Err(e) = self.show_ast(rest) {
self.print_error(&e);
}
}
"bytecode" | "bc" => self.show_bytecode(),
"clear" => {
print!("\x1B[2J\x1B[1;1H");
let _ = std::io::Write::flush(&mut std::io::stdout());
}
"reset" => {
self.accumulated_decls.clear();
self.type_ctx = TypeContext::new();
self.type_checker = TypeChecker::new();
self.last_bytecode = None;
self.last_ast = None;
self.user_names.borrow_mut().clear();
println!("Environment reset.");
}
"version" | "ver" => {
println!("nulang v{}", env!("CARGO_PKG_VERSION"));
}
"stats" => {
println!("Runtime stats not available in REPL mode.");
println!("(use a .nula file with actors and --verbose for runtime stats)");
}
unknown => {
println!("Unknown command: :{}. Type :help for help.", unknown);
}
}
}
buffer.push_str(&line);
buffer.push('\n');
// Use the lexer to track brace/paren/bracket stack.
brace_stack = Self::brace_stack(&buffer);
if !brace_stack.is_empty() {
continue; // Wait for more input
}
// Execute buffered input.
let input = buffer.trim();
if !input.is_empty() {
editor.add_history_entry(input).ok();
if let Err(e) = self.evaluate(input) {
self.print_error(&e);
}
}
buffer.clear();
}
let _ = editor.save_history(&history_path);
println!();
}
/// Evaluate a source string, showing value and type.
fn evaluate(&mut self, source: &str) -> NuResult<()> {
crate::types::set_source_map_with_file(source, Some("<repl>"));
// Parse
let ast = parse_source(source)?;
self.last_ast = Some(ast.clone());
// Separate declarations from the __main expression
let mut new_decls = Vec::new();
let mut main_expr: Option<Expr> = None;
for decl in &ast.decls {
if let Decl::Function { name, .. } = decl {
if name == "__main" {
// Extract the body expression of __main
if let Decl::Function { body, .. } = decl {
main_expr = Some(body.clone());
}
continue;
}
}
new_decls.push(decl.clone());
}
// Build combined module: accumulated + new declarations + __main if present
let mut combined_decls = self.accumulated_decls.clone();
combined_decls.extend(new_decls.clone());
if let Some(ref expr) = main_expr {
combined_decls.push(Decl::Function {
name: "__main".to_string(),
type_params: vec![],
type_param_constraints: vec![],
params: vec![],
default_values: vec![],
using_params: vec![],
ret_type: None,
error_type: None,
effect: None,
cap: None,
body: expr.clone(),
annotations: vec![],
public: false,
span: Span::default(),
});
}
let combined_module = AstModule {
name: "repl".to_string(),
decls: combined_decls,
};
// Type check the combined module
let module_type = self.type_checker.check_module(&combined_module)?;
// Effect check: same two-pass driver as the CLI frontend
// (`run_frontend` in main.rs) over the combined module — accumulated
// + new declarations + __main. Registering function rows first lets
// callee effects propagate to call sites (so new code calling an
// accumulated IO function is charged its row), and pass 2 enforces
// declared rows on every body. Errors print through the caller's
// `print_error` exactly as before.
let mut effect_checker = EffectChecker::new();
effect_checker.check_module(&combined_module.decls)?;
// Capability analysis
let mut cap_analyzer = CapabilityAnalyzer::new();
let cap_ctx = CapContext::new();
if let Some(ref expr) = main_expr {
let _cap = cap_analyzer.infer_cap(&cap_ctx, expr)?;
}
// Compile the combined module via the HIR/MIR pipeline.
let code_module = compile_with_new_pipeline(&combined_module, "repl")?;
self.last_bytecode = Some(disassemble_module(&code_module));
// from scratch (see `combined_module` above), so no closure created
// by a previous evaluation can still be reachable — safe to reclaim
// their capture environments before this run instead of leaking them
// for the life of the REPL session.
self.vm.clear_closure_envs();
// Load and execute
self.vm.load_module(code_module);
let value = self.vm.run()?;
// Print results
if let Some(ref _expr) = main_expr {
let val_str = value_to_pretty_string(&value);
let expr_ty = extract_return_type(&module_type);
let ty_str = type_to_string(expr_ty);
println!("{} : {}", val_str, ty_str);
} else if !new_decls.is_empty() {
// Print declaration info. Each new declaration is re-checked in
// the full session context (accumulated + earlier new decls, in
// source order) rather than in isolation, so a function calling
// a previously-defined function resolves and its type prints.
let mut context_decls = self.accumulated_decls.clone();
for decl in &new_decls {
context_decls.push(decl.clone());
match decl {
Decl::Function { name, .. } => {
let decl_ty = self.type_checker.check_module(&AstModule {
name: "repl".to_string(),
decls: context_decls.clone(),
})?;
println!("{} : {}", name, type_to_string(&decl_ty));
}
Decl::LetBinding { name, .. } => {
let decl_ty = self.type_checker.check_module(&AstModule {
name: "repl".to_string(),
decls: context_decls.clone(),
})?;
println!("{} : {}", name, type_to_string(&decl_ty));
}
_ => {}
}
}
}
// Update accumulated state with new declarations
self.accumulated_decls.extend(new_decls.clone());
// Collect user-defined identifiers for tab completion.
for decl in &new_decls {
let name = match decl {
Decl::Function { name, .. } => Some(name.as_str()),
Decl::Actor { name, .. } => Some(name.as_str()),
Decl::StateMachine { name, .. } => Some(name.as_str()),
Decl::TypeAlias { name, .. } => Some(name.as_str()),
Decl::RecordType { name, .. } => Some(name.as_str()),
Decl::VariantType { name, .. } => Some(name.as_str()),
Decl::EffectDecl { name, .. } => Some(name.as_str()),
Decl::Workflow { name, .. } => Some(name.as_str()),
Decl::Agent { name, .. } => Some(name.as_str()),
Decl::Database { name, .. } => Some(name.as_str()),
Decl::NamedHandler { name, .. } => Some(name.as_str()),
Decl::Class { name, .. } => Some(name.as_str()),
_ => None,
};
if let Some(n) = name {
self.user_names.borrow_mut().insert(n.to_string());
}
}
Ok(())
}
/// Show the inferred type of an expression (without executing).
fn show_type(&mut self, source: &str) -> NuResult<()> {
// Wrap in let ... in ... if needed to make it a valid module expression
let wrapped = if !source.contains("let ") && !source.contains("fn ") {
format!("{}", source)
} else {
source.to_string()
};
let ast = parse_source(&wrapped)?;
// Extract the expression to type-check
let expr = extract_main_expr(&ast)?;
// Build combined module with accumulated decls + this expression
let mut combined_decls = self.accumulated_decls.clone();
combined_decls.push(Decl::Function {
name: "__main".to_string(),
type_params: vec![],
type_param_constraints: vec![],
params: vec![],
default_values: vec![],
using_params: vec![],
ret_type: None,
error_type: None,
effect: None,
cap: None,
body: expr,
annotations: vec![],
public: false,
span: Span::default(),
});
let module = AstModule {
name: "typecheck".to_string(),
decls: combined_decls,
};
let ty = self.type_checker.check_module(&module)?;
let expr_ty = extract_return_type(&ty);
println!("{}", type_to_string(expr_ty));
Ok(())
}
/// Show the AST of an expression.
fn show_ast(&mut self, source: &str) -> NuResult<()> {
let ast = parse_source(source)?;
let expr = extract_main_expr(&ast)?;
println!("{:#?}", expr);
Ok(())
}
/// Show bytecode for the last compiled expression.
fn show_bytecode(&self) {
match &self.last_bytecode {
Some(bc) => println!("{}", bc),
None => println!("No bytecode available. Evaluate an expression first."),
}
}
fn print_help(&self) {
println!("Commands:");
println!(" :quit, :q Exit the REPL");
println!(" :help, :h [t] Show help (topics: syntax, types, actors, effects, commands)");
println!(" :type <expr> Show the inferred type of an expression");
println!(" :load <file> Load and run a .nula file");
println!(" :ast <expr> Show the AST of an expression");
println!(" :bytecode, :bc Show bytecode for the last expression");
println!(" :clear Clear the screen");
println!(" :reset Reset the environment");
println!(" :stats Show runtime stats (--verbose mode only)");
println!(" :version, :ver Print version and exit (repl keeps running)");
}
fn print_help_topic(&self, topic: &str) {
match topic {
"syntax" => {
println!("Nulang Syntax Overview");
println!("======================");
println!();
println!("Basic expressions:");
println!(" 1 + 2 * 3 -- arithmetic");
println!(" true && false -- booleans");
println!(" \"hello\" ^ \" world\" -- string concatenation");
println!();
println!("Let bindings:");
println!(" let x = 5 in x + 1");
println!(" let rec factorial = fn(n) {{ if n <= 1 then 1 else n * factorial(n - 1) }} in factorial(5)");
println!();
println!("Functions:");
println!(" fn add(x, y) {{ x + y }}");
println!(" fn add(x: Int, y: Int) -> Int {{ x + y }}");
println!();
println!("Control flow:");
println!(" if condition then expr1 else expr2");
println!(" match expr {{ pattern1 => result1, pattern2 => result2 }}");
}
"types" => {
println!("Nulang Type System");
println!("==================");
println!();
println!("Primitive types: Int, Float, String, Bool, Unit, Nil");
println!("Compound types:");
println!(" (Int, String) -- tuple");
println!(" {{ x: Int, y: Int }} -- record");
println!(" [Int] -- array");
println!(" Int -> String -- function");
println!(" Int -> String !{{IO}} -- function with effects");
println!();
println!("Type aliases:");
println!(" type Point = {{ x: Int, y: Int }}");
println!();
println!("Capabilities: val, ref, iso, trn, box, tag, linear");
println!("Use :type <expr> to see the inferred type of any expression.");
}
"actors" => {
println!("Nulang Actors");
println!("=============");
println!();
println!("Actors are the core concurrency primitive in Nulang.");
println!("Each actor has private state and communicates via message passing.");
println!();
println!(" actor Counter {{");
println!(" state: Int = 0;");
println!(" behavior inc {{ self.state <- self.state + 1 }}");
println!(" behavior get -> Int {{ self.state }}");
println!(" }}");
println!();
println!(" let counter = spawn Counter;");
println!(" send counter.inc;");
println!(" let v = ask counter.get;");
println!();
println!("Actor features:");
println!(" - Spawn/ask/send for message passing");
println!(" - Links and monitors for fault tolerance");
println!(" - Persistent actors with event sourcing");
println!(" - Workflows with steps, compensation, and signals");
println!(" - CRDT-based state for automatic conflict resolution");
}
"effects" => {
println!("Nulang Effect System");
println!("====================");
println!();
println!("Effects track side effects in the type system.");
println!();
println!("Built-in effects:");
println!(" IO -- input/output (print, read, flush)");
println!(" Timer -- sleep, now");
println!(" Signal -- wait, notify");
println!(" Net -- connect, listen");
println!(" LLM -- AI inference");
println!();
println!("Function signatures declare effects:");
println!(" fn pure() -> Int !{{}} {{ 42 }} -- no effects");
println!(" fn io_fn() -> Unit !{{IO}} {{ perform IO.print(\"x\") }}");
println!();
println!("Effects are checked interprocedurally — calling an IO function");
println!("requires the caller to declare IO in its effect row.");
}
"commands" => {
println!("REPL Commands");
println!("=============");
println!();
println!("All commands start with colon (:).");
println!();
println!(" :quit, :q Exit the REPL");
println!(" :help, :h [t] Show help (topics: syntax, types, actors, effects, commands)");
println!(" :type <expr> Show the inferred type without evaluating");
println!(" :load <file> Load and evaluate a .nula file");
println!(" :ast <expr> Show the parsed AST");
println!(" :bytecode, :bc Show the last evaluated expression's bytecode");
println!(" :clear Clear the terminal screen");
println!(" :reset Clear all accumulated definitions and types");
println!(" :stats Show runtime statistics");
println!(" :version, :ver Print the Nulang version");
println!();
println!("Multi-line input:");
println!(" The REPL automatically enters multi-line mode when it detects");
println!(" unclosed braces, parens, or brackets. The prompt changes to");
println!(" '.... ' until the expression is complete.");
}
_ => {
eprintln!(
"Unknown help topic: '{}'. Topics: syntax, types, actors, effects, commands.",
topic
);
}
}
}
/// Load and evaluate a .nula file in the REPL context.
fn load_file(&mut self, path: &str) -> NuResult<()> {
let source = std::fs::read_to_string(path).map_err(|e| {
NuError::parse_error(
format!("Cannot read file '{}': {}", path, e),
Span::default(),
)
})?;
println!("Loaded '{}' ({} bytes)", path, source.len());
self.evaluate(&source)
}
fn print_error(&self, err: &NuError) {
if std::io::stderr().is_terminal() {
eprint!("{}", err.format_rich());
} else {
eprintln!("Error: {}", err);
}
}
/// Execute source code without running the interactive loop.
/// Used by the CLI for --eval mode.
pub fn execute(&mut self, source: &str) -> NuResult<Value> {
self.evaluate(source)?;
Ok(Value::unit())
}
/// Number of closure capture environments currently retained by the
/// REPL's VM. Exposed for testing that `clear_closure_envs` keeps this
/// bounded across repeated evaluations instead of growing forever.
#[cfg(test)]
pub(crate) fn closure_env_count(&self) -> usize {
self.vm.closure_env_count()
}
/// The last evaluation's disassembled bytecode, if any. Exposed for
/// testing which compiler backend actually ran (the two backends use
/// different register-allocation schemes, so their disassembly differs
/// even for trivial programs).
#[cfg(test)]
pub(crate) fn last_bytecode(&self) -> Option<&str> {
self.last_bytecode.as_deref()
}
/// Track unmatched brace/paren/bracket openers using the lexer, which
/// naturally skips strings and comments. Returns the stack of still-open
/// delimiter characters (most recent last). Mismatched closers are
/// dropped rather than tracked — the parser will report the error.
fn brace_stack(source: &str) -> Vec<char> {
let mut lexer = Lexer::new(source);
let tokens = match lexer.lex() {
Ok(ts) => ts,
Err(_) => return Vec::new(), // Lex error → treat as "done"
};
let mut stack: Vec<char> = Vec::new();
use crate::lexer::TokenKind;
for tok in &tokens {
match tok.kind {
TokenKind::LParen => stack.push('('),
TokenKind::LBrace => stack.push('{'),
TokenKind::LBracket => stack.push('['),
TokenKind::RParen => {
if stack.last() == Some(&'(') {
stack.pop();
}
}
TokenKind::RBrace => {
if stack.last() == Some(&'{') {
stack.pop();
}
}
TokenKind::RBracket => {
if stack.last() == Some(&'[') {
stack.pop();
}
}
_ => {}
}
}
stack
}
}
impl Default for Repl {
fn default() -> Self {
Self::new()
}
}
/// Fallback REPL when rustyline can't initialize (no TTY, pipe, CI, etc.).
fn run_basic_repl() {
println!(
"Nulang v{} \u{2014} Actor-Based Distributed Language",
env!("CARGO_PKG_VERSION")
);
println!("Line editing is not available in this environment.");
println!("Use `nulang --eval '<code>'` to evaluate expressions, or `nulang <file.nula>` to run a file.");
println!("Run `nulang --help` for all options.");
}
// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------
/// Compile via the HIR/MIR pipeline.
fn compile_with_new_pipeline(ast: &AstModule, name: &str) -> NuResult<crate::bytecode::CodeModule> {
let hir = crate::hir_lower::lower_module(ast);
let mir = crate::mir_lower::lower_module(&hir)?;
crate::mir_codegen::compile_mir(&mir, name)
}
/// Parse source code into an AST module.
fn parse_source(source: &str) -> NuResult<AstModule> {
let mut lexer = Lexer::new(source);
let tokens = lexer.lex()?;
let mut parser = Parser::new(tokens);
parser.parse_module()
}
/// Extract the body of the synthetic `__main` function, or return an error
/// if the module doesn't contain an expression.
fn extract_main_expr(ast: &AstModule) -> NuResult<Expr> {
for decl in &ast.decls {
if let Decl::Function { name, body, .. } = decl {
if name == "__main" {
return Ok(body.clone());
}
}
}
Err(NuError::parse_error(
"Expected an expression".to_string(),
Span::default(),
))
}
/// Convert a runtime Value to a pretty display string.
fn value_to_pretty_string(value: &Value) -> String {
value.to_string_repr()
}
/// Extract the return type from a function type, or return the type as-is.
/// This unwraps the __main wrapper function's type (fn(()) -> T) to get
/// the actual expression type T.
fn extract_return_type(ty: &Type) -> &Type {
match ty {
Type::Function { ret, .. } => ret,
_ => ty,
}
}
/// Convert a Type to a human-readable string.
pub fn type_to_string(ty: &Type) -> String {
match ty {
Type::Var(v) => format!("'t{}", v.0),
Type::Primitive(p) => match p {
crate::types::PrimitiveType::Int => "Int".to_string(),
crate::types::PrimitiveType::Float => "Float".to_string(),
crate::types::PrimitiveType::Bool => "Bool".to_string(),
crate::types::PrimitiveType::String => "String".to_string(),
crate::types::PrimitiveType::Unit => "Unit".to_string(),
crate::types::PrimitiveType::Nil => "Nil".to_string(),
crate::types::PrimitiveType::Never => "Never".to_string(),
crate::types::PrimitiveType::Address => "Address".to_string(),
},
Type::Tuple(ts) => {
let parts: Vec<String> = ts.iter().map(type_to_string).collect();
format!("({})", parts.join(", "))
}
Type::Record(fs) => {
let parts: Vec<String> = fs
.iter()
.map(|(n, t)| format!("{}: {}", n, type_to_string(t)))
.collect();
format!("{{ {} }}", parts.join(", "))
}
Type::Variant(vs) => {
let parts: Vec<String> = vs
.iter()
.map(|(n, t)| match t {
Some(t) => format!("{} {}", n, type_to_string(t)),
None => n.clone(),
})
.collect();
format!("{}", parts.join(" | "))
}
Type::Array(t) => format!("[{}]", type_to_string(t)),
Type::Function {
param,
ret,
effect,
cap,
} => {
let param_str = type_to_string(param);
let ret_str = type_to_string(ret);
let eff_str = if effect.effects().is_empty() {
String::new()
} else {
format!(" !{:?}", effect)
};
let cap_str = if *cap == Capability::Ref {