Remove option value type and built-in value syntax

This commit is contained in:
Jeff 2024-02-15 02:02:48 -05:00
parent 4c68bc0260
commit ed1f139595
18 changed files with 25798 additions and 31316 deletions

View File

@ -83,11 +83,7 @@ impl Format for IndexExpression {
fn format(&self, output: &mut String, indent_level: u8) {
match self {
IndexExpression::Value(value_node) => {
if let ValueNode::BuiltInValue(built_in_value) = value_node {
output.push_str(built_in_value.name());
} else {
value_node.format(output, indent_level);
}
value_node.format(output, indent_level);
}
IndexExpression::Identifier(identifier) => identifier.format(output, indent_level),
IndexExpression::FunctionCall(function_call) => {

View File

@ -10,7 +10,6 @@ pub mod r#as;
pub mod assignment;
pub mod assignment_operator;
pub mod block;
pub mod built_in_value;
pub mod command;
pub mod enum_defintion;
pub mod expression;
@ -39,12 +38,12 @@ pub mod value_node;
pub mod r#while;
pub use {
assignment::*, assignment_operator::*, block::*, built_in_value::*, command::*,
enum_defintion::*, expression::*, function_call::*, function_expression::*, function_node::*,
identifier::*, if_else::*, index::*, index_assignment::IndexAssignment, index_expression::*,
logic::*, logic_operator::*, map_node::*, math::*, math_operator::*, new::*, r#as::*, r#for::*,
r#match::*, r#type::*, r#while::*, statement::*, struct_definition::*, type_definition::*,
type_specification::*, value_node::*,
assignment::*, assignment_operator::*, block::*, command::*, enum_defintion::*, expression::*,
function_call::*, function_expression::*, function_node::*, identifier::*, if_else::*,
index::*, index_assignment::IndexAssignment, index_expression::*, logic::*, logic_operator::*,
map_node::*, math::*, math_operator::*, new::*, r#as::*, r#for::*, r#match::*, r#type::*,
r#while::*, statement::*, struct_definition::*, type_definition::*, type_specification::*,
value_node::*,
};
use serde::{Deserialize, Serialize};

View File

@ -5,7 +5,7 @@ use tree_sitter::Node as SyntaxNode;
use crate::{
error::{RuntimeError, SyntaxError, ValidationError},
AbstractTree, BuiltInValue, Context, Expression, Format, Function, FunctionNode,
AbstractTree, Context, Expression, Format, Function, FunctionNode,
Identifier, List, Type, Value, TypeDefinition, MapNode,
};
@ -19,7 +19,6 @@ pub enum ValueNode {
List(Vec<Expression>),
Option(Option<Box<Expression>>),
Map(MapNode),
BuiltInValue(BuiltInValue),
Range(RangeInclusive<i64>),
Struct {
name: Identifier,
@ -81,15 +80,6 @@ impl AbstractTree for ValueNode {
ValueNode::Option(Some(Box::new(expression)))
}
}
"built_in_value" => {
let built_in_value_node = child.child(0).unwrap();
ValueNode::BuiltInValue(BuiltInValue::from_syntax(
built_in_value_node,
source,
context,
)?)
}
"range" => {
let mut split = source[child.byte_range()].split("..");
let start = split.next().unwrap().parse().unwrap();
@ -176,7 +166,6 @@ impl AbstractTree for ValueNode {
}
}
ValueNode::Map(_) => Type::Map,
ValueNode::BuiltInValue(built_in_value) => built_in_value.expected_type(context)?,
ValueNode::Struct { name, .. } => {
Type::Custom(name.clone())
}
@ -233,7 +222,6 @@ impl AbstractTree for ValueNode {
Value::Option(option_value)
}
ValueNode::Map(map_node) => map_node.run(source, context)?,
ValueNode::BuiltInValue(built_in_value) => built_in_value.run(source, context)?,
ValueNode::Range(range) => Value::Range(range.clone()),
ValueNode::Struct { name, properties } => {
let instance = if let Some(definition) = context.get_definition(name.inner())? {
@ -312,7 +300,6 @@ impl Format for ValueNode {
}
}
ValueNode::Map(map_node) => map_node.format(output, indent_level),
ValueNode::BuiltInValue(built_in_value) => built_in_value.format(output, indent_level),
ValueNode::Struct { name, properties } => {
name.format(output, indent_level);
properties.format(output, indent_level);
@ -342,8 +329,6 @@ impl Ord for ValueNode {
(ValueNode::Option(_), _) => Ordering::Greater,
(ValueNode::Map(left), ValueNode::Map(right)) => left.cmp(right),
(ValueNode::Map(_), _) => Ordering::Greater,
(ValueNode::BuiltInValue(left), ValueNode::BuiltInValue(right)) => left.cmp(right),
(ValueNode::BuiltInValue(_), _) => Ordering::Greater,
(ValueNode::Struct{ name: left_name, properties: left_properties }, ValueNode::Struct {name: right_name, properties: right_properties} ) => {
let name_cmp = left_name.cmp(right_name);

View File

@ -16,7 +16,7 @@ static RANDOM: OnceLock<Value> = OnceLock::new();
static STRING: OnceLock<Value> = OnceLock::new();
/// Returns the entire built-in value API.
pub fn built_in_values() -> impl Iterator<Item = BuiltInValue> {
pub fn all_built_in_values() -> impl Iterator<Item = BuiltInValue> {
all()
}
@ -212,11 +212,11 @@ impl Format for BuiltInValue {
#[cfg(test)]
mod tests {
use crate::built_in_values;
use super::all_built_in_values;
#[test]
fn check_built_in_types() {
for built_in_value in built_in_values() {
for built_in_value in all_built_in_values() {
let expected = built_in_value.r#type();
let actual = built_in_value.get().r#type();

View File

@ -5,8 +5,8 @@ use std::{
};
use crate::{
built_in_type_definitions::all_built_in_type_definitions, error::rw_lock_error::RwLockError,
Type, TypeDefinition, Value,
built_in_type_definitions::all_built_in_type_definitions, built_in_values::all_built_in_values,
error::rw_lock_error::RwLockError, Type, TypeDefinition, Value,
};
#[derive(Clone, Debug)]
@ -60,6 +60,12 @@ impl Context {
}
}
for built_in_value in all_built_in_values() {
if key == built_in_value.name() {
return Ok(Some(built_in_value.get().clone()));
}
}
Ok(None)
}

View File

@ -18,6 +18,7 @@ pub use tree_sitter::Node as SyntaxNode;
pub mod abstract_tree;
pub mod built_in_functions;
pub mod built_in_type_definitions;
pub mod built_in_values;
pub mod context;
pub mod error;
pub mod interpret;

View File

@ -10,7 +10,9 @@ use reedline::{
use std::{borrow::Cow, fs::read_to_string, path::PathBuf, process::Command};
use dust_lang::{built_in_values, Context, Error, Interpreter, Value, ValueData};
use dust_lang::{
built_in_values::all_built_in_values, Context, Error, Interpreter, Value, ValueData,
};
/// Command-line arguments to be parsed.
#[derive(Parser, Debug)]
@ -277,7 +279,7 @@ impl Completer for DustCompleter {
}
}
for built_in_value in built_in_values() {
for built_in_value in all_built_in_values() {
let name = built_in_value.name();
let description = built_in_value.description();

View File

@ -25,3 +25,15 @@ fn option() {
)))
);
}
#[test]
fn result() {
assert_eq!(
interpret("new Result:Ok(1)"),
Ok(Value::Enum(EnumInstance::new(
"Result".to_string(),
"Ok".to_string(),
Value::Integer(1)
)))
);
}

View File

@ -13,8 +13,7 @@ async { output ('Whaddup') }
(expression
(function_call
(function_expression
(value
(built_in_value)))
(identifier))
(expression
(value
(string)))))))))

View File

@ -15,8 +15,7 @@ Simple Block
(expression
(function_call
(function_expression
(value
(built_in_value)))
(identifier))
(expression
(value
(integer)))))))))

View File

@ -29,8 +29,7 @@ for i in [1, 2, 3] {
(expression
(function_call
(function_expression
(value
(built_in_value)))
(identifier))
(expression
(identifier)))))))))
@ -63,7 +62,6 @@ for list in list_of_lists {
(expression
(function_call
(function_expression
(value
(built_in_value)))
(identifier))
(expression
(identifier))))))))))))

