summaryrefslogtreecommitdiffstats
path: root/crates/rebel-lang/examples/repl.rs
blob: 7ed3966060de5f66f8c82a2db37165cae2c06218 (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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
use std::rc::Rc;

use rebel_lang::{
	func::{Func, FuncDef, FuncType},
	scope::{MethodMap, Scope},
	typing::{self, Type, TypeFamily, VarType},
	value::{self, Value},
	Error, Result,
};
use rebel_parse::{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::Int(
		array
			.len()
			.try_into()
			.or(Err(Error::eval("array length out of bounds")))?,
	))
}
fn intrinsic_string_len(params: &[Value]) -> Result<Value> {
	assert!(params.len() == 1);
	let Value::Str(string) = &params[0] else {
		panic!();
	};
	Ok(Value::Int(
		string
			.chars()
			.count()
			.try_into()
			.or(Err(Error::eval("string length out of bounds")))?,
	))
}

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 methods = MethodMap::default();
	methods.entry(TypeFamily::Array).or_default().insert(
		"len",
		Func {
			typ: FuncType {
				params: vec![Type::Array(Box::new(Type::Free))],
				ret: Type::Int,
			},
			def: Some(FuncDef::Intrinsic(intrinsic_array_len)),
		},
	);
	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 methods = Rc::new(methods);

	let mut type_scope = Box::<Scope<VarType>>::default();
	type_scope.methods = methods.clone();
	let mut value_scope = Box::<Scope<Value>>::default();
	value_scope.methods = methods.clone();

	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::block_stmt(&tokens) {
			Ok(value) => value,
			Err(err) => {
				println!("Parse error: {err}");
				continue;
			}
		};

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

		let mut type_scope_tmp = type_scope.clone();
		let mut type_ctx = typing::Context(&mut type_scope_tmp);
		let typ = match type_ctx.type_block_stmt(&stmt) {
			Ok(typ) => typ,
			Err(err) => {
				println!("Type checking failed: {err}");
				continue;
			}
		};

		let mut value_scope_tmp = value_scope.clone();
		let mut value_ctx = value::Context(&mut value_scope_tmp);
		let value = match value_ctx.eval_block_stmt(&stmt) {
			Ok(value) => value,
			Err(err) => {
				println!("Evaluation failed: {err}");
				continue;
			}
		};

		if value != Value::Unit {
			println!("{value}: {typ}");
		}

		type_scope = type_scope_tmp;
		value_scope = value_scope_tmp;
	}
}