Skip to content

Commit f56e4a8

Browse files
committed
Add markdown macro and parse/escape functions
1 parent 6be68fd commit f56e4a8

File tree

3 files changed

+35
-1
lines changed

3 files changed

+35
-1
lines changed

internal/compiler/builtin_macros.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ pub fn lower_macro(
8787
}
8888
BuiltinMacroFunction::Rgb => rgb_macro(n, sub_expr.collect(), diag),
8989
BuiltinMacroFunction::Hsv => hsv_macro(n, sub_expr.collect(), diag),
90+
BuiltinMacroFunction::Markdown => markdown_macro(n, sub_expr.collect()),
9091
}
9192
}
9293

@@ -317,6 +318,30 @@ fn debug_macro(
317318
}
318319
}
319320

321+
fn markdown_macro(node: &dyn Spanned, args: Vec<(Expression, Option<NodeOrToken>)>) -> Expression {
322+
let mut string = None;
323+
for (expr, node) in args {
324+
let escaped = Expression::FunctionCall {
325+
function: BuiltinFunction::EscapeMarkdown.into(),
326+
arguments: vec![expr],
327+
source_location: Some(node.to_source_location()),
328+
};
329+
string = Some(match string {
330+
None => escaped,
331+
Some(string) => Expression::BinaryExpression {
332+
lhs: Box::new(string),
333+
op: '+',
334+
rhs: Box::new(escaped),
335+
},
336+
});
337+
}
338+
Expression::FunctionCall {
339+
function: BuiltinFunction::ParseMarkdown.into(),
340+
arguments: vec![string.unwrap_or_else(|| Expression::default_value_for_type(&Type::String))],
341+
source_location: Some(node.to_source_location()),
342+
}
343+
}
344+
320345
fn to_debug_string(
321346
expr: Expression,
322347
node: &dyn Spanned,

internal/compiler/expression_tree.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,8 @@ pub enum BuiltinFunction {
112112
StopTimer,
113113
RestartTimer,
114114
OpenUrl,
115+
ParseMarkdown,
116+
EscapeMarkdown,
115117
}
116118

117119
#[derive(Debug, Clone)]
@@ -140,6 +142,8 @@ pub enum BuiltinMacroFunction {
140142
Hsv,
141143
/// transform `debug(a, b, c)` into debug `a + " " + b + " " + c`
142144
Debug,
145+
/// Markdown
146+
Markdown,
143147
}
144148

145149
macro_rules! declare_builtin_function_types {
@@ -277,7 +281,9 @@ declare_builtin_function_types!(
277281
StartTimer: (Type::ElementReference) -> Type::Void,
278282
StopTimer: (Type::ElementReference) -> Type::Void,
279283
RestartTimer: (Type::ElementReference) -> Type::Void,
280-
OpenUrl: (Type::String) -> Type::Void
284+
OpenUrl: (Type::String) -> Type::Void,
285+
EscapeMarkdown: (Type::String) -> Type::String,
286+
ParseMarkdown: (Type::String) -> Type::Void
281287
);
282288

283289
impl BuiltinFunction {
@@ -374,6 +380,7 @@ impl BuiltinFunction {
374380
BuiltinFunction::StopTimer => false,
375381
BuiltinFunction::RestartTimer => false,
376382
BuiltinFunction::OpenUrl => false,
383+
BuiltinFunction::ParseMarkdown | BuiltinFunction::EscapeMarkdown => true,
377384
}
378385
}
379386

@@ -452,6 +459,7 @@ impl BuiltinFunction {
452459
BuiltinFunction::StopTimer => false,
453460
BuiltinFunction::RestartTimer => false,
454461
BuiltinFunction::OpenUrl => false,
462+
BuiltinFunction::ParseMarkdown | BuiltinFunction::EscapeMarkdown => true,
455463
}
456464
}
457465
}

internal/compiler/llr/optim_passes/inline_expressions.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ fn builtin_function_cost(function: &BuiltinFunction) -> isize {
156156
BuiltinFunction::StopTimer => 10,
157157
BuiltinFunction::RestartTimer => 10,
158158
BuiltinFunction::OpenUrl => isize::MAX,
159+
BuiltinFunction::ParseMarkdown | BuiltinFunction::EscapeMarkdown => isize::MAX,
159160
}
160161
}
161162

0 commit comments

Comments
 (0)