1
0

Fix type check bug

This commit is contained in:
Jeff 2023-12-29 14:35:52 -05:00
parent 93ba04d35c
commit 37a9a37c72
3 changed files with 26 additions and 18 deletions

View File

@ -28,17 +28,17 @@ impl AbstractTree for Assignment {
let identifier = Identifier::from_syntax_node(source, identifier_node, context)?; let identifier = Identifier::from_syntax_node(source, identifier_node, context)?;
let identifier_type = identifier.expected_type(context)?; let identifier_type = identifier.expected_type(context)?;
let type_node = node.child(1); let type_node = node.child(1).unwrap();
let type_definition = if let Some(type_node) = type_node { let type_definition = if type_node.kind() == "type_definition" {
if type_node.kind() == "type_definition" { Some(TypeDefinition::from_syntax_node(
Some(TypeDefinition::from_syntax_node( source, type_node, context,
source, type_node, context, )?)
)?) } else {
if let Some((_, r#type)) = context.variables()?.get(identifier.inner()) {
Some(TypeDefinition::new(r#type.clone()))
} else { } else {
None None
} }
} else {
None
}; };
let operator_node = node.child(child_count - 2).unwrap().child(0).unwrap(); let operator_node = node.child(child_count - 2).unwrap().child(0).unwrap();
@ -87,6 +87,8 @@ impl AbstractTree for Assignment {
AssignmentOperator::Equal => {} AssignmentOperator::Equal => {}
AssignmentOperator::PlusEqual => { AssignmentOperator::PlusEqual => {
if let Type::List(item_type) = identifier_type { if let Type::List(item_type) = identifier_type {
println!("{item_type} {statement_type}");
item_type item_type
.check(&statement_type) .check(&statement_type)
.map_err(|error| error.at_node(statement_node, source))?; .map_err(|error| error.at_node(statement_node, source))?;

View File

@ -4,7 +4,7 @@
//! functions or by constructing your own Evaluator. //! functions or by constructing your own Evaluator.
use tree_sitter::{Parser, Tree as TSTree}; use tree_sitter::{Parser, Tree as TSTree};
use crate::{language, AbstractTree, Map, Result, Root, Value}; use crate::{language, AbstractTree, Error, Map, Result, Root, Value};
/// Interpret the given source code. /// Interpret the given source code.
/// ///
@ -54,7 +54,7 @@ pub fn interpret_with_context(source: &str, context: &mut Map) -> Result<Value>
/// A source code interpreter for the Dust language. /// A source code interpreter for the Dust language.
pub struct Interpreter<'c, 's> { pub struct Interpreter<'c, 's> {
_parser: Parser, parser: Parser,
context: &'c mut Map, context: &'c mut Map,
source: &'s str, source: &'s str,
syntax_tree: Option<TSTree>, syntax_tree: Option<TSTree>,
@ -68,7 +68,7 @@ impl<'c, 's> Interpreter<'c, 's> {
parser.set_language(language())?; parser.set_language(language())?;
Ok(Interpreter { Ok(Interpreter {
_parser: parser, parser,
context, context,
source, source,
syntax_tree: None, syntax_tree: None,
@ -80,8 +80,12 @@ impl<'c, 's> Interpreter<'c, 's> {
self.source = source; self.source = source;
} }
pub fn parse_only(&mut self) {
self.syntax_tree = self.parser.parse(self.source, self.syntax_tree.as_ref());
}
pub fn run(&mut self) -> Result<Value> { pub fn run(&mut self) -> Result<Value> {
self.syntax_tree = self._parser.parse(self.source, self.syntax_tree.as_ref()); self.syntax_tree = self.parser.parse(self.source, self.syntax_tree.as_ref());
self.abstract_tree = if let Some(syntax_tree) = &self.syntax_tree { self.abstract_tree = if let Some(syntax_tree) = &self.syntax_tree {
Some(Root::from_syntax_node( Some(Root::from_syntax_node(
self.source, self.source,
@ -89,7 +93,7 @@ impl<'c, 's> Interpreter<'c, 's> {
&self.context, &self.context,
)?) )?)
} else { } else {
return Err(crate::Error::ParserCancelled); return Err(Error::ParserCancelled);
}; };
if let Some(abstract_tree) = &self.abstract_tree { if let Some(abstract_tree) = &self.abstract_tree {
@ -99,11 +103,11 @@ impl<'c, 's> Interpreter<'c, 's> {
} }
} }
pub fn syntax_tree(&self) -> Option<String> { pub fn syntax_tree(&self) -> Result<String> {
if let Some(syntax_tree) = &self.syntax_tree { if let Some(syntax_tree) = &self.syntax_tree {
Some(syntax_tree.root_node().to_sexp()) Ok(syntax_tree.root_node().to_sexp())
} else { } else {
None Err(Error::ParserCancelled)
} }
} }
} }

View File

@ -76,7 +76,7 @@ fn main() {
let mut parser = TSParser::new(); let mut parser = TSParser::new();
parser.set_language(language()).unwrap(); parser.set_language(language()).unwrap();
let mut interpreter = Interpreter::parse(parser, &mut context, &source).unwrap(); let mut interpreter = Interpreter::new(&mut context, &source).unwrap();
if args.interactive { if args.interactive {
loop { loop {
@ -87,7 +87,9 @@ fn main() {
} }
if args.show_syntax_tree { if args.show_syntax_tree {
println!("{}", interpreter.syntax_tree()); interpreter.parse_only();
println!("{}", interpreter.syntax_tree().unwrap());
} }
let eval_result = interpreter.run(); let eval_result = interpreter.run();