From 035a3b56256ca913a810a979c6ffa5fb03f32c0f Mon Sep 17 00:00:00 2001 From: Thomas Voss Date: Mon, 9 Mar 2026 21:14:42 +0100 Subject: Basic grammar description --- grammar | 103 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 103 insertions(+) create mode 100644 grammar diff --git a/grammar b/grammar new file mode 100644 index 0000000..8e528ce --- /dev/null +++ b/grammar @@ -0,0 +1,103 @@ +root: module-decl top-level*; + +module-decl: "module" ident ";"; +top-level: def-bind ";"; + +def-bind: "def" decl-list "=" expr-list; + +decl-list: (ident expr? ",")* ident expr?; +expr-list: (expr ",")* expr; + +expr + : ident + | number + | string + | function-proto + | function + | function-call + | expr "^" /* Pointer Dereference */ + | unary-op expr + | expr binary-op expr + | "(" expr ")" + ; + +function: function-proto block-statement; +function-call: expr "(" expr-list? ")"; +function-proto: "func" "(" decl-list? ")" (expr | "(" expr-list ")")?; + +block-statement: "{" statement* "}"; +statement + : block-statement + | (expr + | def-bind + | expr-list "=" expr-list + | "return" expr-list? + )? + ";" + ; + +unary-op + : "^" /* Pointer */ + | "~" + | "+" + | "-" + | "&" + ; + +binary-op + : "+" + | "-" + | "*" + | "/" + | "%" /* Remainder */ + | "%%" /* Modulus */ + | "~" /* XOR */ + | "&" + | "|" + | "&~" /* Bitwise-ANDNOT */ + | "<<" + | "<<<" /* Left Rotate */ + | ">>" + | ">>>" /* Right Rotate */ + | "&&" + | "||" + | "==" + | "!=" + | "<=" + | ">=" + | "<" + | ">" + ; + +ident: xid-start xid-continue*; +xid-start + : \p{XID_Start} + | "_" + | "$" + ; +xid-continue + : \p{XID_Continue} + | "′" + | "″" + | "‴" + | "⁗" + ; + +string: "\"" string-part+ "\""; +string-part: [^"] | "\\\""; + +number + : number-bin + | number-oct + | number-dec + | number-hex + ; + +number-bin: "#b" number-rest<[01]>; +number-oct: "#o" number-rest<[01234567>; +number-dec: "#d"? number-rest<[0123456789]>; +number-hex: "#x" number-rest<[0123456789ABCDEF]>; + +(* is the alphabet *) +number-rest: number-word? ("." number-word?)? ("e" [-+] number-word)?; +number-word: (( | "'")* )?; -- cgit v1.2.3