summaryrefslogtreecommitdiff
path: root/src/syntax
diff options
context:
space:
mode:
authorLaurenz <laurmaedje@gmail.com>2021-01-02 19:37:10 +0100
committerLaurenz <laurmaedje@gmail.com>2021-01-02 19:37:10 +0100
commit1c40dc42e7bc7b799b77f06d25414aca59a044ba (patch)
treeea8bdedaebf59f5bc601346b0108236c7264a29d /src/syntax
parent8cad78481cd52680317032c3bb84cacda5666489 (diff)
Dynamic values, Types, Arrays, and Dictionaries 🚀
- Identifiers are now evaluated as variables instead of being plain values - Constants like `left` or `bold` are stored as dynamic values containing the respective rust types - We now distinguish between arrays and dictionaries to make things more intuitive (at the cost of a bit more complex parsing) - Spans were removed from collections (arrays, dictionaries), function arguments still have spans for the top-level values to enable good diagnostics
Diffstat (limited to 'src/syntax')
-rw-r--r--src/syntax/expr.rs52
-rw-r--r--src/syntax/node.rs2
2 files changed, 31 insertions, 23 deletions
diff --git a/src/syntax/expr.rs b/src/syntax/expr.rs
index 4c6ce872..905ade04 100644
--- a/src/syntax/expr.rs
+++ b/src/syntax/expr.rs
@@ -2,7 +2,6 @@
use super::*;
use crate::color::RgbaColor;
-use crate::eval::DictKey;
use crate::geom::Unit;
/// An expression.
@@ -24,10 +23,22 @@ pub struct ExprCall {
/// The name of the function.
pub name: Spanned<Ident>,
/// The arguments to the function.
- ///
- /// In case of a bracketed invocation with a body, the body is _not_
- /// included in the span for the sake of clearer error messages.
- pub args: Spanned<LitDict>,
+ pub args: Spanned<Arguments>,
+}
+
+/// The arguments to a function: `12, draw: false`.
+///
+/// In case of a bracketed invocation with a body, the body is _not_
+/// included in the span for the sake of clearer error messages.
+pub type Arguments = Vec<Argument>;
+
+/// An argument to a function call: `12` or `draw: false`.
+#[derive(Debug, Clone, PartialEq)]
+pub enum Argument {
+ /// A positional arguments.
+ Pos(Spanned<Expr>),
+ /// A named argument.
+ Named(Named),
}
/// A unary operation: `-x`.
@@ -92,28 +103,25 @@ pub enum Lit {
Color(RgbaColor),
/// A string literal: `"hello!"`.
Str(String),
- /// A dictionary literal: `(false, 12cm, greeting: "hi")`.
- Dict(LitDict),
+ /// An array literal: `(1, "hi", 12cm)`.
+ Array(Array),
+ /// A dictionary literal: `(color: #f79143, pattern: dashed)`.
+ Dict(Dict),
/// A content literal: `{*Hello* there!}`.
Content(SynTree),
}
-/// A dictionary literal: `(false, 12cm, greeting: "hi")`.
-#[derive(Debug, Default, Clone, PartialEq)]
-pub struct LitDict(pub Vec<LitDictEntry>);
+/// An array literal: `(1, "hi", 12cm)`.
+pub type Array = SpanVec<Expr>;
-/// An entry in a dictionary literal: `false` or `greeting: "hi"`.
+/// A dictionary literal: `(color: #f79143, pattern: dashed)`.
+pub type Dict = Vec<Named>;
+
+/// A pair of a name and an expression: `pattern: dashed`.
#[derive(Debug, Clone, PartialEq)]
-pub struct LitDictEntry {
- /// The key of the entry if there was one: `greeting`.
- pub key: Option<Spanned<DictKey>>,
- /// The value of the entry: `"hi"`.
+pub struct Named {
+ /// The name: `pattern`.
+ pub name: Spanned<Ident>,
+ /// The right-hand side of the pair: `dashed`.
pub expr: Spanned<Expr>,
}
-
-impl LitDict {
- /// Create an empty dict literal.
- pub fn new() -> Self {
- Self::default()
- }
-}
diff --git a/src/syntax/node.rs b/src/syntax/node.rs
index b7691a70..cee810a2 100644
--- a/src/syntax/node.rs
+++ b/src/syntax/node.rs
@@ -30,7 +30,7 @@ pub enum SynNode {
Expr(Expr),
}
-/// A section heading: `# ...`.
+/// A section heading: `# Introduction`.
#[derive(Debug, Clone, PartialEq)]
pub struct NodeHeading {
/// The section depth (numer of hashtags minus 1).