1
0

Remove function_declaration module

This commit is contained in:
Jeff 2023-12-01 22:54:25 -05:00
parent ae05e942f2
commit 31979364eb
13 changed files with 10989 additions and 9245 deletions

View File

@ -1,4 +1,4 @@
fib <fn int -> int> |i| {
fib = <fn int -> int> |i| {
if i <= 1 {
1
} else {

View File

@ -108,7 +108,7 @@ mod tests {
assert_eq!(
evaluate(
"
foobar <fn str -> str> |message| { message }
foobar = <fn str -> str> |message| { message }
(foobar 'Hiya')
",
),

View File

@ -1,111 +0,0 @@
use serde::{Deserialize, Serialize};
use tree_sitter::Node;
use crate::{
AbstractTree, Block, Error, Function, Identifier, Map, Result, Type, TypeDefinition, Value,
};
#[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)]
pub struct FunctionDeclaration {
name: Identifier,
type_definition: TypeDefinition,
function: Function,
}
impl AbstractTree for FunctionDeclaration {
fn from_syntax_node(source: &str, node: Node, context: &Map) -> Result<Self> {
Error::expect_syntax_node(source, "function_declaration", node)?;
let name_node = node.child(0).unwrap();
let name = Identifier::from_syntax_node(source, name_node, context)?;
let type_node = node.child(1).unwrap();
let type_definition = TypeDefinition::from_syntax_node(source, type_node, context)?;
let (parameter_types, return_type) = if let Type::Function {
parameter_types,
return_type,
} = type_definition.inner()
{
(parameter_types, return_type)
} else {
return Err(Error::TypeCheck {
expected: TypeDefinition::new(Type::Function {
parameter_types: Vec::with_capacity(0),
return_type: Box::new(Type::Empty),
}),
actual: type_definition,
location: type_node.start_position(),
source: source[type_node.byte_range()].to_string(),
});
};
let function = {
let function_node = node.child(2).unwrap();
Error::expect_syntax_node(source, "function", function_node)?;
let child_count = function_node.child_count();
let mut parameters = Vec::new();
for index in 1..child_count - 2 {
let child = function_node.child(index).unwrap();
let parameter_index = parameters.len();
let parameter_type = parameter_types.get(parameter_index).unwrap_or(&Type::Empty);
if child.is_named() {
let identifier = Identifier::from_syntax_node(source, child, context)?;
parameters.push((identifier, TypeDefinition::new(parameter_type.clone())));
}
}
let body_node = function_node.child(child_count - 1).unwrap();
let body = Block::from_syntax_node(source, body_node, context)?;
Function::new(
parameters,
body,
TypeDefinition::new(return_type.as_ref().clone()),
)
};
Ok(FunctionDeclaration {
name,
type_definition,
function,
})
}
fn run(&self, _source: &str, context: &Map) -> Result<Value> {
let key = self.name.inner();
context
.variables_mut()?
.insert(key.clone(), Value::Function(self.function.clone()));
Ok(Value::Empty)
}
fn expected_type(&self, _context: &Map) -> Result<TypeDefinition> {
Ok(TypeDefinition::new(Type::Empty))
}
}
#[cfg(test)]
mod tests {
use crate::{evaluate, Value};
#[test]
fn simple_function_declaration() {
let test = evaluate(
"
foo <fn int -> int> |x| { x }
(foo 42)
",
)
.unwrap();
assert_eq!(Value::Integer(42), test);
}
}

View File

@ -107,7 +107,7 @@ mod tests {
let test = evaluate(
"
x = [1 2 3]
y <fn -> int> || { 0 }
y = <fn -> int> || { 0 }
x:(y)
",
)

View File

@ -11,7 +11,6 @@ pub mod block;
pub mod expression;
pub mod r#for;
pub mod function_call;
pub mod function_declaration;
pub mod identifier;
pub mod if_else;
pub mod index;
@ -27,10 +26,9 @@ pub mod r#while;
pub mod r#yield;
pub use {
assignment::*, block::*, expression::*, function_call::*, function_declaration::*,
identifier::*, if_else::*, index::*, index_assignment::IndexAssignment, logic::*, math::*,
r#for::*, r#match::*, r#use::*, r#while::*, r#yield::*, statement::*, type_defintion::*,
value_node::*,
assignment::*, block::*, expression::*, function_call::*, identifier::*, if_else::*, index::*,
index_assignment::IndexAssignment, logic::*, math::*, r#for::*, r#match::*, r#use::*,
r#while::*, r#yield::*, statement::*, type_defintion::*, value_node::*,
};
use tree_sitter::Node;

View File

@ -2,8 +2,8 @@ use serde::{Deserialize, Serialize};
use tree_sitter::Node;
use crate::{
AbstractTree, Assignment, Block, Error, Expression, For, FunctionDeclaration, IfElse,
IndexAssignment, Map, Match, Result, TypeDefinition, Use, Value, While,
AbstractTree, Assignment, Block, Error, Expression, For, IfElse, IndexAssignment, Map, Match,
Result, TypeDefinition, Use, Value, While,
};
/// Abstract representation of a statement.
@ -12,7 +12,6 @@ pub enum Statement {
Assignment(Box<Assignment>),
Return(Expression),
Expression(Expression),
FunctionDeclaration(FunctionDeclaration),
IfElse(Box<IfElse>),
Match(Match),
While(Box<While>),
@ -40,9 +39,6 @@ impl AbstractTree for Statement {
"expression" => Ok(Statement::Expression(Expression::from_syntax_node(
source, child, context
)?)),
"function_declaration" => Ok(Statement::FunctionDeclaration(FunctionDeclaration::from_syntax_node(
source, child, context
)?)),
"if_else" => Ok(Statement::IfElse(Box::new(IfElse::from_syntax_node(
source, child, context
)?))),
@ -76,9 +72,6 @@ impl AbstractTree for Statement {
Statement::Assignment(assignment) => assignment.run(source, context),
Statement::Return(expression) => expression.run(source, context),
Statement::Expression(expression) => expression.run(source, context),
Statement::FunctionDeclaration(function_declaration) => {
function_declaration.run(source, context)
}
Statement::IfElse(if_else) => if_else.run(source, context),
Statement::Match(r#match) => r#match.run(source, context),
Statement::While(r#while) => r#while.run(source, context),
@ -94,9 +87,6 @@ impl AbstractTree for Statement {
Statement::Assignment(assignment) => assignment.expected_type(context),
Statement::Return(expression) => expression.expected_type(context),
Statement::Expression(expression) => expression.expected_type(context),
Statement::FunctionDeclaration(function_declaration) => {
function_declaration.expected_type(context)
}
Statement::IfElse(if_else) => if_else.expected_type(context),
Statement::Match(r#match) => r#match.expected_type(context),
Statement::While(r#while) => r#while.expected_type(context),

View File

@ -4,14 +4,15 @@ use serde::{Deserialize, Serialize};
use tree_sitter::Node;
use crate::{
AbstractTree, Error, Expression, Identifier, List, Map, Result, Statement, Table, Type,
TypeDefinition, Value,
AbstractTree, Error, Expression, Function, Identifier, List, Map, Result, Statement, Table,
Type, TypeDefinition, Value,
};
#[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)]
pub enum ValueNode {
Boolean(String),
Float(String),
Function(Function),
Integer(String),
String(String),
List(Vec<Expression>),
@ -31,6 +32,7 @@ impl AbstractTree for ValueNode {
let value_node = match child.kind() {
"boolean" => ValueNode::Boolean(source[child.byte_range()].to_string()),
"float" => ValueNode::Float(source[child.byte_range()].to_string()),
"function" => ValueNode::Function(Function::from_syntax_node(source, child, context)?),
"integer" => ValueNode::Integer(source[child.byte_range()].to_string()),
"string" => {
let without_quotes = child.start_byte() + 1..child.end_byte() - 1;
@ -117,6 +119,7 @@ impl AbstractTree for ValueNode {
let value = match self {
ValueNode::Boolean(value_source) => Value::Boolean(value_source.parse().unwrap()),
ValueNode::Float(value_source) => Value::Float(value_source.parse().unwrap()),
ValueNode::Function(function) => Value::Function(function.clone()),
ValueNode::Integer(value_source) => Value::Integer(value_source.parse().unwrap()),
ValueNode::String(value_source) => Value::String(value_source.parse().unwrap()),
ValueNode::List(expressions) => {
@ -181,6 +184,7 @@ impl AbstractTree for ValueNode {
let type_definition = match self {
ValueNode::Boolean(_) => TypeDefinition::new(Type::Boolean),
ValueNode::Float(_) => TypeDefinition::new(Type::Float),
ValueNode::Function(function) => Value::Function(function.clone()).r#type(context)?,
ValueNode::Integer(_) => TypeDefinition::new(Type::Integer),
ValueNode::String(_) => TypeDefinition::new(Type::String),
ValueNode::List(expressions) => {

View File

@ -1,8 +1,11 @@
use std::fmt::{self, Display, Formatter};
use serde::{Deserialize, Serialize};
use tree_sitter::Node;
use crate::{AbstractTree, Block, Expression, Identifier, Map, Result, TypeDefinition, Value};
use crate::{
AbstractTree, Block, Error, Expression, Identifier, Map, Result, Type, TypeDefinition, Value,
};
#[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)]
pub struct Function {
@ -59,6 +62,65 @@ impl Function {
}
}
impl AbstractTree for Function {
fn from_syntax_node(source: &str, node: Node, context: &Map) -> Result<Self> {
Error::expect_syntax_node(source, "function", node)?;
let type_node = node.child(0).unwrap();
let type_definition = TypeDefinition::from_syntax_node(source, type_node, context)?;
let (parameter_types, return_type) = if let Type::Function {
parameter_types,
return_type,
} = type_definition.inner()
{
(parameter_types, return_type)
} else {
return Err(Error::TypeCheck {
expected: TypeDefinition::new(Type::Function {
parameter_types: Vec::with_capacity(0),
return_type: Box::new(Type::Empty),
}),
actual: type_definition,
location: type_node.start_position(),
source: source[type_node.byte_range()].to_string(),
});
};
let child_count = node.child_count();
let mut parameters = Vec::new();
for index in 2..child_count - 2 {
let child = node.child(index).unwrap();
let parameter_index = parameters.len();
let parameter_type = parameter_types.get(parameter_index).unwrap_or(&Type::Empty);
if child.is_named() {
let identifier = Identifier::from_syntax_node(source, child, context)?;
parameters.push((identifier, TypeDefinition::new(parameter_type.clone())));
}
}
let body_node = node.child(child_count - 1).unwrap();
let body = Block::from_syntax_node(source, body_node, context)?;
Ok(Function::new(
parameters,
body,
TypeDefinition::new(return_type.as_ref().clone()),
))
}
fn run(&self, _source: &str, _context: &Map) -> Result<Value> {
Ok(Value::Function(self.clone()))
}
fn expected_type(&self, context: &Map) -> Result<TypeDefinition> {
Value::Function(self.clone()).r#type(context)
}
}
impl Display for Function {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
write!(
@ -68,3 +130,21 @@ impl Display for Function {
)
}
}
#[cfg(test)]
mod tests {
use crate::{evaluate, Value};
#[test]
fn simple_function_declaration() {
let test = evaluate(
"
foo = <fn int -> int> |x| { x }
(foo 42)
",
)
.unwrap();
assert_eq!(Value::Integer(42), test);
}
}

View File

@ -2,23 +2,49 @@
Simple Function
================================================================================
foo <fn -> str> || { "Hiya" }
<fn -> str> || { "Hiya" }
--------------------------------------------------------------------------------
(root
(statement
(function_declaration
(identifier)
(expression
(value
(function
(type_definition
(type
(type)))
(function
(block
(statement
(expression
(value
(string)))))))))
(string))))))))))
================================================================================
Function Assignment
================================================================================
foobar = <fn -> str> |text| { text }
--------------------------------------------------------------------------------
(root
(statement
(assignment
(identifier)
(assignment_operator)
(statement
(expression
(value
(function
(type_definition
(type
(type)))
(identifier)
(block
(statement
(expression
(identifier)))))))))))
================================================================================
Function Call
@ -41,7 +67,7 @@ Function Call
Complex Function
================================================================================
foobar <fn str num> |message number| {
<fn str num> |message number| {
(output message)
(output number)
}
@ -50,13 +76,13 @@ foobar <fn str num> |message number| {
(root
(statement
(function_declaration
(identifier)
(expression
(value
(function
(type_definition
(type
(type)
(type)))
(function
(identifier)
(identifier)
(block
@ -71,7 +97,7 @@ foobar <fn str num> |message number| {
(function_call
(identifier)
(expression
(identifier))))))))))
(identifier)))))))))))
================================================================================
Complex Function Call

View File

@ -27,7 +27,6 @@ module.exports = grammar({
$.block,
$.expression,
$.for,
$.function_declaration,
$.if_else,
$.index_assignment,
$.match,
@ -69,6 +68,7 @@ module.exports = grammar({
choice(
$.integer,
$.float,
$.function,
$.string,
$.boolean,
$.list,
@ -223,6 +223,7 @@ module.exports = grammar({
),
logic_operator: $ =>
prec.left(
choice(
'==',
'!=',
@ -233,6 +234,7 @@ module.exports = grammar({
'>=',
'<=',
),
),
assignment: $ =>
seq(
@ -323,7 +325,9 @@ module.exports = grammar({
),
return: $ =>
prec.right(
seq('return', $.expression),
),
use: $ => seq('use', $.string),
@ -354,14 +358,9 @@ module.exports = grammar({
),
),
function_declaration: $ =>
seq(
$.identifier,
$.type_definition,
$.function,
),
function: $ =>
seq(
$.type_definition,
seq(
'|',
repeat(
@ -373,6 +372,7 @@ module.exports = grammar({
'|',
$.block,
),
),
function_call: $ =>
prec.right(

View File

@ -74,10 +74,6 @@
"type": "SYMBOL",
"name": "for"
},
{
"type": "SYMBOL",
"name": "function_declaration"
},
{
"type": "SYMBOL",
"name": "if_else"
@ -199,6 +195,10 @@
"type": "SYMBOL",
"name": "float"
},
{
"type": "SYMBOL",
"name": "function"
},
{
"type": "SYMBOL",
"name": "string"
@ -686,6 +686,9 @@
}
},
"logic_operator": {
"type": "PREC_LEFT",
"value": 0,
"content": {
"type": "CHOICE",
"members": [
{
@ -721,6 +724,7 @@
"value": "<="
}
]
}
},
"assignment": {
"type": "SEQ",
@ -1003,6 +1007,9 @@
}
},
"return": {
"type": "PREC_RIGHT",
"value": 0,
"content": {
"type": "SEQ",
"members": [
{
@ -1014,6 +1021,7 @@
"name": "expression"
}
]
}
},
"use": {
"type": "SEQ",
@ -1153,24 +1161,14 @@
]
}
},
"function_declaration": {
"function": {
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "identifier"
},
{
"type": "SYMBOL",
"name": "type_definition"
},
{
"type": "SYMBOL",
"name": "function"
}
]
},
"function": {
"type": "SEQ",
"members": [
{
@ -1210,6 +1208,8 @@
"name": "block"
}
]
}
]
},
"function_call": {
"type": "PREC_RIGHT",

View File

@ -162,6 +162,10 @@
{
"type": "identifier",
"named": true
},
{
"type": "type_definition",
"named": true
}
]
}
@ -185,29 +189,6 @@
]
}
},
{
"type": "function_declaration",
"named": true,
"fields": {},
"children": {
"multiple": true,
"required": true,
"types": [
{
"type": "function",
"named": true
},
{
"type": "identifier",
"named": true
},
{
"type": "type_definition",
"named": true
}
]
}
},
{
"type": "if",
"named": true,
@ -443,10 +424,6 @@
"type": "for",
"named": true
},
{
"type": "function_declaration",
"named": true
},
{
"type": "if_else",
"named": true
@ -535,6 +512,10 @@
"type": "float",
"named": true
},
{
"type": "function",
"named": true
},
{
"type": "integer",
"named": true

File diff suppressed because it is too large Load Diff