莉莉编程语言
Lily Programming Language

原始链接: https://lily-lang.org

Lily 是一种静态类型编程语言,具有解释器,并兼具性能和安全性。它采用引用计数进行内存管理,并辅以垃圾回收。主要特性包括内置模板元编程、无缝的 C 嵌入/扩展、单继承类、异常以及强大的代数数据类型——特别是 `Option` 和 `Result`,用于健壮的错误处理。 提供的示例通过一个逆波兰表示法 (RPN) 计算器展示了 Lily 的能力。这个函数 `rpn` 解析字符串输入,使用栈,并执行在字典 (`math_ops`) 中定义的算术运算。它利用 `Result` 来处理潜在的错误,例如栈下溢、无效操作和除以零,并提供信息丰富的失败消息。这段代码展示了 Lily 简洁的语法和类型推断,突出了它优雅地管理错误和有效地执行计算的能力。

黑客新闻 新的 | 过去的 | 评论 | 提问 | 展示 | 工作 | 提交 登录 Lily 编程语言 (lily-lang.org) 6 分,来自 FascinatedBox 2 小时前 | 隐藏 | 过去的 | 收藏 | 2 条评论 oneseven 22 分钟前 [–] 我真正想从 HN 上的 “*-编程语言” 帖子中看到的是 _为什么_。 为什么是 Lily?回复 andyferris 6 分钟前 | 父评论 [–] Gitlab 上的 README 至少有一两句话说明了这一点:https://gitlab.com/FascinatedBox/lily> 一种注重表达力和类型安全的解释型语言。 我个人认为类型化脚本语言可能是未来。 它们应该在必要时支持 AOT 编译。 指南 | 常见问题 | 列表 | API | 安全 | 法律 | 申请 YC | 联系 搜索:
相关文章

原文

Lily is statically-typed, with an interpreter as a reference. Lily uses reference counting for memory management with garbage collection as a fallback.

Key features of Lily:

  • Built-in template mode
  • Embed/extend in C
  • Single-inheritance classes
  • Exceptions
  • Generics
  • Algebraic data types (with Option and Result predefined).

Here's a small example showing most of Lily's features:

var math_ops = ["+" => (|a: Integer, b: Integer| a + b),
                "-" => (|a, b| a - b),
                "*" => (|a, b| a * b),
                "/" => (|a, b| a / b)]

define rpn(input: String): Result[String, List[Integer]]
{
    var values = input.split(" ")
    var stack: List[Integer] = []

    for v in values: {
        with v.parse_i() as Some(s): {
            s |> stack.push
        else:
            if stack.size() < 2: {
                return Failure("Stack underflow.")
            }

            var right = stack.pop()
            var left = stack.pop()
            try: {
                var op_fn = math_ops[v]
                var op_value = op_fn(left, right)
                stack.push(op_value)
            except KeyError:
                return Failure("Invalid operation {}.".format(v))
            except DivisionByZeroError:
                return Failure("Attempt to divide by zero.")
            except Exception:
                return Failure("Unexpected error from op {}.".format(v))
            }
        }
    }

    return Success(stack)
}

print("1 2 3 4 * + -"  |> rpn) # Success([-13])
print("2 2 2 2 * *"    |> rpn) # Success([2, 8])
print("*"              |> rpn) # Failure("Stack underflow.")
print("1 2 ?"          |> rpn) # Failure("Invalid operation ?.")
联系我们 contact @ memedata.com