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
use std::fmt::Display;
use std::ops::Add;

use erg_common::color::{GREEN, RED, YELLOW, RESET};
use erg_common::config::Input;
use erg_common::error::{ErrorCore, ErrorKind::*, ErrorDisplay, MultiErrorDisplay, Location};
use erg_common::traits::{Stream, Locational};
use erg_common::ty::{Type, Predicate};
use erg_common::{Str, fmt_iter};
use erg_common::{impl_stream_for_wrapper, switch_lang};

use erg_parser::error::{ParserRunnerError, ParserRunnerErrors};

use crate::hir::Expr;

/// dname is for "double under name"
pub fn binop_to_dname(op: &str) -> &str {
    match op {
        "+" => "__add__",
        "-" => "__sub__",
        "*" => "__mul__",
        "/" => "__div__",
        "**" => "__pow__",
        "%" => "__mod__",
        ".." => "__rng__",
        "<.." => "__lrng__",
        "..<" => "__rrng__",
        "<..<" => "__lrrng__",
        "and" => "__and__",
        "or" => "__or__",
        "in" => "__in__",
        "contains" => "__contains__",
        "subof" => "__subof__",
        "supof" => "__supof__",
        "is" => "__is__",
        "isnot" => "__isnot__",
        "==" => "__eq__",
        "!=" => "__ne__",
        "<" => "__lt__",
        "<=" => "__le__",
        ">" => "__gt__",
        ">=" => "__ge__",
        _ => todo!(),
    }
}

pub fn unaryop_to_dname(op: &str) -> &str {
    match op {
        "+" => "__pos__",
        "-" => "__neg__",
        "~" => "__invert__",
        "!" => "__mutate__",
        "..." => "__spread__",
        _ => todo!(),
    }
}

pub fn readable_name(name: &str) -> &str {
    match name {
        "__add__" => "`+`",
        "__sub__" => "`-`",
        "__mul__" => "`*`",
        "__div__" => "`/`",
        "__pow__" => "`**`",
        "__mod__" => "`%`",
        "__rng__" => "`..`",
        "__lrng__" => "`<..`",
        "__rrng__" => "`..<`",
        "__lrrng__" => "`<..<`",
        "__and__" => "`and`",
        "__or__" => "`or`",
        "__in__" => "`in`",
        "__contains__" => "`contains`",
        "__subof__" => "`subof`",
        "__supof__" => "`supof`",
        "__is__" => "`is`",
        "__isnot__" => "`isnot`",
        "__eq__" => "`==`",
        "__ne__" => "`!=`",
        "__lt__" => "`<`",
        "__le__" => "`<=`",
        "__gt__" => "`>`",
        "__ge__" => "`>=`",
        "__pos__" => "`+`",
        "__neg__" => "`-`",
        "__invert__" => "`~`",
        "__mutate__" => "`!`",
        "__spread__" => "`...`",
        other => other,
    }
}

#[derive(Debug)]
pub struct CompileError {
    pub core: ErrorCore,
    pub input: Input,
    pub caused_by: Str,
}

impl From<ParserRunnerError> for CompileError {
    fn from(err: ParserRunnerError) -> Self {
        Self { core: err.core, input: err.input, caused_by: "".into() }
    }
}

impl ErrorDisplay for CompileError {
    fn core(&self) -> &ErrorCore { &self.core }
    fn input(&self) -> &Input { &self.input }
    fn caused_by(&self) -> &str { &self.caused_by }
    fn ref_inner(&self) -> Option<&Box<Self>> { None }
}

impl CompileError {
    pub const fn new(core: ErrorCore, input: Input, caused_by: Str) -> Self { Self{ core, input, caused_by } }

    pub fn compiler_bug(errno: usize, input: Input, loc: Location, fn_name: &str, line: u32) -> Self {
        Self::new(ErrorCore::new(errno, CompilerSystemError, loc, switch_lang!(
            format!("this is a bug of the Erg compiler, please report it to https://github.com/...\ncaused from: {fn_name}:{line}"),
            format!("これはErg compilerのバグです、開発者に報告して下さい (https://github.com/...)\n{fn_name}:{line}より発生")
        ), None), input, "".into())
    }

