Add index assignment syntax

This commit is contained in:
Jeff 2023-11-14 20:00:57 -05:00
parent 9ec06997c5
commit 0b14ab5832
8 changed files with 14206 additions and 9857 deletions

View File

@ -5,13 +5,13 @@ while count <= 15 {
divides_by_5 = count % 5 == 0
if divides_by_3 && divides_by_5 {
output 'fizzbuzz'
(output 'fizzbuzz')
} else if divides_by_3 {
output 'fizz'
(output 'fizz')
} else if divides_by_5 {
output 'buzz'
(output 'buzz')
} else {
output count
(output count)
}
count += 1

View File

@ -8,11 +8,11 @@ data = {
}
for creature in sea_creatures {
data.creatures += creature.name
data.total_clams += creature.clams
data:creatures += creature:name
data:total_clams += creature:clams
if creature.type == 'dolphin' {
data.dolphin_clams += creature.clams
if creature:type == 'dolphin' {
data:dolphin_clams += creature:clams
}
}

View File

@ -33,9 +33,9 @@ impl AbstractTree for Index {
}
fn run(&self, source: &str, context: &mut Map) -> Result<Value> {
let value = self.collection.run(source, context)?;
let collection = self.collection.run(source, context)?;
match value {
match collection {
Value::List(list) => {
let index = self.index.run(source, context)?.as_integer()? as usize;
@ -56,7 +56,7 @@ impl AbstractTree for Index {
map.variables()?.get(key).cloned().unwrap_or(Value::Empty)
} else {
let value = self.index.run(source, &mut map)?;
let value = self.index.run(source, context)?;
let key = value.as_string()?;
map.variables()?.get(key).cloned().unwrap_or(Value::Empty)
@ -70,7 +70,7 @@ impl AbstractTree for Index {
Ok(Value::String(item.to_string()))
}
_ => Err(Error::ExpectedCollection { actual: value }),
_ => Err(Error::ExpectedCollection { actual: collection }),
}
}
}

View File

@ -0,0 +1,61 @@
================================================================================
Simple Assignment
================================================================================
x = y
--------------------------------------------------------------------------------
(root
(statement
(assignment
(identifier)
(assignment_operator)
(statement
(expression
(identifier))))))
================================================================================
Map Item Assignment
================================================================================
x:y = 1
--------------------------------------------------------------------------------
(root
(statement
(assignment
(index
(expression
(identifier))
(expression
(identifier)))
(assignment_operator)
(statement
(expression
(value
(integer)))))))
================================================================================
List Item Assignment
================================================================================
x:9 = 'foobar'
--------------------------------------------------------------------------------
(root
(statement
(assignment
(index
(expression
(identifier))
(expression
(value
(integer))))
(assignment_operator)
(statement
(expression
(value
(string)))))))

View File

@ -153,7 +153,10 @@ module.exports = grammar({
),
assignment: $ => seq(
$.identifier,
choice(
$.identifier,
$.index,
),
$.assignment_operator,
$.statement,
),

View File

@ -708,8 +708,17 @@
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "identifier"
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "identifier"
},
{
"type": "SYMBOL",
"name": "index"
}
]
},
{
"type": "SYMBOL",

View File

@ -15,6 +15,10 @@
"type": "identifier",
"named": true
},
{
"type": "index",
"named": true
},
{
"type": "statement",
"named": true

File diff suppressed because it is too large Load Diff