泰坦尼亚编程语言
Titania Programming Language

原始链接: https://github.com/gingerBill/titania

## Oberon-07:一种编译器开发语言 Oberon-07 是一种静态类型、模块化的编程语言,灵感来自 Niklaus Wirth 的 Oberon,专门设计用于教学编译器构造。其语法使用上下文无关文法定义,模块是基本的构建块,允许通过 `import` 语句进行代码组织和重用。 该语言支持标准数据类型,如整数、实数、字符串、布尔值和集合,以及结构化类型,包括数组、记录和指针。过程 (`proc`) 是核心,支持形式参数和 `return` 语句。 控制流通过赋值、条件 `if`/`elseif`/`else`、`case` 以及循环结构 (`while`、`repeat`、`for`) 等语句管理。Oberon-07 还包括用于执行绝对值、位移、字符转换、集合操作和内存管理 (`new`、`delete`) 等操作的内置函数。标准输入/输出通过 `print` 和 `println` 提供。 该语言旨在实现简单性和清晰性,使其成为探索编译器设计原理的理想平台。

Hacker News 新闻 | 过去 | 评论 | 提问 | 展示 | 工作 | 提交 登录 Titania 编程语言 (github.com/gingerbill) 39 分,由 MaximilianEmel 2小时前发布 | 隐藏 | 过去 | 收藏 | 5 条评论 loumf 17分钟前 | 下一个 [–] 看了你的源码,我了解了 Odin -- 现在我想听更多关于它的信息。回复 khaledh 3分钟前 | 父评论 | 下一个 [–] 他也是 Odin 的创造者 :) __d 1小时前 | 上一个 | 下一个 [–] 巧妙的名字。 讨论它与 Oberon07 不同的动机会很好。回复 fijiaarone 15分钟前 | 上一个 | 下一个 [–] 一个谦虚的建议…与其在你的编程语言中拥有 println() 或其等效函数,不如添加一个新的特殊字符来表示字符串后的换行符:print(“Hello world”.) ruslan 32分钟前 | 上一个 [–] 没有指针?回复 指南 | 常见问题 | 列表 | API | 安全 | 法律 | 申请 YC | 联系 搜索:
相关文章

原文

Based on the Oberon-07 programming language designed by the late Niklaus Wirth.

This is designed to be a language to teach compiler development with.

Meaning behind the name:

module = "module" ident ";" [import_list] decl_sequence
         ["begin" stmt_sequence] "end" [";"].

import_list = "import" import_decl {"," import_decl} ";".
decl_sequence = ["const" {const_decl ";"}]
                ["type"  {type_decl  ";"}]
                ["var"   {var_decl   ";"}]
                [{proc_decl          ";"}].

const_decl = ident "=" const_expr.
type_decl = ident "="" struct_type.
var_decl = ident_list ":" type.

proc_decl = "proc" ident [formal_parameters] ";" proc_body.
proc_body = decl_sequence ["begin" stmt_sequence] ["return" expr] "end".


const_expr = expr.
expr = simple_expr {relation simple_expr}.

simple_expr = ["+" | "-"] unary_expr {add_operator unary_expr}.
unary_expr = ["+" | "-"] term.
term = factor {mul_operator factor}.

factor = integer | real | string | nil | true | false | set |
         "(" expr ")" | "not" expr | designator.

element = expr [".." expr].

ident_list = ident {"," ident}.
qual_ident = [ident "."] ident.

struct_type = array_type | record_type | pointer_type | proc_type.
array_type = "["" const_expr {"," const_expr} "]" type.
record_type = "record" ["(" qual_ident ")"] [field_list_sequence] "end".
pointer_type = "^" type.
proc_type = "proc" formal_parameters.
field_list = ident_list ":" type.
formal_parmeters = "(" [fp_section {";" fp_section}] [";"] ")".
formal_type = "[" "]" qual_ident.

stmt_sequence = stmt {";" stmt} [";"].
stmt = [assignment | proc_call | if_stmt | case_stmt | while_stmt | repeat_stmt | for_stmt ].

assignment = designator ":=" expr

if_stmt = "if" expr "then" stmt_sequence
          {"elseif" expr "then" stmt_sequence}
          ["else" stmt_sequence]
          "end".

case_stmt = "case" expr "of" case {"|" case} "end".
case = [case_label_list ":" stmt_sequence].
case_list = label_range {"," label_range}.
label_range = label [".." label].
label = integer | string | qual_ident.

while_stmt = "while" expr "do" stmt_sequence
             {"elseif" expr "then" stmt_sequence}
             "end".
repeat_stmt = "repeat" stmt_sequence "until" expr.
for_stmt = "for" ident ":=" expr "to" expr ["by" const_expr] "do" stmt_sequence "end".


designator = qual_ident {selector}.
selector = "." ident | "[" expr_list "]" | "^" | "(" qual_ident ")".
expr_list = expr {"," expr}.


add_operator = "+" | "-" | "xor" | "or".
mul_operator = "*" | "/" | "%"   | "and".
relation     = "=" | "<>" | "<" | "<=" | ">" | ">=" | "in" | "is".
and    else    import  of      then   while
begin  elseif  in      or      to     xor
by     end     is      proc    true
case   false   module  record  type
const  for     nil     repeat  until
do     if      not     return  var
+    .   (   )   =  <>
-    ,   [   ]   <  <=
*    ;   {   }   >  >=
/    |   :=  :   ..
%    ^

Note: These will be added to as the compiler develops

abs(x)            - absolute value of
lsh(x, y)         - logical shift left
ash(x, y)         - arithmetic shift right
ror(x, y)         - rotate right
chr(i)            - convert int to char
ord(c)            - convert char to int
inc(x)            - x := x + 1
inc(x, y)         - x := x + y
dec(x)            - x := x - 1
dec(x, y)         - x := x - y
incl(x, y)        - include y in set x
excl(x, y)        - exclude y in set x
odd(x)            - x % 2 = 0
floor(x)          - round-down for real
ceil(x)           - round-up   for real
assert(cond)      - assert when cond is false
new(ptr)          - allocate memory
delete(ptr)       - free memory
addr(x)           - address of addressable memory
size_of(x)        - size of the type of 'x'
align_of(x)       - alignment of the type of 'x'
copy(dst, src, n) - non-overlapping memory copying from `src` to `dst` of `n` bytes
print(...)        - variadic print without newline
println(...)      - variadic print with newline
len(x)            - length of an array 'x'
联系我们 contact @ memedata.com