summaryrefslogtreecommitdiffstats
path: root/crates/rebel-lang/examples/repl.rs
blob: 455448656675d5f21a65773dded66def8a7dc186 (plain)
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
use rebel_lang::{
	func::{Func, FuncDef, FuncType},
	scope::Context,
	typing::{ArrayLen, Type, TypeFamily},
	value::{EvalError, Result, Value},
};
use rebel_parse::{ast::BodyStmt, recipe, tokenize};
use reedline::{DefaultPrompt, DefaultPromptSegment, Reedline, Signal, ValidationResult};

fn intrinsic_array_len(params: &[Value]) -> Result<Value> {
	assert!(params.len() == 1);
	let Value::Array(array) = &params[0] else {
		panic!();
	};
	Ok(Value::Integer(array.len().try_into().or(Err(EvalError))?))
}
fn intrinsic_string_len(params: &[Value]) -> Result<Value> {
	assert!(params.len() == 1);
	let Value::Str(string) = &params[0] else {
		panic!();
	};
	Ok(Value::Integer(
		string.chars().count().try_into().or(Err(EvalError))?,
	))
}

struct Validator;

impl reedline::Validator for Validator {
	fn validate(&self, line: &str) -> ValidationResult {
		if tokenize::token_stream(line).is_ok() {
			ValidationResult::Complete
		} else {
			ValidationResult::Incomplete
		}
	}
}

fn main() {
	let mut ctx = Context::default();

	ctx.methods.entry(TypeFamily::Array).or_default().insert(
		"len",
		Func {
			typ: FuncType {
				params: vec![Type::Array(Box::new(Type::Free), ArrayLen::Dynamic)],
				ret: Type::Int,
			},
			def: Some(FuncDef::Intrinsic(intrinsic_array_len)),
		},
	);
	ctx.methods.entry(TypeFamily::Str).or_default().insert(
		"len",
		Func {
			typ: FuncType {
				params: vec![Type::Str],
				ret: Type::Int,
			},
			def: Some(FuncDef::Intrinsic(intrinsic_string_len)),
		},
	);

	let mut rl = Reedline::create().with_validator(Box::new(Validator));
	let prompt = DefaultPrompt::new(DefaultPromptSegment::Empty, DefaultPromptSegment::Empty);

	loop {
		let input = match rl.read_line(&prompt).unwrap() {
			Signal::Success(input) => input,
			Signal::CtrlC => continue,
			Signal::CtrlD => break,
		};

		let tokens = match tokenize::token_stream(&input) {
			Ok(value) => value,
			Err(err) => {
				println!("Tokenize error: {err}");
				continue;
			}
		};
		let stmt = match recipe::body_stmt(&tokens) {
			Ok(value) => value,
			Err(err) => {
				println!("Parse error: {err}");
				continue;
			}
		};

		if matches!(stmt, BodyStmt::Empty) {
			continue;
		}

		if let Err(err) = stmt.validate() {
			println!("Validation error: {err:?}");
			continue;
		}
		if let Err(err) = Type::ast_stmt_type(&ctx, &stmt) {
			println!("Type error: {err:?}");
			continue;
		}

		let value = match ctx.run(&stmt) {
			Ok(value) => value,
			Err(err) => {
				println!("Eval error: {err:?}");
				continue;
			}
		};
		let typ = match value.typ() {
			Ok(typ) => typ,
			Err(err) => {
				println!("Post-eval type error: {err:?}. This should not happen.");
				continue;
			}
		};
		println!("{value}: {typ}");
	}
}