Skip to content

Commit 20206e8

Browse files
committed
Improve typechecking for identifiers that are builtins
1 parent 7030bc4 commit 20206e8

4 files changed

Lines changed: 21 additions & 11 deletions

File tree

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
1-
VERSION 0700
2-
----
3-
0000 GET BUILTIN 16 == 'not'
4-
0003 GET BUILTIN 0 == 'id'
5-
0006 CALL (1 args)
1+
[
2+
(
3+
CompileError(
4+
TypeMismatch {
5+
expected: Bool,
6+
actual: Fn(Value) -> Value,
7+
},
8+
),
9+
5..7,
10+
),
11+
]
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
[
22
(
3-
RuntimeError(
3+
CompileError(
44
TypeMismatch {
55
expected: Bool,
66
actual: Fn(Value) -> Value,
77
},
88
),
9-
0..0,
9+
5..7,
1010
),
1111
]

src/ast.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,11 @@ impl Expr {
5050
pub fn get_type(&self) -> Type {
5151
match self {
5252
Expr::Bool(_) => Type::Bool,
53-
Expr::Identifier(_) => Type::Unknown,
53+
Expr::Identifier(identifier) => identifier
54+
.get_type()
55+
.as_ref()
56+
.unwrap_or(&Type::Unknown)
57+
.clone(),
5458
Expr::Call(_) => Type::Unknown,
5559
Expr::String(_) => Type::String,
5660
Expr::Error => Type::Unknown,

src/errors.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -498,7 +498,7 @@ pub mod diagnostics {
498498

499499
assert_eq!(diagnostics.len(), 1);
500500
let diagnostic = &diagnostics[0];
501-
assert_eq!(diagnostic.code, Some("".to_string()));
501+
assert_eq!(diagnostic.code, Some("lexical".to_string()));
502502
assert_eq!(diagnostic.message, "Invalid token".to_string());
503503
assert_eq!(diagnostic.severity, Severity::Error);
504504
assert_eq!(diagnostic.labels.len(), 1);
@@ -514,7 +514,7 @@ pub mod diagnostics {
514514

515515
assert_eq!(diagnostics.len(), 1);
516516
let diagnostic = &diagnostics[0];
517-
assert_eq!(diagnostic.code, Some("".to_string()));
517+
assert_eq!(diagnostic.code, Some("compiler".to_string()));
518518
assert_eq!(diagnostic.message, "undefined: var".to_string());
519519
assert_eq!(diagnostic.severity, Severity::Error);
520520
assert_eq!(diagnostic.labels.len(), 1);
@@ -533,7 +533,7 @@ pub mod diagnostics {
533533

534534
assert_eq!(diagnostics.len(), 1);
535535
let diagnostic = &diagnostics[0];
536-
assert_eq!(diagnostic.code, Some("".to_string()));
536+
assert_eq!(diagnostic.code, Some("compiler".to_string()));
537537
assert_eq!(
538538
diagnostic.message,
539539
"expects 2 arguments but received 3".to_string()

0 commit comments

Comments
 (0)