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 { if if_boolean {
self.if_block.run(source, context) self.if_block.run(source, context)
} else { } 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()?; let if_boolean = expression.run(source, context)?.as_boolean()?;
if if_boolean { if if_boolean {
let block = self.else_if_blocks.get(index).unwrap();
return block.run(source, context); return block.run(source, context);
} }
} }

View File

@ -44,6 +44,12 @@ impl AbstractTree for Index {
fn validate(&self, _source: &str, _context: &Context) -> Result<(), ValidationError> { fn validate(&self, _source: &str, _context: &Context) -> Result<(), ValidationError> {
self.collection.validate(_source, _context)?; 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)?; self.index.validate(_source, _context)?;
Ok(()) Ok(())

View File

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

View File

@ -28,7 +28,7 @@ impl AbstractTree for Statement {
SyntaxError::expect_syntax_node("statement", node)?; SyntaxError::expect_syntax_node("statement", node)?;
let first_child = node.child(0).unwrap(); 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 { let child = if is_return {
node.child(1).unwrap() node.child(1).unwrap()
} else { } else {

View File

@ -32,7 +32,7 @@ fn range_for_loop() {
} }
#[test] #[test]
fn modify_list() { fn mutate_list() {
let result = interpret( let result = interpret(
" "
list = [] list = []
@ -52,29 +52,7 @@ fn modify_list() {
} }
#[test] #[test]
fn modify_map() { fn do_not_mutate_list_items() {
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() {
let result = interpret( let result = interpret(
" "
list = [1 2 3] list = [1 2 3]
@ -86,9 +64,9 @@ fn modify_list_values() {
assert_eq!( assert_eq!(
result, result,
Ok(Value::List(List::with_items(vec![ Ok(Value::List(List::with_items(vec![
Value::Integer(1),
Value::Integer(2), Value::Integer(2),
Value::Integer(3), Value::Integer(3),
Value::Integer(4),
]))), ]))),
); );
} }

View File

@ -2,16 +2,16 @@ use dust_lang::*;
#[test] #[test]
fn list_index() { 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] #[test]
fn map_index() { 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] #[test]

View File

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

View File

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

View File

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

File diff suppressed because it is too large Load Diff