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 divides_by_5 = count % 5 == 0
if divides_by_3 && divides_by_5 { if divides_by_3 && divides_by_5 {
output 'fizzbuzz' (output 'fizzbuzz')
} else if divides_by_3 { } else if divides_by_3 {
output 'fizz' (output 'fizz')
} else if divides_by_5 { } else if divides_by_5 {
output 'buzz' (output 'buzz')
} else { } else {
output count (output count)
} }
count += 1 count += 1

View File

@ -8,11 +8,11 @@ data = {
} }
for creature in sea_creatures { for creature in sea_creatures {
data.creatures += creature.name data:creatures += creature:name
data.total_clams += creature.clams data:total_clams += creature:clams
if creature.type == 'dolphin' { if creature:type == 'dolphin' {
data.dolphin_clams += creature.clams 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> { 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) => { Value::List(list) => {
let index = self.index.run(source, context)?.as_integer()? as usize; 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) map.variables()?.get(key).cloned().unwrap_or(Value::Empty)
} else { } else {
let value = self.index.run(source, &mut map)?; let value = self.index.run(source, context)?;
let key = value.as_string()?; let key = value.as_string()?;
map.variables()?.get(key).cloned().unwrap_or(Value::Empty) map.variables()?.get(key).cloned().unwrap_or(Value::Empty)
@ -70,7 +70,7 @@ impl AbstractTree for Index {
Ok(Value::String(item.to_string())) 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( assignment: $ => seq(
$.identifier, choice(
$.identifier,
$.index,
),
$.assignment_operator, $.assignment_operator,
$.statement, $.statement,
), ),

View File

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

View File

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

File diff suppressed because it is too large Load Diff