From 9ad06d721c1e481c82b4f43df819d76e35757282 Mon Sep 17 00:00:00 2001 From: Akshay Date: Sun, 14 Jul 2024 13:01:23 +0100 Subject: add examples --- examples/code-overview/out.txt | 68 ++++++++++++++++++++++++++ examples/code-overview/overview.tbsp | 56 +++++++++++++++++++++ examples/md-to-html/convert.tbsp | 65 ++++++++++++++++++++++++ examples/md-to-html/doc.md | 21 ++++++++ examples/md-to-html/out.html | 20 ++++++++ examples/md-to-html/readme.txt | 3 ++ examples/static-analysis/none_comparisons.tbsp | 24 +++++++++ examples/static-analysis/sample.py | 3 ++ 8 files changed, 260 insertions(+) create mode 100644 examples/code-overview/out.txt create mode 100644 examples/code-overview/overview.tbsp create mode 100644 examples/md-to-html/convert.tbsp create mode 100644 examples/md-to-html/doc.md create mode 100644 examples/md-to-html/out.html create mode 100644 examples/md-to-html/readme.txt create mode 100644 examples/static-analysis/none_comparisons.tbsp create mode 100644 examples/static-analysis/sample.py (limited to 'examples') diff --git a/examples/code-overview/out.txt b/examples/code-overview/out.txt new file mode 100644 index 0000000..78c76b1 --- /dev/null +++ b/examples/code-overview/out.txt @@ -0,0 +1,68 @@ +module + └╴struct Variable + └╴trait Variable + └╴fn value + └╴fn ty + └╴fn assign + └╴enum Value + └╴trait Value + └╴fn ty + └╴fn default + └╴fn default_int + └╴fn default_bool + └╴fn default_string + └╴fn as_boolean + └╴fn add + └╴fn sub + └╴fn mul + └╴fn div + └╴fn mod_ + └╴fn equals + └╴fn greater_than + └╴fn less_than + └╴fn greater_than_equals + └╴fn less_than_equals + └╴fn not + └╴fn and + └╴fn or + └╴trait Value + └╴fn fmt + └╴struct Visitor + └╴struct Visitors + └╴trait Visitors + └╴fn default + └╴trait Visitors + └╴fn new + └╴fn insert + └╴fn get_by_node + └╴enum Error + └╴struct Context + └╴trait Context<'a> + └╴fn fmt + └╴trait Context<'a> + └╴fn new + └╴fn with_program + └╴fn with_input + └╴fn with_cursor + └╴fn eval_expr + └╴fn eval_lit + └╴fn lookup + └╴fn lookup_mut + └╴fn bind + └╴fn eval_bin + └╴fn eval_assign + └╴fn eval_arith + └╴fn eval_cmp + └╴fn eval_logic + └╴fn eval_unary + └╴fn eval_if + └╴fn eval_call + └╴fn eval_declaration + └╴fn eval_statement + └╴fn eval_block + └╴fn eval + └╴fn evaluate + └╴mod test + └╴fn bin + └╴fn test_evaluate_blocks + └╴fn test_evaluate_if diff --git a/examples/code-overview/overview.tbsp b/examples/code-overview/overview.tbsp new file mode 100644 index 0000000..dd56c55 --- /dev/null +++ b/examples/code-overview/overview.tbsp @@ -0,0 +1,56 @@ +BEGIN { + int indent = 1; + string tab = " "; + string tree = "└╴"; + print("module\n"); +} + +enter declaration_list { + indent += 1; +} +leave declaration_list { + indent -= 1; +} + +enter block { + indent += 1; +} +leave block { + indent -= 1; +} + +enter function_item { + print(tab * indent); + print(tree); + print("fn " + text(node.name) + "\n"); +} + +enter function_signature_item { + print(tab * indent); + print(tree); + print("fn " + text(node.name) + "\n"); +} + +enter struct_item { + print(tab * indent); + print(tree); + print("struct " + text(node.name) + "\n"); +} + +enter enum_item { + print(tab * indent); + print(tree); + print("enum " + text(node.name) + "\n"); +} + +enter mod_item { + print(tab * indent); + print(tree); + print("mod " + text(node.name) + "\n"); +} + +enter impl_item { + print(tab * indent); + print(tree); + print("trait " + text(node.type) + "\n"); +} diff --git a/examples/md-to-html/convert.tbsp b/examples/md-to-html/convert.tbsp new file mode 100644 index 0000000..103bcaa --- /dev/null +++ b/examples/md-to-html/convert.tbsp @@ -0,0 +1,65 @@ +BEGIN { + int depth = 0; + + print("\n"); + print("\n"); +} + +enter section { + depth += 1; +} +leave section { + depth -= 1; +} + +enter atx_heading { + print(""); +} +leave atx_heading { + print("\n"); +} + +enter paragraph { + print("

"); +} +leave paragraph { + print("

\n"); +} + +enter list { + print("
    "); +} +leave list { + print("
\n"); +} + +enter list_item { + print("
  • "); +} +leave list_item { + print("
  • \n"); +} + +enter fenced_code_block { + print("
    ");
    +}
    +leave fenced_code_block {
    +    print("
    \n"); +} + +enter inline { + print(text(node)); +} +enter code_fence_content { + print(text(node)); +} + +END { + print("\n"); + print("\n"); +} + diff --git a/examples/md-to-html/doc.md b/examples/md-to-html/doc.md new file mode 100644 index 0000000..2a38bb0 --- /dev/null +++ b/examples/md-to-html/doc.md @@ -0,0 +1,21 @@ +# 1 heading + +content of first paragraph + +## 1.1 heading + +content of nested paragraph + +# 2 heading + +content of second paragraph + +``` +// some code in the code block +fn main() { } +``` + +- who dosent +- despise +- lists + diff --git a/examples/md-to-html/out.html b/examples/md-to-html/out.html new file mode 100644 index 0000000..9b03976 --- /dev/null +++ b/examples/md-to-html/out.html @@ -0,0 +1,20 @@ + + +

    1 heading

    +

    content of first paragraph

    +

    1.1 heading

    +

    content of nested paragraph

    +

    2 heading

    +

    content of second paragraph

    +
    // some code in the code block
    +fn main() { }
    +
    +
    1. who dosent

      +
    2. +
    3. despise

      +
    4. +
    5. lists

      +
    6. +
    + + diff --git a/examples/md-to-html/readme.txt b/examples/md-to-html/readme.txt new file mode 100644 index 0000000..812c1e6 --- /dev/null +++ b/examples/md-to-html/readme.txt @@ -0,0 +1,3 @@ +proof-of-concept markdown-to-html converter using tbsp: + + tbsp -f convert.tbsp -l md < doc.md > out.html diff --git a/examples/static-analysis/none_comparisons.tbsp b/examples/static-analysis/none_comparisons.tbsp new file mode 100644 index 0000000..2d65426 --- /dev/null +++ b/examples/static-analysis/none_comparisons.tbsp @@ -0,0 +1,24 @@ +BEGIN { + bool in_equal_compare = false; + string comparison_text = ""; +} + +enter comparison_operator { + if (text(node.operators) == "==") { + in_equal_compare = true; + comparison_text = text(node); + }; +} + +leave comparison_operator { + in_equal_compare = false; +} + +enter none { + if (in_equal_compare) { + print("using `==` to compare with None, use `is None` instead:\n\t"); + print(comparison_text); + print("\n"); + } else { + }; +} diff --git a/examples/static-analysis/sample.py b/examples/static-analysis/sample.py new file mode 100644 index 0000000..7c3ed09 --- /dev/null +++ b/examples/static-analysis/sample.py @@ -0,0 +1,3 @@ +def foo(): + if bar == None: + print("none") -- cgit v1.2.3