Fix infintite loop

This commit is contained in:
Jeff 2024-02-15 16:30:47 -05:00
parent 5e105177cf
commit edded5043d
5 changed files with 24 additions and 24 deletions

View File

@ -3,7 +3,7 @@ use serde::{Deserialize, Serialize};
use crate::{
error::{RuntimeError, SyntaxError, ValidationError},
value_node::ValueNode,
AbstractTree, As, Command, Context, Format, FunctionCall, Identifier, Index, Logic, Math, New,
AbstractTree, As, Command, Context, Format, FunctionCall, Identifier, Index, Logic, Math,
SyntaxNode, Type, Value,
};
@ -20,7 +20,6 @@ pub enum Expression {
Math(Box<Math>),
Logic(Box<Logic>),
FunctionCall(Box<FunctionCall>),
New(New),
Command(Command),
As(Box<As>),
}
@ -51,12 +50,10 @@ impl AbstractTree for Expression {
"function_call" => Expression::FunctionCall(Box::new(FunctionCall::from_syntax(
child, source, _context,
)?)),
"new" => Expression::New(New::from_syntax(child, source, _context)?),
"command" => Expression::Command(Command::from_syntax(child, source, _context)?),
_ => {
return Err(SyntaxError::UnexpectedSyntaxNode {
expected:
"value, identifier, index, math, logic, function call, new, as or command"
expected: "value, identifier, index, math, logic, function call, as or command"
.to_string(),
actual: child.kind().to_string(),
location: child.start_position(),
@ -76,7 +73,6 @@ impl AbstractTree for Expression {
Expression::Logic(logic) => logic.expected_type(_context),
Expression::FunctionCall(function_call) => function_call.expected_type(_context),
Expression::Index(index) => index.expected_type(_context),
Expression::New(new) => new.expected_type(_context),
Expression::Command(command) => command.expected_type(_context),
Expression::As(r#as) => r#as.expected_type(_context),
}
@ -90,7 +86,6 @@ impl AbstractTree for Expression {
Expression::Logic(logic) => logic.validate(_source, _context),
Expression::FunctionCall(function_call) => function_call.validate(_source, _context),
Expression::Index(index) => index.validate(_source, _context),
Expression::New(new) => new.validate(_source, _context),
Expression::Command(command) => command.validate(_source, _context),
Expression::As(r#as) => r#as.validate(_source, _context),
}
@ -104,7 +99,6 @@ impl AbstractTree for Expression {
Expression::Logic(logic) => logic.run(_source, _context),
Expression::FunctionCall(function_call) => function_call.run(_source, _context),
Expression::Index(index) => index.run(_source, _context),
Expression::New(new) => new.run(_source, _context),
Expression::Command(command) => command.run(_source, _context),
Expression::As(r#as) => r#as.run(_source, _context),
}
@ -120,7 +114,6 @@ impl Format for Expression {
Expression::Logic(logic) => logic.format(_output, _indent_level),
Expression::FunctionCall(function_call) => function_call.format(_output, _indent_level),
Expression::Index(index) => index.format(_output, _indent_level),
Expression::New(new) => new.format(_output, _indent_level),
Expression::Command(command) => command.format(_output, _indent_level),
Expression::As(r#as) => r#as.format(_output, _indent_level),
}

View File

@ -31,6 +31,10 @@ impl Identifier {
Identifier(Arc::new(key.to_string()))
}
pub fn from_raw_parts(arc: Arc<String>) -> Self {
Identifier(arc)
}
pub fn inner(&self) -> &Arc<String> {
&self.0
}

View File

@ -30,7 +30,6 @@ pub mod r#match;
pub mod match_pattern;
pub mod math;
pub mod math_operator;
pub mod new;
pub mod statement;
pub mod struct_definition;
pub mod r#type;
@ -43,8 +42,8 @@ pub use {
assignment::*, assignment_operator::*, block::*, command::*, enum_defintion::*,
enum_pattern::*, expression::*, function_call::*, function_expression::*, function_node::*,
identifier::*, if_else::*, index::*, index_assignment::IndexAssignment, index_expression::*,
logic::*, logic_operator::*, map_node::*, match_pattern::*, math::*, math_operator::*, new::*,
r#as::*, r#for::*, r#match::*, r#type::*, r#while::*, statement::*, struct_definition::*,
logic::*, logic_operator::*, map_node::*, match_pattern::*, math::*, math_operator::*, r#as::*,
r#for::*, r#match::*, r#type::*, r#while::*, statement::*, struct_definition::*,
type_definition::*, type_specification::*, value_node::*,
};
@ -120,12 +119,8 @@ impl AbstractTree for Root {
let mut value = Value::none();
for statement in &self.statements {
if let Statement::Return(inner_statement) = statement {
return inner_statement.run(source, context);
} else {
value = statement.run(source, context)?;
}
}
Ok(value)
}

View File

@ -1,4 +1,4 @@
use std::sync::OnceLock;
use std::sync::{Arc, OnceLock};
use enum_iterator::{all, Sequence};
@ -8,10 +8,10 @@ pub fn all_built_in_identifiers() -> impl Iterator<Item = BuiltInIdentifier> {
all()
}
const OPTION: OnceLock<Identifier> = OnceLock::new();
const NONE: OnceLock<Identifier> = OnceLock::new();
static OPTION: OnceLock<Identifier> = OnceLock::new();
static NONE: OnceLock<Identifier> = OnceLock::new();
#[derive(Sequence)]
#[derive(Sequence, Debug)]
pub enum BuiltInIdentifier {
Option,
None,
@ -20,8 +20,12 @@ pub enum BuiltInIdentifier {
impl BuiltInIdentifier {
pub fn get(&self) -> Identifier {
match self {
BuiltInIdentifier::Option => OPTION.get_or_init(|| Identifier::new("Option")).clone(),
BuiltInIdentifier::None => NONE.get_or_init(|| Identifier::new("None")).clone(),
BuiltInIdentifier::Option => OPTION
.get_or_init(|| Identifier::from_raw_parts(Arc::new("Option".to_string())))
.clone(),
BuiltInIdentifier::None => NONE
.get_or_init(|| Identifier::from_raw_parts(Arc::new("None".to_string())))
.clone(),
}
}
}

View File

@ -149,10 +149,14 @@ impl Interpreter {
check_for_error(root, source, &mut cursor)?;
println!("abstracting...");
let abstract_tree = Root::from_syntax(syntax_tree.root_node(), source, &self.context)?;
abstract_tree.validate(source, &self.context)?;
println!("{abstract_tree:?}");
Ok(abstract_tree)
}