Add example; Fix add assignment
This commit is contained in:
parent
9dfe5689e4
commit
8348b6ce85
6
examples/assets/seaCreatures.json
Normal file
6
examples/assets/seaCreatures.json
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
[
|
||||||
|
{ "name": "Sammy", "type": "shark", "clams": 5 },
|
||||||
|
{ "name": "Bubbles", "type": "orca", "clams": 3 },
|
||||||
|
{ "name": "Splish", "type": "dolphin", "clams": 2 },
|
||||||
|
{ "name": "Splash", "type": "dolphin", "clams": 2 }
|
||||||
|
]
|
18
examples/sea_creatures.ds
Normal file
18
examples/sea_creatures.ds
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
raw_data = (read 'examples/assets/seaCreatures.json')
|
||||||
|
sea_creatures = (from_json raw_data)
|
||||||
|
|
||||||
|
data = {
|
||||||
|
creatures = []
|
||||||
|
total_clams = 0
|
||||||
|
dolphin_clams = 0
|
||||||
|
}
|
||||||
|
|
||||||
|
for creature in sea_creatures {
|
||||||
|
data.creatures += creature.name
|
||||||
|
data.total_clams += creature.clams
|
||||||
|
if creature.type == 'dolphin' {
|
||||||
|
data.dolphin_clams += creature.clams
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
data
|
@ -1,7 +1,7 @@
|
|||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use tree_sitter::Node;
|
use tree_sitter::Node;
|
||||||
|
|
||||||
use crate::{AbstractTree, Error, Result, Value, VariableMap};
|
use crate::{value, AbstractTree, Error, Result, Value, VariableMap};
|
||||||
|
|
||||||
use super::{identifier::Identifier, statement::Statement};
|
use super::{identifier::Identifier, statement::Statement};
|
||||||
|
|
||||||
@ -51,23 +51,29 @@ impl AbstractTree for Assignment {
|
|||||||
|
|
||||||
fn run(&self, source: &str, context: &mut VariableMap) -> Result<Value> {
|
fn run(&self, source: &str, context: &mut VariableMap) -> Result<Value> {
|
||||||
let key = self.identifier.inner().clone();
|
let key = self.identifier.inner().clone();
|
||||||
let mut value = self.statement.run(source, context)?;
|
let value = self.statement.run(source, context)?;
|
||||||
|
|
||||||
match self.operator {
|
let new_value = match self.operator {
|
||||||
AssignmentOperator::PlusEqual => {
|
AssignmentOperator::PlusEqual => {
|
||||||
if let Some(previous_value) = context.get_value(&key)? {
|
if let Some(mut previous_value) = context.get_value(&key)? {
|
||||||
value += previous_value
|
previous_value += value;
|
||||||
|
previous_value
|
||||||
|
} else {
|
||||||
|
Value::Empty
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
AssignmentOperator::MinusEqual => {
|
AssignmentOperator::MinusEqual => {
|
||||||
if let Some(previous_value) = context.get_value(&key)? {
|
if let Some(mut previous_value) = context.get_value(&key)? {
|
||||||
value -= previous_value
|
previous_value -= value;
|
||||||
|
previous_value
|
||||||
|
} else {
|
||||||
|
Value::Empty
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
AssignmentOperator::Equal => {}
|
AssignmentOperator::Equal => value,
|
||||||
}
|
};
|
||||||
|
|
||||||
context.set_value(key, value)?;
|
context.set_value(key, new_value)?;
|
||||||
|
|
||||||
Ok(Value::Empty)
|
Ok(Value::Empty)
|
||||||
}
|
}
|
||||||
|
@ -321,6 +321,7 @@ impl AddAssign for Value {
|
|||||||
(Value::Float(left), Value::Float(right)) => *left += right,
|
(Value::Float(left), Value::Float(right)) => *left += right,
|
||||||
(Value::Float(left), Value::Integer(right)) => *left += right as f64,
|
(Value::Float(left), Value::Integer(right)) => *left += right as f64,
|
||||||
(Value::String(left), Value::String(right)) => *left += &right,
|
(Value::String(left), Value::String(right)) => *left += &right,
|
||||||
|
(Value::List(list), value) => list.push(value),
|
||||||
_ => {}
|
_ => {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -27,10 +27,19 @@ Dot Notation
|
|||||||
==================
|
==================
|
||||||
|
|
||||||
dust_data.0.name
|
dust_data.0.name
|
||||||
|
# Separator
|
||||||
|
creature.total_clams
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
(root
|
(root
|
||||||
|
(item
|
||||||
|
(statement
|
||||||
|
(expression
|
||||||
|
(identifier))))
|
||||||
|
(item
|
||||||
|
(statement
|
||||||
|
(comment)))
|
||||||
(item
|
(item
|
||||||
(statement
|
(statement
|
||||||
(expression
|
(expression
|
||||||
|
@ -23,11 +23,21 @@ List Declaration
|
|||||||
List Assignment
|
List Assignment
|
||||||
==================
|
==================
|
||||||
|
|
||||||
|
empty = []
|
||||||
foobar = ['foobar']
|
foobar = ['foobar']
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
(root
|
(root
|
||||||
|
(item
|
||||||
|
(statement
|
||||||
|
(assignment
|
||||||
|
(identifier)
|
||||||
|
(assignment_operator)
|
||||||
|
(statement
|
||||||
|
(expression
|
||||||
|
(value
|
||||||
|
(list)))))))
|
||||||
(item
|
(item
|
||||||
(statement
|
(statement
|
||||||
(assignment
|
(assignment
|
||||||
|
@ -40,7 +40,7 @@ module.exports = grammar({
|
|||||||
$.logic,
|
$.logic,
|
||||||
)),
|
)),
|
||||||
|
|
||||||
identifier: $ => /[a-zA-Z|_]+[.a-zA-Z0-9]*/,
|
identifier: $ => /[a-zA-Z|_]+[._a-zA-Z0-9]*/,
|
||||||
|
|
||||||
value: $ => choice(
|
value: $ => choice(
|
||||||
$.integer,
|
$.integer,
|
||||||
|
@ -147,7 +147,7 @@
|
|||||||
},
|
},
|
||||||
"identifier": {
|
"identifier": {
|
||||||
"type": "PATTERN",
|
"type": "PATTERN",
|
||||||
"value": "[a-zA-Z|_]+[.a-zA-Z0-9]*"
|
"value": "[a-zA-Z|_]+[._a-zA-Z0-9]*"
|
||||||
},
|
},
|
||||||
"value": {
|
"value": {
|
||||||
"type": "CHOICE",
|
"type": "CHOICE",
|
||||||
|
@ -1137,9 +1137,9 @@ static bool ts_lex(TSLexer *lexer, TSStateId state) {
|
|||||||
if (lookahead == '[') ADVANCE(30);
|
if (lookahead == '[') ADVANCE(30);
|
||||||
if (lookahead == ']') ADVANCE(32);
|
if (lookahead == ']') ADVANCE(32);
|
||||||
if (lookahead == '`') ADVANCE(8);
|
if (lookahead == '`') ADVANCE(8);
|
||||||
if (lookahead == 'e') ADVANCE(23);
|
if (lookahead == 'e') ADVANCE(22);
|
||||||
if (lookahead == '{') ADVANCE(36);
|
if (lookahead == '{') ADVANCE(36);
|
||||||
if (lookahead == '|') ADVANCE(21);
|
if (lookahead == '|') ADVANCE(24);
|
||||||
if (lookahead == '}') ADVANCE(37);
|
if (lookahead == '}') ADVANCE(37);
|
||||||
if (lookahead == '\t' ||
|
if (lookahead == '\t' ||
|
||||||
lookahead == '\n' ||
|
lookahead == '\n' ||
|
||||||
@ -1180,7 +1180,7 @@ static bool ts_lex(TSLexer *lexer, TSStateId state) {
|
|||||||
if (lookahead == '<') ADVANCE(33);
|
if (lookahead == '<') ADVANCE(33);
|
||||||
if (lookahead == '=') ADVANCE(7);
|
if (lookahead == '=') ADVANCE(7);
|
||||||
if (lookahead == '>') ADVANCE(35);
|
if (lookahead == '>') ADVANCE(35);
|
||||||
if (lookahead == '|') ADVANCE(21);
|
if (lookahead == '|') ADVANCE(24);
|
||||||
if (lookahead == '}') ADVANCE(37);
|
if (lookahead == '}') ADVANCE(37);
|
||||||
if (lookahead == '\t' ||
|
if (lookahead == '\t' ||
|
||||||
lookahead == '\n' ||
|
lookahead == '\n' ||
|
||||||
@ -1245,7 +1245,7 @@ static bool ts_lex(TSLexer *lexer, TSStateId state) {
|
|||||||
if (lookahead == ']') ADVANCE(32);
|
if (lookahead == ']') ADVANCE(32);
|
||||||
if (lookahead == '`') ADVANCE(8);
|
if (lookahead == '`') ADVANCE(8);
|
||||||
if (lookahead == '{') ADVANCE(36);
|
if (lookahead == '{') ADVANCE(36);
|
||||||
if (lookahead == '|') ADVANCE(21);
|
if (lookahead == '|') ADVANCE(24);
|
||||||
if (lookahead == '}') ADVANCE(37);
|
if (lookahead == '}') ADVANCE(37);
|
||||||
if (lookahead == '\t' ||
|
if (lookahead == '\t' ||
|
||||||
lookahead == '\n' ||
|
lookahead == '\n' ||
|
||||||
@ -1287,7 +1287,7 @@ static bool ts_lex(TSLexer *lexer, TSStateId state) {
|
|||||||
if (lookahead == '-') ADVANCE(12);
|
if (lookahead == '-') ADVANCE(12);
|
||||||
if (lookahead == '[') ADVANCE(30);
|
if (lookahead == '[') ADVANCE(30);
|
||||||
if (lookahead == '`') ADVANCE(8);
|
if (lookahead == '`') ADVANCE(8);
|
||||||
if (lookahead == 'e') ADVANCE(23);
|
if (lookahead == 'e') ADVANCE(22);
|
||||||
if (lookahead == '{') ADVANCE(36);
|
if (lookahead == '{') ADVANCE(36);
|
||||||
if (lookahead == '}') ADVANCE(37);
|
if (lookahead == '}') ADVANCE(37);
|
||||||
if (lookahead == '\t' ||
|
if (lookahead == '\t' ||
|
||||||
@ -1320,50 +1320,50 @@ static bool ts_lex(TSLexer *lexer, TSStateId state) {
|
|||||||
END_STATE();
|
END_STATE();
|
||||||
case 21:
|
case 21:
|
||||||
ACCEPT_TOKEN(sym_identifier);
|
ACCEPT_TOKEN(sym_identifier);
|
||||||
if (lookahead == '_') ADVANCE(25);
|
if (lookahead == 'e') ADVANCE(57);
|
||||||
if (lookahead == '|') ADVANCE(51);
|
if (lookahead == '|') ADVANCE(25);
|
||||||
if (lookahead == '.' ||
|
if (lookahead == '.' ||
|
||||||
('0' <= lookahead && lookahead <= '9')) ADVANCE(26);
|
('0' <= lookahead && lookahead <= '9')) ADVANCE(26);
|
||||||
if (('A' <= lookahead && lookahead <= 'Z') ||
|
if (('A' <= lookahead && lookahead <= 'Z') ||
|
||||||
|
lookahead == '_' ||
|
||||||
('a' <= lookahead && lookahead <= 'z')) ADVANCE(25);
|
('a' <= lookahead && lookahead <= 'z')) ADVANCE(25);
|
||||||
END_STATE();
|
END_STATE();
|
||||||
case 22:
|
case 22:
|
||||||
ACCEPT_TOKEN(sym_identifier);
|
ACCEPT_TOKEN(sym_identifier);
|
||||||
if (lookahead == 'e') ADVANCE(57);
|
if (lookahead == 'l') ADVANCE(23);
|
||||||
if (lookahead == '_' ||
|
if (lookahead == '|') ADVANCE(25);
|
||||||
lookahead == '|') ADVANCE(25);
|
|
||||||
if (lookahead == '.' ||
|
if (lookahead == '.' ||
|
||||||
('0' <= lookahead && lookahead <= '9')) ADVANCE(26);
|
('0' <= lookahead && lookahead <= '9')) ADVANCE(26);
|
||||||
if (('A' <= lookahead && lookahead <= 'Z') ||
|
if (('A' <= lookahead && lookahead <= 'Z') ||
|
||||||
|
lookahead == '_' ||
|
||||||
('a' <= lookahead && lookahead <= 'z')) ADVANCE(25);
|
('a' <= lookahead && lookahead <= 'z')) ADVANCE(25);
|
||||||
END_STATE();
|
END_STATE();
|
||||||
case 23:
|
case 23:
|
||||||
ACCEPT_TOKEN(sym_identifier);
|
ACCEPT_TOKEN(sym_identifier);
|
||||||
if (lookahead == 'l') ADVANCE(24);
|
if (lookahead == 's') ADVANCE(21);
|
||||||
if (lookahead == '_' ||
|
if (lookahead == '|') ADVANCE(25);
|
||||||
lookahead == '|') ADVANCE(25);
|
|
||||||
if (lookahead == '.' ||
|
if (lookahead == '.' ||
|
||||||
('0' <= lookahead && lookahead <= '9')) ADVANCE(26);
|
('0' <= lookahead && lookahead <= '9')) ADVANCE(26);
|
||||||
if (('A' <= lookahead && lookahead <= 'Z') ||
|
if (('A' <= lookahead && lookahead <= 'Z') ||
|
||||||
|
lookahead == '_' ||
|
||||||
('a' <= lookahead && lookahead <= 'z')) ADVANCE(25);
|
('a' <= lookahead && lookahead <= 'z')) ADVANCE(25);
|
||||||
END_STATE();
|
END_STATE();
|
||||||
case 24:
|
case 24:
|
||||||
ACCEPT_TOKEN(sym_identifier);
|
ACCEPT_TOKEN(sym_identifier);
|
||||||
if (lookahead == 's') ADVANCE(22);
|
if (lookahead == '|') ADVANCE(51);
|
||||||
if (lookahead == '_' ||
|
|
||||||
lookahead == '|') ADVANCE(25);
|
|
||||||
if (lookahead == '.' ||
|
if (lookahead == '.' ||
|
||||||
('0' <= lookahead && lookahead <= '9')) ADVANCE(26);
|
('0' <= lookahead && lookahead <= '9')) ADVANCE(26);
|
||||||
if (('A' <= lookahead && lookahead <= 'Z') ||
|
if (('A' <= lookahead && lookahead <= 'Z') ||
|
||||||
|
lookahead == '_' ||
|
||||||
('a' <= lookahead && lookahead <= 'z')) ADVANCE(25);
|
('a' <= lookahead && lookahead <= 'z')) ADVANCE(25);
|
||||||
END_STATE();
|
END_STATE();
|
||||||
case 25:
|
case 25:
|
||||||
ACCEPT_TOKEN(sym_identifier);
|
ACCEPT_TOKEN(sym_identifier);
|
||||||
if (lookahead == '_' ||
|
if (lookahead == '|') ADVANCE(25);
|
||||||
lookahead == '|') ADVANCE(25);
|
|
||||||
if (lookahead == '.' ||
|
if (lookahead == '.' ||
|
||||||
('0' <= lookahead && lookahead <= '9')) ADVANCE(26);
|
('0' <= lookahead && lookahead <= '9')) ADVANCE(26);
|
||||||
if (('A' <= lookahead && lookahead <= 'Z') ||
|
if (('A' <= lookahead && lookahead <= 'Z') ||
|
||||||
|
lookahead == '_' ||
|
||||||
('a' <= lookahead && lookahead <= 'z')) ADVANCE(25);
|
('a' <= lookahead && lookahead <= 'z')) ADVANCE(25);
|
||||||
END_STATE();
|
END_STATE();
|
||||||
case 26:
|
case 26:
|
||||||
@ -1371,6 +1371,7 @@ static bool ts_lex(TSLexer *lexer, TSStateId state) {
|
|||||||
if (lookahead == '.' ||
|
if (lookahead == '.' ||
|
||||||
('0' <= lookahead && lookahead <= '9') ||
|
('0' <= lookahead && lookahead <= '9') ||
|
||||||
('A' <= lookahead && lookahead <= 'Z') ||
|
('A' <= lookahead && lookahead <= 'Z') ||
|
||||||
|
lookahead == '_' ||
|
||||||
('a' <= lookahead && lookahead <= 'z')) ADVANCE(26);
|
('a' <= lookahead && lookahead <= 'z')) ADVANCE(26);
|
||||||
END_STATE();
|
END_STATE();
|
||||||
case 27:
|
case 27:
|
||||||
@ -1457,11 +1458,11 @@ static bool ts_lex(TSLexer *lexer, TSStateId state) {
|
|||||||
END_STATE();
|
END_STATE();
|
||||||
case 51:
|
case 51:
|
||||||
ACCEPT_TOKEN(anon_sym_PIPE_PIPE);
|
ACCEPT_TOKEN(anon_sym_PIPE_PIPE);
|
||||||
if (lookahead == '_' ||
|
if (lookahead == '|') ADVANCE(25);
|
||||||
lookahead == '|') ADVANCE(25);
|
|
||||||
if (lookahead == '.' ||
|
if (lookahead == '.' ||
|
||||||
('0' <= lookahead && lookahead <= '9')) ADVANCE(26);
|
('0' <= lookahead && lookahead <= '9')) ADVANCE(26);
|
||||||
if (('A' <= lookahead && lookahead <= 'Z') ||
|
if (('A' <= lookahead && lookahead <= 'Z') ||
|
||||||
|
lookahead == '_' ||
|
||||||
('a' <= lookahead && lookahead <= 'z')) ADVANCE(25);
|
('a' <= lookahead && lookahead <= 'z')) ADVANCE(25);
|
||||||
END_STATE();
|
END_STATE();
|
||||||
case 52:
|
case 52:
|
||||||
@ -1482,11 +1483,11 @@ static bool ts_lex(TSLexer *lexer, TSStateId state) {
|
|||||||
case 57:
|
case 57:
|
||||||
ACCEPT_TOKEN(anon_sym_else);
|
ACCEPT_TOKEN(anon_sym_else);
|
||||||
if (lookahead == ' ') ADVANCE(10);
|
if (lookahead == ' ') ADVANCE(10);
|
||||||
if (lookahead == '_' ||
|
if (lookahead == '|') ADVANCE(25);
|
||||||
lookahead == '|') ADVANCE(25);
|
|
||||||
if (lookahead == '.' ||
|
if (lookahead == '.' ||
|
||||||
('0' <= lookahead && lookahead <= '9')) ADVANCE(26);
|
('0' <= lookahead && lookahead <= '9')) ADVANCE(26);
|
||||||
if (('A' <= lookahead && lookahead <= 'Z') ||
|
if (('A' <= lookahead && lookahead <= 'Z') ||
|
||||||
|
lookahead == '_' ||
|
||||||
('a' <= lookahead && lookahead <= 'z')) ADVANCE(25);
|
('a' <= lookahead && lookahead <= 'z')) ADVANCE(25);
|
||||||
END_STATE();
|
END_STATE();
|
||||||
default:
|
default:
|
||||||
|
Loading…
Reference in New Issue
Block a user