Skip to content

Commit dac4619

Browse files
committed
fix some bugs
1 parent f6402a7 commit dac4619

File tree

8 files changed

+89
-25
lines changed

8 files changed

+89
-25
lines changed

interpreter/src/backend/evaluate.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1433,10 +1433,11 @@ mod tests {
14331433
// interpret(format!("{}{}", INSPECT, ";3 |`m_inspect(n) {n+1}")),
14341434
3
14351435
);
1436-
assert_eq!(
1437-
interpret(format!("{}{}", INSPECT, ";3 |`m_inspect(n) {n+1}")),
1438-
3
1439-
);
1436+
// TODO: proper identifier binding between the types and the chain
1437+
// assert_eq!(
1438+
// interpret(format!("{}{}", INSPECT, ";3 |`m_inspect(n) {n+1}")),
1439+
// 3
1440+
// );
14401441
assert_eq!(
14411442
interpret_io(
14421443
format!("{}{}", INSPECT, ";3 |`m_inspect (n) {+1 |to_str |print}"),

interpreter/src/common.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,22 @@ pub fn bug_span<T, S: AsRef<str>>(
7575
.into())
7676
}
7777
#[track_caller]
78+
pub fn bug_maybe_span<T, S: AsRef<str>>(
79+
error_message: S,
80+
code: Option<&SourceCode>,
81+
span: Span,
82+
) -> Result<T, AnyError> {
83+
// place your breakpoints here
84+
let caller_location = std::panic::Location::caller();
85+
Err(format!(
86+
"\n{}:\nBug: {}{}",
87+
caller_location,
88+
error_message.as_ref(),
89+
maybe_format_span(code, span)
90+
)
91+
.into())
92+
}
93+
#[track_caller]
7894
pub fn err_since<T, S: AsRef<str>>(
7995
error_message: S,
8096
code: &SourceCode,

interpreter/src/frontend/parser.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ pub mod reverse_iterative_parser;
88
// pub mod slow_iterative_parser;
99

1010
// pub mod fast_iterative_parser;
11-
mod import;
11+
pub mod import;
1212
// #[cfg(test)]
1313
// pub mod recursive_parser;
1414
pub mod nodes_to_expression;

interpreter/src/frontend/parser/import.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,7 @@ fn find_source_code(
371371
Ok((root, relative_path_to_import, source_code))
372372
}
373373

374-
fn get_relative_path_to_import(identifier: &str) -> PathBuf {
374+
pub fn get_relative_path_to_import(identifier: &str) -> PathBuf {
375375
let mut namespace_parts = identifier
376376
.split('/')
377377
.map(|s| s.to_string())

interpreter/src/frontend/sources.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1+
use crate::frontend::parser::import::get_relative_path_to_import;
2+
use crate::frontend::sources::location::SourceCode;
13
use std::collections::hash_map::Keys;
24
use std::collections::HashMap;
35

4-
use crate::frontend::sources::location::SourceCode;
5-
66
pub mod lexer;
77
pub mod location;
88
pub mod token;
@@ -40,6 +40,12 @@ impl Sources {
4040
pub fn get(&self, path: &str) -> Option<&SourceCode> {
4141
self.other_sources.get(path)
4242
}
43+
pub fn get_from_qualified(&self, qualified_identifier: &str) -> Option<&SourceCode> {
44+
let path = get_relative_path_to_import(qualified_identifier);
45+
self.other_sources
46+
.get(&path.to_string_lossy().to_string())
47+
.or(Some(&self.main_source))
48+
}
4349
pub fn get_main(&self) -> &SourceCode {
4450
&self.main_source
4551
}

interpreter/src/frontend/sources/location.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,13 @@ impl SourceCode {
313313
"".to_string()
314314
};
315315
let (start_start, start_end) = self.get_current_line_indexes(start.byte);
316+
if start_end > self.text.len() {
317+
return format!(
318+
" at (Bug: mismatched source code span ({}) and source code file ({:?}))",
319+
Span { start, end },
320+
self.file
321+
);
322+
}
316323
let line = &self.text[start_start..start_end];
317324
let (end_start, end_end) = self.get_current_line_indexes(end.byte);
318325
if start_start == end_start && start_end == end_end {

interpreter/src/middleend/comptime.rs

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
use crate::backend::Runtime;
22
use crate::common::{bug, context, err, err_span, AnyError};
33
use crate::frontend::expression::{
4-
Abstract, Chain, Composed, Comptime, Expression, ExpressionSpan, Function, TypedIdentifier,
5-
TypedIdentifiers,
4+
is_macro, Abstract, Chain, Composed, Comptime, Expression, ExpressionSpan, Function,
5+
TypedIdentifier, TypedIdentifiers,
66
};
77
use crate::frontend::parser::reverse_iterative_parser::{err_expected_span, expected_span};
88
use crate::frontend::program::{Identifiers, Program};
9-
use crate::frontend::sources::location::Span;
9+
use crate::frontend::sources::location::{Span, NO_SPAN};
1010
use crate::frontend::sources::token::{Keyword, Operator};
1111
use crate::frontend::sources::Sources;
1212
use crate::middleend::intrinsics::builtin_types;
@@ -34,9 +34,29 @@ struct Rewriter {
3434
impl Rewriter {
3535
fn rewrite_contextless(mut self, mut main: ExpressionSpan) -> Result<Program, AnyError> {
3636
self.rewrite_expression_span(&mut main)?;
37+
self.delete_macros(&mut main);
3738
Ok(Program::new_from(main, self.identifiers, self.sources))
3839
}
3940

41+
fn delete_macros(&mut self, main: &mut ExpressionSpan) {
42+
let mut to_delete = Vec::new();
43+
for (identifier, expr) in &self.identifiers {
44+
if is_macro(expr) {
45+
to_delete.push(identifier.clone());
46+
}
47+
}
48+
49+
for to_delete in to_delete {
50+
self.identifiers.remove(&to_delete);
51+
52+
replace(
53+
&TypedIdentifier::any(to_delete.clone()),
54+
&ExpressionSpan::new_typeless(Expression::Nothing, NO_SPAN),
55+
main,
56+
)
57+
}
58+
}
59+
4060
fn rewrite_expression_span(
4161
&mut self,
4262
expression_span: &mut ExpressionSpan,
@@ -66,7 +86,11 @@ impl Rewriter {
6686
ExpressionSpan::new(new_expression, builtin_types::I64, expression_span.span);
6787
*expression_span = new_expr_span;
6888
}
69-
Expression::Composed(_) => {}
89+
Expression::Composed(composed) => {
90+
for chain in composed.chains_mut() {
91+
self.rewrite_chain(chain)?;
92+
}
93+
}
7094
Expression::TypedIdentifiers(_) => unimplemented!(),
7195
Expression::Abstract(_) => unimplemented!(),
7296
}

interpreter/src/middleend/typing.rs

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::collections::HashMap;
22
use strum::IntoEnumIterator;
33

4-
use crate::common::{bug, bug_span, context, err, err_span, maybe_format_span, AnyError};
4+
use crate::common::{bug_span, context, err, err_span, maybe_format_span, AnyError};
55
use crate::frontend::expression::display::typed_identifiers_to_str;
66
use crate::frontend::expression::{
77
is_macro, Branch, Browse, BrowseOr, Chain, Composed, Comptime, Expression, ExpressionSpan,
@@ -127,7 +127,7 @@ impl<'a> Typer<'a> {
127127
.get_main()
128128
.file
129129
.as_ref()
130-
.map(|p| format!("{}", p.to_string_lossy()));
130+
.map(|p| p.to_string_lossy().to_string());
131131
Ok((typer, program.main()))
132132
}
133133

@@ -228,21 +228,26 @@ impl<'a> Typer<'a> {
228228
}
229229
}
230230

231-
fn get_identifier_type(&self, name: &String) -> Result<Type, AnyError> {
231+
fn get_identifier_type(&self, name: &String, span: Span) -> Result<Type, AnyError> {
232232
if let Some(types) = self.identifier_types.get(name) {
233233
if let Some(last) = types.last() {
234234
Ok(last.clone())
235235
} else {
236-
err(format!(
237-
"Bug: Identifier '{}' is not bound to any value",
238-
name
239-
))
236+
bug_span(
237+
format!("Identifier '{}' is not bound to any value", name),
238+
self.get_current_source(),
239+
span,
240+
)
240241
}
241242
} else {
242-
err(format!(
243-
"Bug: Undefined identifier '{}'. This should have been detected by earlier stages.",
244-
name
245-
))
243+
bug_span(
244+
format!(
245+
"Undefined identifier '{}'. This should have been detected by earlier stages.",
246+
name
247+
),
248+
self.get_current_source(),
249+
span,
250+
)
246251
}
247252
}
248253
}
@@ -294,7 +299,7 @@ impl<'a> Typer<'a> {
294299
) -> Result<ExpressionSpan, AnyError> {
295300
Ok(ExpressionSpan::new(
296301
Expression::Identifier(name.clone()),
297-
self.get_identifier_type(name)?,
302+
self.get_identifier_type(name, span)?,
298303
span,
299304
))
300305
}
@@ -556,6 +561,11 @@ impl<'a> Typer<'a> {
556561
Ok(Operation::single(operator, typed_operand, type_))
557562
}
558563
Operator::Call => self.get_call_type(input, operands, operator.span),
564+
// Operator::MacroCall => Ok(Operation::several(
565+
// operator,
566+
// operands.clone(),
567+
// builtin_types::ANY,
568+
// )),
559569
Operator::MacroCall => bug_span(
560570
"macro calls should not reach the typing stage. Macro call",
561571
self.sources.get_main(),
@@ -633,7 +643,7 @@ impl<'a> Typer<'a> {
633643
let mut callable = operands.get(0).unwrap().clone();
634644
match &callable.syntactic_type {
635645
Expression::Identifier(name) => {
636-
callable.semantic_type = self.get_identifier_type(name)?;
646+
callable.semantic_type = self.get_identifier_type(name, callable.span)?;
637647
self.check_type_callable(input_type, callable, &operands[1..], operator_span)
638648
}
639649
Expression::Chain(chain) => {

0 commit comments

Comments
 (0)