    pub fn stack_bug(input: Input, loc: Location, stack_len: u32, block_id: usize, fn_name: &str) -> Self {
        Self::new(ErrorCore::new(0, CompilerSystemError, loc, switch_lang!(
            format!("the number of elements in the stack is invalid (num of elems: {stack_len}, block id: {block_id})\n\
                    this is a bug of the Erg compiler, please report it (https://github.com/...)\n\
                    caused from: {fn_name}"),
            format!("スタックの要素数が異常です (要素数: {stack_len}, ブロックID: {block_id})\n\
                    これはコンパイラのバグです、開発者に報告して下さい (https://github.com/...)\n\
                    {fn_name}より発生")
        ), None), input, "".into())
    }

    pub fn feature_error(input: Input, loc: Location, name: &str, caused_by: Str) -> Self {
        Self::new(ErrorCore::new(0, FeatureError, loc, switch_lang!(
            format!("this feature({name}) is not implemented yet"),
            format!("この機能({name})はまだ正式に提供されていません")
        ), None), input, caused_by)
    }
}

#[derive(Debug)]
pub struct TyCheckError {
    pub core: ErrorCore,
    pub caused_by: Str,
}

impl TyCheckError {
    pub const fn new(core: ErrorCore, caused_by: Str) -> Self { Self{ core, caused_by } }

    pub fn unreachable(fn_name: &str, line: u32) -> Self { Self::new(ErrorCore::unreachable(fn_name, line), "".into()) }

    pub fn checker_bug(errno: usize, loc: Location, fn_name: &str, line: u32) -> Self {
        Self::new(ErrorCore::new(errno, CompilerSystemError, loc, switch_lang!(
            format!("this is a bug of the Erg compiler, please report it to https://github.com/...\ncaused from: {fn_name}:{line}"),
            format!("これはErg compilerのバグです、開発者に報告して下さい (https://github.com/...)\n{fn_name}:{line}より発生")
        ), None), "".into())
    }

    pub fn feature_error(loc: Location, name: &str, caused_by: Str) -> Self {
        Self::new(ErrorCore::new(0, FeatureError, loc, switch_lang!(
            format!("this feature({name}) is not implemented yet"),
            format!("この機能({name})はまだ正式に提供されていません")
        ), None), caused_by)
    }

    pub fn syntax_error<S: Into<Str>>(errno: usize, loc: Location, caused_by: Str, desc: S, hint: Option<Str>) -> Self {
        Self::new(ErrorCore::new(errno, SyntaxError, loc, desc, hint), caused_by)
    }

    pub fn duplicate_decl_error(loc: Location, caused_by: Str, name: &str) -> Self {
        let name = readable_name(name);
        Self::new(ErrorCore::new(0, NameError, loc,
            switch_lang!(
                format!("{name} is already declared"),
                format!("{name}は既に宣言されています")
            ), Option::<Str>::None),
            caused_by
        )
    }

    pub fn violate_decl_error(loc: Location, caused_by: Str, name: &str, spec_t: &Type, found_t: &Type) -> Self {
        let name = readable_name(name);
        Self::new(ErrorCore::new(0, TypeError, loc,
            switch_lang!(
                format!("{name} was declared as {GREEN}{spec_t}{RESET}, but an {RED}{found_t}{RESET} object is assigned"),
                format!("{name}は{GREEN}{spec_t}{RESET}型として宣言されましたが、{RED}{found_t}{RESET}型のオブジェクトが代入されています")
            ), Option::<Str>::None),
            caused_by
        )
    }

    pub fn no_type_spec_error(loc: Location, caused_by: Str, name: &str) -> Self {
        let name = readable_name(name);
        Self::new(ErrorCore::new(0, TypeError, loc,
            switch_lang!(
                format!("the type of {name} is not specified"),
                format!("{name}の型が指定されていません")
            ), None),
            caused_by
        )
    }

