1
0

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,12 +83,8 @@ impl Format for IndexExpression {
fn format(&self, output: &mut String, indent_level: u8) { fn format(&self, output: &mut String, indent_level: u8) {
match self { match self {
IndexExpression::Value(value_node) => { 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::Identifier(identifier) => identifier.format(output, indent_level),
IndexExpression::FunctionCall(function_call) => { IndexExpression::FunctionCall(function_call) => {
function_call.format(output, indent_level) function_call.format(output, indent_level)

View File

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

View File

@ -5,7 +5,7 @@ use tree_sitter::Node as SyntaxNode;
use crate::{ use crate::{
error::{RuntimeError, SyntaxError, ValidationError}, error::{RuntimeError, SyntaxError, ValidationError},
AbstractTree, BuiltInValue, Context, Expression, Format, Function, FunctionNode, AbstractTree, Context, Expression, Format, Function, FunctionNode,
Identifier, List, Type, Value, TypeDefinition, MapNode, Identifier, List, Type, Value, TypeDefinition, MapNode,
}; };
@ -19,7 +19,6 @@ pub enum ValueNode {
List(Vec<Expression>), List(Vec<Expression>),
Option(Option<Box<Expression>>), Option(Option<Box<Expression>>),
Map(MapNode), Map(MapNode),
BuiltInValue(BuiltInValue),
Range(RangeInclusive<i64>), Range(RangeInclusive<i64>),
Struct { Struct {
name: Identifier, name: Identifier,
@ -81,15 +80,6 @@ impl AbstractTree for ValueNode {
ValueNode::Option(Some(Box::new(expression))) 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" => { "range" => {
let mut split = source[child.byte_range()].split(".."); let mut split = source[child.byte_range()].split("..");
let start = split.next().unwrap().parse().unwrap(); let start = split.next().unwrap().parse().unwrap();
@ -176,7 +166,6 @@ impl AbstractTree for ValueNode {
} }
} }
ValueNode::Map(_) => Type::Map, ValueNode::Map(_) => Type::Map,
ValueNode::BuiltInValue(built_in_value) => built_in_value.expected_type(context)?,
ValueNode::Struct { name, .. } => { ValueNode::Struct { name, .. } => {
Type::Custom(name.clone()) Type::Custom(name.clone())
} }
@ -233,7 +222,6 @@ impl AbstractTree for ValueNode {
Value::Option(option_value) Value::Option(option_value)
} }
ValueNode::Map(map_node) => map_node.run(source, context)?, 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::Range(range) => Value::Range(range.clone()),
ValueNode::Struct { name, properties } => { ValueNode::Struct { name, properties } => {
let instance = if let Some(definition) = context.get_definition(name.inner())? { 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::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 } => { ValueNode::Struct { name, properties } => {
name.format(output, indent_level); name.format(output, indent_level);
properties.format(output, indent_level); properties.format(output, indent_level);
@ -342,8 +329,6 @@ impl Ord for ValueNode {
(ValueNode::Option(_), _) => Ordering::Greater, (ValueNode::Option(_), _) => Ordering::Greater,
(ValueNode::Map(left), ValueNode::Map(right)) => left.cmp(right), (ValueNode::Map(left), ValueNode::Map(right)) => left.cmp(right),
(ValueNode::Map(_), _) => Ordering::Greater, (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} ) => { (ValueNode::Struct{ name: left_name, properties: left_properties }, ValueNode::Struct {name: right_name, properties: right_properties} ) => {
let name_cmp = left_name.cmp(right_name); 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(); static STRING: OnceLock<Value> = OnceLock::new();
/// Returns the entire built-in value API. /// 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() all()
} }
@ -212,11 +212,11 @@ impl Format for BuiltInValue {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use crate::built_in_values; use super::all_built_in_values;
#[test] #[test]
fn check_built_in_types() { 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 expected = built_in_value.r#type();
let actual = built_in_value.get().r#type(); let actual = built_in_value.get().r#type();

View File

@ -5,8 +5,8 @@ use std::{
}; };
use crate::{ use crate::{
built_in_type_definitions::all_built_in_type_definitions, error::rw_lock_error::RwLockError, built_in_type_definitions::all_built_in_type_definitions, built_in_values::all_built_in_values,
Type, TypeDefinition, Value, error::rw_lock_error::RwLockError, Type, TypeDefinition, Value,
}; };
#[derive(Clone, Debug)] #[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) Ok(None)
} }

View File

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

View File

@ -10,7 +10,9 @@ use reedline::{
use std::{borrow::Cow, fs::read_to_string, path::PathBuf, process::Command}; 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. /// Command-line arguments to be parsed.
#[derive(Parser, Debug)] #[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 name = built_in_value.name();
let description = built_in_value.description(); 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 (expression
(function_call (function_call
(function_expression (function_expression
(value (identifier))
(built_in_value)))
(expression (expression
(value (value
(string))))))))) (string)))))))))

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -122,8 +122,6 @@ module.exports = grammar({
$.boolean, $.boolean,
$.list, $.list,
$.map, $.map,
$.option,
$.built_in_value,
$.range, $.range,
$.struct_instance, $.struct_instance,
$.enum_instance, $.enum_instance,
@ -182,17 +180,6 @@ module.exports = grammar({
), ),
), ),
option: $ =>
choice(
'none',
seq(
'some',
'(',
$.expression,
')',
),
),
index: $ => index: $ =>
prec.left( prec.left(
1, 1,
@ -499,18 +486,5 @@ module.exports = grammar({
struct_instance: $ => struct_instance: $ =>
seq('new', $.identifier, $.map), 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", "type": "SYMBOL",
"name": "map" "name": "map"
}, },
{
"type": "SYMBOL",
"name": "option"
},
{
"type": "SYMBOL",
"name": "built_in_value"
},
{ {
"type": "SYMBOL", "type": "SYMBOL",
"name": "range" "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": { "index": {
"type": "PREC_LEFT", "type": "PREC_LEFT",
"value": 1, "value": 1,
@ -1563,47 +1525,6 @@
"name": "map" "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": [ "extras": [

View File

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

File diff suppressed because it is too large Load Diff