Skip to content

Commit 769d439

Browse files
committed
Add markdown macro and parse/escape functions
1 parent 89aaadf commit 769d439

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

@@ -319,6 +320,30 @@ fn debug_macro(
319320
}
320321
}
321322

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