    pub fn no_var_error(loc: Location, caused_by: Str, name: &str, similar_name: Option<&Str>) -> Self {
        let name = readable_name(name);
        let hint = similar_name.map(|n| {
            let n = readable_name(n);
            switch_lang!(
                format!("exists a similar name variable: {n}"),
                format!("似た名前の変数があります: {n}")
            ).into()
        });
        Self::new(ErrorCore::new(0, NameError, loc, switch_lang!(
            format!("{RED}{name}{RESET} is not defined"),
            format!("{RED}{name}{RESET}という変数は定義されていません")
        ), hint), caused_by)
    }

    pub fn no_attr_error(
        loc: Location,
        caused_by: Str,
        obj_t: &Type,
        name: &str,
        similar_name: Option<&Str>,
    ) -> Self {
        let hint = similar_name.map(|n| {
            let n = readable_name(n);
            switch_lang!(
                format!("has a similar name attribute: {n}"),
                format!("似た名前の属性があります: {n}")
            ).into()
        });
        Self::new(ErrorCore::new( 0, AttributeError, loc, switch_lang!(
                format!("{obj_t} object has no attribute {RED}{name}{RESET}"),
                format!("{obj_t}型オブジェクトに{RED}{name}{RESET}という属性はありません")
            ), hint),
            caused_by,
        )
    }

