Skip to content

Commit

Permalink
bumpalo for AST
Browse files Browse the repository at this point in the history
  • Loading branch information
aatxe committed Sep 29, 2024
1 parent 7bb315f commit a2cf462
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 52 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ name = "sanguinello"
path = "lib/sanguinello.rs"

[dependencies]
bumpalo = { version = "3.16.0", features = ["std", "serde", "boxed", "collections"] }
nom = "7.1.3"
thiserror = "1.0.40"
107 changes: 55 additions & 52 deletions lib/sg/ast.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
type Optional<T> = Option<Box<T>>;
use bumpalo::boxed::Box;
use bumpalo::collections::Vec;

type Optional<'a, T> = Option<Box<'a, T>>;

#[derive(PartialEq, Debug, Eq, Clone)]
pub struct Identifier(pub String);
Expand All @@ -10,11 +13,11 @@ pub struct Identifier(pub String);
/// @library/module/submodule
/// ```
#[derive(PartialEq, Debug, Eq, Clone)]
pub struct Path {
pub struct Path<'a> {
/// The unprefixed root of the path, `None` for `@`.
root: Option<Identifier>,
/// Fragments of the path separated by `/`
fragments: Vec<Identifier>,
fragments: Vec<'a, Identifier>,
}

#[derive(PartialEq, Debug, Clone)]
Expand Down Expand Up @@ -58,10 +61,10 @@ pub enum Operator {
#[derive(PartialEq, Debug, Clone)]
pub struct Type;

#[derive(PartialEq, Debug, Clone)]
pub struct TypeBinding {
key: Optional<Identifier>,
value: Optional<Type>,
#[derive(PartialEq, Debug)]
pub struct TypeBinding<'a> {
key: Optional<'a, Identifier>,
value: Optional<'a, Type>,
}

#[derive(PartialEq, Debug, Clone)]
Expand All @@ -72,61 +75,61 @@ pub struct Binding {

/// A `Block` is a series of `Statement`s followed by an optional `Expression`
/// that the `Block` takes the value of when evaluated.
#[derive(PartialEq, Debug, Clone)]
pub struct Block {
statements: Vec<Statement>,
expression: Optional<Expression>,
#[derive(PartialEq, Debug)]
pub struct Block<'a> {
statements: Vec<'a, Statement<'a>>,
expression: Optional<'a, Expression<'a>>,
}

/// A `Property` is a key-value pair written either as `string_key = expression` or
/// as `[key_expression] = expression`.
#[derive(PartialEq, Debug, Clone)]
pub struct Property {
key: Optional<Expression>,
value: Optional<Expression>,
#[derive(PartialEq, Debug)]
pub struct Property<'a> {
key: Optional<'a, Expression<'a>>,
value: Optional<'a, Expression<'a>>,
}

#[derive(PartialEq, Debug, Clone)]
pub enum Expression {
#[derive(PartialEq, Debug)]
pub enum Expression<'a> {
Identifier(Identifier),
Literal(Literal),

/// ```sg
/// { key = value, key = value, key = value }
/// ```
Table {
types: Vec<TypeBinding>,
elements: Vec<Property>,
types: Vec<'a, TypeBinding<'a>>,
elements: Vec<'a, Property<'a>>,
},

/// ```sg
/// [e1, e2, e3]
/// ```
Array {
elements: Vec<Expression>,
elements: Vec<'a, Expression<'a>>,
},

/// ```sg
/// (e1, e2, e3)
/// e1, e2, e3
/// ```
Tuple {
elements: Vec<Expression>,
elements: Vec<'a, Expression<'a>>,
},

/// ```sg
/// e1[e2]
/// ```
Index {
value: Optional<Expression>,
key: Optional<Expression>,
value: Optional<'a, Expression<'a>>,
key: Optional<'a, Expression<'a>>,
},

/// ```sg
/// e1.string_key
/// ```
Project {
value: Optional<Expression>,
value: Optional<'a, Expression<'a>>,
key: Identifier,
},

Expand All @@ -136,18 +139,18 @@ pub enum Expression {
/// ```
UnaryOperator {
operator: Operator,
operand: Optional<Expression>,
operand: Optional<'a, Expression<'a>>,
},

/// ```sg
/// a or b
/// a and b
/// a ~= b
/// ```
BinaryOperator{
BinaryOperator {
operator: Operator,
left_operand: Optional<Expression>,
right_operand: Optional<Expression>,
left_operand: Optional<'a, Expression<'a>>,
right_operand: Optional<'a, Expression<'a>>,
},

/// ```sg
Expand All @@ -158,7 +161,7 @@ pub enum Expression {
Function {
name: Option<Identifier>,
parameter: Option<Binding>,
body: Optional<Block>,
body: Optional<'a, Block<'a>>,
},

/// ```sg
Expand All @@ -173,8 +176,8 @@ pub enum Expression {
/// function {argument}
/// ```
Application {
function: Optional<Expression>,
argument: Optional<Expression>,
function: Optional<'a, Expression<'a>>,
argument: Optional<'a, Expression<'a>>,
},

/// ```sg
Expand All @@ -189,9 +192,9 @@ pub enum Expression {
/// e1:e2 {argument}
/// ```
ProjectApplication {
value: Optional<Expression>,
value: Optional<'a, Expression<'a>>,
key: Identifier,
argument: Optional<Expression>,
argument: Optional<'a, Expression<'a>>,
},

/// ```sg
Expand All @@ -208,9 +211,9 @@ pub enum Expression {
/// end
/// ```
If {
condition: Optional<Expression>,
consequent: Block,
antecedent: Block,
condition: Optional<'a, Expression<'a>>,
consequent: Block<'a>,
antecedent: Block<'a>,
},

/// ```sg
Expand All @@ -220,21 +223,21 @@ pub enum Expression {
/// expr
/// end
/// ```
Block(Block),
Block(Block<'a>),
}

#[derive(PartialEq, Debug, Clone)]
pub enum Statement {
#[derive(PartialEq, Debug)]
pub enum Statement<'a> {
/// ```sg
/// expr
/// ```
Expression(Expression),
Expression(Expression<'a>),

/// ```sg
/// import @/module/submodule
/// import @library/module/submodule
/// ```
Import(Path),
Import(Path<'a>),

/// ```sg
/// export type Foo = Bar
Expand All @@ -252,7 +255,7 @@ pub enum Statement {
/// ```
Local {
binding: Binding,
expression: Optional<Expression>,
expression: Optional<'a, Expression<'a>>,
},

/// ```sg
Expand All @@ -261,7 +264,7 @@ pub enum Statement {
/// ```
Export {
binding: Binding,
expression: Optional<Expression>,
expression: Optional<'a, Expression<'a>>,
},

/// ```sg
Expand Down Expand Up @@ -291,8 +294,8 @@ pub enum Statement {
Module {
exported: bool,
binding: Binding,
types: Vec<TypeBinding>,
elements: Vec<Property>,
types: Vec<'a, TypeBinding<'a>>,
elements: Vec<'a, Property<'a>>,
},

/// ```sg
Expand All @@ -302,8 +305,8 @@ pub enum Statement {
/// ```
ForIn {
binding: Binding,
iterator: Optional<Expression>,
body: Optional<Block>,
iterator: Optional<'a, Expression<'a>>,
body: Optional<'a, Block<'a>>,
},

/// ```sg
Expand All @@ -312,8 +315,8 @@ pub enum Statement {
/// end
/// ```
While {
condition: Optional<Expression>,
body: Optional<Block>,
condition: Optional<'a, Expression<'a>>,
body: Optional<'a, Block<'a>>,
},

/// ```sg
Expand All @@ -322,8 +325,8 @@ pub enum Statement {
/// until expr
/// ```
RepeatUntil {
body: Optional<Block>,
condition: Optional<Expression>,
body: Optional<'a, Block<'a>>,
condition: Optional<'a, Expression<'a>>,
},

/// ```sg
Expand All @@ -340,5 +343,5 @@ pub enum Statement {
/// return expr
/// return
/// ```
Return(Optional<Expression>),
Return(Optional<'a, Expression<'a>>),
}

0 comments on commit a2cf462

Please sign in to comment.