#+TITLE: Guise Syntax Specification (BNF) #+AUTHOR: Guise Language Project #+DATE: 2025-01-04 * Introduction This document provides a formal BNF (Backus-Naur Form) grammar specification for the Guise programming language. Guise is a purely functional, statically-typed, stack-based language with S-expression syntax. * Notation Conventions - == : Non-terminal symbol - ="literal"= : Terminal symbol (literal text) - =|= : Alternative (or) - =*= : Zero or more repetitions - =+= : One or more repetitions - =?= : Optional (zero or one) - =()= : Grouping * Lexical Elements ** Whitespace #+BEGIN_SRC bnf ::= | | | ::= * #+END_SRC ** Comments #+BEGIN_SRC bnf ::= ";;" * #+END_SRC ** Identifiers #+BEGIN_SRC bnf ::= * ::= | ::= | | ::= "a" | "b" | ... | "z" | "A" | "B" | ... | "Z" ::= "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9" ::= "!" | "$" | "%" | "&" | "*" | "/" | ":" | "<" | "=" | ">" | "?" | "^" | "_" | "~" | "+" | "-" ::= | "." | "@" | "#" #+END_SRC Examples: =square=, =make-point=, =++=, =foo->bar=, =h:helper= ** Literals *** Numbers #+BEGIN_SRC bnf ::= ? ::= | "+" "i" | "-" "i" | "i" | "+" "i" | "-" "i" ::= ? ::= ? ::= | "/" | ::= | "." + | + "." * ::= + ::= ? ::= ("e" | "E") ? + ::= ? | ? ::= "#b" | "#B" ; binary | "#o" | "#O" ; octal | "#d" | "#D" ; decimal | "#x" | "#X" ; hexadecimal ::= "#e" | "#E" ; exact | "#i" | "#I" ; inexact ::= "+" | "-" ::= "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9" #+END_SRC Examples: - Decimal: =42=, =-17=, =3.14=, =2.5e10=, =-1.23e-4= - Binary: =#b11010= (26), =#b-1010= (-10) - Octal: =#o77= (63), =#o-755= (-493) - Hexadecimal: =#xab= (171), =#xFF= (255), =#x-1a= (-26) - Explicit decimal: =#d100= (100) - Rational: =1/2=, =22/7=, =-3/4= - Complex: =3+4i=, =-2-5i=, =1.5+2.5i= - Exactness: =#e3.14= (exact), =#i5= (inexact) - Combined: =#b#e1010= (exact binary) *** Strings #+BEGIN_SRC bnf ::= '"' * '"' ::= | ::= "\\" | "\"" | "\n" | "\t" | "\r" #+END_SRC Examples: ="hello"=, ="Hello, World!"=, ="line1\nline2"= *** Booleans #+BEGIN_SRC bnf ::= "#t" | "#f" | "#true" | "#false" #+END_SRC *** Characters #+BEGIN_SRC bnf ::= "#\\" | "#\\newline" | "#\\space" | "#\\tab" #+END_SRC Examples: =#\a=, =#\Z=, =#\newline=, =#\space= *** Symbols #+BEGIN_SRC bnf ::= #+END_SRC Note: In Guise, all identifiers are symbols when they appear as literals. ** Keywords #+BEGIN_SRC bnf ::= "define" | "module" | "import" | "public" | "private" | "quotation" | "match" | "let" | "sum" | "product" | "⊕" | "⊗" | "only" | "prefix" | "rename" | "except" #+END_SRC ** Type Markers #+BEGIN_SRC bnf ::= "->" ::= "⊕" | "sum" ::= "⊗" | "product" #+END_SRC * Expressions ** Program #+BEGIN_SRC bnf ::= * ::= | | | #+END_SRC ** Atomic Expressions #+BEGIN_SRC bnf ::= | | | | #+END_SRC ** Quotations #+BEGIN_SRC bnf ::= "'" "(" * ")" | "(" "quotation" * ")" #+END_SRC Examples: - ='(dup *)= - ='(1 +)= - ='()= - =(quotation dup dup * *)= ** List Literals #+BEGIN_SRC bnf ::= "#(" * ")" #+END_SRC Examples: - =#(1 2 3)= - =#("a" "b" "c")= - =#(#(1 2) #(3 4))= (nested) ** Sequences #+BEGIN_SRC bnf ::= "(" * ")" ::= | | | #+END_SRC * Type Annotations ** Type Variables #+BEGIN_SRC bnf ::= "'" * #+END_SRC Examples: ='a=, ='b=, ='result=, ='T= ** Primitive Types #+BEGIN_SRC bnf ::= "int" | "float" | "number" | "bool" | "string" | "char" | "symbol" #+END_SRC ** Compound Types #+BEGIN_SRC bnf ::= | | | ::= "(" "quot" "(" ")" ")" ::= "(" "list" ")" ::= "(" "tuple" + ")" ::= #+END_SRC Examples: - =(quot (int -> int))= - =(quot ('a -> 'a 'a))= - =(list int)= - =(tuple float float)= - =Point=, =Maybe= ** Type Expressions #+BEGIN_SRC bnf ::= | | #+END_SRC ** Stack Effects #+BEGIN_SRC bnf ::= * * #+END_SRC Examples: - =number -> number= - =int int -> int= - ='a -> 'a 'a= - ='a 'b -> 'b 'a= - =-> int= (nullary, produces int) - =int ->= (consumes int, produces nothing) * Definitions ** Word Definitions #+BEGIN_SRC bnf ::= "(" "define" ? "(" ")" * ")" ::= "public" | "private" #+END_SRC Examples: #+BEGIN_SRC scheme (define square (number -> number) (dup *)) (define public distance (Point Point -> float) (x. swap x. - dup * y. swap y. - dup * + sqrt)) (define private helper (int -> int) (2 *)) #+END_SRC ** Primitive Definitions #+BEGIN_SRC bnf ::= "(" "define" "primitive" "(" ")" ")" #+END_SRC Example: #+BEGIN_SRC scheme (define primitive dup ('a -> 'a 'a)) (define primitive + (number number -> number)) #+END_SRC ** Product Type Definitions (Tuples) #+BEGIN_SRC bnf ::= "(" "define" ? ? + * ")" ::= "(" ")" ::= "(" "=" ")" #+END_SRC Examples: #+BEGIN_SRC scheme ;; Implicit product type (define Point (x float) (y float)) ;; Explicit with ⊗ (define Vec3 ⊗ (x float) (y float) (z float)) ;; With explicit defaults (define Config (port int = 8080) (host string = "localhost") (debug bool = #f)) ;; Public tuple in module (define public Person (name string) (age int)) #+END_SRC ** Sum Type Definitions #+BEGIN_SRC bnf ::= "(" "define" ? + ")" ::= "(" * ")" #+END_SRC Examples: #+BEGIN_SRC scheme ;; Basic sum type with ⊕ (define Maybe ⊕ (Just 'a) (Nothing)) ;; Using 'sum' keyword (define Either sum (Left 'a) (Right 'b)) ;; Result type (define Result ⊕ (Ok 'ok) (Err 'err)) ;; Enum-style (define Color sum (Red) (Green) (Blue) (RGB int int int)) ;; Recursive type (define List ⊕ (Nil) (Cons 'a (List 'a))) #+END_SRC ** Module Definitions #+BEGIN_SRC bnf ::= "(" "define" "module" ? * ")" ::= | | | #+END_SRC Example: #+BEGIN_SRC scheme (define module math (import (helpers (prefix h:)) (numeric-types (only int float))) (define public square (number -> number) (dup *)) (define public cube (number -> number) (dup dup * *)) (define private internal-helper (int -> int) (2 *))) #+END_SRC * Module System ** Import Statements #+BEGIN_SRC bnf ::= "(" "import" + ")" ::= | "(" + ")" ::= | | | #+END_SRC ** Import Modifiers #+BEGIN_SRC bnf ::= "(" "only" + ")" ::= "(" "prefix" ")" ::= "(" "rename" + ")" ::= "(" "except" + ")" ::= "(" ")" #+END_SRC Examples: #+BEGIN_SRC scheme ;; Simple import (import stack-ops numeric-types) ;; Only specific bindings (import (math (only square cube))) ;; With prefix (import (helpers (prefix h:))) ;; With rename (import (geometry (rename (distance point-distance)))) ;; Multiple modifiers (import (numeric-types (only int float) (prefix num:))) ;; Except modifier (import (collections (except filter map))) ;; Complex example (import stack-ops (math (only square) (prefix m:)) (geometry (rename (distance dist))) (collections (except fold))) #+END_SRC * Pattern Matching (Future) ** Match Expressions #+BEGIN_SRC bnf ::= "(" "match" + ")" ::= "(" "→" * ")" ::= | | | | ::= | | ::= ::= "(" * ")" ::= "(" * ")" ::= "_" #+END_SRC Examples: #+BEGIN_SRC scheme ;; Match on Maybe (42 make-just match (Nothing → 0) ((Just x) → x)) ;; Match on list (my-list match (Nil → 0) ((Cons x xs) → (xs length 1 +))) ;; Match on custom tuple (point match ((Point x y) → (x y + 2 /))) #+END_SRC * Lexical Scoping (Future) ** Let Bindings #+BEGIN_SRC bnf ::= "(" "let" "(" + ")" * ")" ::= "(" ")" | "(" + ")" #+END_SRC Example: #+BEGIN_SRC scheme ;; Bind single value (5 let ((x)) (x x *)) ;; Bind multiple values (1 2 3 let ((z y x)) (x y + z *)) #+END_SRC * Complete Grammar Summary #+BEGIN_SRC bnf ;; ======================================== ;; LEXICAL ELEMENTS ;; ======================================== ::= * ::= | ::= '"' * '"' ::= "#t" | "#f" ::= "#\\" ::= ";;" ;; ======================================== ;; EXPRESSIONS ;; ======================================== ::= * ::= | | | ::= | | | ::= | | | | ::= "'" "(" * ")" | "(" "quotation" * ")" ::= "#(" * ")" ::= "(" * ")" ;; ======================================== ;; TYPES ;; ======================================== ::= | | ::= "'" ::= "int" | "float" | "number" | "bool" | "string" | "char" ::= | | | ::= * "->" * ;; ======================================== ;; DEFINITIONS ;; ======================================== ::= | | | ::= "(" "define" ? "(" ")" * ")" ::= "(" "define" "primitive" "(" ")" ")" ::= "(" "define" ? ? + ")" ::= "(" "define" ? + ")" ::= "(" "define" "module" ? * ")" ;; ======================================== ;; MODULE SYSTEM ;; ======================================== ::= "(" "import" + ")" ::= | "(" + ")" ::= "(" "only" + ")" | "(" "prefix" ")" | "(" "rename" + ")" | "(" "except" + ")" #+END_SRC * Examples ** Simple Expressions #+BEGIN_SRC scheme ;; Stack operations (5 3 +) ; → 8 (dup *) ; → square top of stack (swap drop) ; → keep second element ;; With quotations '(dup *) ; → push quotation (5 '(dup *) call) ; → 25 #+END_SRC ** Word Definitions #+BEGIN_SRC scheme ;; Basic word (define square (number -> number) (dup *)) ;; Multiple inputs/outputs (define pythagorean (number number -> number) (dup * swap dup * + sqrt)) ;; Higher-order (define twice ('a (quot ('a -> 'a)) -> 'a) (dup call call)) #+END_SRC ** Type Definitions #+BEGIN_SRC scheme ;; Product type (tuple) (define Point (x float) (y float)) ;; Sum type (tagged union) (define Maybe ⊕ (Just 'a) (Nothing)) ;; Using the types (3.0 4.0 make-point) ; Creates Point (42 make-just) ; Creates Maybe #+END_SRC ** Modules #+BEGIN_SRC scheme ;; Define a module (define module math (import (helpers (prefix h:))) (define public square (number -> number) (dup *)) (define private helper (number -> number) (2 *))) ;; Use a module (import math) (5 square) ; → 25 #+END_SRC