View File

@ -65,8 +65,7 @@ Complex Logic Sequence
(expression
(function_call
(function_expression
(value
(built_in_value)))
(identifier))
(expression
(identifier))))
(logic_operator)
@ -79,8 +78,7 @@ Complex Logic Sequence
(expression
(function_call
(function_expression
(value
(built_in_value)))
(identifier))
(expression
(identifier))))
(logic_operator)
@ -93,8 +91,7 @@ Complex Logic Sequence
(expression
(function_call
(function_expression
(value
(built_in_value)))
(identifier))
(expression
(identifier))))
(logic_operator)

View File

@ -29,8 +29,7 @@ fs:read('file.txt') | output()
(function_expression
(index
(index_expression
(value
(built_in_value)))
(identifier))
(index_expression
(identifier))))
(expression
@ -38,5 +37,4 @@ fs:read('file.txt') | output()
(string))))
(function_call
(function_expression
(value
(built_in_value)))))))
(identifier))))))

View File

@ -19,8 +19,7 @@ while true {
(expression
(function_call
(function_expression
(value
(built_in_value)))
(identifier))
(expression
(value
(string))))))))))

View File

@ -122,8 +122,6 @@ module.exports = grammar({
$.boolean,
$.list,
$.map,
$.option,
$.built_in_value,
$.range,
$.struct_instance,
$.enum_instance,
@ -182,17 +180,6 @@ module.exports = grammar({
),
),
option: $ =>
choice(
'none',
seq(
'some',
'(',
$.expression,
')',
),
),
index: $ =>
prec.left(
1,
@ -499,18 +486,5 @@ module.exports = grammar({
struct_instance: $ =>
seq('new', $.identifier, $.map),
built_in_value: $ =>
choice(
'args',
'assert_equal',
'env',
'fs',
'json',
'length',
'output',
'random',
'str',
),
},
});

View File

@ -350,14 +350,6 @@
"type": "SYMBOL",
"name": "map"
},
{
"type": "SYMBOL",
"name": "option"
},
{
"type": "SYMBOL",
"name": "built_in_value"
},
{
"type": "SYMBOL",
"name": "range"
@ -524,36 +516,6 @@
]
}
},
"option": {
"type": "CHOICE",
"members": [
{
"type": "STRING",
"value": "none"
},
{
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "some"
},
{
"type": "STRING",
"value": "("
},
{
"type": "SYMBOL",
"name": "expression"
},
{
"type": "STRING",
"value": ")"
}
]
}
]
},
"index": {
"type": "PREC_LEFT",
"value": 1,
@ -1563,47 +1525,6 @@
"name": "map"
}
]
},
"built_in_value": {
"type": "CHOICE",
"members": [
{
"type": "STRING",
"value": "args"
},
{
"type": "STRING",
"value": "assert_equal"
},
{
"type": "STRING",
"value": "env"
},
{
"type": "STRING",
"value": "fs"
},
{
"type": "STRING",
"value": "json"
},
{
"type": "STRING",
"value": "length"
},
{
"type": "STRING",
"value": "output"
},
{
"type": "STRING",
"value": "random"
},
{
"type": "STRING",
"value": "str"
}
]
}
},
"extras": [

View File

@ -70,11 +70,6 @@
"named": true,
"fields": {}
},
{
"type": "built_in_value",
"named": true,
"fields": {}
},
{
"type": "command",
"named": true,
@ -531,21 +526,6 @@
"named": true,
"fields": {}
},
{
"type": "option",
"named": true,
"fields": {},
"children": {
"multiple": false,
"required": false,
"types": [
{
"type": "expression",
"named": true
}
]
}
},
{
"type": "pipe",
"named": true,
@ -770,10 +750,6 @@
"type": "boolean",
"named": true
},
{
"type": "built_in_value",
"named": true
},
{
"type": "enum_instance",
"named": true
@ -798,10 +774,6 @@
"type": "map",
"named": true
},
{
"type": "option",
"named": true
},
{
"type": "range",
"named": true
@ -948,18 +920,10 @@
"type": "any",
"named": false
},
{
"type": "args",
"named": false
},
{
"type": "as",
"named": false
},
{
"type": "assert_equal",
"named": false
},
{
"type": "async",
"named": false
@ -992,10 +956,6 @@
"type": "enum",
"named": false
},
{
"type": "env",
"named": false
},
{
"type": "false",
"named": false
@ -1008,10 +968,6 @@
"type": "for",
"named": false
},
{
"type": "fs",
"named": false
},
{
"type": "identifier",
"named": true
@ -1036,14 +992,6 @@
"type": "integer",
"named": true
},
{
"type": "json",
"named": false
},
{
"type": "length",
"named": false
},
{
"type": "map",
"named": false
@ -1072,14 +1020,6 @@
"type": "option",
"named": false
},
{
"type": "output",
"named": false
},
{
"type": "random",
"named": false
},
{
"type": "range",
"named": true
@ -1088,10 +1028,6 @@
"type": "return",
"named": false
},
{
"type": "some",
"named": false
},
{
"type": "str",
"named": false

File diff suppressed because it is too large Load Diff