Skip to content

Commit

Permalink
Make 'declare' syntax require ::, so that we can parse an expression …
Browse files Browse the repository at this point in the history
…for the target. This also improves error reporting in this place.
  • Loading branch information
Ivorforce committed Apr 26, 2024
1 parent 1e91e7d commit 7f6be14
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 7 deletions.
2 changes: 1 addition & 1 deletion monoteny/core/strings.monoteny
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use!(
-- The easiest way to format an object: It formats itself!
def format(object '$ToString) -> String :: object.to_string();

declare String is ToString {
declare String is ToString :: {
def (self 'Self).to_string() -> String :: self;
};

Expand Down
2 changes: 1 addition & 1 deletion src/interpreter/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ mod tests {

let module = runtime.load_file_as_module(&PathBuf::from(path), module_name("main"))?;

let entry_function = interpreter::run::get_main_function(&module)?;
let entry_function = interpreter::run::get_main_function(&module)?.unwrap();

// TODO Should gather all used functions and compile them
let compiled = compile_deep(&mut runtime, entry_function)?;
Expand Down
2 changes: 1 addition & 1 deletion src/monoteny_grammar.lalrpop
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ Trait: TraitDefinition = {
}

Conformance: TraitConformanceDeclaration = {
"declare" <declared_for: Expression> "is" <declared: Identifier> "{" <block: Box<Block>> "}" => TraitConformanceDeclaration { <> },
"declare" <declared_for: Expression> "is" <declared: Expression> "::" "{" <block: Box<Block>> "}" => TraitConformanceDeclaration { <> },
}

// =============================== Statement =====================================
Expand Down
2 changes: 1 addition & 1 deletion src/parser/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ pub struct TraitDefinition {
#[derive(Eq, PartialEq, Clone)]
pub struct TraitConformanceDeclaration {
pub declared_for: Expression,
pub declared: String,
pub declared: Expression,
pub block: Box<Block>,
}

Expand Down
9 changes: 8 additions & 1 deletion src/resolver/global.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,14 @@ impl <'a> GlobalResolver<'a> {

let mut type_factory = TypeFactory::new(&self.global_variables, &mut self.runtime);
let self_type = type_factory.resolve_type(&syntax.declared_for, true)?;
let declared = type_factory.resolve_trait(&syntax.declared)?;
let declared_type = type_factory.resolve_type(&syntax.declared, false)?;
let TypeUnit::Struct(declared) = &declared_type.unit else {
panic!("Somehow, the resolved type wasn't a struct.")
};
if !declared_type.arguments.is_empty() {
return Err(RuntimeError::error("Conformance cannot be declared with bindings for now.").to_array());
}

if declared.generics.keys().collect_vec() != vec!["Self"] {
// Requires 1) parsing generics that the programmer binds
// and 2) inserting new generics for each that isn't explicitly bound
Expand Down
4 changes: 2 additions & 2 deletions test-code/traits/conformance.monoteny
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ trait Animal {
trait Cat {};

-- Declare every Cat subtype is Animal.
declare $Cat is Animal {
declare $Cat is Animal :: {
def (self 'Self).talk() -> String :: "Meow";
};

trait Dog {};

-- Declare just the Dog struct is Animal.
declare Dog is Animal {
declare Dog is Animal :: {
def (self 'Self).talk() -> String :: "Bark";
};

Expand Down

0 comments on commit 7f6be14

Please sign in to comment.