Skip to content

Commit

Permalink
Merge pull request #168 from mrodz/166-bug-types-in-module-generation
Browse files Browse the repository at this point in the history
add class type to file data even when not exported
  • Loading branch information
mrodz authored Jan 7, 2024
2 parents 2611d9b + 4e50cd7 commit 85deb67
Show file tree
Hide file tree
Showing 11 changed files with 110 additions and 28 deletions.
6 changes: 0 additions & 6 deletions bytecode/src/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,12 +159,6 @@ impl MScriptFile {
};

functions.run_function(name, args, current_frame, callback_state, jump_callback)

// let functions = self.functions.borrow_mut();

// let functions = functions.as_mut()?.get_mut(name).ok()?.run(args, current_frame, callback_state, jump_callback);

// function.ok()
}

/// Get the path of the file.
Expand Down
5 changes: 2 additions & 3 deletions bytecode/src/interpreter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -313,14 +313,13 @@ impl Program {

if let Err(e) = main_ret {
stdout().lock().flush()?;
eprintln!("\n******* MSCRIPT INTERPRETER FATAL RUNTIME ERROR *******\nCall stack trace:\n{e:?}\n\nPlease report this at https://github.com/mrodz/mscript-lang/issues/new\n");
eprintln!("\n******* MSCRIPT INTERPRETER FATAL RUNTIME ERROR *******\nCall stack trace:\n{e:?}\n\nPlease report this at https://github.com/mrodz/mscript/issues/new/choose\n");
bail!("Interpreter crashed")
}

if stack.borrow().size() != 0 {
stdout().lock().flush()?;

eprintln!("\n******* MSCRIPT INTERPRETER STACK MISMATCH *******\nFound:\n{}\n\n... When the program should have unwinded its stack completely. This is most likely a flaw with the compiler.\n\nPlease report this at https://github.com/mrodz/mscript-lang/issues/new\n", stack.borrow());
eprintln!("\n******* MSCRIPT INTERPRETER STACK MISMATCH *******\nFound:\n{}\n\n... When the program should have unwinded its stack completely. This is most likely a flaw with the compiler.\n\nPlease report this at https://github.com/mrodz/mscript/issues/new/choose\n", stack.borrow());
bail!("Program exited in a confused state, execution integrity has been compromised.")
}

Expand Down
2 changes: 1 addition & 1 deletion compiler/src/ast/import.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ impl Parser {
&input.user_data().get_source_file_name(),
|| {
format!(
"`{}` has no visible member `{maybe_property}`.\n Please ensure that:\n - This import is not cyclical (see <https://wikipedia.org/wiki/Circular_reference>)\n - This item is exported using the `export` keyword\n Hint: You can put shared code in a new file and import it from both origins to work around a cyclical dependency.",
"`{}` has no visible member `{maybe_property}`.\n Please ensure that:\n - This item is exported using the `export` keyword\n - This import is not cyclical (see <https://wikipedia.org/wiki/Circular_reference>)\n Hint: You can put shared code in a new file and import it from both origins to work around a cyclical dependency.",
module_type.name().bytecode_str()
)
},
Expand Down
7 changes: 3 additions & 4 deletions compiler/src/ast/math_expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,9 @@ use crate::{
};

use super::{
dot_lookup::DotChain, function::FunctionType, list::Index, map_err,
new_err, r#type::IntoType, ClassType, CompilationState, Compile, CompileTimeEvaluate,
CompiledItem, Dependencies, Dependency, FunctionArguments, TemporaryRegister, TypeLayout,
Value,
dot_lookup::DotChain, function::FunctionType, list::Index, map_err, new_err, r#type::IntoType,
ClassType, CompilationState, Compile, CompileTimeEvaluate, CompiledItem, Dependencies,
Dependency, FunctionArguments, TemporaryRegister, TypeLayout, Value,
};

pub static PRATT_PARSER: Lazy<PrattParser<Rule>> = Lazy::new(|| {
Expand Down
40 changes: 27 additions & 13 deletions compiler/src/ast/type.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
use crate::{
ast::{class::ClassBody, new_err, value::ValToUsize, Assignment},
ast::{
class::{ClassBody, ClassFlags},
new_err,
value::ValToUsize,
Assignment,
},
parser::{AssocFileData, Node, Parser, Rule},
scope::{ScopeReturnStatus, SuccessTypeSearchResult, TypeSearchResult},
CompilationError, VecErr,
Expand Down Expand Up @@ -231,22 +236,28 @@ impl ModuleType {
Rule::class => {
let mut children = child.children();

let class_flags;
let mut class_flags = None;

let maybe_ident_node = children.next().unwrap();

let ident_node = if maybe_ident_node.as_rule() == Rule::ident {
continue;
maybe_ident_node
} else {
class_flags = maybe_ident_node;
class_flags = Some(maybe_ident_node);
children.next().unwrap()
};

let class_flags = Parser::class_flags(class_flags).to_err_vec()?;
let class_flags = if let Some(class_flags_node) = class_flags {
let class_flags = Parser::class_flags(class_flags_node).to_err_vec()?;
log::info!("ignoring flags: {class_flags:?}");
Some(class_flags)
} else {
None
};

if !class_flags.is_export() {
continue;
}
// if !class_flags.is_export() {
// continue;
// }

let name = ident_node.as_str();

Expand All @@ -273,12 +284,14 @@ impl ModuleType {
.user_data()
.add_type(name.into(), ident.ty().unwrap().clone());

log::trace!(
"Gen. mod {:?} -- adding class {name:?}",
input.user_data().source_path()
);
if class_flags.as_ref().is_some_and(ClassFlags::is_export) {
log::trace!(
"Gen. mod {:?} -- adding class {name:?}",
input.user_data().source_path()
);

export.add(ident);
export.add(ident);
}
}
Rule::assignment => {
if let Ok(assignment) = Assignment::type_from_node(&child) {
Expand All @@ -291,6 +304,7 @@ impl ModuleType {
}
}
Rule::type_alias => {
// Default behavior: always added to `AssocFileData` API
let ty = Parser::type_alias(child).to_err_vec()?;

log::trace!(
Expand Down
61 changes: 61 additions & 0 deletions compiler/src/tests/modules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -293,3 +293,64 @@ fn forward_export() {
.run()
.unwrap()
}

#[test]
fn dependent_hidden_type_exports() {
EvalEnvironment::entrypoint(
"main.ms",
r#"
# these functions cannot be called, because their inputs are hidden and cannot be constructed in main.ms
import take_kilometer, take_mile from other
"#,
)
.unwrap()
.add("other.ms", r#"
type Kilometer int
class Mile {}
export take_kilometer: fn(Kilometer) = fn(input: Kilometer) {
print input
}
export take_mile: fn(Mile) = fn(input: Mile) {
print input
}
"#)
.unwrap()
.run()
.unwrap()
}

#[test]
#[should_panic = "`other.ms` has no visible member `Mile`"]
fn dependent_hidden_type_exports_errors_ok() {
EvalEnvironment::entrypoint(
"main.ms",
r#"
import take_kilometer from other
take_kilometer(5)
# this function cannot be called, because its inputs are hidden and cannot be constructed in main.ms
import take_mile, Mile from other
take_mile(Mile())
"#,
)
.unwrap()
.add("other.ms", r#"
type Kilometer int
class Mile {}
export take_kilometer: fn(Kilometer) = fn(input: Kilometer) {
assert input == 5
}
export take_mile: fn(Mile) = fn(input: Mile) {
# should never run
assert false
}
"#)
.unwrap()
.run()
.unwrap()
}
5 changes: 5 additions & 0 deletions examples/crashes/crash.ms
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
x = "hi"

x = [1, 2, 3]

print x[2]
3 changes: 3 additions & 0 deletions examples/crashes/main.ms
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import function, Mile from other

function(Mile())
Binary file added examples/crashes/other.mmm
Binary file not shown.
7 changes: 7 additions & 0 deletions examples/crashes/other.ms
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
type Kilometer int

export class Mile {}

export function: fn(Mile) = fn(input: Mile) {
print input
}
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ fn main() -> Result<()> {
println!(
"{}",
format!(
" Exit:\n Runner thread status: {}\n Task status: {}\n Program exit: {}\n",
" Exit:\n Runner thread status: {}\n Compilation status: {}\n Program exit: {}\n",
if thread_ok { "ok" } else { "failed"},
if job_ok { "ok" } else { "failed" },
if execution_ok { "ok" } else { "failed" },
Expand Down

0 comments on commit 85deb67

Please sign in to comment.