1
0

Partially fix indexes break/return statements

This commit is contained in:
Jeff 2024-02-18 05:36:51 -05:00
parent 5450f00174
commit 4afc8face8
10 changed files with 17904 additions and 17704 deletions

View File

@ -111,14 +111,15 @@ impl AbstractTree for IfElse {
if if_boolean {
self.if_block.run(source, context)
} else {
let expressions = &self.else_if_expressions;
let else_ifs = self
.else_if_expressions
.iter()
.zip(self.else_if_blocks.iter());
for (index, expression) in expressions.iter().enumerate() {
for (expression, block) in else_ifs {
let if_boolean = expression.run(source, context)?.as_boolean()?;
if if_boolean {
let block = self.else_if_blocks.get(index).unwrap();
return block.run(source, context);
}
}

View File

@ -44,6 +44,12 @@ impl AbstractTree for Index {
fn validate(&self, _source: &str, _context: &Context) -> Result<(), ValidationError> {
self.collection.validate(_source, _context)?;
let collection_type = self.collection.expected_type(_context)?;
let index_type = self.index.expected_type(_context)?;
if let (Type::Map, Type::String) = (collection_type, index_type) {}
self.index.validate(_source, _context)?;
Ok(())

View File

@ -63,6 +63,8 @@ impl AbstractTree for MapNode {
fn validate(&self, _source: &str, context: &Context) -> Result<(), ValidationError> {
for (_key, (statement, r#type)) in &self.properties {
statement.validate(_source, context)?;
if let Some(expected) = r#type {
let actual = statement.expected_type(context)?;

View File

@ -28,7 +28,7 @@ impl AbstractTree for Statement {
SyntaxError::expect_syntax_node("statement", node)?;
let first_child = node.child(0).unwrap();
let mut is_return = first_child.kind() == "return";
let mut is_return = first_child.kind() == "return" || first_child.kind() == "break";
let child = if is_return {
node.child(1).unwrap()
} else {

View File

@ -32,7 +32,7 @@ fn range_for_loop() {
}
#[test]
fn modify_list() {
fn mutate_list() {
let result = interpret(
"
list = []
@ -52,29 +52,7 @@ fn modify_list() {
}
#[test]
fn modify_map() {
let result = interpret(
"
map = {}
for i in [['x', 1] ['y', 2]] {
map:(i:0) = i:1
}
map
",
);
let mut map = Map::new();
map.set(Identifier::new("x"), Value::Integer(1));
map.set(Identifier::new("y"), Value::Integer(2));
assert_eq!(Ok(Value::Map(map)), result);
}
#[test]
fn modify_list_values() {
fn do_not_mutate_list_items() {
let result = interpret(
"
list = [1 2 3]
@ -86,9 +64,9 @@ fn modify_list_values() {
assert_eq!(
result,
Ok(Value::List(List::with_items(vec![
Value::Integer(1),
Value::Integer(2),
Value::Integer(3),
Value::Integer(4),
]))),
);
}

View File

@ -2,16 +2,16 @@ use dust_lang::*;
#[test]
fn list_index() {
let test = interpret("x = [1 [2] 3] x:1:0").unwrap();
let test = interpret("x = [1 [2] 3] x:1:0");
assert_eq!(Value::Integer(2), test);
assert_eq!(Ok(Value::Integer(2)), test);
}
#[test]
fn map_index() {
let test = interpret("x = {y = {z = 2}} x:y:z").unwrap();
let test = interpret("x = {y = {z = 2}} x:y:z");
assert_eq!(Value::Integer(2), test);
assert_eq!(Ok(Value::Integer(2)), test);
}
#[test]

View File

@ -14,7 +14,9 @@ module.exports = grammar({
statement: $ =>
prec.left(
seq(
optional('return'),
optional(
choice('return', 'break'),
),
$.statement_kind,
optional(';'),
),

View File

@ -27,8 +27,17 @@
"type": "CHOICE",
"members": [
{
"type": "STRING",
"value": "return"
"type": "CHOICE",
"members": [
{
"type": "STRING",
"value": "return"
},
{
"type": "STRING",
"value": "break"
}
]
},
{
"type": "BLANK"

View File

@ -970,6 +970,10 @@
"type": "bool",
"named": false
},
{
"type": "break",
"named": false
},
{
"type": "collection",
"named": false

File diff suppressed because it is too large Load Diff