We are currently working on new rules for what content should and shouldn't be allowed on this website, and are looking for feedback! See Esolang:2026 topicality proposal to view and give feedback on the current draft.
衍
Jump to navigation
Jump to search
- This is still a work in progress. It may be changed in the future.
衍 is a programming language designed by PSTF. It's designed to be similar to APL and CJK-based.
EBNF Definition
(* ================================================================ *)
(* Lexical Tokens *)
(* ================================================================ *)
letter = "A" | "B" | ... | "Z" | "a" | "b" | ... | "z" ;
digit = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9" ;
number = digit+ { "." digit+ } ;
variable = (letter | "_") { letter | digit | "_" } ;
string = '"' { . - '"' } '"' ; (* Double-quoted, no newline *)
comment = "#" { . - "\n" } "\n" ;
hanzi = (Any Chinese character except for those which have been reserved)
hanziall = (Any Chinese character)
whitespace = " " | "\t" | "\n" | "\r" ;
(*) Comments and whitespace are ignored between tokens. *)
(* ================================================================ *)
(* Syntax *)
(* ================================================================ *)
Program = { Line } ;
Line = [ Label ":" ] ( Stmt ) ";" ;
Label = variable ;
Stmt = Assignment
| ExpressionStmt
| FunctionDef
| IfStmt
| LoopStmt
| ReturnStmt
| PrintStmt
;
Block = "{" { Line } "}" ;
(* ----- Assignment ----- *)
Assignment = ( variable | IndexedAccess ) "是" Expression ;
IndexedAccess = Expression "选" Expression ; (* e.g., arr 选 3 *)
(* ----- Function Definition (recursive, hence Turing-complete) ----- *)
FunctionDef = "函" variable "(" [ variable { "," variable } ] ")" Block ;
(* ----- Control Flow ----- *)
IfStmt = "若" "(" Expression ")" "则" Block [ "否" Block ] ;
LoopStmt = "循" "(" Expression ")" "行" Block ; (* While loop *)
ReturnStmt = "归" [ Expression ] ;
PrintStmt = "言" Expression ;
ExpressionStmt = Expression ;
(* ----- Expressions (Right-to-Left evaluation, no operator precedence) ----- *)
Expression = Primary | Primary DyadicOp Expression ; (* Right-associative *)
Primary = number
| variable
| string
| Array
| FunctionCall
| MonadicOp Primary
| ReduceExpr
| ScanExpr
| OuterExpr
| InnerExpr
| "(" Expression ")"
;
Array = "[" [ Expression { "," Expression } ] "]" ;
FunctionCall = variable "(" [ Expression { "," Expression } ] ")" ;
(* ----- Operators ----- *)
MonadicOp = "反" (* Reverse *)
| "不" (* Logical Not *)
| "形" (* Shape *)
| "转" (* Transpose *)
| "长" (* Length *)
;
DyadicOp = "加" | "减" | "乘" | "除" | "幂"
| "等" | "少" | "多" | "沉" | "溢" | "不等"
| "与" | "或"
| "取" | "丢" (* Take / Drop *)
| "选" (* Select – also used dyadically *)
;
(* ----- Higher-Order Array Operators ----- *)
ReduceExpr = "折" DyadicOp Primary ; (* e.g., 折 加 [1,2,3] -> 6 *)
ScanExpr = "累" DyadicOp Primary ; (* e.g., 累 加 [1,2,3] -> [1,3,6] *)
OuterExpr = "外" DyadicOp Primary Primary ; (* e.g., 外 加 [1,2] [3,4] -> matrix *)
InnerExpr = "内" DyadicOp DyadicOp Primary Primary ; (* e.g., 内 加 乘 M N *)
Examples
x' = x!
函 阶乘 (n) {
若 (n 少等 1) 则 { 归 (1) }
否 { 归 (n 乘 阶乘 (n 减 1)) }
};
言 阶乘 (5); # 输出: 120
x' = sum(n = 1, 10, n)
i 是 1;
总和 是 0;
循 (i 少等 10) 行 {
总和 是 总和 加 i;
i 是 i 加 1;
};
显 总和; # 输出: 55
x' = xT
矩阵 是 [[1, 2, 3], [4, 5, 6]]; 显 转 矩阵; # 输出: [[1, 4], [2, 5], [3, 6]] A 是 [1, 2]; B 是 [3, 4]; 外积 是 外 乘 A B; # 外 乘 -> outer product 显 外积; # 输出: [[3, 4], [6, 8]]