    pub fn callable_impl_error<'a, C: Locational + Display>(callee: &C, param_ts: impl Iterator<Item=&'a Type>, caused_by: Str) -> Self {
        let param_ts =  fmt_iter(param_ts);
        Self::new(ErrorCore::new(0, NotImplementedError, callee.loc(), switch_lang!(
            format!("{callee} is not a Callable object that takes {param_ts} as an argument"),
            format!("{callee}は{param_ts}を引数に取る呼び出し可能オブジェクトではありません")
        ), None), caused_by)
    }

    pub fn type_mismatch_error(
        loc: Location,
        caused_by: Str,
        name: &str,
        expect: &Type,
        found: &Type,
    ) -> Self {
        let name = readable_name(name);
        Self::new(ErrorCore::new(0, TypeError, loc, switch_lang!(
            format!("the type of {name} is mismatched:\nexpected:  {GREEN}{expect}{RESET}\nbut found: {RED}{found}{RESET}"),
            format!("{name}の型が違います。\n予期した型: {GREEN}{expect}{RESET}\n与えられた型: {RED}{found}{RESET}")
        ), None), caused_by)
    }

    pub fn return_type_error(
        loc: Location,
        caused_by: Str,
        name: &str,
        expect: &Type,
        found: &Type,
    ) -> Self {
        let name = readable_name(name);
        Self::new(ErrorCore::new(0, TypeError, loc, switch_lang!(
            format!("the return type of {name} is mismatched:\nexpected:  {GREEN}{expect}{RESET}\nbut found: {RED}{found}{RESET}"),
            format!("{name}の戻り値の型が違います。\n予期した型: {GREEN}{expect}{RESET}\n与えられた型: {RED}{found}{RESET}")
        ), None), caused_by)
    }

    pub fn uninitialized_error(loc: Location, caused_by: Str, name: &str, t: &Type) -> Self {
        let name = readable_name(name);
        Self::new(ErrorCore::new(0, NameError, loc, switch_lang!(
            format!("{name}: {t} is not initialized"),
            format!("{name}: {t}は初期化されていません")
        ), None), caused_by)
    }

    pub fn argument_error(loc: Location, caused_by: Str, expect: usize, found: usize) -> Self {
        Self::new(ErrorCore::new(0, TypeError, loc, switch_lang!(
            format!("the number of positional arguments is mismatched:\nexpected:  {GREEN}{expect}{RESET}\nbut found: {RED}{found}{RESET}"),
            format!("ポジショナル引数の数が違います。\n予期した個数: {GREEN}{expect}{RESET}\n与えられた個数: {RED}{found}{RESET}")
        ), None), caused_by)
    }

    pub fn match_error(loc: Location, caused_by: Str, expr_t: &Type) -> Self {
        Self::new(ErrorCore::new(0, TypeError, loc, switch_lang!(
            format!("not all patterns of type {expr_t} are covered"),
            format!("{expr_t}型の全パターンを網羅していません")
        ), None), caused_by)
    }

    pub fn infer_error(loc: Location, caused_by: Str, expr: &str) -> Self {
        Self::new(ErrorCore::new(0, TypeError, loc, switch_lang!(
            format!("failed to infer the type of {expr}"),
            format!("{expr}の型が推論できません")
        ), None), caused_by)
    }

    pub fn reassign_error(loc: Location, caused_by: Str, name: &str) -> Self {
        let name = readable_name(name);
        Self::new(ErrorCore::new(0, AssignError, loc, switch_lang!(
            format!("cannot assign twice to the immutable variable {name}"),
            format!("定数{name}には再代入できません")
        ), None), caused_by)
    }

    pub fn too_many_args_error(
        loc: Location,
        callee_name: &str,
        caused_by: Str,
        params_len: usize,
        pos_args_len: usize,
        kw_args_len: usize
    ) -> Self {
        let name = readable_name(callee_name);
        Self::new(ErrorCore::new(0, TypeError, loc, switch_lang!(
            format!("too many arguments for {name}:
total expected params:  {GREEN}{params_len}{RESET}
passed positional args: {RED}{pos_args_len}{RESET}
passed keyword args:    {RED}{kw_args_len}{RESET}"),
            format!("{name}に渡された引数の数が多すぎます。
必要な引数の合計数: {GREEN}{params_len}{RESET}個
渡された引数の数:   {RED}{pos_args_len}{RESET}個
キーワード引数の数: {RED}{kw_args_len}{RESET}個")
        ), None), caused_by)
    }

    pub fn multiple_args_error(
        loc: Location,
        callee_name: &str,
        caused_by: Str,
        arg_name: &str,
    ) -> Self {
        let name = readable_name(callee_name);
        Self::new(ErrorCore::new(0, TypeError, loc, switch_lang!(
            format!("{name}'s argument {RED}{arg_name}{RESET} is passed multiple times"),
            format!("{name}の引数{RED}{arg_name}{RESET}が複数回渡されています")
        ), None), caused_by)
    }

    pub fn unexpected_kw_arg_error(
        loc: Location,
        callee_name: &str,
        caused_by: Str,
        param_name: &str,
    ) -> Self {
        let name = readable_name(callee_name);
        Self::new(ErrorCore::new(0, TypeError, loc, switch_lang!(
            format!("{name} got unexpected keyword argument {RED}{param_name}{RESET}"),
            format!("{name}には予期しないキーワード引数{RED}{param_name}{RESET}が渡されています")
        ), None), caused_by)
    }

    pub fn unused_warning(loc: Location, name: &str, caused_by: Str) -> Self {
        let name = readable_name(name);
        Self::new(ErrorCore::new(0, UnusedWarning, loc, switch_lang!(
            format!("{YELLOW}{name}{RESET} is not used"),
            format!("{YELLOW}{name}{RESET}は使用されていません")
        ), None), caused_by)
    }

    pub fn unification_error(lhs_t: &Type, rhs_t: &Type, lhs_loc: Option<Location>, rhs_loc: Option<Location>, caused_by: Str) -> Self {
        let loc = match (lhs_loc, rhs_loc) {
            (Some(l), Some(r)) => Location::pair(l, r),
            (Some(l), None) => l,
            (None, Some(r)) => r,
            (None, None) => Location::Unknown,
        };
        Self::new(ErrorCore::new(0, TypeError, loc, switch_lang!(
            format!("unification failed:\nlhs: {YELLOW}{lhs_t}{RESET}\nrhs: {YELLOW}{rhs_t}{RESET}"),
            format!("型の単一化に失敗しました:\n左辺: {YELLOW}{lhs_t}{RESET}\n右辺: {YELLOW}{rhs_t}{RESET}")
        ), None), caused_by)
    }

    pub fn re_unification_error(lhs_t: &Type, rhs_t: &Type, lhs_loc: Option<Location>, rhs_loc: Option<Location>, caused_by: Str) -> Self {
        let loc = match (lhs_loc, rhs_loc) {
            (Some(l), Some(r)) => Location::pair(l, r),
            (Some(l), None) => l,
            (None, Some(r)) => r,
            (None, None) => Location::Unknown,
        };
        Self::new(ErrorCore::new(0, TypeError, loc, switch_lang!(
            format!("re-unification failed:\nlhs: {YELLOW}{lhs_t}{RESET}\nrhs: {YELLOW}{rhs_t}{RESET}"),
            format!("型の再単一化に失敗しました:\n左辺: {YELLOW}{lhs_t}{RESET}\n右辺: {YELLOW}{rhs_t}{RESET}")
        ), None), caused_by)
    }

    pub fn pred_unification_error(lhs: &Predicate, rhs: &Predicate, caused_by: Str) -> Self {
        Self::new(ErrorCore::new(0, TypeError, Location::Unknown, switch_lang!(
            format!("predicate unification failed:\nlhs: {YELLOW}{lhs}{RESET}\nrhs: {YELLOW}{rhs}{RESET}"),
            format!("述語式の単一化に失敗しました:\n左辺: {YELLOW}{lhs}{RESET}\n右辺: {YELLOW}{rhs}{RESET}")
        ), None), caused_by)
    }

    pub fn has_effect<S: Into<Str>>(expr: &Expr, caused_by: S) -> Self {
        Self::new(ErrorCore::new(0, HasEffect, expr.loc(), switch_lang!(
            format!("this expression causes a side-effect"),
            format!("この式には副作用があります")
        ), None), caused_by.into())
    }

    pub fn move_error<S: Into<Str>>(name: &str, name_loc: Location, moved_loc: Location, caused_by: S) -> Self {
        Self::new(ErrorCore::new(0, MoveError, name_loc, switch_lang!(
            format!("{RED}{name}{RESET} was moved in line {}", moved_loc.ln_begin().unwrap()),
            format!("{RED}{name}{RESET}は{}行目ですでに移動されています", moved_loc.ln_begin().unwrap())
        ), None), caused_by.into())
    }
}

#[derive(Debug)]
pub struct TyCheckErrors(Vec<TyCheckError>);

impl_stream_for_wrapper!(TyCheckErrors, TyCheckError);

impl From<Vec<TyCheckError>> for TyCheckErrors {
    fn from(errs: Vec<TyCheckError>) -> Self { Self(errs) }
}

impl Add for TyCheckErrors {
    type Output = Self;
    fn add(self, other: Self) -> Self {
        Self(self.0.into_iter().chain(other.0.into_iter()).collect())
    }
}

impl From<TyCheckError> for TyCheckErrors {
    fn from(err: TyCheckError) -> Self { Self(vec![err]) }
}

pub type TyCheckResult<T> = Result<T, TyCheckError>;
pub type TyCheckWarning = TyCheckError;
pub type TyCheckWarnings = TyCheckErrors;

pub type EvalError = TyCheckError;
pub type EvalErrors = TyCheckErrors;
pub type EvalResult<T> = TyCheckResult<T>;

pub type EffectError = TyCheckError;
pub type EffectErrors = TyCheckErrors;
pub type EffectResult<T> = Result<T, EffectErrors>;

pub type OwnershipError = TyCheckError;
pub type OwnershipErrors = TyCheckErrors;
pub type OwnershipResult<T> = Result<T, OwnershipErrors>;

pub type LowerError = TyCheckError;
pub type LowerWarning = LowerError;
pub type LowerErrors = TyCheckErrors;
pub type LowerWarnings = LowerErrors;
pub type LowerResult<T> = TyCheckResult<T>;

#[derive(Debug)]
pub struct CompileErrors(Vec<CompileError>);

impl_stream_for_wrapper!(CompileErrors, CompileError);

impl From<ParserRunnerErrors> for CompileErrors {
    fn from(err: ParserRunnerErrors) -> Self {
        Self(err.into_iter().map(CompileError::from).collect())
    }
}

impl From<Vec<CompileError>> for CompileErrors {
    fn from(errs: Vec<CompileError>) -> Self { Self(errs) }
}

impl From<CompileError> for CompileErrors {
    fn from(err: CompileError) -> Self { Self(vec![err]) }
}

impl MultiErrorDisplay<CompileError> for CompileErrors {}

impl CompileErrors {
    pub fn flush(&mut self) -> Self {
        Self(self.0.drain(..).collect())
    }
}

pub type CompileResult<T> = Result<T, CompileError>;
pub type CompileWarning = CompileError;
pub type CompileWarnings = CompileErrors;