plum

#treesitter#compiler#wasm

git clone https://git.pyrossh.dev/plum

A statically typed, imperative programming language inspired by rust, python


plum-tooling/tree-sitter-plum/test/corpus/try.txt
af1776b 1
================================================================================
af1776b 2
try operator - postfix ? unwraps or returns early
af1776b 3
================================================================================
af1776b 4
af1776b 5
fun parsePositive(s: Str) -> Result[Int, Str] =
af1776b 6
  n := parseInt(s)?
af1776b 7
  return Ok(n)
af1776b 8
af1776b 9
--------------------------------------------------------------------------------
af1776b 10
af1776b 11
(source
af1776b 12
  (fn
af1776b 13
    name: (fn_identifier)
af1776b 14
    params: (param
af1776b 15
      name: (var_identifier)
af1776b 16
      type: (type
af1776b 17
        (type_identifier)))
af1776b 18
    returns: (type
af1776b 19
      (type_identifier)
af1776b 20
      generics: (type
af1776b 21
        (type_identifier))
af1776b 22
      generics: (type
af1776b 23
        (type_identifier)))
af1776b 24
    body: (body
af1776b 25
      (assign
af1776b 26
        (var_identifier)
af1776b 27
        (expression
af1776b 28
          (primary_expression
af1776b 29
            (try_expression
af1776b 30
              value: (primary_expression
af1776b 31
                (fn_call
af1776b 32
                  function: (var_identifier)
af1776b 33
                  arguments: (fn_argument_list
af1776b 34
                    (expression
af1776b 35
                      (primary_expression
af1776b 36
                        (var_identifier))))))))))
af1776b 37
      (return
af1776b 38
        (expression
af1776b 39
          (primary_expression
af1776b 40
            (fn_call
af1776b 41
              function: (type_identifier)
af1776b 42
              arguments: (fn_argument_list
af1776b 43
                (expression
af1776b 44
                  (primary_expression
af1776b 45
                    (var_identifier)))))))))))
af1776b 46
af1776b 47
================================================================================
af1776b 48
try operator - coexists with the unrelated ternary operator, same `?` token
af1776b 49
================================================================================
af1776b 50
af1776b 51
fun classify(n: Int) -> Int =
af1776b 52
  picked = n > 0 ? n : 0
af1776b 53
  picked
af1776b 54
af1776b 55
--------------------------------------------------------------------------------
af1776b 56
af1776b 57
(source
af1776b 58
  (fn
af1776b 59
    (fn_identifier)
af1776b 60
    (param
af1776b 61
      (var_identifier)
af1776b 62
      (type
af1776b 63
        (type_identifier)))
af1776b 64
    (type
af1776b 65
      (type_identifier))
af1776b 66
    (body
af1776b 67
      (assign
af1776b 68
        (var_identifier)
af1776b 69
        (expression
af1776b 70
          (ternary_expression
af1776b 71
            (expression
af1776b 72
              (comparison_operator
af1776b 73
                (primary_expression
af1776b 74
                  (var_identifier))
af1776b 75
                (primary_expression
af1776b 76
                  (integer))))
af1776b 77
            (expression
af1776b 78
              (primary_expression
af1776b 79
                (var_identifier)))
af1776b 80
            (expression
af1776b 81
              (primary_expression
af1776b 82
                (integer))))))
af1776b 83
      (expression
af1776b 84
        (primary_expression
af1776b 85
          (var_identifier))))))