Roll back changes from 0.4.3

This commit is contained in:
Jeff 2024-02-22 23:49:07 -05:00
parent 6b88fbf8b9
commit 2023e2c7e3
11 changed files with 24108 additions and 23980 deletions

View File

@ -9,7 +9,7 @@ use crate::{
}; };
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord)] #[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord)]
pub struct FunctionNode { pub struct AnonymousFunction {
parameters: Vec<Identifier>, parameters: Vec<Identifier>,
body: Block, body: Block,
r#type: Type, r#type: Type,
@ -19,7 +19,7 @@ pub struct FunctionNode {
context: Context, context: Context,
} }
impl FunctionNode { impl AnonymousFunction {
pub fn parameters(&self) -> &Vec<Identifier> { pub fn parameters(&self) -> &Vec<Identifier> {
&self.parameters &self.parameters
} }
@ -51,9 +51,9 @@ impl FunctionNode {
} }
} }
impl AbstractTree for FunctionNode { impl AbstractTree for AnonymousFunction {
fn from_syntax(node: SyntaxNode, source: &str, context: &Context) -> Result<Self, SyntaxError> { fn from_syntax(node: SyntaxNode, source: &str, context: &Context) -> Result<Self, SyntaxError> {
SyntaxError::expect_syntax_node("function", node)?; SyntaxError::expect_syntax_node("anonymous_function", node)?;
let child_count = node.child_count(); let child_count = node.child_count();
let mut parameters = Vec::new(); let mut parameters = Vec::new();
@ -86,7 +86,7 @@ impl AbstractTree for FunctionNode {
let r#type = Type::function(parameter_types, return_type.take_inner()); let r#type = Type::function(parameter_types, return_type.take_inner());
let syntax_position = node.range().into(); let syntax_position = node.range().into();
Ok(FunctionNode { Ok(AnonymousFunction {
parameters, parameters,
body, body,
r#type, r#type,
@ -141,7 +141,7 @@ impl AbstractTree for FunctionNode {
} }
} }
impl Format for FunctionNode { impl Format for AnonymousFunction {
fn format(&self, output: &mut String, indent_level: u8) { fn format(&self, output: &mut String, indent_level: u8) {
let (parameter_types, return_type) = if let Type::Function { let (parameter_types, return_type) = if let Type::Function {
parameter_types, parameter_types,
@ -169,7 +169,7 @@ impl Format for FunctionNode {
} }
} }
impl Display for FunctionNode { impl Display for AnonymousFunction {
fn fmt(&self, f: &mut Formatter) -> fmt::Result { fn fmt(&self, f: &mut Formatter) -> fmt::Result {
let mut string = String::new(); let mut string = String::new();

View File

@ -1,6 +1,7 @@
//! Abstract, executable representations of corresponding items found in Dust //! Abstract, executable representations of corresponding items found in Dust
//! source code. The types that implement [AbstractTree] are inteded to be //! source code. The types that implement [AbstractTree] are inteded to be
//! created by an [Interpreter]. //! created by an [Interpreter].
pub mod anonymous_function;
pub mod r#as; pub mod r#as;
pub mod assignment; pub mod assignment;
pub mod assignment_operator; pub mod assignment_operator;
@ -12,7 +13,6 @@ pub mod expression;
pub mod r#for; pub mod r#for;
pub mod function_call; pub mod function_call;
pub mod function_expression; pub mod function_expression;
pub mod function_node;
pub mod identifier; pub mod identifier;
pub mod if_else; pub mod if_else;
pub mod index; pub mod index;
@ -34,8 +34,8 @@ pub mod value_node;
pub mod r#while; pub mod r#while;
pub use { pub use {
assignment::*, assignment_operator::*, block::*, command::*, enum_defintion::*, anonymous_function::*, assignment::*, assignment_operator::*, block::*, command::*,
enum_pattern::*, expression::*, function_call::*, function_expression::*, function_node::*, enum_defintion::*, enum_pattern::*, expression::*, function_call::*, function_expression::*,
identifier::*, if_else::*, index::*, index_assignment::IndexAssignment, index_expression::*, identifier::*, if_else::*, index::*, index_assignment::IndexAssignment, index_expression::*,
logic::*, logic_operator::*, map_node::*, match_pattern::*, math::*, math_operator::*, r#as::*, logic::*, logic_operator::*, map_node::*, match_pattern::*, math::*, math_operator::*, r#as::*,
r#for::*, r#match::*, r#type::*, r#while::*, statement::*, struct_definition::*, r#for::*, r#match::*, r#type::*, r#while::*, statement::*, struct_definition::*,

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, Context, Expression, Format, Function, FunctionNode, AbstractTree, Context, Expression, Format, Function, AnonymousFunction,
Identifier, List, Type, Value, TypeDefinition, MapNode, Identifier, List, Type, Value, TypeDefinition, MapNode,
}; };
@ -38,8 +38,8 @@ impl AbstractTree for ValueNode {
let value_node = match child.kind() { let value_node = match child.kind() {
"boolean" => ValueNode::Boolean(source[child.byte_range()].to_string()), "boolean" => ValueNode::Boolean(source[child.byte_range()].to_string()),
"float" => ValueNode::Float(source[child.byte_range()].to_string()), "float" => ValueNode::Float(source[child.byte_range()].to_string()),
"function" => { "anonymous_function" => {
let function_node = FunctionNode::from_syntax(child, source, context)?; let function_node = AnonymousFunction::from_syntax(child, source, context)?;
ValueNode::Function(Function::ContextDefined(function_node)) ValueNode::Function(Function::ContextDefined(function_node))
} }

View File

@ -3,13 +3,13 @@ use std::fmt::{self, Display, Formatter};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use crate::{ use crate::{
built_in_functions::Callable, BuiltInFunction, Format, FunctionNode, Identifier, Type, built_in_functions::Callable, AnonymousFunction, BuiltInFunction, Format, Identifier, Type,
}; };
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)] #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
pub enum Function { pub enum Function {
BuiltIn(BuiltInFunction), BuiltIn(BuiltInFunction),
ContextDefined(FunctionNode), ContextDefined(AnonymousFunction),
} }
impl Function { impl Function {

View File

@ -22,7 +22,7 @@ fib = (i <int>) <int> {
(statement_kind (statement_kind
(expression (expression
(value (value
(function (anonymous_function
(identifier) (identifier)
(type_specification (type_specification
(type)) (type))

View File

@ -11,7 +11,7 @@ Anonymous Function
(statement_kind (statement_kind
(expression (expression
(value (value
(function (anonymous_function
(type_specification (type_specification
(type)) (type))
(block (block
@ -41,7 +41,7 @@ foobar = (x <int>, y <int>) <int> {
(statement_kind (statement_kind
(expression (expression
(value (value
(function (anonymous_function
(identifier) (identifier)
(type_specification (type_specification
(type)) (type))
@ -139,7 +139,7 @@ Anonymous Function Call
(function_call (function_call
(function_expression (function_expression
(value (value
(function (anonymous_function
(identifier) (identifier)
(type_specification (type_specification
(type)) (type))
@ -215,7 +215,7 @@ x(() <bool> { true })
(identifier)) (identifier))
(expression (expression
(value (value
(function (anonymous_function
(type_specification (type_specification
(type)) (type))
(block (block
@ -247,3 +247,35 @@ from_json(read('file.json'))
(expression (expression
(value (value
(string)))))))))) (string))))))))))
================================================================================
Function Definition
================================================================================
fn foo<T> (bar <T>) <T> {
bar
}
--------------------------------------------------------------------------------
(root
(statement
(statement_kind
(type_definition
(function_definition
(identifier)
(type_arguments
(type
(identifier)))
(identifier)
(type_specification
(type
(identifier)))
(type_specification
(type
(identifier)))
(block
(statement
(statement_kind
(expression
(identifier))))))))))

View File

@ -138,7 +138,7 @@ module.exports = grammar({
value: $ => value: $ =>
choice( choice(
$.function, $.anonymous_function,
$.integer, $.integer,
$.float, $.float,
$.string, $.string,
@ -429,7 +429,7 @@ module.exports = grammar({
), ),
), ),
function: $ => anonymous_function: $ =>
seq( seq(
'(', '(',
repeat( repeat(
@ -478,6 +478,7 @@ module.exports = grammar({
type_definition: $ => type_definition: $ =>
choice( choice(
$.enum_definition, $.enum_definition,
$.function_definition,
$.struct_definition, $.struct_definition,
), ),
@ -555,5 +556,23 @@ module.exports = grammar({
struct_instance: $ => struct_instance: $ =>
seq($.identifier, '::', $.map), seq($.identifier, '::', $.map),
function_definition: $ =>
seq(
'fn',
$.identifier,
optional($.type_arguments),
'(',
repeat(
seq(
$.identifier,
$.type_specification,
optional(','),
),
),
')',
$.type_specification,
$.block,
),
}, },
}); });

View File

@ -390,7 +390,7 @@
"members": [ "members": [
{ {
"type": "SYMBOL", "type": "SYMBOL",
"name": "function" "name": "anonymous_function"
}, },
{ {
"type": "SYMBOL", "type": "SYMBOL",
@ -1311,7 +1311,7 @@
] ]
} }
}, },
"function": { "anonymous_function": {
"type": "SEQ", "type": "SEQ",
"members": [ "members": [
{ {
@ -1451,6 +1451,10 @@
"type": "SYMBOL", "type": "SYMBOL",
"name": "enum_definition" "name": "enum_definition"
}, },
{
"type": "SYMBOL",
"name": "function_definition"
},
{ {
"type": "SYMBOL", "type": "SYMBOL",
"name": "struct_definition" "name": "struct_definition"
@ -1717,6 +1721,75 @@
"name": "map" "name": "map"
} }
] ]
},
"function_definition": {
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "fn"
},
{
"type": "SYMBOL",
"name": "identifier"
},
{
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "type_arguments"
},
{
"type": "BLANK"
}
]
},
{
"type": "STRING",
"value": "("
},
{
"type": "REPEAT",
"content": {
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "identifier"
},
{
"type": "SYMBOL",
"name": "type_specification"
},
{
"type": "CHOICE",
"members": [
{
"type": "STRING",
"value": ","
},
{
"type": "BLANK"
}
]
}
]
}
},
{
"type": "STRING",
"value": ")"
},
{
"type": "SYMBOL",
"name": "type_specification"
},
{
"type": "SYMBOL",
"name": "block"
}
]
} }
}, },
"extras": [ "extras": [

View File

@ -1,4 +1,27 @@
[ [
{
"type": "anonymous_function",
"named": true,
"fields": {},
"children": {
"multiple": true,
"required": true,
"types": [
{
"type": "block",
"named": true
},
{
"type": "identifier",
"named": true
},
{
"type": "type_specification",
"named": true
}
]
}
},
{ {
"type": "as_node", "type": "as_node",
"named": true, "named": true,
@ -268,7 +291,26 @@
} }
}, },
{ {
"type": "function", "type": "function_call",
"named": true,
"fields": {},
"children": {
"multiple": true,
"required": true,
"types": [
{
"type": "expression",
"named": true
},
{
"type": "function_expression",
"named": true
}
]
}
},
{
"type": "function_definition",
"named": true, "named": true,
"fields": {}, "fields": {},
"children": { "children": {
@ -283,6 +325,10 @@
"type": "identifier", "type": "identifier",
"named": true "named": true
}, },
{
"type": "type_arguments",
"named": true
},
{ {
"type": "type_specification", "type": "type_specification",
"named": true "named": true
@ -290,25 +336,6 @@
] ]
} }
}, },
{
"type": "function_call",
"named": true,
"fields": {},
"children": {
"multiple": true,
"required": true,
"types": [
{
"type": "expression",
"named": true
},
{
"type": "function_expression",
"named": true
}
]
}
},
{ {
"type": "function_expression", "type": "function_expression",
"named": true, "named": true,
@ -822,6 +849,10 @@
"type": "enum_definition", "type": "enum_definition",
"named": true "named": true
}, },
{
"type": "function_definition",
"named": true
},
{ {
"type": "struct_definition", "type": "struct_definition",
"named": true "named": true
@ -852,6 +883,10 @@
"multiple": false, "multiple": false,
"required": true, "required": true,
"types": [ "types": [
{
"type": "anonymous_function",
"named": true
},
{ {
"type": "boolean", "type": "boolean",
"named": true "named": true
@ -864,10 +899,6 @@
"type": "float", "type": "float",
"named": true "named": true
}, },
{
"type": "function",
"named": true
},
{ {
"type": "integer", "type": "integer",
"named": true "named": true
@ -1074,6 +1105,10 @@
"type": "float", "type": "float",
"named": false "named": false
}, },
{
"type": "fn",
"named": false
},
{ {
"type": "for", "type": "for",
"named": false "named": false

File diff suppressed because it is too large Load Diff