1
0

Clean up examples; Implement type tool

This commit is contained in:
Jeff 2023-10-22 14:27:18 -04:00
parent fa6af4026b
commit b64ebbbd18
10 changed files with 724 additions and 807 deletions

View File

@ -10,12 +10,12 @@ take_turn = function <current_room opponent_card> {
remove_card = function <opponent_card> { remove_card = function <opponent_card> {
for card_list in cards { for card_list in cards {
remove card from cards { removed = remove card from card_list {
card == opponent_card card == opponent_card
} }
} }
if type of removed_card == 'empty' { if removed == 'empty' {
(output 'Card not found.') (output 'Card not found.')
} }
} }

View File

@ -1,114 +0,0 @@
# transform
list = (1, 2, 3);
test = transform(list, 'input + 1');
assert_equal((2, 3, 4), test);
# string
test = string(42);
assert_equal("42", test);
test = string(42.42);
assert_equal("42.42", test);
test = string(false);
assert_equal("false", test);
# create_table
table = create_table(
("text", "num"),
(
("foo", 1),
("bar", 2)
)
);
# rows
test = rows(table);
assert_equal(
(
("foo", 1),
("bar", 2)
),
test
);
# insert
test = insert(
table,
(
("foo", 1),
("bar", 2)
)
);
assert_equal(
create_table(
("text", "num"),
(
("foo", 1),
("bar", 2),
("foo", 1),
("bar", 2)
)
),
test
);
# select
table = create_table(
("text", "number", "bool"),
(
("a", 1, true),
("b", 2, true),
("a", 3, true)
)
);
test_table = create_table(
("text", "bool"),
(
("a", true),
("b", true),
("a", true)
)
);
assert_equal(select(table, ("text", "bool")), test_table);
test_table = create_table(
("text", "number", "bool"),
(
("a", 1, true),
("a", 3, true)
)
);
assert_equal(where(table, 'text == "a"'), test_table);
# count
table = create_table(
("text", "number", "bool"),
(
("a", 1, true),
("b", 2, true),
("a", 3, true)
)
);
test = count(table);
assert_equal(3, test);
test = count("123");
assert_equal(3, test);
test = count(1, 2, 3);
assert_equal(3, test);
map.x.z.y = 1;
test = count(map);
assert_equal(1, test);

View File

@ -1,4 +0,0 @@
dob = from_toml("1979-05-27T07:32:00-08:00");
toml = to_toml(dob);
assert_equal(toml, "1979-05-27T07:32:00-08:00");

View File

@ -1,7 +1,7 @@
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use tree_sitter::Node; use tree_sitter::Node;
use crate::{AbstractTree, Expression, Identifier, Item, Result, Value, VariableMap}; use crate::{AbstractTree, Expression, Identifier, Result, Value, VariableMap};
#[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)] #[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)]
pub struct Insert { pub struct Insert {

View File

@ -8,7 +8,7 @@ use std::{
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use tree_sitter::Node; use tree_sitter::Node;
use crate::{AbstractTree, Error, Expression, Result, Table, Value, VariableMap}; use crate::{AbstractTree, Error, Expression, Result, Table, Value, ValueType, VariableMap};
#[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)] #[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)]
pub enum Tool { pub enum Tool {
@ -19,6 +19,7 @@ pub enum Tool {
Length(Expression), Length(Expression),
Output(Vec<Expression>), Output(Vec<Expression>),
OutputError(Vec<Expression>), OutputError(Vec<Expression>),
Type(Expression),
// Filesystem // Filesystem
Append(Vec<Expression>), Append(Vec<Expression>),
@ -99,6 +100,12 @@ impl AbstractTree for Tool {
Tool::OutputError(expressions) Tool::OutputError(expressions)
} }
"type" => {
let expression_node = node.child(2).unwrap();
let expression = Expression::from_syntax_node(source, expression_node)?;
Tool::Type(expression)
}
"append" => { "append" => {
let expressions = parse_expressions(source, node)?; let expressions = parse_expressions(source, node)?;
@ -264,6 +271,18 @@ impl AbstractTree for Tool {
Ok(Value::Empty) Ok(Value::Empty)
} }
Tool::Type(expression) => {
let run_expression = expression.run(source, context);
let value_type = if let Ok(value) = run_expression {
value.value_type()
} else if let Err(Error::VariableIdentifierNotFound(_)) = run_expression {
ValueType::Empty
} else {
return run_expression;
};
Ok(Value::String(value_type.to_string()))
}
Tool::Append(expressions) => { Tool::Append(expressions) => {
let path_value = expressions[0].run(source, context)?; let path_value = expressions[0].run(source, context)?;
let path = path_value.as_string()?; let path = path_value.as_string()?;

View File

@ -9,20 +9,6 @@ fn clue_solver() {
evaluate(&file_contents).unwrap(); evaluate(&file_contents).unwrap();
} }
#[test]
fn collections() {
let file_contents = read_to_string("examples/collections.ds").unwrap();
evaluate(&file_contents).unwrap();
}
#[test]
fn data_formats() {
let file_contents = read_to_string("examples/data_formats.ds").unwrap();
evaluate(&file_contents).unwrap();
}
#[test] #[test]
fn fetch() { fn fetch() {
let file_contents = read_to_string("examples/fetch.ds").unwrap(); let file_contents = read_to_string("examples/fetch.ds").unwrap();

View File

@ -266,6 +266,7 @@ module.exports = grammar({
'length', 'length',
'output', 'output',
'output_error', 'output_error',
'type',
// Filesystem // Filesystem
'append', 'append',

View File

@ -1090,6 +1090,10 @@
"type": "STRING", "type": "STRING",
"value": "output_error" "value": "output_error"
}, },
{
"type": "STRING",
"value": "type"
},
{ {
"type": "STRING", "type": "STRING",
"value": "append" "value": "append"

View File

@ -865,6 +865,10 @@
"type": "true", "type": "true",
"named": false "named": false
}, },
{
"type": "type",
"named": false
},
{ {
"type": "while", "type": "while",
"named": false "named": false

File diff suppressed because it is too large Load Diff